Be consistent in doc example.
[autoconf.git] / doc / autoconf.texi
blobda4fa27389095407ca335cf8f223ad1f75d78d0e
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)
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{]}@c
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{]}@c
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 a cache variable index.
56 @defcodeindex CA
57 @c Define an Autoconf macro index that @defmac doesn't write to.
58 @defcodeindex AC
59 @c Define an Autotest macro index that @defmac doesn't write to.
60 @defcodeindex AT
61 @c Define an M4sugar macro index that @defmac doesn't write to.
62 @defcodeindex MS
63 @c Define an index for *foreign* programs: `mv' etc.  Used for the
64 @c portability sections and so on.
65 @defindex pr
67 @c shortindexflag
68 @c --------------
69 @c Shall we factor AC_ out of the Autoconf macro index etc.?
70 @iftex
71 @set shortindexflag
72 @end iftex
74 @c @acindex{MACRO}
75 @c ---------------
76 @c Registering an AC_\MACRO\.
77 @ifset shortindexflag
78 @macro acindex{macro}
79 @ACindex \macro\
81 @end macro
82 @end ifset
83 @ifclear shortindexflag
84 @macro acindex{macro}
85 @ACindex AC_\macro\
86 @end macro
87 @end ifclear
89 @c @ahindex{MACRO}
90 @c ---------------
91 @c Registering an AH_\MACRO\.
92 @macro ahindex{macro}
93 @ACindex AH_\macro\
95 @end macro
97 @c @asindex{MACRO}
98 @c ---------------
99 @c Registering an AS_\MACRO\.
100 @ifset shortindexflag
101 @macro asindex{macro}
102 @MSindex \macro\
104 @end macro
105 @end ifset
106 @ifclear shortindexflag
107 @macro asindex{macro}
108 @MSindex AS_\macro\
109 @end macro
110 @end ifclear
112 @c @atindex{MACRO}
113 @c ---------------
114 @c Registering an AT_\MACRO\.
115 @ifset shortindexflag
116 @macro atindex{macro}
117 @ATindex \macro\
119 @end macro
120 @end ifset
121 @ifclear shortindexflag
122 @macro atindex{macro}
123 @ATindex AT_\macro\
124 @end macro
125 @end ifclear
127 @c @auindex{MACRO}
128 @c ---------------
129 @c Registering an AU_\MACRO\.
130 @macro auindex{macro}
131 @ACindex AU_\macro\
133 @end macro
135 @c @hdrindex{MACRO}
136 @c ----------------
137 @c Indexing a header.
138 @macro hdrindex{macro}
139 @prindex @file{\macro\}
141 @end macro
143 @c @msindex{MACRO}
144 @c ---------------
145 @c Registering an m4_\MACRO\.
146 @ifset shortindexflag
147 @macro msindex{macro}
148 @MSindex \macro\
150 @end macro
151 @end ifset
152 @ifclear shortindexflag
153 @macro msindex{macro}
154 @MSindex m4_\macro\
155 @end macro
156 @end ifclear
159 @c @caindex{VARIABLE}
160 @c ------------------
161 @c Registering an ac_cv_\VARIABLE\ cache variable.
162 @ifset shortindexflag
163 @macro caindex{macro}
164 @CAindex \macro\
165 @end macro
166 @end ifset
167 @ifclear shortindexflag
168 @macro caindex{macro}
169 @CAindex ac_cv_\macro\
170 @end macro
171 @end ifclear
173 @c Define an index for functions: `alloca' etc.  Used for the
174 @c portability sections and so on.  We can't use `fn' (aka `fnindex),
175 @c since `@defmac' goes into it => we'd get all the macros too.
177 @c   FIXME: Aaarg!  It seems there are too many indices for TeX :(
179 @c   ! No room for a new @write .
180 @c   l.112 @defcodeindex fu
182 @c   so don't define yet another one :(  Just put some tags before each
183 @c   @prindex which is actually a @funindex.
185 @c   @defcodeindex fu
188 @c   @c Put the programs and functions into their own index.
189 @c   @syncodeindex fu pr
191 @comment %**end of header
192 @comment ========================================================
194 @copying
196 This manual (@value{UPDATED}) is for GNU Autoconf
197 (version @value{VERSION}),
198 a package for creating scripts to configure source code packages using
199 templates and an M4 macro package.
201 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
202 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
203 Foundation, Inc.
205 @quotation
206 Permission is granted to copy, distribute and/or modify this document
207 under the terms of the GNU Free Documentation License,
208 Version 1.3 or any later version published by the Free Software
209 Foundation; with no Invariant Sections, with the Front-Cover texts
210 being ``A GNU Manual,'' and with the Back-Cover Texts as in
211 (a) below.  A copy of the license is included in the section entitled
212 ``GNU Free Documentation License.''
214 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
215 modify this GNU manual.  Buying copies from the FSF
216 supports it in developing GNU and promoting software
217 freedom.''
218 @end quotation
219 @end copying
223 @dircategory Software development
224 @direntry
225 * Autoconf: (autoconf).         Create source code configuration scripts.
226 @end direntry
228 @dircategory Individual utilities
229 @direntry
230 * autoscan: (autoconf)autoscan Invocation.
231                                 Semi-automatic @file{configure.ac} writing
232 * ifnames: (autoconf)ifnames Invocation.        Listing conditionals in source.
233 * autoconf-invocation: (autoconf)autoconf Invocation.
234                                 How to create configuration scripts
235 * autoreconf: (autoconf)autoreconf Invocation.
236                                 Remaking multiple @command{configure} scripts
237 * autoheader: (autoconf)autoheader Invocation.
238                                 How to create configuration templates
239 * autom4te: (autoconf)autom4te Invocation.
240                                 The Autoconf executables backbone
241 * configure: (autoconf)configure Invocation.    Configuring a package.
242 * autoupdate: (autoconf)autoupdate Invocation.
243                                 Automatic update of @file{configure.ac}
244 * config.status: (autoconf)config.status Invocation. Recreating configurations.
245 * testsuite: (autoconf)testsuite Invocation.    Running an Autotest test suite.
246 @end direntry
248 @titlepage
249 @title Autoconf
250 @subtitle Creating Automatic Configuration Scripts
251 @subtitle for version @value{VERSION}, @value{UPDATED}
252 @author David MacKenzie
253 @author Ben Elliston
254 @author Akim Demaille
255 @page
256 @vskip 0pt plus 1filll
257 @insertcopying
258 @end titlepage
260 @contents
263 @ifnottex
264 @node Top
265 @top Autoconf
266 @insertcopying
267 @end ifnottex
269 @c The master menu, created with texinfo-master-menu, goes here.
271 @menu
272 * Introduction::                Autoconf's purpose, strengths, and weaknesses
273 * The GNU Build System::        A set of tools for portable software packages
274 * Making configure Scripts::    How to organize and produce Autoconf scripts
275 * Setup::                       Initialization and output
276 * Existing Tests::              Macros that check for particular features
277 * Writing Tests::               How to write new feature checks
278 * Results::                     What to do with results from feature checks
279 * Programming in M4::           Layers on top of which Autoconf is written
280 * Programming in M4sh::         Shell portability layer
281 * Writing Autoconf Macros::     Adding new macros to Autoconf
282 * Portable Shell::              Shell script portability pitfalls
283 * Portable Make::               Makefile portability pitfalls
284 * Portable C and C++::          C and C++ portability pitfalls
285 * Manual Configuration::        Selecting features that can't be guessed
286 * Site Configuration::          Local defaults for @command{configure}
287 * Running configure Scripts::   How to use the Autoconf output
288 * config.status Invocation::    Recreating a configuration
289 * Obsolete Constructs::         Kept for backward compatibility
290 * Using Autotest::              Creating portable test suites
291 * FAQ::                         Frequent Autoconf Questions, with answers
292 * History::                     History of Autoconf
293 * GNU Free Documentation License::  License for copying this manual
294 * Indices::                     Indices of symbols, concepts, etc.
296 @detailmenu
297  --- The Detailed Node Listing ---
299 The GNU Build System
301 * Automake::                    Escaping makefile hell
302 * Gnulib::                      The GNU portability library
303 * Libtool::                     Building libraries portably
304 * Pointers::                    More info on the GNU build system
306 Making @command{configure} Scripts
308 * Writing Autoconf Input::      What to put in an Autoconf input file
309 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
310 * ifnames Invocation::          Listing the conditionals in source code
311 * autoconf Invocation::         How to create configuration scripts
312 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
314 Writing @file{configure.ac}
316 * Shell Script Compiler::       Autoconf as solution of a problem
317 * Autoconf Language::           Programming in Autoconf
318 * Autoconf Input Layout::       Standard organization of @file{configure.ac}
320 Initialization and Output Files
322 * Initializing configure::      Option processing etc.
323 * Versioning::                  Dealing with Autoconf versions
324 * Notices::                     Copyright, version numbers in @command{configure}
325 * Input::                       Where Autoconf should find files
326 * Output::                      Outputting results from the configuration
327 * Configuration Actions::       Preparing the output based on results
328 * Configuration Files::         Creating output files
329 * Makefile Substitutions::      Using output variables in makefiles
330 * Configuration Headers::       Creating a configuration header file
331 * Configuration Commands::      Running arbitrary instantiation commands
332 * Configuration Links::         Links depending on the configuration
333 * Subdirectories::              Configuring independent packages together
334 * Default Prefix::              Changing the default installation prefix
336 Substitutions in Makefiles
338 * Preset Output Variables::     Output variables that are always set
339 * Installation Directory Variables::  Other preset output variables
340 * Changed Directory Variables:: Warnings about @file{datarootdir}
341 * Build Directories::           Supporting multiple concurrent compiles
342 * Automatic Remaking::          Makefile rules for configuring
344 Configuration Header Files
346 * Header Templates::            Input for the configuration headers
347 * autoheader Invocation::       How to create configuration templates
348 * Autoheader Macros::           How to specify CPP templates
350 Existing Tests
352 * Common Behavior::             Macros' standard schemes
353 * Alternative Programs::        Selecting between alternative programs
354 * Files::                       Checking for the existence of files
355 * Libraries::                   Library archives that might be missing
356 * Library Functions::           C library functions that might be missing
357 * Header Files::                Header files that might be missing
358 * Declarations::                Declarations that may be missing
359 * Structures::                  Structures or members that might be missing
360 * Types::                       Types that might be missing
361 * Compilers and Preprocessors::  Checking for compiling programs
362 * System Services::             Operating system services
363 * Posix Variants::              Special kludges for specific Posix variants
364 * Erlang Libraries::            Checking for the existence of Erlang libraries
366 Common Behavior
368 * Standard Symbols::            Symbols defined by the macros
369 * Default Includes::            Includes used by the generic macros
371 Alternative Programs
373 * Particular Programs::         Special handling to find certain programs
374 * Generic Programs::            How to find other programs
376 Library Functions
378 * Function Portability::        Pitfalls with usual functions
379 * Particular Functions::        Special handling to find certain functions
380 * Generic Functions::           How to find other functions
382 Header Files
384 * Header Portability::          Collected knowledge on common headers
385 * Particular Headers::          Special handling to find certain headers
386 * Generic Headers::             How to find other headers
388 Declarations
390 * Particular Declarations::     Macros to check for certain declarations
391 * Generic Declarations::        How to find other declarations
393 Structures
395 * Particular Structures::       Macros to check for certain structure members
396 * Generic Structures::          How to find other structure members
398 Types
400 * Particular Types::            Special handling to find certain types
401 * Generic Types::               How to find other types
403 Compilers and Preprocessors
405 * Specific Compiler Characteristics::  Some portability issues
406 * Generic Compiler Characteristics::  Language independent tests and features
407 * C Compiler::                  Checking its characteristics
408 * C++ Compiler::                Likewise
409 * Objective C Compiler::        Likewise
410 * Objective C++ Compiler::      Likewise
411 * Erlang Compiler and Interpreter::  Likewise
412 * Fortran Compiler::            Likewise
414 Writing Tests
416 * Language Choice::             Selecting which language to use for testing
417 * Writing Test Programs::       Forging source files for compilers
418 * Running the Preprocessor::    Detecting preprocessor symbols
419 * Running the Compiler::        Detecting language or header features
420 * Running the Linker::          Detecting library features
421 * Runtime::                     Testing for runtime features
422 * Systemology::                 A zoology of operating systems
423 * Multiple Cases::              Tests for several possible values
425 Writing Test Programs
427 * Guidelines::                  General rules for writing test programs
428 * Test Functions::              Avoiding pitfalls in test programs
429 * Generating Sources::          Source program boilerplate
431 Results of Tests
433 * Defining Symbols::            Defining C preprocessor symbols
434 * Setting Output Variables::    Replacing variables in output files
435 * Special Chars in Variables::  Characters to beware of in variables
436 * Caching Results::             Speeding up subsequent @command{configure} runs
437 * Printing Messages::           Notifying @command{configure} users
439 Caching Results
441 * Cache Variable Names::        Shell variables used in caches
442 * Cache Files::                 Files @command{configure} uses for caching
443 * Cache Checkpointing::         Loading and saving the cache file
445 Programming in M4
447 * M4 Quotation::                Protecting macros from unwanted expansion
448 * Using autom4te::              The Autoconf executables backbone
449 * Programming in M4sugar::      Convenient pure M4 macros
450 * Debugging via autom4te::      Figuring out what M4 was doing
452 Programming in M4sh
454 * Common Shell Constructs::     Portability layer for common shell constructs
455 * Polymorphic Variables::       Support for indirect variable names
456 * Initialization Macros::       Macros to establish a sane shell environment
457 * File Descriptor Macros::      File descriptor macros for input and output
459 M4 Quotation
461 * Active Characters::           Characters that change the behavior of M4
462 * One Macro Call::              Quotation and one macro call
463 * Quoting and Parameters::      M4 vs. shell parameters
464 * Quotation and Nested Macros::  Macros calling macros
465 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
466 * Quadrigraphs::                Another way to escape special characters
467 * Balancing Parentheses::       Dealing with unbalanced parentheses
468 * Quotation Rule Of Thumb::     One parenthesis, one quote
470 Using @command{autom4te}
472 * autom4te Invocation::         A GNU M4 wrapper
473 * Customizing autom4te::        Customizing the Autoconf package
475 Programming in M4sugar
477 * Redefined M4 Macros::         M4 builtins changed in M4sugar
478 * Diagnostic Macros::           Diagnostic messages from M4sugar
479 * Diversion support::           Diversions in M4sugar
480 * Conditional constructs::      Conditions in M4
481 * Looping constructs::          Iteration in M4
482 * Evaluation Macros::           More quotation and evaluation control
483 * Text processing Macros::      String manipulation in M4
484 * Number processing Macros::    Arithmetic computation in M4
485 * Set manipulation Macros::     Set manipulation in M4
486 * Forbidden Patterns::          Catching unexpanded macros
488 Writing Autoconf Macros
490 * Macro Definitions::           Basic format of an Autoconf macro
491 * Macro Names::                 What to call your new macros
492 * Reporting Messages::          Notifying @command{autoconf} users
493 * Dependencies Between Macros::  What to do when macros depend on other macros
494 * Obsoleting Macros::           Warning about old ways of doing things
495 * Coding Style::                Writing Autoconf macros @`a la Autoconf
497 Dependencies Between Macros
499 * Prerequisite Macros::         Ensuring required information
500 * Suggested Ordering::          Warning about possible ordering problems
501 * One-Shot Macros::             Ensuring a macro is called only once
503 Portable Shell Programming
505 * Shellology::                  A zoology of shells
506 * Here-Documents::              Quirks and tricks
507 * File Descriptors::            FDs and redirections
508 * File System Conventions::     File names
509 * Shell Pattern Matching::      Pattern matching
510 * Shell Substitutions::         Variable and command expansions
511 * Assignments::                 Varying side effects of assignments
512 * Parentheses::                 Parentheses in shell scripts
513 * Slashes::                     Slashes in shell scripts
514 * Special Shell Variables::     Variables you should not change
515 * Shell Functions::             What to look out for if you use them
516 * Limitations of Builtins::     Portable use of not so portable /bin/sh
517 * Limitations of Usual Tools::  Portable use of portable tools
519 Portable Make Programming
521 * $< in Ordinary Make Rules::   $< in ordinary rules
522 * Failure in Make Rules::       Failing portably in rules
523 * Special Chars in Names::      Special Characters in Macro Names
524 * Backslash-Newline-Newline::   Empty last lines in macro definitions
525 * Backslash-Newline Comments::  Spanning comments across line boundaries
526 * Long Lines in Makefiles::     Line length limitations
527 * Macros and Submakes::         @code{make macro=value} and submakes
528 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
529 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
530 * Parallel Make::               Parallel @command{make} quirks
531 * Comments in Make Rules::      Other problems with Make comments
532 * obj/ and Make::               Don't name a subdirectory @file{obj}
533 * make -k Status::              Exit status of @samp{make -k}
534 * VPATH and Make::              @code{VPATH} woes
535 * Single Suffix Rules::         Single suffix rules and separated dependencies
536 * Timestamps and Make::         Subsecond timestamp resolution
538 @code{VPATH} and Make
540 * Variables listed in VPATH::   @code{VPATH} must be literal on ancient hosts
541 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
542 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
543 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
544 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
545 * Make Target Lookup::          More details about @code{VPATH} lookup
547 Portable C and C++ Programming
549 * Varieties of Unportability::  How to make your programs unportable
550 * Integer Overflow::            When integers get too large
551 * Preprocessor Arithmetic::     @code{#if} expression problems
552 * Null Pointers::               Properties of null pointers
553 * Buffer Overruns::             Subscript errors and the like
554 * Volatile Objects::            @code{volatile} and signals
555 * Floating Point Portability::  Portable floating-point arithmetic
556 * Exiting Portably::            Exiting and the exit status
558 Manual Configuration
560 * Specifying Target Triplets::  Specifying target triplets
561 * Canonicalizing::              Getting the canonical system type
562 * Using System Type::           What to do with the system type
564 Site Configuration
566 * Help Formatting::             Customizing @samp{configure --help}
567 * External Software::           Working with other optional software
568 * Package Options::             Selecting optional features
569 * Pretty Help Strings::         Formatting help string
570 * Option Checking::             Controlling checking of @command{configure} options
571 * Site Details::                Configuring site details
572 * Transforming Names::          Changing program names when installing
573 * Site Defaults::               Giving @command{configure} local defaults
575 Transforming Program Names When Installing
577 * Transformation Options::      @command{configure} options to transform names
578 * Transformation Examples::     Sample uses of transforming names
579 * Transformation Rules::        Makefile uses of transforming names
581 Running @command{configure} Scripts
583 * Basic Installation::          Instructions for typical cases
584 * Compilers and Options::       Selecting compilers and optimization
585 * Multiple Architectures::      Compiling for multiple architectures at once
586 * Installation Names::          Installing in different directories
587 * Optional Features::           Selecting optional features
588 * Particular Systems::          Particular systems
589 * System Type::                 Specifying the system type
590 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
591 * Defining Variables::          Specifying the compiler etc.
592 * configure Invocation::        Changing how @command{configure} runs
594 Obsolete Constructs
596 * Obsolete config.status Use::  Obsolete convention for @command{config.status}
597 * acconfig Header::             Additional entries in @file{config.h.in}
598 * autoupdate Invocation::       Automatic update of @file{configure.ac}
599 * Obsolete Macros::             Backward compatibility macros
600 * Autoconf 1::                  Tips for upgrading your files
601 * Autoconf 2.13::               Some fresher tips
603 Upgrading From Version 1
605 * Changed File Names::          Files you might rename
606 * Changed Makefiles::           New things to put in @file{Makefile.in}
607 * Changed Macros::              Macro calls you might replace
608 * Changed Results::             Changes in how to check test results
609 * Changed Macro Writing::       Better ways to write your own macros
611 Upgrading From Version 2.13
613 * Changed Quotation::           Broken code which used to work
614 * New Macros::                  Interaction with foreign macros
615 * Hosts and Cross-Compilation::  Bugward compatibility kludges
616 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
617 * AC_ACT_IFELSE vs AC_TRY_ACT::  A more generic scheme for testing sources
619 Generating Test Suites with Autotest
621 * Using an Autotest Test Suite::  Autotest and the user
622 * Writing Testsuites::          Autotest macros
623 * testsuite Invocation::        Running @command{testsuite} scripts
624 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
626 Using an Autotest Test Suite
628 * testsuite Scripts::           The concepts of Autotest
629 * Autotest Logs::               Their contents
631 Frequent Autoconf Questions, with answers
633 * Distributing::                Distributing @command{configure} scripts
634 * Why GNU M4::                  Why not use the standard M4?
635 * Bootstrapping::               Autoconf and GNU M4 require each other?
636 * Why Not Imake::               Why GNU uses @command{configure} instead of Imake
637 * Defining Directories::        Passing @code{datadir} to program
638 * Autom4te Cache::              What is it?  Can I remove it?
639 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
640 * Expanded Before Required::    Expanded Before Required
641 * Debugging::                   Debugging @command{configure} scripts
643 History of Autoconf
645 * Genesis::                     Prehistory and naming of @command{configure}
646 * Exodus::                      The plagues of M4 and Perl
647 * Leviticus::                   The priestly code of portability arrives
648 * Numbers::                     Growth and contributors
649 * Deuteronomy::                 Approaching the promises of easy configuration
651 Indices
653 * Environment Variable Index::  Index of environment variables used
654 * Output Variable Index::       Index of variables set in output files
655 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
656 * Cache Variable Index::        Index of documented cache variables
657 * Autoconf Macro Index::        Index of Autoconf macros
658 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
659 * Autotest Macro Index::        Index of Autotest macros
660 * Program & Function Index::    Index of those with portability problems
661 * Concept Index::               General index
663 @end detailmenu
664 @end menu
666 @c ============================================================= Introduction.
668 @node Introduction
669 @chapter Introduction
670 @cindex Introduction
672 @flushright
673 A physicist, an engineer, and a computer scientist were discussing the
674 nature of God.  ``Surely a Physicist,'' said the physicist, ``because
675 early in the Creation, God made Light; and you know, Maxwell's
676 equations, the dual nature of electromagnetic waves, the relativistic
677 consequences@enddots{}'' ``An Engineer!,'' said the engineer, ``because
678 before making Light, God split the Chaos into Land and Water; it takes a
679 hell of an engineer to handle that big amount of mud, and orderly
680 separation of solids from liquids@enddots{}'' The computer scientist
681 shouted: ``And the Chaos, where do you think it was coming from, hmm?''
683 ---Anonymous
684 @end flushright
685 @c (via Franc,ois Pinard)
687 Autoconf is a tool for producing shell scripts that automatically
688 configure software source code packages to adapt to many kinds of
689 Posix-like systems.  The configuration scripts produced by Autoconf
690 are independent of Autoconf when they are run, so their users do not
691 need to have Autoconf.
693 The configuration scripts produced by Autoconf require no manual user
694 intervention when run; they do not normally even need an argument
695 specifying the system type.  Instead, they individually test for the
696 presence of each feature that the software package they are for might need.
697 (Before each check, they print a one-line message stating what they are
698 checking for, so the user doesn't get too bored while waiting for the
699 script to finish.)  As a result, they deal well with systems that are
700 hybrids or customized from the more common Posix variants.  There is
701 no need to maintain files that list the features supported by each
702 release of each variant of Posix.
704 For each software package that Autoconf is used with, it creates a
705 configuration script from a template file that lists the system features
706 that the package needs or can use.  After the shell code to recognize
707 and respond to a system feature has been written, Autoconf allows it to
708 be shared by many software packages that can use (or need) that feature.
709 If it later turns out that the shell code needs adjustment for some
710 reason, it needs to be changed in only one place; all of the
711 configuration scripts can be regenerated automatically to take advantage
712 of the updated code.
714 @c "Those who do not understand Unix are condemned to reinvent it, poorly."
715 @c --Henry Spencer, 1987 (see http://en.wikipedia.org/wiki/Unix_philosophy)
716 Those who do not understand Autoconf are condemned to reinvent it, poorly.
717 The primary goal of Autoconf is making the @emph{user's} life easier;
718 making the @emph{maintainer's} life easier is only a secondary goal.
719 Put another way, the primary goal is not to make the generation of
720 @file{configure} automatic for package maintainers (although patches
721 along that front are welcome, since package maintainers form the user
722 base of Autoconf); rather, the goal is to make @file{configure}
723 painless, portable, and predictable for the end user of each
724 @dfn{autoconfiscated} package.  And to this degree, Autoconf is highly
725 successful at its goal --- most complaints to the Autoconf list are
726 about difficulties in writing Autoconf input, and not in the behavior of
727 the resulting @file{configure}.  Even packages that don't use Autoconf
728 will generally provide a @file{configure} script, and the most common
729 complaint about these alternative home-grown scripts is that they fail
730 to meet one or more of the GNU Coding Standards that users
731 have come to expect from Autoconf-generated @file{configure} scripts.
733 The Metaconfig package is similar in purpose to Autoconf, but the
734 scripts it produces require manual user intervention, which is quite
735 inconvenient when configuring large source trees.  Unlike Metaconfig
736 scripts, Autoconf scripts can support cross-compiling, if some care is
737 taken in writing them.
739 Autoconf does not solve all problems related to making portable
740 software packages---for a more complete solution, it should be used in
741 concert with other GNU build tools like Automake and
742 Libtool.  These other tools take on jobs like the creation of a
743 portable, recursive makefile with all of the standard targets,
744 linking of shared libraries, and so on.  @xref{The GNU Build System},
745 for more information.
747 Autoconf imposes some restrictions on the names of macros used with
748 @code{#if} in C programs (@pxref{Preprocessor Symbol Index}).
750 Autoconf requires GNU M4 version 1.4.6 or later in order to
751 generate the scripts.  It uses features that some versions of M4,
752 including GNU M4 1.3, do not have.  Autoconf works better
753 with GNU M4 version 1.4.14 or later, though this is not
754 required.
756 @xref{Autoconf 1}, for information about upgrading from version 1.
757 @xref{History}, for the story of Autoconf's development.  @xref{FAQ},
758 for answers to some common questions about Autoconf.
760 See the @uref{http://@/www.gnu.org/@/software/@/autoconf/,
761 Autoconf web page} for up-to-date information, details on the mailing
762 lists, pointers to a list of known bugs, etc.
764 Mail suggestions to @email{autoconf@@gnu.org, the Autoconf mailing
765 list}.  Past suggestions are
766 @uref{http://@/lists.gnu.org/@/archive/@/html/@/autoconf/, archived}.
768 Mail bug reports to @email{bug-autoconf@@gnu.org, the
769 Autoconf Bugs mailing list}.  Past bug reports are
770 @uref{http://@/lists.gnu.org/@/archive/@/html/@/bug-autoconf/, archived}.
772 If possible, first check that your bug is
773 not already solved in current development versions, and that it has not
774 been reported yet.  Be sure to include all the needed information and a
775 short @file{configure.ac} that demonstrates the problem.
777 Autoconf's development tree is accessible via @command{git}; see the
778 @uref{http://@/savannah.gnu.org/@/projects/@/autoconf/, Autoconf
779 Summary} for details, or view
780 @uref{http://@/git.sv.gnu.org/@/gitweb/@/?p=autoconf.git, the actual
781 repository}.  Anonymous CVS access is also available, see
782 @file{README} for more details.  Patches relative to the
783 current @command{git} version can be sent for review to the
784 @email{autoconf-patches@@gnu.org, Autoconf Patches mailing list}, with
785 discussion on prior patches
786 @uref{http://@/lists.gnu.org/@/archive/@/html/@/autoconf-@/patches/,
787 archived}; and all commits are posted in the read-only
788 @email{autoconf-commit@@gnu.org, Autoconf Commit mailing list}, which is
789 also @uref{http://@/lists.gnu.org/@/archive/@/html/@/autoconf-commit/,
790 archived}.
792 Because of its mission, the Autoconf package itself
793 includes only a set of often-used
794 macros that have already demonstrated their usefulness.  Nevertheless,
795 if you wish to share your macros, or find existing ones, see the
796 @uref{http://@/www.gnu.org/@/software/@/autoconf-archive/, Autoconf Macro
797 Archive}, which is kindly run by @email{simons@@cryp.to,
798 Peter Simons}.
801 @c ================================================= The GNU Build System
803 @node The GNU Build System
804 @chapter The GNU Build System
805 @cindex GNU build system
807 Autoconf solves an important problem---reliable discovery of
808 system-specific build and runtime information---but this is only one
809 piece of the puzzle for the development of portable software.  To this
810 end, the GNU project has developed a suite of integrated
811 utilities to finish the job Autoconf started: the GNU build
812 system, whose most important components are Autoconf, Automake, and
813 Libtool.  In this chapter, we introduce you to those tools, point you
814 to sources of more information, and try to convince you to use the
815 entire GNU build system for your software.
817 @menu
818 * Automake::                    Escaping makefile hell
819 * Gnulib::                      The GNU portability library
820 * Libtool::                     Building libraries portably
821 * Pointers::                    More info on the GNU build system
822 @end menu
824 @node Automake
825 @section Automake
827 The ubiquity of @command{make} means that a makefile is almost the
828 only viable way to distribute automatic build rules for software, but
829 one quickly runs into its numerous limitations.  Its lack of
830 support for automatic dependency tracking, recursive builds in
831 subdirectories, reliable timestamps (e.g., for network file systems), and
832 so on, mean that developers must painfully (and often incorrectly)
833 reinvent the wheel for each project.  Portability is non-trivial, thanks
834 to the quirks of @command{make} on many systems.  On top of all this is the
835 manual labor required to implement the many standard targets that users
836 have come to expect (@code{make install}, @code{make distclean},
837 @code{make uninstall}, etc.).  Since you are, of course, using Autoconf,
838 you also have to insert repetitive code in your @file{Makefile.in} to
839 recognize @code{@@CC@@}, @code{@@CFLAGS@@}, and other substitutions
840 provided by @command{configure}.  Into this mess steps @dfn{Automake}.
841 @cindex Automake
843 Automake allows you to specify your build needs in a @file{Makefile.am}
844 file with a vastly simpler and more powerful syntax than that of a plain
845 makefile, and then generates a portable @file{Makefile.in} for
846 use with Autoconf.  For example, the @file{Makefile.am} to build and
847 install a simple ``Hello world'' program might look like:
849 @example
850 bin_PROGRAMS = hello
851 hello_SOURCES = hello.c
852 @end example
854 @noindent
855 The resulting @file{Makefile.in} (~400 lines) automatically supports all
856 the standard targets, the substitutions provided by Autoconf, automatic
857 dependency tracking, @code{VPATH} building, and so on.  @command{make}
858 builds the @code{hello} program, and @code{make install} installs it
859 in @file{/usr/local/bin} (or whatever prefix was given to
860 @command{configure}, if not @file{/usr/local}).
862 The benefits of Automake increase for larger packages (especially ones
863 with subdirectories), but even for small programs the added convenience
864 and portability can be substantial.  And that's not all@enddots{}
866 @node Gnulib
867 @section Gnulib
869 GNU software has a well-deserved reputation for running on
870 many different types of systems.  While our primary goal is to write
871 software for the GNU system, many users and developers have
872 been introduced to us through the systems that they were already using.
874 @cindex Gnulib
875 Gnulib is a central location for common GNU code, intended to
876 be shared among free software packages.  Its components are typically
877 shared at the source level, rather than being a library that gets built,
878 installed, and linked against.  The idea is to copy files from Gnulib
879 into your own source tree.  There is no distribution tarball; developers
880 should just grab source modules from the repository.  The source files
881 are available online, under various licenses, mostly GNU
882 GPL or GNU LGPL.
884 Gnulib modules typically contain C source code along with Autoconf
885 macros used to configure the source code.  For example, the Gnulib
886 @code{stdbool} module implements a @file{stdbool.h} header that nearly
887 conforms to C99, even on old-fashioned hosts that lack @file{stdbool.h}.
888 This module contains a source file for the replacement header, along
889 with an Autoconf macro that arranges to use the replacement header on
890 old-fashioned systems.
892 @node Libtool
893 @section Libtool
895 Often, one wants to build not only programs, but libraries, so that
896 other programs can benefit from the fruits of your labor.  Ideally, one
897 would like to produce @emph{shared} (dynamically linked) libraries,
898 which can be used by multiple programs without duplication on disk or in
899 memory and can be updated independently of the linked programs.
900 Producing shared libraries portably, however, is the stuff of
901 nightmares---each system has its own incompatible tools, compiler flags,
902 and magic incantations.  Fortunately, GNU provides a solution:
903 @dfn{Libtool}.
904 @cindex Libtool
906 Libtool handles all the requirements of building shared libraries for
907 you, and at this time seems to be the @emph{only} way to do so with any
908 portability.  It also handles many other headaches, such as: the
909 interaction of Make rules with the variable suffixes of
910 shared libraries, linking reliably with shared libraries before they are
911 installed by the superuser, and supplying a consistent versioning system
912 (so that different versions of a library can be installed or upgraded
913 without breaking binary compatibility).  Although Libtool, like
914 Autoconf, can be used without Automake, it is most simply utilized in
915 conjunction with Automake---there, Libtool is used automatically
916 whenever shared libraries are needed, and you need not know its syntax.
918 @node Pointers
919 @section Pointers
921 Developers who are used to the simplicity of @command{make} for small
922 projects on a single system might be daunted at the prospect of
923 learning to use Automake and Autoconf.  As your software is
924 distributed to more and more users, however, you otherwise
925 quickly find yourself putting lots of effort into reinventing the
926 services that the GNU build tools provide, and making the
927 same mistakes that they once made and overcame.  (Besides, since
928 you're already learning Autoconf, Automake is a piece of cake.)
930 There are a number of places that you can go to for more information on
931 the GNU build tools.
933 @itemize @minus
935 @item Web
937 The project home pages for
938 @uref{http://@/www@/.gnu@/.org/@/software/@/autoconf/, Autoconf},
939 @uref{http://@/www@/.gnu@/.org/@/software/@/automake/, Automake},
940 @uref{http://@/www@/.gnu@/.org/@/software/@/gnulib/, Gnulib}, and
941 @uref{http://@/www@/.gnu@/.org/@/software/@/libtool/, Libtool}.
943 @item Automake Manual
945 @xref{Top, , Automake, automake, GNU Automake}, for more
946 information on Automake.
948 @item Books
950 The book @cite{GNU Autoconf, Automake and
951 Libtool}@footnote{@cite{GNU Autoconf, Automake and Libtool},
952 by G. V. Vaughan, B. Elliston, T. Tromey, and I. L. Taylor.  SAMS (originally
953 New Riders), 2000, ISBN 1578701902.} describes the complete GNU
954 build environment.  You can also find
955 @uref{http://@/sources.redhat.com/@/autobook/, the entire book on-line}.
957 @end itemize
959 @c ================================================= Making configure Scripts.
961 @node Making configure Scripts
962 @chapter Making @command{configure} Scripts
963 @cindex @file{aclocal.m4}
964 @cindex @command{configure}
966 The configuration scripts that Autoconf produces are by convention
967 called @command{configure}.  When run, @command{configure} creates several
968 files, replacing configuration parameters in them with appropriate
969 values.  The files that @command{configure} creates are:
971 @itemize @minus
972 @item
973 one or more @file{Makefile} files, usually one in each subdirectory of the
974 package (@pxref{Makefile Substitutions});
976 @item
977 optionally, a C header file, the name of which is configurable,
978 containing @code{#define} directives (@pxref{Configuration Headers});
980 @item
981 a shell script called @file{config.status} that, when run, recreates
982 the files listed above (@pxref{config.status Invocation});
984 @item
985 an optional shell script normally called @file{config.cache}
986 (created when using @samp{configure --config-cache}) that
987 saves the results of running many of the tests (@pxref{Cache Files});
989 @item
990 a file called @file{config.log} containing any messages produced by
991 compilers, to help debugging if @command{configure} makes a mistake.
992 @end itemize
994 @cindex @file{configure.in}
995 @cindex @file{configure.ac}
996 To create a @command{configure} script with Autoconf, you need to write an
997 Autoconf input file @file{configure.ac} (or @file{configure.in}) and run
998 @command{autoconf} on it.  If you write your own feature tests to
999 supplement those that come with Autoconf, you might also write files
1000 called @file{aclocal.m4} and @file{acsite.m4}.  If you use a C header
1001 file to contain @code{#define} directives, you might also run
1002 @command{autoheader}, and you can distribute the generated file
1003 @file{config.h.in} with the package.
1005 Here is a diagram showing how the files that can be used in
1006 configuration are produced.  Programs that are executed are suffixed by
1007 @samp{*}.  Optional files are enclosed in square brackets (@samp{[]}).
1008 @command{autoconf} and @command{autoheader} also read the installed Autoconf
1009 macro files (by reading @file{autoconf.m4}).
1011 @noindent
1012 Files used in preparing a software package for distribution, when using
1013 just Autoconf:
1014 @example
1015 your source files --> [autoscan*] --> [configure.scan] --> configure.ac
1017 @group
1018 configure.ac --.
1019                |   .------> autoconf* -----> configure
1020 [aclocal.m4] --+---+
1021                |   `-----> [autoheader*] --> [config.h.in]
1022 [acsite.m4] ---'
1023 @end group
1025 Makefile.in
1026 @end example
1028 @noindent
1029 Additionally, if you use Automake, the following additional productions
1030 come into play:
1032 @example
1033 @group
1034 [acinclude.m4] --.
1035                  |
1036 [local macros] --+--> aclocal* --> aclocal.m4
1037                  |
1038 configure.ac ----'
1039 @end group
1041 @group
1042 configure.ac --.
1043                +--> automake* --> Makefile.in
1044 Makefile.am ---'
1045 @end group
1046 @end example
1048 @noindent
1049 Files used in configuring a software package:
1050 @example
1051 @group
1052                        .-------------> [config.cache]
1053 configure* ------------+-------------> config.log
1054                        |
1055 [config.h.in] -.       v            .-> [config.h] -.
1056                +--> config.status* -+               +--> make*
1057 Makefile.in ---'                    `-> Makefile ---'
1058 @end group
1059 @end example
1061 @menu
1062 * Writing Autoconf Input::      What to put in an Autoconf input file
1063 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
1064 * ifnames Invocation::          Listing the conditionals in source code
1065 * autoconf Invocation::         How to create configuration scripts
1066 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
1067 @end menu
1069 @node Writing Autoconf Input
1070 @section Writing @file{configure.ac}
1072 To produce a @command{configure} script for a software package, create a
1073 file called @file{configure.ac} that contains invocations of the
1074 Autoconf macros that test the system features your package needs or can
1075 use.  Autoconf macros already exist to check for many features; see
1076 @ref{Existing Tests}, for their descriptions.  For most other features,
1077 you can use Autoconf template macros to produce custom checks; see
1078 @ref{Writing Tests}, for information about them.  For especially tricky
1079 or specialized features, @file{configure.ac} might need to contain some
1080 hand-crafted shell commands; see @ref{Portable Shell, , Portable Shell
1081 Programming}.  The @command{autoscan} program can give you a good start
1082 in writing @file{configure.ac} (@pxref{autoscan Invocation}, for more
1083 information).
1085 Previous versions of Autoconf promoted the name @file{configure.in},
1086 which is somewhat ambiguous (the tool needed to process this file is not
1087 described by its extension), and introduces a slight confusion with
1088 @file{config.h.in} and so on (for which @samp{.in} means ``to be
1089 processed by @command{configure}'').  Using @file{configure.ac} is now
1090 preferred.
1092 @menu
1093 * Shell Script Compiler::       Autoconf as solution of a problem
1094 * Autoconf Language::           Programming in Autoconf
1095 * Autoconf Input Layout::       Standard organization of @file{configure.ac}
1096 @end menu
1098 @node Shell Script Compiler
1099 @subsection A Shell Script Compiler
1101 Just as for any other computer language, in order to properly program
1102 @file{configure.ac} in Autoconf you must understand @emph{what} problem
1103 the language tries to address and @emph{how} it does so.
1105 The problem Autoconf addresses is that the world is a mess.  After all,
1106 you are using Autoconf in order to have your package compile easily on
1107 all sorts of different systems, some of them being extremely hostile.
1108 Autoconf itself bears the price for these differences: @command{configure}
1109 must run on all those systems, and thus @command{configure} must limit itself
1110 to their lowest common denominator of features.
1112 Naturally, you might then think of shell scripts; who needs
1113 @command{autoconf}?  A set of properly written shell functions is enough to
1114 make it easy to write @command{configure} scripts by hand.  Sigh!
1115 Unfortunately, even in 2008, where shells without any function support are
1116 far and few between, there are pitfalls to avoid when making use of them.
1117 Also, finding a Bourne shell that accepts shell functions is not trivial,
1118 even though there is almost always one on interesting porting targets.
1120 So, what is really needed is some kind of compiler, @command{autoconf},
1121 that takes an Autoconf program, @file{configure.ac}, and transforms it
1122 into a portable shell script, @command{configure}.
1124 How does @command{autoconf} perform this task?
1126 There are two obvious possibilities: creating a brand new language or
1127 extending an existing one.  The former option is attractive: all
1128 sorts of optimizations could easily be implemented in the compiler and
1129 many rigorous checks could be performed on the Autoconf program
1130 (e.g., rejecting any non-portable construct).  Alternatively, you can
1131 extend an existing language, such as the @code{sh} (Bourne shell)
1132 language.
1134 Autoconf does the latter: it is a layer on top of @code{sh}.  It was
1135 therefore most convenient to implement @command{autoconf} as a macro
1136 expander: a program that repeatedly performs @dfn{macro expansions} on
1137 text input, replacing macro calls with macro bodies and producing a pure
1138 @code{sh} script in the end.  Instead of implementing a dedicated
1139 Autoconf macro expander, it is natural to use an existing
1140 general-purpose macro language, such as M4, and implement the extensions
1141 as a set of M4 macros.
1144 @node Autoconf Language
1145 @subsection The Autoconf Language
1146 @cindex quotation
1148 The Autoconf language differs from many other computer
1149 languages because it treats actual code the same as plain text.  Whereas
1150 in C, for instance, data and instructions have different syntactic
1151 status, in Autoconf their status is rigorously the same.  Therefore, we
1152 need a means to distinguish literal strings from text to be expanded:
1153 quotation.
1155 When calling macros that take arguments, there must not be any white
1156 space between the macro name and the open parenthesis.
1158 @example
1159 AC_INIT ([oops], [1.0]) # incorrect
1160 AC_INIT([hello], [1.0]) # good
1161 @end example
1163 Arguments should
1164 be enclosed within the quote characters @samp{[} and @samp{]}, and be
1165 separated by commas.  Any leading blanks or newlines in arguments are ignored,
1166 unless they are quoted.  You should always quote an argument that
1167 might contain a macro name, comma, parenthesis, or a leading blank or
1168 newline.  This rule applies recursively for every macro
1169 call, including macros called from other macros.  For more details on
1170 quoting rules, see @ref{Programming in M4}.
1172 For instance:
1174 @example
1175 AC_CHECK_HEADER([stdio.h],
1176                 [AC_DEFINE([HAVE_STDIO_H], [1],
1177                    [Define to 1 if you have <stdio.h>.])],
1178                 [AC_MSG_ERROR([sorry, can't do anything for you])])
1179 @end example
1181 @noindent
1182 is quoted properly.  You may safely simplify its quotation to:
1184 @example
1185 AC_CHECK_HEADER([stdio.h],
1186                 [AC_DEFINE([HAVE_STDIO_H], 1,
1187                    [Define to 1 if you have <stdio.h>.])],
1188                 [AC_MSG_ERROR([sorry, can't do anything for you])])
1189 @end example
1191 @noindent
1192 because @samp{1} cannot contain a macro call.  Here, the argument of
1193 @code{AC_MSG_ERROR} must be quoted; otherwise, its comma would be
1194 interpreted as an argument separator.  Also, the second and third arguments
1195 of @samp{AC_CHECK_HEADER} must be quoted, since they contain
1196 macro calls.  The three arguments @samp{HAVE_STDIO_H}, @samp{stdio.h},
1197 and @samp{Define to 1 if you have <stdio.h>.} do not need quoting, but
1198 if you unwisely defined a macro with a name like @samp{Define} or
1199 @samp{stdio} then they would need quoting.  Cautious Autoconf users
1200 would keep the quotes, but many Autoconf users find such precautions
1201 annoying, and would rewrite the example as follows:
1203 @example
1204 AC_CHECK_HEADER(stdio.h,
1205                 [AC_DEFINE(HAVE_STDIO_H, 1,
1206                    [Define to 1 if you have <stdio.h>.])],
1207                 [AC_MSG_ERROR([sorry, can't do anything for you])])
1208 @end example
1210 @noindent
1211 This is safe, so long as you adopt good naming conventions and do not
1212 define macros with names like @samp{HAVE_STDIO_H}, @samp{stdio}, or
1213 @samp{h}.  Though it is also safe here to omit the quotes around
1214 @samp{Define to 1 if you have <stdio.h>.} this is not recommended, as
1215 message strings are more likely to inadvertently contain commas.
1217 The following example is wrong and dangerous, as it is underquoted:
1219 @example
1220 AC_CHECK_HEADER(stdio.h,
1221                 AC_DEFINE(HAVE_STDIO_H, 1,
1222                    Define to 1 if you have <stdio.h>.),
1223                 AC_MSG_ERROR([sorry, can't do anything for you]))
1224 @end example
1226 In other cases, you may have to use text that also resembles a macro
1227 call.  You must quote that text even when it is not passed as a macro
1228 argument.  For example, these two approaches in @file{configure.ac}
1229 (quoting just the potential problems, or quoting the entire line) will
1230 protect your script in case autoconf ever adds a macro @code{AC_DC}:
1232 @example
1233 echo "Hard rock was here!  --[AC_DC]"
1234 [echo "Hard rock was here!  --AC_DC"]
1235 @end example
1237 @noindent
1238 which results in this text in @file{configure}:
1240 @example
1241 echo "Hard rock was here!  --AC_DC"
1242 echo "Hard rock was here!  --AC_DC"
1243 @end example
1245 @noindent
1246 When you use the same text in a macro argument, you must therefore have
1247 an extra quotation level (since one is stripped away by the macro
1248 substitution).  In general, then, it is a good idea to @emph{use double
1249 quoting for all literal string arguments}, either around just the
1250 problematic portions, or over the entire argument:
1252 @example
1253 AC_MSG_WARN([[AC_DC] stinks  --Iron Maiden])
1254 AC_MSG_WARN([[AC_DC stinks  --Iron Maiden]])
1255 @end example
1257 However, the above example triggers a warning about a possibly
1258 unexpanded macro when running @command{autoconf}, because it collides
1259 with the namespace of macros reserved for the Autoconf language.  To be
1260 really safe, you can use additional escaping (either a quadrigraph, or
1261 creative shell constructs) to silence that particular warning:
1263 @example
1264 echo "Hard rock was here!  --AC""_DC"
1265 AC_MSG_WARN([[AC@@&t@@_DC stinks  --Iron Maiden]])
1266 @end example
1268 You are now able to understand one of the constructs of Autoconf that
1269 has been continually misunderstood@enddots{}  The rule of thumb is that
1270 @emph{whenever you expect macro expansion, expect quote expansion};
1271 i.e., expect one level of quotes to be lost.  For instance:
1273 @example
1274 AC_COMPILE_IFELSE([char b[10];], [], [AC_MSG_ERROR([you lose])])
1275 @end example
1277 @noindent
1278 is incorrect: here, the first argument of @code{AC_COMPILE_IFELSE} is
1279 @samp{char b[10];} and is expanded once, which results in
1280 @samp{char b10;}.  (There was an idiom common in Autoconf's past to
1281 address this issue via the M4 @code{changequote} primitive, but do not
1282 use it!)  Let's take a closer look: the author meant the first argument
1283 to be understood as a literal, and therefore it must be quoted twice:
1285 @example
1286 AC_COMPILE_IFELSE([[char b[10];]], [], [AC_MSG_ERROR([you lose])])
1287 @end example
1289 @noindent
1290 Voil@`a, you actually produce @samp{char b[10];} this time!
1292 On the other hand, descriptions (e.g., the last parameter of
1293 @code{AC_DEFINE} or @code{AS_HELP_STRING}) are not literals---they
1294 are subject to line breaking, for example---and should not be double quoted.
1295 Even if these descriptions are short and are not actually broken, double
1296 quoting them yields weird results.
1298 Some macros take optional arguments, which this documentation represents
1299 as @ovar{arg} (not to be confused with the quote characters).  You may
1300 just leave them empty, or use @samp{[]} to make the emptiness of the
1301 argument explicit, or you may simply omit the trailing commas.  The
1302 three lines below are equivalent:
1304 @example
1305 AC_CHECK_HEADERS([stdio.h], [], [], [])
1306 AC_CHECK_HEADERS([stdio.h],,,)
1307 AC_CHECK_HEADERS([stdio.h])
1308 @end example
1310 It is best to put each macro call on its own line in
1311 @file{configure.ac}.  Most of the macros don't add extra newlines; they
1312 rely on the newline after the macro call to terminate the commands.
1313 This approach makes the generated @command{configure} script a little
1314 easier to read by not inserting lots of blank lines.  It is generally
1315 safe to set shell variables on the same line as a macro call, because
1316 the shell allows assignments without intervening newlines.
1318 You can include comments in @file{configure.ac} files by starting them
1319 with the @samp{#}.  For example, it is helpful to begin
1320 @file{configure.ac} files with a line like this:
1322 @example
1323 # Process this file with autoconf to produce a configure script.
1324 @end example
1326 @node Autoconf Input Layout
1327 @subsection Standard @file{configure.ac} Layout
1329 The order in which @file{configure.ac} calls the Autoconf macros is not
1330 important, with a few exceptions.  Every @file{configure.ac} must
1331 contain a call to @code{AC_INIT} before the checks, and a call to
1332 @code{AC_OUTPUT} at the end (@pxref{Output}).  Additionally, some macros
1333 rely on other macros having been called first, because they check
1334 previously set values of some variables to decide what to do.  These
1335 macros are noted in the individual descriptions (@pxref{Existing
1336 Tests}), and they also warn you when @command{configure} is created if they
1337 are called out of order.
1339 To encourage consistency, here is a suggested order for calling the
1340 Autoconf macros.  Generally speaking, the things near the end of this
1341 list are those that could depend on things earlier in it.  For example,
1342 library functions could be affected by types and libraries.
1344 @display
1345 @group
1346 Autoconf requirements
1347 @code{AC_INIT(@var{package}, @var{version}, @var{bug-report-address})}
1348 information on the package
1349 checks for programs
1350 checks for libraries
1351 checks for header files
1352 checks for types
1353 checks for structures
1354 checks for compiler characteristics
1355 checks for library functions
1356 checks for system services
1357 @code{AC_CONFIG_FILES(@r{[}@var{file@dots{}}@r{]})}
1358 @code{AC_OUTPUT}
1359 @end group
1360 @end display
1363 @node autoscan Invocation
1364 @section Using @command{autoscan} to Create @file{configure.ac}
1365 @cindex @command{autoscan}
1367 The @command{autoscan} program can help you create and/or maintain a
1368 @file{configure.ac} file for a software package.  @command{autoscan}
1369 examines source files in the directory tree rooted at a directory given
1370 as a command line argument, or the current directory if none is given.
1371 It searches the source files for common portability problems and creates
1372 a file @file{configure.scan} which is a preliminary @file{configure.ac}
1373 for that package, and checks a possibly existing @file{configure.ac} for
1374 completeness.
1376 When using @command{autoscan} to create a @file{configure.ac}, you
1377 should manually examine @file{configure.scan} before renaming it to
1378 @file{configure.ac}; it probably needs some adjustments.
1379 Occasionally, @command{autoscan} outputs a macro in the wrong order
1380 relative to another macro, so that @command{autoconf} produces a warning;
1381 you need to move such macros manually.  Also, if you want the package to
1382 use a configuration header file, you must add a call to
1383 @code{AC_CONFIG_HEADERS} (@pxref{Configuration Headers}).  You might
1384 also have to change or add some @code{#if} directives to your program in
1385 order to make it work with Autoconf (@pxref{ifnames Invocation}, for
1386 information about a program that can help with that job).
1388 When using @command{autoscan} to maintain a @file{configure.ac}, simply
1389 consider adding its suggestions.  The file @file{autoscan.log}
1390 contains detailed information on why a macro is requested.
1392 @command{autoscan} uses several data files (installed along with Autoconf)
1393 to determine which macros to output when it finds particular symbols in
1394 a package's source files.  These data files all have the same format:
1395 each line consists of a symbol, one or more blanks, and the Autoconf macro to
1396 output if that symbol is encountered.  Lines starting with @samp{#} are
1397 comments.
1399 @command{autoscan} accepts the following options:
1401 @table @option
1402 @item --help
1403 @itemx -h
1404 Print a summary of the command line options and exit.
1406 @item --version
1407 @itemx -V
1408 Print the version number of Autoconf and exit.
1410 @item --verbose
1411 @itemx -v
1412 Print the names of the files it examines and the potentially interesting
1413 symbols it finds in them.  This output can be voluminous.
1415 @item --debug
1416 @itemx -d
1417 Don't remove temporary files.
1419 @item --include=@var{dir}
1420 @itemx -I @var{dir}
1421 Append @var{dir} to the include path.  Multiple invocations accumulate.
1423 @item --prepend-include=@var{dir}
1424 @itemx -B @var{dir}
1425 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1426 @end table
1428 @node ifnames Invocation
1429 @section Using @command{ifnames} to List Conditionals
1430 @cindex @command{ifnames}
1432 @command{ifnames} can help you write @file{configure.ac} for a software
1433 package.  It prints the identifiers that the package already uses in C
1434 preprocessor conditionals.  If a package has already been set up to have
1435 some portability, @command{ifnames} can thus help you figure out what its
1436 @command{configure} needs to check for.  It may help fill in some gaps in a
1437 @file{configure.ac} generated by @command{autoscan} (@pxref{autoscan
1438 Invocation}).
1440 @command{ifnames} scans all of the C source files named on the command line
1441 (or the standard input, if none are given) and writes to the standard
1442 output a sorted list of all the identifiers that appear in those files
1443 in @code{#if}, @code{#elif}, @code{#ifdef}, or @code{#ifndef}
1444 directives.  It prints each identifier on a line, followed by a
1445 space-separated list of the files in which that identifier occurs.
1447 @noindent
1448 @command{ifnames} accepts the following options:
1450 @table @option
1451 @item --help
1452 @itemx -h
1453 Print a summary of the command line options and exit.
1455 @item --version
1456 @itemx -V
1457 Print the version number of Autoconf and exit.
1458 @end table
1460 @node autoconf Invocation
1461 @section Using @command{autoconf} to Create @command{configure}
1462 @cindex @command{autoconf}
1464 To create @command{configure} from @file{configure.ac}, run the
1465 @command{autoconf} program with no arguments.  @command{autoconf} processes
1466 @file{configure.ac} with the M4 macro processor, using the
1467 Autoconf macros.  If you give @command{autoconf} an argument, it reads that
1468 file instead of @file{configure.ac} and writes the configuration script
1469 to the standard output instead of to @command{configure}.  If you give
1470 @command{autoconf} the argument @option{-}, it reads from the standard
1471 input instead of @file{configure.ac} and writes the configuration script
1472 to the standard output.
1474 The Autoconf macros are defined in several files.  Some of the files are
1475 distributed with Autoconf; @command{autoconf} reads them first.  Then it
1476 looks for the optional file @file{acsite.m4} in the directory that
1477 contains the distributed Autoconf macro files, and for the optional file
1478 @file{aclocal.m4} in the current directory.  Those files can contain
1479 your site's or the package's own Autoconf macro definitions
1480 (@pxref{Writing Autoconf Macros}, for more information).  If a macro is
1481 defined in more than one of the files that @command{autoconf} reads, the
1482 last definition it reads overrides the earlier ones.
1484 @command{autoconf} accepts the following options:
1486 @table @option
1487 @item --help
1488 @itemx -h
1489 Print a summary of the command line options and exit.
1491 @item --version
1492 @itemx -V
1493 Print the version number of Autoconf and exit.
1495 @item --verbose
1496 @itemx -v
1497 Report processing steps.
1499 @item --debug
1500 @itemx -d
1501 Don't remove the temporary files.
1503 @item --force
1504 @itemx -f
1505 Remake @file{configure} even if newer than its input files.
1507 @item --include=@var{dir}
1508 @itemx -I @var{dir}
1509 Append @var{dir} to the include path.  Multiple invocations accumulate.
1511 @item --prepend-include=@var{dir}
1512 @itemx -B @var{dir}
1513 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1515 @item --output=@var{file}
1516 @itemx -o @var{file}
1517 Save output (script or trace) to @var{file}.  The file @option{-} stands
1518 for the standard output.
1520 @item --warnings=@var{category}
1521 @itemx -W @var{category}
1522 @evindex WARNINGS
1523 Report the warnings related to @var{category} (which can actually be a
1524 comma separated list).  @xref{Reporting Messages}, macro
1525 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
1526 values include:
1528 @table @samp
1529 @item all
1530 report all the warnings
1532 @item none
1533 report none
1535 @item error
1536 treats warnings as errors
1538 @item no-@var{category}
1539 disable warnings falling into @var{category}
1540 @end table
1542 Warnings about @samp{syntax} are enabled by default, and the environment
1543 variable @env{WARNINGS}, a comma separated list of categories, is
1544 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1545 you had passed @option{--warnings syntax,$WARNINGS,@var{category}}.  To
1546 disable the defaults and @env{WARNINGS}, and then
1547 enable warnings about obsolete constructs, use @option{-W
1548 none,obsolete}.
1550 @cindex Back trace
1551 @cindex Macro invocation stack
1552 Because @command{autoconf} uses @command{autom4te} behind the scenes, it
1553 displays a back trace for errors, but not for warnings; if you want
1554 them, just pass @option{-W error}.  @xref{autom4te Invocation}, for some
1555 examples.
1557 @item --trace=@var{macro}[:@var{format}]
1558 @itemx -t @var{macro}[:@var{format}]
1559 Do not create the @command{configure} script, but list the calls to
1560 @var{macro} according to the @var{format}.  Multiple @option{--trace}
1561 arguments can be used to list several macros.  Multiple @option{--trace}
1562 arguments for a single macro are not cumulative; instead, you should
1563 just make @var{format} as long as needed.
1565 The @var{format} is a regular string, with newlines if desired, and
1566 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}; see
1567 @ref{autom4te Invocation}, for details on the @var{format}.
1569 @item --initialization
1570 @itemx -i
1571 By default, @option{--trace} does not trace the initialization of the
1572 Autoconf macros (typically the @code{AC_DEFUN} definitions).  This
1573 results in a noticeable speedup, but can be disabled by this option.
1574 @end table
1577 It is often necessary to check the content of a @file{configure.ac}
1578 file, but parsing it yourself is extremely fragile and error-prone.  It
1579 is suggested that you rely upon @option{--trace} to scan
1580 @file{configure.ac}.  For instance, to find the list of variables that
1581 are substituted, use:
1583 @example
1584 @group
1585 $ @kbd{autoconf -t AC_SUBST}
1586 configure.ac:2:AC_SUBST:ECHO_C
1587 configure.ac:2:AC_SUBST:ECHO_N
1588 configure.ac:2:AC_SUBST:ECHO_T
1589 @i{More traces deleted}
1590 @end group
1591 @end example
1593 @noindent
1594 The example below highlights the difference between @samp{$@@},
1595 @samp{$*}, and @samp{$%}.
1597 @example
1598 @group
1599 $ @kbd{cat configure.ac}
1600 AC_DEFINE(This, is, [an
1601 [example]])
1602 $ @kbd{autoconf -t 'AC_DEFINE:@@: $@@}
1603 *: $*
1604 %: $%'
1605 @@: [This],[is],[an
1606 [example]]
1607 *: This,is,an
1608 [example]
1609 %: This:is:an [example]
1610 @end group
1611 @end example
1613 @noindent
1614 The @var{format} gives you a lot of freedom:
1616 @example
1617 @group
1618 $ @kbd{autoconf -t 'AC_SUBST:$$ac_subst@{"$1"@} = "$f:$l";'}
1619 $ac_subst@{"ECHO_C"@} = "configure.ac:2";
1620 $ac_subst@{"ECHO_N"@} = "configure.ac:2";
1621 $ac_subst@{"ECHO_T"@} = "configure.ac:2";
1622 @i{More traces deleted}
1623 @end group
1624 @end example
1626 @noindent
1627 A long @var{separator} can be used to improve the readability of complex
1628 structures, and to ease their parsing (for instance when no single
1629 character is suitable as a separator):
1631 @example
1632 @group
1633 $ @kbd{autoconf -t 'AM_MISSING_PROG:$@{|:::::|@}*'}
1634 ACLOCAL|:::::|aclocal|:::::|$missing_dir
1635 AUTOCONF|:::::|autoconf|:::::|$missing_dir
1636 AUTOMAKE|:::::|automake|:::::|$missing_dir
1637 @i{More traces deleted}
1638 @end group
1639 @end example
1641 @node autoreconf Invocation
1642 @section Using @command{autoreconf} to Update @command{configure} Scripts
1643 @cindex @command{autoreconf}
1645 Installing the various components of the GNU Build System can be
1646 tedious: running @command{autopoint} for Gettext, @command{automake} for
1647 @file{Makefile.in} etc.@: in each directory.  It may be needed either
1648 because some tools such as @command{automake} have been updated on your
1649 system, or because some of the sources such as @file{configure.ac} have
1650 been updated, or finally, simply in order to install the GNU Build
1651 System in a fresh tree.
1653 @command{autoreconf} runs @command{autoconf}, @command{autoheader},
1654 @command{aclocal}, @command{automake}, @command{libtoolize}, and
1655 @command{autopoint} (when appropriate) repeatedly to update the
1656 GNU Build System in the specified directories and their
1657 subdirectories (@pxref{Subdirectories}).  By default, it only remakes
1658 those files that are older than their sources.  The environment variables
1659 @env{AUTOCONF}, @env{AUTOHEADER}, @env{AUTOMAKE}, @env{ACLOCAL},
1660 @env{AUTOPOINT}, @env{LIBTOOLIZE}, @env{M4}, and @env{MAKE} may be used
1661 to override the invocation of the respective tools.
1663 If you install a new version of some tool, you can make
1664 @command{autoreconf} remake @emph{all} of the files by giving it the
1665 @option{--force} option.
1667 @xref{Automatic Remaking}, for Make rules to automatically
1668 rebuild @command{configure} scripts when their source files change.  That
1669 method handles the timestamps of configuration header templates
1670 properly, but does not pass @option{--autoconf-dir=@var{dir}} or
1671 @option{--localdir=@var{dir}}.
1673 @cindex Gettext
1674 @cindex @command{autopoint}
1675 Gettext supplies the @command{autopoint} command to add translation
1676 infrastructure to a source package.  If you use @command{autopoint},
1677 your @file{configure.ac} should invoke both @code{AM_GNU_GETTEXT} and
1678 @code{AM_GNU_GETTEXT_VERSION(@var{gettext-version})}.  @xref{autopoint
1679 Invocation, , Invoking the @code{autopoint} Program, gettext,
1680 GNU @code{gettext} utilities}, for further details.
1682 @noindent
1683 @command{autoreconf} accepts the following options:
1685 @table @option
1686 @item --help
1687 @itemx -h
1688 Print a summary of the command line options and exit.
1690 @item --version
1691 @itemx -V
1692 Print the version number of Autoconf and exit.
1694 @item --verbose
1695 @itemx -V
1696 Print the name of each directory @command{autoreconf} examines and the
1697 commands it runs.  If given two or more times, pass @option{--verbose}
1698 to subordinate tools that support it.
1700 @item --debug
1701 @itemx -d
1702 Don't remove the temporary files.
1704 @item --force
1705 @itemx -f
1706 Remake even @file{configure} scripts and configuration headers that are
1707 newer than their input files (@file{configure.ac} and, if present,
1708 @file{aclocal.m4}).
1710 @item --install
1711 @itemx -i
1712 Install the missing auxiliary files in the package.  By default, files
1713 are copied; this can be changed with @option{--symlink}.
1715 If deemed appropriate, this option triggers calls to
1716 @samp{automake --add-missing},
1717 @samp{libtoolize}, @samp{autopoint}, etc.
1719 @item --no-recursive
1720 Do not rebuild files in subdirectories to configure (see @ref{Subdirectories},
1721 macro @code{AC_CONFIG_SUBDIRS}).
1723 @item --symlink
1724 @itemx -s
1725 When used with @option{--install}, install symbolic links to the missing
1726 auxiliary files instead of copying them.
1728 @item --make
1729 @itemx -m
1730 When the directories were configured, update the configuration by
1731 running @samp{./config.status --recheck && ./config.status}, and then
1732 run @samp{make}.
1734 @item --include=@var{dir}
1735 @itemx -I @var{dir}
1736 Append @var{dir} to the include path.  Multiple invocations accumulate.
1737 Passed on to @command{aclocal}, @command{autoconf} and
1738 @command{autoheader} internally.
1740 @item --prepend-include=@var{dir}
1741 @itemx -B @var{dir}
1742 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1743 Passed on to @command{autoconf} and @command{autoheader} internally.
1745 @item --warnings=@var{category}
1746 @itemx -W @var{category}
1747 @evindex WARNINGS
1748 Report the warnings related to @var{category} (which can actually be a
1749 comma separated list).
1751 @table @samp
1752 @item cross
1753 related to cross compilation issues.
1755 @item obsolete
1756 report the uses of obsolete constructs.
1758 @item portability
1759 portability issues
1761 @item syntax
1762 dubious syntactic constructs.
1764 @item all
1765 report all the warnings
1767 @item none
1768 report none
1770 @item error
1771 treats warnings as errors
1773 @item no-@var{category}
1774 disable warnings falling into @var{category}
1775 @end table
1777 Warnings about @samp{syntax} are enabled by default, and the environment
1778 variable @env{WARNINGS}, a comma separated list of categories, is
1779 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1780 you had passed @option{--warnings syntax,$WARNINGS,@var{category}}.  To
1781 disable the defaults and @env{WARNINGS}, and then
1782 enable warnings about obsolete constructs, use @option{-W
1783 none,obsolete}.
1784 @end table
1786 If you want @command{autoreconf} to pass flags that are not listed here
1787 on to @command{aclocal}, set @code{ACLOCAL_AMFLAGS} in your @file{Makefile.am}.
1788 Due to a limitation in the Autoconf implementation these flags currently
1789 must be set on a single line in @file{Makefile.am}, without any
1790 backslash-newlines.
1792 @c ========================================= Initialization and Output Files.
1794 @node Setup
1795 @chapter Initialization and Output Files
1797 Autoconf-generated @command{configure} scripts need some information about
1798 how to initialize, such as how to find the package's source files and
1799 about the output files to produce.  The following sections describe the
1800 initialization and the creation of output files.
1802 @menu
1803 * Initializing configure::      Option processing etc.
1804 * Versioning::                  Dealing with Autoconf versions
1805 * Notices::                     Copyright, version numbers in @command{configure}
1806 * Input::                       Where Autoconf should find files
1807 * Output::                      Outputting results from the configuration
1808 * Configuration Actions::       Preparing the output based on results
1809 * Configuration Files::         Creating output files
1810 * Makefile Substitutions::      Using output variables in makefiles
1811 * Configuration Headers::       Creating a configuration header file
1812 * Configuration Commands::      Running arbitrary instantiation commands
1813 * Configuration Links::         Links depending on the configuration
1814 * Subdirectories::              Configuring independent packages together
1815 * Default Prefix::              Changing the default installation prefix
1816 @end menu
1818 @node Initializing configure
1819 @section Initializing @command{configure}
1821 Every @command{configure} script must call @code{AC_INIT} before doing
1822 anything else that produces output.  Calls to silent macros, such as
1823 @code{AC_DEFUN}, may also occur prior to @code{AC_INIT}, although these
1824 are generally used via @file{aclocal.m4}, since that is implicitly
1825 included before the start of @file{configure.ac}.  The only other
1826 required macro is @code{AC_OUTPUT} (@pxref{Output}).
1828 @anchor{AC_INIT}
1829 @defmac AC_INIT (@var{package}, @var{version}, @ovar{bug-report}, @
1830   @ovar{tarname}, @ovar{url})
1831 @acindex{INIT}
1832 Process any command-line arguments and perform various initializations
1833 and verifications.
1835 Set the name of the @var{package} and its @var{version}.  These are
1836 typically used in @option{--version} support, including that of
1837 @command{configure}.  The optional argument @var{bug-report} should be
1838 the email to which users should send bug reports.  The package
1839 @var{tarname} differs from @var{package}: the latter designates the full
1840 package name (e.g., @samp{GNU Autoconf}), while the former is meant for
1841 distribution tar ball names (e.g., @samp{autoconf}).  It defaults to
1842 @var{package} with @samp{GNU } stripped, lower-cased, and all characters
1843 other than alphanumerics and underscores are changed to @samp{-}.  If
1844 provided, @var{url} should be the home page for the package.
1846 It is preferable that the arguments of @code{AC_INIT} be static, i.e.,
1847 there should not be any shell computation, but they can be computed by
1850 The following M4 macros (e.g., @code{AC_PACKAGE_NAME}), output variables
1851 (e.g., @code{PACKAGE_NAME}), and preprocessor symbols (e.g.,
1852 @code{PACKAGE_NAME}), are defined by @code{AC_INIT}:
1854 @table @asis
1855 @item @code{AC_PACKAGE_NAME}, @code{PACKAGE_NAME}
1856 @acindex{PACKAGE_NAME}
1857 @ovindex PACKAGE_NAME
1858 @cvindex PACKAGE_NAME
1859 Exactly @var{package}.
1861 @item @code{AC_PACKAGE_TARNAME}, @code{PACKAGE_TARNAME}
1862 @acindex{PACKAGE_TARNAME}
1863 @ovindex PACKAGE_TARNAME
1864 @cvindex PACKAGE_TARNAME
1865 Exactly @var{tarname}, possibly generated from @var{package}.
1867 @item @code{AC_PACKAGE_VERSION}, @code{PACKAGE_VERSION}
1868 @acindex{PACKAGE_VERSION}
1869 @ovindex PACKAGE_VERSION
1870 @cvindex PACKAGE_VERSION
1871 Exactly @var{version}.
1873 @item @code{AC_PACKAGE_STRING}, @code{PACKAGE_STRING}
1874 @acindex{PACKAGE_STRING}
1875 @ovindex PACKAGE_STRING
1876 @cvindex PACKAGE_STRING
1877 Exactly @samp{@var{package} @var{version}}.
1879 @item @code{AC_PACKAGE_BUGREPORT}, @code{PACKAGE_BUGREPORT}
1880 @acindex{PACKAGE_BUGREPORT}
1881 @ovindex PACKAGE_BUGREPORT
1882 @cvindex PACKAGE_BUGREPORT
1883 Exactly @var{bug-report}, if one was provided.
1885 @item @code{AC_PACKAGE_URL}, @code{PACKAGE_URL}
1886 @acindex{PACKAGE_URL}
1887 @ovindex PACKAGE_URL
1888 @cvindex PACKAGE_URL
1889 Exactly @var{url}, if one was provided.  If @var{url} was empty, but
1890 @var{package} begins with @samp{GNU }, then this defaults to
1891 @samp{http://@/www.gnu.org/@/software/@/@var{tarname}/}, otherwise, no URL is
1892 assumed.
1893 @end table
1894 @end defmac
1896 If your @command{configure} script does its own option processing, it
1897 should inspect @samp{$@@} or @samp{$*} immediately after calling
1898 @code{AC_INIT}, because other Autoconf macros liberally use the
1899 @command{set} command to process strings, and this has the side effect
1900 of updating @samp{$@@} and @samp{$*}.  However, we suggest that you use
1901 standard macros like @code{AC_ARG_ENABLE} instead of attempting to
1902 implement your own option processing.  @xref{Site Configuration}.
1904 @node Versioning
1905 @section Dealing with Autoconf versions
1906 @cindex Autoconf version
1907 @cindex version, Autoconf
1909 The following optional macros can be used to help choose the minimum
1910 version of Autoconf that can successfully compile a given
1911 @file{configure.ac}.
1913 @defmac AC_PREREQ (@var{version})
1914 @acindex{PREREQ}
1915 @cindex Version
1916 Ensure that a recent enough version of Autoconf is being used.  If the
1917 version of Autoconf being used to create @command{configure} is
1918 earlier than @var{version}, print an error message to the standard
1919 error output and exit with failure (exit status is 63).  For example:
1921 @example
1922 AC_PREREQ([@value{VERSION}])
1923 @end example
1925 This macro may be used before @code{AC_INIT}.
1926 @end defmac
1928 @defmac AC_AUTOCONF_VERSION
1929 @acindex{AUTOCONF_VERSION}
1930 This macro was introduced in Autoconf 2.62.  It identifies the version
1931 of Autoconf that is currently parsing the input file, in a format
1932 suitable for @code{m4_version_compare} (@pxref{m4_version_compare}); in
1933 other words, for this release of Autoconf, its value is
1934 @samp{@value{VERSION}}.  One potential use of this macro is for writing
1935 conditional fallbacks based on when a feature was added to Autoconf,
1936 rather than using @code{AC_PREREQ} to require the newer version of
1937 Autoconf.  However, remember that the Autoconf philosophy favors feature
1938 checks over version checks.
1940 You should not expand this macro directly; use
1941 @samp{m4_defn([AC_AUTOCONF_VERSION])} instead.  This is because some
1942 users might
1943 have a beta version of Autoconf installed, with arbitrary letters
1944 included in its version string.  This means it is possible for the
1945 version string to contain the name of a defined macro, such that
1946 expanding @code{AC_AUTOCONF_VERSION} would trigger the expansion of that
1947 macro during rescanning, and change the version string to be different
1948 than what you intended to check.
1949 @end defmac
1951 @node Notices
1952 @section Notices in @command{configure}
1953 @cindex Notices in @command{configure}
1955 The following macros manage version numbers for @command{configure}
1956 scripts.  Using them is optional.
1958 @defmac AC_COPYRIGHT (@var{copyright-notice})
1959 @acindex{COPYRIGHT}
1960 @cindex Copyright Notice
1961 State that, in addition to the Free Software Foundation's copyright on
1962 the Autoconf macros, parts of your @command{configure} are covered by the
1963 @var{copyright-notice}.
1965 The @var{copyright-notice} shows up in both the head of
1966 @command{configure} and in @samp{configure --version}.
1967 @end defmac
1970 @defmac AC_REVISION (@var{revision-info})
1971 @acindex{REVISION}
1972 @cindex Revision
1973 Copy revision stamp @var{revision-info} into the @command{configure}
1974 script, with any dollar signs or double-quotes removed.  This macro lets
1975 you put a revision stamp from @file{configure.ac} into @command{configure}
1976 without RCS or CVS changing it when you check in
1977 @command{configure}.  That way, you can determine easily which revision of
1978 @file{configure.ac} a particular @command{configure} corresponds to.
1980 For example, this line in @file{configure.ac}:
1982 @c The @w prevents RCS from changing the example in the manual.
1983 @example
1984 AC_REVISION([@w{$}Revision: 1.30 $])
1985 @end example
1987 @noindent
1988 produces this in @command{configure}:
1990 @example
1991 #!/bin/sh
1992 # From configure.ac Revision: 1.30
1993 @end example
1994 @end defmac
1997 @node Input
1998 @section Finding @command{configure} Input
2000 @anchor{AC_CONFIG_SRCDIR}
2001 @defmac AC_CONFIG_SRCDIR (@var{unique-file-in-source-dir})
2002 @acindex{CONFIG_SRCDIR}
2003 @var{unique-file-in-source-dir} is some file that is in the package's
2004 source directory; @command{configure} checks for this file's existence to
2005 make sure that the directory that it is told contains the source code in
2006 fact does.  Occasionally people accidentally specify the wrong directory
2007 with @option{--srcdir}; this is a safety check.  @xref{configure
2008 Invocation}, for more information.
2009 @end defmac
2012 @c FIXME: Remove definitively once --install explained.
2014 @c Small packages may store all their macros in @code{aclocal.m4}.  As the
2015 @c set of macros grows, or for maintenance reasons, a maintainer may prefer
2016 @c to split the macros in several files.  In this case, Autoconf must be
2017 @c told which files to load, and in which order.
2019 @c @defmac AC_INCLUDE (@var{file}@dots{})
2020 @c @acindex{INCLUDE}
2021 @c @c FIXME: There is no longer shell globbing.
2022 @c Read the macro definitions that appear in the listed files.  A list of
2023 @c space-separated file names or shell globbing patterns is expected.  The
2024 @c files are read in the order they're listed.
2026 @c Because the order of definition of macros is important (only the last
2027 @c definition of a macro is used), beware that it is @code{AC_INIT} that
2028 @c loads @file{acsite.m4} and @file{aclocal.m4}.  Note that
2029 @c @code{AC_INCLUDE}ing a file before @code{AC_INIT} or within
2030 @c @file{aclocal.m4} is different from doing so after @code{AC_INIT}: in
2031 @c the latter case, non-macro lines from included files may end up in the
2032 @c @file{configure} script, whereas in the former case, they'd be discarded
2033 @c just like any text that appear before @code{AC_INIT}.
2034 @c @end defmac
2036 Packages that do manual configuration or use the @command{install} program
2037 might need to tell @command{configure} where to find some other shell
2038 scripts by calling @code{AC_CONFIG_AUX_DIR}, though the default places
2039 it looks are correct for most cases.
2041 @defmac AC_CONFIG_AUX_DIR (@var{dir})
2042 @acindex{CONFIG_AUX_DIR}
2043 Use the auxiliary build tools (e.g., @file{install-sh},
2044 @file{config.sub}, @file{config.guess}, Cygnus @command{configure},
2045 Automake and Libtool scripts, etc.)@: that are in directory @var{dir}.
2046 These are auxiliary files used in configuration.  @var{dir} can be
2047 either absolute or relative to @file{@var{srcdir}}.  The default is
2048 @file{@var{srcdir}} or @file{@var{srcdir}/..} or
2049 @file{@var{srcdir}/../..}, whichever is the first that contains
2050 @file{install-sh}.  The other files are not checked for, so that using
2051 @code{AC_PROG_INSTALL} does not automatically require distributing the
2052 other auxiliary files.  It checks for @file{install.sh} also, but that
2053 name is obsolete because some @command{make} have a rule that creates
2054 @file{install} from it if there is no makefile.
2056 The auxiliary directory is commonly named @file{build-aux}.
2057 If you need portability to DOS variants, do not name the
2058 auxiliary directory @file{aux}.  @xref{File System Conventions}.
2059 @end defmac
2061 @defmac AC_REQUIRE_AUX_FILE (@var{file})
2062 @acindex{REQUIRE_AUX_FILE}
2063 Declares that @var{file} is expected in the directory defined above.  In
2064 Autoconf proper, this macro does nothing: its sole purpose is to be
2065 traced by third-party tools to produce a list of expected auxiliary
2066 files.  For instance it is called by macros like @code{AC_PROG_INSTALL}
2067 (@pxref{Particular Programs}) or @code{AC_CANONICAL_BUILD}
2068 (@pxref{Canonicalizing}) to register the auxiliary files they need.
2069 @end defmac
2071 Similarly, packages that use @command{aclocal} should declare where
2072 local macros can be found using @code{AC_CONFIG_MACRO_DIR}.
2074 @defmac AC_CONFIG_MACRO_DIR (@var{dir})
2075 @acindex{CONFIG_MACRO_DIR}
2076 Specify @var{dir} as the location of additional local Autoconf macros.
2077 This macro is intended for use by future versions of commands like
2078 @command{autoreconf} that trace macro calls.  It should be called
2079 directly from @file{configure.ac} so that tools that install macros for
2080 @command{aclocal} can find the macros' declarations.
2082 Note that if you use @command{aclocal} from Automake to generate
2083 @file{aclocal.m4}, you must also set @code{ACLOCAL_AMFLAGS = -I
2084 @var{dir}} in your top-level @file{Makefile.am}.  Due to a limitation in
2085 the Autoconf implementation of @command{autoreconf}, these include
2086 directives currently must be set on a single line in @file{Makefile.am},
2087 without any backslash-newlines.
2088 @end defmac
2091 @node Output
2092 @section Outputting Files
2093 @cindex Outputting files
2095 Every Autoconf script, e.g., @file{configure.ac}, should finish by
2096 calling @code{AC_OUTPUT}.  That is the macro that generates and runs
2097 @file{config.status}, which in turn creates the makefiles and any
2098 other files resulting from configuration.  This is the only required
2099 macro besides @code{AC_INIT} (@pxref{Input}).
2101 @anchor{AC_OUTPUT}
2102 @defmac AC_OUTPUT
2103 @acindex{OUTPUT}
2104 @cindex Instantiation
2105 Generate @file{config.status} and launch it.  Call this macro once, at
2106 the end of @file{configure.ac}.
2108 @file{config.status} performs all the configuration actions: all the
2109 output files (see @ref{Configuration Files}, macro
2110 @code{AC_CONFIG_FILES}), header files (see @ref{Configuration Headers},
2111 macro @code{AC_CONFIG_HEADERS}), commands (see @ref{Configuration
2112 Commands}, macro @code{AC_CONFIG_COMMANDS}), links (see
2113 @ref{Configuration Links}, macro @code{AC_CONFIG_LINKS}), subdirectories
2114 to configure (see @ref{Subdirectories}, macro @code{AC_CONFIG_SUBDIRS})
2115 are honored.
2117 The location of your @code{AC_OUTPUT} invocation is the exact point
2118 where configuration actions are taken: any code afterwards is
2119 executed by @command{configure} once @command{config.status} was run.  If
2120 you want to bind actions to @command{config.status} itself
2121 (independently of whether @command{configure} is being run), see
2122 @ref{Configuration Commands, , Running Arbitrary Configuration
2123 Commands}.
2124 @end defmac
2126 Historically, the usage of @code{AC_OUTPUT} was somewhat different.
2127 @xref{Obsolete Macros}, for a description of the arguments that
2128 @code{AC_OUTPUT} used to support.
2131 If you run @command{make} in subdirectories, you should run it using the
2132 @command{make} variable @code{MAKE}.  Most versions of @command{make} set
2133 @code{MAKE} to the name of the @command{make} program plus any options it
2134 was given.  (But many do not include in it the values of any variables
2135 set on the command line, so those are not passed on automatically.)
2136 Some old versions of @command{make} do not set this variable.  The
2137 following macro allows you to use it even with those versions.
2139 @anchor{AC_PROG_MAKE_SET}
2140 @defmac AC_PROG_MAKE_SET
2141 @acindex{PROG_MAKE_SET}
2142 @ovindex SET_MAKE
2143 If the Make command, @code{$MAKE} if set or else @samp{make}, predefines
2144 @code{$(MAKE)}, define output variable @code{SET_MAKE} to be empty.
2145 Otherwise, define @code{SET_MAKE} to a macro definition that sets
2146 @code{$(MAKE)}, such as @samp{MAKE=make}.  Calls @code{AC_SUBST} for
2147 @code{SET_MAKE}.
2148 @end defmac
2150 If you use this macro, place a line like this in each @file{Makefile.in}
2151 that runs @command{MAKE} on other directories:
2153 @example
2154 @@SET_MAKE@@
2155 @end example
2159 @node Configuration Actions
2160 @section Performing Configuration Actions
2161 @cindex Configuration actions
2163 @file{configure} is designed so that it appears to do everything itself,
2164 but there is actually a hidden slave: @file{config.status}.
2165 @file{configure} is in charge of examining your system, but it is
2166 @file{config.status} that actually takes the proper actions based on the
2167 results of @file{configure}.  The most typical task of
2168 @file{config.status} is to @emph{instantiate} files.
2170 @acindex{CONFIG_@var{ITEMS}}
2171 This section describes the common behavior of the four standard
2172 instantiating macros: @code{AC_CONFIG_FILES}, @code{AC_CONFIG_HEADERS},
2173 @code{AC_CONFIG_COMMANDS} and @code{AC_CONFIG_LINKS}.  They all
2174 have this prototype:
2176 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
2177 @c awful.
2178 @example
2179 AC_CONFIG_@var{ITEMS}(@var{tag}@dots{}, @r{[}@var{commands}@r{]}, @r{[}@var{init-cmds}@r{]})
2180 @end example
2182 @noindent
2183 where the arguments are:
2185 @table @var
2186 @item tag@dots{}
2187 A blank-or-newline-separated list of tags, which are typically the names of
2188 the files to instantiate.
2190 You are encouraged to use literals as @var{tags}.  In particular, you
2191 should avoid
2193 @example
2194 @dots{} && my_foos="$my_foos fooo"
2195 @dots{} && my_foos="$my_foos foooo"
2196 AC_CONFIG_@var{ITEMS}([$my_foos])
2197 @end example
2199 @noindent
2200 and use this instead:
2202 @example
2203 @dots{} && AC_CONFIG_@var{ITEMS}([fooo])
2204 @dots{} && AC_CONFIG_@var{ITEMS}([foooo])
2205 @end example
2207 The macros @code{AC_CONFIG_FILES} and @code{AC_CONFIG_HEADERS} use
2208 special @var{tag} values: they may have the form @samp{@var{output}} or
2209 @samp{@var{output}:@var{inputs}}.  The file @var{output} is instantiated
2210 from its templates, @var{inputs} (defaulting to @samp{@var{output}.in}).
2212 @samp{AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk)]},
2213 for example, asks for
2214 the creation of the file @file{Makefile} that contains the expansion of the
2215 output variables in the concatenation of @file{boiler/top.mk} and
2216 @file{boiler/bot.mk}.
2218 The special value @samp{-} might be used to denote the standard output
2219 when used in @var{output}, or the standard input when used in the
2220 @var{inputs}.  You most probably don't need to use this in
2221 @file{configure.ac}, but it is convenient when using the command line
2222 interface of @file{./config.status}, see @ref{config.status Invocation},
2223 for more details.
2225 The @var{inputs} may be absolute or relative file names.  In the latter
2226 case they are first looked for in the build tree, and then in the source
2227 tree.  Input files should be text files, and a line length below 2000
2228 bytes should be safe.
2230 @item commands
2231 Shell commands output literally into @file{config.status}, and
2232 associated with a tag that the user can use to tell @file{config.status}
2233 which commands to run.  The commands are run each time a @var{tag}
2234 request is given to @file{config.status}, typically each time the file
2235 @file{@var{tag}} is created.
2237 The variables set during the execution of @command{configure} are
2238 @emph{not} available here: you first need to set them via the
2239 @var{init-cmds}.  Nonetheless the following variables are precomputed:
2241 @table @code
2242 @item srcdir
2243 The name of the top source directory, assuming that the working
2244 directory is the top build directory.  This
2245 is what the @command{configure} option @option{--srcdir} sets.
2247 @item ac_top_srcdir
2248 The name of the top source directory, assuming that the working
2249 directory is the current build directory.
2251 @item ac_top_build_prefix
2252 The name of the top build directory, assuming that the working
2253 directory is the current build directory.
2254 It can be empty, or else ends with a slash, so that you may concatenate
2257 @item ac_srcdir
2258 The name of the corresponding source directory, assuming that the
2259 working directory is the current build directory.
2261 @item tmp
2262 The name of a temporary directory within the build tree, which you
2263 can use if you need to create additional temporary files.  The
2264 directory is cleaned up when @command{config.status} is done or
2265 interrupted.  Please use package-specific file name prefixes to
2266 avoid clashing with files that @command{config.status} may use
2267 internally.
2268 @end table
2270 @noindent
2271 The @dfn{current} directory refers to the directory (or
2272 pseudo-directory) containing the input part of @var{tags}.  For
2273 instance, running
2275 @example
2276 AC_CONFIG_COMMANDS([deep/dir/out:in/in.in], [@dots{}], [@dots{}])
2277 @end example
2279 @noindent
2280  with @option{--srcdir=../package} produces the following values:
2282 @example
2283 # Argument of --srcdir
2284 srcdir='../package'
2285 # Reversing deep/dir
2286 ac_top_build_prefix='../../'
2287 # Concatenation of $ac_top_build_prefix and srcdir
2288 ac_top_srcdir='../../../package'
2289 # Concatenation of $ac_top_srcdir and deep/dir
2290 ac_srcdir='../../../package/deep/dir'
2291 @end example
2293 @noindent
2294 independently of @samp{in/in.in}.
2296 @item init-cmds
2297 Shell commands output @emph{unquoted} near the beginning of
2298 @file{config.status}, and executed each time @file{config.status} runs
2299 (regardless of the tag).  Because they are unquoted, for example,
2300 @samp{$var} is output as the value of @code{var}.  @var{init-cmds}
2301 is typically used by @file{configure} to give @file{config.status} some
2302 variables it needs to run the @var{commands}.
2304 You should be extremely cautious in your variable names: all the
2305 @var{init-cmds} share the same name space and may overwrite each other
2306 in unpredictable ways.  Sorry@enddots{}
2307 @end table
2309 All these macros can be called multiple times, with different
2310 @var{tag} values, of course!
2313 @node Configuration Files
2314 @section Creating Configuration Files
2315 @cindex Creating configuration files
2316 @cindex Configuration file creation
2318 Be sure to read the previous section, @ref{Configuration Actions}.
2320 @anchor{AC_CONFIG_FILES}
2321 @defmac AC_CONFIG_FILES (@var{file}@dots{}, @ovar{cmds}, @ovar{init-cmds})
2322 @acindex{CONFIG_FILES}
2323 Make @code{AC_OUTPUT} create each @file{@var{file}} by copying an input
2324 file (by default @file{@var{file}.in}), substituting the output variable
2325 values.
2326 @c Before we used to have this feature, which was later rejected
2327 @c because it complicates the writing of makefiles:
2328 @c If the file would be unchanged, it is left untouched, to preserve
2329 @c timestamp.
2330 This macro is one of the instantiating macros; see @ref{Configuration
2331 Actions}.  @xref{Makefile Substitutions}, for more information on using
2332 output variables.  @xref{Setting Output Variables}, for more information
2333 on creating them.  This macro creates the directory that the file is in
2334 if it doesn't exist.  Usually, makefiles are created this way,
2335 but other files, such as @file{.gdbinit}, can be specified as well.
2337 Typical calls to @code{AC_CONFIG_FILES} look like this:
2339 @example
2340 AC_CONFIG_FILES([Makefile src/Makefile man/Makefile X/Imakefile])
2341 AC_CONFIG_FILES([autoconf], [chmod +x autoconf])
2342 @end example
2344 You can override an input file name by appending to @var{file} a
2345 colon-separated list of input files.  Examples:
2347 @example
2348 AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk]
2349                 [lib/Makefile:boiler/lib.mk])
2350 @end example
2352 @noindent
2353 Doing this allows you to keep your file names acceptable to
2354 DOS variants, or
2355 to prepend and/or append boilerplate to the file.
2356 @end defmac
2360 @node Makefile Substitutions
2361 @section Substitutions in Makefiles
2362 @cindex Substitutions in makefiles
2363 @cindex Makefile substitutions
2365 Each subdirectory in a distribution that contains something to be
2366 compiled or installed should come with a file @file{Makefile.in}, from
2367 which @command{configure} creates a file @file{Makefile} in that directory.
2368 To create @file{Makefile}, @command{configure} performs a simple variable
2369 substitution, replacing occurrences of @samp{@@@var{variable}@@} in
2370 @file{Makefile.in} with the value that @command{configure} has determined
2371 for that variable.  Variables that are substituted into output files in
2372 this way are called @dfn{output variables}.  They are ordinary shell
2373 variables that are set in @command{configure}.  To make @command{configure}
2374 substitute a particular variable into the output files, the macro
2375 @code{AC_SUBST} must be called with that variable name as an argument.
2376 Any occurrences of @samp{@@@var{variable}@@} for other variables are
2377 left unchanged.  @xref{Setting Output Variables}, for more information
2378 on creating output variables with @code{AC_SUBST}.
2380 A software package that uses a @command{configure} script should be
2381 distributed with a file @file{Makefile.in}, but no makefile; that
2382 way, the user has to properly configure the package for the local system
2383 before compiling it.
2385 @xref{Makefile Conventions, , Makefile Conventions, standards, The
2386 GNU Coding Standards}, for more information on what to put in
2387 makefiles.
2389 @menu
2390 * Preset Output Variables::     Output variables that are always set
2391 * Installation Directory Variables::  Other preset output variables
2392 * Changed Directory Variables:: Warnings about @file{datarootdir}
2393 * Build Directories::           Supporting multiple concurrent compiles
2394 * Automatic Remaking::          Makefile rules for configuring
2395 @end menu
2397 @node Preset Output Variables
2398 @subsection Preset Output Variables
2399 @cindex Output variables
2401 Some output variables are preset by the Autoconf macros.  Some of the
2402 Autoconf macros set additional output variables, which are mentioned in
2403 the descriptions for those macros.  @xref{Output Variable Index}, for a
2404 complete list of output variables.  @xref{Installation Directory
2405 Variables}, for the list of the preset ones related to installation
2406 directories.  Below are listed the other preset ones, many of which are
2407 precious variables (@pxref{Setting Output Variables},
2408 @code{AC_ARG_VAR}).
2410 The preset variables which are available during @file{config.status}
2411 (@pxref{Configuration Actions}) may also be used during
2412 @command{configure} tests.  For example, it is permissible to reference
2413 @samp{$srcdir} when constructing a list of directories to pass via
2414 option @option{-I} during a compiler feature check.  When used in this
2415 manner, coupled with the fact that @command{configure} is always run
2416 from the top build directory, it is sufficient to use just
2417 @samp{$srcdir} instead of @samp{$top_srcdir}.
2419 @c Just say no to ASCII sorting!  We're humans, not computers.
2420 @c These variables are listed as they would be in a dictionary:
2421 @c actor
2422 @c Actress
2423 @c actress
2425 @defvar CFLAGS
2426 @evindex CFLAGS
2427 @ovindex CFLAGS
2428 Debugging and optimization options for the C compiler.  If it is not set
2429 in the environment when @command{configure} runs, the default value is set
2430 when you call @code{AC_PROG_CC} (or empty if you don't).  @command{configure}
2431 uses this variable when compiling or linking programs to test for C features.
2433 If a compiler option affects only the behavior of the preprocessor
2434 (e.g., @option{-D @var{name}}), it should be put into @code{CPPFLAGS}
2435 instead.  If it affects only the linker (e.g., @option{-L
2436 @var{directory}}), it should be put into @code{LDFLAGS} instead.  If it
2437 affects only the compiler proper, @code{CFLAGS} is the natural home for
2438 it.  If an option affects multiple phases of the compiler, though,
2439 matters get tricky.  One approach to put such options directly into
2440 @code{CC}, e.g., @code{CC='gcc -m64'}.  Another is to put them into both
2441 @code{CPPFLAGS} and @code{LDFLAGS}, but not into @code{CFLAGS}.
2443 However, remember that some @file{Makefile} variables are reserved by
2444 the GNU Coding Standards for the use of the ``user''---the person
2445 building the package.  For instance, @code{CFLAGS} is one such variable.
2447 Sometimes package developers are tempted to set user variables such as
2448 @code{CFLAGS} because it appears to make their job easier.  However, the
2449 package itself should never set a user variable, particularly not to
2450 include switches that are required for proper compilation of the
2451 package.  Since these variables are documented as being for the package
2452 builder, that person rightfully expects to be able to override any of
2453 these variables at build time.  If the package developer needs to add
2454 switches without interfering with the user, the proper way to do that is
2455 to introduce an additional variable.  Automake makes this easy by
2456 introducing @code{AM_CFLAGS} (@pxref{Flag Variables Ordering, , ,
2457 automake, GNU Automake}), but the concept is the same even if
2458 Automake is not used.
2459 @end defvar
2461 @defvar configure_input
2462 @ovindex configure_input
2463 A comment saying that the file was generated automatically by
2464 @command{configure} and giving the name of the input file.
2465 @code{AC_OUTPUT} adds a comment line containing this variable to the top
2466 of every makefile it creates.  For other files, you should
2467 reference this variable in a comment at the top of each input file.  For
2468 example, an input shell script should begin like this:
2470 @example
2471 #!/bin/sh
2472 # @@configure_input@@
2473 @end example
2475 @noindent
2476 The presence of that line also reminds people editing the file that it
2477 needs to be processed by @command{configure} in order to be used.
2478 @end defvar
2480 @defvar CPPFLAGS
2481 @evindex CPPFLAGS
2482 @ovindex CPPFLAGS
2483 Preprocessor options for the C, C++, Objective C, and Objective C++
2484 preprocessors and compilers.  If
2485 it is not set in the environment when @command{configure} runs, the default
2486 value is empty.  @command{configure} uses this variable when preprocessing
2487 or compiling programs to test for C, C++, Objective C, and Objective C++
2488 features.
2490 This variable's contents should contain options like @option{-I},
2491 @option{-D}, and @option{-U} that affect only the behavior of the
2492 preprocessor.  Please see the explanation of @code{CFLAGS} for what you
2493 can do if an option affects other phases of the compiler as well.
2495 Currently, @command{configure} always links as part of a single
2496 invocation of the compiler that also preprocesses and compiles, so it
2497 uses this variable also when linking programs.  However, it is unwise to
2498 depend on this behavior because the GNU Coding Standards do
2499 not require it and many packages do not use @code{CPPFLAGS} when linking
2500 programs.
2502 @xref{Special Chars in Variables}, for limitations that @code{CPPFLAGS}
2503 might run into.
2504 @end defvar
2506 @defvar CXXFLAGS
2507 @evindex CXXFLAGS
2508 @ovindex CXXFLAGS
2509 Debugging and optimization options for the C++ compiler.  It acts like
2510 @code{CFLAGS}, but for C++ instead of C.
2511 @end defvar
2513 @defvar DEFS
2514 @ovindex DEFS
2515 @option{-D} options to pass to the C compiler.  If @code{AC_CONFIG_HEADERS}
2516 is called, @command{configure} replaces @samp{@@DEFS@@} with
2517 @option{-DHAVE_CONFIG_H} instead (@pxref{Configuration Headers}).  This
2518 variable is not defined while @command{configure} is performing its tests,
2519 only when creating the output files.  @xref{Setting Output Variables}, for
2520 how to check the results of previous tests.
2521 @end defvar
2523 @defvar ECHO_C
2524 @defvarx ECHO_N
2525 @defvarx ECHO_T
2526 @ovindex ECHO_C
2527 @ovindex ECHO_N
2528 @ovindex ECHO_T
2529 How does one suppress the trailing newline from @command{echo} for
2530 question-answer message pairs?  These variables provide a way:
2532 @example
2533 echo $ECHO_N "And the winner is... $ECHO_C"
2534 sleep 100000000000
2535 echo "$@{ECHO_T@}dead."
2536 @end example
2538 @noindent
2539 Some old and uncommon @command{echo} implementations offer no means to
2540 achieve this, in which case @code{ECHO_T} is set to tab.  You might not
2541 want to use it.
2542 @end defvar
2544 @defvar ERLCFLAGS
2545 @evindex ERLCFLAGS
2546 @ovindex ERLCFLAGS
2547 Debugging and optimization options for the Erlang compiler.  If it is not set
2548 in the environment when @command{configure} runs, the default value is empty.
2549 @command{configure} uses this variable when compiling
2550 programs to test for Erlang features.
2551 @end defvar
2553 @defvar FCFLAGS
2554 @evindex FCFLAGS
2555 @ovindex FCFLAGS
2556 Debugging and optimization options for the Fortran compiler.  If it
2557 is not set in the environment when @command{configure} runs, the default
2558 value is set when you call @code{AC_PROG_FC} (or empty if you don't).
2559 @command{configure} uses this variable when compiling or linking
2560 programs to test for Fortran features.
2561 @end defvar
2563 @defvar FFLAGS
2564 @evindex FFLAGS
2565 @ovindex FFLAGS
2566 Debugging and optimization options for the Fortran 77 compiler.  If it
2567 is not set in the environment when @command{configure} runs, the default
2568 value is set when you call @code{AC_PROG_F77} (or empty if you don't).
2569 @command{configure} uses this variable when compiling or linking
2570 programs to test for Fortran 77 features.
2571 @end defvar
2573 @defvar LDFLAGS
2574 @evindex LDFLAGS
2575 @ovindex LDFLAGS
2576 Options for the linker.  If it is not set
2577 in the environment when @command{configure} runs, the default value is empty.
2578 @command{configure} uses this variable when linking programs to test for
2579 C, C++, Objective C, Objective C++, and Fortran features.
2581 This variable's contents should contain options like @option{-s} and
2582 @option{-L} that affect only the behavior of the linker.  Please see the
2583 explanation of @code{CFLAGS} for what you can do if an option also
2584 affects other phases of the compiler.
2586 Don't use this variable to pass library names
2587 (@option{-l}) to the linker; use @code{LIBS} instead.
2588 @end defvar
2590 @defvar LIBS
2591 @evindex LIBS
2592 @ovindex LIBS
2593 @option{-l} options to pass to the linker.  The default value is empty,
2594 but some Autoconf macros may prepend extra libraries to this variable if
2595 those libraries are found and provide necessary functions, see
2596 @ref{Libraries}.  @command{configure} uses this variable when linking
2597 programs to test for C, C++, Objective C, Objective C++, and Fortran
2598 features.
2599 @end defvar
2601 @defvar OBJCFLAGS
2602 @evindex OBJCFLAGS
2603 @ovindex OBJCFLAGS
2604 Debugging and optimization options for the Objective C compiler.  It
2605 acts like @code{CFLAGS}, but for Objective C instead of C.
2606 @end defvar
2608 @defvar OBJCXXFLAGS
2609 @evindex OBJCXXFLAGS
2610 @ovindex OBJCXXFLAGS
2611 Debugging and optimization options for the Objective C++ compiler.  It
2612 acts like @code{CXXFLAGS}, but for Objective C++ instead of C++.
2613 @end defvar
2615 @defvar builddir
2616 @ovindex builddir
2617 Rigorously equal to @samp{.}.  Added for symmetry only.
2618 @end defvar
2620 @defvar abs_builddir
2621 @ovindex abs_builddir
2622 Absolute name of @code{builddir}.
2623 @end defvar
2625 @defvar top_builddir
2626 @ovindex top_builddir
2627 The relative name of the top level of the current build tree.  In the
2628 top-level directory, this is the same as @code{builddir}.
2629 @end defvar
2631 @defvar top_build_prefix
2632 @ovindex top_build_prefix
2633 The relative name of the top level of the current build tree with final
2634 slash if nonemtpy.  This is the same as @code{top_builddir}, except that
2635 it contains zero or more runs of @code{../}, so it should not be
2636 appended with a slash for concatenation.  This helps for @command{make}
2637 implementations that otherwise do not treat @file{./file} and @file{file}
2638 as equal in the toplevel build directory.
2639 @end defvar
2641 @defvar abs_top_builddir
2642 @ovindex abs_top_builddir
2643 Absolute name of @code{top_builddir}.
2644 @end defvar
2646 @defvar srcdir
2647 @ovindex srcdir
2648 The name of the directory that contains the source code for
2649 that makefile.
2650 @end defvar
2652 @defvar abs_srcdir
2653 @ovindex abs_srcdir
2654 Absolute name of @code{srcdir}.
2655 @end defvar
2657 @defvar top_srcdir
2658 @ovindex top_srcdir
2659 The name of the top-level source code directory for the
2660 package.  In the top-level directory, this is the same as @code{srcdir}.
2661 @end defvar
2663 @defvar abs_top_srcdir
2664 @ovindex abs_top_srcdir
2665 Absolute name of @code{top_srcdir}.
2666 @end defvar
2668 @node Installation Directory Variables
2669 @subsection Installation Directory Variables
2670 @cindex Installation directories
2671 @cindex Directories, installation
2673 The following variables specify the directories for
2674 package installation, see @ref{Directory Variables, , Variables for
2675 Installation Directories, standards, The GNU Coding
2676 Standards}, for more information.  Each variable corresponds to an
2677 argument of @command{configure}; trailing slashes are stripped so that
2678 expressions such as @samp{$@{prefix@}/lib} expand with only one slash
2679 between directory names.  See the end of this section for
2680 details on when and how to use these variables.
2682 @defvar bindir
2683 @ovindex bindir
2684 The directory for installing executables that users run.
2685 @end defvar
2687 @defvar datadir
2688 @ovindex datadir
2689 The directory for installing idiosyncratic read-only
2690 architecture-independent data.
2691 @end defvar
2693 @defvar datarootdir
2694 @ovindex datarootdir
2695 The root of the directory tree for read-only architecture-independent
2696 data files.
2697 @end defvar
2699 @defvar docdir
2700 @ovindex docdir
2701 The directory for installing documentation files (other than Info and
2702 man).
2703 @end defvar
2705 @defvar dvidir
2706 @ovindex dvidir
2707 The directory for installing documentation files in DVI format.
2708 @end defvar
2710 @defvar exec_prefix
2711 @ovindex exec_prefix
2712 The installation prefix for architecture-dependent files.  By default
2713 it's the same as @code{prefix}.  You should avoid installing anything
2714 directly to @code{exec_prefix}.  However, the default value for
2715 directories containing architecture-dependent files should be relative
2716 to @code{exec_prefix}.
2717 @end defvar
2719 @defvar htmldir
2720 @ovindex htmldir
2721 The directory for installing HTML documentation.
2722 @end defvar
2724 @defvar includedir
2725 @ovindex includedir
2726 The directory for installing C header files.
2727 @end defvar
2729 @defvar infodir
2730 @ovindex infodir
2731 The directory for installing documentation in Info format.
2732 @end defvar
2734 @defvar libdir
2735 @ovindex libdir
2736 The directory for installing object code libraries.
2737 @end defvar
2739 @defvar libexecdir
2740 @ovindex libexecdir
2741 The directory for installing executables that other programs run.
2742 @end defvar
2744 @defvar localedir
2745 @ovindex localedir
2746 The directory for installing locale-dependent but
2747 architecture-independent data, such as message catalogs.  This directory
2748 usually has a subdirectory per locale.
2749 @end defvar
2751 @defvar localstatedir
2752 @ovindex localstatedir
2753 The directory for installing modifiable single-machine data.
2754 @end defvar
2756 @defvar mandir
2757 @ovindex mandir
2758 The top-level directory for installing documentation in man format.
2759 @end defvar
2761 @defvar oldincludedir
2762 @ovindex oldincludedir
2763 The directory for installing C header files for non-GCC compilers.
2764 @end defvar
2766 @defvar pdfdir
2767 @ovindex pdfdir
2768 The directory for installing PDF documentation.
2769 @end defvar
2771 @defvar prefix
2772 @ovindex prefix
2773 The common installation prefix for all files.  If @code{exec_prefix}
2774 is defined to a different value, @code{prefix} is used only for
2775 architecture-independent files.
2776 @end defvar
2778 @defvar psdir
2779 @ovindex psdir
2780 The directory for installing PostScript documentation.
2781 @end defvar
2783 @defvar sbindir
2784 @ovindex sbindir
2785 The directory for installing executables that system
2786 administrators run.
2787 @end defvar
2789 @defvar sharedstatedir
2790 @ovindex sharedstatedir
2791 The directory for installing modifiable architecture-independent data.
2792 @end defvar
2794 @defvar sysconfdir
2795 @ovindex sysconfdir
2796 The directory for installing read-only single-machine data.
2797 @end defvar
2800 Most of these variables have values that rely on @code{prefix} or
2801 @code{exec_prefix}.  It is deliberate that the directory output
2802 variables keep them unexpanded: typically @samp{@@datarootdir@@} is
2803 replaced by @samp{$@{prefix@}/share}, not @samp{/usr/local/share}, and
2804 @samp{@@datadir@@} is replaced by @samp{$@{datarootdir@}}.
2806 This behavior is mandated by the GNU Coding Standards, so that when
2807 the user runs:
2809 @table @samp
2810 @item make
2811 she can still specify a different prefix from the one specified to
2812 @command{configure}, in which case, if needed, the package should hard
2813 code dependencies corresponding to the make-specified prefix.
2815 @item make install
2816 she can specify a different installation location, in which case the
2817 package @emph{must} still depend on the location which was compiled in
2818 (i.e., never recompile when @samp{make install} is run).  This is an
2819 extremely important feature, as many people may decide to install all
2820 the files of a package grouped together, and then install links from
2821 the final locations to there.
2822 @end table
2824 In order to support these features, it is essential that
2825 @code{datarootdir} remains defined as @samp{$@{prefix@}/share},
2826 so that its value can be expanded based
2827 on the current value of @code{prefix}.
2829 A corollary is that you should not use these variables except in
2830 makefiles.  For instance, instead of trying to evaluate @code{datadir}
2831 in @file{configure} and hard-coding it in makefiles using
2832 e.g., @samp{AC_DEFINE_UNQUOTED([DATADIR], ["$datadir"], [Data directory.])},
2833 you should add
2834 @option{-DDATADIR='$(datadir)'} to your makefile's definition of
2835 @code{CPPFLAGS} (@code{AM_CPPFLAGS} if you are also using Automake).
2837 Similarly, you should not rely on @code{AC_CONFIG_FILES} to replace
2838 @code{bindir} and friends in your shell scripts and other files; instead,
2839 let @command{make} manage their replacement.  For instance Autoconf
2840 ships templates of its shell scripts ending with @samp{.in}, and uses a
2841 makefile snippet similar to the following to build scripts like
2842 @command{autoheader} and @command{autom4te}:
2844 @example
2845 @group
2846 edit = sed \
2847         -e 's|@@bindir[@@]|$(bindir)|g' \
2848         -e 's|@@pkgdatadir[@@]|$(pkgdatadir)|g' \
2849         -e 's|@@prefix[@@]|$(prefix)|g'
2850 @end group
2852 @group
2853 autoheader autom4te: Makefile
2854         rm -f $@@ $@@.tmp
2855         srcdir=''; \
2856           test -f ./$@@.in || srcdir=$(srcdir)/; \
2857           $(edit) $$@{srcdir@}$@@.in >$@@.tmp
2858 @c $$ restore font-lock
2859         chmod +x $@@.tmp
2860         chmod a-w $@@.tmp
2861         mv $@@.tmp $@@
2862 @end group
2864 @group
2865 autoheader: $(srcdir)/autoheader.in
2866 autom4te: $(srcdir)/autom4te.in
2867 @end group
2868 @end example
2870 Some details are noteworthy:
2872 @table @asis
2873 @item @samp{@@bindir[@@]}
2874 The brackets prevent @command{configure} from replacing
2875 @samp{@@bindir@@} in the Sed expression itself.
2876 Brackets are preferable to a backslash here, since
2877 Posix says @samp{\@@} is not portable.
2879 @item @samp{$(bindir)}
2880 Don't use @samp{@@bindir@@}!  Use the matching makefile variable
2881 instead.
2883 @item @samp{$(pkgdatadir)}
2884 The example takes advantage of the variable @samp{$(pkgdatadir)}
2885 provided by Automake; it is equivalent to @samp{$(datadir)/$(PACKAGE)}.
2887 @item @samp{/}
2888 Don't use @samp{/} in the Sed expressions that replace file names since
2889 most likely the
2890 variables you use, such as @samp{$(bindir)}, contain @samp{/}.
2891 Use a shell metacharacter instead, such as @samp{|}.
2893 @item special characters
2894 File names, file name components, and the value of @code{VPATH} should
2895 not contain shell metacharacters or white
2896 space.  @xref{Special Chars in Variables}.
2898 @item dependency on @file{Makefile}
2899 Since @code{edit} uses values that depend on the configuration specific
2900 values (@code{prefix}, etc.)@: and not only on @code{VERSION} and so forth,
2901 the output depends on @file{Makefile}, not @file{configure.ac}.
2903 @item @samp{$@@}
2904 The main rule is generic, and uses @samp{$@@} extensively to
2905 avoid the need for multiple copies of the rule.
2907 @item Separated dependencies and single suffix rules
2908 You can't use them!  The above snippet cannot be (portably) rewritten
2911 @example
2912 autoconf autoheader: Makefile
2913 @group
2914 .in:
2915         rm -f $@@ $@@.tmp
2916         $(edit) $< >$@@.tmp
2917         chmod +x $@@.tmp
2918         mv $@@.tmp $@@
2919 @end group
2920 @end example
2922 @xref{Single Suffix Rules}, for details.
2924 @item @samp{$(srcdir)}
2925 Be sure to specify the name of the source directory,
2926 otherwise the package won't support separated builds.
2927 @end table
2929 For the more specific installation of Erlang libraries, the following variables
2930 are defined:
2932 @defvar ERLANG_INSTALL_LIB_DIR
2933 @ovindex ERLANG_INSTALL_LIB_DIR
2934 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
2935 The common parent directory of Erlang library installation directories.
2936 This variable is set by calling the @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR}
2937 macro in @file{configure.ac}.
2938 @end defvar
2940 @defvar ERLANG_INSTALL_LIB_DIR_@var{library}
2941 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
2942 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
2943 The installation directory for Erlang library @var{library}.
2944 This variable is set by using the
2945 @samp{AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR}
2946 macro in @file{configure.ac}.
2947 @end defvar
2949 @xref{Erlang Libraries}, for details.
2952 @node Changed Directory Variables
2953 @subsection Changed Directory Variables
2954 @cindex @file{datarootdir}
2956 In Autoconf 2.60, the set of directory variables has changed, and the
2957 defaults of some variables have been adjusted
2958 (@pxref{Installation Directory Variables}) to changes in the
2959 GNU Coding Standards.  Notably, @file{datadir}, @file{infodir}, and
2960 @file{mandir} are now expressed in terms of @file{datarootdir}.  If you are
2961 upgrading from an earlier Autoconf version, you may need to adjust your files
2962 to ensure that the directory variables are substituted correctly
2963 (@pxref{Defining Directories}), and that a definition of @file{datarootdir} is
2964 in place.  For example, in a @file{Makefile.in}, adding
2966 @example
2967 datarootdir = @@datarootdir@@
2968 @end example
2970 @noindent
2971 is usually sufficient.  If you use Automake to create @file{Makefile.in},
2972 it will add this for you.
2974 To help with the transition, Autoconf warns about files that seem to use
2975 @code{datarootdir} without defining it.  In some cases, it then expands
2976 the value of @code{$datarootdir} in substitutions of the directory
2977 variables.  The following example shows such a warning:
2979 @example
2980 $ @kbd{cat configure.ac}
2981 AC_INIT
2982 AC_CONFIG_FILES([Makefile])
2983 AC_OUTPUT
2984 $ @kbd{cat Makefile.in}
2985 prefix = @@prefix@@
2986 datadir = @@datadir@@
2987 $ @kbd{autoconf}
2988 $ @kbd{configure}
2989 configure: creating ./config.status
2990 config.status: creating Makefile
2991 config.status: WARNING:
2992                Makefile.in seems to ignore the --datarootdir setting
2993 $ @kbd{cat Makefile}
2994 prefix = /usr/local
2995 datadir = $@{prefix@}/share
2996 @end example
2998 Usually one can easily change the file to accommodate both older and newer
2999 Autoconf releases:
3001 @example
3002 $ @kbd{cat Makefile.in}
3003 prefix = @@prefix@@
3004 datarootdir = @@datarootdir@@
3005 datadir = @@datadir@@
3006 $ @kbd{configure}
3007 configure: creating ./config.status
3008 config.status: creating Makefile
3009 $ @kbd{cat Makefile}
3010 prefix = /usr/local
3011 datarootdir = $@{prefix@}/share
3012 datadir = $@{datarootdir@}
3013 @end example
3015 @acindex{DATAROOTDIR_CHECKED}
3016 In some cases, however, the checks may not be able to detect that a suitable
3017 initialization of @code{datarootdir} is in place, or they may fail to detect
3018 that such an initialization is necessary in the output file.  If, after
3019 auditing your package, there are still spurious @file{configure} warnings about
3020 @code{datarootdir}, you may add the line
3022 @example
3023 AC_DEFUN([AC_DATAROOTDIR_CHECKED])
3024 @end example
3026 @noindent
3027 to your @file{configure.ac} to disable the warnings.  This is an exception
3028 to the usual rule that you should not define a macro whose name begins with
3029 @code{AC_} (@pxref{Macro Names}).
3033 @node Build Directories
3034 @subsection Build Directories
3035 @cindex Build directories
3036 @cindex Directories, build
3038 You can support compiling a software package for several architectures
3039 simultaneously from the same copy of the source code.  The object files
3040 for each architecture are kept in their own directory.
3042 To support doing this, @command{make} uses the @code{VPATH} variable to
3043 find the files that are in the source directory.  GNU Make
3044 can do this.  Most other recent @command{make} programs can do this as
3045 well, though they may have difficulties and it is often simpler to
3046 recommend GNU @command{make} (@pxref{VPATH and Make}).  Older
3047 @command{make} programs do not support @code{VPATH}; when using them, the
3048 source code must be in the same directory as the object files.
3050 If you are using GNU Automake, the remaining details in this
3051 section are already covered for you, based on the contents of your
3052 @file{Makefile.am}.  But if you are using Autoconf in isolation, then
3053 supporting @code{VPATH} requires the following in your
3054 @file{Makefile.in}:
3056 @example
3057 srcdir = @@srcdir@@
3058 VPATH = @@srcdir@@
3059 @end example
3061 Do not set @code{VPATH} to the value of another variable (@pxref{Variables
3062 listed in VPATH}.
3064 @command{configure} substitutes the correct value for @code{srcdir} when
3065 it produces @file{Makefile}.
3067 Do not use the @command{make} variable @code{$<}, which expands to the
3068 file name of the file in the source directory (found with @code{VPATH}),
3069 except in implicit rules.  (An implicit rule is one such as @samp{.c.o},
3070 which tells how to create a @file{.o} file from a @file{.c} file.)  Some
3071 versions of @command{make} do not set @code{$<} in explicit rules; they
3072 expand it to an empty value.
3074 Instead, Make command lines should always refer to source
3075 files by prefixing them with @samp{$(srcdir)/}.  For example:
3077 @example
3078 time.info: time.texinfo
3079         $(MAKEINFO) '$(srcdir)/time.texinfo'
3080 @end example
3082 @node Automatic Remaking
3083 @subsection Automatic Remaking
3084 @cindex Automatic remaking
3085 @cindex Remaking automatically
3087 You can put rules like the following in the top-level @file{Makefile.in}
3088 for a package to automatically update the configuration information when
3089 you change the configuration files.  This example includes all of the
3090 optional files, such as @file{aclocal.m4} and those related to
3091 configuration header files.  Omit from the @file{Makefile.in} rules for
3092 any of these files that your package does not use.
3094 The @samp{$(srcdir)/} prefix is included because of limitations in the
3095 @code{VPATH} mechanism.
3097 The @file{stamp-} files are necessary because the timestamps of
3098 @file{config.h.in} and @file{config.h} are not changed if remaking
3099 them does not change their contents.  This feature avoids unnecessary
3100 recompilation.  You should include the file @file{stamp-h.in} in your
3101 package's distribution, so that @command{make} considers
3102 @file{config.h.in} up to date.  Don't use @command{touch}
3103 (@pxref{touch, , Limitations of Usual Tools}); instead, use
3104 @command{echo} (using
3105 @command{date} would cause needless differences, hence CVS
3106 conflicts, etc.).
3108 @example
3109 @group
3110 $(srcdir)/configure: configure.ac aclocal.m4
3111         cd '$(srcdir)' && autoconf
3113 # autoheader might not change config.h.in, so touch a stamp file.
3114 $(srcdir)/config.h.in: stamp-h.in
3115 $(srcdir)/stamp-h.in: configure.ac aclocal.m4
3116         cd '$(srcdir)' && autoheader
3117         echo timestamp > '$(srcdir)/stamp-h.in'
3119 config.h: stamp-h
3120 stamp-h: config.h.in config.status
3121         ./config.status
3123 Makefile: Makefile.in config.status
3124         ./config.status
3126 config.status: configure
3127         ./config.status --recheck
3128 @end group
3129 @end example
3131 @noindent
3132 (Be careful if you copy these lines directly into your makefile, as you
3133 need to convert the indented lines to start with the tab character.)
3135 In addition, you should use
3137 @example
3138 AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h])
3139 @end example
3141 @noindent
3142 so @file{config.status} ensures that @file{config.h} is considered up to
3143 date.  @xref{Output}, for more information about @code{AC_OUTPUT}.
3145 @xref{config.status Invocation}, for more examples of handling
3146 configuration-related dependencies.
3148 @node Configuration Headers
3149 @section Configuration Header Files
3150 @cindex Configuration Header
3151 @cindex @file{config.h}
3153 When a package contains more than a few tests that define C preprocessor
3154 symbols, the command lines to pass @option{-D} options to the compiler
3155 can get quite long.  This causes two problems.  One is that the
3156 @command{make} output is hard to visually scan for errors.  More
3157 seriously, the command lines can exceed the length limits of some
3158 operating systems.  As an alternative to passing @option{-D} options to
3159 the compiler, @command{configure} scripts can create a C header file
3160 containing @samp{#define} directives.  The @code{AC_CONFIG_HEADERS}
3161 macro selects this kind of output.  Though it can be called anywhere
3162 between @code{AC_INIT} and @code{AC_OUTPUT}, it is customary to call
3163 it right after @code{AC_INIT}.
3165 The package should @samp{#include} the configuration header file before
3166 any other header files, to prevent inconsistencies in declarations (for
3167 example, if it redefines @code{const}).
3169 To provide for VPATH builds, remember to pass the C compiler a @option{-I.}
3170 option (or @option{-I..}; whichever directory contains @file{config.h}).
3171 Even if you use @samp{#include "config.h"}, the preprocessor searches only
3172 the directory of the currently read file, i.e., the source directory, not
3173 the build directory.
3175 With the appropriate @option{-I} option, you can use
3176 @samp{#include <config.h>}.  Actually, it's a good habit to use it,
3177 because in the rare case when the source directory contains another
3178 @file{config.h}, the build directory should be searched first.
3181 @defmac AC_CONFIG_HEADERS (@var{header} @dots{}, @ovar{cmds}, @ovar{init-cmds})
3182 @acindex{CONFIG_HEADERS}
3183 @cvindex HAVE_CONFIG_H
3184 This macro is one of the instantiating macros; see @ref{Configuration
3185 Actions}.  Make @code{AC_OUTPUT} create the file(s) in the
3186 blank-or-newline-separated list @var{header} containing C preprocessor
3187 @code{#define} statements, and replace @samp{@@DEFS@@} in generated
3188 files with @option{-DHAVE_CONFIG_H} instead of the value of @code{DEFS}.
3189 The usual name for @var{header} is @file{config.h}.
3191 If @var{header} already exists and its contents are identical to what
3192 @code{AC_OUTPUT} would put in it, it is left alone.  Doing this allows
3193 making some changes in the configuration without needlessly causing
3194 object files that depend on the header file to be recompiled.
3196 Usually the input file is named @file{@var{header}.in}; however, you can
3197 override the input file name by appending to @var{header} a
3198 colon-separated list of input files.  For example, you might need to make
3199 the input file name acceptable to DOS variants:
3201 @example
3202 AC_CONFIG_HEADERS([config.h:config.hin])
3203 @end example
3205 @end defmac
3207 @defmac AH_HEADER
3208 @ahindex{HEADER}
3209 This macro is defined as the name of the first declared config header
3210 and undefined if no config headers have been declared up to this point.
3211 A third-party macro may, for example, require use of a config header
3212 without invoking AC_CONFIG_HEADERS twice, like this:
3214 @example
3215 AC_CONFIG_COMMANDS_PRE(
3216         [m4_ifndef([AH_HEADER], [AC_CONFIG_HEADERS([config.h])])])
3217 @end example
3219 @end defmac
3221 @xref{Configuration Actions}, for more details on @var{header}.
3223 @menu
3224 * Header Templates::            Input for the configuration headers
3225 * autoheader Invocation::       How to create configuration templates
3226 * Autoheader Macros::           How to specify CPP templates
3227 @end menu
3229 @node Header Templates
3230 @subsection Configuration Header Templates
3231 @cindex Configuration Header Template
3232 @cindex Header templates
3233 @cindex @file{config.h.in}
3235 Your distribution should contain a template file that looks as you want
3236 the final header file to look, including comments, with @code{#undef}
3237 statements which are used as hooks.  For example, suppose your
3238 @file{configure.ac} makes these calls:
3240 @example
3241 AC_CONFIG_HEADERS([conf.h])
3242 AC_CHECK_HEADERS([unistd.h])
3243 @end example
3245 @noindent
3246 Then you could have code like the following in @file{conf.h.in}.
3247 The @file{conf.h} created by @command{configure} defines @samp{HAVE_UNISTD_H}
3248 to 1, if and only if the system has @file{unistd.h}.
3250 @example
3251 @group
3252 /* Define as 1 if you have unistd.h.  */
3253 #undef HAVE_UNISTD_H
3254 @end group
3255 @end example
3257 The format of the template file is stricter than what the C preprocessor
3258 is required to accept.  A directive line should contain only whitespace,
3259 @samp{#undef}, and @samp{HAVE_UNISTD_H}.  The use of @samp{#define}
3260 instead of @samp{#undef}, or of comments on the same line as
3261 @samp{#undef}, is strongly discouraged.  Each hook should only be listed
3262 once.  Other preprocessor lines, such as @samp{#ifdef} or
3263 @samp{#include}, are copied verbatim from the template into the
3264 generated header.
3266 Since it is a tedious task to keep a template header up to date, you may
3267 use @command{autoheader} to generate it, see @ref{autoheader Invocation}.
3269 During the instantiation of the header, each @samp{#undef} line in the
3270 template file for each symbol defined by @samp{AC_DEFINE} is changed to an
3271 appropriate @samp{#define}. If the corresponding @samp{AC_DEFINE} has not
3272 been executed during the @command{configure} run, the @samp{#undef} line is
3273 commented out.  (This is important, e.g., for @samp{_POSIX_SOURCE}:
3274 on many systems, it can be implicitly defined by the compiler, and
3275 undefining it in the header would then break compilation of subsequent
3276 headers.)
3278 Currently, @emph{all} remaining @samp{#undef} lines in the header
3279 template are commented out, whether or not there was a corresponding
3280 @samp{AC_DEFINE} for the macro name; but this behavior is not guaranteed
3281 for future releases of Autoconf.
3283 Generally speaking, since you should not use @samp{#define}, and you
3284 cannot guarantee whether a @samp{#undef} directive in the header
3285 template will be converted to a @samp{#define} or commented out in the
3286 generated header file, the template file cannot be used for conditional
3287 definition effects.  Consequently, if you need to use the construct
3289 @example
3290 @group
3291 #ifdef THIS
3292 # define THAT
3293 #endif
3294 @end group
3295 @end example
3297 @noindent
3298 you must place it outside of the template.
3299 If you absolutely need to hook it to the config header itself, please put
3300 the directives to a separate file, and @samp{#include} that file from the
3301 config header template.  If you are using @command{autoheader}, you would
3302 probably use @samp{AH_BOTTOM} to append the @samp{#include} directive.
3305 @node autoheader Invocation
3306 @subsection Using @command{autoheader} to Create @file{config.h.in}
3307 @cindex @command{autoheader}
3309 The @command{autoheader} program can create a template file of C
3310 @samp{#define} statements for @command{configure} to use.
3311 It searches for the first invocation of @code{AC_CONFIG_HEADERS} in
3312 @file{configure} sources to determine the name of the template.
3313 (If the first call of @code{AC_CONFIG_HEADERS} specifies more than one
3314 input file name, @command{autoheader} uses the first one.)
3316 It is recommended that only one input file is used.  If you want to append
3317 a boilerplate code, it is preferable to use
3318 @samp{AH_BOTTOM([#include <conf_post.h>])}.
3319 File @file{conf_post.h} is not processed during the configuration then,
3320 which make things clearer.  Analogically, @code{AH_TOP} can be used to
3321 prepend a boilerplate code.
3323 In order to do its job, @command{autoheader} needs you to document all
3324 of the symbols that you might use.  Typically this is done via an
3325 @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED} call whose first argument
3326 is a literal symbol and whose third argument describes the symbol
3327 (@pxref{Defining Symbols}).  Alternatively, you can use
3328 @code{AH_TEMPLATE} (@pxref{Autoheader Macros}), or you can supply a
3329 suitable input file for a subsequent configuration header file.
3330 Symbols defined by Autoconf's builtin tests are already documented properly;
3331 you need to document only those that you
3332 define yourself.
3334 You might wonder why @command{autoheader} is needed: after all, why
3335 would @command{configure} need to ``patch'' a @file{config.h.in} to
3336 produce a @file{config.h} instead of just creating @file{config.h} from
3337 scratch?  Well, when everything rocks, the answer is just that we are
3338 wasting our time maintaining @command{autoheader}: generating
3339 @file{config.h} directly is all that is needed.  When things go wrong,
3340 however, you'll be thankful for the existence of @command{autoheader}.
3342 The fact that the symbols are documented is important in order to
3343 @emph{check} that @file{config.h} makes sense.  The fact that there is a
3344 well-defined list of symbols that should be defined (or not) is
3345 also important for people who are porting packages to environments where
3346 @command{configure} cannot be run: they just have to @emph{fill in the
3347 blanks}.
3349 But let's come back to the point: the invocation of @command{autoheader}@dots{}
3351 If you give @command{autoheader} an argument, it uses that file instead
3352 of @file{configure.ac} and writes the header file to the standard output
3353 instead of to @file{config.h.in}.  If you give @command{autoheader} an
3354 argument of @option{-}, it reads the standard input instead of
3355 @file{configure.ac} and writes the header file to the standard output.
3357 @command{autoheader} accepts the following options:
3359 @table @option
3360 @item --help
3361 @itemx -h
3362 Print a summary of the command line options and exit.
3364 @item --version
3365 @itemx -V
3366 Print the version number of Autoconf and exit.
3368 @item --verbose
3369 @itemx -v
3370 Report processing steps.
3372 @item --debug
3373 @itemx -d
3374 Don't remove the temporary files.
3376 @item --force
3377 @itemx -f
3378 Remake the template file even if newer than its input files.
3380 @item --include=@var{dir}
3381 @itemx -I @var{dir}
3382 Append @var{dir} to the include path.  Multiple invocations accumulate.
3384 @item --prepend-include=@var{dir}
3385 @itemx -B @var{dir}
3386 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
3388 @item --warnings=@var{category}
3389 @itemx -W @var{category}
3390 @evindex WARNINGS
3391 Report the warnings related to @var{category} (which can actually be a
3392 comma separated list).  Current categories include:
3394 @table @samp
3395 @item obsolete
3396 report the uses of obsolete constructs
3398 @item all
3399 report all the warnings
3401 @item none
3402 report none
3404 @item error
3405 treats warnings as errors
3407 @item no-@var{category}
3408 disable warnings falling into @var{category}
3409 @end table
3411 @end table
3415 @node Autoheader Macros
3416 @subsection Autoheader Macros
3417 @cindex Autoheader macros
3419 @command{autoheader} scans @file{configure.ac} and figures out which C
3420 preprocessor symbols it might define.  It knows how to generate
3421 templates for symbols defined by @code{AC_CHECK_HEADERS},
3422 @code{AC_CHECK_FUNCS} etc., but if you @code{AC_DEFINE} any additional
3423 symbol, you must define a template for it.  If there are missing
3424 templates, @command{autoheader} fails with an error message.
3426 The template for a @var{symbol} is created
3427 by @command{autoheader} from
3428 the @var{description} argument to an @code{AC_DEFINE};
3429 see @ref{Defining Symbols}.
3431 For special needs, you can use the following macros.
3434 @defmac AH_TEMPLATE (@var{key}, @var{description})
3435 @ahindex{TEMPLATE}
3436 Tell @command{autoheader} to generate a template for @var{key}.  This macro
3437 generates standard templates just like @code{AC_DEFINE} when a
3438 @var{description} is given.
3440 For example:
3442 @example
3443 AH_TEMPLATE([CRAY_STACKSEG_END],
3444             [Define to one of _getb67, GETB67, getb67
3445              for Cray-2 and Cray-YMP systems.  This
3446              function is required for alloca.c support
3447              on those systems.])
3448 @end example
3450 @noindent
3451 generates the following template, with the description properly
3452 justified.
3454 @example
3455 /* Define to one of _getb67, GETB67, getb67 for Cray-2 and
3456    Cray-YMP systems.  This function is required for alloca.c
3457    support on those systems.  */
3458 #undef CRAY_STACKSEG_END
3459 @end example
3460 @end defmac
3463 @defmac AH_VERBATIM (@var{key}, @var{template})
3464 @ahindex{VERBATIM}
3465 Tell @command{autoheader} to include the @var{template} as-is in the header
3466 template file.  This @var{template} is associated with the @var{key},
3467 which is used to sort all the different templates and guarantee their
3468 uniqueness.  It should be a symbol that can be defined via @code{AC_DEFINE}.
3469 @end defmac
3472 @defmac AH_TOP (@var{text})
3473 @ahindex{TOP}
3474 Include @var{text} at the top of the header template file.
3475 @end defmac
3478 @defmac AH_BOTTOM (@var{text})
3479 @ahindex{BOTTOM}
3480 Include @var{text} at the bottom of the header template file.
3481 @end defmac
3484 Please note that @var{text} gets included ``verbatim'' to the template file,
3485 not to the resulting config header, so it can easily get mangled when the
3486 template is processed.  There is rarely a need for something other than
3488 @example
3489 AH_BOTTOM([#include <custom.h>])
3490 @end example
3494 @node Configuration Commands
3495 @section Running Arbitrary Configuration Commands
3496 @cindex Configuration commands
3497 @cindex Commands for configuration
3499 You can execute arbitrary commands before, during, and after
3500 @file{config.status} is run.  The three following macros accumulate the
3501 commands to run when they are called multiple times.
3502 @code{AC_CONFIG_COMMANDS} replaces the obsolete macro
3503 @code{AC_OUTPUT_COMMANDS}; see @ref{Obsolete Macros}, for details.
3505 @anchor{AC_CONFIG_COMMANDS}
3506 @defmac AC_CONFIG_COMMANDS (@var{tag}@dots{}, @ovar{cmds}, @ovar{init-cmds})
3507 @acindex{CONFIG_COMMANDS}
3508 Specify additional shell commands to run at the end of
3509 @file{config.status}, and shell commands to initialize any variables
3510 from @command{configure}.  Associate the commands with @var{tag}.
3511 Since typically the @var{cmds} create a file, @var{tag} should
3512 naturally be the name of that file.  If needed, the directory hosting
3513 @var{tag} is created.  This macro is one of the instantiating macros;
3514 see @ref{Configuration Actions}.
3516 Here is an unrealistic example:
3517 @example
3518 fubar=42
3519 AC_CONFIG_COMMANDS([fubar],
3520                    [echo this is extra $fubar, and so on.],
3521                    [fubar=$fubar])
3522 @end example
3524 Here is a better one:
3525 @example
3526 AC_CONFIG_COMMANDS([timestamp], [date >timestamp])
3527 @end example
3528 @end defmac
3530 The following two macros look similar, but in fact they are not of the same
3531 breed: they are executed directly by @file{configure}, so you cannot use
3532 @file{config.status} to rerun them.
3534 @c Yet it is good to leave them here.  The user sees them together and
3535 @c decides which best fits their needs.
3537 @defmac AC_CONFIG_COMMANDS_PRE (@var{cmds})
3538 @acindex{CONFIG_COMMANDS_PRE}
3539 Execute the @var{cmds} right before creating @file{config.status}.
3541 This macro presents the last opportunity to call @code{AC_SUBST},
3542 @code{AC_DEFINE}, or @code{AC_CONFIG_@var{ITEMS}} macros.
3543 @end defmac
3545 @defmac AC_CONFIG_COMMANDS_POST (@var{cmds})
3546 @acindex{CONFIG_COMMANDS_POST}
3547 Execute the @var{cmds} right after creating @file{config.status}.
3548 @end defmac
3553 @node Configuration Links
3554 @section Creating Configuration Links
3555 @cindex Configuration links
3556 @cindex Links for configuration
3558 You may find it convenient to create links whose destinations depend upon
3559 results of tests.  One can use @code{AC_CONFIG_COMMANDS} but the
3560 creation of relative symbolic links can be delicate when the package is
3561 built in a directory different from the source directory.
3563 @anchor{AC_CONFIG_LINKS}
3564 @defmac AC_CONFIG_LINKS (@var{dest}:@var{source}@dots{}, @ovar{cmds}, @
3565   @ovar{init-cmds})
3566 @acindex{CONFIG_LINKS}
3567 @cindex Links
3568 Make @code{AC_OUTPUT} link each of the existing files @var{source} to
3569 the corresponding link name @var{dest}.  Makes a symbolic link if
3570 possible, otherwise a hard link if possible, otherwise a copy.  The
3571 @var{dest} and @var{source} names should be relative to the top level
3572 source or build directory.  This macro is one of the instantiating
3573 macros; see @ref{Configuration Actions}.
3575 For example, this call:
3577 @example
3578 AC_CONFIG_LINKS([host.h:config/$machine.h
3579                 object.h:config/$obj_format.h])
3580 @end example
3582 @noindent
3583 creates in the current directory @file{host.h} as a link to
3584 @file{@var{srcdir}/config/$machine.h}, and @file{object.h} as a
3585 link to @file{@var{srcdir}/config/$obj_format.h}.
3587 The tempting value @samp{.} for @var{dest} is invalid: it makes it
3588 impossible for @samp{config.status} to guess the links to establish.
3590 One can then run:
3591 @example
3592 ./config.status host.h object.h
3593 @end example
3594 @noindent
3595 to create the links.
3596 @end defmac
3600 @node Subdirectories
3601 @section Configuring Other Packages in Subdirectories
3602 @cindex Configure subdirectories
3603 @cindex Subdirectory configure
3605 In most situations, calling @code{AC_OUTPUT} is sufficient to produce
3606 makefiles in subdirectories.  However, @command{configure} scripts
3607 that control more than one independent package can use
3608 @code{AC_CONFIG_SUBDIRS} to run @command{configure} scripts for other
3609 packages in subdirectories.
3611 @defmac AC_CONFIG_SUBDIRS (@var{dir} @dots{})
3612 @acindex{CONFIG_SUBDIRS}
3613 @ovindex subdirs
3614 Make @code{AC_OUTPUT} run @command{configure} in each subdirectory
3615 @var{dir} in the given blank-or-newline-separated list.  Each @var{dir} should
3616 be a literal, i.e., please do not use:
3618 @example
3619 if test "x$package_foo_enabled" = xyes; then
3620   my_subdirs="$my_subdirs foo"
3622 AC_CONFIG_SUBDIRS([$my_subdirs])
3623 @end example
3625 @noindent
3626 because this prevents @samp{./configure --help=recursive} from
3627 displaying the options of the package @code{foo}.  Instead, you should
3628 write:
3630 @example
3631 if test "x$package_foo_enabled" = xyes; then
3632   AC_CONFIG_SUBDIRS([foo])
3634 @end example
3636 If a given @var{dir} is not found, an error is reported: if the
3637 subdirectory is optional, write:
3639 @example
3640 if test -d "$srcdir/foo"; then
3641   AC_CONFIG_SUBDIRS([foo])
3643 @end example
3645 @c NB: Yes, below we mean configure.in, not configure.ac.
3646 If a given @var{dir} contains @command{configure.gnu}, it is run instead
3647 of @command{configure}.  This is for packages that might use a
3648 non-Autoconf script @command{Configure}, which can't be called through a
3649 wrapper @command{configure} since it would be the same file on
3650 case-insensitive file systems.  Likewise, if a @var{dir} contains
3651 @file{configure.in} but no @command{configure}, the Cygnus
3652 @command{configure} script found by @code{AC_CONFIG_AUX_DIR} is used.
3654 The subdirectory @command{configure} scripts are given the same command
3655 line options that were given to this @command{configure} script, with minor
3656 changes if needed, which include:
3658 @itemize @minus
3659 @item
3660 adjusting a relative name for the cache file;
3662 @item
3663 adjusting a relative name for the source directory;
3665 @item
3666 propagating the current value of @code{$prefix}, including if it was
3667 defaulted, and if the default values of the top level and of the subdirectory
3668 @file{configure} differ.
3669 @end itemize
3671 This macro also sets the output variable @code{subdirs} to the list of
3672 directories @samp{@var{dir} @dots{}}.  Make rules can use
3673 this variable to determine which subdirectories to recurse into.
3675 This macro may be called multiple times.
3676 @end defmac
3678 @node Default Prefix
3679 @section Default Prefix
3680 @cindex Install prefix
3681 @cindex Prefix for install
3683 By default, @command{configure} sets the prefix for files it installs to
3684 @file{/usr/local}.  The user of @command{configure} can select a different
3685 prefix using the @option{--prefix} and @option{--exec-prefix} options.
3686 There are two ways to change the default: when creating
3687 @command{configure}, and when running it.
3689 Some software packages might want to install in a directory other than
3690 @file{/usr/local} by default.  To accomplish that, use the
3691 @code{AC_PREFIX_DEFAULT} macro.
3693 @defmac AC_PREFIX_DEFAULT (@var{prefix})
3694 @acindex{PREFIX_DEFAULT}
3695 Set the default installation prefix to @var{prefix} instead of
3696 @file{/usr/local}.
3697 @end defmac
3699 It may be convenient for users to have @command{configure} guess the
3700 installation prefix from the location of a related program that they
3701 have already installed.  If you wish to do that, you can call
3702 @code{AC_PREFIX_PROGRAM}.
3704 @anchor{AC_PREFIX_PROGRAM}
3705 @defmac AC_PREFIX_PROGRAM (@var{program})
3706 @acindex{PREFIX_PROGRAM}
3707 If the user did not specify an installation prefix (using the
3708 @option{--prefix} option), guess a value for it by looking for
3709 @var{program} in @env{PATH}, the way the shell does.  If @var{program}
3710 is found, set the prefix to the parent of the directory containing
3711 @var{program}, else default the prefix as described above
3712 (@file{/usr/local} or @code{AC_PREFIX_DEFAULT}).  For example, if
3713 @var{program} is @code{gcc} and the @env{PATH} contains
3714 @file{/usr/local/gnu/bin/gcc}, set the prefix to @file{/usr/local/gnu}.
3715 @end defmac
3719 @c ======================================================== Existing tests
3721 @node Existing Tests
3722 @chapter Existing Tests
3724 These macros test for particular system features that packages might
3725 need or want to use.  If you need to test for a kind of feature that
3726 none of these macros check for, you can probably do it by calling
3727 primitive test macros with appropriate arguments (@pxref{Writing
3728 Tests}).
3730 These tests print messages telling the user which feature they're
3731 checking for, and what they find.  They cache their results for future
3732 @command{configure} runs (@pxref{Caching Results}).
3734 Some of these macros set output variables.  @xref{Makefile
3735 Substitutions}, for how to get their values.  The phrase ``define
3736 @var{name}'' is used below as a shorthand to mean ``define the C
3737 preprocessor symbol @var{name} to the value 1''.  @xref{Defining
3738 Symbols}, for how to get those symbol definitions into your program.
3740 @menu
3741 * Common Behavior::             Macros' standard schemes
3742 * Alternative Programs::        Selecting between alternative programs
3743 * Files::                       Checking for the existence of files
3744 * Libraries::                   Library archives that might be missing
3745 * Library Functions::           C library functions that might be missing
3746 * Header Files::                Header files that might be missing
3747 * Declarations::                Declarations that may be missing
3748 * Structures::                  Structures or members that might be missing
3749 * Types::                       Types that might be missing
3750 * Compilers and Preprocessors::  Checking for compiling programs
3751 * System Services::             Operating system services
3752 * Posix Variants::              Special kludges for specific Posix variants
3753 * Erlang Libraries::            Checking for the existence of Erlang libraries
3754 @end menu
3756 @node Common Behavior
3757 @section Common Behavior
3758 @cindex Common autoconf behavior
3760 Much effort has been expended to make Autoconf easy to learn.  The most
3761 obvious way to reach this goal is simply to enforce standard interfaces
3762 and behaviors, avoiding exceptions as much as possible.  Because of
3763 history and inertia, unfortunately, there are still too many exceptions
3764 in Autoconf; nevertheless, this section describes some of the common
3765 rules.
3767 @menu
3768 * Standard Symbols::            Symbols defined by the macros
3769 * Default Includes::            Includes used by the generic macros
3770 @end menu
3772 @node Standard Symbols
3773 @subsection Standard Symbols
3774 @cindex Standard symbols
3776 All the generic macros that @code{AC_DEFINE} a symbol as a result of
3777 their test transform their @var{argument} values to a standard alphabet.
3778 First, @var{argument} is converted to upper case and any asterisks
3779 (@samp{*}) are each converted to @samp{P}.  Any remaining characters
3780 that are not alphanumeric are converted to underscores.
3782 For instance,
3784 @example
3785 AC_CHECK_TYPES([struct $Expensive*])
3786 @end example
3788 @noindent
3789 defines the symbol @samp{HAVE_STRUCT__EXPENSIVEP} if the check
3790 succeeds.
3793 @node Default Includes
3794 @subsection Default Includes
3795 @cindex Default includes
3796 @cindex Includes, default
3798 Several tests depend upon a set of header files.  Since these headers
3799 are not universally available, tests actually have to provide a set of
3800 protected includes, such as:
3802 @example
3803 @group
3804 #ifdef TIME_WITH_SYS_TIME
3805 # include <sys/time.h>
3806 # include <time.h>
3807 #else
3808 # ifdef HAVE_SYS_TIME_H
3809 #  include <sys/time.h>
3810 # else
3811 #  include <time.h>
3812 # endif
3813 #endif
3814 @end group
3815 @end example
3817 @noindent
3818 Unless you know exactly what you are doing, you should avoid using
3819 unconditional includes, and check the existence of the headers you
3820 include beforehand (@pxref{Header Files}).
3822 Most generic macros use the following macro to provide the default set
3823 of includes:
3825 @defmac AC_INCLUDES_DEFAULT (@ovar{include-directives})
3826 @acindex{INCLUDES_DEFAULT}
3827 Expand to @var{include-directives} if defined, otherwise to:
3829 @example
3830 @group
3831 #include <stdio.h>
3832 #ifdef HAVE_SYS_TYPES_H
3833 # include <sys/types.h>
3834 #endif
3835 #ifdef HAVE_SYS_STAT_H
3836 # include <sys/stat.h>
3837 #endif
3838 #ifdef STDC_HEADERS
3839 # include <stdlib.h>
3840 # include <stddef.h>
3841 #else
3842 # ifdef HAVE_STDLIB_H
3843 #  include <stdlib.h>
3844 # endif
3845 #endif
3846 #ifdef HAVE_STRING_H
3847 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
3848 #  include <memory.h>
3849 # endif
3850 # include <string.h>
3851 #endif
3852 #ifdef HAVE_STRINGS_H
3853 # include <strings.h>
3854 #endif
3855 #ifdef HAVE_INTTYPES_H
3856 # include <inttypes.h>
3857 #endif
3858 #ifdef HAVE_STDINT_H
3859 # include <stdint.h>
3860 #endif
3861 #ifdef HAVE_UNISTD_H
3862 # include <unistd.h>
3863 #endif
3864 @end group
3865 @end example
3867 If the default includes are used, then check for the presence of these
3868 headers and their compatibility, i.e., you don't need to run
3869 @code{AC_HEADER_STDC}, nor check for @file{stdlib.h} etc.
3871 These headers are checked for in the same order as they are included.
3872 For instance, on some systems @file{string.h} and @file{strings.h} both
3873 exist, but conflict.  Then @code{HAVE_STRING_H} is defined, not
3874 @code{HAVE_STRINGS_H}.
3875 @end defmac
3877 @node Alternative Programs
3878 @section Alternative Programs
3879 @cindex Programs, checking
3881 These macros check for the presence or behavior of particular programs.
3882 They are used to choose between several alternative programs and to
3883 decide what to do once one has been chosen.  If there is no macro
3884 specifically defined to check for a program you need, and you don't need
3885 to check for any special properties of it, then you can use one of the
3886 general program-check macros.
3888 @menu
3889 * Particular Programs::         Special handling to find certain programs
3890 * Generic Programs::            How to find other programs
3891 @end menu
3893 @node Particular Programs
3894 @subsection Particular Program Checks
3896 These macros check for particular programs---whether they exist, and
3897 in some cases whether they support certain features.
3899 @defmac AC_PROG_AWK
3900 @acindex{PROG_AWK}
3901 @ovindex AWK
3902 @caindex prog_AWK
3903 Check for @code{gawk}, @code{mawk}, @code{nawk}, and @code{awk}, in that
3904 order, and set output variable @code{AWK} to the first one that is found.
3905 It tries @code{gawk} first because that is reported to be the
3906 best implementation.  The result can be overridden by setting the
3907 variable @code{AWK} or the cache variable @code{ac_cv_prog_AWK}.
3908 @end defmac
3910 @defmac AC_PROG_GREP
3911 @acindex{PROG_GREP}
3912 @ovindex GREP
3913 @caindex prog_GREP
3914 Look for the best available @code{grep} or @code{ggrep} that accepts the
3915 longest input lines possible, and that supports multiple @option{-e} options.
3916 Set the output variable @code{GREP} to whatever is chosen.
3917 @xref{grep, , Limitations of Usual Tools}, for more information about
3918 portability problems with the @command{grep} command family.  The result
3919 can be overridden by setting the @code{GREP} variable and is cached in the
3920 @code{ac_cv_path_GREP} variable.
3921 @end defmac
3923 @defmac AC_PROG_EGREP
3924 @acindex{PROG_EGREP}
3925 @ovindex EGREP
3926 @caindex prog_EGREP
3927 Check whether @code{$GREP -E} works, or else look for the best available
3928 @code{egrep} or @code{gegrep} that accepts the longest input lines possible.
3929 Set the output variable @code{EGREP} to whatever is chosen.  The result
3930 can be overridden by setting the @code{EGREP} variable and is cached in the
3931 @code{ac_cv_path_EGREP} variable.
3932 @end defmac
3934 @defmac AC_PROG_FGREP
3935 @acindex{PROG_FGREP}
3936 @ovindex FGREP
3937 @caindex prog_FGREP
3938 Check whether @code{$GREP -F} works, or else look for the best available
3939 @code{fgrep} or @code{gfgrep} that accepts the longest input lines possible.
3940 Set the output variable @code{FGREP} to whatever is chosen.  The result
3941 can be overridden by setting the @code{FGREP} variable and is cached in the
3942 @code{ac_cv_path_FGREP} variable.
3943 @end defmac
3945 @defmac AC_PROG_INSTALL
3946 @acindex{PROG_INSTALL}
3947 @ovindex INSTALL
3948 @ovindex INSTALL_PROGRAM
3949 @ovindex INSTALL_DATA
3950 @ovindex INSTALL_SCRIPT
3951 @caindex path_install
3952 Set output variable @code{INSTALL} to the name of a BSD-compatible
3953 @command{install} program, if one is found in the current @env{PATH}.
3954 Otherwise, set @code{INSTALL} to @samp{@var{dir}/install-sh -c},
3955 checking the directories specified to @code{AC_CONFIG_AUX_DIR} (or its
3956 default directories) to determine @var{dir} (@pxref{Output}).  Also set
3957 the variables @code{INSTALL_PROGRAM} and @code{INSTALL_SCRIPT} to
3958 @samp{$@{INSTALL@}} and @code{INSTALL_DATA} to @samp{$@{INSTALL@} -m 644}.
3960 @samp{@@INSTALL@@} is special, as its value may vary for different
3961 configuration files.
3963 This macro screens out various instances of @command{install} known not to
3964 work.  It prefers to find a C program rather than a shell script, for
3965 speed.  Instead of @file{install-sh}, it can also use @file{install.sh},
3966 but that name is obsolete because some @command{make} programs have a rule
3967 that creates @file{install} from it if there is no makefile.  Further, this
3968 macro requires @command{install} to be able to install multiple files into a
3969 target directory in a single invocation.
3971 Autoconf comes with a copy of @file{install-sh} that you can use.  If
3972 you use @code{AC_PROG_INSTALL}, you must include either
3973 @file{install-sh} or @file{install.sh} in your distribution; otherwise
3974 @command{configure} produces an error message saying it can't find
3975 them---even if the system you're on has a good @command{install} program.
3976 This check is a safety measure to prevent you from accidentally leaving
3977 that file out, which would prevent your package from installing on
3978 systems that don't have a BSD-compatible @command{install} program.
3980 If you need to use your own installation program because it has features
3981 not found in standard @command{install} programs, there is no reason to use
3982 @code{AC_PROG_INSTALL}; just put the file name of your program into your
3983 @file{Makefile.in} files.
3985 The result of the test can be overridden by setting the variable
3986 @code{INSTALL} or the cache variable @code{ac_cv_path_install}.
3987 @end defmac
3989 @defmac AC_PROG_MKDIR_P
3990 @acindex{PROG_MKDIR_P}
3991 @ovindex MKDIR_P
3992 @caindex path_mkdir
3993 Set output variable @code{MKDIR_P} to a program that ensures that for
3994 each argument, a directory named by this argument exists, creating it
3995 and its parent directories if needed, and without race conditions when
3996 two instances of the program attempt to make the same directory at
3997 nearly the same time.
3999 This macro uses the @samp{mkdir -p} command if possible.  Otherwise, it
4000 falls back on invoking @command{install-sh} with the @option{-d} option,
4001 so your package should
4002 contain @file{install-sh} as described under @code{AC_PROG_INSTALL}.
4003 An @file{install-sh} file that predates Autoconf 2.60 or Automake 1.10
4004 is vulnerable to race conditions, so if you want to support parallel
4005 installs from
4006 different packages into the same directory you need to make sure you
4007 have an up-to-date @file{install-sh}.  In particular, be careful about
4008 using @samp{autoreconf -if} if your Automake predates Automake 1.10.
4010 This macro is related to the @code{AS_MKDIR_P} macro (@pxref{Programming
4011 in M4sh}), but it sets an output variable intended for use in other
4012 files, whereas @code{AS_MKDIR_P} is intended for use in scripts like
4013 @command{configure}.  Also, @code{AS_MKDIR_P} does not accept options,
4014 but @code{MKDIR_P} supports the @option{-m} option, e.g., a makefile
4015 might invoke @code{$(MKDIR_P) -m 0 dir} to create an inaccessible
4016 directory, and conversely a makefile should use @code{$(MKDIR_P) --
4017 $(FOO)} if @var{FOO} might yield a value that begins with @samp{-}.
4018 Finally, @code{AS_MKDIR_P} does not check for race condition
4019 vulnerability, whereas @code{AC_PROG_MKDIR_P} does.
4021 @samp{@@MKDIR_P@@} is special, as its value may vary for different
4022 configuration files.
4024 The result of the test can be overridden by setting the variable
4025 @code{MKDIR_P} or the cache variable @code{ac_cv_path_mkdir}.
4026 @end defmac
4028 @anchor{AC_PROG_LEX}
4029 @defmac AC_PROG_LEX
4030 @acindex{PROG_LEX}
4031 @ovindex LEX
4032 @ovindex LEXLIB
4033 @cvindex YYTEXT_POINTER
4034 @ovindex LEX_OUTPUT_ROOT
4035 @caindex prog_LEX
4036 If @code{flex} is found, set output variable @code{LEX} to @samp{flex}
4037 and @code{LEXLIB} to @option{-lfl}, if that library is in a standard
4038 place.  Otherwise set @code{LEX} to @samp{lex} and @code{LEXLIB} to
4039 @option{-ll}, if found.  If neither variant is available, set @code{LEX}
4040 to @samp{:}; for packages that ship the generated @file{file.yy.c}
4041 alongside the source @file{file.l}, this default allows users without a
4042 lexer generator to still build the package even if the timestamp for
4043 @file{file.l} is inadvertantly changed.
4045 Define @code{YYTEXT_POINTER} if @code{yytext} defaults to @samp{char *} instead
4046 of to @samp{char []}.  Also set output variable @code{LEX_OUTPUT_ROOT} to
4047 the base of the file name that the lexer generates; usually
4048 @file{lex.yy}, but sometimes something else.  These results vary
4049 according to whether @code{lex} or @code{flex} is being used.
4051 You are encouraged to use Flex in your sources, since it is both more
4052 pleasant to use than plain Lex and the C source it produces is portable.
4053 In order to ensure portability, however, you must either provide a
4054 function @code{yywrap} or, if you don't use it (e.g., your scanner has
4055 no @samp{#include}-like feature), simply include a @samp{%noyywrap}
4056 statement in the scanner's source.  Once this done, the scanner is
4057 portable (unless @emph{you} felt free to use nonportable constructs) and
4058 does not depend on any library.  In this case, and in this case only, it
4059 is suggested that you use this Autoconf snippet:
4061 @example
4062 AC_PROG_LEX
4063 if test "x$LEX" != xflex; then
4064   LEX="$SHELL $missing_dir/missing flex"
4065   AC_SUBST([LEX_OUTPUT_ROOT], [lex.yy])
4066   AC_SUBST([LEXLIB], [''])
4068 @end example
4070 The shell script @command{missing} can be found in the Automake
4071 distribution.
4073 Remember that the user may have supplied an alternate location in
4074 @env{LEX}, so if Flex is required, it is better to check that the user
4075 provided something sufficient by parsing the output of @samp{$LEX
4076 --version} than by simply relying on @code{test "x$LEX" = xflex}.
4078 To ensure backward compatibility, Automake's @code{AM_PROG_LEX} invokes
4079 (indirectly) this macro twice, which causes an annoying but benign
4080 ``@code{AC_PROG_LEX} invoked multiple times'' warning.  Future versions
4081 of Automake will fix this issue; meanwhile, just ignore this message.
4083 As part of running the test, this macro may delete any file in the
4084 configuration directory named @file{lex.yy.c} or @file{lexyy.c}.
4086 The result of this test can be influenced by setting the variable
4087 @code{LEX} or the cache variable @code{ac_cv_prog_LEX}.
4088 @end defmac
4090 @anchor{AC_PROG_LN_S}
4091 @defmac AC_PROG_LN_S
4092 @acindex{PROG_LN_S}
4093 @ovindex LN_S
4094 If @samp{ln -s} works on the current file system (the operating system
4095 and file system support symbolic links), set the output variable
4096 @code{LN_S} to @samp{ln -s}; otherwise, if @samp{ln} works, set
4097 @code{LN_S} to @samp{ln}, and otherwise set it to @samp{cp -p}.
4099 If you make a link in a directory other than the current directory, its
4100 meaning depends on whether @samp{ln} or @samp{ln -s} is used.  To safely
4101 create links using @samp{$(LN_S)}, either find out which form is used
4102 and adjust the arguments, or always invoke @code{ln} in the directory
4103 where the link is to be created.
4105 In other words, it does not work to do:
4106 @example
4107 $(LN_S) foo /x/bar
4108 @end example
4110 Instead, do:
4112 @example
4113 (cd /x && $(LN_S) foo bar)
4114 @end example
4115 @end defmac
4117 @defmac AC_PROG_RANLIB
4118 @acindex{PROG_RANLIB}
4119 @ovindex RANLIB
4120 @c @caindex prog_RANLIB
4121 @c @caindex prog_ac_ct_RANLIB
4122 Set output variable @code{RANLIB} to @samp{ranlib} if @code{ranlib}
4123 is found, and otherwise to @samp{:} (do nothing).
4124 @end defmac
4126 @defmac AC_PROG_SED
4127 @acindex{PROG_SED}
4128 @ovindex SED
4129 @caindex path_SED
4130 Set output variable @code{SED} to a Sed implementation that conforms to
4131 Posix and does not have arbitrary length limits.  Report an error if no
4132 acceptable Sed is found.  @xref{sed, , Limitations of Usual Tools}, for more
4133 information about portability problems with Sed.
4135 The result of this test can be overridden by setting the @code{SED} variable
4136 and is cached in the @code{ac_cv_path_SED} variable.
4137 @end defmac
4139 @defmac AC_PROG_YACC
4140 @acindex{PROG_YACC}
4141 @evindex YACC
4142 @evindex YFLAGS
4143 @ovindex YACC
4144 @caindex prog_YACC
4145 If @code{bison} is found, set output variable @code{YACC} to @samp{bison
4146 -y}.  Otherwise, if @code{byacc} is found, set @code{YACC} to
4147 @samp{byacc}.  Otherwise set @code{YACC} to @samp{yacc}.
4148 The result of this test can be influenced by setting the variable
4149 @code{YACC} or the cache variable @code{ac_cv_prog_YACC}.
4150 @end defmac
4152 @node Generic Programs
4153 @subsection Generic Program and File Checks
4155 These macros are used to find programs not covered by the ``particular''
4156 test macros.  If you need to check the behavior of a program as well as
4157 find out whether it is present, you have to write your own test for it
4158 (@pxref{Writing Tests}).  By default, these macros use the environment
4159 variable @env{PATH}.  If you need to check for a program that might not
4160 be in the user's @env{PATH}, you can pass a modified path to use
4161 instead, like this:
4163 @example
4164 AC_PATH_PROG([INETD], [inetd], [/usr/libexec/inetd],
4165              [$PATH$PATH_SEPARATOR/usr/libexec$PATH_SEPARATOR]dnl
4166 [/usr/sbin$PATH_SEPARATOR/usr/etc$PATH_SEPARATOR/etc])
4167 @end example
4169 You are strongly encouraged to declare the @var{variable} passed to
4170 @code{AC_CHECK_PROG} etc.@: as precious, @xref{Setting Output Variables},
4171 @code{AC_ARG_VAR}, for more details.
4173 @anchor{AC_CHECK_PROG}
4174 @defmac AC_CHECK_PROG (@var{variable}, @var{prog-to-check-for}, @
4175   @var{value-if-found}, @ovar{value-if-not-found}, @dvar{path, $PATH}, @
4176   @ovar{reject})
4177 @acindex{CHECK_PROG}
4178 @caindex prog_@var{variable}
4179 Check whether program @var{prog-to-check-for} exists in @var{path}.  If
4180 it is found, set @var{variable} to @var{value-if-found}, otherwise to
4181 @var{value-if-not-found}, if given.  Always pass over @var{reject} (an
4182 absolute file name) even if it is the first found in the search path; in
4183 that case, set @var{variable} using the absolute file name of the
4184 @var{prog-to-check-for} found that is not @var{reject}.  If
4185 @var{variable} was already set, do nothing.  Calls @code{AC_SUBST} for
4186 @var{variable}.  The result of this test can be overridden by setting the
4187 @var{variable} variable or the cache variable
4188 @code{ac_cv_prog_@var{variable}}.
4189 @end defmac
4191 @anchor{AC_CHECK_PROGS}
4192 @defmac AC_CHECK_PROGS (@var{variable}, @var{progs-to-check-for}, @
4193   @ovar{value-if-not-found}, @dvar{path, $PATH})
4194 @acindex{CHECK_PROGS}
4195 @caindex prog_@var{variable}
4196 Check for each program in the blank-separated list
4197 @var{progs-to-check-for} existing in the @var{path}.  If one is found, set
4198 @var{variable} to the name of that program.  Otherwise, continue
4199 checking the next program in the list.  If none of the programs in the
4200 list are found, set @var{variable} to @var{value-if-not-found}; if
4201 @var{value-if-not-found} is not specified, the value of @var{variable}
4202 is not changed.  Calls @code{AC_SUBST} for @var{variable}.  The result of
4203 this test can be overridden by setting the @var{variable} variable or the
4204 cache variable @code{ac_cv_prog_@var{variable}}.
4205 @end defmac
4207 @defmac AC_CHECK_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
4208   @ovar{value-if-not-found}, @dvar{path, $PATH})
4209 @acindex{CHECK_TARGET_TOOL}
4210 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
4211 with a prefix of the target type as determined by
4212 @code{AC_CANONICAL_TARGET}, followed by a dash (@pxref{Canonicalizing}).
4213 If the tool cannot be found with a prefix, and if the build and target
4214 types are equal, then it is also searched for without a prefix.
4216 As noted in @ref{Specifying Target Triplets}, the
4217 target is rarely specified, because most of the time it is the same
4218 as the host: it is the type of system for which any compiler tool in
4219 the package produces code.  What this macro looks for is,
4220 for example, @emph{a tool @r{(assembler, linker, etc.)}@: that the
4221 compiler driver @r{(@command{gcc} for the GNU C Compiler)}
4222 uses to produce objects, archives or executables}.
4223 @end defmac
4225 @defmac AC_CHECK_TOOL (@var{variable}, @var{prog-to-check-for}, @
4226   @ovar{value-if-not-found}, @dvar{path, $PATH})
4227 @acindex{CHECK_TOOL}
4228 @c @caindex prog_@var{VARIABLE}
4229 @c @caindex prog_ac_ct_@var{VARIABLE}
4230 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
4231 with a prefix of the host type as specified by @option{--host}, followed by a
4232 dash.  For example, if the user runs
4233 @samp{configure --build=x86_64-gnu --host=i386-gnu}, then this call:
4234 @example
4235 AC_CHECK_TOOL([RANLIB], [ranlib], [:])
4236 @end example
4237 @noindent
4238 sets @code{RANLIB} to @file{i386-gnu-ranlib} if that program exists in
4239 @var{path}, or otherwise to @samp{ranlib} if that program exists in
4240 @var{path}, or to @samp{:} if neither program exists.
4242 When cross-compiling, this macro will issue a warning if no program
4243 prefixed with the host type could be found.
4244 For more information, see @ref{Specifying Target Triplets}.
4245 @end defmac
4247 @defmac AC_CHECK_TARGET_TOOLS (@var{variable}, @var{progs-to-check-for}, @
4248   @ovar{value-if-not-found}, @dvar{path, $PATH})
4249 @acindex{CHECK_TARGET_TOOLS}
4250 Like @code{AC_CHECK_TARGET_TOOL}, each of the tools in the list
4251 @var{progs-to-check-for} are checked with a prefix of the target type as
4252 determined by @code{AC_CANONICAL_TARGET}, followed by a dash
4253 (@pxref{Canonicalizing}).  If none of the tools can be found with a
4254 prefix, and if the build and target types are equal, then the first one
4255 without a prefix is used.  If a tool is found, set @var{variable} to
4256 the name of that program.  If none of the tools in the list are found,
4257 set @var{variable} to @var{value-if-not-found}; if @var{value-if-not-found}
4258 is not specified, the value of @var{variable} is not changed.  Calls
4259 @code{AC_SUBST} for @var{variable}.
4260 @end defmac
4262 @defmac AC_CHECK_TOOLS (@var{variable}, @var{progs-to-check-for}, @
4263   @ovar{value-if-not-found}, @dvar{path, $PATH})
4264 @acindex{CHECK_TOOLS}
4265 Like @code{AC_CHECK_TOOL}, each of the tools in the list
4266 @var{progs-to-check-for} are checked with a prefix of the host type as
4267 determined by @code{AC_CANONICAL_HOST}, followed by a dash
4268 (@pxref{Canonicalizing}).  If none of the tools can be found with a
4269 prefix, then the first one without a prefix is used.  If a tool is found,
4270 set @var{variable} to the name of that program.  If none of the tools in
4271 the list are found, set @var{variable} to @var{value-if-not-found}; if
4272 @var{value-if-not-found} is not specified, the value of @var{variable}
4273 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
4275 When cross-compiling, this macro will issue a warning if no program
4276 prefixed with the host type could be found.
4277 For more information, see @ref{Specifying Target Triplets}.
4278 @end defmac
4280 @anchor{AC_PATH_PROG}
4281 @defmac AC_PATH_PROG (@var{variable}, @var{prog-to-check-for}, @
4282   @ovar{value-if-not-found}, @dvar{path, $PATH})
4283 @acindex{PATH_PROG}
4284 @caindex path_@var{variable}
4285 Like @code{AC_CHECK_PROG}, but set @var{variable} to the absolute
4286 name of @var{prog-to-check-for} if found.  The result of this test
4287 can be overridden by setting the @var{variable} variable.  A positive
4288 result of this test is cached in the @code{ac_cv_path_@var{variable}}
4289 variable.
4290 @end defmac
4292 @anchor{AC_PATH_PROGS}
4293 @defmac AC_PATH_PROGS (@var{variable}, @var{progs-to-check-for}, @
4294   @ovar{value-if-not-found}, @dvar{path, $PATH})
4295 @acindex{PATH_PROGS}
4296 @caindex path_@var{variable}
4297 Like @code{AC_CHECK_PROGS}, but if any of @var{progs-to-check-for}
4298 are found, set @var{variable} to the absolute name of the program
4299 found.  The result of this test can be overridden by setting the
4300 @var{variable} variable.  A positive result of this test is cached in
4301 the @code{ac_cv_path_@var{variable}} variable.
4302 @end defmac
4304 @defmac AC_PATH_PROGS_FEATURE_CHECK (@var{variable}, @
4305   @var{progs-to-check-for}, @var{feature-test}, @
4306   @ovar{action-if-not-found}, @dvar{path, $PATH})
4307 @acindex{PATH_PROGS_FEATURE_CHECK}
4308 @caindex path_@var{variable}
4309 This macro was introduced in Autoconf 2.62.  If @var{variable} is not
4310 empty, then set the cache variable @code{ac_cv_path_@var{variable}} to
4311 its value.  Otherwise, check for each program in the blank-separated
4312 list @var{progs-to-check-for} existing in @var{path}.  For each program
4313 found, execute @var{feature-test} with @code{ac_path_@var{variable}}
4314 set to the absolute name of the candidate program.  If no invocation of
4315 @var{feature-test} sets the shell variable
4316 @code{ac_cv_path_@var{variable}}, then @var{action-if-not-found} is
4317 executed.  @var{feature-test} will be run even when
4318 @code{ac_cv_path_@var{variable}} is set, to provide the ability to
4319 choose a better candidate found later in @var{path}; to accept the
4320 current setting and bypass all futher checks, @var{feature-test} can
4321 execute @code{ac_path_@var{variable}_found=:}.
4323 Note that this macro has some subtle differences from
4324 @code{AC_CHECK_PROGS}.  It is designed to be run inside
4325 @code{AC_CACHE_VAL}, therefore, it should have no side effects.  In
4326 particular, @var{variable} is not set to the final value of
4327 @code{ac_cv_path_@var{variable}}, nor is @code{AC_SUBST} automatically
4328 run.  Also, on failure, any action can be performed, whereas
4329 @code{AC_CHECK_PROGS} only performs
4330 @code{@var{variable}=@var{value-if-not-found}}.
4332 Here is an example, similar to what Autoconf uses in its own configure
4333 script.  It will search for an implementation of @command{m4} that
4334 supports the @code{indir} builtin, even if it goes by the name
4335 @command{gm4} or is not the first implementation on @env{PATH}.
4337 @example
4338 AC_CACHE_CHECK([for m4 that supports indir], [ac_cv_path_M4],
4339   [AC_PATH_PROGS_FEATURE_CHECK([M4], [m4 gm4],
4340     [[m4out=`echo 'changequote([,])indir([divnum])' | $ac_path_M4`
4341       test "x$m4out" = x0 \
4342       && ac_cv_path_M4=$ac_path_M4 ac_path_M4_found=:]],
4343     [AC_MSG_ERROR([could not find m4 that supports indir])])])
4344 AC_SUBST([M4], [$ac_cv_path_M4])
4345 @end example
4346 @end defmac
4348 @defmac AC_PATH_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
4349   @ovar{value-if-not-found}, @dvar{path, $PATH})
4350 @acindex{PATH_TARGET_TOOL}
4351 Like @code{AC_CHECK_TARGET_TOOL}, but set @var{variable} to the absolute
4352 name of the program if it is found.
4353 @end defmac
4355 @defmac AC_PATH_TOOL (@var{variable}, @var{prog-to-check-for}, @
4356   @ovar{value-if-not-found}, @dvar{path, $PATH})
4357 @acindex{PATH_TOOL}
4358 Like @code{AC_CHECK_TOOL}, but set @var{variable} to the absolute
4359 name of the program if it is found.
4361 When cross-compiling, this macro will issue a warning if no program
4362 prefixed with the host type could be found.
4363 For more information, see @ref{Specifying Target Triplets}.
4364 @end defmac
4367 @node Files
4368 @section Files
4369 @cindex File, checking
4371 You might also need to check for the existence of files.  Before using
4372 these macros, ask yourself whether a runtime test might not be a better
4373 solution.  Be aware that, like most Autoconf macros, they test a feature
4374 of the host machine, and therefore, they die when cross-compiling.
4376 @defmac AC_CHECK_FILE (@var{file}, @ovar{action-if-found}, @
4377   @ovar{action-if-not-found})
4378 @acindex{CHECK_FILE}
4379 @caindex file_@var{file}
4380 Check whether file @var{file} exists on the native system.  If it is
4381 found, execute @var{action-if-found}, otherwise do
4382 @var{action-if-not-found}, if given.  The result of this test is cached
4383 in the @code{ac_cv_file_@var{file}} variable, with characters not
4384 suitable for a variable name mapped to underscores.
4385 @end defmac
4387 @defmac AC_CHECK_FILES (@var{files}, @ovar{action-if-found}, @
4388   @ovar{action-if-not-found})
4389 @acindex{CHECK_FILES}
4390 @caindex file_@var{file}
4391 Executes @code{AC_CHECK_FILE} once for each file listed in @var{files}.
4392 Additionally, defines @samp{HAVE_@var{file}} (@pxref{Standard Symbols})
4393 for each file found.  The results of each test are cached in the
4394 @code{ac_cv_file_@var{file}} variable, with characters not suitable for
4395 a variable name mapped to underscores.
4396 @end defmac
4399 @node Libraries
4400 @section Library Files
4401 @cindex Library, checking
4403 The following macros check for the presence of certain C, C++, or Fortran
4404 library archive files.
4406 @anchor{AC_CHECK_LIB}
4407 @defmac AC_CHECK_LIB (@var{library}, @var{function}, @
4408   @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4409 @acindex{CHECK_LIB}
4410 @caindex lib_@var{library}_@var{function}
4411 Test whether the library @var{library} is available by trying to link
4412 a test program that calls function @var{function} with the library.
4413 @var{function} should be a function provided by the library.
4414 Use the base
4415 name of the library; e.g., to check for @option{-lmp}, use @samp{mp} as
4416 the @var{library} argument.
4418 @var{action-if-found} is a list of shell commands to run if the link
4419 with the library succeeds; @var{action-if-not-found} is a list of shell
4420 commands to run if the link fails.  If @var{action-if-found} is not
4421 specified, the default action prepends @option{-l@var{library}} to
4422 @code{LIBS} and defines @samp{HAVE_LIB@var{library}} (in all
4423 capitals).  This macro is intended to support building @code{LIBS} in
4424 a right-to-left (least-dependent to most-dependent) fashion such that
4425 library dependencies are satisfied as a natural side effect of
4426 consecutive tests.  Linkers are sensitive to library ordering
4427 so the order in which @code{LIBS} is generated is important to reliable
4428 detection of libraries.
4430 If linking with @var{library} results in unresolved symbols that would
4431 be resolved by linking with additional libraries, give those libraries
4432 as the @var{other-libraries} argument, separated by spaces:
4433 e.g., @option{-lXt -lX11}.  Otherwise, this macro may fail to detect
4434 that @var{library} is present, because linking the test program can
4435 fail with unresolved symbols.  The @var{other-libraries} argument
4436 should be limited to cases where it is desirable to test for one library
4437 in the presence of another that is not already in @code{LIBS}.
4439 @code{AC_CHECK_LIB} requires some care in usage, and should be avoided
4440 in some common cases.  Many standard functions like @code{gethostbyname}
4441 appear in the standard C library on some hosts, and in special libraries
4442 like @code{nsl} on other hosts.  On some hosts the special libraries
4443 contain variant implementations that you may not want to use.  These
4444 days it is normally better to use @code{AC_SEARCH_LIBS([gethostbyname],
4445 [nsl])} instead of @code{AC_CHECK_LIB([nsl], [gethostbyname])}.
4447 The result of this test is cached in the
4448 @code{ac_cv_lib_@var{library}_@var{function}} variable.
4449 @end defmac
4451 @anchor{AC_SEARCH_LIBS}
4452 @defmac AC_SEARCH_LIBS (@var{function}, @var{search-libs}, @
4453   @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4454 @acindex{SEARCH_LIBS}
4455 @caindex search_@var{function}
4456 Search for a library defining @var{function} if it's not already
4457 available.  This equates to calling
4458 @samp{AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])])} first with
4459 no libraries, then for each library listed in @var{search-libs}.
4461 Prepend @option{-l@var{library}} to @code{LIBS} for the first library found
4462 to contain @var{function}, and run @var{action-if-found}.  If the
4463 function is not found, run @var{action-if-not-found}.
4465 If linking with @var{library} results in unresolved symbols that would
4466 be resolved by linking with additional libraries, give those libraries
4467 as the @var{other-libraries} argument, separated by spaces:
4468 e.g., @option{-lXt -lX11}.  Otherwise, this macro fails to detect
4469 that @var{function} is present, because linking the test program
4470 always fails with unresolved symbols.
4472 The result of this test is cached in the
4473 @code{ac_cv_search_@var{function}} variable as @samp{none required} if
4474 @var{function} is already available, as @samp{no} if no library
4475 containing @var{function} was found, otherwise as the
4476 @option{-l@var{library}} option that needs to be prepended to @code{LIBS}.
4477 @end defmac
4481 @node Library Functions
4482 @section Library Functions
4484 The following macros check for particular C library functions.
4485 If there is no macro specifically defined to check for a function you need,
4486 and you don't need to check for any special properties of
4487 it, then you can use one of the general function-check macros.
4489 @menu
4490 * Function Portability::        Pitfalls with usual functions
4491 * Particular Functions::        Special handling to find certain functions
4492 * Generic Functions::           How to find other functions
4493 @end menu
4495 @node Function Portability
4496 @subsection Portability of C Functions
4497 @cindex Portability of C functions
4498 @cindex C function portability
4500 Most usual functions can either be missing, or be buggy, or be limited
4501 on some architectures.  This section tries to make an inventory of these
4502 portability issues.  By definition, this list always requires
4503 additions.  Please help us keeping it as complete as possible.
4505 @table @asis
4506 @item @code{exit}
4507 @c @fuindex exit
4508 @prindex @code{exit}
4509 On ancient hosts, @code{exit} returned @code{int}.
4510 This is because @code{exit} predates @code{void}, and there was a long
4511 tradition of it returning @code{int}.
4513 On current hosts, the problem more likely is that @code{exit} is not
4514 declared, due to C++ problems of some sort or another.  For this reason
4515 we suggest that test programs not invoke @code{exit}, but return from
4516 @code{main} instead.
4518 @item @code{free}
4519 @c @fuindex free
4520 @prindex @code{free}
4521 The C standard says a call @code{free (NULL)} does nothing, but
4522 some old systems don't support this (e.g., NextStep).
4524 @item @code{isinf}
4525 @itemx @code{isnan}
4526 @c @fuindex isinf
4527 @c @fuindex isnan
4528 @prindex @code{isinf}
4529 @prindex @code{isnan}
4530 The C99 standard says that @code{isinf} and @code{isnan} are
4531 macros.  On some systems just macros are available
4532 (e.g., HP-UX and Solaris 10), on
4533 some systems both macros and functions (e.g., glibc 2.3.2), and on some
4534 systems only functions (e.g., IRIX 6 and Solaris 9).  In some cases
4535 these functions are declared in nonstandard headers like
4536 @code{<sunmath.h>} and defined in non-default libraries like
4537 @option{-lm} or @option{-lsunmath}.
4539 The C99 @code{isinf} and @code{isnan} macros work correctly with
4540 @code{long double} arguments, but pre-C99 systems that use functions
4541 typically assume @code{double} arguments.  On such a system,
4542 @code{isinf} incorrectly returns true for a finite @code{long double}
4543 argument that is outside the range of @code{double}.
4545 The best workaround for these issues is to use gnulib modules
4546 @code{isinf} and @code{isnan} (@pxref{Gnulib}).  But a lighter weight
4547 solution involves code like the following.
4549 @smallexample
4550 #include <math.h>
4552 #ifndef isnan
4553 # define isnan(x) \
4554     (sizeof (x) == sizeof (long double) ? isnan_ld (x) \
4555      : sizeof (x) == sizeof (double) ? isnan_d (x) \
4556      : isnan_f (x))
4557 static inline int isnan_f  (float       x) @{ return x != x; @}
4558 static inline int isnan_d  (double      x) @{ return x != x; @}
4559 static inline int isnan_ld (long double x) @{ return x != x; @}
4560 #endif
4562 #ifndef isinf
4563 # define isinf(x) \
4564     (sizeof (x) == sizeof (long double) ? isinf_ld (x) \
4565      : sizeof (x) == sizeof (double) ? isinf_d (x) \
4566      : isinf_f (x))
4567 static inline int isinf_f  (float       x)
4568 @{ return !isnan (x) && isnan (x - x); @}
4569 static inline int isinf_d  (double      x)
4570 @{ return !isnan (x) && isnan (x - x); @}
4571 static inline int isinf_ld (long double x)
4572 @{ return !isnan (x) && isnan (x - x); @}
4573 #endif
4574 @end smallexample
4576 Use @code{AC_C_INLINE} (@pxref{C Compiler}) so that this code works on
4577 compilers that lack the @code{inline} keyword.  Some optimizing
4578 compilers mishandle these definitions, but systems with that bug
4579 typically have many other floating point corner-case compliance problems
4580 anyway, so it's probably not worth worrying about.
4582 @item @code{malloc}
4583 @c @fuindex malloc
4584 @prindex @code{malloc}
4585 The C standard says a call @code{malloc (0)} is implementation
4586 dependent.  It can return either @code{NULL} or a new non-null pointer.
4587 The latter is more common (e.g., the GNU C Library) but is by
4588 no means universal.  @code{AC_FUNC_MALLOC}
4589 can be used to insist on non-@code{NULL} (@pxref{Particular Functions}).
4591 @item @code{putenv}
4592 @c @fuindex putenv
4593 @prindex @code{putenv}
4594 Posix prefers @code{setenv} to @code{putenv}; among other things,
4595 @code{putenv} is not required of all Posix implementations, but
4596 @code{setenv} is.
4598 Posix specifies that @code{putenv} puts the given string directly in
4599 @code{environ}, but some systems make a copy of it instead (e.g.,
4600 glibc 2.0, or BSD).  And when a copy is made, @code{unsetenv} might
4601 not free it, causing a memory leak (e.g., FreeBSD 4).
4603 On some systems @code{putenv ("FOO")} removes @samp{FOO} from the
4604 environment, but this is not standard usage and it dumps core
4605 on some systems (e.g., AIX).
4607 On MinGW, a call @code{putenv ("FOO=")} removes @samp{FOO} from the
4608 environment, rather than inserting it with an empty value.
4610 @item @code{realloc}
4611 @c @fuindex realloc
4612 @prindex @code{realloc}
4613 The C standard says a call @code{realloc (NULL, size)} is equivalent
4614 to @code{malloc (size)}, but some old systems don't support this (e.g.,
4615 NextStep).
4617 @item @code{signal} handler
4618 @c @fuindex signal
4619 @prindex @code{signal}
4620 @prindex @code{sigaction}
4621 Normally @code{signal} takes a handler function with a return type of
4622 @code{void}, but some old systems required @code{int} instead.  Any
4623 actual @code{int} value returned is not used; this is only a
4624 difference in the function prototype demanded.
4626 All systems we know of in current use return @code{void}.  The
4627 @code{int} was to support K&R C, where of course @code{void} is not
4628 available.  The obsolete macro @code{AC_TYPE_SIGNAL}
4629 (@pxref{AC_TYPE_SIGNAL}) can be used to establish the correct type in
4630 all cases.
4632 In most cases, it is more robust to use @code{sigaction} when it is
4633 available, rather than @code{signal}.
4635 @item @code{snprintf}
4636 @c @fuindex snprintf
4637 @prindex @code{snprintf}
4638 @c @fuindex vsnprintf
4639 @prindex @code{vsnprintf}
4640 The C99 standard says that if the output array isn't big enough
4641 and if no other errors occur, @code{snprintf} and @code{vsnprintf}
4642 truncate the output and return the number of bytes that ought to have
4643 been produced.  Some older systems return the truncated length (e.g.,
4644 GNU C Library 2.0.x or IRIX 6.5), some a negative value
4645 (e.g., earlier GNU C Library versions), and some the buffer
4646 length without truncation (e.g., 32-bit Solaris 7).  Also, some buggy
4647 older systems ignore the length and overrun the buffer (e.g., 64-bit
4648 Solaris 7).
4650 @item @code{sprintf}
4651 @c @fuindex sprintf
4652 @prindex @code{sprintf}
4653 @c @fuindex vsprintf
4654 @prindex @code{vsprintf}
4655 The C standard says @code{sprintf} and @code{vsprintf} return the
4656 number of bytes written.  On some ancient systems (SunOS 4 for
4657 instance) they return the buffer pointer instead, but these no
4658 longer need to be worried about.
4660 @item @code{sscanf}
4661 @c @fuindex sscanf
4662 @prindex @code{sscanf}
4663 On various old systems, e.g., HP-UX 9, @code{sscanf} requires
4664 that its
4665 input string be writable (though it doesn't actually change it).  This
4666 can be a problem when using @command{gcc} since it normally puts
4667 constant strings in read-only memory (@pxref{Incompatibilities,
4668 Incompatibilities of GCC, , gcc, Using and
4669 Porting the GNU Compiler Collection}).  Apparently in some cases even
4670 having format strings read-only can be a problem.
4672 @item @code{strerror_r}
4673 @c @fuindex strerror_r
4674 @prindex @code{strerror_r}
4675 Posix specifies that @code{strerror_r} returns an @code{int}, but many
4676 systems (e.g., GNU C Library version 2.2.4) provide a
4677 different version returning a @code{char *}.  @code{AC_FUNC_STRERROR_R}
4678 can detect which is in use (@pxref{Particular Functions}).
4680 @item @code{strnlen}
4681 @c @fuindex strnlen
4682 @prindex @code{strnlen}
4683 AIX 4.3 provides a broken version which produces the
4684 following results:
4686 @example
4687 strnlen ("foobar", 0) = 0
4688 strnlen ("foobar", 1) = 3
4689 strnlen ("foobar", 2) = 2
4690 strnlen ("foobar", 3) = 1
4691 strnlen ("foobar", 4) = 0
4692 strnlen ("foobar", 5) = 6
4693 strnlen ("foobar", 6) = 6
4694 strnlen ("foobar", 7) = 6
4695 strnlen ("foobar", 8) = 6
4696 strnlen ("foobar", 9) = 6
4697 @end example
4699 @item @code{sysconf}
4700 @c @fuindex sysconf
4701 @prindex @code{sysconf}
4702 @code{_SC_PAGESIZE} is standard, but some older systems (e.g., HP-UX
4703 9) have @code{_SC_PAGE_SIZE} instead.  This can be tested with
4704 @code{#ifdef}.
4706 @item @code{unlink}
4707 @c @fuindex unlink
4708 @prindex @code{unlink}
4709 The Posix spec says that @code{unlink} causes the given file to be
4710 removed only after there are no more open file handles for it.  Some
4711 non-Posix hosts have trouble with this requirement, though,
4712 and some DOS variants even corrupt the file system.
4714 @item @code{unsetenv}
4715 @c @fuindex unsetenv
4716 @prindex @code{unsetenv}
4717 On MinGW, @code{unsetenv} is not available, but a variable @samp{FOO}
4718 can be removed with a call @code{putenv ("FOO=")}, as described under
4719 @code{putenv} above.
4721 @item @code{va_copy}
4722 @c @fuindex va_copy
4723 @prindex @code{va_copy}
4724 The C99 standard provides @code{va_copy} for copying
4725 @code{va_list} variables.  It may be available in older environments
4726 too, though possibly as @code{__va_copy} (e.g., @command{gcc} in strict
4727 pre-C99 mode).  These can be tested with @code{#ifdef}.  A fallback to
4728 @code{memcpy (&dst, &src, sizeof (va_list))} gives maximum
4729 portability.
4731 @item @code{va_list}
4732 @c @fuindex va_list
4733 @prindex @code{va_list}
4734 @code{va_list} is not necessarily just a pointer.  It can be a
4735 @code{struct} (e.g., @command{gcc} on Alpha), which means @code{NULL} is
4736 not portable.  Or it can be an array (e.g., @command{gcc} in some
4737 PowerPC configurations), which means as a function parameter it can be
4738 effectively call-by-reference and library routines might modify the
4739 value back in the caller (e.g., @code{vsnprintf} in the GNU C Library
4740 2.1).
4742 @item Signed @code{>>}
4743 Normally the C @code{>>} right shift of a signed type replicates the
4744 high bit, giving a so-called ``arithmetic'' shift.  But care should be
4745 taken since Standard C doesn't require that behavior.  On those
4746 few processors without a native arithmetic shift (for instance Cray
4747 vector systems) zero bits may be shifted in, the same as a shift of an
4748 unsigned type.
4750 @item Integer @code{/}
4751 C divides signed integers by truncating their quotient toward zero,
4752 yielding the same result as Fortran.  However, before C99 the standard
4753 allowed C implementations to take the floor or ceiling of the quotient
4754 in some cases.  Hardly any implementations took advantage of this
4755 freedom, though, and it's probably not worth worrying about this issue
4756 nowadays.
4757 @end table
4760 @node Particular Functions
4761 @subsection Particular Function Checks
4762 @cindex Function, checking
4764 These macros check for particular C functions---whether they exist, and
4765 in some cases how they respond when given certain arguments.
4767 @anchor{AC_FUNC_ALLOCA}
4768 @defmac AC_FUNC_ALLOCA
4769 @acindex{FUNC_ALLOCA}
4770 @cvindex C_ALLOCA
4771 @cvindex HAVE_ALLOCA_H
4772 @ovindex ALLOCA
4773 @c @fuindex alloca
4774 @prindex @code{alloca}
4775 @hdrindex{alloca.h}
4776 @c @caindex working_alloca_h
4777 Check how to get @code{alloca}.  Tries to get a builtin version by
4778 checking for @file{alloca.h} or the predefined C preprocessor macros
4779 @code{__GNUC__} and @code{_AIX}.  If this macro finds @file{alloca.h},
4780 it defines @code{HAVE_ALLOCA_H}.
4782 If those attempts fail, it looks for the function in the standard C
4783 library.  If any of those methods succeed, it defines
4784 @code{HAVE_ALLOCA}.  Otherwise, it sets the output variable
4785 @code{ALLOCA} to @samp{$@{LIBOBJDIR@}alloca.o} and defines
4786 @code{C_ALLOCA} (so programs can periodically call @samp{alloca (0)} to
4787 garbage collect).  This variable is separate from @code{LIBOBJS} so
4788 multiple programs can share the value of @code{ALLOCA} without needing
4789 to create an actual library, in case only some of them use the code in
4790 @code{LIBOBJS}.  The @samp{$@{LIBOBJDIR@}} prefix serves the same
4791 purpose as in @code{LIBOBJS} (@pxref{AC_LIBOBJ vs LIBOBJS}).
4793 This macro does not try to get @code{alloca} from the System V R3
4794 @file{libPW} or the System V R4 @file{libucb} because those libraries
4795 contain some incompatible functions that cause trouble.  Some versions
4796 do not even contain @code{alloca} or contain a buggy version.  If you
4797 still want to use their @code{alloca}, use @code{ar} to extract
4798 @file{alloca.o} from them instead of compiling @file{alloca.c}.
4800 Source files that use @code{alloca} should start with a piece of code
4801 like the following, to declare it properly.
4803 @example
4804 @group
4805 #ifdef HAVE_ALLOCA_H
4806 # include <alloca.h>
4807 #elif defined __GNUC__
4808 # define alloca __builtin_alloca
4809 #elif defined _AIX
4810 # define alloca __alloca
4811 #elif defined _MSC_VER
4812 # include <malloc.h>
4813 # define alloca _alloca
4814 #else
4815 # include <stddef.h>
4816 # ifdef  __cplusplus
4817 extern "C"
4818 # endif
4819 void *alloca (size_t);
4820 #endif
4821 @end group
4822 @end example
4823 @end defmac
4825 @defmac AC_FUNC_CHOWN
4826 @acindex{FUNC_CHOWN}
4827 @cvindex HAVE_CHOWN
4828 @c @fuindex chown
4829 @prindex @code{chown}
4830 @caindex func_chown_works
4831 If the @code{chown} function is available and works (in particular, it
4832 should accept @option{-1} for @code{uid} and @code{gid}), define
4833 @code{HAVE_CHOWN}.  The result of this macro is cached in the
4834 @code{ac_cv_func_chown_works} variable.
4835 @end defmac
4837 @anchor{AC_FUNC_CLOSEDIR_VOID}
4838 @defmac AC_FUNC_CLOSEDIR_VOID
4839 @acindex{FUNC_CLOSEDIR_VOID}
4840 @cvindex CLOSEDIR_VOID
4841 @c @fuindex closedir
4842 @prindex @code{closedir}
4843 @caindex func_closedir_void
4844 If the @code{closedir} function does not return a meaningful value,
4845 define @code{CLOSEDIR_VOID}.  Otherwise, callers ought to check its
4846 return value for an error indicator.
4848 Currently this test is implemented by running a test program.  When
4849 cross compiling the pessimistic assumption that @code{closedir} does not
4850 return a meaningful value is made.
4852 The result of this macro is cached in the @code{ac_cv_func_closedir_void}
4853 variable.
4855 This macro is obsolescent, as @code{closedir} returns a meaningful value
4856 on current systems.  New programs need not use this macro.
4857 @end defmac
4859 @defmac AC_FUNC_ERROR_AT_LINE
4860 @acindex{FUNC_ERROR_AT_LINE}
4861 @c @fuindex error_at_line
4862 @prindex @code{error_at_line}
4863 @caindex lib_error_at_line
4864 If the @code{error_at_line} function is not found, require an
4865 @code{AC_LIBOBJ} replacement of @samp{error}.
4867 The result of this macro is cached in the @code{ac_cv_lib_error_at_line}
4868 variable.
4869 @end defmac
4871 @defmac AC_FUNC_FNMATCH
4872 @acindex{FUNC_FNMATCH}
4873 @c @fuindex fnmatch
4874 @prindex @code{fnmatch}
4875 @caindex func_fnmatch_works
4876 If the @code{fnmatch} function conforms to Posix, define
4877 @code{HAVE_FNMATCH}.  Detect common implementation bugs, for example,
4878 the bugs in Solaris 2.4.
4880 Unlike the other specific
4881 @code{AC_FUNC} macros, @code{AC_FUNC_FNMATCH} does not replace a
4882 broken/missing @code{fnmatch}.  This is for historical reasons.
4883 See @code{AC_REPLACE_FNMATCH} below.
4885 The result of this macro is cached in the @code{ac_cv_func_fnmatch_works}
4886 variable.
4888 This macro is obsolescent.  New programs should use Gnulib's
4889 @code{fnmatch-posix} module.  @xref{Gnulib}.
4890 @end defmac
4892 @defmac AC_FUNC_FNMATCH_GNU
4893 @acindex{FUNC_FNMATCH_GNU}
4894 @c @fuindex fnmatch
4895 @prindex @code{fnmatch}
4896 @caindex func_fnmatch_gnu
4897 Behave like @code{AC_REPLACE_FNMATCH} (@emph{replace}) but also test
4898 whether @code{fnmatch} supports GNU extensions.  Detect common
4899 implementation bugs, for example, the bugs in the GNU C
4900 Library 2.1.
4902 The result of this macro is cached in the @code{ac_cv_func_fnmatch_gnu}
4903 variable.
4905 This macro is obsolescent.  New programs should use Gnulib's
4906 @code{fnmatch-gnu} module.  @xref{Gnulib}.
4907 @end defmac
4909 @anchor{AC_FUNC_FORK}
4910 @defmac AC_FUNC_FORK
4911 @acindex{FUNC_FORK}
4912 @cvindex HAVE_VFORK_H
4913 @cvindex HAVE_WORKING_FORK
4914 @cvindex HAVE_WORKING_VFORK
4915 @cvindex vfork
4916 @c @fuindex fork
4917 @prindex @code{fork}
4918 @c @fuindex vfork
4919 @prindex @code{vfork}
4920 @hdrindex{vfork.h}
4921 @c @caindex func_fork
4922 @c @caindex func_fork_works
4923 This macro checks for the @code{fork} and @code{vfork} functions.  If a
4924 working @code{fork} is found, define @code{HAVE_WORKING_FORK}.  This macro
4925 checks whether @code{fork} is just a stub by trying to run it.
4927 If @file{vfork.h} is found, define @code{HAVE_VFORK_H}.  If a working
4928 @code{vfork} is found, define @code{HAVE_WORKING_VFORK}.  Otherwise,
4929 define @code{vfork} to be @code{fork} for backward compatibility with
4930 previous versions of @command{autoconf}.  This macro checks for several known
4931 errors in implementations of @code{vfork} and considers the system to not
4932 have a working @code{vfork} if it detects any of them.  It is not considered
4933 to be an implementation error if a child's invocation of @code{signal}
4934 modifies the parent's signal handler, since child processes rarely change
4935 their signal handlers.
4937 Since this macro defines @code{vfork} only for backward compatibility with
4938 previous versions of @command{autoconf} you're encouraged to define it
4939 yourself in new code:
4940 @example
4941 @group
4942 #ifndef HAVE_WORKING_VFORK
4943 # define vfork fork
4944 #endif
4945 @end group
4946 @end example
4947 @end defmac
4949 @defmac AC_FUNC_FSEEKO
4950 @acindex{FUNC_FSEEKO}
4951 @cvindex _LARGEFILE_SOURCE
4952 @cvindex HAVE_FSEEKO
4953 @c @fuindex fseeko
4954 @prindex @code{fseeko}
4955 @c @fuindex ftello
4956 @prindex @code{ftello}
4957 @c @caindex sys_largefile_source
4958 If the @code{fseeko} function is available, define @code{HAVE_FSEEKO}.
4959 Define @code{_LARGEFILE_SOURCE} if necessary to make the prototype
4960 visible on some systems (e.g., glibc 2.2).  Otherwise linkage problems
4961 may occur when compiling with @code{AC_SYS_LARGEFILE} on
4962 largefile-sensitive systems where @code{off_t} does not default to a
4963 64bit entity.  All systems with @code{fseeko} also supply @code{ftello}.
4964 @end defmac
4966 @defmac AC_FUNC_GETGROUPS
4967 @acindex{FUNC_GETGROUPS}
4968 @cvindex HAVE_GETGROUPS
4969 @ovindex GETGROUPS_LIBS
4970 @c @fuindex getgroups
4971 @prindex @code{getgroups}
4972 @caindex func_getgroups_works
4973 If the @code{getgroups} function is available and works (unlike on
4974 Ultrix 4.3, where @samp{getgroups (0, 0)} always fails), define
4975 @code{HAVE_GETGROUPS}.  Set @code{GETGROUPS_LIBS} to any libraries
4976 needed to get that function.  This macro runs @code{AC_TYPE_GETGROUPS}.
4977 @end defmac
4979 @anchor{AC_FUNC_GETLOADAVG}
4980 @defmac AC_FUNC_GETLOADAVG
4981 @acindex{FUNC_GETLOADAVG}
4982 @cvindex SVR4
4983 @cvindex DGUX
4984 @cvindex UMAX
4985 @cvindex UMAX4_3
4986 @cvindex HAVE_NLIST_H
4987 @cvindex NLIST_NAME_UNION
4988 @cvindex GETLOADAVG_PRIVILEGED
4989 @cvindex NEED_SETGID
4990 @cvindex C_GETLOADAVG
4991 @ovindex LIBOBJS
4992 @ovindex NEED_SETGID
4993 @ovindex KMEM_GROUP
4994 @ovindex GETLOADAVG_LIBS
4995 @c @fuindex getloadavg
4996 @prindex @code{getloadavg}
4997 Check how to get the system load averages.  To perform its tests
4998 properly, this macro needs the file @file{getloadavg.c}; therefore, be
4999 sure to set the @code{AC_LIBOBJ} replacement directory properly (see
5000 @ref{Generic Functions}, @code{AC_CONFIG_LIBOBJ_DIR}).
5002 If the system has the @code{getloadavg} function, define
5003 @code{HAVE_GETLOADAVG}, and set @code{GETLOADAVG_LIBS} to any libraries
5004 necessary to get that function.  Also add @code{GETLOADAVG_LIBS} to
5005 @code{LIBS}.  Otherwise, require an @code{AC_LIBOBJ} replacement for
5006 @samp{getloadavg} with source code in @file{@var{dir}/getloadavg.c}, and
5007 possibly define several other C preprocessor macros and output
5008 variables:
5010 @enumerate
5011 @item
5012 Define @code{C_GETLOADAVG}.
5014 @item
5015 Define @code{SVR4}, @code{DGUX}, @code{UMAX}, or @code{UMAX4_3} if on
5016 those systems.
5018 @item
5019 @hdrindex{nlist.h}
5020 If @file{nlist.h} is found, define @code{HAVE_NLIST_H}.
5022 @item
5023 If @samp{struct nlist} has an @samp{n_un.n_name} member, define
5024 @code{HAVE_STRUCT_NLIST_N_UN_N_NAME}.  The obsolete symbol
5025 @code{NLIST_NAME_UNION} is still defined, but do not depend upon it.
5027 @item
5028 Programs may need to be installed set-group-ID (or set-user-ID) for
5029 @code{getloadavg} to work.  In this case, define
5030 @code{GETLOADAVG_PRIVILEGED}, set the output variable @code{NEED_SETGID}
5031 to @samp{true} (and otherwise to @samp{false}), and set
5032 @code{KMEM_GROUP} to the name of the group that should own the installed
5033 program.
5034 @end enumerate
5036 The @code{AC_FUNC_GETLOADAVG} macro is obsolescent.  New programs should
5037 use Gnulib's @code{getloadavg} module.  @xref{Gnulib}.
5038 @end defmac
5040 @anchor{AC_FUNC_GETMNTENT}
5041 @defmac AC_FUNC_GETMNTENT
5042 @acindex{FUNC_GETMNTENT}
5043 @cvindex HAVE_GETMNTENT
5044 @c @fuindex getmntent
5045 @prindex @code{getmntent}
5046 @caindex search_getmntent
5047 Check for @code{getmntent} in the standard C library, and then in the
5048 @file{sun}, @file{seq}, and @file{gen} libraries, for UNICOS,
5049 IRIX 4, PTX, and UnixWare, respectively.  Then, if
5050 @code{getmntent} is available, define @code{HAVE_GETMNTENT} and set
5051 @code{ac_cv_func_getmntent} to @code{yes}.  Otherwise set
5052 @code{ac_cv_func_getmntent} to @code{no}.
5054 The result of this macro can be overridden by setting the cache variable
5055 @code{ac_cv_search_getmntent}.
5056 @end defmac
5058 @defmac AC_FUNC_GETPGRP
5059 @acindex{FUNC_GETPGRP}
5060 @cvindex GETPGRP_VOID
5061 @c @fuindex getpgid
5062 @c @fuindex getpgrp
5063 @prindex @code{getpgid}
5064 @prindex @code{getpgrp}
5065 @caindex func_getpgrp_void
5066 Define @code{GETPGRP_VOID} if it is an error to pass 0 to
5067 @code{getpgrp}; this is the Posix behavior.  On older BSD
5068 systems, you must pass 0 to @code{getpgrp}, as it takes an argument and
5069 behaves like Posix's @code{getpgid}.
5071 @example
5072 #ifdef GETPGRP_VOID
5073   pid = getpgrp ();
5074 #else
5075   pid = getpgrp (0);
5076 #endif
5077 @end example
5079 This macro does not check whether
5080 @code{getpgrp} exists at all; if you need to work in that situation,
5081 first call @code{AC_CHECK_FUNC} for @code{getpgrp}.
5083 The result of this macro is cached in the @code{ac_cv_func_getpgrp_void}
5084 variable.
5086 This macro is obsolescent, as current systems have a @code{getpgrp}
5087 whose signature conforms to Posix.  New programs need not use this macro.
5088 @end defmac
5090 @defmac AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
5091 @acindex{FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK}
5092 @cvindex LSTAT_FOLLOWS_SLASHED_SYMLINK
5093 @c @fuindex lstat
5094 @prindex @code{lstat}
5095 @caindex func_lstat_dereferences_slashed_symlink
5096 If @file{link} is a symbolic link, then @code{lstat} should treat
5097 @file{link/} the same as @file{link/.}.  However, many older
5098 @code{lstat} implementations incorrectly ignore trailing slashes.
5100 It is safe to assume that if @code{lstat} incorrectly ignores
5101 trailing slashes, then other symbolic-link-aware functions like
5102 @code{unlink} also incorrectly ignore trailing slashes.
5104 If @code{lstat} behaves properly, define
5105 @code{LSTAT_FOLLOWS_SLASHED_SYMLINK}, otherwise require an
5106 @code{AC_LIBOBJ} replacement of @code{lstat}.
5108 The result of this macro is cached in the
5109 @code{ac_cv_func_lstat_dereferences_slashed_symlink} variable.
5110 @end defmac
5112 @defmac AC_FUNC_MALLOC
5113 @acindex{FUNC_MALLOC}
5114 @cvindex HAVE_MALLOC
5115 @cvindex malloc
5116 @c @fuindex malloc
5117 @prindex @code{malloc}
5118 @caindex func_malloc_0_nonnull
5119 If the @code{malloc} function is compatible with the GNU C
5120 library @code{malloc} (i.e., @samp{malloc (0)} returns a valid
5121 pointer), define @code{HAVE_MALLOC} to 1.  Otherwise define
5122 @code{HAVE_MALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
5123 @samp{malloc}, and define @code{malloc} to @code{rpl_malloc} so that the
5124 native @code{malloc} is not used in the main project.
5126 Typically, the replacement file @file{malloc.c} should look like (note
5127 the @samp{#undef malloc}):
5129 @verbatim
5130 #include <config.h>
5131 #undef malloc
5133 #include <sys/types.h>
5135 void *malloc ();
5137 /* Allocate an N-byte block of memory from the heap.
5138    If N is zero, allocate a 1-byte block.  */
5140 void *
5141 rpl_malloc (size_t n)
5143   if (n == 0)
5144     n = 1;
5145   return malloc (n);
5147 @end verbatim
5149 The result of this macro is cached in the
5150 @code{ac_cv_func_malloc_0_nonnull} variable.
5151 @end defmac
5153 @defmac AC_FUNC_MBRTOWC
5154 @acindex{FUNC_MBRTOWC}
5155 @cvindex HAVE_MBRTOWC
5156 @c @fuindex mbrtowc
5157 @prindex @code{mbrtowc}
5158 @caindex func_mbrtowc
5159 Define @code{HAVE_MBRTOWC} to 1 if the function @code{mbrtowc} and the
5160 type @code{mbstate_t} are properly declared.
5162 The result of this macro is cached in the @code{ac_cv_func_mbrtowc}
5163 variable.
5164 @end defmac
5166 @defmac AC_FUNC_MEMCMP
5167 @acindex{FUNC_MEMCMP}
5168 @ovindex LIBOBJS
5169 @c @fuindex memcmp
5170 @prindex @code{memcmp}
5171 @caindex func_memcmp_working
5172 If the @code{memcmp} function is not available, or does not work on
5173 8-bit data (like the one on SunOS 4.1.3), or fails when comparing 16
5174 bytes or more and with at least one buffer not starting on a 4-byte
5175 boundary (such as the one on NeXT x86 OpenStep), require an
5176 @code{AC_LIBOBJ} replacement for @samp{memcmp}.
5178 The result of this macro is cached in the
5179 @code{ac_cv_func_memcmp_working} variable.
5181 This macro is obsolescent, as current systems have a working
5182 @code{memcmp}.  New programs need not use this macro.
5183 @end defmac
5185 @defmac AC_FUNC_MKTIME
5186 @acindex{FUNC_MKTIME}
5187 @ovindex LIBOBJS
5188 @c @fuindex mktime
5189 @prindex @code{mktime}
5190 @caindex func_working_mktime
5191 If the @code{mktime} function is not available, or does not work
5192 correctly, require an @code{AC_LIBOBJ} replacement for @samp{mktime}.
5193 For the purposes of this test, @code{mktime} should conform to the
5194 Posix standard and should be the inverse of
5195 @code{localtime}.
5197 The result of this macro is cached in the
5198 @code{ac_cv_func_working_mktime} variable.
5199 @end defmac
5201 @anchor{AC_FUNC_MMAP}
5202 @defmac AC_FUNC_MMAP
5203 @acindex{FUNC_MMAP}
5204 @cvindex HAVE_MMAP
5205 @c @fuindex mmap
5206 @prindex @code{mmap}
5207 @caindex func_mmap_fixed_mapped
5208 If the @code{mmap} function exists and works correctly, define
5209 @code{HAVE_MMAP}.  This checks only private fixed mapping of already-mapped
5210 memory.
5212 The result of this macro is cached in the
5213 @code{ac_cv_func_mmap_fixed_mapped} variable.
5214 @end defmac
5216 @defmac AC_FUNC_OBSTACK
5217 @acindex{FUNC_OBSTACK}
5218 @cvindex HAVE_OBSTACK
5219 @cindex obstack
5220 @caindex func_obstack
5221 If the obstacks are found, define @code{HAVE_OBSTACK}, else require an
5222 @code{AC_LIBOBJ} replacement for @samp{obstack}.
5224 The result of this macro is cached in the @code{ac_cv_func_obstack}
5225 variable.
5226 @end defmac
5228 @defmac AC_FUNC_REALLOC
5229 @acindex{FUNC_REALLOC}
5230 @cvindex HAVE_REALLOC
5231 @cvindex realloc
5232 @c @fuindex realloc
5233 @prindex @code{realloc}
5234 @caindex func_realloc_0_nonnull
5235 If the @code{realloc} function is compatible with the GNU C
5236 library @code{realloc} (i.e., @samp{realloc (NULL, 0)} returns a
5237 valid pointer), define @code{HAVE_REALLOC} to 1.  Otherwise define
5238 @code{HAVE_REALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
5239 @samp{realloc}, and define @code{realloc} to @code{rpl_realloc} so that
5240 the native @code{realloc} is not used in the main project.  See
5241 @code{AC_FUNC_MALLOC} for details.
5243 The result of this macro is cached in the
5244 @code{ac_cv_func_realloc_0_nonnull} variable.
5245 @end defmac
5247 @defmac AC_FUNC_SELECT_ARGTYPES
5248 @acindex{FUNC_SELECT_ARGTYPES}
5249 @cvindex SELECT_TYPE_ARG1
5250 @cvindex SELECT_TYPE_ARG234
5251 @cvindex SELECT_TYPE_ARG5
5252 @c @fuindex select
5253 @prindex @code{select}
5254 @c @caindex func_select_args
5255 Determines the correct type to be passed for each of the
5256 @code{select} function's arguments, and defines those types
5257 in @code{SELECT_TYPE_ARG1}, @code{SELECT_TYPE_ARG234}, and
5258 @code{SELECT_TYPE_ARG5} respectively.  @code{SELECT_TYPE_ARG1} defaults
5259 to @samp{int}, @code{SELECT_TYPE_ARG234} defaults to @samp{int *},
5260 and @code{SELECT_TYPE_ARG5} defaults to @samp{struct timeval *}.
5262 This macro is obsolescent, as current systems have a @code{select} whose
5263 signature conforms to Posix.  New programs need not use this macro.
5264 @end defmac
5266 @defmac AC_FUNC_SETPGRP
5267 @acindex{FUNC_SETPGRP}
5268 @cvindex SETPGRP_VOID
5269 @c @fuindex setpgrp
5270 @prindex @code{setpgrp}
5271 @caindex func_setpgrp_void
5272 If @code{setpgrp} takes no argument (the Posix version), define
5273 @code{SETPGRP_VOID}.  Otherwise, it is the BSD version, which takes
5274 two process IDs as arguments.  This macro does not check whether
5275 @code{setpgrp} exists at all; if you need to work in that situation,
5276 first call @code{AC_CHECK_FUNC} for @code{setpgrp}.
5278 The result of this macro is cached in the @code{ac_cv_func_setpgrp_void}
5279 variable.
5281 This macro is obsolescent, as current systems have a @code{setpgrp}
5282 whose signature conforms to Posix.  New programs need not use this macro.
5283 @end defmac
5285 @defmac AC_FUNC_STAT
5286 @defmacx AC_FUNC_LSTAT
5287 @acindex{FUNC_STAT}
5288 @acindex{FUNC_LSTAT}
5289 @cvindex HAVE_STAT_EMPTY_STRING_BUG
5290 @cvindex HAVE_LSTAT_EMPTY_STRING_BUG
5291 @c @fuindex stat
5292 @prindex @code{stat}
5293 @c @fuindex lstat
5294 @prindex @code{lstat}
5295 @caindex func_stat_empty_string_bug
5296 @caindex func_lstat_empty_string_bug
5297 Determine whether @code{stat} or @code{lstat} have the bug that it
5298 succeeds when given the zero-length file name as argument.  The @code{stat}
5299 and @code{lstat} from SunOS 4.1.4 and the Hurd (as of 1998-11-01) do
5300 this.
5302 If it does, then define @code{HAVE_STAT_EMPTY_STRING_BUG} (or
5303 @code{HAVE_LSTAT_EMPTY_STRING_BUG}) and ask for an @code{AC_LIBOBJ}
5304 replacement of it.
5306 The results of these macros are cached in the
5307 @code{ac_cv_func_stat_empty_string_bug} and the
5308 @code{ac_cv_func_lstat_empty_string_bug} variables, respectively.
5310 These macros are obsolescent, as no current systems have the bug.
5311 New programs need not use these macros.
5312 @end defmac
5314 @anchor{AC_FUNC_STRCOLL}
5315 @defmac AC_FUNC_STRCOLL
5316 @acindex{FUNC_STRCOLL}
5317 @cvindex HAVE_STRCOLL
5318 @c @fuindex strcoll
5319 @prindex @code{strcoll}
5320 @caindex func_strcoll_works
5321 If the @code{strcoll} function exists and works correctly, define
5322 @code{HAVE_STRCOLL}.  This does a bit more than
5323 @samp{AC_CHECK_FUNCS(strcoll)}, because some systems have incorrect
5324 definitions of @code{strcoll} that should not be used.
5326 The result of this macro is cached in the @code{ac_cv_func_strcoll_works}
5327 variable.
5328 @end defmac
5330 @defmac AC_FUNC_STRERROR_R
5331 @acindex{FUNC_STRERROR_R}
5332 @cvindex HAVE_STRERROR_R
5333 @cvindex HAVE_DECL_STRERROR_R
5334 @cvindex STRERROR_R_CHAR_P
5335 @c @fuindex strerror_r
5336 @caindex func_strerror_r_char_p
5337 @prindex @code{strerror_r}
5338 If @code{strerror_r} is available, define @code{HAVE_STRERROR_R}, and if
5339 it is declared, define @code{HAVE_DECL_STRERROR_R}.  If it returns a
5340 @code{char *} message, define @code{STRERROR_R_CHAR_P}; otherwise it
5341 returns an @code{int} error number.  The Thread-Safe Functions option of
5342 Posix requires @code{strerror_r} to return @code{int}, but
5343 many systems (including, for example, version 2.2.4 of the GNU C
5344 Library) return a @code{char *} value that is not necessarily equal to
5345 the buffer argument.
5347 The result of this macro is cached in the
5348 @code{ac_cv_func_strerror_r_char_p} variable.
5349 @end defmac
5351 @anchor{AC_FUNC_STRFTIME}
5352 @defmac AC_FUNC_STRFTIME
5353 @acindex{FUNC_STRFTIME}
5354 @cvindex HAVE_STRFTIME
5355 @c @fuindex strftime
5356 @prindex @code{strftime}
5357 Check for @code{strftime} in the @file{intl} library, for SCO Unix.
5358 Then, if @code{strftime} is available, define @code{HAVE_STRFTIME}.
5360 This macro is obsolescent, as no current systems require the @file{intl}
5361 library for @code{strftime}.  New programs need not use this macro.
5362 @end defmac
5364 @defmac AC_FUNC_STRTOD
5365 @acindex{FUNC_STRTOD}
5366 @ovindex POW_LIB
5367 @c @fuindex strtod
5368 @prindex @code{strtod}
5369 @caindex func_strtod
5370 @caindex func_pow
5371 If the @code{strtod} function does not exist or doesn't work correctly,
5372 ask for an @code{AC_LIBOBJ} replacement of @samp{strtod}.  In this case,
5373 because @file{strtod.c} is likely to need @samp{pow}, set the output
5374 variable @code{POW_LIB} to the extra library needed.
5376 This macro caches its result in the @code{ac_cv_func_strtod} variable
5377 and depends upon the result in the @code{ac_cv_func_pow} variable.
5378 @end defmac
5380 @defmac AC_FUNC_STRTOLD
5381 @acindex{FUNC_STRTOLD}
5382 @cvindex HAVE_STRTOLD
5383 @prindex @code{strtold}
5384 @caindex func_strtold
5385 If the @code{strtold} function exists and conforms to C99, define
5386 @code{HAVE_STRTOLD}.
5388 This macro caches its result in the @code{ac_cv_func_strtold} variable.
5389 @end defmac
5391 @defmac AC_FUNC_STRNLEN
5392 @acindex{FUNC_STRNLEN}
5393 @cvindex HAVE_STRNLEN
5394 @c @fuindex strnlen
5395 @prindex @code{strnlen}
5396 @caindex func_strnlen_working
5397 If the @code{strnlen} function is not available, or is buggy (like the one
5398 from AIX 4.3), require an @code{AC_LIBOBJ} replacement for it.
5400 This macro caches its result in the @code{ac_cv_func_strnlen_working}
5401 variable.
5402 @end defmac
5404 @anchor{AC_FUNC_UTIME_NULL}
5405 @defmac AC_FUNC_UTIME_NULL
5406 @acindex{FUNC_UTIME_NULL}
5407 @cvindex HAVE_UTIME_NULL
5408 @c @fuindex utime
5409 @prindex @code{utime}
5410 @caindex func_utime_null
5411 If @samp{utime (@var{file}, NULL)} sets @var{file}'s timestamp to
5412 the present, define @code{HAVE_UTIME_NULL}.
5414 This macro caches its result in the @code{ac_cv_func_utime_null}
5415 variable.
5417 This macro is obsolescent, as all current systems have a @code{utime}
5418 that behaves this way.  New programs need not use this macro.
5419 @end defmac
5421 @anchor{AC_FUNC_VPRINTF}
5422 @defmac AC_FUNC_VPRINTF
5423 @acindex{FUNC_VPRINTF}
5424 @cvindex HAVE_VPRINTF
5425 @cvindex HAVE_DOPRNT
5426 @c @fuindex vprintf
5427 @prindex @code{vprintf}
5428 @c @fuindex vsprintf
5429 @prindex @code{vsprintf}
5430 If @code{vprintf} is found, define @code{HAVE_VPRINTF}.  Otherwise, if
5431 @code{_doprnt} is found, define @code{HAVE_DOPRNT}.  (If @code{vprintf}
5432 is available, you may assume that @code{vfprintf} and @code{vsprintf}
5433 are also available.)
5435 This macro is obsolescent, as all current systems have @code{vprintf}.
5436 New programs need not use this macro.
5437 @end defmac
5439 @defmac AC_REPLACE_FNMATCH
5440 @acindex{REPLACE_FNMATCH}
5441 @c @fuindex fnmatch
5442 @prindex @code{fnmatch}
5443 @hdrindex{fnmatch.h}
5444 @caindex func_fnmatch_works
5445 If the @code{fnmatch} function does not conform to Posix (see
5446 @code{AC_FUNC_FNMATCH}), ask for its @code{AC_LIBOBJ} replacement.
5448 The files @file{fnmatch.c}, @file{fnmatch_loop.c}, and @file{fnmatch_.h}
5449 in the @code{AC_LIBOBJ} replacement directory are assumed to contain a
5450 copy of the source code of GNU @code{fnmatch}.  If necessary,
5451 this source code is compiled as an @code{AC_LIBOBJ} replacement, and the
5452 @file{fnmatch_.h} file is linked to @file{fnmatch.h} so that it can be
5453 included in place of the system @code{<fnmatch.h>}.
5455 This macro caches its result in the @code{ac_cv_func_fnmatch_works}
5456 variable.
5458 This macro is obsolescent, as it assumes the use of particular source
5459 files.  New programs should use Gnulib's @code{fnmatch-posix} module,
5460 which provides this macro along with the source files.  @xref{Gnulib}.
5461 @end defmac
5465 @node Generic Functions
5466 @subsection Generic Function Checks
5468 These macros are used to find functions not covered by the ``particular''
5469 test macros.  If the functions might be in libraries other than the
5470 default C library, first call @code{AC_CHECK_LIB} for those libraries.
5471 If you need to check the behavior of a function as well as find out
5472 whether it is present, you have to write your own test for
5473 it (@pxref{Writing Tests}).
5475 @anchor{AC_CHECK_FUNC}
5476 @defmac AC_CHECK_FUNC (@var{function}, @ovar{action-if-found}, @
5477   @ovar{action-if-not-found})
5478 @acindex{CHECK_FUNC}
5479 @caindex func_@var{function}
5480 If C function @var{function} is available, run shell commands
5481 @var{action-if-found}, otherwise @var{action-if-not-found}.  If you just
5482 want to define a symbol if the function is available, consider using
5483 @code{AC_CHECK_FUNCS} instead.  This macro checks for functions with C
5484 linkage even when @code{AC_LANG(C++)} has been called, since C is more
5485 standardized than C++.  (@pxref{Language Choice}, for more information
5486 about selecting the language for checks.)
5488 This macro caches its result in the @code{ac_cv_func_@var{function}}
5489 variable.
5490 @end defmac
5492 @anchor{AC_CHECK_FUNCS}
5493 @defmac AC_CHECK_FUNCS (@var{function}@dots{}, @ovar{action-if-found}, @
5494   @ovar{action-if-not-found})
5495 @acindex{CHECK_FUNCS}
5496 @cvindex HAVE_@var{function}
5497 For each @var{function} enumerated in the blank-or-newline-separated argument
5498 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5499 If @var{action-if-found} is given, it is additional shell code to
5500 execute when one of the functions is found.  You can give it a value of
5501 @samp{break} to break out of the loop on the first match.  If
5502 @var{action-if-not-found} is given, it is executed when one of the
5503 functions is not found.
5505 Results are cached for each @var{function} as in @code{AC_CHECK_FUNC}.
5506 @end defmac
5508 @defmac AC_CHECK_FUNCS_ONCE (@var{function}@dots{})
5509 @acindex{CHECK_FUNCS_ONCE}
5510 @cvindex HAVE_@var{function}
5511 For each @var{function} enumerated in the blank-or-newline-separated argument
5512 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5513 This is a once-only variant of @code{AC_CHECK_FUNCS}.  It generates the
5514 checking code at most once, so that @command{configure} is smaller and
5515 faster; but the checks cannot be conditionalized and are always done once,
5516 early during the @command{configure} run.
5517 @end defmac
5519 @sp 1
5521 Autoconf follows a philosophy that was formed over the years by those
5522 who have struggled for portability: isolate the portability issues in
5523 specific files, and then program as if you were in a Posix
5524 environment.  Some functions may be missing or unfixable, and your
5525 package must be ready to replace them.
5527 Suitable replacements for many such problem functions are available from
5528 Gnulib (@pxref{Gnulib}).
5530 @defmac AC_LIBOBJ (@var{function})
5531 @acindex{LIBOBJ}
5532 @ovindex LIBOBJS
5533 Specify that @samp{@var{function}.c} must be included in the executables
5534 to replace a missing or broken implementation of @var{function}.
5536 Technically, it adds @samp{@var{function}.$ac_objext} to the output
5537 variable @code{LIBOBJS} if it is not already in, and calls
5538 @code{AC_LIBSOURCE} for @samp{@var{function}.c}.  You should not
5539 directly change @code{LIBOBJS}, since this is not traceable.
5540 @end defmac
5542 @defmac AC_LIBSOURCE (@var{file})
5543 @acindex{LIBSOURCE}
5544 Specify that @var{file} might be needed to compile the project.  If you
5545 need to know what files might be needed by a @file{configure.ac}, you
5546 should trace @code{AC_LIBSOURCE}.  @var{file} must be a literal.
5548 This macro is called automatically from @code{AC_LIBOBJ}, but you must
5549 call it explicitly if you pass a shell variable to @code{AC_LIBOBJ}.  In
5550 that case, since shell variables cannot be traced statically, you must
5551 pass to @code{AC_LIBSOURCE} any possible files that the shell variable
5552 might cause @code{AC_LIBOBJ} to need.  For example, if you want to pass
5553 a variable @code{$foo_or_bar} to @code{AC_LIBOBJ} that holds either
5554 @code{"foo"} or @code{"bar"}, you should do:
5556 @example
5557 AC_LIBSOURCE([foo.c])
5558 AC_LIBSOURCE([bar.c])
5559 AC_LIBOBJ([$foo_or_bar])
5560 @end example
5562 @noindent
5563 There is usually a way to avoid this, however, and you are encouraged to
5564 simply call @code{AC_LIBOBJ} with literal arguments.
5566 Note that this macro replaces the obsolete @code{AC_LIBOBJ_DECL}, with
5567 slightly different semantics: the old macro took the function name,
5568 e.g., @code{foo}, as its argument rather than the file name.
5569 @end defmac
5571 @defmac AC_LIBSOURCES (@var{files})
5572 @acindex{LIBSOURCES}
5573 Like @code{AC_LIBSOURCE}, but accepts one or more @var{files} in a
5574 comma-separated M4 list.  Thus, the above example might be rewritten:
5576 @example
5577 AC_LIBSOURCES([foo.c, bar.c])
5578 AC_LIBOBJ([$foo_or_bar])
5579 @end example
5580 @end defmac
5582 @defmac AC_CONFIG_LIBOBJ_DIR (@var{directory})
5583 @acindex{CONFIG_LIBOBJ_DIR}
5584 Specify that @code{AC_LIBOBJ} replacement files are to be found in
5585 @var{directory}, a name relative to the top level of the
5586 source tree.  The replacement directory defaults to @file{.}, the top
5587 level directory, and the most typical value is @file{lib}, corresponding
5588 to @samp{AC_CONFIG_LIBOBJ_DIR([lib])}.
5590 @command{configure} might need to know the replacement directory for the
5591 following reasons: (i) some checks use the replacement files, (ii) some
5592 macros bypass broken system headers by installing links to the
5593 replacement headers (iii) when used in conjunction with Automake,
5594 within each makefile, @var{directory} is used as a relative path
5595 from @code{$(top_srcdir)} to each object named in @code{LIBOBJS} and
5596 @code{LTLIBOBJS}, etc.
5597 @end defmac
5599 @sp 1
5601 It is common to merely check for the existence of a function, and ask
5602 for its @code{AC_LIBOBJ} replacement if missing.  The following macro is
5603 a convenient shorthand.
5605 @defmac AC_REPLACE_FUNCS (@var{function}@dots{})
5606 @acindex{REPLACE_FUNCS}
5607 @cvindex HAVE_@var{function}
5608 @ovindex LIBOBJS
5609 Like @code{AC_CHECK_FUNCS}, but uses @samp{AC_LIBOBJ(@var{function})} as
5610 @var{action-if-not-found}.  You can declare your replacement function by
5611 enclosing the prototype in @samp{#ifndef HAVE_@var{function}}.  If the
5612 system has the function, it probably declares it in a header file you
5613 should be including, so you shouldn't redeclare it lest your declaration
5614 conflict.
5615 @end defmac
5617 @node Header Files
5618 @section Header Files
5619 @cindex Header, checking
5621 The following macros check for the presence of certain C header files.
5622 If there is no macro specifically defined to check for a header file you need,
5623 and you don't need to check for any special properties of
5624 it, then you can use one of the general header-file check macros.
5626 @menu
5627 * Header Portability::          Collected knowledge on common headers
5628 * Particular Headers::          Special handling to find certain headers
5629 * Generic Headers::             How to find other headers
5630 @end menu
5632 @node Header Portability
5633 @subsection Portability of Headers
5634 @cindex Portability of headers
5635 @cindex Header portability
5637 This section tries to collect knowledge about common headers, and the
5638 problems they cause.  By definition, this list always requires
5639 additions.  Please help us keeping it as complete as possible.
5641 @table @asis
5643 @item @file{limits.h}
5644 C99 says that @file{limits.h} defines @code{LLONG_MIN},
5645 @code{LLONG_MAX}, and @code{ULLONG_MAX}, but many almost-C99
5646 environments (e.g., default GCC 4.0.2 + glibc 2.4) do not
5647 define them.
5649 @item @file{inttypes.h} vs.@: @file{stdint.h}
5650 @hdrindex{inttypes.h}
5651 @hdrindex{stdint.h}
5652 The C99 standard says that @file{inttypes.h} includes
5653 @file{stdint.h}, so there's no need to include @file{stdint.h}
5654 separately in a standard environment.  Some implementations have
5655 @file{inttypes.h} but not @file{stdint.h} (e.g., Solaris 7), but we don't
5656 know of any implementation that has @file{stdint.h} but not
5657 @file{inttypes.h}.
5659 @item @file{linux/irda.h}
5660 @hdrindex{linux/irda.h}
5661 It requires @file{linux/types.h} and @file{sys/socket.h}.
5663 @item @file{linux/random.h}
5664 @hdrindex{linux/random.h}
5665 It requires @file{linux/types.h}.
5667 @item @file{net/if.h}
5668 @hdrindex{net/if.h}
5669 On Darwin, this file requires that @file{sys/socket.h} be included
5670 beforehand.  One should run:
5672 @example
5673 AC_CHECK_HEADERS([sys/socket.h])
5674 AC_CHECK_HEADERS([net/if.h], [], [],
5675 [#include <stdio.h>
5676 #ifdef STDC_HEADERS
5677 # include <stdlib.h>
5678 # include <stddef.h>
5679 #else
5680 # ifdef HAVE_STDLIB_H
5681 #  include <stdlib.h>
5682 # endif
5683 #endif
5684 #ifdef HAVE_SYS_SOCKET_H
5685 # include <sys/socket.h>
5686 #endif
5688 @end example
5690 @item @file{netinet/if_ether.h}
5691 @hdrindex{netinet/if_ether.h}
5692 On Darwin, this file requires that @file{stdio.h} and
5693 @file{sys/socket.h} be included beforehand.  One should run:
5695 @example
5696 AC_CHECK_HEADERS([sys/socket.h])
5697 AC_CHECK_HEADERS([netinet/if_ether.h], [], [],
5698 [#include <stdio.h>
5699 #ifdef STDC_HEADERS
5700 # include <stdlib.h>
5701 # include <stddef.h>
5702 #else
5703 # ifdef HAVE_STDLIB_H
5704 #  include <stdlib.h>
5705 # endif
5706 #endif
5707 #ifdef HAVE_SYS_SOCKET_H
5708 # include <sys/socket.h>
5709 #endif
5711 @end example
5713 @item @file{stdint.h}
5714 See above, item @file{inttypes.h} vs.@: @file{stdint.h}.
5716 @item @file{stdlib.h}
5717 @hdrindex{stdlib.h}
5718 On many systems (e.g., Darwin), @file{stdio.h} is a prerequisite.
5720 @item @file{sys/mount.h}
5721 @hdrindex{sys/mount.h}
5722 On FreeBSD 4.8 on ia32 and using gcc version 2.95.4,
5723 @file{sys/params.h} is a prerequisite.
5725 @item @file{sys/ptem.h}
5726 @hdrindex{sys/ptem.h}
5727 On Solaris 8, @file{sys/stream.h} is a prerequisite.
5729 @item @file{sys/socket.h}
5730 @hdrindex{sys/socket.h}
5731 On Darwin, @file{stdlib.h} is a prerequisite.
5733 @item @file{sys/ucred.h}
5734 @hdrindex{sys/ucred.h}
5735 On Tru64 5.1, @file{sys/types.h} is a prerequisite.
5737 @item @file{X11/extensions/scrnsaver.h}
5738 @hdrindex{X11/extensions/scrnsaver.h}
5739 Using XFree86, this header requires @file{X11/Xlib.h}, which is probably
5740 so required that you might not even consider looking for it.
5742 @example
5743 AC_CHECK_HEADERS([X11/extensions/scrnsaver.h], [], [],
5744 [[#include <X11/Xlib.h>
5746 @end example
5747 @end table
5750 @node Particular Headers
5751 @subsection Particular Header Checks
5753 These macros check for particular system header files---whether they
5754 exist, and in some cases whether they declare certain symbols.
5756 @defmac AC_HEADER_ASSERT
5757 @acindex{HEADER_ASSERT}
5758 @cvindex NDEBUG
5759 @hdrindex{assert.h}
5760 Check whether to enable assertions in the style of @file{assert.h}.
5761 Assertions are enabled by default, but the user can override this by
5762 invoking @command{configure} with the @option{--disable-assert} option.
5763 @end defmac
5765 @anchor{AC_HEADER_DIRENT}
5766 @defmac AC_HEADER_DIRENT
5767 @acindex{HEADER_DIRENT}
5768 @cvindex HAVE_DIRENT_H
5769 @cvindex HAVE_NDIR_H
5770 @cvindex HAVE_SYS_DIR_H
5771 @cvindex HAVE_SYS_NDIR_H
5772 @hdrindex{dirent.h}
5773 @hdrindex{sys/ndir.h}
5774 @hdrindex{sys/dir.h}
5775 @hdrindex{ndir.h}
5776 Check for the following header files.  For the first one that is
5777 found and defines @samp{DIR}, define the listed C preprocessor macro:
5779 @multitable {@file{sys/ndir.h}} {@code{HAVE_SYS_NDIR_H}}
5780 @item @file{dirent.h}   @tab @code{HAVE_DIRENT_H}
5781 @item @file{sys/ndir.h} @tab @code{HAVE_SYS_NDIR_H}
5782 @item @file{sys/dir.h}  @tab @code{HAVE_SYS_DIR_H}
5783 @item @file{ndir.h}     @tab @code{HAVE_NDIR_H}
5784 @end multitable
5786 The directory-library declarations in your source code should look
5787 something like the following:
5789 @example
5790 @group
5791 #include <sys/types.h>
5792 #ifdef HAVE_DIRENT_H
5793 # include <dirent.h>
5794 # define NAMLEN(dirent) strlen ((dirent)->d_name)
5795 #else
5796 # define dirent direct
5797 # define NAMLEN(dirent) ((dirent)->d_namlen)
5798 # ifdef HAVE_SYS_NDIR_H
5799 #  include <sys/ndir.h>
5800 # endif
5801 # ifdef HAVE_SYS_DIR_H
5802 #  include <sys/dir.h>
5803 # endif
5804 # ifdef HAVE_NDIR_H
5805 #  include <ndir.h>
5806 # endif
5807 #endif
5808 @end group
5809 @end example
5811 Using the above declarations, the program would declare variables to be
5812 of type @code{struct dirent}, not @code{struct direct}, and would access
5813 the length of a directory entry name by passing a pointer to a
5814 @code{struct dirent} to the @code{NAMLEN} macro.
5816 This macro also checks for the SCO Xenix @file{dir} and @file{x} libraries.
5818 This macro is obsolescent, as all current systems with directory
5819 libraries have @code{<dirent.h>}.  New programs need not use this macro.
5821 Also see @code{AC_STRUCT_DIRENT_D_INO} and
5822 @code{AC_STRUCT_DIRENT_D_TYPE} (@pxref{Particular Structures}).
5823 @end defmac
5825 @anchor{AC_HEADER_MAJOR}
5826 @defmac AC_HEADER_MAJOR
5827 @acindex{HEADER_MAJOR}
5828 @cvindex MAJOR_IN_MKDEV
5829 @cvindex MAJOR_IN_SYSMACROS
5830 @hdrindex{sys/mkdev.h}
5831 @hdrindex{sys/sysmacros.h}
5832 If @file{sys/types.h} does not define @code{major}, @code{minor}, and
5833 @code{makedev}, but @file{sys/mkdev.h} does, define
5834 @code{MAJOR_IN_MKDEV}; otherwise, if @file{sys/sysmacros.h} does, define
5835 @code{MAJOR_IN_SYSMACROS}.
5836 @end defmac
5838 @defmac AC_HEADER_RESOLV
5839 @acindex{HEADER_RESOLV}
5840 @cvindex HAVE_RESOLV_H
5841 @hdrindex{resolv.h}
5842 Checks for header @file{resolv.h}, checking for prerequisites first.
5843 To properly use @file{resolv.h}, your code should contain something like
5844 the following:
5846 @verbatim
5847 #ifdef HAVE_SYS_TYPES_H
5848 #  include <sys/types.h>
5849 #endif
5850 #ifdef HAVE_NETINET_IN_H
5851 #  include <netinet/in.h>   /* inet_ functions / structs */
5852 #endif
5853 #ifdef HAVE_ARPA_NAMESER_H
5854 #  include <arpa/nameser.h> /* DNS HEADER struct */
5855 #endif
5856 #ifdef HAVE_NETDB_H
5857 #  include <netdb.h>
5858 #endif
5859 #include <resolv.h>
5860 @end verbatim
5861 @end defmac
5863 @anchor{AC_HEADER_STAT}
5864 @defmac AC_HEADER_STAT
5865 @acindex{HEADER_STAT}
5866 @cvindex STAT_MACROS_BROKEN
5867 @hdrindex{sys/stat.h}
5868 If the macros @code{S_ISDIR}, @code{S_ISREG}, etc.@: defined in
5869 @file{sys/stat.h} do not work properly (returning false positives),
5870 define @code{STAT_MACROS_BROKEN}.  This is the case on Tektronix UTekV,
5871 Amdahl UTS and Motorola System V/88.
5873 This macro is obsolescent, as no current systems have the bug.
5874 New programs need not use this macro.
5875 @end defmac
5877 @defmac AC_HEADER_STDBOOL
5878 @acindex{HEADER_STDBOOL}
5879 @cvindex HAVE_STDBOOL_H
5880 @cvindex HAVE__BOOL
5881 @hdrindex{stdbool.h}
5882 @hdrindex{system.h}
5883 @caindex header_stdbool_h
5884 If @file{stdbool.h} exists and conforms to C99, define
5885 @code{HAVE_STDBOOL_H} to 1; if the type @code{_Bool} is defined, define
5886 @code{HAVE__BOOL} to 1.  To fulfill the C99 requirements, your
5887 @file{system.h} could contain the following code:
5889 @verbatim
5890 #ifdef HAVE_STDBOOL_H
5891 # include <stdbool.h>
5892 #else
5893 # ifndef HAVE__BOOL
5894 #  ifdef __cplusplus
5895 typedef bool _Bool;
5896 #  else
5897 #   define _Bool signed char
5898 #  endif
5899 # endif
5900 # define bool _Bool
5901 # define false 0
5902 # define true 1
5903 # define __bool_true_false_are_defined 1
5904 #endif
5905 @end verbatim
5907 Alternatively you can use the @samp{stdbool} package of Gnulib
5908 (@pxref{Gnulib}); it packages the above code into a replacement header
5909 and contains a few other bells and whistles.
5911 This macro caches its result in the @code{ac_cv_header_stdbool_h}
5912 variable.
5913 @end defmac
5915 @anchor{AC_HEADER_STDC}
5916 @defmac AC_HEADER_STDC
5917 @acindex{HEADER_STDC}
5918 @cvindex STDC_HEADERS
5919 @hdrindex{stdlib.h}
5920 @hdrindex{stdarg.h}
5921 @hdrindex{string.h}
5922 @hdrindex{float.h}
5923 @hdrindex{ctype.h}
5924 @caindex header_stdc
5925 Define @code{STDC_HEADERS} if the system has C header files
5926 conforming to ANSI C89 (ISO C90).
5927 Specifically, this macro checks for @file{stdlib.h}, @file{stdarg.h},
5928 @file{string.h}, and @file{float.h}; if the system has those, it
5929 probably has the rest of the C89 header files.  This macro also
5930 checks whether @file{string.h} declares @code{memchr} (and thus
5931 presumably the other @code{mem} functions), whether @file{stdlib.h}
5932 declare @code{free} (and thus presumably @code{malloc} and other related
5933 functions), and whether the @file{ctype.h} macros work on characters
5934 with the high bit set, as the C standard requires.
5936 If you use this macro, your code can refer to @code{STDC_HEADERS} to
5937 determine whether the system has conforming header files (and probably C
5938 library functions).
5940 This macro caches its result in the @code{ac_cv_header_stdc} variable.
5942 This macro is obsolescent, as current systems have conforming header
5943 files.  New programs need not use this macro.
5945 @hdrindex{string.h}
5946 @hdrindex{strings.h}
5947 Nowadays @file{string.h} is part of the C standard and declares functions like
5948 @code{strcpy}, and @file{strings.h} is standardized by Posix and declares
5949 BSD functions like @code{bcopy}; but
5950 historically, string functions were a major sticking point in this area.
5951 If you still want to worry about portability to ancient systems without
5952 standard headers, there is so much variation
5953 that it is probably easier to declare the functions you use than to
5954 figure out exactly what the system header files declare.  Some ancient systems
5955 contained a mix of functions from the C standard and from BSD;
5956 some were mostly standard but lacked @samp{memmove}; some defined the
5957 BSD functions as macros in @file{string.h} or
5958 @file{strings.h}; some had only the BSD functions but
5959 @file{string.h}; some declared the memory functions in @file{memory.h},
5960 some in @file{string.h}; etc.  It is probably sufficient to check for
5961 one string function and one memory function; if the library had the
5962 standard versions of those then it probably had most of the others.
5963 If you put the following in @file{configure.ac}:
5965 @example
5966 # This example is obsolescent.
5967 # Nowadays you can omit these macro calls.
5968 AC_HEADER_STDC
5969 AC_CHECK_FUNCS([strchr memcpy])
5970 @end example
5972 @noindent
5973 then, in your code, you can use declarations like this:
5975 @example
5976 @group
5977 /* This example is obsolescent.
5978    Nowadays you can just #include <string.h>.  */
5979 #ifdef STDC_HEADERS
5980 # include <string.h>
5981 #else
5982 # ifndef HAVE_STRCHR
5983 #  define strchr index
5984 #  define strrchr rindex
5985 # endif
5986 char *strchr (), *strrchr ();
5987 # ifndef HAVE_MEMCPY
5988 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
5989 #  define memmove(d, s, n) bcopy ((s), (d), (n))
5990 # endif
5991 #endif
5992 @end group
5993 @end example
5995 @noindent
5996 If you use a function like @code{memchr}, @code{memset}, @code{strtok},
5997 or @code{strspn}, which have no BSD equivalent, then macros don't
5998 suffice to port to ancient hosts; you must provide an implementation of
5999 each function.  An easy
6000 way to incorporate your implementations only when needed (since the ones
6001 in system C libraries may be hand optimized) is to, taking @code{memchr}
6002 for example, put it in @file{memchr.c} and use
6003 @samp{AC_REPLACE_FUNCS([memchr])}.
6004 @end defmac
6006 @defmac AC_HEADER_SYS_WAIT
6007 @acindex{HEADER_SYS_WAIT}
6008 @cvindex HAVE_SYS_WAIT_H
6009 @hdrindex{sys/wait.h}
6010 @caindex header_sys_wait_h
6011 If @file{sys/wait.h} exists and is compatible with Posix, define
6012 @code{HAVE_SYS_WAIT_H}.  Incompatibility can occur if @file{sys/wait.h}
6013 does not exist, or if it uses the old BSD @code{union wait} instead
6014 of @code{int} to store a status value.  If @file{sys/wait.h} is not
6015 Posix compatible, then instead of including it, define the
6016 Posix macros with their usual interpretations.  Here is an
6017 example:
6019 @example
6020 @group
6021 #include <sys/types.h>
6022 #ifdef HAVE_SYS_WAIT_H
6023 # include <sys/wait.h>
6024 #endif
6025 #ifndef WEXITSTATUS
6026 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
6027 #endif
6028 #ifndef WIFEXITED
6029 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
6030 #endif
6031 @end group
6032 @end example
6034 @noindent
6035 This macro caches its result in the @code{ac_cv_header_sys_wait_h}
6036 variable.
6038 This macro is obsolescent, as current systems are compatible with Posix.
6039 New programs need not use this macro.
6040 @end defmac
6042 @cvindex _POSIX_VERSION
6043 @hdrindex{unistd.h}
6044 @code{_POSIX_VERSION} is defined when @file{unistd.h} is included on
6045 Posix systems.  If there is no @file{unistd.h}, it is definitely
6046 not a Posix system.  However, some non-Posix systems do
6047 have @file{unistd.h}.
6049 The way to check whether the system supports Posix is:
6051 @example
6052 @group
6053 #ifdef HAVE_UNISTD_H
6054 # include <sys/types.h>
6055 # include <unistd.h>
6056 #endif
6058 #ifdef _POSIX_VERSION
6059 /* Code for Posix systems.  */
6060 #endif
6061 @end group
6062 @end example
6064 @anchor{AC_HEADER_TIME}
6065 @defmac AC_HEADER_TIME
6066 @acindex{HEADER_TIME}
6067 @cvindex TIME_WITH_SYS_TIME
6068 @hdrindex{time.h}
6069 @hdrindex{sys/time.h}
6070 @caindex header_time
6071 If a program may include both @file{time.h} and @file{sys/time.h},
6072 define @code{TIME_WITH_SYS_TIME}.  On some ancient systems,
6073 @file{sys/time.h} included @file{time.h}, but @file{time.h} was not
6074 protected against multiple inclusion, so programs could not explicitly
6075 include both files.  This macro is useful in programs that use, for
6076 example, @code{struct timeval} as well as
6077 @code{struct tm}.  It is best used in conjunction with
6078 @code{HAVE_SYS_TIME_H}, which can be checked for using
6079 @code{AC_CHECK_HEADERS([sys/time.h])}.
6081 @example
6082 @group
6083 #ifdef TIME_WITH_SYS_TIME
6084 # include <sys/time.h>
6085 # include <time.h>
6086 #else
6087 # ifdef HAVE_SYS_TIME_H
6088 #  include <sys/time.h>
6089 # else
6090 #  include <time.h>
6091 # endif
6092 #endif
6093 @end group
6094 @end example
6096 @noindent
6097 This macro caches its result in the @code{ac_cv_header_time} variable.
6099 This macro is obsolescent, as current systems can include both files
6100 when they exist.  New programs need not use this macro.
6101 @end defmac
6104 @defmac AC_HEADER_TIOCGWINSZ
6105 @acindex{HEADER_TIOCGWINSZ}
6106 @cvindex GWINSZ_IN_SYS_IOCTL
6107 @hdrindex{sys/ioctl.h}
6108 @hdrindex{termios.h}
6109 @c FIXME: I need clarifications from Jim.
6110 If the use of @code{TIOCGWINSZ} requires @file{<sys/ioctl.h>}, then
6111 define @code{GWINSZ_IN_SYS_IOCTL}.  Otherwise @code{TIOCGWINSZ} can be
6112 found in @file{<termios.h>}.
6114 Use:
6116 @example
6117 @group
6118 #ifdef HAVE_TERMIOS_H
6119 # include <termios.h>
6120 #endif
6122 #ifdef GWINSZ_IN_SYS_IOCTL
6123 # include <sys/ioctl.h>
6124 #endif
6125 @end group
6126 @end example
6127 @end defmac
6129 @node Generic Headers
6130 @subsection Generic Header Checks
6132 These macros are used to find system header files not covered by the
6133 ``particular'' test macros.  If you need to check the contents of a header
6134 as well as find out whether it is present, you have to write your own
6135 test for it (@pxref{Writing Tests}).
6137 @anchor{AC_CHECK_HEADER}
6138 @defmac AC_CHECK_HEADER (@var{header-file}, @ovar{action-if-found}, @
6139   @ovar{action-if-not-found}, @ovar{includes})
6140 @acindex{CHECK_HEADER}
6141 @caindex header_@var{header-file}
6142 If the system header file @var{header-file} is compilable, execute shell
6143 commands @var{action-if-found}, otherwise execute
6144 @var{action-if-not-found}.  If you just want to define a symbol if the
6145 header file is available, consider using @code{AC_CHECK_HEADERS}
6146 instead.
6148 @var{includes} is decoded to determine the appropriate include
6149 directives.  If omitted or empty, @file{configure} will check for both header
6150 existence (with the preprocessor) and usability (with the compiler),
6151 using @code{AC_INCLUDES_DEFAULT} for the compile test.  If
6152 there is a discrepancy between the results, a warning is issued to the
6153 user, and the compiler results are favored (@pxref{Present But
6154 Cannot Be Compiled}).  In general, favoring the compiler results means
6155 that a header will be treated as not found even though the file exists,
6156 because you did not provide enough prerequisites.
6158 Providing a non-empty @var{includes} argument allows the code to provide
6159 any prerequisites prior to including the header under test; it is common
6160 to use the argument @code{AC_INCLUDES_DEFAULT} (@pxref{Default
6161 Includes}).  With an explicit fourth argument, no preprocessor test is
6162 needed.  As a special case, an @var{includes} of exactly @samp{-}
6163 triggers the older preprocessor check, which merely determines existence
6164 of the file in the preprocessor search path; this should only be used as
6165 a last resort (it is safer to determine the actual prerequisites and
6166 perform a compiler check, or else use @code{AC_PREPROC_IFELSE} to make
6167 it obvious that only a preprocessor check is desired).
6169 This macro caches its result in the @code{ac_cv_header_@var{header-file}}
6170 variable, with characters not suitable for a variable name mapped to
6171 underscores.
6172 @end defmac
6174 @anchor{AC_CHECK_HEADERS}
6175 @defmac AC_CHECK_HEADERS (@var{header-file}@dots{}, @
6176   @ovar{action-if-found}, @ovar{action-if-not-found}, @
6177   @ovar{includes})
6178 @acindex{CHECK_HEADERS}
6179 @cvindex HAVE_@var{header}
6180 @caindex header_@var{header-file}
6181 For each given system header file @var{header-file} in the
6182 blank-separated argument list that exists, define
6183 @code{HAVE_@var{header-file}} (in all capitals).  If @var{action-if-found}
6184 is given, it is additional shell code to execute when one of the header
6185 files is found.  You can give it a value of @samp{break} to break out of
6186 the loop on the first match.  If @var{action-if-not-found} is given, it
6187 is executed when one of the header files is not found.
6189 @var{includes} is interpreted as in @code{AC_CHECK_HEADER}, in order to
6190 choose the set of preprocessor directives supplied before the header
6191 under test.
6193 This macro caches its result in the @code{ac_cv_header_@var{header-file}}
6194 variable, with characters not suitable for a variable name mapped to
6195 underscores.
6196 @end defmac
6198 Previous versions of Autoconf merely checked whether the header was
6199 accepted by the preprocessor.  This was changed because the old test was
6200 inappropriate for typical uses.  Headers are typically used to compile,
6201 not merely to preprocess, and the old behavior sometimes accepted
6202 headers that clashed at compile-time (@pxref{Present But Cannot Be
6203 Compiled}).  If you need to check whether a header is preprocessable,
6204 you can use @code{AC_PREPROC_IFELSE} (@pxref{Running the Preprocessor}).
6206 Actually requiring a header to compile improves the robustness of the
6207 test, but it also requires
6208 that you make sure that headers that must be included before the
6209 @var{header-file} be part of the @var{includes}, (@pxref{Default
6210 Includes}).  If looking for @file{bar.h}, which requires that
6211 @file{foo.h} be included before if it exists, we suggest the following
6212 scheme:
6214 @verbatim
6215 AC_CHECK_HEADERS([foo.h])
6216 AC_CHECK_HEADERS([bar.h], [], [],
6217 [#ifdef HAVE_FOO_H
6218 # include <foo.h>
6219 #endif
6221 @end verbatim
6223 The following variant generates smaller, faster @command{configure}
6224 files if you do not need the full power of @code{AC_CHECK_HEADERS}.
6226 @defmac AC_CHECK_HEADERS_ONCE (@var{header-file}@dots{})
6227 @acindex{CHECK_HEADERS_ONCE}
6228 @cvindex HAVE_@var{header}
6229 For each given system header file @var{header-file} in the
6230 blank-separated argument list that exists, define
6231 @code{HAVE_@var{header-file}} (in all capitals).
6232 This is a once-only variant of @code{AC_CHECK_HEADERS}.  It generates the
6233 checking code at most once, so that @command{configure} is smaller and
6234 faster; but the checks cannot be conditionalized and are always done once,
6235 early during the @command{configure} run.  Thus, this macro is only safe
6236 for checking headers that do not have prerequisites beyond what
6237 @code{AC_INCLUDES_DEFAULT} provides.
6238 @end defmac
6240 @node Declarations
6241 @section Declarations
6242 @cindex Declaration, checking
6244 The following macros check for the declaration of variables and
6245 functions.  If there is no macro specifically defined to check for a
6246 symbol you need, then you can use the general macros (@pxref{Generic
6247 Declarations}) or, for more complex tests, you may use
6248 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
6250 @menu
6251 * Particular Declarations::     Macros to check for certain declarations
6252 * Generic Declarations::        How to find other declarations
6253 @end menu
6255 @node Particular Declarations
6256 @subsection Particular Declaration Checks
6258 There are no specific macros for declarations.
6260 @node Generic Declarations
6261 @subsection Generic Declaration Checks
6263 These macros are used to find declarations not covered by the ``particular''
6264 test macros.
6266 @defmac AC_CHECK_DECL (@var{symbol}, @ovar{action-if-found}, @
6267   @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
6268 @acindex{CHECK_DECL}
6269 @caindex have_decl_@var{symbol}
6270 If @var{symbol} (a function, variable, or constant) is not declared in
6271 @var{includes} and a declaration is needed, run the shell commands
6272 @var{action-if-not-found}, otherwise @var{action-if-found}.
6273 @var{includes} is a series of include directives, defaulting to
6274 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), which are used
6275 prior to the declaration under test.
6277 This macro actually tests whether @var{symbol} is defined as a macro or
6278 can be used as an r-value, not whether it is really declared, because it
6279 is much safer to avoid introducing extra declarations when they are not
6280 needed.  In order to facilitate use of C++ and overloaded function
6281 declarations, it is possible to specify function argument types in
6282 parentheses for types which can be zero-initialized:
6284 @example
6285 AC_CHECK_DECL([basename(char *)])
6286 @end example
6288 This macro caches its result in the @code{ac_cv_have_decl_@var{symbol}}
6289 variable, with characters not suitable for a variable name mapped to
6290 underscores.
6291 @end defmac
6293 @anchor{AC_CHECK_DECLS}
6294 @defmac AC_CHECK_DECLS (@var{symbols}, @ovar{action-if-found}, @
6295   @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
6296 @acindex{CHECK_DECLS}
6297 @cvindex HAVE_DECL_@var{symbol}
6298 @caindex have_decl_@var{symbol}
6299 For each of the @var{symbols} (@emph{comma}-separated list with optional
6300 function argument types for C++ overloads), define
6301 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
6302 @var{symbol} is declared, otherwise to @samp{0}.  If
6303 @var{action-if-not-found} is given, it is additional shell code to
6304 execute when one of the function declarations is needed, otherwise
6305 @var{action-if-found} is executed.
6307 @var{includes} is a series of include directives, defaulting to
6308 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), which are used
6309 prior to the declarations under test.
6311 This macro uses an M4 list as first argument:
6312 @example
6313 AC_CHECK_DECLS([strdup])
6314 AC_CHECK_DECLS([strlen])
6315 AC_CHECK_DECLS([malloc, realloc, calloc, free])
6316 AC_CHECK_DECLS([j0], [], [], [[#include <math.h>]])
6317 AC_CHECK_DECLS([[basename(char *)], [dirname(char *)]])
6318 @end example
6320 Unlike the other @samp{AC_CHECK_*S} macros, when a @var{symbol} is not
6321 declared, @code{HAVE_DECL_@var{symbol}} is defined to @samp{0} instead
6322 of leaving @code{HAVE_DECL_@var{symbol}} undeclared.  When you are
6323 @emph{sure} that the check was performed, use
6324 @code{HAVE_DECL_@var{symbol}} in @code{#if}:
6326 @example
6327 #if !HAVE_DECL_SYMBOL
6328 extern char *symbol;
6329 #endif
6330 @end example
6332 @noindent
6333 If the test may have not been performed, however, because it is safer
6334 @emph{not} to declare a symbol than to use a declaration that conflicts
6335 with the system's one, you should use:
6337 @example
6338 #if defined HAVE_DECL_MALLOC && !HAVE_DECL_MALLOC
6339 void *malloc (size_t *s);
6340 #endif
6341 @end example
6343 @noindent
6344 You fall into the second category only in extreme situations: either
6345 your files may be used without being configured, or they are used during
6346 the configuration.  In most cases the traditional approach is enough.
6348 This macro caches its results in @code{ac_cv_have_decl_@var{symbol}}
6349 variables, with characters not suitable for a variable name mapped to
6350 underscores.
6351 @end defmac
6353 @defmac AC_CHECK_DECLS_ONCE (@var{symbols})
6354 @acindex{CHECK_DECLS_ONCE}
6355 @cvindex HAVE_DECL_@var{symbol}
6356 For each of the @var{symbols} (@emph{comma}-separated list), define
6357 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
6358 @var{symbol} is declared in the default include files, otherwise to
6359 @samp{0}.  This is a once-only variant of @code{AC_CHECK_DECLS}.  It
6360 generates the checking code at most once, so that @command{configure} is
6361 smaller and faster; but the checks cannot be conditionalized and are
6362 always done once, early during the @command{configure} run.
6363 @end defmac
6366 @node Structures
6367 @section Structures
6368 @cindex Structure, checking
6370 The following macros check for the presence of certain members in C
6371 structures.  If there is no macro specifically defined to check for a
6372 member you need, then you can use the general structure-member macros
6373 (@pxref{Generic Structures}) or, for more complex tests, you may use
6374 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
6376 @menu
6377 * Particular Structures::       Macros to check for certain structure members
6378 * Generic Structures::          How to find other structure members
6379 @end menu
6381 @node Particular Structures
6382 @subsection Particular Structure Checks
6384 The following macros check for certain structures or structure members.
6386 @defmac AC_STRUCT_DIRENT_D_INO
6387 @acindex{STRUCT_DIRENT_D_INO}
6388 @cvindex HAVE_STRUCT_DIRENT_D_INO
6389 @c @caindex header_dirent_dirent_h
6390 @c @caindex member_struct_dirent_d_ino
6391 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
6392 Headers}).  Then, if @code{struct dirent} contains a @code{d_ino}
6393 member, define @code{HAVE_STRUCT_DIRENT_D_INO}.
6395 @code{HAVE_STRUCT_DIRENT_D_INO} indicates only the presence of
6396 @code{d_ino}, not whether its contents are always reliable.
6397 Traditionally, a zero @code{d_ino} indicated a deleted directory entry,
6398 though current systems hide this detail from the user and never return
6399 zero @code{d_ino} values.
6400 Many current systems report an incorrect @code{d_ino} for a directory
6401 entry that is a mount point.
6402 @end defmac
6404 @defmac AC_STRUCT_DIRENT_D_TYPE
6405 @acindex{STRUCT_DIRENT_D_TYPE}
6406 @cvindex HAVE_STRUCT_DIRENT_D_TYPE
6407 @c @caindex header_dirent_dirent_h
6408 @c @caindex member_struct_dirent_d_type
6409 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
6410 Headers}).  Then, if @code{struct dirent} contains a @code{d_type}
6411 member, define @code{HAVE_STRUCT_DIRENT_D_TYPE}.
6412 @end defmac
6414 @anchor{AC_STRUCT_ST_BLOCKS}
6415 @defmac AC_STRUCT_ST_BLOCKS
6416 @acindex{STRUCT_ST_BLOCKS}
6417 @cvindex HAVE_STRUCT_STAT_ST_BLOCKS
6418 @cvindex HAVE_ST_BLOCKS
6419 @ovindex LIBOBJS
6420 @caindex member_struct_stat_st_blocks
6421 If @code{struct stat} contains an @code{st_blocks} member, define
6422 @code{HAVE_STRUCT_STAT_ST_BLOCKS}.  Otherwise, require an
6423 @code{AC_LIBOBJ} replacement of @samp{fileblocks}.  The former name,
6424 @code{HAVE_ST_BLOCKS} is to be avoided, as its support will cease in the
6425 future.
6427 This macro caches its result in the @code{ac_cv_member_struct_stat_st_blocks}
6428 variable.
6429 @end defmac
6431 @defmac AC_STRUCT_TM
6432 @acindex{STRUCT_TM}
6433 @cvindex TM_IN_SYS_TIME
6434 @hdrindex{time.h}
6435 @hdrindex{sys/time.h}
6436 If @file{time.h} does not define @code{struct tm}, define
6437 @code{TM_IN_SYS_TIME}, which means that including @file{sys/time.h}
6438 had better define @code{struct tm}.
6440 This macro is obsolescent, as @file{time.h} defines @code{struct tm} in
6441 current systems.  New programs need not use this macro.
6442 @end defmac
6444 @anchor{AC_STRUCT_TIMEZONE}
6445 @defmac AC_STRUCT_TIMEZONE
6446 @acindex{STRUCT_TIMEZONE}
6447 @cvindex HAVE_DECL_TZNAME
6448 @cvindex HAVE_STRUCT_TM_TM_ZONE
6449 @cvindex HAVE_TM_ZONE
6450 @cvindex HAVE_TZNAME
6451 @c @caindex member_struct_tm_tm_zone
6452 @c @caindex struct_tm
6453 Figure out how to get the current timezone.  If @code{struct tm} has a
6454 @code{tm_zone} member, define @code{HAVE_STRUCT_TM_TM_ZONE} (and the
6455 obsoleted @code{HAVE_TM_ZONE}).  Otherwise, if the external array
6456 @code{tzname} is found, define @code{HAVE_TZNAME}; if it is declared,
6457 define @code{HAVE_DECL_TZNAME}.
6458 @end defmac
6460 @node Generic Structures
6461 @subsection Generic Structure Checks
6463 These macros are used to find structure members not covered by the
6464 ``particular'' test macros.
6466 @defmac AC_CHECK_MEMBER (@var{aggregate}.@var{member}, @
6467   @ovar{action-if-found}, @ovar{action-if-not-found}, @
6468   @dvar{includes, AC_INCLUDES_DEFAULT})
6469 @acindex{CHECK_MEMBER}
6470 @caindex member_@var{aggregate}_@var{member}
6471 Check whether @var{member} is a member of the aggregate @var{aggregate}.
6472 If no @var{includes} are specified, the default includes are used
6473 (@pxref{Default Includes}).
6475 @example
6476 AC_CHECK_MEMBER([struct passwd.pw_gecos], [],
6477                 [AC_MSG_ERROR([we need `passwd.pw_gecos'])],
6478                 [[#include <pwd.h>]])
6479 @end example
6481 You can use this macro for submembers:
6483 @example
6484 AC_CHECK_MEMBER(struct top.middle.bot)
6485 @end example
6487 This macro caches its result in the
6488 @code{av_cv_member_@var{aggregate}_@var{member}} variable, with
6489 characters not suitable for a variable name mapped to underscores.
6490 @end defmac
6492 @anchor{AC_CHECK_MEMBERS}
6493 @defmac AC_CHECK_MEMBERS (@var{members}, @ovar{action-if-found}, @
6494   @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
6495 @acindex{CHECK_MEMBERS}
6496 @cvindex HAVE_@var{aggregate}_@var{member}
6497 Check for the existence of each @samp{@var{aggregate}.@var{member}} of
6498 @var{members} using the previous macro.  When @var{member} belongs to
6499 @var{aggregate}, define @code{HAVE_@var{aggregate}_@var{member}} (in all
6500 capitals, with spaces and dots replaced by underscores).  If
6501 @var{action-if-found} is given, it is executed for each of the found
6502 members.  If @var{action-if-not-found} is given, it is executed for each
6503 of the members that could not be found.
6505 @var{includes} is a series of include directives, defaulting to
6506 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), which are used
6507 prior to the members under test.
6509 This macro uses M4 lists:
6510 @example
6511 AC_CHECK_MEMBERS([struct stat.st_rdev, struct stat.st_blksize])
6512 @end example
6513 @end defmac
6516 @node Types
6517 @section Types
6518 @cindex Types
6519 @cindex C types
6521 The following macros check for C types, either builtin or typedefs.  If
6522 there is no macro specifically defined to check for a type you need, and
6523 you don't need to check for any special properties of it, then you can
6524 use a general type-check macro.
6526 @menu
6527 * Particular Types::            Special handling to find certain types
6528 * Generic Types::               How to find other types
6529 @end menu
6531 @node Particular Types
6532 @subsection Particular Type Checks
6534 @hdrindex{sys/types.h}
6535 @hdrindex{stdlib.h}
6536 @hdrindex{stdint.h}
6537 @hdrindex{inttypes.h}
6538 These macros check for particular C types in @file{sys/types.h},
6539 @file{stdlib.h}, @file{stdint.h}, @file{inttypes.h} and others, if they
6540 exist.
6542 The Gnulib @code{stdint} module is an alternate way to define many of
6543 these symbols; it is useful if you prefer your code to assume a
6544 C99-or-better environment.  @xref{Gnulib}.
6546 @anchor{AC_TYPE_GETGROUPS}
6547 @defmac AC_TYPE_GETGROUPS
6548 @acindex{TYPE_GETGROUPS}
6549 @cvindex GETGROUPS_T
6550 @caindex type_getgroups
6551 Define @code{GETGROUPS_T} to be whichever of @code{gid_t} or @code{int}
6552 is the base type of the array argument to @code{getgroups}.
6554 This macro caches the base type in the @code{ac_cv_type_getgroups}
6555 variable.
6556 @end defmac
6558 @defmac AC_TYPE_INT8_T
6559 @acindex{TYPE_INT8_T}
6560 @cvindex HAVE_INT8_T
6561 @cvindex int8_t
6562 @caindex c_int8_t
6563 If @file{stdint.h} or @file{inttypes.h} does not define the type
6564 @code{int8_t}, define @code{int8_t} to a signed
6565 integer type that is exactly 8 bits wide and that uses two's complement
6566 representation, if such a type exists.
6567 If you are worried about porting to hosts that lack such a type, you can
6568 use the results of this macro in C89-or-later code as follows:
6570 @example
6571 #if HAVE_STDINT_H
6572 # include <stdint.h>
6573 #endif
6574 #if defined INT8_MAX || defined int8_t
6575  @emph{code using int8_t}
6576 #else
6577  @emph{complicated alternative using >8-bit 'signed char'}
6578 #endif
6579 @end example
6581 This macro caches the type in the @code{ac_cv_c_int8_t} variable.
6582 @end defmac
6584 @defmac AC_TYPE_INT16_T
6585 @acindex{TYPE_INT16_T}
6586 @cvindex HAVE_INT16_T
6587 @cvindex int16_t
6588 @caindex c_int16_t
6589 This is like @code{AC_TYPE_INT8_T}, except for 16-bit integers.
6590 @end defmac
6592 @defmac AC_TYPE_INT32_T
6593 @acindex{TYPE_INT32_T}
6594 @cvindex HAVE_INT32_T
6595 @cvindex int32_t
6596 @caindex c_int32_t
6597 This is like @code{AC_TYPE_INT8_T}, except for 32-bit integers.
6598 @end defmac
6600 @defmac AC_TYPE_INT64_T
6601 @acindex{TYPE_INT64_T}
6602 @cvindex HAVE_INT64_T
6603 @cvindex int64_t
6604 @caindex c_int64_t
6605 This is like @code{AC_TYPE_INT8_T}, except for 64-bit integers.
6606 @end defmac
6608 @defmac AC_TYPE_INTMAX_T
6609 @acindex{TYPE_INTMAX_T}
6610 @cvindex HAVE_INTMAX_T
6611 @cvindex intmax_t
6612 @c @caindex type_intmax_t
6613 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intmax_t},
6614 define @code{HAVE_INTMAX_T}.  Otherwise, define @code{intmax_t} to the
6615 widest signed integer type.
6616 @end defmac
6618 @defmac AC_TYPE_INTPTR_T
6619 @acindex{TYPE_INTPTR_T}
6620 @cvindex HAVE_INTPTR_T
6621 @cvindex intptr_t
6622 @c @caindex type_intptr_t
6623 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intptr_t},
6624 define @code{HAVE_INTPTR_T}.  Otherwise, define @code{intptr_t} to a
6625 signed integer type wide enough to hold a pointer, if such a type
6626 exists.
6627 @end defmac
6629 @defmac AC_TYPE_LONG_DOUBLE
6630 @acindex{TYPE_LONG_DOUBLE}
6631 @cvindex HAVE_LONG_DOUBLE
6632 @caindex type_long_double
6633 If the C compiler supports a working @code{long double} type, define
6634 @code{HAVE_LONG_DOUBLE}.  The @code{long double} type might have the
6635 same range and precision as @code{double}.
6637 This macro caches its result in the @code{ac_cv_type_long_double}
6638 variable.
6640 This macro is obsolescent, as current C compilers support @code{long
6641 double}.  New programs need not use this macro.
6642 @end defmac
6644 @defmac AC_TYPE_LONG_DOUBLE_WIDER
6645 @acindex{TYPE_LONG_DOUBLE_WIDER}
6646 @cvindex HAVE_LONG_DOUBLE_WIDER
6647 @caindex type_long_double_wider
6648 If the C compiler supports a working @code{long double} type with more
6649 range or precision than the @code{double} type, define
6650 @code{HAVE_LONG_DOUBLE_WIDER}.
6652 This macro caches its result in the @code{ac_cv_type_long_double_wider}
6653 variable.
6654 @end defmac
6656 @defmac AC_TYPE_LONG_LONG_INT
6657 @acindex{TYPE_LONG_LONG_INT}
6658 @cvindex HAVE_LONG_LONG_INT
6659 @caindex type_long_long_int
6660 If the C compiler supports a working @code{long long int} type, define
6661 @code{HAVE_LONG_LONG_INT}.  However, this test does not test
6662 @code{long long int} values in preprocessor @code{#if} expressions,
6663 because too many compilers mishandle such expressions.
6664 @xref{Preprocessor Arithmetic}.
6666 This macro caches its result in the @code{ac_cv_type_long_long_int}
6667 variable.
6668 @end defmac
6670 @defmac AC_TYPE_MBSTATE_T
6671 @acindex{TYPE_MBSTATE_T}
6672 @cvindex mbstate_t
6673 @hdrindex{wchar.h}
6674 @caindex type_mbstate_t
6675 Define @code{HAVE_MBSTATE_T} if @code{<wchar.h>} declares the
6676 @code{mbstate_t} type.  Also, define @code{mbstate_t} to be a type if
6677 @code{<wchar.h>} does not declare it.
6679 This macro caches its result in the @code{ac_cv_type_mbstate_t}
6680 variable.
6681 @end defmac
6683 @anchor{AC_TYPE_MODE_T}
6684 @defmac AC_TYPE_MODE_T
6685 @acindex{TYPE_MODE_T}
6686 @cvindex mode_t
6687 @caindex type_mode_t
6688 Define @code{mode_t} to a suitable type, if standard headers do not
6689 define it.
6691 This macro caches its result in the @code{ac_cv_type_mode_t} variable.
6692 @end defmac
6694 @anchor{AC_TYPE_OFF_T}
6695 @defmac AC_TYPE_OFF_T
6696 @acindex{TYPE_OFF_T}
6697 @cvindex off_t
6698 @caindex type_off_t
6699 Define @code{off_t} to a suitable type, if standard headers do not
6700 define it.
6702 This macro caches its result in the @code{ac_cv_type_off_t} variable.
6703 @end defmac
6705 @anchor{AC_TYPE_PID_T}
6706 @defmac AC_TYPE_PID_T
6707 @acindex{TYPE_PID_T}
6708 @cvindex pid_t
6709 @caindex type_pid_t
6710 Define @code{pid_t} to a suitable type, if standard headers do not
6711 define it.
6713 This macro caches its result in the @code{ac_cv_type_pid_t} variable.
6714 @end defmac
6716 @anchor{AC_TYPE_SIZE_T}
6717 @defmac AC_TYPE_SIZE_T
6718 @acindex{TYPE_SIZE_T}
6719 @cvindex size_t
6720 @caindex type_size_t
6721 Define @code{size_t} to a suitable type, if standard headers do not
6722 define it.
6724 This macro caches its result in the @code{ac_cv_type_size_t} variable.
6725 @end defmac
6727 @defmac AC_TYPE_SSIZE_T
6728 @acindex{TYPE_SSIZE_T}
6729 @cvindex ssize_t
6730 @caindex type_ssize_t
6731 Define @code{ssize_t} to a suitable type, if standard headers do not
6732 define it.
6734 This macro caches its result in the @code{ac_cv_type_ssize_t} variable.
6735 @end defmac
6737 @anchor{AC_TYPE_UID_T}
6738 @defmac AC_TYPE_UID_T
6739 @acindex{TYPE_UID_T}
6740 @cvindex uid_t
6741 @cvindex gid_t
6742 @caindex type_uid_t
6743 Define @code{uid_t} and @code{gid_t} to suitable types, if standard
6744 headers do not define them.
6746 This macro caches its result in the @code{ac_cv_type_uid_t} variable.
6747 @end defmac
6749 @defmac AC_TYPE_UINT8_T
6750 @acindex{TYPE_UINT8_T}
6751 @cvindex HAVE_UINT8_T
6752 @cvindex uint8_t
6753 @caindex c_uint8_t
6754 If @file{stdint.h} or @file{inttypes.h} does not define the type
6755 @code{uint8_t}, define @code{uint8_t} to an
6756 unsigned integer type that is exactly 8 bits wide, if such a type
6757 exists.
6758 This is like @code{AC_TYPE_INT8_T}, except for unsigned integers.
6759 @end defmac
6761 @defmac AC_TYPE_UINT16_T
6762 @acindex{TYPE_UINT16_T}
6763 @cvindex HAVE_UINT16_T
6764 @cvindex uint16_t
6765 @caindex c_uint16_t
6766 This is like @code{AC_TYPE_UINT8_T}, except for 16-bit integers.
6767 @end defmac
6769 @defmac AC_TYPE_UINT32_T
6770 @acindex{TYPE_UINT32_T}
6771 @cvindex HAVE_UINT32_T
6772 @cvindex uint32_t
6773 @caindex c_uint32_t
6774 This is like @code{AC_TYPE_UINT8_T}, except for 32-bit integers.
6775 @end defmac
6777 @defmac AC_TYPE_UINT64_T
6778 @acindex{TYPE_UINT64_T}
6779 @cvindex HAVE_UINT64_T
6780 @cvindex uint64_t
6781 @caindex c_uint64_t
6782 This is like @code{AC_TYPE_UINT8_T}, except for 64-bit integers.
6783 @end defmac
6785 @defmac AC_TYPE_UINTMAX_T
6786 @acindex{TYPE_UINTMAX_T}
6787 @cvindex HAVE_UINTMAX_T
6788 @cvindex uintmax_t
6789 @c @caindex type_uintmax_t
6790 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintmax_t},
6791 define @code{HAVE_UINTMAX_T}.  Otherwise, define @code{uintmax_t} to the
6792 widest unsigned integer type.
6793 @end defmac
6795 @defmac AC_TYPE_UINTPTR_T
6796 @acindex{TYPE_UINTPTR_T}
6797 @cvindex HAVE_UINTPTR_T
6798 @cvindex uintptr_t
6799 @c @caindex type_uintptr_t
6800 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintptr_t},
6801 define @code{HAVE_UINTPTR_T}.  Otherwise, define @code{uintptr_t} to an
6802 unsigned integer type wide enough to hold a pointer, if such a type
6803 exists.
6804 @end defmac
6806 @defmac AC_TYPE_UNSIGNED_LONG_LONG_INT
6807 @acindex{TYPE_UNSIGNED_LONG_LONG_INT}
6808 @cvindex HAVE_UNSIGNED_LONG_LONG_INT
6809 @caindex type_unsigned_long_long_int
6810 If the C compiler supports a working @code{unsigned long long int} type,
6811 define @code{HAVE_UNSIGNED_LONG_LONG_INT}.  However, this test does not test
6812 @code{unsigned long long int} values in preprocessor @code{#if} expressions,
6813 because too many compilers mishandle such expressions.
6814 @xref{Preprocessor Arithmetic}.
6816 This macro caches its result in the @code{ac_cv_type_unsigned_long_long_int}
6817 variable.
6818 @end defmac
6820 @node Generic Types
6821 @subsection Generic Type Checks
6823 These macros are used to check for types not covered by the ``particular''
6824 test macros.
6826 @defmac AC_CHECK_TYPE (@var{type}, @ovar{action-if-found}, @
6827   @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
6828 @acindex{CHECK_TYPE}
6829 @caindex type_@var{type}
6830 Check whether @var{type} is defined.  It may be a compiler builtin type
6831 or defined by the @var{includes}.  @var{includes} is a series of include
6832 directives, defaulting to @code{AC_INCLUDES_DEFAULT} (@pxref{Default
6833 Includes}), which are used prior to the type under test.
6835 In C, @var{type} must be a type-name, so that the expression @samp{sizeof
6836 (@var{type})} is valid (but @samp{sizeof ((@var{type}))} is not).  The
6837 same test is applied when compiling for C++, which means that in C++
6838 @var{type} should be a type-id and should not be an anonymous
6839 @samp{struct} or @samp{union}.
6841 This macro caches its result in the @code{ac_cv_type_@var{type}}
6842 variable, with @samp{*} mapped to @samp{p} and other characters not
6843 suitable for a variable name mapped to underscores.
6844 @end defmac
6847 @defmac AC_CHECK_TYPES (@var{types}, @ovar{action-if-found}, @
6848   @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
6849 @acindex{CHECK_TYPES}
6850 @cvindex HAVE_@var{type}
6851 For each @var{type} of the @var{types} that is defined, define
6852 @code{HAVE_@var{type}} (in all capitals).  Each @var{type} must follow
6853 the rules of @code{AC_CHECK_TYPE}.  If no @var{includes} are
6854 specified, the default includes are used (@pxref{Default Includes}).  If
6855 @var{action-if-found} is given, it is additional shell code to execute
6856 when one of the types is found.  If @var{action-if-not-found} is given,
6857 it is executed when one of the types is not found.
6859 This macro uses M4 lists:
6860 @example
6861 AC_CHECK_TYPES([ptrdiff_t])
6862 AC_CHECK_TYPES([unsigned long long int, uintmax_t])
6863 AC_CHECK_TYPES([float_t], [], [], [[#include <math.h>]])
6864 @end example
6866 @end defmac
6868 Autoconf, up to 2.13, used to provide to another version of
6869 @code{AC_CHECK_TYPE}, broken by design.  In order to keep backward
6870 compatibility, a simple heuristic, quite safe but not totally, is
6871 implemented.  In case of doubt, read the documentation of the former
6872 @code{AC_CHECK_TYPE}, see @ref{Obsolete Macros}.
6875 @node Compilers and Preprocessors
6876 @section Compilers and Preprocessors
6877 @cindex Compilers
6878 @cindex Preprocessors
6880 @ovindex EXEEXT
6881 All the tests for compilers (@code{AC_PROG_CC}, @code{AC_PROG_CXX},
6882 @code{AC_PROG_F77}) define the output variable @code{EXEEXT} based on
6883 the output of the compiler, typically to the empty string if
6884 Posix and @samp{.exe} if a DOS variant.
6886 @ovindex OBJEXT
6887 They also define the output variable @code{OBJEXT} based on the
6888 output of the compiler, after @file{.c} files have been excluded, typically
6889 to @samp{o} if Posix, @samp{obj} if a DOS variant.
6891 If the compiler being used does not produce executables, the tests fail.  If
6892 the executables can't be run, and cross-compilation is not enabled, they
6893 fail too.  @xref{Manual Configuration}, for more on support for cross
6894 compiling.
6896 @menu
6897 * Specific Compiler Characteristics::  Some portability issues
6898 * Generic Compiler Characteristics::  Language independent tests and features
6899 * C Compiler::                  Checking its characteristics
6900 * C++ Compiler::                Likewise
6901 * Objective C Compiler::        Likewise
6902 * Objective C++ Compiler::      Likewise
6903 * Erlang Compiler and Interpreter::  Likewise
6904 * Fortran Compiler::            Likewise
6905 @end menu
6907 @node Specific Compiler Characteristics
6908 @subsection Specific Compiler Characteristics
6910 Some compilers exhibit different behaviors.
6912 @table @asis
6913 @item Static/Dynamic Expressions
6914 Autoconf relies on a trick to extract one bit of information from the C
6915 compiler: using negative array sizes.  For instance the following
6916 excerpt of a C source demonstrates how to test whether @samp{int} objects are 4
6917 bytes wide:
6919 @example
6920 static int test_array[sizeof (int) == 4 ? 1 : -1];
6921 @end example
6923 @noindent
6924 To our knowledge, there is a single compiler that does not support this
6925 trick: the HP C compilers (the real ones, not only the
6926 ``bundled'') on HP-UX 11.00.
6927 They incorrectly reject the above program with the diagnostic
6928 ``Variable-length arrays cannot have static storage.''
6929 This bug comes from HP compilers' mishandling of @code{sizeof (int)},
6930 not from the @code{? 1 : -1}, and
6931 Autoconf works around this problem by casting @code{sizeof (int)} to
6932 @code{long int} before comparing it.
6933 @end table
6935 @node Generic Compiler Characteristics
6936 @subsection Generic Compiler Characteristics
6938 @anchor{AC_CHECK_SIZEOF}
6939 @defmac AC_CHECK_SIZEOF (@var{type-or-expr}, @ovar{unused}, @
6940   @dvar{includes, AC_INCLUDES_DEFAULT})
6941 @acindex{CHECK_SIZEOF}
6942 @cvindex SIZEOF_@var{type-or-expr}
6943 @caindex sizeof_@var{type-or-expr}
6944 Define @code{SIZEOF_@var{type-or-expr}} (@pxref{Standard Symbols}) to be
6945 the size in bytes of @var{type-or-expr}, which may be either a type or
6946 an expression returning a value that has a size.  If the expression
6947 @samp{sizeof (@var{type-or-expr})} is invalid, the result is 0.
6948 @var{includes} is a series of include directives, defaulting to
6949 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), which are used
6950 prior to the expression under test.
6952 This macro now works even when cross-compiling.  The @var{unused}
6953 argument was used when cross-compiling.
6955 For example, the call
6957 @example
6958 AC_CHECK_SIZEOF([int *])
6959 @end example
6961 @noindent
6962 defines @code{SIZEOF_INT_P} to be 8 on DEC Alpha AXP systems.
6964 This macro caches its result in the @code{ac_cv_sizeof_@var{type-or-expr}}
6965 variable, with @samp{*} mapped to @samp{p} and other characters not
6966 suitable for a variable name mapped to underscores.
6967 @end defmac
6969 @defmac AC_CHECK_ALIGNOF (@var{type}, @dvar{includes, AC_INCLUDES_DEFAULT})
6970 @acindex{CHECK_ALIGNOF}
6971 @cvindex ALIGNOF_@var{type}
6972 @caindex alignof_@var{type-or-expr}
6973 Define @code{ALIGNOF_@var{type}} (@pxref{Standard Symbols}) to be the
6974 alignment in bytes of @var{type}.  @samp{@var{type} y;} must be valid as
6975 a structure member declaration.  If @samp{type} is unknown, the result
6976 is 0.  If no @var{includes} are specified, the default includes are used
6977 (@pxref{Default Includes}).
6979 This macro caches its result in the @code{ac_cv_alignof_@var{type-or-expr}}
6980 variable, with @samp{*} mapped to @samp{p} and other characters not
6981 suitable for a variable name mapped to underscores.
6982 @end defmac
6984 @defmac AC_COMPUTE_INT (@var{var}, @var{expression}, @
6985   @dvar{includes, AC_INCLUDES_DEFAULT}, @ovar{action-if-fails})
6986 @acindex{COMPUTE_INT}
6987 Store into the shell variable @var{var} the value of the integer
6988 @var{expression}.  The
6989 value should fit in an initializer in a C variable of type @code{signed
6990 long}.  To support cross compilation (in which case, the macro only works on
6991 hosts that use twos-complement arithmetic), it should be possible to evaluate
6992 the expression at compile-time.  If no @var{includes} are specified, the
6993 default includes are used (@pxref{Default Includes}).
6995 Execute @var{action-if-fails} if the value cannot be determined correctly.
6996 @end defmac
6998 @defmac AC_LANG_WERROR
6999 @acindex{LANG_WERROR}
7000 Normally Autoconf ignores warnings generated by the compiler, linker, and
7001 preprocessor.  If this macro is used, warnings count as fatal
7002 errors for the current language.  This macro is useful when the
7003 results of configuration are used where warnings are unacceptable; for
7004 instance, if parts of a program are built with the GCC
7005 @option{-Werror}
7006 option.  If the whole program is built using @option{-Werror} it is
7007 often simpler to put @option{-Werror} in the compiler flags (@code{CFLAGS},
7008 etc.).
7009 @end defmac
7011 @defmac AC_OPENMP
7012 @acindex{OPENMP}
7013 @cvindex _OPENMP
7014 @ovindex OPENMP_CFLAGS
7015 @ovindex OPENMP_CXXFLAGS
7016 @ovindex OPENMP_FFLAGS
7017 @ovindex OPENMP_FCFLAGS
7018 OpenMP (@url{http://@/www.openmp.org/}) specifies extensions of C, C++,
7019 and Fortran that simplify optimization of shared memory parallelism,
7020 which is a common problem on multicore CPUs.
7022 If the current language is C, the macro @code{AC_OPENMP} sets the
7023 variable @code{OPENMP_CFLAGS} to the C compiler flags needed for
7024 supporting OpenMP@.  @code{OPENMP_CFLAGS} is set to empty if the
7025 compiler already supports OpenMP, if it has no way to activate OpenMP
7026 support, or if the user rejects OpenMP support by invoking
7027 @samp{configure} with the @samp{--disable-openmp} option.
7029 @code{OPENMP_CFLAGS} needs to be used when compiling programs, when
7030 preprocessing program source, and when linking programs.  Therefore you
7031 need to add @code{$(OPENMP_CFLAGS)} to the @code{CFLAGS} of C programs
7032 that use OpenMP@.  If you preprocess OpenMP-specific C code, you also
7033 need to add @code{$(OPENMP_CFLAGS)} to @code{CPPFLAGS}.  The presence of
7034 OpenMP support is revealed at compile time by the preprocessor macro
7035 @code{_OPENMP}.
7037 Linking a program with @code{OPENMP_CFLAGS} typically adds one more
7038 shared library to the program's dependencies, so its use is recommended
7039 only on programs that actually require OpenMP.
7041 If the current language is C++, @code{AC_OPENMP} sets the variable
7042 @code{OPENMP_CXXFLAGS}, suitably for the C++ compiler.  The same remarks
7043 hold as for C.
7045 If the current language is Fortran 77 or Fortran, @code{AC_OPENMP} sets
7046 the variable @code{OPENMP_FFLAGS} or @code{OPENMP_FCFLAGS},
7047 respectively.  Similar remarks as for C hold, except that
7048 @code{CPPFLAGS} is not used for Fortran, and no preprocessor macro
7049 signals OpenMP support.
7051 For portability, it is best to avoid spaces between @samp{#} and
7052 @samp{pragma omp}.  That is, write @samp{#pragma omp}, not
7053 @samp{# pragma omp}.  The Sun WorkShop 6.2 C compiler chokes on the
7054 latter.
7055 @end defmac
7057 @node C Compiler
7058 @subsection C Compiler Characteristics
7060 The following macros provide ways to find and exercise a C Compiler.
7061 There are a few constructs that ought to be avoided, but do not deserve
7062 being checked for, since they can easily be worked around.
7064 @table @asis
7065 @item Don't use lines containing solitary backslashes
7066 They tickle a bug in the HP-UX C compiler (checked on
7067 HP-UX 10.20,
7068 11.00, and 11i).  When given the following source:
7070 @example
7071 #ifdef __STDC__
7073 * A comment with backslash-newlines in it.  %@{ %@} *\
7076 char str[] = "\\
7077 " A string with backslash-newlines in it %@{ %@} \\
7079 char apostrophe = '\\
7083 #endif
7084 @end example
7086 @noindent
7087 the compiler incorrectly fails with the diagnostics ``Non-terminating
7088 comment at end of file'' and ``Missing @samp{#endif} at end of file.''
7089 Removing the lines with solitary backslashes solves the problem.
7091 @item Don't compile several files at once if output matters to you
7092 Some compilers, such as HP's, report names of files being
7093 compiled when given more than one file operand.  For instance:
7095 @example
7096 $ @kbd{cc a.c b.c}
7097 a.c:
7098 b.c:
7099 @end example
7101 @noindent
7102 This can cause problems if you observe the output of the compiler to
7103 detect failures.  Invoking @samp{cc -c a.c && cc -c b.c && cc -o c a.o
7104 b.o} solves the issue.
7106 @item Don't rely on @code{#error} failing
7107 The IRIX C compiler does not fail when #error is preprocessed; it
7108 simply emits a diagnostic and continues, exiting successfully.  So,
7109 instead of an error directive like @code{#error "Unsupported word size"}
7110 it is more portable to use an invalid directive like @code{#Unsupported
7111 word size} in Autoconf tests.  In ordinary source code, @code{#error} is
7112 OK, since installers with inadequate compilers like IRIX can simply
7113 examine these compilers' diagnostic output.
7115 @item Don't rely on correct @code{#line} support
7116 On Solaris, @command{c89} (at least Sun C 5.3 through 5.8)
7117 diagnoses @code{#line} directives whose line
7118 numbers are greater than 32767.  Nothing in Posix
7119 makes this invalid.  That is why Autoconf stopped issuing
7120 @code{#line} directives.
7121 @end table
7123 @defmac AC_PROG_CC (@ovar{compiler-search-list})
7124 @acindex{PROG_CC}
7125 @evindex CC
7126 @evindex CFLAGS
7127 @ovindex CC
7128 @ovindex CFLAGS
7129 @caindex prog_cc_c89
7130 Determine a C compiler to use.  If @code{CC} is not already set in the
7131 environment, check for @code{gcc} and @code{cc}, then for other C
7132 compilers.  Set output variable @code{CC} to the name of the compiler
7133 found.
7135 This macro may, however, be invoked with an optional first argument
7136 which, if specified, must be a blank-separated list of C compilers to
7137 search for.  This just gives the user an opportunity to specify an
7138 alternative search list for the C compiler.  For example, if you didn't
7139 like the default order, then you could invoke @code{AC_PROG_CC} like
7140 this:
7142 @example
7143 AC_PROG_CC([gcc cl cc])
7144 @end example
7146 If the C compiler does not handle function prototypes correctly by
7147 default, try to add an option to output variable @code{CC} to make it
7148 so.  This macro tries various options that select standard-conformance
7149 modes on various systems.
7151 After calling this macro you can check whether the C compiler has been
7152 set to accept ANSI C89 (ISO C90); if not, the shell
7153 variable
7154 @code{ac_cv_prog_cc_c89} is set to @samp{no}.  See also
7155 @code{AC_C_PROTOTYPES} below.
7157 If using the GNU C compiler, set shell variable @code{GCC} to
7158 @samp{yes}.  If output variable @code{CFLAGS} was not already set, set
7159 it to @option{-g -O2} for the GNU C compiler (@option{-O2} on systems
7160 where GCC does not accept @option{-g}), or @option{-g} for
7161 other compilers.  If your package does not like this default, then it is
7162 acceptable to insert the line @samp{: $@{CFLAGS=""@}} after @code{AC_INIT}
7163 and before @code{AC_PROG_CC} to select an empty default instead.
7165 Many Autoconf macros use a compiler, and thus call
7166 @samp{AC_REQUIRE([AC_PROG_CC])} to ensure that the compiler has been
7167 determined before the body of the outermost @code{AC_DEFUN} macro.
7168 Although @code{AC_PROG_CC} is safe to directly expand multiple times, it
7169 performs certain checks (such as the proper value of @env{EXEEXT}) only
7170 on the first invocation.  Therefore, care must be used when invoking
7171 this macro from within another macro rather than at the top level
7172 (@pxref{Expanded Before Required}).
7173 @end defmac
7175 @anchor{AC_PROG_CC_C_O}
7176 @defmac AC_PROG_CC_C_O
7177 @acindex{PROG_CC_C_O}
7178 @cvindex NO_MINUS_C_MINUS_O
7179 @caindex prog_cc_@var{compiler}_c_o
7180 If the C compiler does not accept the @option{-c} and @option{-o} options
7181 simultaneously, define @code{NO_MINUS_C_MINUS_O}.  This macro actually
7182 tests both the compiler found by @code{AC_PROG_CC}, and, if different,
7183 the first @code{cc} in the path.  The test fails if one fails.  This
7184 macro was created for GNU Make to choose the default C compilation
7185 rule.
7187 For the compiler @var{compiler}, this macro caches its result in the
7188 @code{ac_cv_prog_cc_@var{compiler}_c_o} variable.
7189 @end defmac
7192 @defmac AC_PROG_CPP
7193 @acindex{PROG_CPP}
7194 @evindex CPP
7195 @ovindex CPP
7196 Set output variable @code{CPP} to a command that runs the
7197 C preprocessor.  If @samp{$CC -E} doesn't work, @file{/lib/cpp} is used.
7198 It is only portable to run @code{CPP} on files with a @file{.c}
7199 extension.
7201 Some preprocessors don't indicate missing include files by the error
7202 status.  For such preprocessors an internal variable is set that causes
7203 other macros to check the standard error from the preprocessor and
7204 consider the test failed if any warnings have been reported.
7205 For most preprocessors, though, warnings do not cause include-file
7206 tests to fail unless @code{AC_PROG_CPP_WERROR} is also specified.
7207 @end defmac
7209 @defmac AC_PROG_CPP_WERROR
7210 @acindex{PROG_CPP_WERROR}
7211 @ovindex CPP
7212 This acts like @code{AC_PROG_CPP}, except it treats warnings from the
7213 preprocessor as errors even if the preprocessor exit status indicates
7214 success.  This is useful for avoiding headers that generate mandatory
7215 warnings, such as deprecation notices.
7216 @end defmac
7219 The following macros check for C compiler or machine architecture
7220 features.  To check for characteristics not listed here, use
7221 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
7222 @code{AC_RUN_IFELSE} (@pxref{Runtime}).
7224 @defmac AC_PROG_CC_STDC
7225 @acindex{PROG_CC_STDC}
7226 @caindex prog_cc_stdc
7227 If the C compiler cannot compile ISO Standard C (currently
7228 C99), try to add an option to output variable @code{CC} to make it work.
7229 If the compiler does not support C99, fall back to supporting
7230 ANSI C89 (ISO C90).
7232 After calling this macro you can check whether the C compiler has been
7233 set to accept Standard C; if not, the shell variable
7234 @code{ac_cv_prog_cc_stdc} is set to @samp{no}.
7235 @end defmac
7237 @defmac AC_PROG_CC_C89
7238 @acindex{PROG_CC_C89}
7239 @caindex prog_cc_c89
7240 If the C compiler is not in ANSI C89 (ISO C90) mode by
7241 default, try to add an option to output variable @code{CC} to make it
7242 so.  This macro tries various options that select ANSI C89 on
7243 some system or another, preferring extended functionality modes over
7244 strict conformance modes.  It considers the compiler to be in
7245 ANSI C89 mode if it handles function prototypes correctly.
7247 After calling this macro you can check whether the C compiler has been
7248 set to accept ANSI C89; if not, the shell variable
7249 @code{ac_cv_prog_cc_c89} is set to @samp{no}.
7251 This macro is called automatically by @code{AC_PROG_CC}.
7252 @end defmac
7254 @defmac AC_PROG_CC_C99
7255 @acindex{PROG_CC_C99}
7256 @caindex prog_cc_c99
7257 If the C compiler is not in C99 mode by default, try to add an
7258 option to output variable @code{CC} to make it so.  This macro tries
7259 various options that select C99 on some system or another, preferring
7260 extended functionality modes over strict conformance modes.  It
7261 considers the compiler to be in C99 mode if it handles @code{_Bool},
7262 @code{//} comments, flexible array members, @code{inline}, signed and
7263 unsigned @code{long long int}, mixed code and declarations, named
7264 initialization of structs,
7265 @code{restrict}, @code{va_copy}, varargs macros, variable declarations
7266 in @code{for} loops, and variable length arrays.
7268 After calling this macro you can check whether the C compiler has been
7269 set to accept C99; if not, the shell variable
7270 @code{ac_cv_prog_cc_c99} is set to @samp{no}.
7271 @end defmac
7273 @defmac AC_C_BACKSLASH_A
7274 @acindex{C_BACKSLASH_A}
7275 @cvindex HAVE_C_BACKSLASH_A
7276 Define @samp{HAVE_C_BACKSLASH_A} to 1 if the C compiler understands
7277 @samp{\a}.
7279 This macro is obsolescent, as current C compilers understand @samp{\a}.
7280 New programs need not use this macro.
7281 @end defmac
7283 @anchor{AC_C_BIGENDIAN}
7284 @defmac AC_C_BIGENDIAN (@ovar{action-if-true}, @ovar{action-if-false}, @
7285   @ovar{action-if-unknown}, @ovar{action-if-universal})
7286 @acindex{C_BIGENDIAN}
7287 @cvindex WORDS_BIGENDIAN
7288 @cindex Endianness
7289 If words are stored with the most significant byte first (like Motorola
7290 and SPARC CPUs), execute @var{action-if-true}.  If words are stored with
7291 the least significant byte first (like Intel and VAX CPUs), execute
7292 @var{action-if-false}.
7294 This macro runs a test-case if endianness cannot be determined from the
7295 system header files.  When cross-compiling, the test-case is not run but
7296 grep'ed for some magic values.  @var{action-if-unknown} is executed if
7297 the latter case fails to determine the byte sex of the host system.
7299 In some cases a single run of a compiler can generate code for multiple
7300 architectures.  This can happen, for example, when generating Mac OS X
7301 universal binary files, which work on both PowerPC and Intel
7302 architectures.  In this case, the different variants might be for
7303 different architectures whose endiannesses differ.  If
7304 @command{configure} detects this, it executes @var{action-if-universal}
7305 instead of @var{action-if-unknown}.
7307 The default for @var{action-if-true} is to define
7308 @samp{WORDS_BIGENDIAN}.  The default for @var{action-if-false} is to do
7309 nothing.  The default for @var{action-if-unknown} is to
7310 abort configure and tell the installer how to bypass this test.
7311 And finally, the default for @var{action-if-universal} is to ensure that
7312 @samp{WORDS_BIGENDIAN} is defined if and only if a universal build is
7313 detected and the current code is big-endian; this default works only if
7314 @command{autoheader} is used (@pxref{autoheader Invocation}).
7316 If you use this macro without specifying @var{action-if-universal}, you
7317 should also use @code{AC_CONFIG_HEADERS}; otherwise
7318 @samp{WORDS_BIGENDIAN} may be set incorrectly for Mac OS X universal
7319 binary files.
7320 @end defmac
7322 @anchor{AC_C_CONST}
7323 @defmac AC_C_CONST
7324 @acindex{C_CONST}
7325 @cvindex const
7326 @caindex c_const
7327 If the C compiler does not fully support the @code{const} keyword,
7328 define @code{const} to be empty.  Some C compilers that do
7329 not define @code{__STDC__} do support @code{const}; some compilers that
7330 define @code{__STDC__} do not completely support @code{const}.  Programs
7331 can simply use @code{const} as if every C compiler supported it; for
7332 those that don't, the makefile or configuration header file
7333 defines it as empty.
7335 Occasionally installers use a C++ compiler to compile C code, typically
7336 because they lack a C compiler.  This causes problems with @code{const},
7337 because C and C++ treat @code{const} differently.  For example:
7339 @example
7340 const int foo;
7341 @end example
7343 @noindent
7344 is valid in C but not in C++.  These differences unfortunately cannot be
7345 papered over by defining @code{const} to be empty.
7347 If @command{autoconf} detects this situation, it leaves @code{const} alone,
7348 as this generally yields better results in practice.  However, using a
7349 C++ compiler to compile C code is not recommended or supported, and
7350 installers who run into trouble in this area should get a C compiler
7351 like GCC to compile their C code.
7353 This macro caches its result in the @code{ac_cv_c_const} variable.
7355 This macro is obsolescent, as current C compilers support @code{const}.
7356 New programs need not use this macro.
7357 @end defmac
7359 @defmac AC_C_RESTRICT
7360 @acindex{C_RESTRICT}
7361 @cvindex restrict
7362 @caindex c_restrict
7363 If the C compiler recognizes a variant spelling for the @code{restrict}
7364 keyword (@code{__restrict}, @code{__restrict__}, or @code{_Restrict}),
7365 then define @code{restrict} to that; this is more likely to do the right
7366 thing with compilers that support language variants where plain
7367 @code{restrict} is not a keyword.  Otherwise, if the C compiler
7368 recognizes the @code{restrict} keyword, don't do anything.
7369 Otherwise, define @code{restrict} to be empty.
7370 Thus, programs may simply use @code{restrict} as if every C compiler
7371 supported it; for those that do not, the makefile
7372 or configuration header defines it away.
7374 Although support in C++ for the @code{restrict} keyword is not
7375 required, several C++ compilers do accept the keyword.
7376 This macro works for them, too.
7378 This macro caches @samp{no} in the @code{ac_cv_c_restrict} variable
7379 if @code{restrict} is not supported, and a supported spelling otherwise.
7380 @end defmac
7382 @defmac AC_C_VOLATILE
7383 @acindex{C_VOLATILE}
7384 @cvindex volatile
7385 If the C compiler does not understand the keyword @code{volatile},
7386 define @code{volatile} to be empty.  Programs can simply use
7387 @code{volatile} as if every C compiler supported it; for those that do
7388 not, the makefile or configuration header defines it as
7389 empty.
7391 If the correctness of your program depends on the semantics of
7392 @code{volatile}, simply defining it to be empty does, in a sense, break
7393 your code.  However, given that the compiler does not support
7394 @code{volatile}, you are at its mercy anyway.  At least your
7395 program compiles, when it wouldn't before.
7396 @xref{Volatile Objects}, for more about @code{volatile}.
7398 In general, the @code{volatile} keyword is a standard C feature, so
7399 you might expect that @code{volatile} is available only when
7400 @code{__STDC__} is defined.  However, Ultrix 4.3's native compiler does
7401 support volatile, but does not define @code{__STDC__}.
7403 This macro is obsolescent, as current C compilers support @code{volatile}.
7404 New programs need not use this macro.
7405 @end defmac
7407 @anchor{AC_C_INLINE}
7408 @defmac AC_C_INLINE
7409 @acindex{C_INLINE}
7410 @cvindex inline
7411 If the C compiler supports the keyword @code{inline}, do nothing.
7412 Otherwise define @code{inline} to @code{__inline__} or @code{__inline}
7413 if it accepts one of those, otherwise define @code{inline} to be empty.
7414 @end defmac
7416 @anchor{AC_C_CHAR_UNSIGNED}
7417 @defmac AC_C_CHAR_UNSIGNED
7418 @acindex{C_CHAR_UNSIGNED}
7419 @cvindex __CHAR_UNSIGNED__
7420 If the C type @code{char} is unsigned, define @code{__CHAR_UNSIGNED__},
7421 unless the C compiler predefines it.
7423 These days, using this macro is not necessary.  The same information can
7424 be determined by this portable alternative, thus avoiding the use of
7425 preprocessor macros in the namespace reserved for the implementation.
7427 @example
7428 #include <limits.h>
7429 #if CHAR_MIN == 0
7430 # define CHAR_UNSIGNED 1
7431 #endif
7432 @end example
7433 @end defmac
7435 @defmac AC_C_STRINGIZE
7436 @acindex{C_STRINGIZE}
7437 @cvindex HAVE_STRINGIZE
7438 If the C preprocessor supports the stringizing operator, define
7439 @code{HAVE_STRINGIZE}.  The stringizing operator is @samp{#} and is
7440 found in macros such as this:
7442 @example
7443 #define x(y) #y
7444 @end example
7446 This macro is obsolescent, as current C compilers support the
7447 stringizing operator.  New programs need not use this macro.
7448 @end defmac
7450 @defmac AC_C_FLEXIBLE_ARRAY_MEMBER
7451 @acindex{C_FLEXIBLE_ARRAY_MEMBER}
7452 @cvindex FLEXIBLE_ARRAY_MEMBER
7453 If the C compiler supports flexible array members, define
7454 @code{FLEXIBLE_ARRAY_MEMBER} to nothing; otherwise define it to 1.
7455 That way, a declaration like this:
7457 @example
7458 struct s
7459   @{
7460     size_t n_vals;
7461     double val[FLEXIBLE_ARRAY_MEMBER];
7462   @};
7463 @end example
7465 @noindent
7466 will let applications use the ``struct hack'' even with compilers that
7467 do not support flexible array members.  To allocate and use such an
7468 object, you can use code like this:
7470 @example
7471 size_t i;
7472 size_t n = compute_value_count ();
7473 struct s *p =
7474    malloc (offsetof (struct s, val)
7475            + n * sizeof (double));
7476 p->n_vals = n;
7477 for (i = 0; i < n; i++)
7478   p->val[i] = compute_value (i);
7479 @end example
7480 @end defmac
7482 @defmac AC_C_VARARRAYS
7483 @acindex{C_VARARRAYS}
7484 @cvindex HAVE_C_VARARRAYS
7485 If the C compiler supports variable-length arrays, define
7486 @code{HAVE_C_VARARRAYS}.  A variable-length array is an array of automatic
7487 storage duration whose length is determined at run time, when the array
7488 is declared.
7489 @end defmac
7491 @defmac AC_C_TYPEOF
7492 @acindex{C_TYPEOF}
7493 @cvindex HAVE_TYPEOF
7494 @cvindex typeof
7495 If the C compiler supports GCC's @code{typeof} syntax either
7496 directly or
7497 through a different spelling of the keyword (e.g., @code{__typeof__}),
7498 define @code{HAVE_TYPEOF}.  If the support is available only through a
7499 different spelling, define @code{typeof} to that spelling.
7500 @end defmac
7502 @defmac AC_C_PROTOTYPES
7503 @acindex{C_PROTOTYPES}
7504 @cvindex PROTOTYPES
7505 @cvindex __PROTOTYPES
7506 @cvindex PARAMS
7507 If function prototypes are understood by the compiler (as determined by
7508 @code{AC_PROG_CC}), define @code{PROTOTYPES} and @code{__PROTOTYPES}.
7509 Defining @code{__PROTOTYPES} is for the benefit of
7510 header files that cannot use macros that infringe on user name space.
7512 This macro is obsolescent, as current C compilers support prototypes.
7513 New programs need not use this macro.
7514 @end defmac
7516 @anchor{AC_PROG_GCC_TRADITIONAL}
7517 @defmac AC_PROG_GCC_TRADITIONAL
7518 @acindex{PROG_GCC_TRADITIONAL}
7519 @ovindex CC
7520 Add @option{-traditional} to output variable @code{CC} if using the
7521 GNU C compiler and @code{ioctl} does not work properly without
7522 @option{-traditional}.  That usually happens when the fixed header files
7523 have not been installed on an old system.
7525 This macro is obsolescent, since current versions of the GNU C
7526 compiler fix the header files automatically when installed.
7527 @end defmac
7530 @node C++ Compiler
7531 @subsection C++ Compiler Characteristics
7534 @defmac AC_PROG_CXX (@ovar{compiler-search-list})
7535 @acindex{PROG_CXX}
7536 @evindex CXX
7537 @evindex CXXFLAGS
7538 @ovindex CXX
7539 @ovindex CXXFLAGS
7540 Determine a C++ compiler to use.  Check whether the environment variable
7541 @code{CXX} or @code{CCC} (in that order) is set; if so, then set output
7542 variable @code{CXX} to its value.
7544 Otherwise, if the macro is invoked without an argument, then search for
7545 a C++ compiler under the likely names (first @code{g++} and @code{c++}
7546 then other names).  If none of those checks succeed, then as a last
7547 resort set @code{CXX} to @code{g++}.
7549 This macro may, however, be invoked with an optional first argument
7550 which, if specified, must be a blank-separated list of C++ compilers to
7551 search for.  This just gives the user an opportunity to specify an
7552 alternative search list for the C++ compiler.  For example, if you
7553 didn't like the default order, then you could invoke @code{AC_PROG_CXX}
7554 like this:
7556 @example
7557 AC_PROG_CXX([gcc cl KCC CC cxx cc++ xlC aCC c++ g++])
7558 @end example
7560 If using the GNU C++ compiler, set shell variable @code{GXX} to
7561 @samp{yes}.  If output variable @code{CXXFLAGS} was not already set, set
7562 it to @option{-g -O2} for the GNU C++ compiler (@option{-O2} on
7563 systems where G++ does not accept @option{-g}), or @option{-g} for other
7564 compilers.  If your package does not like this default, then it is
7565 acceptable to insert the line @samp{: $@{CXXFLAGS=""@}} after @code{AC_INIT}
7566 and before @code{AC_PROG_CXX} to select an empty default instead.
7568 @end defmac
7570 @defmac AC_PROG_CXXCPP
7571 @acindex{PROG_CXXCPP}
7572 @evindex CXXCPP
7573 @ovindex CXXCPP
7574 Set output variable @code{CXXCPP} to a command that runs the C++
7575 preprocessor.  If @samp{$CXX -E} doesn't work, @file{/lib/cpp} is used.
7576 It is portable to run @code{CXXCPP} only on files with a @file{.c},
7577 @file{.C}, @file{.cc}, or @file{.cpp} extension.
7579 Some preprocessors don't indicate missing include files by the error
7580 status.  For such preprocessors an internal variable is set that causes
7581 other macros to check the standard error from the preprocessor and
7582 consider the test failed if any warnings have been reported.  However,
7583 it is not known whether such broken preprocessors exist for C++.
7584 @end defmac
7586 @defmac AC_PROG_CXX_C_O
7587 @acindex{PROG_CXX_C_O}
7588 @cvindex CXX_NO_MINUS_C_MINUS_O
7589 Test whether the C++ compiler accepts the options @option{-c} and
7590 @option{-o} simultaneously, and define @code{CXX_NO_MINUS_C_MINUS_O},
7591 if it does not.
7592 @end defmac
7595 @node Objective C Compiler
7596 @subsection Objective C Compiler Characteristics
7599 @defmac AC_PROG_OBJC (@ovar{compiler-search-list})
7600 @acindex{PROG_OBJC}
7601 @evindex OBJC
7602 @evindex OBJCFLAGS
7603 @ovindex OBJC
7604 @ovindex OBJCFLAGS
7605 Determine an Objective C compiler to use.  If @code{OBJC} is not already
7606 set in the environment, check for Objective C compilers.  Set output
7607 variable @code{OBJC} to the name of the compiler found.
7609 This macro may, however, be invoked with an optional first argument
7610 which, if specified, must be a blank-separated list of Objective C compilers to
7611 search for.  This just gives the user an opportunity to specify an
7612 alternative search list for the Objective C compiler.  For example, if you
7613 didn't like the default order, then you could invoke @code{AC_PROG_OBJC}
7614 like this:
7616 @example
7617 AC_PROG_OBJC([gcc objcc objc])
7618 @end example
7620 If using the GNU Objective C compiler, set shell variable
7621 @code{GOBJC} to @samp{yes}.  If output variable @code{OBJCFLAGS} was not
7622 already set, set it to @option{-g -O2} for the GNU Objective C
7623 compiler (@option{-O2} on systems where @command{gcc} does not accept
7624 @option{-g}), or @option{-g} for other compilers.
7625 @end defmac
7627 @defmac AC_PROG_OBJCPP
7628 @acindex{PROG_OBJCPP}
7629 @evindex OBJCPP
7630 @ovindex OBJCPP
7631 Set output variable @code{OBJCPP} to a command that runs the Objective C
7632 preprocessor.  If @samp{$OBJC -E} doesn't work, @file{/lib/cpp} is used.
7633 @end defmac
7636 @node Objective C++ Compiler
7637 @subsection Objective C++ Compiler Characteristics
7640 @defmac AC_PROG_OBJCXX (@ovar{compiler-search-list})
7641 @acindex{PROG_OBJCXX}
7642 @evindex OBJCXX
7643 @evindex OBJCXXFLAGS
7644 @ovindex OBJCXX
7645 @ovindex OBJCXXFLAGS
7646 Determine an Objective C++ compiler to use.  If @code{OBJCXX} is not already
7647 set in the environment, check for Objective C++ compilers.  Set output
7648 variable @code{OBJCXX} to the name of the compiler found.
7650 This macro may, however, be invoked with an optional first argument
7651 which, if specified, must be a blank-separated list of Objective C++ compilers
7652 to search for.  This just gives the user an opportunity to specify an
7653 alternative search list for the Objective C++ compiler.  For example, if you
7654 didn't like the default order, then you could invoke @code{AC_PROG_OBJCXX}
7655 like this:
7657 @example
7658 AC_PROG_OBJCXX([gcc g++ objcc++ objcxx])
7659 @end example
7661 If using the GNU Objective C++ compiler, set shell variable
7662 @code{GOBJCXX} to @samp{yes}.  If output variable @code{OBJCXXFLAGS} was not
7663 already set, set it to @option{-g -O2} for the GNU Objective C++
7664 compiler (@option{-O2} on systems where @command{gcc} does not accept
7665 @option{-g}), or @option{-g} for other compilers.
7666 @end defmac
7668 @defmac AC_PROG_OBJCXXCPP
7669 @acindex{PROG_OBJCXXCPP}
7670 @evindex OBJCXXCPP
7671 @ovindex OBJCXXCPP
7672 Set output variable @code{OBJCXXCPP} to a command that runs the Objective C++
7673 preprocessor.  If @samp{$OBJCXX -E} doesn't work, @file{/lib/cpp} is used.
7674 @end defmac
7677 @node Erlang Compiler and Interpreter
7678 @subsection Erlang Compiler and Interpreter Characteristics
7679 @cindex Erlang
7681 Autoconf defines the following macros for determining paths to the essential
7682 Erlang/OTP programs:
7684 @defmac AC_ERLANG_PATH_ERLC (@ovar{value-if-not-found}, @dvar{path, $PATH})
7685 @acindex{ERLANG_PATH_ERLC}
7686 @evindex ERLC
7687 @evindex ERLCFLAGS
7688 @ovindex ERLC
7689 @ovindex ERLCFLAGS
7690 Determine an Erlang compiler to use.  If @code{ERLC} is not already set in the
7691 environment, check for @command{erlc}.  Set output variable @code{ERLC} to the
7692 complete path of the compiler command found.  In addition, if @code{ERLCFLAGS}
7693 is not set in the environment, set it to an empty value.
7695 The two optional arguments have the same meaning as the two last arguments of
7696 macro @code{AC_PROG_PATH} for looking for the @command{erlc} program.  For
7697 example, to look for @command{erlc} only in the @file{/usr/lib/erlang/bin}
7698 directory:
7700 @example
7701 AC_ERLANG_PATH_ERLC([not found], [/usr/lib/erlang/bin])
7702 @end example
7703 @end defmac
7705 @defmac AC_ERLANG_NEED_ERLC (@dvar{path, $PATH})
7706 @acindex{ERLANG_NEED_ERLC}
7707 A simplified variant of the @code{AC_ERLANG_PATH_ERLC} macro, that prints an
7708 error message and exits the @command{configure} script if the @command{erlc}
7709 program is not found.
7710 @end defmac
7712 @defmac AC_ERLANG_PATH_ERL (@ovar{value-if-not-found}, @dvar{path, $PATH})
7713 @acindex{ERLANG_PATH_ERL}
7714 @evindex ERL
7715 @ovindex ERL
7716 Determine an Erlang interpreter to use.  If @code{ERL} is not already
7717 set in the
7718 environment, check for @command{erl}.  Set output variable @code{ERL} to the
7719 complete path of the interpreter command found.
7721 The two optional arguments have the same meaning as the two last arguments of
7722 macro @code{AC_PROG_PATH} for looking for the @command{erl} program.  For
7723 example, to look for @command{erl} only in the @file{/usr/lib/erlang/bin}
7724 directory:
7726 @example
7727 AC_ERLANG_PATH_ERL([not found], [/usr/lib/erlang/bin])
7728 @end example
7729 @end defmac
7731 @defmac AC_ERLANG_NEED_ERL (@dvar{path, $PATH})
7732 @acindex{ERLANG_NEED_ERL}
7733 A simplified variant of the @code{AC_ERLANG_PATH_ERL} macro, that prints an
7734 error message and exits the @command{configure} script if the @command{erl}
7735 program is not found.
7736 @end defmac
7739 @node Fortran Compiler
7740 @subsection Fortran Compiler Characteristics
7741 @cindex Fortran
7742 @cindex F77
7744 The Autoconf Fortran support is divided into two categories: legacy
7745 Fortran 77 macros (@code{F77}), and modern Fortran macros (@code{FC}).
7746 The former are intended for traditional Fortran 77 code, and have output
7747 variables like @code{F77}, @code{FFLAGS}, and @code{FLIBS}.  The latter
7748 are for newer programs that can (or must) compile under the newer
7749 Fortran standards, and have output variables like @code{FC},
7750 @code{FCFLAGS}, and @code{FCLIBS}.
7752 Except for two new macros @code{AC_FC_SRCEXT} and
7753 @code{AC_FC_FREEFORM} (see below), the @code{FC} and @code{F77} macros
7754 behave almost identically, and so they are documented together in this
7755 section.
7758 @defmac AC_PROG_F77 (@ovar{compiler-search-list})
7759 @acindex{PROG_F77}
7760 @evindex F77
7761 @evindex FFLAGS
7762 @ovindex F77
7763 @ovindex FFLAGS
7764 Determine a Fortran 77 compiler to use.  If @code{F77} is not already
7765 set in the environment, then check for @code{g77} and @code{f77}, and
7766 then some other names.  Set the output variable @code{F77} to the name
7767 of the compiler found.
7769 This macro may, however, be invoked with an optional first argument
7770 which, if specified, must be a blank-separated list of Fortran 77
7771 compilers to search for.  This just gives the user an opportunity to
7772 specify an alternative search list for the Fortran 77 compiler.  For
7773 example, if you didn't like the default order, then you could invoke
7774 @code{AC_PROG_F77} like this:
7776 @example
7777 AC_PROG_F77([fl32 f77 fort77 xlf g77 f90 xlf90])
7778 @end example
7780 If using @code{g77} (the GNU Fortran 77 compiler), then
7781 set the shell variable @code{G77} to @samp{yes}.
7782 If the output variable @code{FFLAGS} was not already set in the
7783 environment, then set it to @option{-g -02} for @code{g77} (or @option{-O2}
7784 where @code{g77} does not accept @option{-g}).  Otherwise, set
7785 @code{FFLAGS} to @option{-g} for all other Fortran 77 compilers.
7786 @end defmac
7788 @defmac AC_PROG_FC (@ovar{compiler-search-list}, @ovar{dialect})
7789 @acindex{PROG_FC}
7790 @evindex FC
7791 @evindex FCFLAGS
7792 @ovindex FC
7793 @ovindex FCFLAGS
7794 Determine a Fortran compiler to use.  If @code{FC} is not already set in
7795 the environment, then @code{dialect} is a hint to indicate what Fortran
7796 dialect to search for; the default is to search for the newest available
7797 dialect.  Set the output variable @code{FC} to the name of the compiler
7798 found.
7800 By default, newer dialects are preferred over older dialects, but if
7801 @code{dialect} is specified then older dialects are preferred starting
7802 with the specified dialect.  @code{dialect} can currently be one of
7803 Fortran 77, Fortran 90, or Fortran 95.  However, this is only a hint of
7804 which compiler @emph{name} to prefer (e.g., @code{f90} or @code{f95}),
7805 and no attempt is made to guarantee that a particular language standard
7806 is actually supported.  Thus, it is preferable that you avoid the
7807 @code{dialect} option, and use AC_PROG_FC only for code compatible with
7808 the latest Fortran standard.
7810 This macro may, alternatively, be invoked with an optional first argument
7811 which, if specified, must be a blank-separated list of Fortran
7812 compilers to search for, just as in @code{AC_PROG_F77}.
7814 If the output variable @code{FCFLAGS} was not already set in the
7815 environment, then set it to @option{-g -02} for GNU @code{g77} (or
7816 @option{-O2} where @code{g77} does not accept @option{-g}).  Otherwise,
7817 set @code{FCFLAGS} to @option{-g} for all other Fortran compilers.
7818 @end defmac
7820 @defmac AC_PROG_F77_C_O
7821 @defmacx AC_PROG_FC_C_O
7822 @acindex{PROG_F77_C_O}
7823 @acindex{PROG_FC_C_O}
7824 @cvindex F77_NO_MINUS_C_MINUS_O
7825 @cvindex FC_NO_MINUS_C_MINUS_O
7826 Test whether the Fortran compiler accepts the options @option{-c} and
7827 @option{-o} simultaneously, and define @code{F77_NO_MINUS_C_MINUS_O} or
7828 @code{FC_NO_MINUS_C_MINUS_O}, respectively, if it does not.
7829 @end defmac
7831 The following macros check for Fortran compiler characteristics.
7832 To check for characteristics not listed here, use
7833 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
7834 @code{AC_RUN_IFELSE} (@pxref{Runtime}), making sure to first set the
7835 current language to Fortran 77 or Fortran via @code{AC_LANG([Fortran 77])}
7836 or @code{AC_LANG(Fortran)} (@pxref{Language Choice}).
7839 @defmac AC_F77_LIBRARY_LDFLAGS
7840 @defmacx AC_FC_LIBRARY_LDFLAGS
7841 @acindex{F77_LIBRARY_LDFLAGS}
7842 @ovindex FLIBS
7843 @acindex{FC_LIBRARY_LDFLAGS}
7844 @ovindex FCLIBS
7845 Determine the linker flags (e.g., @option{-L} and @option{-l}) for the
7846 @dfn{Fortran intrinsic and runtime libraries} that are required to
7847 successfully link a Fortran program or shared library.  The output
7848 variable @code{FLIBS} or @code{FCLIBS} is set to these flags (which
7849 should be included after @code{LIBS} when linking).
7851 This macro is intended to be used in those situations when it is
7852 necessary to mix, e.g., C++ and Fortran source code in a single
7853 program or shared library (@pxref{Mixing Fortran 77 With C and C++, , ,
7854 automake, GNU Automake}).
7856 For example, if object files from a C++ and Fortran compiler must be
7857 linked together, then the C++ compiler/linker must be used for linking
7858 (since special C++-ish things need to happen at link time like calling
7859 global constructors, instantiating templates, enabling exception
7860 support, etc.).
7862 However, the Fortran intrinsic and runtime libraries must be linked in
7863 as well, but the C++ compiler/linker doesn't know by default how to add
7864 these Fortran 77 libraries.  Hence, this macro was created to determine
7865 these Fortran libraries.
7867 The macros @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
7868 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} are probably also necessary to
7869 link C/C++ with Fortran; see below.
7870 @end defmac
7872 @defmac AC_F77_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
7873 @defmacx AC_FC_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
7874 @acindex{F77_DUMMY_MAIN}
7875 @cvindex F77_DUMMY_MAIN
7876 With many compilers, the Fortran libraries detected by
7877 @code{AC_F77_LIBRARY_LDFLAGS} or @code{AC_FC_LIBRARY_LDFLAGS} provide
7878 their own @code{main} entry function that initializes things like
7879 Fortran I/O, and which then calls a user-provided entry function named
7880 (say) @code{MAIN__} to run the user's program.  The
7881 @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
7882 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros figure out how to deal with
7883 this interaction.
7885 When using Fortran for purely numerical functions (no I/O, etc.)@: often
7886 one prefers to provide one's own @code{main} and skip the Fortran
7887 library initializations.  In this case, however, one may still need to
7888 provide a dummy @code{MAIN__} routine in order to prevent linking errors
7889 on some systems.  @code{AC_F77_DUMMY_MAIN} or @code{AC_FC_DUMMY_MAIN}
7890 detects whether any such routine is @emph{required} for linking, and
7891 what its name is; the shell variable @code{F77_DUMMY_MAIN} or
7892 @code{FC_DUMMY_MAIN} holds this name, @code{unknown} when no solution
7893 was found, and @code{none} when no such dummy main is needed.
7895 By default, @var{action-if-found} defines @code{F77_DUMMY_MAIN} or
7896 @code{FC_DUMMY_MAIN} to the name of this routine (e.g., @code{MAIN__})
7897 @emph{if} it is required.  @var{action-if-not-found} defaults to
7898 exiting with an error.
7900 In order to link with Fortran routines, the user's C/C++ program should
7901 then include the following code to define the dummy main if it is
7902 needed:
7904 @example
7905 #ifdef F77_DUMMY_MAIN
7906 #  ifdef __cplusplus
7907      extern "C"
7908 #  endif
7909    int F77_DUMMY_MAIN() @{ return 1; @}
7910 #endif
7911 @end example
7913 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7915 Note that this macro is called automatically from @code{AC_F77_WRAPPERS}
7916 or @code{AC_FC_WRAPPERS}; there is generally no need to call it
7917 explicitly unless one wants to change the default actions.
7918 @end defmac
7920 @defmac AC_F77_MAIN
7921 @defmacx AC_FC_MAIN
7922 @acindex{F77_MAIN}
7923 @cvindex F77_MAIN
7924 @acindex{FC_MAIN}
7925 @cvindex FC_MAIN
7926 As discussed above, many Fortran libraries allow you to provide an entry
7927 point called (say) @code{MAIN__} instead of the usual @code{main}, which
7928 is then called by a @code{main} function in the Fortran libraries that
7929 initializes things like Fortran I/O@.  The
7930 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros detect whether it is
7931 @emph{possible} to utilize such an alternate main function, and defines
7932 @code{F77_MAIN} and @code{FC_MAIN} to the name of the function.  (If no
7933 alternate main function name is found, @code{F77_MAIN} and @code{FC_MAIN} are
7934 simply defined to @code{main}.)
7936 Thus, when calling Fortran routines from C that perform things like I/O,
7937 one should use this macro and declare the "main" function like so:
7939 @example
7940 #ifdef __cplusplus
7941   extern "C"
7942 #endif
7943 int F77_MAIN(int argc, char *argv[]);
7944 @end example
7946 (Again, replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7947 @end defmac
7949 @defmac AC_F77_WRAPPERS
7950 @defmacx AC_FC_WRAPPERS
7951 @acindex{F77_WRAPPERS}
7952 @cvindex F77_FUNC
7953 @cvindex F77_FUNC_
7954 @acindex{FC_WRAPPERS}
7955 @cvindex FC_FUNC
7956 @cvindex FC_FUNC_
7957 Defines C macros @code{F77_FUNC (name, NAME)}, @code{FC_FUNC (name, NAME)},
7958 @code{F77_FUNC_(name, NAME)}, and @code{FC_FUNC_(name, NAME)} to properly
7959 mangle the names of C/C++ identifiers, and identifiers with underscores,
7960 respectively, so that they match the name-mangling scheme used by the
7961 Fortran compiler.
7963 Fortran is case-insensitive, and in order to achieve this the Fortran
7964 compiler converts all identifiers into a canonical case and format.  To
7965 call a Fortran subroutine from C or to write a C function that is
7966 callable from Fortran, the C program must explicitly use identifiers in
7967 the format expected by the Fortran compiler.  In order to do this, one
7968 simply wraps all C identifiers in one of the macros provided by
7969 @code{AC_F77_WRAPPERS} or @code{AC_FC_WRAPPERS}.  For example, suppose
7970 you have the following Fortran 77 subroutine:
7972 @example
7973       subroutine foobar (x, y)
7974       double precision x, y
7975       y = 3.14159 * x
7976       return
7977       end
7978 @end example
7980 You would then declare its prototype in C or C++ as:
7982 @example
7983 #define FOOBAR_F77 F77_FUNC (foobar, FOOBAR)
7984 #ifdef __cplusplus
7985 extern "C"  /* prevent C++ name mangling */
7986 #endif
7987 void FOOBAR_F77(double *x, double *y);
7988 @end example
7990 Note that we pass both the lowercase and uppercase versions of the
7991 function name to @code{F77_FUNC} so that it can select the right one.
7992 Note also that all parameters to Fortran 77 routines are passed as
7993 pointers (@pxref{Mixing Fortran 77 With C and C++, , , automake, GNU
7994 Automake}).
7996 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7998 Although Autoconf tries to be intelligent about detecting the
7999 name-mangling scheme of the Fortran compiler, there may be Fortran
8000 compilers that it doesn't support yet.  In this case, the above code
8001 generates a compile-time error, but some other behavior
8002 (e.g., disabling Fortran-related features) can be induced by checking
8003 whether @code{F77_FUNC} or @code{FC_FUNC} is defined.
8005 Now, to call that routine from a C program, we would do something like:
8007 @example
8009     double x = 2.7183, y;
8010     FOOBAR_F77 (&x, &y);
8012 @end example
8014 If the Fortran identifier contains an underscore (e.g., @code{foo_bar}),
8015 you should use @code{F77_FUNC_} or @code{FC_FUNC_} instead of
8016 @code{F77_FUNC} or @code{FC_FUNC} (with the same arguments).  This is
8017 because some Fortran compilers mangle names differently if they contain
8018 an underscore.
8019 @end defmac
8021 @defmac AC_F77_FUNC (@var{name}, @ovar{shellvar})
8022 @defmacx AC_FC_FUNC (@var{name}, @ovar{shellvar})
8023 @acindex{F77_FUNC}
8024 @acindex{FC_FUNC}
8025 Given an identifier @var{name}, set the shell variable @var{shellvar} to
8026 hold the mangled version @var{name} according to the rules of the
8027 Fortran linker (see also @code{AC_F77_WRAPPERS} or
8028 @code{AC_FC_WRAPPERS}).  @var{shellvar} is optional; if it is not
8029 supplied, the shell variable is simply @var{name}.  The purpose of
8030 this macro is to give the caller a way to access the name-mangling
8031 information other than through the C preprocessor as above, for example,
8032 to call Fortran routines from some language other than C/C++.
8033 @end defmac
8035 @defmac AC_FC_SRCEXT (@var{ext}, @ovar{action-if-success}, @
8036   @ovar{action-if-failure})
8037 @acindex{FC_SRCEXT}
8038 By default, the @code{FC} macros perform their tests using a @file{.f}
8039 extension for source-code files.  Some compilers, however, only enable
8040 newer language features for appropriately named files, e.g., Fortran 90
8041 features only for @file{.f90} files.  On the other hand, some other
8042 compilers expect all source files to end in @file{.f} and require
8043 special flags to support other file name extensions.  The
8044 @code{AC_FC_SRCEXT} macro deals with both of these issues.
8046 The @code{AC_FC_SRCEXT} tries to get the @code{FC} compiler to accept files
8047 ending with the extension .@var{ext} (i.e., @var{ext} does @emph{not}
8048 contain the dot).  If any special compiler flags are needed for this, it
8049 stores them in the output variable @code{FCFLAGS_}@var{ext}.  This
8050 extension and these flags are then used for all subsequent @code{FC} tests
8051 (until @code{AC_FC_SRCEXT} is called again).
8053 For example, you would use @code{AC_FC_SRCEXT(f90)} to employ the
8054 @file{.f90} extension in future tests, and it would set the
8055 @code{FCFLAGS_f90} output variable with any extra flags that are needed
8056 to compile such files.
8058 The @code{FCFLAGS_}@var{ext} can @emph{not} be simply absorbed into
8059 @code{FCFLAGS}, for two reasons based on the limitations of some
8060 compilers.  First, only one @code{FCFLAGS_}@var{ext} can be used at a
8061 time, so files with different extensions must be compiled separately.
8062 Second, @code{FCFLAGS_}@var{ext} must appear @emph{immediately} before
8063 the source-code file name when compiling.  So, continuing the example
8064 above, you might compile a @file{foo.f90} file in your makefile with the
8065 command:
8067 @example
8068 foo.o: foo.f90
8069      $(FC) -c $(FCFLAGS) $(FCFLAGS_f90) '$(srcdir)/foo.f90'
8070 @end example
8072 If @code{AC_FC_SRCEXT} succeeds in compiling files with the @var{ext}
8073 extension, it calls @var{action-if-success} (defaults to nothing).  If
8074 it fails, and cannot find a way to make the @code{FC} compiler accept such
8075 files, it calls @var{action-if-failure} (defaults to exiting with an
8076 error message).
8078 @end defmac
8080 @defmac AC_FC_FREEFORM (@ovar{action-if-success}, @ovar{action-if-failure})
8081 @acindex{FC_FREEFORM}
8083 The @code{AC_FC_FREEFORM} tries to ensure that the Fortran compiler
8084 (@code{$FC}) allows free-format source code (as opposed to the older
8085 fixed-format style from Fortran 77).  If necessary, it may add some
8086 additional flags to @code{FCFLAGS}.
8088 This macro is most important if you are using the default @file{.f}
8089 extension, since many compilers interpret this extension as indicating
8090 fixed-format source unless an additional flag is supplied.  If you
8091 specify a different extension with @code{AC_FC_SRCEXT}, such as
8092 @file{.f90} or @file{.f95}, then @code{AC_FC_FREEFORM} ordinarily
8093 succeeds without modifying @code{FCFLAGS}.
8095 If @code{AC_FC_FREEFORM} succeeds in compiling free-form source, it
8096 calls @var{action-if-success} (defaults to nothing).  If it fails, it
8097 calls @var{action-if-failure} (defaults to exiting with an error
8098 message).
8099 @end defmac
8101 @node System Services
8102 @section System Services
8104 The following macros check for operating system services or capabilities.
8106 @anchor{AC_PATH_X}
8107 @defmac AC_PATH_X
8108 @acindex{PATH_X}
8109 @evindex XMKMF
8110 @cindex X Window System
8111 Try to locate the X Window System include files and libraries.  If the
8112 user gave the command line options @option{--x-includes=@var{dir}} and
8113 @option{--x-libraries=@var{dir}}, use those directories.
8115 If either or both were not given, get the missing values by running
8116 @code{xmkmf} (or an executable pointed to by the @code{XMKMF}
8117 environment variable) on a trivial @file{Imakefile} and examining the
8118 makefile that it produces.  Setting @code{XMKMF} to @samp{false}
8119 disables this method.
8121 If this method fails to find the X Window System, @command{configure}
8122 looks for the files in several directories where they often reside.
8123 If either method is successful, set the shell variables
8124 @code{x_includes} and @code{x_libraries} to their locations, unless they
8125 are in directories the compiler searches by default.
8127 If both methods fail, or the user gave the command line option
8128 @option{--without-x}, set the shell variable @code{no_x} to @samp{yes};
8129 otherwise set it to the empty string.
8130 @end defmac
8132 @anchor{AC_PATH_XTRA}
8133 @defmac AC_PATH_XTRA
8134 @acindex{PATH_XTRA}
8135 @ovindex X_CFLAGS
8136 @ovindex X_LIBS
8137 @ovindex X_EXTRA_LIBS
8138 @ovindex X_PRE_LIBS
8139 @cvindex X_DISPLAY_MISSING
8140 An enhanced version of @code{AC_PATH_X}.  It adds the C compiler flags
8141 that X needs to output variable @code{X_CFLAGS}, and the X linker flags
8142 to @code{X_LIBS}.  Define @code{X_DISPLAY_MISSING} if X is not
8143 available.
8145 This macro also checks for special libraries that some systems need in
8146 order to compile X programs.  It adds any that the system needs to
8147 output variable @code{X_EXTRA_LIBS}.  And it checks for special X11R6
8148 libraries that need to be linked with before @option{-lX11}, and adds
8149 any found to the output variable @code{X_PRE_LIBS}.
8151 @c This is an incomplete kludge.  Make a real way to do it.
8152 @c If you need to check for other X functions or libraries yourself, then
8153 @c after calling this macro, add the contents of @code{X_EXTRA_LIBS} to
8154 @c @code{LIBS} temporarily, like this: (FIXME - add example)
8155 @end defmac
8157 @anchor{AC_SYS_INTERPRETER}
8158 @defmac AC_SYS_INTERPRETER
8159 @acindex{SYS_INTERPRETER}
8160 Check whether the system supports starting scripts with a line of the
8161 form @samp{#!/bin/sh} to select the interpreter to use for the script.
8162 After running this macro, shell code in @file{configure.ac} can check
8163 the shell variable @code{interpval}; it is set to @samp{yes}
8164 if the system supports @samp{#!}, @samp{no} if not.
8165 @end defmac
8167 @defmac AC_SYS_LARGEFILE
8168 @acindex{SYS_LARGEFILE}
8169 @cvindex _FILE_OFFSET_BITS
8170 @cvindex _LARGE_FILES
8171 @ovindex CC
8172 @cindex Large file support
8173 @cindex LFS
8174 Arrange for 64-bit file offsets, known as
8175 @uref{http://@/www.unix-systems@/.org/@/version2/@/whatsnew/@/lfs20mar.html,
8176 large-file support}.  On some hosts, one must use special compiler
8177 options to build programs that can access large files.  Append any such
8178 options to the output variable @code{CC}.  Define
8179 @code{_FILE_OFFSET_BITS} and @code{_LARGE_FILES} if necessary.
8181 Large-file support can be disabled by configuring with the
8182 @option{--disable-largefile} option.
8184 If you use this macro, check that your program works even when
8185 @code{off_t} is wider than @code{long int}, since this is common when
8186 large-file support is enabled.  For example, it is not correct to print
8187 an arbitrary @code{off_t} value @code{X} with @code{printf ("%ld",
8188 (long int) X)}.
8190 The LFS introduced the @code{fseeko} and @code{ftello} functions to
8191 replace their C counterparts @code{fseek} and @code{ftell} that do not
8192 use @code{off_t}.  Take care to use @code{AC_FUNC_FSEEKO} to make their
8193 prototypes available when using them and large-file support is
8194 enabled.
8195 @end defmac
8197 @anchor{AC_SYS_LONG_FILE_NAMES}
8198 @defmac AC_SYS_LONG_FILE_NAMES
8199 @acindex{SYS_LONG_FILE_NAMES}
8200 @cvindex HAVE_LONG_FILE_NAMES
8201 If the system supports file names longer than 14 characters, define
8202 @code{HAVE_LONG_FILE_NAMES}.
8203 @end defmac
8205 @defmac AC_SYS_POSIX_TERMIOS
8206 @acindex{SYS_POSIX_TERMIOS}
8207 @cindex Posix termios headers
8208 @cindex termios Posix headers
8209 @caindex sys_posix_termios
8210 Check to see if the Posix termios headers and functions are available on the
8211 system.  If so, set the shell variable @code{ac_cv_sys_posix_termios} to
8212 @samp{yes}.  If not, set the variable to @samp{no}.
8213 @end defmac
8215 @node Posix Variants
8216 @section Posix Variants
8218 The following macro makes it possible to use features of Posix that are
8219 extensions to C, as well as platform extensions not defined by Posix.
8221 @anchor{AC_USE_SYSTEM_EXTENSIONS}
8222 @defmac AC_USE_SYSTEM_EXTENSIONS
8223 @acindex{USE_SYSTEM_EXTENSIONS}
8224 @cvindex _ALL_SOURCE
8225 @cvindex _GNU_SOURCE
8226 @cvindex _MINIX
8227 @cvindex _POSIX_1_SOURCE
8228 @cvindex _POSIX_PTHREAD_SEMANTICS
8229 @cvindex _POSIX_SOURCE
8230 @cvindex _TANDEM_SOURCE
8231 @cvindex __EXTENSIONS__
8232 This macro was introduced in Autoconf 2.60.  If possible, enable
8233 extensions to C or Posix on hosts that normally disable the extensions,
8234 typically due to standards-conformance namespace issues.  This should be
8235 called before any macros that run the C compiler.  The following
8236 preprocessor macros are defined where appropriate:
8238 @table @code
8239 @item _GNU_SOURCE
8240 Enable extensions on GNU/Linux.
8241 @item __EXTENSIONS__
8242 Enable general extensions on Solaris.
8243 @item _POSIX_PTHREAD_SEMANTICS
8244 Enable threading extensions on Solaris.
8245 @item _TANDEM_SOURCE
8246 Enable extensions for the HP NonStop platform.
8247 @item _ALL_SOURCE
8248 Enable extensions for AIX 3, and for Interix.
8249 @item _POSIX_SOURCE
8250 Enable Posix functions for Minix.
8251 @item _POSIX_1_SOURCE
8252 Enable additional Posix functions for Minix.
8253 @item _MINIX
8254 Identify Minix platform.  This particular preprocessor macro is
8255 obsolescent, and may be removed in a future release of Autoconf.
8256 @end table
8257 @end defmac
8260 @node Erlang Libraries
8261 @section Erlang Libraries
8262 @cindex Erlang, Library, checking
8264 The following macros check for an installation of Erlang/OTP, and for the
8265 presence of certain Erlang libraries.  All those macros require the
8266 configuration of an Erlang interpreter and an Erlang compiler
8267 (@pxref{Erlang Compiler and Interpreter}).
8269 @defmac AC_ERLANG_SUBST_ERTS_VER
8270 @acindex{ERLANG_SUBST_ERTS_VER}
8271 @ovindex ERLANG_ERTS_VER
8272 Set the output variable @code{ERLANG_ERTS_VER} to the version of the
8273 Erlang runtime system (as returned by Erlang's
8274 @code{erlang:system_info(version)} function).  The result of this test
8275 is cached if caching is enabled when running @command{configure}.  The
8276 @code{ERLANG_ERTS_VER} variable is not intended to be used for testing
8277 for features of specific ERTS versions, but to be used for substituting
8278 the ERTS version in Erlang/OTP release resource files (@code{.rel}
8279 files), as shown below.
8280 @end defmac
8282 @defmac AC_ERLANG_SUBST_ROOT_DIR
8283 @acindex{ERLANG_SUBST_ROOT_DIR}
8284 @ovindex ERLANG_ROOT_DIR
8285 Set the output variable @code{ERLANG_ROOT_DIR} to the path to the base
8286 directory in which Erlang/OTP is installed (as returned by Erlang's
8287 @code{code:root_dir/0} function).  The result of this test is cached if
8288 caching is enabled when running @command{configure}.
8289 @end defmac
8291 @defmac AC_ERLANG_SUBST_LIB_DIR
8292 @acindex{ERLANG_SUBST_LIB_DIR}
8293 @ovindex ERLANG_LIB_DIR
8294 Set the output variable @code{ERLANG_LIB_DIR} to the path of the library
8295 directory of Erlang/OTP (as returned by Erlang's
8296 @code{code:lib_dir/0} function), which subdirectories each contain an installed
8297 Erlang/OTP library.  The result of this test is cached if caching is enabled
8298 when running @command{configure}.
8299 @end defmac
8301 @defmac AC_ERLANG_CHECK_LIB (@var{library}, @ovar{action-if-found}, @
8302   @ovar{action-if-not-found})
8303 @acindex{ERLANG_CHECK_LIB}
8304 @ovindex ERLANG_LIB_DIR_@var{library}
8305 @ovindex ERLANG_LIB_VER_@var{library}
8306 Test whether the Erlang/OTP library @var{library} is installed by
8307 calling Erlang's @code{code:lib_dir/1} function.  The result of this
8308 test is cached if caching is enabled when running @command{configure}.
8309 @var{action-if-found} is a list of shell commands to run if the library
8310 is installed; @var{action-if-not-found} is a list of shell commands to
8311 run if it is not.  Additionally, if the library is installed, the output
8312 variable @samp{ERLANG_LIB_DIR_@var{library}} is set to the path to the
8313 library installation directory, and the output variable
8314 @samp{ERLANG_LIB_VER_@var{library}} is set to the version number that is
8315 part of the subdirectory name, if it is in the standard form
8316 (@code{@var{library}-@var{version}}).  If the directory name does not
8317 have a version part, @samp{ERLANG_LIB_VER_@var{library}} is set to the
8318 empty string.  If the library is not installed,
8319 @samp{ERLANG_LIB_DIR_@var{library}} and
8320 @samp{ERLANG_LIB_VER_@var{library}} are set to @code{"not found"}.  For
8321 example, to check if library @code{stdlib} is installed:
8323 @example
8324 AC_ERLANG_CHECK_LIB([stdlib],
8325   [echo "stdlib version \"$ERLANG_LIB_VER_stdlib\""
8326    echo "is installed in \"$ERLANG_LIB_DIR_stdlib\""],
8327   [AC_MSG_ERROR([stdlib was not found!])])
8328 @end example
8330 The @samp{ERLANG_LIB_VER_@var{library}} variables (set by
8331 @code{AC_ERLANG_CHECK_LIB}) and the @code{ERLANG_ERTS_VER} variable (set
8332 by @code{AC_ERLANG_SUBST_ERTS_VER}) are not intended to be used for
8333 testing for features of specific versions of libraries or of the Erlang
8334 runtime system.  Those variables are intended to be substituted in
8335 Erlang release resource files (@code{.rel} files).  For instance, to
8336 generate a @file{example.rel} file for an application depending on the
8337 @code{stdlib} library, @file{configure.ac} could contain:
8339 @example
8340 AC_ERLANG_SUBST_ERTS_VER
8341 AC_ERLANG_CHECK_LIB([stdlib],
8342   [],
8343   [AC_MSG_ERROR([stdlib was not found!])])
8344 AC_CONFIG_FILES([example.rel])
8345 @end example
8347 @noindent
8348 The @file{example.rel.in} file used to generate @file{example.rel}
8349 should contain:
8351 @example
8352 @{release,
8353     @{"@@PACKAGE@@", "@@VERSION@@"@},
8354     @{erts, "@@ERLANG_ERTS_VER@@"@},
8355     [@{stdlib, "@@ERLANG_LIB_VER_stdlib@@"@},
8356      @{@@PACKAGE@@, "@@VERSION@@"@}]@}.
8357 @end example
8358 @end defmac
8360 In addition to the above macros, which test installed Erlang libraries, the
8361 following macros determine the paths to the directories into which newly built
8362 Erlang libraries are to be installed:
8364 @defmac AC_ERLANG_SUBST_INSTALL_LIB_DIR
8365 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
8366 @ovindex ERLANG_INSTALL_LIB_DIR
8368 Set the @code{ERLANG_INSTALL_LIB_DIR} output variable to the directory into
8369 which every built Erlang library should be installed in a separate
8370 subdirectory.
8371 If this variable is not set in the environment when @command{configure} runs,
8372 its default value is @code{$@{libdir@}/erlang/lib}.
8373 @end defmac
8375 @defmac AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR (@var{library}, @var{version})
8376 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
8377 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
8379 Set the @samp{ERLANG_INSTALL_LIB_DIR_@var{library}} output variable to the
8380 directory into which the built Erlang library @var{library} version
8381 @var{version} should be installed.  If this variable is not set in the
8382 environment when @command{configure} runs, its default value is
8383 @samp{$ERLANG_INSTALL_LIB_DIR/@var{library}-@var{version}}, the value of the
8384 @code{ERLANG_INSTALL_LIB_DIR} variable being set by the
8385 @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR} macro.
8386 @end defmac
8392 @c ========================================================= Writing Tests
8394 @node Writing Tests
8395 @chapter Writing Tests
8397 If the existing feature tests don't do something you need, you have to
8398 write new ones.  These macros are the building blocks.  They provide
8399 ways for other macros to check whether various kinds of features are
8400 available and report the results.
8402 This chapter contains some suggestions and some of the reasons why the
8403 existing tests are written the way they are.  You can also learn a lot
8404 about how to write Autoconf tests by looking at the existing ones.  If
8405 something goes wrong in one or more of the Autoconf tests, this
8406 information can help you understand the assumptions behind them, which
8407 might help you figure out how to best solve the problem.
8409 These macros check the output of the compiler system of the current
8410 language (@pxref{Language Choice}).  They do not cache the results of
8411 their tests for future use (@pxref{Caching Results}), because they don't
8412 know enough about the information they are checking for to generate a
8413 cache variable name.  They also do not print any messages, for the same
8414 reason.  The checks for particular kinds of features call these macros
8415 and do cache their results and print messages about what they're
8416 checking for.
8418 When you write a feature test that could be applicable to more than one
8419 software package, the best thing to do is encapsulate it in a new macro.
8420 @xref{Writing Autoconf Macros}, for how to do that.
8422 @menu
8423 * Language Choice::             Selecting which language to use for testing
8424 * Writing Test Programs::       Forging source files for compilers
8425 * Running the Preprocessor::    Detecting preprocessor symbols
8426 * Running the Compiler::        Detecting language or header features
8427 * Running the Linker::          Detecting library features
8428 * Runtime::                     Testing for runtime features
8429 * Systemology::                 A zoology of operating systems
8430 * Multiple Cases::              Tests for several possible values
8431 @end menu
8433 @node Language Choice
8434 @section Language Choice
8435 @cindex Language
8437 Autoconf-generated @command{configure} scripts check for the C compiler and
8438 its features by default.  Packages that use other programming languages
8439 (maybe more than one, e.g., C and C++) need to test features of the
8440 compilers for the respective languages.  The following macros determine
8441 which programming language is used in the subsequent tests in
8442 @file{configure.ac}.
8444 @anchor{AC_LANG}
8445 @defmac AC_LANG (@var{language})
8446 Do compilation tests using the compiler, preprocessor, and file
8447 extensions for the specified @var{language}.
8449 Supported languages are:
8451 @table @samp
8452 @item C
8453 Do compilation tests using @code{CC} and @code{CPP} and use extension
8454 @file{.c} for test programs.  Use compilation flags: @code{CPPFLAGS} with
8455 @code{CPP}, and both @code{CPPFLAGS} and @code{CFLAGS} with @code{CC}.
8457 @item C++
8458 Do compilation tests using @code{CXX} and @code{CXXCPP} and use
8459 extension @file{.C} for test programs.  Use compilation flags:
8460 @code{CPPFLAGS} with @code{CXXCPP}, and both @code{CPPFLAGS} and
8461 @code{CXXFLAGS} with @code{CXX}.
8463 @item Fortran 77
8464 Do compilation tests using @code{F77} and use extension @file{.f} for
8465 test programs.  Use compilation flags: @code{FFLAGS}.
8467 @item Fortran
8468 Do compilation tests using @code{FC} and use extension @file{.f} (or
8469 whatever has been set by @code{AC_FC_SRCEXT}) for test programs.  Use
8470 compilation flags: @code{FCFLAGS}.
8472 @item Erlang
8473 @ovindex ERLC
8474 @ovindex ERL
8475 @ovindex ERLCFLAGS
8476 Compile and execute tests using @code{ERLC} and @code{ERL} and use extension
8477 @file{.erl} for test Erlang modules.  Use compilation flags: @code{ERLCFLAGS}.
8479 @item Objective C
8480 Do compilation tests using @code{OBJC} and @code{OBJCPP} and use
8481 extension @file{.m} for test programs.  Use compilation flags:
8482 @code{CPPFLAGS} with @code{OBJCPP}, and both @code{CPPFLAGS} and
8483 @code{OBJCFLAGS} with @code{OBJC}.
8485 @item Objective C++
8486 Do compilation tests using @code{OBJCXX} and @code{OBJCXXCPP} and use
8487 extension @file{.mm} for test programs.  Use compilation flags:
8488 @code{CPPFLAGS} with @code{OBJCXXCPP}, and both @code{CPPFLAGS} and
8489 @code{OBJCXXFLAGS} with @code{OBJCXX}.
8490 @end table
8491 @end defmac
8493 @anchor{AC_LANG_PUSH}
8494 @defmac AC_LANG_PUSH (@var{language})
8495 @acindex{LANG_PUSH}
8496 Remember the current language (as set by @code{AC_LANG}) on a stack, and
8497 then select the @var{language}.  Use this macro and @code{AC_LANG_POP}
8498 in macros that need to temporarily switch to a particular language.
8499 @end defmac
8501 @defmac AC_LANG_POP (@ovar{language})
8502 @acindex{LANG_POP}
8503 Select the language that is saved on the top of the stack, as set by
8504 @code{AC_LANG_PUSH}, and remove it from the stack.
8506 If given, @var{language} specifies the language we just @emph{quit}.  It
8507 is a good idea to specify it when it's known (which should be the
8508 case@dots{}), since Autoconf detects inconsistencies.
8510 @example
8511 AC_LANG_PUSH([Fortran 77])
8512 # Perform some tests on Fortran 77.
8513 # @dots{}
8514 AC_LANG_POP([Fortran 77])
8515 @end example
8516 @end defmac
8518 @defmac AC_LANG_ASSERT (@var{language})
8519 @acindex{LANG_ASSERT} Check statically that the current language is
8520 @var{language}.  You should use this in your language specific macros
8521 to avoid that they be called with an inappropriate language.
8523 This macro runs only at @command{autoconf} time, and incurs no cost at
8524 @command{configure} time.  Sadly enough and because Autoconf is a two
8525 layer language @footnote{Because M4 is not aware of Sh code,
8526 especially conditionals, some optimizations that look nice statically
8527 may produce incorrect results at runtime.}, the macros
8528 @code{AC_LANG_PUSH} and @code{AC_LANG_POP} cannot be ``optimizing'',
8529 therefore as much as possible you ought to avoid using them to wrap
8530 your code, rather, require from the user to run the macro with a
8531 correct current language, and check it with @code{AC_LANG_ASSERT}.
8532 And anyway, that may help the user understand she is running a Fortran
8533 macro while expecting a result about her Fortran 77 compiler@enddots{}
8534 @end defmac
8537 @defmac AC_REQUIRE_CPP
8538 @acindex{REQUIRE_CPP}
8539 Ensure that whichever preprocessor would currently be used for tests has
8540 been found.  Calls @code{AC_REQUIRE} (@pxref{Prerequisite Macros}) with an
8541 argument of either @code{AC_PROG_CPP} or @code{AC_PROG_CXXCPP},
8542 depending on which language is current.
8543 @end defmac
8546 @node Writing Test Programs
8547 @section Writing Test Programs
8549 Autoconf tests follow a common scheme: feed some program with some
8550 input, and most of the time, feed a compiler with some source file.
8551 This section is dedicated to these source samples.
8553 @menu
8554 * Guidelines::                  General rules for writing test programs
8555 * Test Functions::              Avoiding pitfalls in test programs
8556 * Generating Sources::          Source program boilerplate
8557 @end menu
8559 @node Guidelines
8560 @subsection Guidelines for Test Programs
8562 The most important rule to follow when writing testing samples is:
8564 @center @emph{Look for realism.}
8566 This motto means that testing samples must be written with the same
8567 strictness as real programs are written.  In particular, you should
8568 avoid ``shortcuts'' and simplifications.
8570 Don't just play with the preprocessor if you want to prepare a
8571 compilation.  For instance, using @command{cpp} to check whether a header is
8572 functional might let your @command{configure} accept a header which
8573 causes some @emph{compiler} error.  Do not hesitate to check a header with
8574 other headers included before, especially required headers.
8576 Make sure the symbols you use are properly defined, i.e., refrain from
8577 simply declaring a function yourself instead of including the proper
8578 header.
8580 Test programs should not write to standard output.  They
8581 should exit with status 0 if the test succeeds, and with status 1
8582 otherwise, so that success
8583 can be distinguished easily from a core dump or other failure;
8584 segmentation violations and other failures produce a nonzero exit
8585 status.  Unless you arrange for @code{exit} to be declared, test
8586 programs should @code{return}, not @code{exit}, from @code{main},
8587 because on many systems @code{exit} is not declared by default.
8589 Test programs can use @code{#if} or @code{#ifdef} to check the values of
8590 preprocessor macros defined by tests that have already run.  For
8591 example, if you call @code{AC_HEADER_STDBOOL}, then later on in
8592 @file{configure.ac} you can have a test program that includes
8593 @file{stdbool.h} conditionally:
8595 @example
8596 @group
8597 #ifdef HAVE_STDBOOL_H
8598 # include <stdbool.h>
8599 #endif
8600 @end group
8601 @end example
8603 Both @code{#if HAVE_STDBOOL_H} and @code{#ifdef HAVE_STDBOOL_H} will
8604 work with any standard C compiler.  Some developers prefer @code{#if}
8605 because it is easier to read, while others prefer @code{#ifdef} because
8606 it avoids diagnostics with picky compilers like GCC with the
8607 @option{-Wundef} option.
8609 If a test program needs to use or create a data file, give it a name
8610 that starts with @file{conftest}, such as @file{conftest.data}.  The
8611 @command{configure} script cleans up by running @samp{rm -f -r conftest*}
8612 after running test programs and if the script is interrupted.
8614 @node Test Functions
8615 @subsection Test Functions
8617 These days it's safe to assume support for function prototypes
8618 (introduced in C89).
8620 Functions that test programs declare should also be conditionalized for
8621 C++, which requires @samp{extern "C"} prototypes.  Make sure to not
8622 include any header files containing clashing prototypes.
8624 @example
8625 #ifdef __cplusplus
8626 extern "C"
8627 #endif
8628 void *valloc (size_t);
8629 @end example
8631 If a test program calls a function with invalid parameters (just to see
8632 whether it exists), organize the program to ensure that it never invokes
8633 that function.  You can do this by calling it in another function that is
8634 never invoked.  You can't do it by putting it after a call to
8635 @code{exit}, because GCC version 2 knows that @code{exit}
8636 never returns
8637 and optimizes out any code that follows it in the same block.
8639 If you include any header files, be sure to call the functions
8640 relevant to them with the correct number of arguments, even if they are
8641 just 0, to avoid compilation errors due to prototypes.  GCC
8642 version 2
8643 has internal prototypes for several functions that it automatically
8644 inlines; for example, @code{memcpy}.  To avoid errors when checking for
8645 them, either pass them the correct number of arguments or redeclare them
8646 with a different return type (such as @code{char}).
8649 @node Generating Sources
8650 @subsection Generating Sources
8652 Autoconf provides a set of macros that can be used to generate test
8653 source files.  They are written to be language generic, i.e., they
8654 actually depend on the current language (@pxref{Language Choice}) to
8655 ``format'' the output properly.
8658 @defmac AC_LANG_CONFTEST (@var{source})
8659 @acindex{LANG_CONFTEST}
8660 Save the @var{source} text in the current test source file:
8661 @file{conftest.@var{extension}} where the @var{extension} depends on the
8662 current language.  As of Autoconf 2.63b, the source file also contains
8663 the results of all of the @code{AC_DEFINE} performed so far.
8665 Note that the @var{source} is evaluated exactly once, like regular
8666 Autoconf macro arguments, and therefore (i) you may pass a macro
8667 invocation, (ii) if not, be sure to double quote if needed.
8668 @end defmac
8670 @defmac AC_LANG_SOURCE (@var{source})
8671 @acindex{LANG_SOURCE}
8672 Expands into the @var{source}, with the definition of
8673 all the @code{AC_DEFINE} performed so far.
8674 @end defmac
8676 For instance executing (observe the double quotation!):
8678 @example
8679 @c If you change this example, adjust tests/compile.at:AC_LANG_SOURCE example.
8680 AC_INIT([Hello], [1.0], [bug-hello@@example.org], [],
8681         [http://www.example.org/])
8682 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
8683   [Greetings string.])
8684 AC_LANG([C])
8685 AC_LANG_CONFTEST(
8686    [AC_LANG_SOURCE([[const char hw[] = "Hello, World\n";]])])
8687 gcc -E -dD -o - conftest.c
8688 @end example
8690 @noindent
8691 on a system with @command{gcc} installed, results in:
8693 @example
8694 @c If you change this example, adjust tests/compile.at:AC_LANG_SOURCE example.
8695 @dots{}
8696 # 1 "conftest.c"
8698 #define PACKAGE_NAME "Hello"
8699 #define PACKAGE_TARNAME "hello"
8700 #define PACKAGE_VERSION "1.0"
8701 #define PACKAGE_STRING "Hello 1.0"
8702 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
8703 #define PACKAGE_URL "http://www.example.org/"
8704 #define HELLO_WORLD "Hello, World\n"
8706 const char hw[] = "Hello, World\n";
8707 @end example
8709 When the test language is Fortran or Erlang, the @code{AC_DEFINE} definitions
8710 are not automatically translated into constants in the source code by this
8711 macro.
8713 @defmac AC_LANG_PROGRAM (@var{prologue}, @var{body})
8714 @acindex{LANG_PROGRAM}
8715 Expands into a source file which consists of the @var{prologue}, and
8716 then @var{body} as body of the main function (e.g., @code{main} in
8717 C).  Since it uses @code{AC_LANG_SOURCE}, the features of the latter are
8718 available.
8719 @end defmac
8721 For instance:
8723 @example
8724 @c If you change this example, adjust tests/compile.at:AC_LANG_PROGRAM example.
8725 AC_INIT([Hello], [1.0], [bug-hello@@example.org], [],
8726         [http://www.example.org/])
8727 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
8728   [Greetings string.])
8729 AC_LANG_CONFTEST(
8730 [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
8731                  [[fputs (hw, stdout);]])])
8732 gcc -E -dD -o - conftest.c
8733 @end example
8735 @noindent
8736 on a system with @command{gcc} installed, results in:
8738 @example
8739 @c If you change this example, adjust tests/compile.at:AC_LANG_PROGRAM example.
8740 @dots{}
8741 # 1 "conftest.c"
8743 #define PACKAGE_NAME "Hello"
8744 #define PACKAGE_TARNAME "hello"
8745 #define PACKAGE_VERSION "1.0"
8746 #define PACKAGE_STRING "Hello 1.0"
8747 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
8748 #define PACKAGE_URL "http://www.example.org/"
8749 #define HELLO_WORLD "Hello, World\n"
8751 const char hw[] = "Hello, World\n";
8753 main ()
8755 fputs (hw, stdout);
8756   ;
8757   return 0;
8759 @end example
8761 In Erlang tests, the created source file is that of an Erlang module called
8762 @code{conftest} (@file{conftest.erl}).  This module defines and exports
8763 at least
8764 one @code{start/0} function, which is called to perform the test.  The
8765 @var{prologue} is optional code that is inserted between the module header and
8766 the @code{start/0} function definition.  @var{body} is the body of the
8767 @code{start/0} function without the final period (@pxref{Runtime}, about
8768 constraints on this function's behavior).
8770 For instance:
8772 @example
8773 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
8774 AC_LANG(Erlang)
8775 AC_LANG_CONFTEST(
8776 [AC_LANG_PROGRAM([[-define(HELLO_WORLD, "Hello, world!").]],
8777                  [[io:format("~s~n", [?HELLO_WORLD])]])])
8778 cat conftest.erl
8779 @end example
8781 @noindent
8782 results in:
8784 @example
8785 -module(conftest).
8786 -export([start/0]).
8787 -define(HELLO_WORLD, "Hello, world!").
8788 start() ->
8789 io:format("~s~n", [?HELLO_WORLD])
8791 @end example
8793 @defmac AC_LANG_CALL (@var{prologue}, @var{function})
8794 @acindex{LANG_CALL}
8795 Expands into a source file which consists of the @var{prologue}, and
8796 then a call to the @var{function} as body of the main function (e.g.,
8797 @code{main} in C).  Since it uses @code{AC_LANG_PROGRAM}, the feature
8798 of the latter are available.
8800 This function will probably be replaced in the future by a version
8801 which would enable specifying the arguments.  The use of this macro is
8802 not encouraged, as it violates strongly the typing system.
8804 This macro cannot be used for Erlang tests.
8805 @end defmac
8807 @defmac AC_LANG_FUNC_LINK_TRY (@var{function})
8808 @acindex{LANG_FUNC_LINK_TRY}
8809 Expands into a source file which uses the @var{function} in the body of
8810 the main function (e.g., @code{main} in C).  Since it uses
8811 @code{AC_LANG_PROGRAM}, the features of the latter are available.
8813 As @code{AC_LANG_CALL}, this macro is documented only for completeness.
8814 It is considered to be severely broken, and in the future will be
8815 removed in favor of actual function calls (with properly typed
8816 arguments).
8818 This macro cannot be used for Erlang tests.
8819 @end defmac
8821 @node Running the Preprocessor
8822 @section Running the Preprocessor
8824 Sometimes one might need to run the preprocessor on some source file.
8825 @emph{Usually it is a bad idea}, as you typically need to @emph{compile}
8826 your project, not merely run the preprocessor on it; therefore you
8827 certainly want to run the compiler, not the preprocessor.  Resist the
8828 temptation of following the easiest path.
8830 Nevertheless, if you need to run the preprocessor, then use
8831 @code{AC_PREPROC_IFELSE}.
8833 The macros described in this section cannot be used for tests in Erlang or
8834 Fortran, since those languages require no preprocessor.
8836 @anchor{AC_PREPROC_IFELSE}
8837 @defmac AC_PREPROC_IFELSE (@var{input}, @ovar{action-if-true}, @
8838   @ovar{action-if-false})
8839 @acindex{PREPROC_IFELSE}
8840 Run the preprocessor of the current language (@pxref{Language Choice})
8841 on the @var{input}, run the shell commands @var{action-if-true} on
8842 success, @var{action-if-false} otherwise.  The @var{input} can be made
8843 by @code{AC_LANG_PROGRAM} and friends.
8845 This macro uses @code{CPPFLAGS}, but not @code{CFLAGS}, because
8846 @option{-g}, @option{-O}, etc.@: are not valid options to many C
8847 preprocessors.
8849 It is customary to report unexpected failures with
8850 @code{AC_MSG_FAILURE}.
8851 @end defmac
8853 For instance:
8855 @example
8856 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
8857 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
8858   [Greetings string.])
8859 AC_PREPROC_IFELSE(
8860    [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
8861                     [[fputs (hw, stdout);]])],
8862    [AC_MSG_RESULT([OK])],
8863    [AC_MSG_FAILURE([unexpected preprocessor failure])])
8864 @end example
8866 @noindent
8867 results in:
8869 @example
8870 checking for gcc... gcc
8871 checking for C compiler default output file name... a.out
8872 checking whether the C compiler works... yes
8873 checking whether we are cross compiling... no
8874 checking for suffix of executables...
8875 checking for suffix of object files... o
8876 checking whether we are using the GNU C compiler... yes
8877 checking whether gcc accepts -g... yes
8878 checking for gcc option to accept ISO C89... none needed
8879 checking how to run the C preprocessor... gcc -E
8881 @end example
8883 @sp 1
8885 The macro @code{AC_TRY_CPP} (@pxref{Obsolete Macros}) used to play the
8886 role of @code{AC_PREPROC_IFELSE}, but double quotes its argument, making
8887 it impossible to use it to elaborate sources.  You are encouraged to
8888 get rid of your old use of the macro @code{AC_TRY_CPP} in favor of
8889 @code{AC_PREPROC_IFELSE}, but, in the first place, are you sure you need
8890 to run the @emph{preprocessor} and not the compiler?
8892 @anchor{AC_EGREP_HEADER}
8893 @defmac AC_EGREP_HEADER (@var{pattern}, @var{header-file}, @
8894   @var{action-if-found}, @ovar{action-if-not-found})
8895 @acindex{EGREP_HEADER}
8896 If the output of running the preprocessor on the system header file
8897 @var{header-file} matches the extended regular expression
8898 @var{pattern}, execute shell commands @var{action-if-found}, otherwise
8899 execute @var{action-if-not-found}.
8900 @end defmac
8902 @anchor{AC_EGREP_CPP}
8903 @defmac AC_EGREP_CPP (@var{pattern}, @var{program}, @
8904   @ovar{action-if-found}, @ovar{action-if-not-found})
8905 @acindex{EGREP_CPP}
8906 @var{program} is the text of a C or C++ program, on which shell
8907 variable, back quote, and backslash substitutions are performed.  If the
8908 output of running the preprocessor on @var{program} matches the
8909 extended regular expression @var{pattern}, execute shell commands
8910 @var{action-if-found}, otherwise execute @var{action-if-not-found}.
8911 @end defmac
8915 @node Running the Compiler
8916 @section Running the Compiler
8918 To check for a syntax feature of the current language's (@pxref{Language
8919 Choice}) compiler, such as whether it recognizes a certain keyword, or
8920 simply to try some library feature, use @code{AC_COMPILE_IFELSE} to try
8921 to compile a small program that uses that feature.
8923 @defmac AC_COMPILE_IFELSE (@var{input}, @ovar{action-if-true}, @
8924   @ovar{action-if-false})
8925 @acindex{COMPILE_IFELSE}
8926 Run the compiler and compilation flags of the current language
8927 (@pxref{Language Choice}) on the @var{input}, run the shell commands
8928 @var{action-if-true} on success, @var{action-if-false} otherwise.  The
8929 @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
8931 It is customary to report unexpected failures with
8932 @code{AC_MSG_FAILURE}.  This macro does not try to link; use
8933 @code{AC_LINK_IFELSE} if you need to do that (@pxref{Running the
8934 Linker}).  If needed, @var{action-if-true} can further access the
8935 just-compiled object file @file{conftest.$OBJEXT}.
8937 This macro uses @code{AC_REQUIRE} for the compiler associated with the
8938 current language, which means that if the compiler has not yet been
8939 determined, the compiler determination will be made prior to the body of
8940 the outermust @code{AC_DEFUN} macro that triggered this macro to
8941 expand (@pxref{Expanded Before Required}).
8942 @end defmac
8944 @ovindex ERL
8945 For tests in Erlang, the @var{input} must be the source code of a module named
8946 @code{conftest}.  @code{AC_COMPILE_IFELSE} generates a @file{conftest.beam}
8947 file that can be interpreted by the Erlang virtual machine (@code{ERL}).  It is
8948 recommended to use @code{AC_LANG_PROGRAM} to specify the test program,
8949 to ensure that the Erlang module has the right name.
8951 @node Running the Linker
8952 @section Running the Linker
8954 To check for a library, a function, or a global variable, Autoconf
8955 @command{configure} scripts try to compile and link a small program that
8956 uses it.  This is unlike Metaconfig, which by default uses @code{nm} or
8957 @code{ar} on the C library to try to figure out which functions are
8958 available.  Trying to link with the function is usually a more reliable
8959 approach because it avoids dealing with the variations in the options
8960 and output formats of @code{nm} and @code{ar} and in the location of the
8961 standard libraries.  It also allows configuring for cross-compilation or
8962 checking a function's runtime behavior if needed.  On the other hand,
8963 it can be slower than scanning the libraries once, but accuracy is more
8964 important than speed.
8966 @code{AC_LINK_IFELSE} is used to compile test programs to test for
8967 functions and global variables.  It is also used by @code{AC_CHECK_LIB}
8968 to check for libraries (@pxref{Libraries}), by adding the library being
8969 checked for to @code{LIBS} temporarily and trying to link a small
8970 program.
8972 @anchor{AC_LINK_IFELSE}
8973 @defmac AC_LINK_IFELSE (@var{input}, @ovar{action-if-true}, @
8974   @ovar{action-if-false})
8975 @acindex{LINK_IFELSE}
8976 Run the compiler (and compilation flags) and the linker of the current
8977 language (@pxref{Language Choice}) on the @var{input}, run the shell
8978 commands @var{action-if-true} on success, @var{action-if-false}
8979 otherwise.  The @var{input} can be made by @code{AC_LANG_PROGRAM} and
8980 friends.  If needed, @var{action-if-true} can further access the
8981 just-linked program file @file{conftest$EXEEXT}.
8983 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
8984 current compilation flags.
8986 It is customary to report unexpected failures with
8987 @code{AC_MSG_FAILURE}.  This macro does not try to execute the program;
8988 use @code{AC_RUN_IFELSE} if you need to do that (@pxref{Runtime}).
8989 @end defmac
8991 The @code{AC_LINK_IFELSE} macro cannot be used for Erlang tests, since Erlang
8992 programs are interpreted and do not require linking.
8996 @node Runtime
8997 @section Checking Runtime Behavior
8999 Sometimes you need to find out how a system performs at runtime, such
9000 as whether a given function has a certain capability or bug.  If you
9001 can, make such checks when your program runs instead of when it is
9002 configured.  You can check for things like the machine's endianness when
9003 your program initializes itself.
9005 If you really need to test for a runtime behavior while configuring,
9006 you can write a test program to determine the result, and compile and
9007 run it using @code{AC_RUN_IFELSE}.  Avoid running test programs if
9008 possible, because this prevents people from configuring your package for
9009 cross-compiling.
9011 @anchor{AC_RUN_IFELSE}
9012 @defmac AC_RUN_IFELSE (@var{input}, @ovar{action-if-true}, @
9013   @ovar{action-if-false}, @ovar{action-if-cross-compiling})
9014 @acindex{RUN_IFELSE}
9015 If @var{program} compiles and links successfully and returns an exit
9016 status of 0 when executed, run shell commands @var{action-if-true}.
9017 Otherwise, run shell commands @var{action-if-false}.
9019 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
9020 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
9021 compilation flags of the current language (@pxref{Language Choice}).
9022 Additionally, @var{action-if-true} can run @command{./conftest$EXEEXT}
9023 for further testing.
9025 If the compiler being used does not produce executables that run on the
9026 system where @command{configure} is being run, then the test program is
9027 not run.  If the optional shell commands @var{action-if-cross-compiling}
9028 are given, they are run instead.  Otherwise, @command{configure} prints
9029 an error message and exits.
9031 In the @var{action-if-false} section, the failing exit status is
9032 available in the shell variable @samp{$?}.  This exit status might be
9033 that of a failed compilation, or it might be that of a failed program
9034 execution.
9036 It is customary to report unexpected failures with
9037 @code{AC_MSG_FAILURE}.
9038 @end defmac
9040 Try to provide a pessimistic default value to use when cross-compiling
9041 makes runtime tests impossible.  You do this by passing the optional
9042 last argument to @code{AC_RUN_IFELSE}.  @command{autoconf} prints a
9043 warning message when creating @command{configure} each time it
9044 encounters a call to @code{AC_RUN_IFELSE} with no
9045 @var{action-if-cross-compiling} argument given.  You may ignore the
9046 warning, though users cannot configure your package for
9047 cross-compiling.  A few of the macros distributed with Autoconf produce
9048 this warning message.
9050 To configure for cross-compiling you can also choose a value for those
9051 parameters based on the canonical system name (@pxref{Manual
9052 Configuration}).  Alternatively, set up a test results cache file with
9053 the correct values for the host system (@pxref{Caching Results}).
9055 @ovindex cross_compiling
9056 To provide a default for calls of @code{AC_RUN_IFELSE} that are embedded
9057 in other macros, including a few of the ones that come with Autoconf,
9058 you can test whether the shell variable @code{cross_compiling} is set to
9059 @samp{yes}, and then use an alternate method to get the results instead
9060 of calling the macros.
9062 It is also permissible to temporarily assign to @code{cross_compiling}
9063 in order to force tests to behave as though they are in a
9064 cross-compilation environment, particularly since this provides a way to
9065 test your @var{action-if-cross-compiling} even when you are not using a
9066 cross-compiler.
9068 @example
9069 # We temporarily set cross-compile mode to force AC_COMPUTE_INT
9070 # to use the slow link-only method
9071 save_cross_compiling=$cross_compiling
9072 cross_compiling=yes
9073 AC_COMPUTE_INT([@dots{}])
9074 cross_compiling=$save_cross_compiling
9075 @end example
9077 A C or C++ runtime test should be portable.
9078 @xref{Portable C and C++}.
9080 Erlang tests must exit themselves the Erlang VM by calling the @code{halt/1}
9081 function: the given status code is used to determine the success of the test
9082 (status is @code{0}) or its failure (status is different than @code{0}), as
9083 explained above.  It must be noted that data output through the standard output
9084 (e.g., using @code{io:format/2}) may be truncated when halting the VM.
9085 Therefore, if a test must output configuration information, it is recommended
9086 to create and to output data into the temporary file named @file{conftest.out},
9087 using the functions of module @code{file}.  The @code{conftest.out} file is
9088 automatically deleted by the @code{AC_RUN_IFELSE} macro.  For instance, a
9089 simplified implementation of Autoconf's @code{AC_ERLANG_SUBST_LIB_DIR}
9090 macro is:
9092 @example
9093 AC_INIT([LibdirTest], [1.0], [bug-libdirtest@@example.org])
9094 AC_ERLANG_NEED_ERL
9095 AC_LANG(Erlang)
9096 AC_RUN_IFELSE(
9097   [AC_LANG_PROGRAM([], [dnl
9098     file:write_file("conftest.out", code:lib_dir()),
9099     halt(0)])],
9100   [echo "code:lib_dir() returned: `cat conftest.out`"],
9101   [AC_MSG_FAILURE([test Erlang program execution failed])])
9102 @end example
9105 @node Systemology
9106 @section Systemology
9107 @cindex Systemology
9109 This section aims at presenting some systems and pointers to
9110 documentation.  It may help you addressing particular problems reported
9111 by users.
9113 @uref{http://@/www.opengroup.org/@/susv3, Posix-conforming systems} are
9114 derived from the @uref{http://@/www.bell-labs.com/@/history/@/unix/, Unix
9115 operating system}.
9117 The @uref{http://@/bhami.com/@/rosetta.html, Rosetta Stone for Unix}
9118 contains a table correlating the features of various Posix-conforming
9119 systems.  @uref{http://@/www.levenez.com/@/unix/, Unix History} is a
9120 simplified diagram of how many Unix systems were derived from each
9121 other.
9123 @uref{http://@/heirloom.sourceforge.net/, The Heirloom Project}
9124 provides some variants of traditional implementations of Unix utilities.
9126 @table @asis
9127 @item Darwin
9128 @cindex Darwin
9129 Darwin is also known as Mac OS X@.  Beware that the file system @emph{can} be
9130 case-preserving, but case insensitive.  This can cause nasty problems,
9131 since for instance the installation attempt for a package having an
9132 @file{INSTALL} file can result in @samp{make install} report that
9133 nothing was to be done!
9135 That's all dependent on whether the file system is a UFS (case
9136 sensitive) or HFS+ (case preserving).  By default Apple wants you to
9137 install the OS on HFS+.  Unfortunately, there are some pieces of
9138 software which really need to be built on UFS@.  We may want to rebuild
9139 Darwin to have both UFS and HFS+ available (and put the /local/build
9140 tree on the UFS).
9142 @item QNX 4.25
9143 @cindex QNX 4.25
9144 @c FIXME: Please, if you feel like writing something more precise,
9145 @c it'd be great.  In particular, I can't understand the difference with
9146 @c QNX Neutrino.
9147 QNX is a realtime operating system running on Intel architecture
9148 meant to be scalable from the small embedded systems to the hundred
9149 processor super-computer.  It claims to be Posix certified.  More
9150 information is available on the
9151 @uref{http://@/www.qnx.com/, QNX home page}.
9153 @item Tru64
9154 @cindex Tru64
9155 @uref{http://@/h30097.www3.hp.com/@/docs/,
9156 Documentation of several versions of Tru64} is available in different
9157 formats.
9159 @item Unix version 7
9160 @cindex Unix version 7
9161 @cindex V7
9162 Officially this was called the ``Seventh Edition'' of ``the UNIX
9163 time-sharing system'' but we use the more-common name ``Unix version 7''.
9164 Documentation is available in the
9165 @uref{http://@/plan9.bell-labs.com/@/7thEdMan/, Unix Seventh Edition Manual}.
9166 Previous versions of Unix are called ``Unix version 6'', etc., but
9167 they were not as widely used.
9168 @end table
9171 @node Multiple Cases
9172 @section Multiple Cases
9174 Some operations are accomplished in several possible ways, depending on
9175 the OS variant.  Checking for them essentially requires a ``case
9176 statement''.  Autoconf does not directly provide one; however, it is
9177 easy to simulate by using a shell variable to keep track of whether a
9178 way to perform the operation has been found yet.
9180 Here is an example that uses the shell variable @code{fstype} to keep
9181 track of whether the remaining cases need to be checked.  Note that
9182 since the value of @code{fstype} is under our control, we don't have to
9183 use the longer @samp{test "x$fstype" = xno}.
9185 @example
9186 @group
9187 AC_MSG_CHECKING([how to get file system type])
9188 fstype=no
9189 # The order of these tests is important.
9190 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statvfs.h>
9191 #include <sys/fstyp.h>]])],
9192                   [AC_DEFINE([FSTYPE_STATVFS], [1],
9193                      [Define if statvfs exists.])
9194                    fstype=SVR4])
9195 if test $fstype = no; then
9196   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
9197 #include <sys/fstyp.h>]])],
9198                   [AC_DEFINE([FSTYPE_USG_STATFS], [1],
9199                      [Define if USG statfs.])
9200                    fstype=SVR3])
9202 if test $fstype = no; then
9203   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
9204 #include <sys/vmount.h>]])]),
9205                   [AC_DEFINE([FSTYPE_AIX_STATFS], [1],
9206                      [Define if AIX statfs.])
9207                    fstype=AIX])
9209 # (more cases omitted here)
9210 AC_MSG_RESULT([$fstype])
9211 @end group
9212 @end example
9214 @c ====================================================== Results of Tests.
9216 @node Results
9217 @chapter Results of Tests
9219 Once @command{configure} has determined whether a feature exists, what can
9220 it do to record that information?  There are four sorts of things it can
9221 do: define a C preprocessor symbol, set a variable in the output files,
9222 save the result in a cache file for future @command{configure} runs, and
9223 print a message letting the user know the result of the test.
9225 @menu
9226 * Defining Symbols::            Defining C preprocessor symbols
9227 * Setting Output Variables::    Replacing variables in output files
9228 * Special Chars in Variables::  Characters to beware of in variables
9229 * Caching Results::             Speeding up subsequent @command{configure} runs
9230 * Printing Messages::           Notifying @command{configure} users
9231 @end menu
9233 @node Defining Symbols
9234 @section Defining C Preprocessor Symbols
9236 A common action to take in response to a feature test is to define a C
9237 preprocessor symbol indicating the results of the test.  That is done by
9238 calling @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}.
9240 By default, @code{AC_OUTPUT} places the symbols defined by these macros
9241 into the output variable @code{DEFS}, which contains an option
9242 @option{-D@var{symbol}=@var{value}} for each symbol defined.  Unlike in
9243 Autoconf version 1, there is no variable @code{DEFS} defined while
9244 @command{configure} is running.  To check whether Autoconf macros have
9245 already defined a certain C preprocessor symbol, test the value of the
9246 appropriate cache variable, as in this example:
9248 @example
9249 AC_CHECK_FUNC([vprintf], [AC_DEFINE([HAVE_VPRINTF], [1],
9250                           [Define if vprintf exists.])])
9251 if test "x$ac_cv_func_vprintf" != xyes; then
9252   AC_CHECK_FUNC([_doprnt], [AC_DEFINE([HAVE_DOPRNT], [1],
9253                             [Define if _doprnt exists.])])
9255 @end example
9257 If @code{AC_CONFIG_HEADERS} has been called, then instead of creating
9258 @code{DEFS}, @code{AC_OUTPUT} creates a header file by substituting the
9259 correct values into @code{#define} statements in a template file.
9260 @xref{Configuration Headers}, for more information about this kind of
9261 output.
9263 @defmac AC_DEFINE (@var{variable}, @var{value}, @ovar{description})
9264 @defmacx AC_DEFINE (@var{variable})
9265 @cvindex @var{variable}
9266 @acindex{DEFINE}
9267 Define @var{variable} to @var{value} (verbatim), by defining a C
9268 preprocessor macro for @var{variable}.  @var{variable} should be a C
9269 identifier, optionally suffixed by a parenthesized argument list to
9270 define a C preprocessor macro with arguments.  The macro argument list,
9271 if present, should be a comma-separated list of C identifiers, possibly
9272 terminated by an ellipsis @samp{...} if C99 syntax is employed.
9273 @var{variable} should not contain comments, white space, trigraphs,
9274 backslash-newlines, universal character names, or non-ASCII
9275 characters.
9277 @var{value} may contain backslash-escaped newlines, which will be
9278 preserved if you use @code{AC_CONFIG_HEADERS} but flattened if passed
9279 via @code{@@DEFS@@} (with no effect on the compilation, since the
9280 preprocessor sees only one line in the first place).  @var{value} should
9281 not contain raw newlines.  If you are not using
9282 @code{AC_CONFIG_HEADERS}, @var{value} should not contain any @samp{#}
9283 characters, as @command{make} tends to eat them.  To use a shell
9284 variable, use @code{AC_DEFINE_UNQUOTED} instead.
9286 @var{description} is only useful if you are using
9287 @code{AC_CONFIG_HEADERS}.  In this case, @var{description} is put into
9288 the generated @file{config.h.in} as the comment before the macro define.
9289 The following example defines the C preprocessor variable
9290 @code{EQUATION} to be the string constant @samp{"$a > $b"}:
9292 @example
9293 AC_DEFINE([EQUATION], ["$a > $b"],
9294   [Equation string.])
9295 @end example
9297 If neither @var{value} nor @var{description} are given, then
9298 @var{value} defaults to 1 instead of to the empty string.  This is for
9299 backwards compatibility with older versions of Autoconf, but this usage
9300 is obsolescent and may be withdrawn in future versions of Autoconf.
9302 If the @var{variable} is a literal string, it is passed to
9303 @code{m4_pattern_allow} (@pxref{Forbidden Patterns}).
9305 If multiple @code{AC_DEFINE} statements are executed for the same
9306 @var{variable} name (not counting any parenthesized argument list),
9307 the last one wins.
9308 @end defmac
9310 @defmac AC_DEFINE_UNQUOTED (@var{variable}, @var{value}, @ovar{description})
9311 @defmacx AC_DEFINE_UNQUOTED (@var{variable})
9312 @acindex{DEFINE_UNQUOTED}
9313 @cvindex @var{variable}
9314 Like @code{AC_DEFINE}, but three shell expansions are
9315 performed---once---on @var{variable} and @var{value}: variable expansion
9316 (@samp{$}), command substitution (@samp{`}), and backslash escaping
9317 (@samp{\}), as if in an unquoted here-document.  Single and double quote
9318 characters in the value have no
9319 special meaning.  Use this macro instead of @code{AC_DEFINE} when
9320 @var{variable} or @var{value} is a shell variable.  Examples:
9322 @example
9323 AC_DEFINE_UNQUOTED([config_machfile], ["$machfile"],
9324   [Configuration machine file.])
9325 AC_DEFINE_UNQUOTED([GETGROUPS_T], [$ac_cv_type_getgroups],
9326   [getgroups return type.])
9327 AC_DEFINE_UNQUOTED([$ac_tr_hdr], [1],
9328   [Translated header name.])
9329 @end example
9330 @end defmac
9332 Due to a syntactical bizarreness of the Bourne shell, do not use
9333 semicolons to separate @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}
9334 calls from other macro calls or shell code; that can cause syntax errors
9335 in the resulting @command{configure} script.  Use either blanks or
9336 newlines.  That is, do this:
9338 @example
9339 AC_CHECK_HEADER([elf.h],
9340   [AC_DEFINE([SVR4], [1], [System V Release 4]) LIBS="-lelf $LIBS"])
9341 @end example
9343 @noindent
9344 or this:
9346 @example
9347 AC_CHECK_HEADER([elf.h],
9348   [AC_DEFINE([SVR4], [1], [System V Release 4])
9349    LIBS="-lelf $LIBS"])
9350 @end example
9352 @noindent
9353 instead of this:
9355 @example
9356 AC_CHECK_HEADER([elf.h],
9357   [AC_DEFINE([SVR4], [1], [System V Release 4]); LIBS="-lelf $LIBS"])
9358 @end example
9360 @node Setting Output Variables
9361 @section Setting Output Variables
9362 @cindex Output variables
9364 Another way to record the results of tests is to set @dfn{output
9365 variables}, which are shell variables whose values are substituted into
9366 files that @command{configure} outputs.  The two macros below create new
9367 output variables.  @xref{Preset Output Variables}, for a list of output
9368 variables that are always available.
9370 @defmac AC_SUBST (@var{variable}, @ovar{value})
9371 @acindex{SUBST}
9372 Create an output variable from a shell variable.  Make @code{AC_OUTPUT}
9373 substitute the variable @var{variable} into output files (typically one
9374 or more makefiles).  This means that @code{AC_OUTPUT}
9375 replaces instances of @samp{@@@var{variable}@@} in input files with the
9376 value that the shell variable @var{variable} has when @code{AC_OUTPUT}
9377 is called.  The value can contain any non-@code{NUL} character, including
9378 newline.  If you are using Automake 1.11 or newer, for newlines in values
9379 you might want to consider using @code{AM_SUBST_NOTMAKE} to prevent
9380 @command{automake} from adding a line @code{@var{variable} =
9381 @@@var{variable}@@} to the @file{Makefile.in} files (@pxref{Optional, ,
9382 Automake, automake, Other things Automake recognizes}).
9384 Variable occurrences should not overlap: e.g., an input file should
9385 not contain @samp{@@@var{var1}@@@var{var2}@@} if @var{var1} and @var{var2}
9386 are variable names.
9387 The substituted value is not rescanned for more output variables;
9388 occurrences of @samp{@@@var{variable}@@} in the value are inserted
9389 literally into the output file.  (The algorithm uses the special marker
9390 @code{|#_!!_#|} internally, so neither the substituted value nor the
9391 output file may contain @code{|#_!!_#|}.)
9393 If @var{value} is given, in addition assign it to @var{variable}.
9395 The string @var{variable} is passed to @code{m4_pattern_allow}
9396 (@pxref{Forbidden Patterns}).
9397 @end defmac
9399 @defmac AC_SUBST_FILE (@var{variable})
9400 @acindex{SUBST_FILE}
9401 Another way to create an output variable from a shell variable.  Make
9402 @code{AC_OUTPUT} insert (without substitutions) the contents of the file
9403 named by shell variable @var{variable} into output files.  This means
9404 that @code{AC_OUTPUT} replaces instances of
9405 @samp{@@@var{variable}@@} in output files (such as @file{Makefile.in})
9406 with the contents of the file that the shell variable @var{variable}
9407 names when @code{AC_OUTPUT} is called.  Set the variable to
9408 @file{/dev/null} for cases that do not have a file to insert.
9409 This substitution occurs only when the @samp{@@@var{variable}@@} is on a
9410 line by itself, optionally surrounded by spaces and tabs.  The
9411 substitution replaces the whole line, including the spaces, tabs, and
9412 the terminating newline.
9414 This macro is useful for inserting makefile fragments containing
9415 special dependencies or other @command{make} directives for particular host
9416 or target types into makefiles.  For example, @file{configure.ac}
9417 could contain:
9419 @example
9420 AC_SUBST_FILE([host_frag])
9421 host_frag=$srcdir/conf/sun4.mh
9422 @end example
9424 @noindent
9425 and then a @file{Makefile.in} could contain:
9427 @example
9428 @@host_frag@@
9429 @end example
9431 The string @var{variable} is passed to @code{m4_pattern_allow}
9432 (@pxref{Forbidden Patterns}).
9433 @end defmac
9435 @cindex Precious Variable
9436 @cindex Variable, Precious
9437 Running @command{configure} in varying environments can be extremely
9438 dangerous.  If for instance the user runs @samp{CC=bizarre-cc
9439 ./configure}, then the cache, @file{config.h}, and many other output
9440 files depend upon @command{bizarre-cc} being the C compiler.  If
9441 for some reason the user runs @command{./configure} again, or if it is
9442 run via @samp{./config.status --recheck}, (@xref{Automatic Remaking},
9443 and @pxref{config.status Invocation}), then the configuration can be
9444 inconsistent, composed of results depending upon two different
9445 compilers.
9447 Environment variables that affect this situation, such as @samp{CC}
9448 above, are called @dfn{precious variables}, and can be declared as such
9449 by @code{AC_ARG_VAR}.
9451 @defmac AC_ARG_VAR (@var{variable}, @var{description})
9452 @acindex{ARG_VAR}
9453 Declare @var{variable} is a precious variable, and include its
9454 @var{description} in the variable section of @samp{./configure --help}.
9456 Being precious means that
9457 @itemize @minus
9458 @item
9459 @var{variable} is substituted via @code{AC_SUBST}.
9461 @item
9462 The value of @var{variable} when @command{configure} was launched is
9463 saved in the cache, including if it was not specified on the command
9464 line but via the environment.  Indeed, while @command{configure} can
9465 notice the definition of @code{CC} in @samp{./configure CC=bizarre-cc},
9466 it is impossible to notice it in @samp{CC=bizarre-cc ./configure},
9467 which, unfortunately, is what most users do.
9469 We emphasize that it is the @emph{initial} value of @var{variable} which
9470 is saved, not that found during the execution of @command{configure}.
9471 Indeed, specifying @samp{./configure FOO=foo} and letting
9472 @samp{./configure} guess that @code{FOO} is @code{foo} can be two
9473 different things.
9475 @item
9476 @var{variable} is checked for consistency between two
9477 @command{configure} runs.  For instance:
9479 @example
9480 $ @kbd{./configure --silent --config-cache}
9481 $ @kbd{CC=cc ./configure --silent --config-cache}
9482 configure: error: `CC' was not set in the previous run
9483 configure: error: changes in the environment can compromise \
9484 the build
9485 configure: error: run `make distclean' and/or \
9486 `rm config.cache' and start over
9487 @end example
9489 @noindent
9490 and similarly if the variable is unset, or if its content is changed.
9491 If the content has white space changes only, then the error is degraded
9492 to a warning only, but the old value is reused.
9494 @item
9495 @var{variable} is kept during automatic reconfiguration
9496 (@pxref{config.status Invocation}) as if it had been passed as a command
9497 line argument, including when no cache is used:
9499 @example
9500 $ @kbd{CC=/usr/bin/cc ./configure var=raboof --silent}
9501 $ @kbd{./config.status --recheck}
9502 running CONFIG_SHELL=/bin/sh /bin/sh ./configure var=raboof \
9503   CC=/usr/bin/cc  --no-create --no-recursion
9504 @end example
9505 @end itemize
9506 @end defmac
9508 @node Special Chars in Variables
9509 @section Special Characters in Output Variables
9510 @cindex Output variables, special characters in
9512 Many output variables are intended to be evaluated both by
9513 @command{make} and by the shell.  Some characters are expanded
9514 differently in these two contexts, so to avoid confusion these
9515 variables' values should not contain any of the following characters:
9517 @example
9518 " # $ & ' ( ) * ; < > ? [ \ ^ ` |
9519 @end example
9521 Also, these variables' values should neither contain newlines, nor start
9522 with @samp{~}, nor contain white space or @samp{:} immediately followed
9523 by @samp{~}.  The values can contain nonempty sequences of white space
9524 characters like tabs and spaces, but each such sequence might
9525 arbitrarily be replaced by a single space during substitution.
9527 These restrictions apply both to the values that @command{configure}
9528 computes, and to the values set directly by the user.  For example, the
9529 following invocations of @command{configure} are problematic, since they
9530 attempt to use special characters within @code{CPPFLAGS} and white space
9531 within @code{$(srcdir)}:
9533 @example
9534 CPPFLAGS='-DOUCH="&\"#$*?"' '../My Source/ouch-1.0/configure'
9536 '../My Source/ouch-1.0/configure' CPPFLAGS='-DOUCH="&\"#$*?"'
9537 @end example
9539 @node Caching Results
9540 @section Caching Results
9541 @cindex Cache
9543 To avoid checking for the same features repeatedly in various
9544 @command{configure} scripts (or in repeated runs of one script),
9545 @command{configure} can optionally save the results of many checks in a
9546 @dfn{cache file} (@pxref{Cache Files}).  If a @command{configure} script
9547 runs with caching enabled and finds a cache file, it reads the results
9548 of previous runs from the cache and avoids rerunning those checks.  As a
9549 result, @command{configure} can then run much faster than if it had to
9550 perform all of the checks every time.
9552 @defmac AC_CACHE_VAL (@var{cache-id}, @var{commands-to-set-it})
9553 @acindex{CACHE_VAL}
9554 Ensure that the results of the check identified by @var{cache-id} are
9555 available.  If the results of the check were in the cache file that was
9556 read, and @command{configure} was not given the @option{--quiet} or
9557 @option{--silent} option, print a message saying that the result was
9558 cached; otherwise, run the shell commands @var{commands-to-set-it}.  If
9559 the shell commands are run to determine the value, the value is
9560 saved in the cache file just before @command{configure} creates its output
9561 files.  @xref{Cache Variable Names}, for how to choose the name of the
9562 @var{cache-id} variable.
9564 The @var{commands-to-set-it} @emph{must have no side effects} except for
9565 setting the variable @var{cache-id}, see below.
9566 @end defmac
9568 @defmac AC_CACHE_CHECK (@var{message}, @var{cache-id}, @
9569   @var{commands-to-set-it})
9570 @acindex{CACHE_CHECK}
9571 A wrapper for @code{AC_CACHE_VAL} that takes care of printing the
9572 messages.  This macro provides a convenient shorthand for the most
9573 common way to use these macros.  It calls @code{AC_MSG_CHECKING} for
9574 @var{message}, then @code{AC_CACHE_VAL} with the @var{cache-id} and
9575 @var{commands} arguments, and @code{AC_MSG_RESULT} with @var{cache-id}.
9577 The @var{commands-to-set-it} @emph{must have no side effects} except for
9578 setting the variable @var{cache-id}, see below.
9579 @end defmac
9581 It is common to find buggy macros using @code{AC_CACHE_VAL} or
9582 @code{AC_CACHE_CHECK}, because people are tempted to call
9583 @code{AC_DEFINE} in the @var{commands-to-set-it}.  Instead, the code that
9584 @emph{follows} the call to @code{AC_CACHE_VAL} should call
9585 @code{AC_DEFINE}, by examining the value of the cache variable.  For
9586 instance, the following macro is broken:
9588 @example
9589 @c If you change this example, adjust tests/base.at:AC_CACHE_CHECK.
9590 @group
9591 AC_DEFUN([AC_SHELL_TRUE],
9592 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
9593                 [my_cv_shell_true_works=no
9594                  (true) 2>/dev/null && my_cv_shell_true_works=yes
9595                  if test "x$my_cv_shell_true_works" = xyes; then
9596                    AC_DEFINE([TRUE_WORKS], [1],
9597                              [Define if `true(1)' works properly.])
9598                  fi])
9600 @end group
9601 @end example
9603 @noindent
9604 This fails if the cache is enabled: the second time this macro is run,
9605 @code{TRUE_WORKS} @emph{will not be defined}.  The proper implementation
9608 @example
9609 @c If you change this example, adjust tests/base.at:AC_CACHE_CHECK.
9610 @group
9611 AC_DEFUN([AC_SHELL_TRUE],
9612 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
9613                 [my_cv_shell_true_works=no
9614                  (true) 2>/dev/null && my_cv_shell_true_works=yes])
9615  if test "x$my_cv_shell_true_works" = xyes; then
9616    AC_DEFINE([TRUE_WORKS], [1],
9617              [Define if `true(1)' works properly.])
9618  fi
9620 @end group
9621 @end example
9623 Also, @var{commands-to-set-it} should not print any messages, for
9624 example with @code{AC_MSG_CHECKING}; do that before calling
9625 @code{AC_CACHE_VAL}, so the messages are printed regardless of whether
9626 the results of the check are retrieved from the cache or determined by
9627 running the shell commands.
9629 @menu
9630 * Cache Variable Names::        Shell variables used in caches
9631 * Cache Files::                 Files @command{configure} uses for caching
9632 * Cache Checkpointing::         Loading and saving the cache file
9633 @end menu
9635 @node Cache Variable Names
9636 @subsection Cache Variable Names
9637 @cindex Cache variable
9639 The names of cache variables should have the following format:
9641 @example
9642 @var{package-prefix}_cv_@var{value-type}_@var{specific-value}_@ovar{additional-options}
9643 @end example
9645 @noindent
9646 for example, @samp{ac_cv_header_stat_broken} or
9647 @samp{ac_cv_prog_gcc_traditional}.  The parts of the variable name are:
9649 @table @asis
9650 @item @var{package-prefix}
9651 An abbreviation for your package or organization; the same prefix you
9652 begin local Autoconf macros with, except lowercase by convention.
9653 For cache values used by the distributed Autoconf macros, this value is
9654 @samp{ac}.
9656 @item @code{_cv_}
9657 Indicates that this shell variable is a cache value.  This string
9658 @emph{must} be present in the variable name, including the leading
9659 underscore.
9661 @item @var{value-type}
9662 A convention for classifying cache values, to produce a rational naming
9663 system.  The values used in Autoconf are listed in @ref{Macro Names}.
9665 @item @var{specific-value}
9666 Which member of the class of cache values this test applies to.
9667 For example, which function (@samp{alloca}), program (@samp{gcc}), or
9668 output variable (@samp{INSTALL}).
9670 @item @var{additional-options}
9671 Any particular behavior of the specific member that this test applies to.
9672 For example, @samp{broken} or @samp{set}.  This part of the name may
9673 be omitted if it does not apply.
9674 @end table
9676 The values assigned to cache variables may not contain newlines.
9677 Usually, their values are Boolean (@samp{yes} or @samp{no}) or the
9678 names of files or functions; so this is not an important restriction.
9679 @ref{Cache Variable Index} for an index of cache variables with
9680 documented semantics.
9683 @node Cache Files
9684 @subsection Cache Files
9686 A cache file is a shell script that caches the results of configure
9687 tests run on one system so they can be shared between configure scripts
9688 and configure runs.  It is not useful on other systems.  If its contents
9689 are invalid for some reason, the user may delete or edit it, or override
9690 documented cache variables on the @command{configure} command line.
9692 By default, @command{configure} uses no cache file,
9693 to avoid problems caused by accidental
9694 use of stale cache files.
9696 To enable caching, @command{configure} accepts @option{--config-cache} (or
9697 @option{-C}) to cache results in the file @file{config.cache}.
9698 Alternatively, @option{--cache-file=@var{file}} specifies that
9699 @var{file} be the cache file.  The cache file is created if it does not
9700 exist already.  When @command{configure} calls @command{configure} scripts in
9701 subdirectories, it uses the @option{--cache-file} argument so that they
9702 share the same cache.  @xref{Subdirectories}, for information on
9703 configuring subdirectories with the @code{AC_CONFIG_SUBDIRS} macro.
9705 @file{config.status} only pays attention to the cache file if it is
9706 given the @option{--recheck} option, which makes it rerun
9707 @command{configure}.
9709 It is wrong to try to distribute cache files for particular system types.
9710 There is too much room for error in doing that, and too much
9711 administrative overhead in maintaining them.  For any features that
9712 can't be guessed automatically, use the standard method of the canonical
9713 system type and linking files (@pxref{Manual Configuration}).
9715 The site initialization script can specify a site-wide cache file to
9716 use, instead of the usual per-program cache.  In this case, the cache
9717 file gradually accumulates information whenever someone runs a new
9718 @command{configure} script.  (Running @command{configure} merges the new cache
9719 results with the existing cache file.)  This may cause problems,
9720 however, if the system configuration (e.g., the installed libraries or
9721 compilers) changes and the stale cache file is not deleted.
9723 @node Cache Checkpointing
9724 @subsection Cache Checkpointing
9726 If your configure script, or a macro called from @file{configure.ac}, happens
9727 to abort the configure process, it may be useful to checkpoint the cache
9728 a few times at key points using @code{AC_CACHE_SAVE}.  Doing so
9729 reduces the amount of time it takes to rerun the configure script with
9730 (hopefully) the error that caused the previous abort corrected.
9732 @c FIXME: Do we really want to document this guy?
9733 @defmac AC_CACHE_LOAD
9734 @acindex{CACHE_LOAD}
9735 Loads values from existing cache file, or creates a new cache file if a
9736 cache file is not found.  Called automatically from @code{AC_INIT}.
9737 @end defmac
9739 @defmac AC_CACHE_SAVE
9740 @acindex{CACHE_SAVE}
9741 Flushes all cached values to the cache file.  Called automatically from
9742 @code{AC_OUTPUT}, but it can be quite useful to call
9743 @code{AC_CACHE_SAVE} at key points in @file{configure.ac}.
9744 @end defmac
9746 For instance:
9748 @example
9749 @r{ @dots{} AC_INIT, etc. @dots{}}
9750 @group
9751 # Checks for programs.
9752 AC_PROG_CC
9753 AC_PROG_AWK
9754 @r{ @dots{} more program checks @dots{}}
9755 AC_CACHE_SAVE
9756 @end group
9758 @group
9759 # Checks for libraries.
9760 AC_CHECK_LIB([nsl], [gethostbyname])
9761 AC_CHECK_LIB([socket], [connect])
9762 @r{ @dots{} more lib checks @dots{}}
9763 AC_CACHE_SAVE
9764 @end group
9766 @group
9767 # Might abort@dots{}
9768 AM_PATH_GTK([1.0.2], [], [AC_MSG_ERROR([GTK not in path])])
9769 AM_PATH_GTKMM([0.9.5], [], [AC_MSG_ERROR([GTK not in path])])
9770 @end group
9771 @r{ @dots{} AC_OUTPUT, etc. @dots{}}
9772 @end example
9774 @node Printing Messages
9775 @section Printing Messages
9776 @cindex Messages, from @command{configure}
9778 @command{configure} scripts need to give users running them several kinds
9779 of information.  The following macros print messages in ways appropriate
9780 for each kind.  The arguments to all of them get enclosed in shell
9781 double quotes, so the shell performs variable and back-quote
9782 substitution on them.
9784 These macros are all wrappers around the @command{echo} shell command.
9785 They direct output to the appropriate file descriptor (@pxref{File
9786 Descriptor Macros}).
9787 @command{configure} scripts should rarely need to run @command{echo} directly
9788 to print messages for the user.  Using these macros makes it easy to
9789 change how and when each kind of message is printed; such changes need
9790 only be made to the macro definitions and all the callers change
9791 automatically.
9793 To diagnose static issues, i.e., when @command{autoconf} is run, see
9794 @ref{Diagnostic Macros}.
9796 @defmac AC_MSG_CHECKING (@var{feature-description})
9797 @acindex{MSG_CHECKING}
9798 Notify the user that @command{configure} is checking for a particular
9799 feature.  This macro prints a message that starts with @samp{checking }
9800 and ends with @samp{...} and no newline.  It must be followed by a call
9801 to @code{AC_MSG_RESULT} to print the result of the check and the
9802 newline.  The @var{feature-description} should be something like
9803 @samp{whether the Fortran compiler accepts C++ comments} or @samp{for
9804 c89}.
9806 This macro prints nothing if @command{configure} is run with the
9807 @option{--quiet} or @option{--silent} option.
9808 @end defmac
9810 @anchor{AC_MSG_RESULT}
9811 @defmac AC_MSG_RESULT (@var{result-description})
9812 @acindex{MSG_RESULT}
9813 Notify the user of the results of a check.  @var{result-description} is
9814 almost always the value of the cache variable for the check, typically
9815 @samp{yes}, @samp{no}, or a file name.  This macro should follow a call
9816 to @code{AC_MSG_CHECKING}, and the @var{result-description} should be
9817 the completion of the message printed by the call to
9818 @code{AC_MSG_CHECKING}.
9820 This macro prints nothing if @command{configure} is run with the
9821 @option{--quiet} or @option{--silent} option.
9822 @end defmac
9824 @anchor{AC_MSG_NOTICE}
9825 @defmac AC_MSG_NOTICE (@var{message})
9826 @acindex{MSG_NOTICE}
9827 Deliver the @var{message} to the user.  It is useful mainly to print a
9828 general description of the overall purpose of a group of feature checks,
9829 e.g.,
9831 @example
9832 AC_MSG_NOTICE([checking if stack overflow is detectable])
9833 @end example
9835 This macro prints nothing if @command{configure} is run with the
9836 @option{--quiet} or @option{--silent} option.
9837 @end defmac
9839 @anchor{AC_MSG_ERROR}
9840 @defmac AC_MSG_ERROR (@var{error-description}, @dvar{exit-status, $?/1})
9841 @acindex{MSG_ERROR}
9842 Notify the user of an error that prevents @command{configure} from
9843 completing.  This macro prints an error message to the standard error
9844 output and exits @command{configure} with @var{exit-status} (@samp{$?}
9845 by default, except that @samp{0} is converted to @samp{1}).
9846 @var{error-description} should be something like @samp{invalid value
9847 $HOME for \$HOME}.
9849 The @var{error-description} should start with a lower-case letter, and
9850 ``cannot'' is preferred to ``can't''.
9851 @end defmac
9853 @defmac AC_MSG_FAILURE (@var{error-description}, @ovar{exit-status})
9854 @acindex{MSG_FAILURE}
9855 This @code{AC_MSG_ERROR} wrapper notifies the user of an error that
9856 prevents @command{configure} from completing @emph{and} that additional
9857 details are provided in @file{config.log}.  This is typically used when
9858 abnormal results are found during a compilation.
9859 @end defmac
9861 @anchor{AC_MSG_WARN}
9862 @defmac AC_MSG_WARN (@var{problem-description})
9863 @acindex{MSG_WARN}
9864 Notify the @command{configure} user of a possible problem.  This macro
9865 prints the message to the standard error output; @command{configure}
9866 continues running afterward, so macros that call @code{AC_MSG_WARN} should
9867 provide a default (back-up) behavior for the situations they warn about.
9868 @var{problem-description} should be something like @samp{ln -s seems to
9869 make hard links}.
9870 @end defmac
9874 @c ====================================================== Programming in M4.
9876 @node Programming in M4
9877 @chapter Programming in M4
9878 @cindex M4
9880 Autoconf is written on top of two layers: @dfn{M4sugar}, which provides
9881 convenient macros for pure M4 programming, and @dfn{M4sh}, which
9882 provides macros dedicated to shell script generation.
9884 As of this version of Autoconf, these two layers still contain
9885 experimental macros, whose interface might change in the future.  As a
9886 matter of fact, @emph{anything that is not documented must not be used}.
9888 @menu
9889 * M4 Quotation::                Protecting macros from unwanted expansion
9890 * Using autom4te::              The Autoconf executables backbone
9891 * Programming in M4sugar::      Convenient pure M4 macros
9892 * Debugging via autom4te::      Figuring out what M4 was doing
9893 @end menu
9895 @node M4 Quotation
9896 @section M4 Quotation
9897 @cindex M4 quotation
9898 @cindex quotation
9900 The most common problem with existing macros is an improper quotation.
9901 This section, which users of Autoconf can skip, but which macro writers
9902 @emph{must} read, first justifies the quotation scheme that was chosen
9903 for Autoconf and then ends with a rule of thumb.  Understanding the
9904 former helps one to follow the latter.
9906 @menu
9907 * Active Characters::           Characters that change the behavior of M4
9908 * One Macro Call::              Quotation and one macro call
9909 * Quoting and Parameters::      M4 vs. shell parameters
9910 * Quotation and Nested Macros::  Macros calling macros
9911 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
9912 * Quadrigraphs::                Another way to escape special characters
9913 * Balancing Parentheses::       Dealing with unbalanced parentheses
9914 * Quotation Rule Of Thumb::     One parenthesis, one quote
9915 @end menu
9917 @node Active Characters
9918 @subsection Active Characters
9920 To fully understand where proper quotation is important, you first need
9921 to know what the special characters are in Autoconf: @samp{#} introduces
9922 a comment inside which no macro expansion is performed, @samp{,}
9923 separates arguments, @samp{[} and @samp{]} are the quotes
9924 themselves@footnote{By itself, M4 uses @samp{`} and @samp{'}; it is the
9925 M4sugar layer that sets up the preferred quotes of @samp{[} and @samp{]}.},
9926 @samp{(} and @samp{)} (which M4 tries to match by pairs), and finally
9927 @samp{$} inside a macro definition.
9929 In order to understand the delicate case of macro calls, we first have
9930 to present some obvious failures.  Below they are ``obvious-ified'',
9931 but when you find them in real life, they are usually in disguise.
9933 Comments, introduced by a hash and running up to the newline, are opaque
9934 tokens to the top level: active characters are turned off, and there is
9935 no macro expansion:
9937 @example
9938 # define([def], ine)
9939 @result{}# define([def], ine)
9940 @end example
9942 Each time there can be a macro expansion, there is a quotation
9943 expansion, i.e., one level of quotes is stripped:
9945 @example
9946 int tab[10];
9947 @result{}int tab10;
9948 [int tab[10];]
9949 @result{}int tab[10];
9950 @end example
9952 Without this in mind, the reader might try hopelessly to use her macro
9953 @code{array}:
9955 @example
9956 define([array], [int tab[10];])
9957 array
9958 @result{}int tab10;
9959 [array]
9960 @result{}array
9961 @end example
9963 @noindent
9964 How can you correctly output the intended results@footnote{Using
9965 @code{defn}.}?
9968 @node One Macro Call
9969 @subsection One Macro Call
9971 Let's proceed on the interaction between active characters and macros
9972 with this small macro, which just returns its first argument:
9974 @example
9975 define([car], [$1])
9976 @end example
9978 @noindent
9979 The two pairs of quotes above are not part of the arguments of
9980 @code{define}; rather, they are understood by the top level when it
9981 tries to find the arguments of @code{define}.  Therefore, assuming
9982 @code{car} is not already defined, it is equivalent to write:
9984 @example
9985 define(car, $1)
9986 @end example
9988 @noindent
9989 But, while it is acceptable for a @file{configure.ac} to avoid unnecessary
9990 quotes, it is bad practice for Autoconf macros which must both be more
9991 robust and also advocate perfect style.
9993 At the top level, there are only two possibilities: either you
9994 quote or you don't:
9996 @example
9997 car(foo, bar, baz)
9998 @result{}foo
9999 [car(foo, bar, baz)]
10000 @result{}car(foo, bar, baz)
10001 @end example
10003 Let's pay attention to the special characters:
10005 @example
10006 car(#)
10007 @error{}EOF in argument list
10008 @end example
10010 The closing parenthesis is hidden in the comment; with a hypothetical
10011 quoting, the top level understood it this way:
10013 @example
10014 car([#)]
10015 @end example
10017 @noindent
10018 Proper quotation, of course, fixes the problem:
10020 @example
10021 car([#])
10022 @result{}#
10023 @end example
10025 Here are more examples:
10027 @example
10028 car(foo, bar)
10029 @result{}foo
10030 car([foo, bar])
10031 @result{}foo, bar
10032 car((foo, bar))
10033 @result{}(foo, bar)
10034 car([(foo], [bar)])
10035 @result{}(foo
10036 define([a], [b])
10037 @result{}
10038 car(a)
10039 @result{}b
10040 car([a])
10041 @result{}b
10042 car([[a]])
10043 @result{}a
10044 car([[[a]]])
10045 @result{}[a]
10046 @end example
10048 @node Quoting and Parameters
10049 @subsection Quoting and Parameters
10051 When M4 encounters @samp{$} within a macro definition, followed
10052 immediately by a character it recognizes (@samp{0}@dots{}@samp{9},
10053 @samp{#}, @samp{@@}, or @samp{*}), it will perform M4 parameter
10054 expansion.  This happens regardless of how many layers of quotes the
10055 parameter expansion is nested within, or even if it occurs in text that
10056 will be rescanned as a comment.
10058 @example
10059 define([none], [$1])
10060 @result{}
10061 define([one], [[$1]])
10062 @result{}
10063 define([two], [[[$1]]])
10064 @result{}
10065 define([comment], [# $1])
10066 @result{}
10067 define([active], [ACTIVE])
10068 @result{}
10069 none([active])
10070 @result{}ACTIVE
10071 one([active])
10072 @result{}active
10073 two([active])
10074 @result{}[active]
10075 comment([active])
10076 @result{}# active
10077 @end example
10079 On the other hand, since autoconf generates shell code, you often want
10080 to output shell variable expansion, rather than performing M4 parameter
10081 expansion.  To do this, you must use M4 quoting to separate the @samp{$}
10082 from the next character in the definition of your macro.  If the macro
10083 definition occurs in single-quoted text, then insert another level of
10084 quoting; if the usage is already inside a double-quoted string, then
10085 split it into concatenated strings.
10087 @example
10088 define([single], [a single-quoted $[]1 definition])
10089 @result{}
10090 define([double], [[a double-quoted $][1 definition]])
10091 @result{}
10092 single
10093 @result{}a single-quoted $1 definition
10094 double
10095 @result{}a double-quoted $1 definition
10096 @end example
10098 Posix states that M4 implementations are free to provide implementation
10099 extensions when @samp{$@{} is encountered in a macro definition.
10100 Autoconf reserves the longer sequence @samp{$@{@{} for use with planned
10101 extensions that will be available in the future GNU M4 2.0,
10102 but guarantees that all other instances of @samp{$@{} will be output
10103 literally.  Therefore, this idiom can also be used to output shell code
10104 parameter references:
10106 @example
10107 define([first], [$@{1@}])first
10108 @result{}$@{1@}
10109 @end example
10111 Posix also states that @samp{$11} should expand to the first parameter
10112 concatenated with a literal @samp{1}, although some versions of
10113 GNU M4 expand the eleventh parameter instead.  For
10114 portability, you should only use single-digit M4 parameter expansion.
10116 With this in mind, we can explore the cases where macros invoke
10117 macros@enddots{}
10119 @node Quotation and Nested Macros
10120 @subsection Quotation and Nested Macros
10122 The examples below use the following macros:
10124 @example
10125 define([car], [$1])
10126 define([active], [ACT, IVE])
10127 define([array], [int tab[10]])
10128 @end example
10130 Each additional embedded macro call introduces other possible
10131 interesting quotations:
10133 @example
10134 car(active)
10135 @result{}ACT
10136 car([active])
10137 @result{}ACT, IVE
10138 car([[active]])
10139 @result{}active
10140 @end example
10142 In the first case, the top level looks for the arguments of @code{car},
10143 and finds @samp{active}.  Because M4 evaluates its arguments
10144 before applying the macro, @samp{active} is expanded, which results in:
10146 @example
10147 car(ACT, IVE)
10148 @result{}ACT
10149 @end example
10151 @noindent
10152 In the second case, the top level gives @samp{active} as first and only
10153 argument of @code{car}, which results in:
10155 @example
10156 active
10157 @result{}ACT, IVE
10158 @end example
10160 @noindent
10161 i.e., the argument is evaluated @emph{after} the macro that invokes it.
10162 In the third case, @code{car} receives @samp{[active]}, which results in:
10164 @example
10165 [active]
10166 @result{}active
10167 @end example
10169 @noindent
10170 exactly as we already saw above.
10172 The example above, applied to a more realistic example, gives:
10174 @example
10175 car(int tab[10];)
10176 @result{}int tab10;
10177 car([int tab[10];])
10178 @result{}int tab10;
10179 car([[int tab[10];]])
10180 @result{}int tab[10];
10181 @end example
10183 @noindent
10184 Huh?  The first case is easily understood, but why is the second wrong,
10185 and the third right?  To understand that, you must know that after
10186 M4 expands a macro, the resulting text is immediately subjected
10187 to macro expansion and quote removal.  This means that the quote removal
10188 occurs twice---first before the argument is passed to the @code{car}
10189 macro, and second after the @code{car} macro expands to the first
10190 argument.
10192 As the author of the Autoconf macro @code{car}, you then consider it to
10193 be incorrect that your users have to double-quote the arguments of
10194 @code{car}, so you ``fix'' your macro.  Let's call it @code{qar} for
10195 quoted car:
10197 @example
10198 define([qar], [[$1]])
10199 @end example
10201 @noindent
10202 and check that @code{qar} is properly fixed:
10204 @example
10205 qar([int tab[10];])
10206 @result{}int tab[10];
10207 @end example
10209 @noindent
10210 Ahhh!  That's much better.
10212 But note what you've done: now that the result of @code{qar} is always
10213 a literal string, the only time a user can use nested macros is if she
10214 relies on an @emph{unquoted} macro call:
10216 @example
10217 qar(active)
10218 @result{}ACT
10219 qar([active])
10220 @result{}active
10221 @end example
10223 @noindent
10224 leaving no way for her to reproduce what she used to do with @code{car}:
10226 @example
10227 car([active])
10228 @result{}ACT, IVE
10229 @end example
10231 @noindent
10232 Worse yet: she wants to use a macro that produces a set of @code{cpp}
10233 macros:
10235 @example
10236 define([my_includes], [#include <stdio.h>])
10237 car([my_includes])
10238 @result{}#include <stdio.h>
10239 qar(my_includes)
10240 @error{}EOF in argument list
10241 @end example
10243 This macro, @code{qar}, because it double quotes its arguments, forces
10244 its users to leave their macro calls unquoted, which is dangerous.
10245 Commas and other active symbols are interpreted by M4 before
10246 they are given to the macro, often not in the way the users expect.
10247 Also, because @code{qar} behaves differently from the other macros,
10248 it's an exception that should be avoided in Autoconf.
10250 @node Changequote is Evil
10251 @subsection @code{changequote} is Evil
10252 @cindex @code{changequote}
10254 The temptation is often high to bypass proper quotation, in particular
10255 when it's late at night.  Then, many experienced Autoconf hackers
10256 finally surrender to the dark side of the force and use the ultimate
10257 weapon: @code{changequote}.
10259 The M4 builtin @code{changequote} belongs to a set of primitives that
10260 allow one to adjust the syntax of the language to adjust it to one's
10261 needs.  For instance, by default M4 uses @samp{`} and @samp{'} as
10262 quotes, but in the context of shell programming (and actually of most
10263 programming languages), that's about the worst choice one can make:
10264 because of strings and back-quoted expressions in shell code (such as
10265 @samp{'this'} and @samp{`that`}), and because of literal characters in usual
10266 programming languages (as in @samp{'0'}), there are many unbalanced
10267 @samp{`} and @samp{'}.  Proper M4 quotation then becomes a nightmare, if
10268 not impossible.  In order to make M4 useful in such a context, its
10269 designers have equipped it with @code{changequote}, which makes it
10270 possible to choose another pair of quotes.  M4sugar, M4sh, Autoconf, and
10271 Autotest all have chosen to use @samp{[} and @samp{]}.  Not especially
10272 because they are unlikely characters, but @emph{because they are
10273 characters unlikely to be unbalanced}.
10275 There are other magic primitives, such as @code{changecom} to specify
10276 what syntactic forms are comments (it is common to see
10277 @samp{changecom(<!--, -->)} when M4 is used to produce HTML pages),
10278 @code{changeword} and @code{changesyntax} to change other syntactic
10279 details (such as the character to denote the @var{n}th argument, @samp{$} by
10280 default, the parentheses around arguments, etc.).
10282 These primitives are really meant to make M4 more useful for specific
10283 domains: they should be considered like command line options:
10284 @option{--quotes}, @option{--comments}, @option{--words}, and
10285 @option{--syntax}.  Nevertheless, they are implemented as M4 builtins, as
10286 it makes M4 libraries self contained (no need for additional options).
10288 There lies the problem@enddots{}
10290 @sp 1
10292 The problem is that it is then tempting to use them in the middle of an
10293 M4 script, as opposed to its initialization.  This, if not carefully
10294 thought out, can lead to disastrous effects: @emph{you are changing the
10295 language in the middle of the execution}.  Changing and restoring the
10296 syntax is often not enough: if you happened to invoke macros in between,
10297 these macros are lost, as the current syntax is probably not
10298 the one they were implemented with.
10300 @c FIXME: I've been looking for a short, real case example, but I
10301 @c lost them all :(
10304 @node Quadrigraphs
10305 @subsection Quadrigraphs
10306 @cindex quadrigraphs
10307 @cindex @samp{@@S|@@}
10308 @cindex @samp{@@&t@@}
10309 @c Info cannot handle `:' in index entries.
10310 @ifnotinfo
10311 @cindex @samp{@@<:@@}
10312 @cindex @samp{@@:>@@}
10313 @cindex @samp{@@%:@@}
10314 @cindex @samp{@@@{:@@}
10315 @cindex @samp{@@:@}@@}
10316 @end ifnotinfo
10318 When writing an Autoconf macro you may occasionally need to generate
10319 special characters that are difficult to express with the standard
10320 Autoconf quoting rules.  For example, you may need to output the regular
10321 expression @samp{[^[]}, which matches any character other than @samp{[}.
10322 This expression contains unbalanced brackets so it cannot be put easily
10323 into an M4 macro.
10325 Additionally, there are a few m4sugar macros (such as @code{m4_split}
10326 and @code{m4_expand}) which internally use special markers in addition
10327 to the regular quoting characters.  If the arguments to these macros
10328 contain the literal strings @samp{-=<@{(} or @samp{)@}>=-}, the macros
10329 might behave incorrectly.
10331 You can work around these problems by using one of the following
10332 @dfn{quadrigraphs}:
10334 @table @samp
10335 @item @@<:@@
10336 @samp{[}
10337 @item @@:>@@
10338 @samp{]}
10339 @item @@S|@@
10340 @samp{$}
10341 @item @@%:@@
10342 @samp{#}
10343 @item @@@{:@@
10344 @samp{(}
10345 @item @@:@}@@
10346 @samp{)}
10347 @item @@&t@@
10348 Expands to nothing.
10349 @end table
10351 Quadrigraphs are replaced at a late stage of the translation process,
10352 after @command{m4} is run, so they do not get in the way of M4 quoting.
10353 For example, the string @samp{^@@<:@@}, independently of its quotation,
10354 appears as @samp{^[} in the output.
10356 The empty quadrigraph can be used:
10358 @itemize @minus
10359 @item to mark trailing spaces explicitly
10361 Trailing spaces are smashed by @command{autom4te}.  This is a feature.
10363 @item to produce quadrigraphs and other strings reserved by m4sugar
10365 For instance @samp{@@<@@&t@@:@@} produces @samp{@@<:@@}.  For a more
10366 contrived example:
10368 @example
10369 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])dnl
10370 m4_split([a )@}>=- b -=<@{( c])
10371 @result{}[a], [], [B], [], [c]
10372 m4_split([a )@}@@&t@@>=- b -=<@@&t@@@{( c])
10373 @result{}[a], [)@}>=-], [b], [-=<@{(], [c]
10374 @end example
10376 @item to escape @emph{occurrences} of forbidden patterns
10378 For instance you might want to mention @code{AC_FOO} in a comment, while
10379 still being sure that @command{autom4te} still catches unexpanded
10380 @samp{AC_*}.  Then write @samp{AC@@&t@@_FOO}.
10381 @end itemize
10383 The name @samp{@@&t@@} was suggested by Paul Eggert:
10385 @quotation
10386 I should give some credit to the @samp{@@&t@@} pun.  The @samp{&} is my
10387 own invention, but the @samp{t} came from the source code of the
10388 ALGOL68C compiler, written by Steve Bourne (of Bourne shell fame),
10389 and which used @samp{mt} to denote the empty string.  In C, it would
10390 have looked like something like:
10392 @example
10393 char const mt[] = "";
10394 @end example
10396 @noindent
10397 but of course the source code was written in Algol 68.
10399 I don't know where he got @samp{mt} from: it could have been his own
10400 invention, and I suppose it could have been a common pun around the
10401 Cambridge University computer lab at the time.
10402 @end quotation
10405 @node Balancing Parentheses
10406 @subsection Dealing with unbalanced parentheses
10407 @cindex balancing parentheses
10408 @cindex parentheses, balancing
10409 @cindex unbalanced parentheses, managing
10411 One of the pitfalls of portable shell programming is that @command{case}
10412 statements require unbalanced parentheses (@pxref{case, , Limitations of
10413 Shell Builtins}).  With syntax highlighting
10414 editors, the presence of unbalanced @samp{)} can interfere with editors
10415 that perform syntax highlighting of macro contents based on finding the
10416 matching @samp{(}.  Another concern is how much editing must be done
10417 when transferring code snippets between shell scripts and macro
10418 definitions.  But most importantly, the presence of unbalanced
10419 parentheses can introduce expansion bugs.
10421 For an example, here is an underquoted attempt to use the macro
10422 @code{my_case}, which happens to expand to a portable @command{case}
10423 statement:
10425 @example
10426 AC_DEFUN([my_case],
10427 [case $file_name in
10428   *.c) echo "C source code";;
10429 esac])
10430 AS_IF(:, my_case)
10431 @end example
10433 @noindent
10434 In the above example, the @code{AS_IF} call underquotes its arguments.
10435 As a result, the unbalanced @samp{)} generated by the premature
10436 expansion of @code{my_case} results in expanding @code{AS_IF} with a
10437 truncated parameter, and the expansion is syntactically invalid:
10439 @example
10440 if :; then
10441   case $file_name in
10442   *.c
10443 fi echo "C source code";;
10444 esac)
10445 @end example
10447 If nothing else, this should emphasize the importance of the quoting
10448 arguments to macro calls.  On the other hand, there are several
10449 variations for defining @code{my_case} to be more robust, even when used
10450 without proper quoting, each with some benefits and some drawbacks.
10452 @itemize @w{}
10453 @item Creative literal shell comment
10454 @example
10455 AC_DEFUN([my_case],
10456 [case $file_name in #(
10457   *.c) echo "C source code";;
10458 esac])
10459 @end example
10460 @noindent
10461 This version provides balanced parentheses to several editors, and can
10462 be copied and pasted into a terminal as is.  Unfortunately, it is still
10463 unbalanced as an Autoconf argument, since @samp{#(} is an M4 comment
10464 that masks the normal properties of @samp{(}.
10466 @item Quadrigraph shell comment
10467 @example
10468 AC_DEFUN([my_case],
10469 [case $file_name in @@%:@@(
10470   *.c) echo "C source code";;
10471 esac])
10472 @end example
10473 @noindent
10474 This version provides balanced parentheses to even more editors, and can
10475 be used as a balanced Autoconf argument.  Unfortunately, it requires
10476 some editing before it can be copied and pasted into a terminal, and the
10477 use of the quadrigraph @samp{@@%:@@} for @samp{#} reduces readability.
10479 @item Quoting just the parenthesis
10480 @example
10481 AC_DEFUN([my_case],
10482 [case $file_name in
10483   *.c[)] echo "C source code";;
10484 esac])
10485 @end example
10486 @noindent
10487 This version quotes the @samp{)}, so that it can be used as a balanced
10488 Autoconf argument.  As written, this is not balanced to an editor, but
10489 it can be coupled with @samp{[#(]} to meet that need, too.  However, it
10490 still requires some edits before it can be copied and pasted into a
10491 terminal.
10493 @item Double-quoting the entire statement
10494 @example
10495 AC_DEFUN([my_case],
10496 [[case $file_name in #(
10497   *.c) echo "C source code";;
10498 esac]])
10499 @end example
10500 @noindent
10501 Since the entire macro is double-quoted, there is no problem with using
10502 this as an Autoconf argument; and since the double-quoting is over the
10503 entire statement, this code can be easily copied and pasted into a
10504 terminal.  However, the double quoting prevents the expansion of any
10505 macros inside the case statement, which may cause its own set of
10506 problems.
10508 @item Using @code{AS_CASE}
10509 @example
10510 AC_DEFUN([my_case],
10511 [AS_CASE([$file_name],
10512   [*.c], [echo "C source code"])])
10513 @end example
10514 @noindent
10515 This version avoids the balancing issue altogether, by relying on
10516 @code{AS_CASE} (@pxref{Common Shell Constructs}); it also allows for the
10517 expansion of @code{AC_REQUIRE} to occur prior to the entire case
10518 statement, rather than within a branch of the case statement that might
10519 not be taken.  However, the abstraction comes with a penalty that it is
10520 no longer a quick copy, paste, and edit to get back to shell code.
10521 @end itemize
10524 @node Quotation Rule Of Thumb
10525 @subsection Quotation Rule Of Thumb
10527 To conclude, the quotation rule of thumb is:
10529 @center @emph{One pair of quotes per pair of parentheses.}
10531 Never over-quote, never under-quote, in particular in the definition of
10532 macros.  In the few places where the macros need to use brackets
10533 (usually in C program text or regular expressions), properly quote
10534 @emph{the arguments}!
10536 It is common to read Autoconf programs with snippets like:
10538 @example
10539 AC_TRY_LINK(
10540 changequote(<<, >>)dnl
10541 <<#include <time.h>
10542 #ifndef tzname /* For SGI.  */
10543 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
10544 #endif>>,
10545 changequote([, ])dnl
10546 [atoi (*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
10547 @end example
10549 @noindent
10550 which is incredibly useless since @code{AC_TRY_LINK} is @emph{already}
10551 double quoting, so you just need:
10553 @example
10554 AC_TRY_LINK(
10555 [#include <time.h>
10556 #ifndef tzname /* For SGI.  */
10557 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
10558 #endif],
10559             [atoi (*tzname);],
10560             [ac_cv_var_tzname=yes],
10561             [ac_cv_var_tzname=no])
10562 @end example
10564 @noindent
10565 The M4-fluent reader might note that these two examples are rigorously
10566 equivalent, since M4 swallows both the @samp{changequote(<<, >>)}
10567 and @samp{<<} @samp{>>} when it @dfn{collects} the arguments: these
10568 quotes are not part of the arguments!
10570 Simplified, the example above is just doing this:
10572 @example
10573 changequote(<<, >>)dnl
10574 <<[]>>
10575 changequote([, ])dnl
10576 @end example
10578 @noindent
10579 instead of simply:
10581 @example
10582 [[]]
10583 @end example
10585 With macros that do not double quote their arguments (which is the
10586 rule), double-quote the (risky) literals:
10588 @example
10589 AC_LINK_IFELSE([AC_LANG_PROGRAM(
10590 [[#include <time.h>
10591 #ifndef tzname /* For SGI.  */
10592 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
10593 #endif]],
10594                                 [atoi (*tzname);])],
10595                [ac_cv_var_tzname=yes],
10596                [ac_cv_var_tzname=no])
10597 @end example
10599 Please note that the macro @code{AC_TRY_LINK} is obsolete, so you really
10600 should be using @code{AC_LINK_IFELSE} instead.
10602 @xref{Quadrigraphs}, for what to do if you run into a hopeless case
10603 where quoting does not suffice.
10605 When you create a @command{configure} script using newly written macros,
10606 examine it carefully to check whether you need to add more quotes in
10607 your macros.  If one or more words have disappeared in the M4
10608 output, you need more quotes.  When in doubt, quote.
10610 However, it's also possible to put on too many layers of quotes.  If
10611 this happens, the resulting @command{configure} script may contain
10612 unexpanded macros.  The @command{autoconf} program checks for this problem
10613 by looking for the string @samp{AC_} in @file{configure}.  However, this
10614 heuristic does not work in general: for example, it does not catch
10615 overquoting in @code{AC_DEFINE} descriptions.
10618 @c ---------------------------------------- Using autom4te
10620 @node Using autom4te
10621 @section Using @command{autom4te}
10623 The Autoconf suite, including M4sugar, M4sh, and Autotest, in addition
10624 to Autoconf per se, heavily rely on M4.  All these different uses
10625 revealed common needs factored into a layer over M4:
10626 @command{autom4te}@footnote{
10628 Yet another great name from Lars J. Aas.
10632 @command{autom4te} is a preprocessor that is like @command{m4}.
10633 It supports M4 extensions designed for use in tools like Autoconf.
10635 @menu
10636 * autom4te Invocation::         A GNU M4 wrapper
10637 * Customizing autom4te::        Customizing the Autoconf package
10638 @end menu
10640 @node autom4te Invocation
10641 @subsection Invoking @command{autom4te}
10643 The command line arguments are modeled after M4's:
10645 @example
10646 autom4te @var{options} @var{files}
10647 @end example
10649 @noindent
10650 @evindex M4
10651 where the @var{files} are directly passed to @command{m4}.  By default,
10652 GNU M4 is found during configuration, but the environment
10653 variable
10654 @env{M4} can be set to tell @command{autom4te} where to look.  In addition
10655 to the regular expansion, it handles the replacement of the quadrigraphs
10656 (@pxref{Quadrigraphs}), and of @samp{__oline__}, the current line in the
10657 output.  It supports an extended syntax for the @var{files}:
10659 @table @file
10660 @item @var{file}.m4f
10661 This file is an M4 frozen file.  Note that @emph{all the previous files
10662 are ignored}.  See the option @option{--melt} for the rationale.
10664 @item @var{file}?
10665 If found in the library path, the @var{file} is included for expansion,
10666 otherwise it is ignored instead of triggering a failure.
10667 @end table
10669 @sp 1
10671 Of course, it supports the Autoconf common subset of options:
10673 @table @option
10674 @item --help
10675 @itemx -h
10676 Print a summary of the command line options and exit.
10678 @item --version
10679 @itemx -V
10680 Print the version number of Autoconf and exit.
10682 @item --verbose
10683 @itemx -v
10684 Report processing steps.
10686 @item --debug
10687 @itemx -d
10688 Don't remove the temporary files and be even more verbose.
10690 @item --include=@var{dir}
10691 @itemx -I @var{dir}
10692 Also look for input files in @var{dir}.  Multiple invocations
10693 accumulate.
10695 @item --output=@var{file}
10696 @itemx -o @var{file}
10697 Save output (script or trace) to @var{file}.  The file @option{-} stands
10698 for the standard output.
10699 @end table
10701 @sp 1
10703 As an extension of @command{m4}, it includes the following options:
10705 @table @option
10706 @item --warnings=@var{category}
10707 @itemx -W @var{category}
10708 @evindex WARNINGS
10709 @c FIXME: Point to the M4sugar macros, not Autoconf's.
10710 Report the warnings related to @var{category} (which can actually be a
10711 comma separated list).  @xref{Reporting Messages}, macro
10712 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
10713 values include:
10715 @table @samp
10716 @item all
10717 report all the warnings
10719 @item none
10720 report none
10722 @item error
10723 treats warnings as errors
10725 @item no-@var{category}
10726 disable warnings falling into @var{category}
10727 @end table
10729 Warnings about @samp{syntax} are enabled by default, and the environment
10730 variable @env{WARNINGS}, a comma separated list of categories, is
10731 honored.  @samp{autom4te -W @var{category}} actually
10732 behaves as if you had run:
10734 @example
10735 autom4te --warnings=syntax,$WARNINGS,@var{category}
10736 @end example
10738 @noindent
10739 For example, if you want to disable defaults and @env{WARNINGS}
10740 of @command{autom4te}, but enable the warnings about obsolete
10741 constructs, you would use @option{-W none,obsolete}.
10743 @cindex Back trace
10744 @cindex Macro invocation stack
10745 @command{autom4te} displays a back trace for errors, but not for
10746 warnings; if you want them, just pass @option{-W error}.
10748 @item --melt
10749 @itemx -M
10750 Do not use frozen files.  Any argument @code{@var{file}.m4f} is
10751 replaced by @code{@var{file}.m4}.  This helps tracing the macros which
10752 are executed only when the files are frozen, typically
10753 @code{m4_define}.  For instance, running:
10755 @example
10756 autom4te --melt 1.m4 2.m4f 3.m4 4.m4f input.m4
10757 @end example
10759 @noindent
10760 is roughly equivalent to running:
10762 @example
10763 m4 1.m4 2.m4 3.m4 4.m4 input.m4
10764 @end example
10766 @noindent
10767 while
10769 @example
10770 autom4te 1.m4 2.m4f 3.m4 4.m4f input.m4
10771 @end example
10773 @noindent
10774 is equivalent to:
10776 @example
10777 m4 --reload-state=4.m4f input.m4
10778 @end example
10780 @item --freeze
10781 @itemx -F
10782 Produce a frozen state file.  @command{autom4te} freezing is stricter
10783 than M4's: it must produce no warnings, and no output other than empty
10784 lines (a line with white space is @emph{not} empty) and comments
10785 (starting with @samp{#}).  Unlike @command{m4}'s similarly-named option,
10786 this option takes no argument:
10788 @example
10789 autom4te 1.m4 2.m4 3.m4 --freeze --output=3.m4f
10790 @end example
10792 @noindent
10793 corresponds to
10795 @example
10796 m4 1.m4 2.m4 3.m4 --freeze-state=3.m4f
10797 @end example
10799 @item --mode=@var{octal-mode}
10800 @itemx -m @var{octal-mode}
10801 Set the mode of the non-traces output to @var{octal-mode}; by default
10802 @samp{0666}.
10803 @end table
10805 @sp 1
10807 @cindex @file{autom4te.cache}
10808 As another additional feature over @command{m4}, @command{autom4te}
10809 caches its results.  GNU M4 is able to produce a regular
10810 output and traces at the same time.  Traces are heavily used in the
10811 GNU Build System: @command{autoheader} uses them to build
10812 @file{config.h.in}, @command{autoreconf} to determine what
10813 GNU Build System components are used, @command{automake} to
10814 ``parse'' @file{configure.ac} etc.  To avoid recomputation,
10815 traces are cached while performing regular expansion,
10816 and conversely.  This cache is (actually, the caches are) stored in
10817 the directory @file{autom4te.cache}.  @emph{It can safely be removed}
10818 at any moment (especially if for some reason @command{autom4te}
10819 considers it trashed).
10821 @table @option
10822 @item --cache=@var{directory}
10823 @itemx -C @var{directory}
10824 Specify the name of the directory where the result should be cached.
10825 Passing an empty value disables caching.  Be sure to pass a relative
10826 file name, as for the time being, global caches are not supported.
10828 @item --no-cache
10829 Don't cache the results.
10831 @item --force
10832 @itemx -f
10833 If a cache is used, consider it obsolete (but update it anyway).
10834 @end table
10836 @sp 1
10838 Because traces are so important to the GNU Build System,
10839 @command{autom4te} provides high level tracing features as compared to
10840 M4, and helps exploiting the cache:
10842 @table @option
10843 @item --trace=@var{macro}[:@var{format}]
10844 @itemx -t @var{macro}[:@var{format}]
10845 Trace the invocations of @var{macro} according to the @var{format}.
10846 Multiple @option{--trace} arguments can be used to list several macros.
10847 Multiple @option{--trace} arguments for a single macro are not
10848 cumulative; instead, you should just make @var{format} as long as
10849 needed.
10851 The @var{format} is a regular string, with newlines if desired, and
10852 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}.  It can
10853 use the following special escapes:
10855 @table @samp
10856 @item $$
10857 @c $$ restore font-lock
10858 The character @samp{$}.
10860 @item $f
10861 The file name from which @var{macro} is called.
10863 @item $l
10864 The line number from which @var{macro} is called.
10866 @item $d
10867 The depth of the @var{macro} call.  This is an M4 technical detail that
10868 you probably don't want to know about.
10870 @item $n
10871 The name of the @var{macro}.
10873 @item $@var{num}
10874 The @var{num}th argument of the call to @var{macro}.
10876 @item $@@
10877 @itemx $@var{sep}@@
10878 @itemx $@{@var{separator}@}@@
10879 All the arguments passed to @var{macro}, separated by the character
10880 @var{sep} or the string @var{separator} (@samp{,} by default).  Each
10881 argument is quoted, i.e., enclosed in a pair of square brackets.
10883 @item $*
10884 @itemx $@var{sep}*
10885 @itemx $@{@var{separator}@}*
10886 As above, but the arguments are not quoted.
10888 @item $%
10889 @itemx $@var{sep}%
10890 @itemx $@{@var{separator}@}%
10891 As above, but the arguments are not quoted, all new line characters in
10892 the arguments are smashed, and the default separator is @samp{:}.
10894 The escape @samp{$%} produces single-line trace outputs (unless you put
10895 newlines in the @samp{separator}), while @samp{$@@} and @samp{$*} do
10896 not.
10897 @end table
10899 @xref{autoconf Invocation}, for examples of trace uses.
10901 @item --preselect=@var{macro}
10902 @itemx -p @var{macro}
10903 Cache the traces of @var{macro}, but do not enable traces.  This is
10904 especially important to save CPU cycles in the future.  For instance,
10905 when invoked, @command{autoconf} preselects all the macros that
10906 @command{autoheader}, @command{automake}, @command{autoreconf}, etc.,
10907 trace, so that running @command{m4} is not needed to trace them: the
10908 cache suffices.  This results in a huge speed-up.
10909 @end table
10911 @sp 1
10913 @cindex Autom4te Library
10914 Finally, @command{autom4te} introduces the concept of @dfn{Autom4te
10915 libraries}.  They consists in a powerful yet extremely simple feature:
10916 sets of combined command line arguments:
10918 @table @option
10919 @item --language=@var{language}
10920 @itemx -l @var{language}
10921 Use the @var{language} Autom4te library.  Current languages include:
10923 @table @code
10924 @item M4sugar
10925 create M4sugar output.
10927 @item M4sh
10928 create M4sh executable shell scripts.
10930 @item Autotest
10931 create Autotest executable test suites.
10933 @item Autoconf-without-aclocal-m4
10934 create Autoconf executable configure scripts without
10935 reading @file{aclocal.m4}.
10937 @item Autoconf
10938 create Autoconf executable configure scripts.  This language inherits
10939 all the characteristics of @code{Autoconf-without-aclocal-m4} and
10940 additionally reads @file{aclocal.m4}.
10941 @end table
10943 @item --prepend-include=@var{dir}
10944 @itemx -B @var{dir}
10945 Prepend directory @var{dir} to the search path.  This is used to include
10946 the language-specific files before any third-party macros.
10948 @end table
10950 @cindex @file{autom4te.cfg}
10951 As an example, if Autoconf is installed in its default location,
10952 @file{/usr/local}, the command @samp{autom4te -l m4sugar foo.m4} is
10953 strictly equivalent to the command:
10955 @example
10956 autom4te --prepend-include /usr/local/share/autoconf \
10957   m4sugar/m4sugar.m4f --warnings syntax foo.m4
10958 @end example
10960 @noindent
10961 Recursive expansion applies here: the command @samp{autom4te -l m4sh foo.m4}
10962 is the same as @samp{autom4te --language M4sugar m4sugar/m4sh.m4f
10963 foo.m4}, i.e.:
10965 @example
10966 autom4te --prepend-include /usr/local/share/autoconf \
10967   m4sugar/m4sugar.m4f m4sugar/m4sh.m4f --mode 777 foo.m4
10968 @end example
10970 @noindent
10971 The definition of the languages is stored in @file{autom4te.cfg}.
10973 @node Customizing autom4te
10974 @subsection Customizing @command{autom4te}
10976 One can customize @command{autom4te} via @file{~/.autom4te.cfg} (i.e.,
10977 as found in the user home directory), and @file{./.autom4te.cfg} (i.e.,
10978 as found in the directory from which @command{autom4te} is run).  The
10979 order is first reading @file{autom4te.cfg}, then @file{~/.autom4te.cfg},
10980 then @file{./.autom4te.cfg}, and finally the command line arguments.
10982 In these text files, comments are introduced with @code{#}, and empty
10983 lines are ignored.  Customization is performed on a per-language basis,
10984 wrapped in between a @samp{begin-language: "@var{language}"},
10985 @samp{end-language: "@var{language}"} pair.
10987 Customizing a language stands for appending options (@pxref{autom4te
10988 Invocation}) to the current definition of the language.  Options, and
10989 more generally arguments, are introduced by @samp{args:
10990 @var{arguments}}.  You may use the traditional shell syntax to quote the
10991 @var{arguments}.
10993 As an example, to disable Autoconf caches (@file{autom4te.cache})
10994 globally, include the following lines in @file{~/.autom4te.cfg}:
10996 @verbatim
10997 ## ------------------ ##
10998 ## User Preferences.  ##
10999 ## ------------------ ##
11001 begin-language: "Autoconf-without-aclocal-m4"
11002 args: --no-cache
11003 end-language: "Autoconf-without-aclocal-m4"
11004 @end verbatim
11007 @node Programming in M4sugar
11008 @section Programming in M4sugar
11010 @cindex M4sugar
11011 M4 by itself provides only a small, but sufficient, set of all-purpose
11012 macros.  M4sugar introduces additional generic macros.  Its name was
11013 coined by Lars J. Aas: ``Readability And Greater Understanding Stands 4
11014 M4sugar''.
11016 M4sugar reserves the macro namespace @samp{^_m4_} for internal use, and
11017 the macro namespace @samp{^m4_} for M4sugar macros.  You should not
11018 define your own macros into these namespaces.
11020 @menu
11021 * Redefined M4 Macros::         M4 builtins changed in M4sugar
11022 * Diagnostic Macros::           Diagnostic messages from M4sugar
11023 * Diversion support::           Diversions in M4sugar
11024 * Conditional constructs::      Conditions in M4
11025 * Looping constructs::          Iteration in M4
11026 * Evaluation Macros::           More quotation and evaluation control
11027 * Text processing Macros::      String manipulation in M4
11028 * Number processing Macros::    Arithmetic computation in M4
11029 * Set manipulation Macros::     Set manipulation in M4
11030 * Forbidden Patterns::          Catching unexpanded macros
11031 @end menu
11033 @node Redefined M4 Macros
11034 @subsection Redefined M4 Macros
11036 @msindex{builtin}
11037 @msindex{changecom}
11038 @msindex{changequote}
11039 @msindex{debugfile}
11040 @msindex{debugmode}
11041 @msindex{decr}
11042 @msindex{define}
11043 @msindex{divnum}
11044 @msindex{errprint}
11045 @msindex{esyscmd}
11046 @msindex{eval}
11047 @msindex{format}
11048 @msindex{ifdef}
11049 @msindex{incr}
11050 @msindex{index}
11051 @msindex{indir}
11052 @msindex{len}
11053 @msindex{pushdef}
11054 @msindex{shift}
11055 @msindex{substr}
11056 @msindex{syscmd}
11057 @msindex{sysval}
11058 @msindex{traceoff}
11059 @msindex{traceon}
11060 @msindex{translit}
11061 With a few exceptions, all the M4 native macros are moved in the
11062 @samp{m4_} pseudo-namespace, e.g., M4sugar renames @code{define} as
11063 @code{m4_define} etc.
11065 The list of macros unchanged from M4, except for their name, is:
11066 @itemize @minus
11067 @item m4_builtin
11068 @item m4_changecom
11069 @item m4_changequote
11070 @item m4_debugfile
11071 @item m4_debugmode
11072 @item m4_decr
11073 @item m4_define
11074 @item m4_divnum
11075 @item m4_errprint
11076 @item m4_esyscmd
11077 @item m4_eval
11078 @item m4_format
11079 @item m4_ifdef
11080 @item m4_incr
11081 @item m4_index
11082 @item m4_indir
11083 @item m4_len
11084 @item m4_pushdef
11085 @item m4_shift
11086 @item m4_substr
11087 @item m4_syscmd
11088 @item m4_sysval
11089 @item m4_traceoff
11090 @item m4_traceon
11091 @item m4_translit
11092 @end itemize
11094 Some M4 macros are redefined, and are slightly incompatible with their
11095 native equivalent.
11097 @defmac __file__
11098 @defmacx __line__
11099 @MSindex __file__
11100 @MSindex __line__
11101 All M4 macros starting with @samp{__} retain their original name: for
11102 example, no @code{m4__file__} is defined.
11103 @end defmac
11105 @defmac __oline__
11106 @MSindex __oline__
11107 This is not technically a macro, but a feature of Autom4te.  The
11108 sequence @code{__oline__} can be used similarly to the other m4sugar
11109 location macros, but rather than expanding to the location of the input
11110 file, it is translated to the line number where it appears in the output
11111 file after all other M4 expansions.
11112 @end defmac
11114 @defmac dnl
11115 @MSindex dnl
11116 This macro kept its original name: no @code{m4_dnl} is defined.
11117 @end defmac
11119 @defmac m4_bpatsubst (@var{string}, @var{regexp}, @ovar{replacement})
11120 @msindex{bpatsubst}
11121 This macro corresponds to @code{patsubst}.  The name @code{m4_patsubst}
11122 is kept for future versions of M4sugar, once GNU M4 2.0 is
11123 released and supports extended regular expression syntax.
11124 @end defmac
11126 @defmac m4_bregexp (@var{string}, @var{regexp}, @ovar{replacement})
11127 @msindex{bregexp}
11128 This macro corresponds to @code{regexp}.  The name @code{m4_regexp}
11129 is kept for future versions of M4sugar, once GNU M4 2.0 is
11130 released and supports extended regular expression syntax.
11131 @end defmac
11133 @defmac m4_copy (@var{source}, @var{dest})
11134 @defmacx m4_copy_force (@var{source}, @var{dest})
11135 @defmacx m4_rename (@var{source}, @var{dest})
11136 @defmacx m4_rename_force (@var{source}, @var{dest})
11137 @msindex{copy}
11138 @msindex{copy_force}
11139 @msindex{rename}
11140 @msindex{rename_force}
11141 These macros aren't directly builtins, but are closely related to
11142 @code{m4_pushdef} and @code{m4_defn}.  @code{m4_copy} and
11143 @code{m4_rename} ensure that @var{dest} is undefined, while
11144 @code{m4_copy_force} and @code{m4_rename_force} overwrite any existing
11145 definition.  All four macros then proceed to copy the entire pushdef
11146 stack of definitions of @var{source} over to @var{dest}.  @code{m4_copy}
11147 and @code{m4_copy_force} preserve the source (including in the special
11148 case where @var{source} is undefined), while @code{m4_rename} and
11149 @code{m4_rename_force} undefine the original macro name (making it an
11150 error to rename an undefined @var{source}).
11152 Note that attempting to invoke a renamed macro might not work, since the
11153 macro may have a dependence on helper macros accessed via composition of
11154 @samp{$0} but that were not also renamed; likewise, other macros may
11155 have a hard-coded dependence on @var{source} and could break if
11156 @var{source} has been deleted.  On the other hand, it is always safe to
11157 rename a macro to temporarily move it out of the way, then rename it
11158 back later to restore original semantics.
11159 @end defmac
11161 @defmac m4_defn (@var{macro}@dots{})
11162 @msindex{defn}
11163 This macro fails if @var{macro} is not defined, even when using older
11164 versions of M4 that did not warn.  See @code{m4_undefine}.
11165 Unfortunately, in order to support these older versions of M4, there are
11166 some situations involving unbalanced quotes where concatenating multiple
11167 macros together will work in newer M4 but not in m4sugar; use
11168 quadrigraphs to work around this.
11169 @end defmac
11171 @defmac m4_divert (@var{diversion})
11172 @msindex{divert}
11173 M4sugar relies heavily on diversions, so rather than behaving as a
11174 primitive, @code{m4_divert} behaves like:
11175 @example
11176 m4_divert_pop()m4_divert_push([@var{diversion}])
11177 @end example
11178 @noindent
11179 @xref{Diversion support}, for more details about the use of the
11180 diversion stack.  In particular, this implies that @var{diversion}
11181 should be a named diversion rather than a raw number.  But be aware that
11182 it is seldom necessary to explicitly change the diversion stack, and
11183 that when done incorrectly, it can lead to syntactically invalid
11184 scripts.
11185 @end defmac
11187 @defmac m4_dumpdef (@var{name}@dots{})
11188 @defmacx m4_dumpdefs (@var{name}@dots{})
11189 @msindex{dumpdef}
11190 @msindex{dumpdefs}
11191 @code{m4_dumpdef} is like the M4 builtin, except that this version
11192 requires at least one argument, output always goes to standard error
11193 rather than the current debug file, no sorting is done on multiple
11194 arguments, and an error is issued if any
11195 @var{name} is undefined.  @code{m4_dumpdefs} is a convenience macro that
11196 calls @code{m4_dumpdef} for all of the
11197 @code{m4_pushdef} stack of definitions, starting with the current, and
11198 silently does nothing if @var{name} is undefined.
11200 Unfortunately, due to a limitation in M4 1.4.x, any macro defined as a
11201 builtin is output as the empty string.  This behavior is rectified by
11202 using M4 1.6 or newer.  However, this behavior difference means that
11203 @code{m4_dumpdef} should only be used while developing m4sugar macros,
11204 and never in the final published form of a macro.
11205 @end defmac
11207 @defmac m4_esyscmd_s (@var{command})
11208 @msindex{esyscmd_s}
11209 Like @code{m4_esyscmd}, this macro expands to the result of running
11210 @var{command} in a shell.  The difference is that any trailing newlines
11211 are removed, so that the output behaves more like shell command
11212 substitution.
11213 @end defmac
11215 @defmac m4_exit (@var{exit-status})
11216 @msindex{exit}
11217 This macro corresponds to @code{m4exit}.
11218 @end defmac
11220 @defmac m4_if (@var{comment})
11221 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @ovar{not-equal})
11222 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal-1}, @
11223   @var{string-3}, @var{string-4}, @var{equal-2}, @dots{}, @ovar{not-equal})
11224 @msindex{if}
11225 This macro corresponds to @code{ifelse}.  @var{string-1} and
11226 @var{string-2} are compared literally, so usually one of the two
11227 arguments is passed unquoted.  @xref{Conditional constructs}, for more
11228 conditional idioms.
11229 @end defmac
11231 @defmac m4_include (@var{file})
11232 @defmacx m4_sinclude (@var{file})
11233 @msindex{include}
11234 @msindex{sinclude}
11235 Like the M4 builtins, but warn against multiple inclusions of @var{file}.
11236 @end defmac
11238 @defmac m4_mkstemp (@var{template})
11239 @defmacx m4_maketemp (@var{template})
11240 @msindex{maketemp}
11241 @msindex{mkstemp}
11242 Posix requires @code{maketemp} to replace the trailing @samp{X}
11243 characters in @var{template} with the process id, without regards to the
11244 existence of a file by that name, but this a security hole.  When this
11245 was pointed out to the Posix folks, they agreed to invent a new macro
11246 @code{mkstemp} that always creates a uniquely named file, but not all
11247 versions of GNU M4 support the new macro.  In M4sugar,
11248 @code{m4_maketemp} and @code{m4_mkstemp} are synonyms for each other,
11249 and both have the secure semantics regardless of which macro the
11250 underlying M4 provides.
11251 @end defmac
11253 @defmac m4_popdef (@var{macro}@dots{})
11254 @msindex{popdef}
11255 This macro fails if @var{macro} is not defined, even when using older
11256 versions of M4 that did not warn.  See @code{m4_undefine}.
11257 @end defmac
11259 @defmac m4_undefine (@var{macro}@dots{})
11260 @msindex{undefine}
11261 This macro fails if @var{macro} is not defined, even when using older
11262 versions of M4 that did not warn.  Use
11264 @example
11265 m4_ifdef([@var{macro}], [m4_undefine([@var{macro}])])
11266 @end example
11268 @noindent
11269 if you are not sure whether @var{macro} is defined.
11270 @end defmac
11272 @defmac m4_undivert (@var{diversion}@dots{})
11273 @msindex{undivert}
11274 Unlike the M4 builtin, at least one @var{diversion} must be specified.
11275 Also, since the M4sugar diversion stack prefers named
11276 diversions, the use of @code{m4_undivert} to include files is risky.
11277 @xref{Diversion support}, for more details about the use of the
11278 diversion stack.  But be aware that it is seldom necessary to explicitly
11279 change the diversion stack, and that when done incorrectly, it can lead
11280 to syntactically invalid scripts.
11281 @end defmac
11283 @defmac m4_wrap (@var{text})
11284 @defmacx m4_wrap_lifo (@var{text})
11285 @msindex{wrap}
11286 @msindex{wrap_lifo}
11287 These macros correspond to @code{m4wrap}.  Posix requires arguments of
11288 multiple wrap calls to be reprocessed at EOF in the same order
11289 as the original calls (first-in, first-out).  GNU M4 versions
11290 through 1.4.10, however, reprocess them in reverse order (last-in,
11291 first-out).  Both orders are useful, therefore, you can rely on
11292 @code{m4_wrap} to provide FIFO semantics and @code{m4_wrap_lifo} for
11293 LIFO semantics, regardless of the underlying GNU M4 version.
11295 Unlike the GNU M4 builtin, these macros only recognize one
11296 argument, and avoid token pasting between consecutive invocations.  On
11297 the other hand, nested calls to @code{m4_wrap} from within wrapped text
11298 work just as in the builtin.
11299 @end defmac
11302 @node Diagnostic Macros
11303 @subsection Diagnostic messages from M4sugar
11304 @cindex Messages, from @command{M4sugar}
11306 When macros statically diagnose abnormal situations, benign or fatal,
11307 they should report them using these macros.  For issuing dynamic issues,
11308 i.e., when @command{configure} is run, see @ref{Printing Messages}.
11310 @defmac m4_assert (@var{expression}, @dvar{exit-status, 1})
11311 @msindex{assert}
11312 Assert that the arithmetic @var{expression} evaluates to non-zero.
11313 Otherwise, issue a fatal error, and exit @command{autom4te} with
11314 @var{exit-status}.
11315 @end defmac
11317 @defmac m4_errprintn (@var{message})
11318 @msindex{errprintn}
11319 Similar to the builtin @code{m4_errprint}, except that a newline is
11320 guaranteed after @var{message}.
11321 @end defmac
11323 @anchor{m4_fatal}
11324 @defmac m4_fatal (@var{message})
11325 @msindex{fatal}
11326 Report a severe error @var{message} prefixed with the current location,
11327 and have @command{autom4te} die.
11328 @end defmac
11330 @defmac m4_location
11331 @msindex{location}
11332 Useful as a prefix in a message line.  Short for:
11333 @example
11334 __file__:__line__
11335 @end example
11336 @end defmac
11338 @anchor{m4_warn}
11339 @defmac m4_warn (@var{category}, @var{message})
11340 @msindex{warn}
11341 Report @var{message} as a warning (or as an error if requested by the
11342 user) if warnings of the @var{category} are turned on.  If the message
11343 is emitted, it is prefixed with the current location, and followed by a
11344 call trace of all macros defined via @code{AC_DEFUN} used to get to the
11345 current expansion.  You are encouraged to use standard categories, which
11346 currently include:
11348 @table @samp
11349 @item all
11350 messages that don't fall into one of the following categories.  Use of an
11351 empty @var{category} is equivalent.
11353 @item cross
11354 related to cross compilation issues.
11356 @item obsolete
11357 use of an obsolete construct.
11359 @item syntax
11360 dubious syntactic constructs, incorrectly ordered macro calls.
11361 @end table
11362 @end defmac
11365 @node Diversion support
11366 @subsection Diversion support
11368 M4sugar makes heavy use of diversions under the hood, because it is
11369 often the case that
11370 text that must appear early in the output is not discovered until late
11371 in the input.  Additionally, some of the topological sorting algorithms
11372 used in resolving macro dependencies use diversions.  However, most
11373 macros should not need to change diversions directly, but rather rely on
11374 higher-level M4sugar macros to manage diversions transparently.  If you
11375 change diversions improperly, you risk generating a syntactically
11376 invalid script, because an incorrect diversion will violate assumptions
11377 made by many macros about whether prerequisite text has been previously
11378 output.  In short, if you manually change the diversion, you should not
11379 expect any macros provided by the Autoconf package to work until you
11380 have restored the diversion stack back to its original state.
11382 In the rare case that it is necessary to write a macro that explicitly
11383 outputs text to a different diversion, it is important to be aware of an
11384 M4 limitation regarding diversions: text only goes to a diversion if it
11385 is not part of argument collection.  Therefore, any macro that changes
11386 the current diversion cannot be used as an unquoted argument to another
11387 macro, but must be expanded at the top level.  The macro
11388 @code{m4_expand} will diagnose any attempt to change diversions, since
11389 it is generally useful only as an argument to another macro.  The
11390 following example shows what happens when diversion manipulation is
11391 attempted within macro arguments:
11393 @example
11394 m4_do([normal text]
11395 m4_divert_push([KILL])unwanted[]m4_divert_pop([KILL])
11396 [m4_divert_push([KILL])discarded[]m4_divert_pop([KILL])])dnl
11397 @result{}normal text
11398 @result{}unwanted
11399 @end example
11401 @noindent
11402 Notice that the unquoted text @code{unwanted} is output, even though it
11403 was processed while the current diversion was @code{KILL}, because it
11404 was collected as part of the argument to @code{m4_do}.  However, the
11405 text @code{discarded} disappeared as desired, because the diversion
11406 changes were single-quoted, and were not expanded until the top-level
11407 rescan of the output of @code{m4_do}.
11409 To make diversion management easier, M4sugar uses the concept of named
11410 diversions.  Rather than using diversion numbers directly, it is nicer
11411 to associate a name with each diversion.  The diversion number associated
11412 with a particular diversion name is an implementation detail, and a
11413 syntax warning is issued if a diversion number is used instead of a
11414 name.  In general, you should not output text
11415 to a named diversion until after calling the appropriate initialization
11416 routine for your language (@code{m4_init}, @code{AS_INIT},
11417 @code{AT_INIT}, @dots{}), although there are some exceptions documented
11418 below.
11420 M4sugar defines two named diversions.
11421 @table @code
11422 @item KILL
11423 Text written to this diversion is discarded.  This is the default
11424 diversion once M4sugar is initialized.
11425 @item GROW
11426 This diversion is used behind the scenes by topological sorting macros,
11427 such as @code{AC_REQUIRE}.
11428 @end table
11430 M4sh adds several more named diversions.
11431 @table @code
11432 @item BINSH
11433 This diversion is reserved for the @samp{#!} interpreter line.
11434 @item HEADER-REVISION
11435 This diversion holds text from @code{AC_REVISION}.
11436 @item HEADER-COMMENT
11437 This diversion holds comments about the purpose of a file.
11438 @item HEADER-COPYRIGHT
11439 This diversion is managed by @code{AC_COPYRIGHT}.
11440 @item M4SH-SANITIZE
11441 This diversion contains M4sh sanitization code, used to ensure M4sh is
11442 executing in a reasonable shell environment.
11443 @item M4SH-INIT
11444 This diversion contains M4sh initialization code, initializing variables
11445 that are required by other M4sh macros.
11446 @item BODY
11447 This diversion contains the body of the shell code, and is the default
11448 diversion once M4sh is initialized.
11449 @end table
11451 Autotest inherits diversions from M4sh, and changes the default
11452 diversion from @code{BODY} back to @code{KILL}.  It also adds several
11453 more named diversions, with the following subset designed for developer
11454 use.
11455 @table @code
11456 @item PREPARE_TESTS
11457 This diversion contains initialization sequences which are executed
11458 after @file{atconfig} and @file{atlocal}, and after all command line
11459 arguments have been parsed, but prior to running any tests.  It can be
11460 used to set up state that is required across all tests.  This diversion
11461 will work even before @code{AT_INIT}.
11462 @end table
11464 Autoconf inherits diversions from M4sh, and adds the following named
11465 diversions which developers can utilize.
11466 @table @code
11467 @item DEFAULTS
11468 This diversion contains shell variable assignments to set defaults that
11469 must be in place before arguments are parsed.  This diversion is placed
11470 early enough in @file{configure} that it is unsafe to expand any
11471 autoconf macros into this diversion.
11472 @item HELP_ENABLE
11473 If @code{AC_PRESERVE_HELP_ORDER} was used, then text placed in this
11474 diversion will be included as part of a quoted here-doc providing all of
11475 the @option{--help} output of @file{configure} related to options
11476 created by @code{AC_ARG_WITH} and @code{AC_ARG_ENABLE}.
11477 @item INIT_PREPARE
11478 This diversion occurs after all command line options have been parsed,
11479 but prior to the main body of the @file{configure} script.  This
11480 diversion is the last chance to insert shell code such as variable
11481 assignments or shell function declarations that will used by the
11482 expansion of other macros.
11483 @end table
11485 For now, the remaining named diversions of Autoconf, Autoheader, and
11486 Autotest are not documented.  In other words,
11487 intentionally outputting text into an undocumented diversion is subject
11488 to breakage in a future release of Autoconf.
11490 @defmac m4_cleardivert (@var{diversion}@dots{})
11491 @msindex{cleardivert}
11492 Permanently discard any text that has been diverted into
11493 @var{diversion}.
11494 @end defmac
11496 @defmac m4_divert_once (@var{diversion}, @ovar{content})
11497 @msindex{divert_once}
11498 Similar to @code{m4_divert_text}, except that @var{content} is only
11499 output to @var{diversion} if this is the first time that
11500 @code{m4_divert_once} has been called with its particular arguments.
11501 @end defmac
11503 @defmac m4_divert_pop (@ovar{diversion})
11504 @msindex{divert_pop}
11505 If provided, check that the current diversion is indeed @var{diversion}.
11506 Then change to the diversion located earlier on the stack, giving an
11507 error if an attempt is made to pop beyond the initial m4sugar diversion
11508 of @code{KILL}.
11509 @end defmac
11511 @defmac m4_divert_push (@var{diversion})
11512 @msindex{divert_push}
11513 Remember the former diversion on the diversion stack, and output
11514 subsequent text into @var{diversion}.  M4sugar maintains a diversion
11515 stack, and issues an error if there is not a matching pop for every
11516 push.
11517 @end defmac
11519 @defmac m4_divert_text (@var{diversion}, @ovar{content})
11520 @msindex{divert_text}
11521 Output @var{content} and a newline into @var{diversion}, without
11522 affecting the current diversion.  Shorthand for:
11523 @example
11524 m4_divert_push([@var{diversion}])@var{content}
11525 m4_divert_pop([@var{diversion}])dnl
11526 @end example
11528 One use of @code{m4_divert_text} is to develop two related macros, where
11529 macro @samp{MY_A} does the work, but adjusts what work is performed
11530 based on whether the optional macro @samp{MY_B} has also been expanded.
11531 Of course, it is possible to use @code{AC_BEFORE} within @code{MY_A} to
11532 require that @samp{MY_B} occurs first, if it occurs at all.  But this
11533 imposes an ordering restriction on the user; it would be nicer if macros
11534 @samp{MY_A} and @samp{MY_B} can be invoked in either order.  The trick
11535 is to let @samp{MY_B} leave a breadcrumb in an early diversion, which
11536 @samp{MY_A} can then use to determine whether @samp{MY_B} has been
11537 expanded.
11539 @example
11540 AC_DEFUN([MY_A],
11541 [# various actions
11542 if test -n "$b_was_used"; then
11543   # extra action
11544 fi])
11545 AC_DEFUN([MY_B],
11546 [AC_REQUIRE([MY_A])dnl
11547 m4_divert_text([INIT_PREPARE], [b_was_used=true])])
11548 @end example
11550 @end defmac
11552 @defmac m4_init
11553 @msindex{init}
11554 Initialize the M4sugar environment, setting up the default named
11555 diversion to be @code{KILL}.
11556 @end defmac
11558 @node Conditional constructs
11559 @subsection Conditional constructs
11561 The following macros provide additional conditional constructs as
11562 convenience wrappers around @code{m4_if}.
11564 @defmac m4_bmatch (@var{string}, @var{regex-1}, @var{value-1}, @
11565   @ovar{regex-2}, @ovar{value-2}, @dots{}, @ovar{default})
11566 @msindex{bmatch}
11567 The string @var{string} is repeatedly compared against a series of
11568 @var{regex} arguments; if a match is found, the expansion is the
11569 corresponding @var{value}, otherwise, the macro moves on to the next
11570 @var{regex}.  If no @var{regex} match, then the result is the optional
11571 @var{default}, or nothing.
11572 @end defmac
11574 @defmac m4_bpatsubsts (@var{string}, @var{regex-1}, @var{subst-1}, @
11575   @ovar{regex-2}, @ovar{subst-2}, @dots{})
11576 @msindex{bpatsubsts}
11577 The string @var{string} is altered by @var{regex-1} and @var{subst-1},
11578 as if by:
11579 @example
11580 m4_bpatsubst([[@var{string}]], [@var{regex}], [@var{subst}])
11581 @end example
11583 @noindent
11584 The result of the substitution is then passed through the next set of
11585 @var{regex} and @var{subst}, and so forth.  An empty @var{subst} implies
11586 deletion of any matched portions in the current string.  Note that this
11587 macro over-quotes @var{string}; this behavior is intentional, so that
11588 the result of each step of the recursion remains as a quoted string.
11589 However, it means that anchors (@samp{^} and @samp{$} in the @var{regex}
11590 will line up with the extra quotations, and not the characters of the
11591 original string.  The overquoting is removed after the final
11592 substitution.
11593 @end defmac
11595 @defmac m4_case (@var{string}, @var{value-1}, @var{if-value-1}, @
11596   @ovar{value-2}, @ovar{if-value-2}, @dots{}, @ovar{default})
11597 @msindex{case}
11598 Test @var{string} against multiple @var{value} possibilities, resulting
11599 in the first @var{if-value} for a match, or in the optional
11600 @var{default}.  This is shorthand for:
11601 @example
11602 m4_if([@var{string}], [@var{value-1}], [@var{if-value-1}],
11603       [@var{string}], [@var{value-2}], [@var{if-value-2}], @dots{},
11604       [@var{default}])
11605 @end example
11606 @end defmac
11608 @defmac m4_cond (@var{test-1}, @var{value-1}, @var{if-value-1}, @
11609   @ovar{test-2}, @ovar{value-2}, @ovar{if-value-2}, @dots{}, @ovar{default})
11610 @msindex{cond}
11611 This macro was introduced in Autoconf 2.62.  Similar to @code{m4_if},
11612 except that each @var{test} is expanded only when it is encountered.
11613 This is useful for short-circuiting expensive tests; while @code{m4_if}
11614 requires all its strings to be expanded up front before doing
11615 comparisons, @code{m4_cond} only expands a @var{test} when all earlier
11616 tests have failed.
11618 For an example, these two sequences give the same result, but in the
11619 case where @samp{$1} does not contain a backslash, the @code{m4_cond}
11620 version only expands @code{m4_index} once, instead of five times, for
11621 faster computation if this is a common case for @samp{$1}.  Notice that
11622 every third argument is unquoted for @code{m4_if}, and quoted for
11623 @code{m4_cond}:
11625 @example
11626 m4_if(m4_index([$1], [\]), [-1], [$2],
11627       m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
11628       m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
11629       m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
11630       m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
11631       [$2])
11632 m4_cond([m4_index([$1], [\])], [-1], [$2],
11633         [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
11634         [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
11635         [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
11636         [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
11637         [$2])
11638 @end example
11639 @end defmac
11641 @defmac m4_default (@var{expr-1}, @var{expr-2})
11642 @defmacx m4_default_quoted (@var{expr-1}, @var{expr-2})
11643 @defmacx m4_default_nblank (@var{expr-1}, @ovar{expr-2})
11644 @defmacx m4_default_nblank_quoted (@var{expr-1}, @ovar{expr-2})
11645 @msindex{default}
11646 @msindex{default_quoted}
11647 @msindex{default_nblank}
11648 @msindex{default_nblank_quoted}
11649 If @var{expr-1} contains text, use it.  Otherwise, select @var{expr-2}.
11650 @code{m4_default} expands the result, while @code{m4_default_quoted}
11651 does not.  Useful for providing a fixed default if the expression that
11652 results in @var{expr-1} would otherwise be empty.  The difference
11653 between @code{m4_default} and @code{m4_default_nblank} is whether an
11654 argument consisting of just blanks (space, tab, newline) is
11655 significant.  When using the expanding versions, note that an argument
11656 may contain text but still expand to an empty string.
11658 @example
11659 m4_define([active], [ACTIVE])dnl
11660 m4_define([empty], [])dnl
11661 m4_define([demo1], [m4_default([$1], [$2])])dnl
11662 m4_define([demo2], [m4_default_quoted([$1], [$2])])dnl
11663 m4_define([demo3], [m4_default_nblank([$1], [$2])])dnl
11664 m4_define([demo4], [m4_default_nblank_quoted([$1], [$2])])dnl
11665 demo1([active], [default])
11666 @result{}ACTIVE
11667 demo1([], [active])
11668 @result{}ACTIVE
11669 demo1([empty], [text])
11670 @result{}
11671 -demo1([ ], [active])-
11672 @result{}- -
11673 demo2([active], [default])
11674 @result{}active
11675 demo2([], [active])
11676 @result{}active
11677 demo2([empty], [text])
11678 @result{}empty
11679 -demo2([ ], [active])-
11680 @result{}- -
11681 demo3([active], [default])
11682 @result{}ACTIVE
11683 demo3([], [active])
11684 @result{}ACTIVE
11685 demo3([empty], [text])
11686 @result{}
11687 -demo3([ ], [active])-
11688 @result{}-ACTIVE-
11689 demo4([active], [default])
11690 @result{}active
11691 demo4([], [active])
11692 @result{}active
11693 demo4([empty], [text])
11694 @result{}empty
11695 -demo4([ ], [active])-
11696 @result{}-active-
11697 @end example
11698 @end defmac
11700 @defmac m4_ifblank (@var{cond}, @ovar{if-blank}, @ovar{if-text})
11701 @defmacx m4_ifnblank (@var{cond}, @ovar{if-text}, @ovar{if-blank})
11702 @msindex{ifblank}
11703 @msindex{ifnblank}
11704 If @var{cond} is empty or consists only of blanks (space, tab, newline),
11705 then expand @var{if-blank}; otherwise, expand @var{if-text}.  Two
11706 variants exist, in order to make it easier to select the correct logical
11707 sense when using only two parameters.  Note that this is more efficient
11708 than the equivalent behavior of:
11709 @example
11710 m4_ifval(m4_normalize([@var{cond}]), @var{if-text}, @var{if-blank})
11711 @end example
11712 @end defmac
11714 @defmac m4_ifndef (@var{macro}, @var{if-not-defined}, @ovar{if-defined})
11715 @msindex{ifndef}
11716 This is shorthand for:
11717 @example
11718 m4_ifdef([@var{macro}], [@var{if-defined}], [@var{if-not-defined}])
11719 @end example
11720 @end defmac
11722 @defmac m4_ifset (@var{macro}, @ovar{if-true}, @ovar{if-false})
11723 @msindex{ifset}
11724 If @var{macro} is undefined, or is defined as the empty string, expand
11725 to @var{if-false}.  Otherwise, expands to @var{if-true}.  Similar to:
11726 @example
11727 m4_ifval(m4_defn([@var{macro}]), [@var{if-true}], [@var{if-false}])
11728 @end example
11729 @noindent
11730 except that it is not an error if @var{macro} is undefined.
11731 @end defmac
11733 @defmac m4_ifval (@var{cond}, @ovar{if-true}, @ovar{if-false})
11734 @msindex{ifval}
11735 Expands to @var{if-true} if @var{cond} is not empty, otherwise to
11736 @var{if-false}.  This is shorthand for:
11737 @example
11738 m4_if([@var{cond}], [], [@var{if-true}], [@var{if-false}])
11739 @end example
11740 @end defmac
11742 @defmac m4_ifvaln (@var{cond}, @ovar{if-true}, @ovar{if-false})
11743 @msindex{ifvaln}
11744 Similar to @code{m4_ifval}, except guarantee that a newline is present
11745 after any non-empty expansion.  Often followed by @code{dnl}.
11746 @end defmac
11748 @defmac m4_n (@var{text})
11749 @msindex{n}
11750 Expand to @var{text}, and add a newline if @var{text} is not empty.
11751 Often followed by @code{dnl}.
11752 @end defmac
11755 @node Looping constructs
11756 @subsection Looping constructs
11758 The following macros are useful in implementing recursive algorithms in
11759 M4, including loop operations.  An M4 list is formed by quoting a list
11760 of quoted elements; generally the lists are comma-separated, although
11761 @code{m4_foreach_w} is whitespace-separated.  For example, the list
11762 @samp{[[a], [b,c]]} contains two elements: @samp{[a]} and @samp{[b,c]}.
11763 It is common to see lists with unquoted elements when those elements are
11764 not likely to be macro names, as in @samp{[fputc_unlocked,
11765 fgetc_unlocked]}.
11767 Although not generally recommended, it is possible for quoted lists to
11768 have side effects; all side effects are expanded only once, and prior to
11769 visiting any list element.  On the other hand, the fact that unquoted
11770 macros are expanded exactly once means that macros without side effects
11771 can be used to generate lists.  For example,
11773 @example
11774 m4_foreach([i], [[1], [2], [3]m4_errprintn([hi])], [i])
11775 @error{}hi
11776 @result{}123
11777 m4_define([list], [[1], [2], [3]])
11778 @result{}
11779 m4_foreach([i], [list], [i])
11780 @result{}123
11781 @end example
11783 @defmac m4_argn (@var{n}, @ovar{arg}@dots{})
11784 @msindex{argn}
11785 Extracts argument @var{n} (larger than 0) from the remaining arguments.
11786 If there are too few arguments, the empty string is used.  For any
11787 @var{n} besides 1, this is more efficient than the similar
11788 @samp{m4_car(m4_shiftn([@var{n}], [], [@var{arg}@dots{}]))}.
11789 @end defmac
11791 @defmac m4_car (@var{arg}@dots{})
11792 @msindex{car}
11793 Expands to the quoted first @var{arg}.  Can be used with @code{m4_cdr}
11794 to recursively iterate
11795 through a list.  Generally, when using quoted lists of quoted elements,
11796 @code{m4_car} should be called without any extra quotes.
11797 @end defmac
11799 @defmac m4_cdr (@var{arg}@dots{})
11800 @msindex{cdr}
11801 Expands to a quoted list of all but the first @var{arg}, or the empty
11802 string if there was only one argument.  Generally, when using quoted
11803 lists of quoted elements, @code{m4_cdr} should be called without any
11804 extra quotes.
11806 For example, this is a simple implementation of @code{m4_map}; note how
11807 each iteration checks for the end of recursion, then merely applies the
11808 first argument to the first element of the list, then repeats with the
11809 rest of the list.  (The actual implementation in M4sugar is a bit more
11810 involved, to gain some speed and share code with @code{m4_map_sep}, and
11811 also to avoid expanding side effects in @samp{$2} twice).
11812 @example
11813 m4_define([m4_map], [m4_ifval([$2],
11814   [m4_apply([$1], m4_car($2))[]$0([$1], m4_cdr($2))])])dnl
11815 m4_map([ m4_eval], [[[1]], [[1+1]], [[10],[16]]])
11816 @result{} 1 2 a
11817 @end example
11818 @end defmac
11820 @defmac m4_for (@var{var}, @var{first}, @var{last}, @ovar{step}, @
11821   @var{expression})
11822 @msindex{for}
11823 Loop over the numeric values between @var{first} and @var{last}
11824 including bounds by increments of @var{step}.  For each iteration,
11825 expand @var{expression} with the numeric value assigned to @var{var}.
11826 If @var{step} is omitted, it defaults to @samp{1} or @samp{-1} depending
11827 on the order of the limits.  If given, @var{step} has to match this
11828 order.  The number of iterations is determined independently from
11829 definition of @var{var}; iteration cannot be short-circuited or
11830 lengthened by modifying @var{var} from within @var{expression}.
11831 @end defmac
11833 @defmac m4_foreach (@var{var}, @var{list}, @var{expression})
11834 @msindex{foreach}
11835 Loop over the comma-separated M4 list @var{list}, assigning each value
11836 to @var{var}, and expand @var{expression}.  The following example
11837 outputs two lines:
11839 @example
11840 m4_foreach([myvar], [[foo], [bar, baz]],
11841            [echo myvar
11842 ])dnl
11843 @result{}echo foo
11844 @result{}echo bar, baz
11845 @end example
11847 Note that for some forms of @var{expression}, it may be faster to use
11848 @code{m4_map_args}.
11849 @end defmac
11851 @anchor{m4_foreach_w}
11852 @defmac m4_foreach_w (@var{var}, @var{list}, @var{expression})
11853 @msindex{foreach_w}
11854 Loop over the white-space-separated list @var{list}, assigning each value
11855 to @var{var}, and expand @var{expression}.  If @var{var} is only
11856 referenced once in @var{expression}, it is more efficient to use
11857 @code{m4_map_args_w}.
11859 The deprecated macro @code{AC_FOREACH} is an alias of
11860 @code{m4_foreach_w}.
11861 @end defmac
11863 @defmac m4_map (@var{macro}, @var{list})
11864 @defmacx m4_mapall (@var{macro}, @var{list})
11865 @defmacx m4_map_sep (@var{macro}, @var{separator}, @var{list})
11866 @defmacx m4_mapall_sep (@var{macro}, @var{separator}, @var{list})
11867 @msindex{map}
11868 @msindex{mapall}
11869 @msindex{map_sep}
11870 @msindex{mapall_sep}
11871 Loop over the comma separated quoted list of argument descriptions in
11872 @var{list}, and invoke @var{macro} with the arguments.  An argument
11873 description is in turn a comma-separated quoted list of quoted elements,
11874 suitable for @code{m4_apply}.  The macros @code{m4_map} and
11875 @code{m4_map_sep} ignore empty argument descriptions, while
11876 @code{m4_mapall} and @code{m4_mapall_sep} invoke @var{macro} with no
11877 arguments.  The macros @code{m4_map_sep} and @code{m4_mapall_sep}
11878 additionally expand @var{separator} between invocations of @var{macro}.
11880 Note that @var{separator} is expanded, unlike in @code{m4_join}.  When
11881 separating output with commas, this means that the map result can be
11882 used as a series of arguments, by using a single-quoted comma as
11883 @var{separator}, or as a single string, by using a double-quoted comma.
11885 @example
11886 m4_map([m4_count], [])
11887 @result{}
11888 m4_map([ m4_count], [[],
11889                      [[1]],
11890                      [[1], [2]]])
11891 @result{} 1 2
11892 m4_mapall([ m4_count], [[],
11893                         [[1]],
11894                         [[1], [2]]])
11895 @result{} 0 1 2
11896 m4_map_sep([m4_eval], [,], [[[1+2]],
11897                             [[10], [16]]])
11898 @result{}3,a
11899 m4_map_sep([m4_echo], [,], [[[a]], [[b]]])
11900 @result{}a,b
11901 m4_count(m4_map_sep([m4_echo], [,], [[[a]], [[b]]]))
11902 @result{}2
11903 m4_map_sep([m4_echo], [[,]], [[[a]], [[b]]])
11904 @result{}a,b
11905 m4_count(m4_map_sep([m4_echo], [[,]], [[[a]], [[b]]]))
11906 @result{}1
11907 @end example
11908 @end defmac
11910 @defmac m4_map_args (@var{macro}, @var{arg}@dots{})
11911 @msindex{map_args}
11912 Repeatedly invoke @var{macro} with each successive @var{arg} as its only
11913 argument.  In the following example, three solutions are presented with
11914 the same expansion; the solution using @code{m4_map_args} is the most
11915 efficient.
11916 @example
11917 m4_define([active], [ACTIVE])dnl
11918 m4_foreach([var], [[plain], [active]], [ m4_echo(m4_defn([var]))])
11919 @result{} plain active
11920 m4_map([ m4_echo], [[[plain]], [[active]]])
11921 @result{} plain active
11922 m4_map_args([ m4_echo], [plain], [active])
11923 @result{} plain active
11924 @end example
11926 In cases where it is useful to operate on additional parameters besides
11927 the list elements, the macro @code{m4_curry} can be used in @var{macro}
11928 to supply the argument currying necessary to generate the desired
11929 argument list.  In the following example, @code{list_add_n} is more
11930 efficient than @code{list_add_x}.  On the other hand, using
11931 @code{m4_map_args_sep} can be even more efficient.
11933 @example
11934 m4_define([list], [[1], [2], [3]])dnl
11935 m4_define([add], [m4_eval(([$1]) + ([$2]))])dnl
11936 dnl list_add_n(N, ARG...)
11937 dnl Output a list consisting of each ARG added to N
11938 m4_define([list_add_n],
11939 [m4_shift(m4_map_args([,m4_curry([add], [$1])], m4_shift($@@)))])dnl
11940 list_add_n([1], list)
11941 @result{}2,3,4
11942 list_add_n([2], list)
11943 @result{}3,4,5
11944 m4_define([list_add_x],
11945 [m4_shift(m4_foreach([var], m4_dquote(m4_shift($@@)),
11946   [,add([$1],m4_defn([var]))]))])dnl
11947 list_add_x([1], list)
11948 @result{}2,3,4
11949 @end example
11950 @end defmac
11952 @defmac m4_map_args_pair (@var{macro}, @dvar{macro-end, macro}, @
11953   @var{arg}@dots{})
11954 @msindex{map_args_pair}
11955 For every pair of arguments @var{arg}, invoke @var{macro} with two
11956 arguments.  If there is an odd number of arguments, invoke
11957 @var{macro-end}, which defaults to @var{macro}, with the remaining
11958 argument.
11960 @example
11961 m4_map_args_pair([, m4_reverse], [], [1], [2], [3])
11962 @result{}, 2, 1, 3
11963 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3])
11964 @result{}, 2, 1, [3]
11965 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3], [4])
11966 @result{}, 2, 1, 4, 3
11967 @end example
11968 @end defmac
11970 @defmac m4_map_args_sep (@ovar{pre}, @ovar{post}, @ovar{sep}, @var{arg}@dots{})
11971 @msindex{map_args_sep}
11972 Expand the sequence @code{@var{pre}[@var{arg}]@var{post}} for each
11973 argument, additionally expanding @var{sep} between arguments.  One
11974 common use of this macro is constructing a macro call, where the opening
11975 and closing parentheses are split between @var{pre} and @var{post}; in
11976 particular, @code{m4_map_args([@var{macro}], [@var{arg}])} is equivalent
11977 to @code{m4_map_args_sep([@var{macro}(], [)], [], [@var{arg}])}.  This
11978 macro provides the most efficient means for iterating over an arbitrary
11979 list of arguments, particularly when repeatedly constructing a macro
11980 call with more arguments than @var{arg}.
11981 @end defmac
11983 @defmac m4_map_args_w (@var{string}, @ovar{pre}, @ovar{post}, @ovar{sep})
11984 @msindex{map_args_w}
11985 Expand the sequence @code{@var{pre}[word]@var{post}} for each word in
11986 the whitespace-separated @var{string}, additionally expanding @var{sep}
11987 between words.  This macro provides the most efficient means for
11988 iterating over a whitespace-separated string.  In particular,
11989 @code{m4_map_args_w([@var{string}], [@var{action}(], [)])} is more
11990 efficient than @code{m4_foreach_w([var], [@var{string}],
11991 [@var{action}(m4_defn([var]))])}.
11992 @end defmac
11994 @defmac m4_shiftn (@var{count}, @dots{})
11995 @defmacx m4_shift2 (@dots{})
11996 @defmacx m4_shift3 (@dots{})
11997 @msindex{shift2}
11998 @msindex{shift3}
11999 @msindex{shiftn}
12000 @code{m4_shiftn} performs @var{count} iterations of @code{m4_shift},
12001 along with validation that enough arguments were passed in to match the
12002 shift count, and that the count is positive.  @code{m4_shift2} and
12003 @code{m4_shift3} are specializations
12004 of @code{m4_shiftn}, introduced in Autoconf 2.62, and are more efficient
12005 for two and three shifts, respectively.
12006 @end defmac
12008 @defmac m4_stack_foreach (@var{macro}, @var{action})
12009 @defmacx m4_stack_foreach_lifo (@var{macro}, @var{action})
12010 @msindex{stack_foreach}
12011 @msindex{stack_foreach_lifo}
12012 For each of the @code{m4_pushdef} definitions of @var{macro}, expand
12013 @var{action} with the single argument of a definition of @var{macro}.
12014 @code{m4_stack_foreach} starts with the oldest definition, while
12015 @code{m4_stack_foreach_lifo} starts with the current definition.
12016 @var{action} should not push or pop definitions of @var{macro}, nor is
12017 there any guarantee that the current definition of @var{macro} matches
12018 the argument that was passed to @var{action}.  The macro @code{m4_curry}
12019 can be used if @var{action} needs more than one argument, although in
12020 that case it is more efficient to use @var{m4_stack_foreach_sep}.
12022 Due to technical limitations, there are a few low-level m4sugar
12023 functions, such as @code{m4_pushdef}, that cannot be used as the
12024 @var{macro} argument.
12026 @example
12027 m4_pushdef([a], [1])m4_pushdef([a], [2])dnl
12028 m4_stack_foreach([a], [ m4_incr])
12029 @result{} 2 3
12030 m4_stack_foreach_lifo([a], [ m4_curry([m4_substr], [abcd])])
12031 @result{} cd bcd
12032 @end example
12033 @end defmac
12035 @defmac m4_stack_foreach_sep (@var{macro}, @ovar{pre}, @ovar{post}, @ovar{sep})
12036 @defmacx m4_stack_foreach_sep_lifo (@var{macro}, @ovar{pre}, @ovar{post}, @
12037   @ovar{sep})
12038 @msindex{stack_foreach_sep}
12039 @msindex{stack_foreach_sep_lifo}
12040 Expand the sequence @code{@var{pre}[definition]@var{post}} for each
12041 @code{m4_pushdef} definition of @var{macro}, additionally expanding
12042 @var{sep} between definitions.  @code{m4_stack_foreach_sep} visits the
12043 oldest definition first, while @code{m4_stack_foreach_sep_lifo} visits
12044 the current definition first.  This macro provides the most efficient
12045 means for iterating over a pushdef stack.  In particular,
12046 @code{m4_stack_foreach([@var{macro}], [@var{action}])} is short for
12047 @code{m4_stack_foreach_sep([@var{macro}], [@var{action}(], [)])}.
12048 @end defmac
12050 @node Evaluation Macros
12051 @subsection Evaluation Macros
12053 The following macros give some control over the order of the evaluation
12054 by adding or removing levels of quotes.
12056 @defmac m4_apply (@var{macro}, @var{list})
12057 @msindex{apply}
12058 Apply the elements of the quoted, comma-separated @var{list} as the
12059 arguments to @var{macro}.  If @var{list} is empty, invoke @var{macro}
12060 without arguments.  Note the difference between @code{m4_indir}, which
12061 expects its first argument to be a macro name but can use names that are
12062 otherwise invalid, and @code{m4_apply}, where @var{macro} can contain
12063 other text, but must end in a valid macro name.
12064 @example
12065 m4_apply([m4_count], [])
12066 @result{}0
12067 m4_apply([m4_count], [[]])
12068 @result{}1
12069 m4_apply([m4_count], [[1], [2]])
12070 @result{}2
12071 m4_apply([m4_join], [[|], [1], [2]])
12072 @result{}1|2
12073 @end example
12074 @end defmac
12076 @defmac m4_count (@var{arg}, @dots{})
12077 @msindex{count}
12078 This macro returns the decimal count of the number of arguments it was
12079 passed.
12080 @end defmac
12082 @defmac m4_curry (@var{macro}, @var{arg}@dots{})
12083 @msindex{curry}
12084 This macro performs argument currying.  The expansion of this macro is
12085 another macro name that expects exactly one argument; that argument is
12086 then appended to the @var{arg} list, and then @var{macro} is expanded
12087 with the resulting argument list.
12089 @example
12090 m4_curry([m4_curry], [m4_reverse], [1])([2])([3])
12091 @result{}3, 2, 1
12092 @end example
12094 Unfortunately, due to a limitation in M4 1.4.x, it is not possible to
12095 pass the definition of a builtin macro as the argument to the output of
12096 @code{m4_curry}; the empty string is used instead of the builtin token.
12097 This behavior is rectified by using M4 1.6 or newer.
12098 @end defmac
12100 @defmac m4_do (@var{arg}, @dots{})
12101 @msindex{do}
12102 This macro loops over its arguments and expands each @var{arg} in
12103 sequence.  Its main use is for readability; it allows the use of
12104 indentation and fewer @code{dnl} to result in the same expansion.  This
12105 macro guarantees that no expansion will be concatenated with subsequent
12106 text; to achieve full concatenation, use @code{m4_unquote(m4_join([],
12107 @var{arg@dots{}}))}.
12109 @example
12110 m4_define([ab],[1])m4_define([bc],[2])m4_define([abc],[3])dnl
12111 m4_do([a],[b])c
12112 @result{}abc
12113 m4_unquote(m4_join([],[a],[b]))c
12114 @result{}3
12115 m4_define([a],[A])m4_define([b],[B])m4_define([c],[C])dnl
12116 m4_define([AB],[4])m4_define([BC],[5])m4_define([ABC],[6])dnl
12117 m4_do([a],[b])c
12118 @result{}ABC
12119 m4_unquote(m4_join([],[a],[b]))c
12120 @result{}3
12121 @end example
12122 @end defmac
12124 @defmac m4_dquote (@var{arg}, @dots{})
12125 @msindex{dquote}
12126 Return the arguments as a quoted list of quoted arguments.
12127 Conveniently, if there is just one @var{arg}, this effectively adds a
12128 level of quoting.
12129 @end defmac
12131 @defmac m4_dquote_elt (@var{arg}, @dots{})
12132 @msindex{dquote_elt}
12133 Return the arguments as a series of double-quoted arguments.  Whereas
12134 @code{m4_dquote} returns a single argument, @code{m4_dquote_elt} returns
12135 as many arguments as it was passed.
12136 @end defmac
12138 @defmac m4_echo (@var{arg}, @dots{})
12139 @msindex{echo}
12140 Return the arguments, with the same level of quoting.  Other than
12141 discarding whitespace after unquoted commas, this macro is a no-op.
12142 @end defmac
12144 @defmac m4_expand (@var{arg})
12145 @msindex{expand}
12146 Return the expansion of @var{arg} as a quoted string.  Whereas
12147 @code{m4_quote} is designed to collect expanded text into a single
12148 argument, @code{m4_expand} is designed to perform one level of expansion
12149 on quoted text.  One distinction is in the treatment of whitespace
12150 following a comma in the original @var{arg}.  Any time multiple
12151 arguments are collected into one with @code{m4_quote}, the M4 argument
12152 collection rules discard the whitespace.  However, with @code{m4_expand},
12153 whitespace is preserved, even after the expansion of macros contained in
12154 @var{arg}.  Additionally, @code{m4_expand} is able to expand text that
12155 would involve an unterminated comment, whereas expanding that same text
12156 as the argument to @code{m4_quote} runs into difficulty in finding the
12157 end of the argument.  Since manipulating diversions during argument
12158 collection is inherently unsafe, @code{m4_expand} issues an error if
12159 @var{arg} attempts to change the current diversion (@pxref{Diversion
12160 support}).
12162 @example
12163 m4_define([active], [ACT, IVE])dnl
12164 m4_define([active2], [[ACT, IVE]])dnl
12165 m4_quote(active, active)
12166 @result{}ACT,IVE,ACT,IVE
12167 m4_expand([active, active])
12168 @result{}ACT, IVE, ACT, IVE
12169 m4_quote(active2, active2)
12170 @result{}ACT, IVE,ACT, IVE
12171 m4_expand([active2, active2])
12172 @result{}ACT, IVE, ACT, IVE
12173 m4_expand([# m4_echo])
12174 @result{}# m4_echo
12175 m4_quote(# m4_echo)
12177 @result{}# m4_echo)
12178 @result{}
12179 @end example
12181 Note that @code{m4_expand} cannot handle an @var{arg} that expands to
12182 literal unbalanced quotes, but that quadrigraphs can be used when
12183 unbalanced output is necessary.  Likewise, unbalanced parentheses should
12184 be supplied with double quoting or a quadrigraph.
12186 @example
12187 m4_define([pattern], [[!@@<:@@]])dnl
12188 m4_define([bar], [BAR])dnl
12189 m4_expand([case $foo in
12190   m4_defn([pattern])@@:@}@@ bar ;;
12191   *[)] blah ;;
12192 esac])
12193 @result{}case $foo in
12194 @result{}  [![]) BAR ;;
12195 @result{}  *) blah ;;
12196 @result{}esac
12197 @end example
12198 @end defmac
12200 @defmac m4_ignore (@dots{})
12201 @msindex{ignore}
12202 This macro was introduced in Autoconf 2.62.  Expands to nothing,
12203 ignoring all of its arguments.  By itself, this isn't very useful.
12204 However, it can be used to conditionally ignore an arbitrary number of
12205 arguments, by deciding which macro name to apply to a list of arguments.
12206 @example
12207 dnl foo outputs a message only if [debug] is defined.
12208 m4_define([foo],
12209 [m4_ifdef([debug],[AC_MSG_NOTICE],[m4_ignore])([debug message])])
12210 @end example
12212 Note that for earlier versions of Autoconf, the macro @code{__gnu__} can
12213 serve the same purpose, although it is less readable.
12214 @end defmac
12216 @defmac m4_make_list (@var{arg}, @dots{})
12217 @msindex{make_list}
12218 This macro exists to aid debugging of M4sugar algorithms.  Its net
12219 effect is similar to @code{m4_dquote}---it produces a quoted list of
12220 quoted arguments, for each @var{arg}.  The difference is that this
12221 version uses a comma-newline separator instead of just comma, to improve
12222 readability of the list; with the result that it is less efficient than
12223 @code{m4_dquote}.
12224 @example
12225 m4_define([zero],[0])m4_define([one],[1])m4_define([two],[2])dnl
12226 m4_dquote(zero, [one], [[two]])
12227 @result{}[0],[one],[[two]]
12228 m4_make_list(zero, [one], [[two]])
12229 @result{}[0],
12230 @result{}[one],
12231 @result{}[[two]]
12232 m4_foreach([number], m4_dquote(zero, [one], [[two]]), [ number])
12233 @result{} 0 1 two
12234 m4_foreach([number], m4_make_list(zero, [one], [[two]]), [ number])
12235 @result{} 0 1 two
12236 @end example
12237 @end defmac
12239 @c m4_noquote is too dangerous to document - it invokes macros that
12240 @c probably rely on @samp{[]} nested quoting for proper operation.  The
12241 @c user should generally prefer m4_unquote instead.
12243 @defmac m4_quote (@var{arg}, @dots{})
12244 @msindex{quote}
12245 Return the arguments as a single entity, i.e., wrap them into a pair of
12246 quotes.  This effectively collapses multiple arguments into one,
12247 although it loses whitespace after unquoted commas in the process.
12248 @end defmac
12250 @defmac m4_reverse (@var{arg}, @dots{})
12251 @msindex{reverse}
12252 Outputs each argument with the same level of quoting, but in reverse
12253 order, and with space following each comma for readability.
12255 @example
12256 m4_define([active], [ACT,IVE])
12257 @result{}
12258 m4_reverse(active, [active])
12259 @result{}active, IVE, ACT
12260 @end example
12261 @end defmac
12263 @defmac m4_unquote (@var{arg}, @dots{})
12264 @msindex{unquote}
12265 This macro was introduced in Autoconf 2.62.  Expand each argument,
12266 separated by commas.  For a single @var{arg}, this effectively removes a
12267 layer of quoting, and @code{m4_unquote([@var{arg}])} is more efficient
12268 than the equivalent @code{m4_do([@var{arg}])}.  For multiple arguments,
12269 this results in an unquoted list of expansions.  This is commonly used
12270 with @code{m4_split}, in order to convert a single quoted list into a
12271 series of quoted elements.
12272 @end defmac
12274 The following example aims at emphasizing the difference between several
12275 scenarios: not using these macros, using @code{m4_defn}, using
12276 @code{m4_quote}, using @code{m4_dquote}, and using @code{m4_expand}.
12278 @example
12279 $ @kbd{cat example.m4}
12280 dnl Overquote, so that quotes are visible.
12281 m4_define([show], [$[]1 = [$1], $[]@@ = [$@@]])
12282 m4_define([a], [A])
12283 m4_define([mkargs], [1, 2[,] 3])
12284 m4_define([arg1], [[$1]])
12285 m4_divert([0])dnl
12286 show(a, b)
12287 show([a, b])
12288 show(m4_quote(a, b))
12289 show(m4_dquote(a, b))
12290 show(m4_expand([a, b]))
12292 arg1(mkargs)
12293 arg1([mkargs])
12294 arg1(m4_defn([mkargs]))
12295 arg1(m4_quote(mkargs))
12296 arg1(m4_dquote(mkargs))
12297 arg1(m4_expand([mkargs]))
12298 $ @kbd{autom4te -l m4sugar example.m4}
12299 $1 = A, $@@ = [A],[b]
12300 $1 = a, b, $@@ = [a, b]
12301 $1 = A,b, $@@ = [A,b]
12302 $1 = [A],[b], $@@ = [[A],[b]]
12303 $1 = A, b, $@@ = [A, b]
12306 mkargs
12307 1, 2[,] 3
12308 1,2, 3
12309 [1],[2, 3]
12310 1, 2, 3
12311 @end example
12314 @node Text processing Macros
12315 @subsection String manipulation in M4
12317 The following macros may be used to manipulate strings in M4.  Many of
12318 the macros in this section intentionally result in quoted strings as
12319 output, rather than subjecting the arguments to further expansions.  As
12320 a result, if you are manipulating text that contains active M4
12321 characters, the arguments are passed with single quoting rather than
12322 double.
12324 @defmac m4_append (@var{macro-name}, @var{string}, @ovar{separator})
12325 @defmacx m4_append_uniq (@var{macro-name}, @var{string}, @ovar{separator} @
12326   @ovar{if-uniq}, @ovar{if-duplicate})
12327 @msindex{append}
12328 @msindex{append_uniq}
12329 Redefine @var{macro-name} to its former contents with @var{separator}
12330 and @var{string} added at the end.  If @var{macro-name} was undefined
12331 before (but not if it was defined but empty), then no @var{separator} is
12332 added.  As of Autoconf 2.62, neither @var{string} nor @var{separator}
12333 are expanded during this macro; instead, they are expanded when
12334 @var{macro-name} is invoked.
12336 @code{m4_append} can be used to grow strings, and @code{m4_append_uniq}
12337 to grow strings without duplicating substrings.  Additionally,
12338 @code{m4_append_uniq} takes two optional parameters as of Autoconf 2.62;
12339 @var{if-uniq} is expanded if @var{string} was appended, and
12340 @var{if-duplicate} is expanded if @var{string} was already present.
12341 Also, @code{m4_append_uniq} warns if @var{separator} is not empty, but
12342 occurs within @var{string}, since that can lead to duplicates.
12344 Note that @code{m4_append} can scale linearly in the length of the final
12345 string, depending on the quality of the underlying M4 implementation,
12346 while @code{m4_append_uniq} has an inherent quadratic scaling factor.
12347 If an algorithm can tolerate duplicates in the final string, use the
12348 former for speed.  If duplicates must be avoided, consider using
12349 @code{m4_set_add} instead (@pxref{Set manipulation Macros}).
12351 @example
12352 m4_define([active], [ACTIVE])dnl
12353 m4_append([sentence], [This is an])dnl
12354 m4_append([sentence], [ active ])dnl
12355 m4_append([sentence], [symbol.])dnl
12356 sentence
12357 @result{}This is an ACTIVE symbol.
12358 m4_undefine([active])dnl
12359 @result{}This is an active symbol.
12360 m4_append_uniq([list], [one], [, ], [new], [existing])
12361 @result{}new
12362 m4_append_uniq([list], [one], [, ], [new], [existing])
12363 @result{}existing
12364 m4_append_uniq([list], [two], [, ], [new], [existing])
12365 @result{}new
12366 m4_append_uniq([list], [three], [, ], [new], [existing])
12367 @result{}new
12368 m4_append_uniq([list], [two], [, ], [new], [existing])
12369 @result{}existing
12370 list
12371 @result{}one, two, three
12372 m4_dquote(list)
12373 @result{}[one],[two],[three]
12374 m4_append([list2], [one], [[, ]])dnl
12375 m4_append_uniq([list2], [two], [[, ]])dnl
12376 m4_append([list2], [three], [[, ]])dnl
12377 list2
12378 @result{}one, two, three
12379 m4_dquote(list2)
12380 @result{}[one, two, three]
12381 @end example
12382 @end defmac
12384 @defmac m4_append_uniq_w (@var{macro-name}, @var{strings})
12385 @msindex{append_uniq_w}
12386 This macro was introduced in Autoconf 2.62.  It is similar to
12387 @code{m4_append_uniq}, but treats @var{strings} as a whitespace
12388 separated list of words to append, and only appends unique words.
12389 @var{macro-name} is updated with a single space between new words.
12390 @example
12391 m4_append_uniq_w([numbers], [1 1 2])dnl
12392 m4_append_uniq_w([numbers], [ 2 3 ])dnl
12393 numbers
12394 @result{}1 2 3
12395 @end example
12396 @end defmac
12398 @defmac m4_chomp (@var{string})
12399 @defmacx m4_chomp_all (@var{string})
12400 @msindex{chomp}
12401 @msindex{chomp_all}
12402 Output @var{string} in quotes, but without a trailing newline.  The
12403 macro @code{m4_chomp} is slightly faster, and removes at most one
12404 newline; the macro @code{m4_chomp_all} removes all consecutive trailing
12405 newlines.  Unlike @code{m4_flatten}, embedded newlines are left intact,
12406 and backslash does not influence the result.
12407 @end defmac
12409 @defmac m4_combine (@ovar{separator}, @var{prefix-list}, @ovar{infix}, @
12410   @var{suffix-1}, @ovar{suffix-2}, @dots{})
12411 @msindex{combine}
12412 This macro produces a quoted string containing the pairwise combination
12413 of every element of the quoted, comma-separated @var{prefix-list}, and
12414 every element from the @var{suffix} arguments.  Each pairwise
12415 combination is joined with @var{infix} in the middle, and successive
12416 pairs are joined by @var{separator}.  No expansion occurs on any of the
12417 arguments.  No output occurs if either the @var{prefix} or @var{suffix}
12418 list is empty, but the lists can contain empty elements.
12419 @example
12420 m4_define([a], [oops])dnl
12421 m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
12422 @result{}a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
12423 m4_combine([, ], [[a], [b]], [-])
12424 @result{}
12425 m4_combine([, ], [[a], [b]], [-], [])
12426 @result{}a-, b-
12427 m4_combine([, ], [], [-], [1], [2])
12428 @result{}
12429 m4_combine([, ], [[]], [-], [1], [2])
12430 @result{}-1, -2
12431 @end example
12432 @end defmac
12434 @defmac m4_escape (@var{string})
12435 @msindex{escape}
12436 Convert all instances of @samp{[}, @samp{]}, @samp{#}, and @samp{$}
12437 within @var{string} into their respective quadrigraphs.  The result is
12438 still a quoted string.
12439 @end defmac
12441 @defmac m4_flatten (@var{string})
12442 @msindex{flatten}
12443 Flatten @var{string} into a single line.  Delete all backslash-newline
12444 pairs, and replace all remaining newlines with a space.  The result is
12445 still a quoted string.
12446 @end defmac
12448 @defmac m4_join (@ovar{separator}, @var{args}@dots{})
12449 @defmacx m4_joinall (@ovar{separator}, @var{args}@dots{})
12450 @msindex{join}
12451 @msindex{joinall}
12452 Concatenate each @var{arg}, separated by @var{separator}.
12453 @code{joinall} uses every argument, while @code{join} omits empty
12454 arguments so that there are no back-to-back separators in the output.
12455 The result is a quoted string.
12456 @example
12457 m4_define([active], [ACTIVE])dnl
12458 m4_join([|], [one], [], [active], [two])
12459 @result{}one|active|two
12460 m4_joinall([|], [one], [], [active], [two])
12461 @result{}one||active|two
12462 @end example
12464 Note that if all you intend to do is join @var{args} with commas between
12465 them, to form a quoted list suitable for @code{m4_foreach}, it is more
12466 efficient to use @code{m4_dquote}.
12467 @end defmac
12469 @defmac m4_newline (@ovar{text})
12470 @msindex{newline}
12471 This macro was introduced in Autoconf 2.62, and expands to a newline,
12472 followed by any @var{text}.
12473 It is primarily useful for maintaining macro formatting, and ensuring
12474 that M4 does not discard leading whitespace during argument collection.
12475 @end defmac
12477 @defmac m4_normalize (@var{string})
12478 @msindex{normalize}
12479 Remove leading and trailing spaces and tabs, sequences of
12480 backslash-then-newline, and replace multiple spaces, tabs, and newlines
12481 with a single space.  This is a combination of @code{m4_flatten} and
12482 @code{m4_strip}.  To determine if @var{string} consists only of bytes
12483 that would be removed by @code{m4_normalize}, you can use
12484 @code{m4_ifblank}.
12485 @end defmac
12487 @defmac m4_re_escape (@var{string})
12488 @msindex{re_escape}
12489 Backslash-escape all characters in @var{string} that are active in
12490 regexps.
12491 @end defmac
12493 @c We cannot use @dvar because the macro expansion mistreats backslashes.
12494 @defmac m4_split (@var{string}, @r{[}@var{regexp} = @samp{[\t ]+}@r{]})
12495 @msindex{split}
12496 Split @var{string} into an M4 list of elements quoted by @samp{[} and
12497 @samp{]}, while keeping white space at the beginning and at the end.
12498 If @var{regexp} is given, use it instead of @samp{[\t ]+} for splitting.
12499 If @var{string} is empty, the result is an empty list.
12500 @end defmac
12502 @defmac m4_strip (@var{string})
12503 @msindex{strip}
12504 Strip whitespace from @var{string}.  Sequences of spaces and tabs are
12505 reduced to a single space, then leading and trailing spaces are removed.
12506 The result is still a quoted string.  Note that this does not interfere
12507 with newlines; if you want newlines stripped as well, consider
12508 @code{m4_flatten}, or do it all at once with @code{m4_normalize}.  To
12509 quickly test if @var{string} has only whitespace, use @code{m4_ifblank}.
12510 @end defmac
12512 @defmac m4_text_box (@var{message}, @dvar{frame, -})
12513 @msindex{text_box}
12514 Add a text box around @var{message}, using @var{frame} as the border
12515 character above and below the message.  The frame correctly accounts for
12516 the subsequent expansion of @var{message}.  For example:
12517 @example
12518 m4_define([macro], [abc])dnl
12519 m4_text_box([macro])
12520 @result{}## --- ##
12521 @result{}## abc ##
12522 @result{}## --- ##
12523 @end example
12525 The @var{message} must contain balanced quotes and parentheses, although
12526 quadrigraphs can be used to work around this.
12527 @end defmac
12529 @defmac m4_text_wrap (@var{string}, @ovar{prefix}, @
12530   @dvar{prefix1, @var{prefix}}, @dvar{width, 79})
12531 @msindex{text_wrap}
12532 Break @var{string} into a series of whitespace-separated words, then
12533 output those words separated by spaces, and wrapping lines any time the
12534 output would exceed @var{width} columns.  If given, @var{prefix1} begins
12535 the first line, and @var{prefix} begins all wrapped lines.  If
12536 @var{prefix1} is longer than @var{prefix}, then the first line consists
12537 of just @var{prefix1}.  If @var{prefix} is longer than @var{prefix1},
12538 padding is inserted so that the first word of @var{string} begins at the
12539 same indentation as all wrapped lines.  Note that using literal tab
12540 characters in any of the arguments will interfere with the calculation
12541 of width.  No expansions occur on @var{prefix}, @var{prefix1}, or the
12542 words of @var{string}, although quadrigraphs are recognized.
12544 For some examples:
12545 @example
12546 m4_text_wrap([Short string */], [   ], [/* ], [20])
12547 @result{}/* Short string */
12548 m4_text_wrap([Much longer string */], [   ], [/* ], [20])
12549 @result{}/* Much longer
12550 @result{}   string */
12551 m4_text_wrap([Short doc.], [          ], [  --short ], [30])
12552 @result{}  --short Short doc.
12553 m4_text_wrap([Short doc.], [          ], [  --too-wide ], [30])
12554 @result{}  --too-wide
12555 @result{}          Short doc.
12556 m4_text_wrap([Super long documentation.], [     ],
12557              [  --too-wide ], 30)
12558 @result{}  --too-wide
12559 @result{}     Super long
12560 @result{}     documentation.
12561 @end example
12562 @end defmac
12564 @defmac m4_tolower (@var{string})
12565 @defmacx m4_toupper (@var{string})
12566 @msindex{tolower}
12567 @msindex{toupper}
12568 Return @var{string} with letters converted to upper or lower case,
12569 respectively.
12570 @end defmac
12572 @node Number processing Macros
12573 @subsection Arithmetic computation in M4
12575 The following macros facilitate integer arithmetic operations.
12576 Where a parameter is documented as taking an arithmetic expression, you
12577 can use anything that can be parsed by @code{m4_eval}.
12579 @defmac m4_cmp (@var{expr-1}, @var{expr-2})
12580 @msindex{cmp}
12581 Compare the arithmetic expressions @var{expr-1} and @var{expr-2}, and
12582 expand to @samp{-1} if @var{expr-1} is smaller, @samp{0} if they are
12583 equal, and @samp{1} if @var{expr-1} is larger.
12584 @end defmac
12586 @defmac m4_list_cmp (@var{list-1}, @var{list-2})
12587 @msindex{list_cmp}
12588 Compare the two M4 lists consisting of comma-separated arithmetic
12589 expressions, left to right.  Expand to @samp{-1} for the first element
12590 pairing where the value from @var{list-1} is smaller, @samp{1} where the
12591 value from @var{list-2} is smaller, or @samp{0} if both lists have the
12592 same values.  If one list is shorter than the other, the remaining
12593 elements of the longer list are compared against zero.
12594 @example
12595 m4_list_cmp([1, 0],       [1])
12596 @result{}0
12597 m4_list_cmp([1, [1 * 0]], [1, 0])
12598 @result{}0
12599 m4_list_cmp([1, 2],       [1, 0])
12600 @result{}1
12601 m4_list_cmp([1, [1+1], 3],[1, 2])
12602 @result{}1
12603 m4_list_cmp([1, 2, -3],   [1, 2])
12604 @result{}-1
12605 m4_list_cmp([1, 0],       [1, 2])
12606 @result{}-1
12607 m4_list_cmp([1],          [1, 2])
12608 @result{}-1
12609 @end example
12610 @end defmac
12612 @defmac m4_max (@var{arg}, @dots{})
12613 @msindex{max}
12614 This macro was introduced in Autoconf 2.62.  Expand to the decimal value
12615 of the maximum arithmetic expression among all the arguments.
12616 @end defmac
12618 @defmac m4_min (@var{arg}, @dots{})
12619 @msindex{min}
12620 This macro was introduced in Autoconf 2.62.  Expand to the decimal value
12621 of the minimum arithmetic expression among all the arguments.
12622 @end defmac
12624 @defmac m4_sign (@var{expr})
12625 @msindex{sign}
12626 Expand to @samp{-1} if the arithmetic expression @var{expr} is negative,
12627 @samp{1} if it is positive, and @samp{0} if it is zero.
12628 @end defmac
12630 @anchor{m4_version_compare}
12631 @defmac m4_version_compare (@var{version-1}, @var{version-2})
12632 @msindex{version_compare}
12633 This macro was introduced in Autoconf 2.53, but had a number of
12634 usability limitations that were not lifted until Autoconf 2.62.  Compare
12635 the version strings @var{version-1} and @var{version-2}, and expand to
12636 @samp{-1} if @var{version-1} is smaller, @samp{0} if they are the same,
12637 or @samp{1} @var{version-2} is smaller.  Version strings must be a list
12638 of elements separated by @samp{.}, @samp{,} or @samp{-}, where each
12639 element is a number along with optional case-insensitive letters
12640 designating beta releases.  The comparison stops at the leftmost element
12641 that contains a difference, although a 0 element compares equal to a
12642 missing element.
12644 It is permissible to include commit identifiers in @var{version}, such
12645 as an abbreviated SHA1 of the commit, provided there is still a
12646 monotonically increasing prefix to allow for accurate version-based
12647 comparisons.  For example, this paragraph was written when the
12648 development snapshot of autoconf claimed to be at version
12649 @samp{2.61a-248-dc51}, or 248 commits after the 2.61a release, with an
12650 abbreviated commit identification of @samp{dc51}.
12652 @example
12653 m4_version_compare([1.1], [2.0])
12654 @result{}-1
12655 m4_version_compare([2.0b], [2.0a])
12656 @result{}1
12657 m4_version_compare([1.1.1], [1.1.1a])
12658 @result{}-1
12659 m4_version_compare([1.2], [1.1.1a])
12660 @result{}1
12661 m4_version_compare([1.0], [1])
12662 @result{}0
12663 m4_version_compare([1.1pre], [1.1PRE])
12664 @result{}0
12665 m4_version_compare([1.1a], [1,10])
12666 @result{}-1
12667 m4_version_compare([2.61a], [2.61a-248-dc51])
12668 @result{}-1
12669 m4_version_compare([2.61b], [2.61a-248-dc51])
12670 @result{}1
12671 @end example
12672 @end defmac
12674 @defmac m4_version_prereq (@var{version}, @ovar{if-new-enough}, @
12675   @dvar{if-old, m4_fatal})
12676 @msindex{version_prereq}
12677 Compares @var{version} against the version of Autoconf currently
12678 running.  If the running version is at @var{version} or newer, expand
12679 @var{if-new-enough}, but if @var{version} is larger than the version
12680 currently executing, expand @var{if-old}, which defaults to printing an
12681 error message and exiting m4sugar with status 63.  When given only one
12682 argument, this behaves like @code{AC_PREREQ} (@pxref{Versioning}).
12683 Remember that the autoconf philosophy favors feature checks over version
12684 checks.
12685 @end defmac
12687 @node Set manipulation Macros
12688 @subsection Set manipulation in M4
12689 @cindex Set manipulation
12690 @cindex Data structure, set
12691 @cindex Unordered set manipulation
12693 Sometimes, it is necessary to track a set of data, where the order does
12694 not matter and where there are no duplicates in the set.  The following
12695 macros facilitate set manipulations.  Each set is an opaque object,
12696 which can only be accessed via these basic operations.  The underlying
12697 implementation guarantees linear scaling for set creation, which is more
12698 efficient than using the quadratic @code{m4_append_uniq}.  Both set
12699 names and values can be arbitrary strings, except for unbalanced quotes.
12700 This implementation ties up memory for removed elements until the next
12701 operation that must traverse all the elements of a set; and although
12702 that may slow down some operations until the memory for removed elements
12703 is pruned, it still guarantees linear performance.
12705 @defmac m4_set_add (@var{set}, @var{value}, @ovar{if-uniq}, @ovar{if-dup})
12706 @msindex{set_add}
12707 Adds the string @var{value} as a member of set @var{set}.  Expand
12708 @var{if-uniq} if the element was added, or @var{if-dup} if it was
12709 previously in the set.  Operates in amortized constant time, so that set
12710 creation scales linearly.
12711 @end defmac
12713 @defmac m4_set_add_all (@var{set}, @var{value}@dots{})
12714 @msindex{set_add_all}
12715 Adds each @var{value} to the set @var{set}.  This is slightly more
12716 efficient than repeatedly invoking @code{m4_set_add}.
12717 @end defmac
12719 @defmac m4_set_contains (@var{set}, @var{value}, @ovar{if-present}, @
12720  @ovar{if-absent})
12721 @msindex{set_contains}
12722 Expands @var{if-present} if the string @var{value} is a member of
12723 @var{set}, otherwise @var{if-absent}.
12725 @example
12726 m4_set_contains([a], [1], [yes], [no])
12727 @result{}no
12728 m4_set_add([a], [1], [added], [dup])
12729 @result{}added
12730 m4_set_add([a], [1], [added], [dup])
12731 @result{}dup
12732 m4_set_contains([a], [1], [yes], [no])
12733 @result{}yes
12734 m4_set_remove([a], [1], [removed], [missing])
12735 @result{}removed
12736 m4_set_contains([a], [1], [yes], [no])
12737 @result{}no
12738 m4_set_remove([a], [1], [removed], [missing])
12739 @result{}missing
12740 @end example
12741 @end defmac
12743 @defmac m4_set_contents (@var{set}, @ovar{sep})
12744 @defmacx m4_set_dump (@var{set}, @ovar{sep})
12745 @msindex{set_contents}
12746 @msindex{set_dump}
12747 Expands to a single string consisting of all the members of the set
12748 @var{set}, each separated by @var{sep}, which is not expanded.
12749 @code{m4_set_contents} leaves the elements in @var{set} but reclaims any
12750 memory occupied by removed elements, while @code{m4_set_dump} is a
12751 faster one-shot action that also deletes the set.  No provision is made
12752 for disambiguating members that contain a non-empty @var{sep} as a
12753 substring; use @code{m4_set_empty} to distinguish between an empty set
12754 and the set containing only the empty string.  The order of the output
12755 is unspecified; in the current implementation, part of the speed of
12756 @code{m4_set_dump} results from using a different output order than
12757 @code{m4_set_contents}.  These macros scale linearly in the size of the
12758 set before memory pruning, and @code{m4_set_contents([@var{set}],
12759 [@var{sep}])} is faster than
12760 @code{m4_joinall([@var{sep}]m4_set_listc([@var{set}]))}.
12762 @example
12763 m4_set_add_all([a], [1], [2], [3])
12764 @result{}
12765 m4_set_contents([a], [-])
12766 @result{}1-2-3
12767 m4_joinall([-]m4_set_listc([a]))
12768 @result{}1-2-3
12769 m4_set_dump([a], [-])
12770 @result{}3-2-1
12771 m4_set_contents([a])
12772 @result{}
12773 m4_set_add([a], [])
12774 @result{}
12775 m4_set_contents([a], [-])
12776 @result{}
12777 @end example
12778 @end defmac
12780 @defmac m4_set_delete (@var{set})
12781 @msindex{set_delete}
12782 Delete all elements and memory associated with @var{set}.  This is
12783 linear in the set size, and faster than removing one element at a time.
12784 @end defmac
12786 @defmac m4_set_difference (@var{seta}, @var{setb})
12787 @defmacx m4_set_intersection (@var{seta}, @var{setb})
12788 @defmacx m4_set_union (@var{seta}, @var{setb})
12789 @msindex{set_difference}
12790 @msindex{set_intersection}
12791 @msindex{set_union}
12792 Compute the relation between @var{seta} and @var{setb}, and output the
12793 result as a list of quoted arguments without duplicates and with a
12794 leading comma.  Set difference selects the elements in @var{seta} but
12795 not @var{setb}, intersection selects only elements in both sets, and
12796 union selects elements in either set.  These actions are linear in the
12797 sum of the set sizes.  The leading comma is necessary to distinguish
12798 between no elements and the empty string as the only element.
12800 @example
12801 m4_set_add_all([a], [1], [2], [3])
12802 @result{}
12803 m4_set_add_all([b], [3], [], [4])
12804 @result{}
12805 m4_set_difference([a], [b])
12806 @result{},1,2
12807 m4_set_difference([b], [a])
12808 @result{},,4
12809 m4_set_intersection([a], [b])
12810 @result{},3
12811 m4_set_union([a], [b])
12812 @result{},1,2,3,,4
12813 @end example
12814 @end defmac
12816 @defmac m4_set_empty (@var{set}, @ovar{if-empty}, @ovar{if-elements})
12817 @msindex{set_empty}
12818 Expand @var{if-empty} if the set @var{set} has no elements, otherwise
12819 expand @var{if-elements}.  This macro operates in constant time.  Using
12820 this macro can help disambiguate output from @code{m4_set_contents} or
12821 @code{m4_set_list}.
12822 @end defmac
12824 @defmac m4_set_foreach (@var{set}, @var{variable}, @var{action})
12825 @msindex{set_foreach}
12826 For each element in the set @var{set}, expand @var{action} with the
12827 macro @var{variable} defined as the set element.  Behavior is
12828 unspecified if @var{action} recursively lists the contents of @var{set}
12829 (although listing other sets is acceptable), or if it modifies the set
12830 in any way other than removing the element currently contained in
12831 @var{variable}.  This macro is faster than the corresponding
12832 @code{m4_foreach([@var{variable}],
12833 m4_indir([m4_dquote]m4_set_listc([@var{set}])), [@var{action}])},
12834 although @code{m4_set_map} might be faster still.
12836 @example
12837 m4_set_add_all([a]m4_for([i], [1], [5], [], [,i]))
12838 @result{}
12839 m4_set_contents([a])
12840 @result{}12345
12841 m4_set_foreach([a], [i],
12842   [m4_if(m4_eval(i&1), [0], [m4_set_remove([a], i, [i])])])
12843 @result{}24
12844 m4_set_contents([a])
12845 @result{}135
12846 @end example
12847 @end defmac
12849 @defmac m4_set_list (@var{set})
12850 @defmacx m4_set_listc (@var{set})
12851 @msindex{set_list}
12852 @msindex{set_listc}
12853 Produce a list of arguments, where each argument is a quoted element
12854 from the set @var{set}.  The variant @code{m4_set_listc} is unambiguous,
12855 by adding a leading comma if there are any set elements, whereas the
12856 variant @code{m4_set_list} cannot distinguish between an empty set and a
12857 set containing only the empty string.  These can be directly used in
12858 macros that take multiple arguments, such as @code{m4_join} or
12859 @code{m4_set_add_all}, or wrapped by @code{m4_dquote} for macros that
12860 take a quoted list, such as @code{m4_map} or @code{m4_foreach}.  Any
12861 memory occupied by removed elements is reclaimed during these macros.
12863 @example
12864 m4_set_add_all([a], [1], [2], [3])
12865 @result{}
12866 m4_set_list([a])
12867 @result{}1,2,3
12868 m4_set_list([b])
12869 @result{}
12870 m4_set_listc([b])
12871 @result{}
12872 m4_count(m4_set_list([b]))
12873 @result{}1
12874 m4_set_empty([b], [0], [m4_count(m4_set_list([b]))])
12875 @result{}0
12876 m4_set_add([b], [])
12877 @result{}
12878 m4_set_list([b])
12879 @result{}
12880 m4_set_listc([b])
12881 @result{},
12882 m4_count(m4_set_list([b]))
12883 @result{}1
12884 m4_set_empty([b], [0], [m4_count(m4_set_list([b]))])
12885 @result{}1
12886 @end example
12887 @end defmac
12889 @defmac m4_set_map (@var{set}, @var{action})
12890 @msindex{set_map}
12891 For each element in the set @var{set}, expand @var{action} with a single
12892 argument of the set element.  Behavior is unspecified if @var{action}
12893 recursively lists the contents of @var{set} (although listing other sets
12894 is acceptable), or if it modifies the set in any way other than removing
12895 the element passed as an argument.  This macro is faster than either
12896 corresponding counterpart of
12897 @code{m4_map_args([@var{action}]m4_set_listc([@var{set}]))} or
12898 @code{m4_set_foreach([@var{set}], [var],
12899 [@var{action}(m4_defn([var]))])}.  It is possible to use @code{m4_curry}
12900 if more than one argument is needed for @var{action}, although it is
12901 more efficient to use @code{m4_set_map_sep} in that case.
12902 @end defmac
12904 @defmac m4_set_map_sep (@var{set}, @ovar{pre}, @ovar{post}, @ovar{sep})
12905 @msindex{set_map_sep}
12906 For each element in the set @var{set}, expand
12907 @code{@var{pre}[element]@var{post}}, additionally expanding @var{sep}
12908 between elements.  Behavior is unspecified if the expansion recursively
12909 lists the contents of @var{set} (although listing other sets
12910 is acceptable), or if it modifies the set in any way other than removing
12911 the element visited by the expansion.  This macro provides the most
12912 efficient means for non-destructively visiting the elements of a set; in
12913 particular, @code{m4_set_map([@var{set}], [@var{action}])} is equivalent
12914 to @code{m4_set_map_sep([@var{set}], [@var{action}(], [)])}.
12915 @end defmac
12917 @defmac m4_set_remove (@var{set}, @var{value}, @ovar{if-present}, @
12918  @ovar{if-absent})
12919 @msindex{set_remove}
12920 If @var{value} is an element in the set @var{set}, then remove it and
12921 expand @var{if-present}.  Otherwise expand @var{if-absent}.  This macro
12922 operates in constant time so that multiple removals will scale linearly
12923 rather than quadratically; but when used outside of
12924 @code{m4_set_foreach} or @code{m4_set_map}, it leaves memory occupied
12925 until the set is later
12926 compacted by @code{m4_set_contents} or @code{m4_set_list}.  Several
12927 other set operations are then less efficient between the time of element
12928 removal and subsequent memory compaction, but still maintain their
12929 guaranteed scaling performance.
12930 @end defmac
12932 @defmac m4_set_size (@var{set})
12933 @msindex{set_size}
12934 Expand to the size of the set @var{set}.  This implementation operates
12935 in constant time, and is thus more efficient than
12936 @code{m4_eval(m4_count(m4_set_listc([set])) - 1)}.
12937 @end defmac
12940 @node Forbidden Patterns
12941 @subsection Forbidden Patterns
12942 @cindex Forbidden patterns
12943 @cindex Patterns, forbidden
12945 M4sugar provides a means to define suspicious patterns, patterns
12946 describing tokens which should not be found in the output.  For
12947 instance, if an Autoconf @file{configure} script includes tokens such as
12948 @samp{AC_DEFINE}, or @samp{dnl}, then most probably something went
12949 wrong (typically a macro was not evaluated because of overquotation).
12951 M4sugar forbids all the tokens matching @samp{^_?m4_} and @samp{^dnl$}.
12952 Additional layers, such as M4sh and Autoconf, add additional forbidden
12953 patterns to the list.
12955 @defmac m4_pattern_forbid (@var{pattern})
12956 @msindex{pattern_forbid}
12957 Declare that no token matching @var{pattern} must be found in the output.
12958 Comments are not checked; this can be a problem if, for instance, you
12959 have some macro left unexpanded after an @samp{#include}.  No consensus
12960 is currently found in the Autoconf community, as some people consider it
12961 should be valid to name macros in comments (which doesn't make sense to
12962 the authors of this documentation: input, such as macros, should be
12963 documented by @samp{dnl} comments; reserving @samp{#}-comments to
12964 document the output).
12965 @end defmac
12967 Of course, you might encounter exceptions to these generic rules, for
12968 instance you might have to refer to @samp{$m4_flags}.
12970 @defmac m4_pattern_allow (@var{pattern})
12971 @msindex{pattern_allow}
12972 Any token matching @var{pattern} is allowed, including if it matches an
12973 @code{m4_pattern_forbid} pattern.
12974 @end defmac
12976 @node Debugging via autom4te
12977 @section Debugging via autom4te
12978 @cindex debugging tips
12979 @cindex autom4te debugging tips
12980 @cindex m4sugar debugging tips
12981 At times, it is desirable to see what was happening inside m4, to see
12982 why output was not matching expectations.  However, post-processing done
12983 by @command{autom4te} means that directly using the m4 builtin
12984 @code{m4_traceon} is likely to interfere with operation.  Also, frequent
12985 diversion changes and the concept of forbidden tokens make it difficult
12986 to use @code{m4_defn} to generate inline comments in the final output.
12988 There are a couple of tools to help with this.  One is the use of the
12989 @option{--trace} option provided by @command{autom4te} (as well as each
12990 of the programs that wrap @command{autom4te}, such as
12991 @command{autoconf}), in order to inspect when a macro is called and with
12992 which arguments.  For example, when this paragraph was written, the
12993 autoconf version could be found by:
12995 @example
12996 $ @kbd{autoconf --trace=AC_INIT}
12997 configure.ac:23:AC_INIT:GNU Autoconf:2.63b.95-3963:bug-autoconf@@gnu.org
12998 $ @kbd{autoconf --trace='AC_INIT:version is $2'}
12999 version is 2.63b.95-3963
13000 @end example
13002 Another trick is to print out the expansion of various m4 expressions to
13003 standard error or to an independent file, with no further m4 expansion,
13004 and without interfering with diversion changes or the post-processing
13005 done to standard output.  @code{m4_errprintn} shows a given expression
13006 on standard error.  For example, if you want to see the expansion of an
13007 autoconf primitive or of one of your autoconf macros, you can do it like
13008 this:
13010 @example
13011 $ @kbd{cat <<\EOF > configure.ac}
13012 AC_INIT
13013 m4_errprintn([The definition of AC_DEFINE_UNQUOTED:])
13014 m4_errprintn(m4_defn([AC_DEFINE_UNQUOTED]))
13015 AC_OUTPUT
13017 $ @kbd{autoconf}
13018 @error{}The definition of AC_DEFINE_UNQUOTED:
13019 @error{}_AC_DEFINE_Q([], $@@)
13020 @end example
13022 @node Programming in M4sh
13023 @chapter Programming in M4sh
13025 M4sh, pronounced ``mash'', is aiming at producing portable Bourne shell
13026 scripts.  This name was coined by Lars J. Aas, who notes that,
13027 according to the Webster's Revised Unabridged Dictionary (1913):
13029 @quotation
13030 Mash \Mash\, n.  [Akin to G. meisch, maisch, meische, maische, mash,
13031 wash, and prob.@: to AS. miscian to mix.  See ``Mix''.]
13033 @enumerate 1
13034 @item
13035 A mass of mixed ingredients reduced to a soft pulpy state by beating or
13036 pressure@enddots{}
13038 @item
13039 A mixture of meal or bran and water fed to animals.
13041 @item
13042 A mess; trouble.  [Obs.] --Beau.@: & Fl.
13043 @end enumerate
13044 @end quotation
13046 M4sh reserves the M4 macro namespace @samp{^_AS_} for internal use, and
13047 the namespace @samp{^AS_} for M4sh macros.  It also reserves the shell
13048 and environment variable namespace @samp{^as_}, and the here-document
13049 delimiter namespace @samp{^_AS[A-Z]} in the output file.  You should not
13050 define your own macros or output shell code that conflicts with these
13051 namespaces.
13053 @menu
13054 * Common Shell Constructs::     Portability layer for common shell constructs
13055 * Polymorphic Variables::       Support for indirect variable names
13056 * Initialization Macros::       Macros to establish a sane shell environment
13057 * File Descriptor Macros::      File descriptor macros for input and output
13058 @end menu
13060 @node Common Shell Constructs
13061 @section Common Shell Constructs
13063 M4sh provides portable alternatives for some common shell constructs
13064 that unfortunately are not portable in practice.
13066 @c Deprecated, to be replaced by a better API
13067 @ignore
13068 @defmac AS_BASENAME (@var{file-name})
13069 @asindex{BASENAME}
13070 Output the non-directory portion of @var{file-name}.  For example,
13071 if @code{$file} is @samp{/one/two/three}, the command
13072 @code{base=`AS_BASENAME(["$file"])`} sets @code{base} to @samp{three}.
13073 @end defmac
13074 @end ignore
13076 @defmac AS_CASE (@var{word}, @ovar{pattern1}, @ovar{if-matched1}, @
13077   @dots{}, @ovar{default})
13078 @asindex{CASE}
13079 Expand into a shell @samp{case} statement, where @var{word} is matched
13080 against one or more patterns.  @var{if-matched} is run if the
13081 corresponding pattern matched @var{word}, else @var{default} is run.
13082 Avoids several portability issues (@pxref{case, , Limitations of Shell
13083 Builtins}).
13084 @end defmac
13086 @c Deprecated, to be replaced by a better API
13087 @defmac AS_DIRNAME (@var{file-name})
13088 @asindex{DIRNAME}
13089 Output the directory portion of @var{file-name}.  For example,
13090 if @code{$file} is @samp{/one/two/three}, the command
13091 @code{dir=`AS_DIRNAME(["$file"])`} sets @code{dir} to @samp{/one/two}.
13093 This interface may be improved in the future to avoid forks and losing
13094 trailing newlines.
13095 @end defmac
13097 @defmac AS_ECHO (@var{word})
13098 @asindex{ECHO}
13099 Emits @var{word} to the standard output, followed by a newline.  @var{word}
13100 must be a single shell word (typically a quoted string).  The bytes of
13101 @var{word} are output as-is, even if it starts with "-" or contains "\".
13102 Redirections can be placed outside the macro invocation.  This is much
13103 more portable than using @command{echo} (@pxref{echo, , Limitations of
13104 Shell Builtins}).
13105 @end defmac
13107 @defmac AS_ECHO_N (@var{word})
13108 @asindex{ECHO_N}
13109 Emits @var{word} to the standard output, without a following newline.
13110 @var{word} must be a single shell word (typically a quoted string) and,
13111 for portability, should not include more than one newline.  The bytes of
13112 @var{word} are output as-is, even if it starts with "-" or contains "\".
13113 Redirections can be placed outside the macro invocation.
13114 @end defmac
13116 @c We cannot use @dvar because the macro expansion mistreats backslashes.
13117 @defmac AS_ESCAPE (@var{string}, @r{[}@var{chars} = @samp{`\"$}@r{]})
13118 @asindex{ESCAPE}
13119 Expands to @var{string}, with any characters in @var{chars} escaped with
13120 a backslash (@samp{\}).  @var{chars} should be at most four bytes long,
13121 and only contain characters from the set @samp{`\"$}; however,
13122 characters may be safely listed more than once in @var{chars} for the
13123 sake of syntax highlighting editors.  The current implementation expands
13124 @var{string} after adding escapes; if @var{string} contains macro calls
13125 that in turn expand to text needing shell quoting, you can use
13126 @code{AS_ESCAPE(m4_dquote(m4_expand([string])))}.
13128 The default for @var{chars} (@samp{\"$`}) is the set of characters
13129 needing escapes when @var{string} will be used literally within double
13130 quotes.  One common variant is the set of characters to protect when
13131 @var{string} will be used literally within back-ticks or an unquoted
13132 here-document (@samp{\$`}).  Another common variant is @samp{""}, which can
13133 be used to form a double-quoted string containing the same expansions
13134 that would have occurred if @var{string} were expanded in an unquoted
13135 here-document; however, when using this variant, care must be taken that
13136 @var{string} does not use double quotes within complex variable
13137 expansions (such as @samp{$@{foo-`echo "hi"`@}}) that would be broken
13138 with improper escapes.
13140 This macro is often used with @code{AS_ECHO}.  For an example, observe
13141 the output generated by the shell code generated from this snippet:
13143 @example
13144 foo=bar
13145 AS_ECHO(["AS_ESCAPE(["$foo" = ])AS_ESCAPE(["$foo"], [""])"])
13146 @result{}"$foo" = "bar"
13147 m4_define([macro], [a, [\b]])
13148 AS_ECHO(["AS_ESCAPE([[macro]])"])
13149 @result{}macro
13150 AS_ECHO(["AS_ESCAPE([macro])"])
13151 @result{}a, b
13152 AS_ECHO(["AS_ESCAPE(m4_dquote(m4_expand([macro])))"])
13153 @result{}a, \b
13154 @end example
13156 @comment Should we add AS_ESCAPE_SINGLE? If we do, we can optimize in
13157 @comment the case of @var{string} that does not contain '.
13158 To escape a string that will be placed within single quotes, use:
13160 @example
13161 m4_bpatsubst([[@var{string}]], ['], ['\\''])
13162 @end example
13163 @end defmac
13165 @defmac AS_EXIT (@dvar{status, $?})
13166 @asindex{EXIT}
13167 Emit code to exit the shell with @var{status}, defaulting to @samp{$?}.
13168 This macro
13169 works around shells that see the exit status of the command prior to
13170 @code{exit} inside a @samp{trap 0} handler (@pxref{trap, , Limitations
13171 of Shell Builtins}).
13172 @end defmac
13174 @defmac AS_IF (@var{test1}, @ovar{run-if-true1}, @dots{}, @ovar{run-if-false})
13175 @asindex{IF}
13176 Run shell code @var{test1}.  If @var{test1} exits with a zero status then
13177 run shell code @var{run-if-true1}, else examine further tests.  If no test
13178 exits with a zero status, run shell code @var{run-if-false}, with
13179 simplifications if either @var{run-if-true1} or @var{run-if-false}
13180 is empty.  For example,
13182 @example
13183 AS_IF([test "x$foo" = xyes], [HANDLE_FOO([yes])],
13184       [test "x$foo" != xno], [HANDLE_FOO([maybe])],
13185       [echo foo not specified])
13186 @end example
13188 @noindent
13189 ensures any required macros of @code{HANDLE_FOO}
13190 are expanded before the first test.
13191 @end defmac
13193 @defmac AS_MKDIR_P (@var{file-name})
13194 @asindex{MKDIR_P}
13195 Make the directory @var{file-name}, including intervening directories
13196 as necessary.  This is equivalent to @samp{mkdir -p -- @var{file-name}},
13197 except that it is portable to older versions of @command{mkdir} that
13198 lack support for the @option{-p} option or for the @option{--}
13199 delimiter (@pxref{mkdir, , Limitations of Usual Tools}).  Also,
13200 @code{AS_MKDIR_P}
13201 succeeds if @var{file-name} is a symbolic link to an existing directory,
13202 even though Posix is unclear whether @samp{mkdir -p} should
13203 succeed in that case.  If creation of @var{file-name} fails, exit the
13204 script.
13206 Also see the @code{AC_PROG_MKDIR_P} macro (@pxref{Particular Programs}).
13207 @end defmac
13209 @defmac AS_SET_STATUS (@var{status})
13210 @asindex{SET_STATUS}
13211 Emit shell code to set the value of @samp{$?} to @var{status}, as
13212 efficiently as possible.  However, this is not guaranteed to abort a
13213 shell running with @code{set -e} (@pxref{set, , Limitations of Shell
13214 Builtins}).  This should also be used at the end of a complex shell
13215 function instead of @samp{return} (@pxref{Shell Functions}) to avoid
13216 a DJGPP shell bug.
13217 @end defmac
13219 @defmac AS_TR_CPP (@var{expression})
13220 @asindex{TR_CPP}
13221 Transform @var{expression} into a valid right-hand side for a C @code{#define}.
13222 For example:
13224 @example
13225 # This outputs "#define HAVE_CHAR_P 1".
13226 # Notice the m4 quoting around #, to prevent an m4 comment
13227 type="char *"
13228 echo "[#]define AS_TR_CPP([HAVE_$type]) 1"
13229 @end example
13230 @end defmac
13232 @defmac AS_TR_SH (@var{expression})
13233 @asindex{TR_SH}
13234 Transform @var{expression} into shell code that generates a valid shell
13235 variable name.  The result is literal when possible at m4 time, but must
13236 be used with @code{eval} if @var{expression} causes shell indirections.
13237 For example:
13239 @example
13240 # This outputs "Have it!".
13241 header="sys/some file.h"
13242 eval AS_TR_SH([HAVE_$header])=yes
13243 if test "x$HAVE_sys_some_file_h" = xyes; then echo "Have it!"; fi
13244 @end example
13245 @end defmac
13247 @defmac AS_SET_CATFILE (@var{var}, @var{dir}, @var{file})
13248 @asindex{SET_CATFILE}
13249 Set the polymorphic shell variable @var{var} to @var{dir}/@var{file},
13250 but optimizing the common cases (@var{dir} or @var{file} is @samp{.},
13251 @var{file} is absolute, etc.).
13252 @end defmac
13254 @defmac AS_UNSET (@var{var})
13255 @asindex{UNSET}
13256 Unsets the shell variable @var{var}, working around bugs in older
13257 shells (@pxref{unset, , Limitations of Shell
13258 Builtins}).  @var{var} can be a literal or indirect variable name.
13259 @end defmac
13261 @defmac AS_VERSION_COMPARE (@var{version-1}, @var{version-2}, @
13262   @ovar{action-if-less}, @ovar{action-if-equal}, @ovar{action-if-greater})
13263 @asindex{VERSION_COMPARE}
13264 Compare two strings @var{version-1} and @var{version-2}, possibly
13265 containing shell variables, as version strings, and expand
13266 @var{action-if-less}, @var{action-if-equal}, or @var{action-if-greater}
13267 depending upon the result.
13268 The algorithm to compare is similar to the one used by strverscmp in
13269 glibc (@pxref{String/Array Comparison, , String/Array Comparison, libc,
13270 The GNU C Library}).
13271 @end defmac
13273 @node Polymorphic Variables
13274 @section Support for indirect variable names
13275 @cindex variable name indirection
13276 @cindex polymorphic variable name
13277 @cindex indirection, variable name
13279 Often, it is convenient to write a macro that will emit shell code
13280 operating on a shell variable.  The simplest case is when the variable
13281 name is known.  But a more powerful idiom is writing shell code that can
13282 work through an indirection, where another variable or command
13283 substitution produces the name of the variable to actually manipulate.
13284 M4sh supports the notion of polymorphic shell variables, making it easy
13285 to write a macro that can deal with either literal or indirect variable
13286 names and output shell code appropriate for both use cases.  Behavior is
13287 undefined if expansion of an indirect variable does not result in a
13288 literal variable name.
13290 @defmac AS_LITERAL_IF (@var{expression}, @ovar{if-literal}, @ovar{if-not})
13291 @asindex{LITERAL_IF}
13292 If the expansion of @var{expression} is definitely a shell literal,
13293 expand @var{if-literal}.  If the expansion of @var{expression} looks
13294 like it might contain shell indirections (such as @code{$var} or
13295 @code{`expr`}), then @var{if-not} is expanded.  In order to reduce the
13296 time spent deciding whether an expression is literal, the implementation
13297 is somewhat conservative (for example, @samp{'[$]'} is a single-quoted
13298 shell literal, but causes @var{if-not} to be expanded).  While this
13299 macro is often used for recognizing shell variable names, it can also be
13300 used in other contexts.
13302 @example
13303 AC_DEFUN([MY_ACTION],
13304 [AS_LITERAL_IF([$1],
13305 [echo "$1"],
13306 [AS_VAR_COPY([tmp], [$1])
13307 echo "$tmp"])])
13308 @end example
13309 @end defmac
13311 @defmac AS_VAR_APPEND (@var{var}, @var{text})
13312 @asindex{VAR_APPEND}
13313 Emit shell code to append the shell expansion of @var{text} to the end
13314 of the current contents of the polymorphic shell variable @var{var},
13315 taking advantage of shells that provide the @samp{+=} extension for more
13316 efficient scaling.
13318 For situations where the final contents of @var{var} are relatively
13319 short (less than 256 bytes), it is more efficient to use the simpler
13320 code sequence of @code{@var{var}=$@{@var{var}@}@var{text}} (or its
13321 polymorphic equivalent of @code{AS_VAR_COPY([tmp], [@var{var}])} and
13322 @code{AS_VAR_SET([@var{var}], ["$tmp"@var{text}])}).  But in the case
13323 when the script will be repeatedly appending text into @code{var},
13324 issues of scaling start to become apparent.  A naive implementation
13325 requires execution time linear to the length of the current contents of
13326 @var{var} as well as the length of @var{text} for a single append, for
13327 an overall quadratic scaling with multiple appends.  This macro takes
13328 advantage of shells which provide the extension
13329 @code{@var{var}+=@var{text}}, which can provide amortized constant time
13330 for a single append, for an overall linear scaling with multiple
13331 appends.  Note that unlike @code{AS_VAR_SET}, this macro requires that
13332 @var{text} be quoted properly to avoid field splitting and file name
13333 expansion.
13334 @end defmac
13336 @defmac AS_VAR_ARITH (@var{var}, @var{expression})
13337 @asindex{VAR_ARITH}
13338 Emit shell code to compute the arithmetic expansion of @var{expression},
13339 assigning the result as the contents of the polymorphic shell variable
13340 @var{var}.  The code takes advantage of shells that provide @samp{$(())}
13341 for fewer forks, but uses @command{expr} as a fallback.  Therefore, the
13342 syntax for a valid @var{expression} is rather limited: all operators
13343 must occur as separate shell arguments and with proper quoting, there is
13344 no portable equality operator, all variables containing numeric values
13345 must be expanded prior to the computation, all numeric values must be
13346 provided in decimal without leading zeroes, and the first shell argument
13347 should not be a negative number.  In the following example, this snippet
13348 will print @samp{(2+3)*4 == 20}.
13350 @example
13351 bar=3
13352 AS_VAR_ARITH([foo], [\( 2 + $bar \) \* 4])
13353 echo "(2+$bar)*4 == $foo"
13354 @end example
13355 @end defmac
13357 @defmac AS_VAR_COPY (@var{dest}, @var{source})
13358 @asindex{VAR_COPY}
13359 Emit shell code to assign the contents of the polymorphic shell variable
13360 @var{source} to the polymorphic shell variable @var{dest}.  For example,
13361 executing this M4sh snippet will output @samp{bar hi}:
13363 @example
13364 foo=bar bar=hi
13365 AS_VAR_COPY([a], [foo])
13366 AS_VAR_COPY([b], [$foo])
13367 echo "$a $b"
13368 @end example
13370 When it is necessary to access the contents of an indirect variable
13371 inside a shell double-quoted context, the recommended idiom is to first
13372 copy the contents into a temporary literal shell variable.
13374 @smallexample
13375 for header in stdint_h inttypes_h ; do
13376   AS_VAR_COPY([var], [ac_cv_header_$header])
13377   echo "$header detected: $var"
13378 done
13379 @end smallexample
13380 @end defmac
13382 @comment AS_VAR_GET is intentionally undocumented; it can't handle
13383 @comment trailing newlines uniformly, and forks too much.
13385 @defmac AS_VAR_IF (@var{var}, @ovar{value}, @ovar{if-equal}, @
13386   @ovar{if-not-equal})
13387 @asindex{VAR_IF}
13388 Output a shell conditional statement.  If the contents of the
13389 polymorphic shell variable @var{var} match the string @var{value},
13390 execute @var{if-equal}; otherwise execute @var{if-not-equal}.  Avoids
13391 shell bugs if an interrupt signal arrives while a command substitution
13392 in @var{var} is being expanded.
13393 @end defmac
13395 @defmac AS_VAR_PUSHDEF (@var{m4-name}, @var{value})
13396 @defmacx AS_VAR_POPDEF (@var{m4-name})
13397 @asindex{VAR_PUSHDEF}
13398 @asindex{VAR_POPDEF}
13399 @cindex composing variable names
13400 @cindex variable names, composing
13401 A common M4sh idiom involves composing shell variable names from an m4
13402 argument (for example, writing a macro that uses a cache variable).
13403 @var{value} can be an arbitrary string, which will be transliterated
13404 into a valid shell name by @code{AS_TR_SH}.  In order to access the
13405 composed variable name based on @var{value}, it is easier to declare a
13406 temporary m4 macro @var{m4-name} with @code{AS_VAR_PUSHDEF}, then use
13407 that macro as the argument to subsequent @code{AS_VAR} macros as a
13408 polymorphic variable name, and finally free the temporary macro with
13409 @code{AS_VAR_POPDEF}.  These macros are often followed with @code{dnl},
13410 to avoid excess newlines in the output.
13412 Here is an involved example, that shows the power of writing macros that
13413 can handle composed shell variable names:
13415 @example
13416 m4_define([MY_CHECK_HEADER],
13417 [AS_VAR_PUSHDEF([my_Header], [ac_cv_header_$1])dnl
13418 AS_VAR_IF([my_Header], [yes], [echo "header $1 detected"])dnl
13419 AS_VAR_POPDEF([my_Header])dnl
13421 MY_CHECK_HEADER([stdint.h])
13422 for header in inttypes.h stdlib.h ; do
13423   MY_CHECK_HEADER([$header])
13424 done
13425 @end example
13427 @noindent
13428 In the above example, @code{MY_CHECK_HEADER} can operate on polymorphic
13429 variable names.  In the first invocation, the m4 argument is
13430 @code{stdint.h}, which transliterates into a literal @code{stdint_h}.
13431 As a result, the temporary macro @code{my_Header} expands to the literal
13432 shell name @samp{ac_cv_header_stdint_h}.  In the second invocation, the
13433 m4 argument to @code{MY_CHECK_HEADER} is @code{$header}, and the
13434 temporary macro @code{my_Header} expands to the indirect shell name
13435 @samp{$as_my_Header}.  During the shell execution of the for loop, when
13436 @samp{$header} contains @samp{inttypes.h}, then @samp{$as_my_Header}
13437 contains @samp{ac_cv_header_inttypes_h}.  If this script is then run on a
13438 platform where all three headers have been previously detected, the
13439 output of the script will include:
13441 @smallexample
13442 header stdint.h detected
13443 header inttypes.h detected
13444 header stdlib.h detected
13445 @end smallexample
13446 @end defmac
13448 @defmac AS_VAR_SET (@var{var}, @ovar{value})
13449 @asindex{VAR_SET}
13450 Emit shell code to assign the contents of the polymorphic shell variable
13451 @var{var} to the shell expansion of @var{value}.  @var{value} is not
13452 subject to field splitting or file name expansion, so if command
13453 substitution is used, it may be done with @samp{`""`} rather than using
13454 an intermediate variable (@pxref{Shell Substitutions}).  However,
13455 @var{value} does undergo rescanning for additional macro names; behavior
13456 is unspecified if late expansion results in any shell meta-characters.
13457 @end defmac
13459 @defmac AS_VAR_SET_IF (@var{var}, @ovar{if-set}, @ovar{if-undef})
13460 @asindex{VAR_SET_IF}
13461 Emit a shell conditional statement, which executes @var{if-set} if the
13462 polymorphic shell variable @code{var} is set to any value, and
13463 @var{if-undef} otherwise.
13464 @end defmac
13466 @defmac AS_VAR_TEST_SET (@var{var})
13467 @asindex{VAR_TEST_SET}
13468 Emit a shell statement that results in a successful exit status only if
13469 the polymorphic shell variable @code{var} is set.
13470 @end defmac
13472 @node Initialization Macros
13473 @section Initialization Macros
13475 @defmac AS_BOURNE_COMPATIBLE
13476 @asindex{BOURNE_COMPATIBLE}
13477 Set up the shell to be more compatible with the Bourne shell as
13478 standardized by Posix, if possible.  This may involve setting
13479 environment variables, or setting options, or similar
13480 implementation-specific actions.  This macro is deprecated, since
13481 @code{AS_INIT} already invokes it.
13482 @end defmac
13484 @defmac AS_INIT
13485 @asindex{INIT}
13486 @evindex LC_ALL
13487 @evindex SHELL
13488 Initialize the M4sh environment.  This macro calls @code{m4_init}, then
13489 outputs the @code{#! /bin/sh} line, a notice about where the output was
13490 generated from, and code to sanitize the environment for the rest of the
13491 script.  Among other initializations, this sets @env{SHELL} to the shell
13492 chosen to run the script (@pxref{CONFIG_SHELL}), and @env{LC_ALL} to
13493 ensure the C locale.  Finally, it changes the current diversion to
13494 @code{BODY}.  @code{AS_INIT} is called automatically by @code{AC_INIT}
13495 and @code{AT_INIT}, so shell code in @file{configure},
13496 @file{config.status}, and @file{testsuite} all benefit from a sanitized
13497 shell environment.
13498 @end defmac
13500 @defmac AS_INIT_GENERATED (@var{file}, @ovar{comment})
13501 @asindex{INIT_GENERATED}
13502 Emit shell code to start the creation of a subsidiary shell script in
13503 @var{file}, including changing @var{file} to be executable.  This macro
13504 populates the child script with information learned from the parent
13505 (thus, the emitted code is equivalent in effect, but more efficient,
13506 than the code output by @code{AS_INIT}, @code{AS_BOURNE_COMPATIBLE}, and
13507 @code{AS_SHELL_SANITIZE}).  If present, @var{comment} is output near the
13508 beginning of the child, prior to the shell initialization code, and is
13509 subject to parameter expansion, command substitution, and backslash
13510 quote removal.  The
13511 parent script should check the exit status after this macro, in case
13512 @var{file} could not be properly created (for example, if the disk was
13513 full).  If successfully created, the parent script can then proceed to
13514 append additional M4sh constructs into the child script.
13516 Note that the child script starts life without a log file open, so if
13517 the parent script uses logging (@pxref{AS_MESSAGE_LOG_FD}), you
13518 must temporarily disable any attempts to use the log file until after
13519 emitting code to open a log within the child.  On the other hand, if the
13520 parent script has @code{AS_MESSAGE_FD} redirected somewhere besides
13521 @samp{1}, then the child script already has code that copies stdout to
13522 that descriptor.  Currently, the suggested
13523 idiom for writing a M4sh shell script from within another script is:
13525 @example
13526 AS_INIT_GENERATED([@var{file}], [[# My child script.
13527 ]]) || @{ AS_ECHO(["Failed to create child script"]); AS_EXIT; @}
13528 m4_pushdef([AS_MESSAGE_LOG_FD])dnl
13529 cat >> "@var{file}" <<\__EOF__
13530 # Code to initialize AS_MESSAGE_LOG_FD
13531 m4_popdef([AS_MESSAGE_LOG_FD])dnl
13532 # Additional code
13533 __EOF__
13534 @end example
13536 This, however, may change in the future as the M4sh interface is
13537 stabilized further.
13539 Also, be aware that use of @env{LINENO} within the child script may
13540 report line numbers relative to their location in the parent script,
13541 even when using @code{AS_LINENO_PREPARE}, if the parent script was
13542 unable to locate a shell with working @env{LINENO} support.
13543 @end defmac
13545 @defmac AS_LINENO_PREPARE
13546 @asindex{LINENO_PREPARE}
13547 @evindex LINENO
13548 Find a shell that supports the special variable @env{LINENO}, which
13549 contains the number of the currently executing line.  This macro is
13550 automatically invoked by @code{AC_INIT} in configure scripts.
13551 @end defmac
13553 @defmac AS_ME_PREPARE
13554 @asindex{ME_PREPARE}
13555 Set up variable @env{as_me} to be the basename of the currently executing
13556 script.  This macro is automatically invoked by @code{AC_INIT} in
13557 configure scripts.
13558 @end defmac
13560 @defmac AS_SHELL_SANITIZE
13561 @asindex{SHELL_SANITIZE}
13562 Initialize the shell suitably for @command{configure} scripts.  This has
13563 the effect of @code{AS_BOURNE_COMPATIBLE}, and sets some other
13564 environment variables for predictable results from configuration tests.
13565 For example, it sets @env{LC_ALL} to change to the default C locale.
13566 @xref{Special Shell Variables}.  This macro is deprecated, since
13567 @code{AS_INIT} already invokes it.
13568 @end defmac
13571 @node File Descriptor Macros
13572 @section File Descriptor Macros
13573 @cindex input
13574 @cindex standard input
13575 @cindex file descriptors
13576 @cindex descriptors
13577 @cindex low-level output
13578 @cindex output, low-level
13580 The following macros define file descriptors used to output messages
13581 (or input values) from @file{configure} scripts.
13582 For example:
13584 @example
13585 echo "$wombats found" >&AS_MESSAGE_LOG_FD
13586 echo 'Enter desired kangaroo count:' >&AS_MESSAGE_FD
13587 read kangaroos <&AS_ORIGINAL_STDIN_FD`
13588 @end example
13590 @noindent
13591 However doing so is seldom needed, because Autoconf provides higher
13592 level macros as described below.
13594 @defmac AS_MESSAGE_FD
13595 @asindex{MESSAGE_FD}
13596 The file descriptor for @samp{checking for...}  messages and results.
13597 By default, @code{AS_INIT} sets this to @samp{1} for standalone M4sh
13598 clients.  However, @code{AC_INIT} shuffles things around to another file
13599 descriptor, in order to allow the @option{-q} option of
13600 @command{configure} to choose whether messages should go to the script's
13601 standard output or be discarded.
13603 If you want to display some messages, consider using one of the printing
13604 macros (@pxref{Printing Messages}) instead.  Copies of messages output
13605 via these macros are also recorded in @file{config.log}.
13606 @end defmac
13608 @anchor{AS_MESSAGE_LOG_FD}
13609 @defmac AS_MESSAGE_LOG_FD
13610 @asindex{MESSAGE_LOG_FD}
13611 This must either be empty, or expand to a file descriptor for log
13612 messages.  By default, @code{AS_INIT} sets this macro to the empty
13613 string for standalone M4sh clients, thus disabling logging.  However,
13614 @code{AC_INIT} shuffles things around so that both @command{configure}
13615 and @command{config.status} use @file{config.log} for log messages.
13616 Macros that run tools, like @code{AC_COMPILE_IFELSE} (@pxref{Running the
13617 Compiler}), redirect all output to this descriptor.  You may want to do
13618 so if you develop such a low-level macro.
13619 @end defmac
13621 @defmac AS_ORIGINAL_STDIN_FD
13622 @asindex{ORIGINAL_STDIN_FD}
13623 This must expand to a file descriptor for the original standard input.
13624 By default, @code{AS_INIT} sets this macro to @samp{0} for standalone
13625 M4sh clients.  However, @code{AC_INIT} shuffles things around for
13626 safety.
13628 When @command{configure} runs, it may accidentally execute an
13629 interactive command that has the same name as the non-interactive meant
13630 to be used or checked.  If the standard input was the terminal, such
13631 interactive programs would cause @command{configure} to stop, pending
13632 some user input.  Therefore @command{configure} redirects its standard
13633 input from @file{/dev/null} during its initialization.  This is not
13634 normally a problem, since @command{configure} normally does not need
13635 user input.
13637 In the extreme case where your @file{configure} script really needs to
13638 obtain some values from the original standard input, you can read them
13639 explicitly from @code{AS_ORIGINAL_STDIN_FD}.
13640 @end defmac
13643 @c =================================================== Writing Autoconf Macros.
13645 @node Writing Autoconf Macros
13646 @chapter Writing Autoconf Macros
13648 When you write a feature test that could be applicable to more than one
13649 software package, the best thing to do is encapsulate it in a new macro.
13650 Here are some instructions and guidelines for writing Autoconf macros.
13652 @menu
13653 * Macro Definitions::           Basic format of an Autoconf macro
13654 * Macro Names::                 What to call your new macros
13655 * Reporting Messages::          Notifying @command{autoconf} users
13656 * Dependencies Between Macros::  What to do when macros depend on other macros
13657 * Obsoleting Macros::           Warning about old ways of doing things
13658 * Coding Style::                Writing Autoconf macros @`a la Autoconf
13659 @end menu
13661 @node Macro Definitions
13662 @section Macro Definitions
13664 @defmac AC_DEFUN (@var{name}, @ovar{body})
13665 @acindex{DEFUN}
13666 Autoconf macros are defined using the @code{AC_DEFUN} macro, which is
13667 similar to the M4 builtin @code{m4_define} macro; this creates a macro
13668 named @var{name} and with @var{body} as its expansion.  In addition to
13669 defining a macro, @code{AC_DEFUN} adds to it some code that is used to
13670 constrain the order in which macros are called, while avoiding redundant
13671 output (@pxref{Prerequisite Macros}).
13672 @end defmac
13674 An Autoconf macro definition looks like this:
13676 @example
13677 AC_DEFUN(@var{macro-name}, @var{macro-body})
13678 @end example
13680 You can refer to any arguments passed to the macro as @samp{$1},
13681 @samp{$2}, etc.  @xref{Definitions, , How to define new macros, m4.info,
13682 GNU M4}, for more complete information on writing M4 macros.
13684 Most macros fall in one of two general categories.  The first category
13685 includes macros which take arguments, in order to generate output
13686 parameterized by those arguments.  Macros in this category are designed
13687 to be directly expanded, often multiple times, and should not be used as
13688 the argument to @code{AC_REQUIRE}.  The other category includes macros
13689 which are shorthand for a fixed block of text, and therefore do not take
13690 arguments.  For this category of macros, directly expanding the macro
13691 multiple times results in redundant output, so it is more common to use
13692 the macro as the argument to @code{AC_REQUIRE}, or to declare the macro
13693 with @code{AC_DEFUN_ONCE} (@pxref{One-Shot Macros}).
13695 Be sure to properly quote both the @var{macro-body} @emph{and} the
13696 @var{macro-name} to avoid any problems if the macro happens to have
13697 been previously defined.
13699 Each macro should have a header comment that gives its prototype, and a
13700 brief description.  When arguments have default values, display them in
13701 the prototype.  For example:
13703 @example
13704 # AC_MSG_ERROR(ERROR, [EXIT-STATUS = 1])
13705 # --------------------------------------
13706 m4_define([AC_MSG_ERROR],
13707   [@{ AS_MESSAGE([error: $1], [2])
13708      exit m4_default([$2], [1]); @}])
13709 @end example
13711 Comments about the macro should be left in the header comment.  Most
13712 other comments make their way into @file{configure}, so just keep
13713 using @samp{#} to introduce comments.
13715 @cindex @code{dnl}
13716 If you have some special comments about pure M4 code, comments
13717 that make no sense in @file{configure} and in the header comment, then
13718 use the builtin @code{dnl}: it causes M4 to discard the text
13719 through the next newline.
13721 Keep in mind that @code{dnl} is rarely needed to introduce comments;
13722 @code{dnl} is more useful to get rid of the newlines following macros
13723 that produce no output, such as @code{AC_REQUIRE}.
13725 Public third-party macros need to use @code{AC_DEFUN}, and not
13726 @code{m4_define}, in order to be found by @command{aclocal}
13727 (@pxref{Extending aclocal,,, automake, GNU Automake}).
13728 Additionally, if it is ever determined that a macro should be made
13729 obsolete, it is easy to convert from @code{AC_DEFUN} to @code{AU_DEFUN}
13730 in order to have @command{autoupdate} assist the user in choosing a
13731 better alternative, but there is no corresponding way to make
13732 @code{m4_define} issue an upgrade notice (@pxref{AU_DEFUN}).
13734 There is another subtle, but important, difference between using
13735 @code{m4_define} and @code{AC_DEFUN}: only the former is unaffected by
13736 @code{AC_REQUIRE}.  When writing a file, it is always safe to replace a
13737 block of text with a @code{m4_define} macro that will expand to the same
13738 text.  But replacing a block of text with an @code{AC_DEFUN} macro with
13739 the same content does not necessarily give the same results, because it
13740 changes the location where any embedded but unsatisfied
13741 @code{AC_REQUIRE} invocations within the block will be expanded.  For an
13742 example of this, see @ref{Expanded Before Required}.
13744 @node Macro Names
13745 @section Macro Names
13747 All of the public Autoconf macros have all-uppercase names in the
13748 namespace @samp{^AC_} to prevent them from accidentally conflicting with
13749 other text; Autoconf also reserves the namespace @samp{^_AC_} for
13750 internal macros.  All shell variables that they use for internal
13751 purposes have mostly-lowercase names starting with @samp{ac_}.  Autoconf
13752 also uses here-document delimiters in the namespace @samp{^_AC[A-Z]}.  During
13753 @command{configure}, files produced by Autoconf make heavy use of the
13754 file system namespace @samp{^conf}.
13756 Since Autoconf is built on top of M4sugar (@pxref{Programming in
13757 M4sugar}) and M4sh (@pxref{Programming in M4sh}), you must also be aware
13758 of those namespaces (@samp{^_?\(m4\|AS\)_}).  And since
13759 @file{configure.ac} is also designed to be scanned by Autoheader,
13760 Autoscan, Autoupdate, and Automake, you should be aware of the
13761 @samp{^_?A[HNUM]_} namespaces.  In general, you @emph{should not use}
13762 the namespace of a package that does not own the macro or shell code you
13763 are writing.
13765 To ensure that your macros don't conflict with present or future
13766 Autoconf macros, you should prefix your own macro names and any shell
13767 variables they use with some other sequence.  Possibilities include your
13768 initials, or an abbreviation for the name of your organization or
13769 software package.  Historically, people have not always followed the
13770 rule of using a namespace appropriate for their package, and this has
13771 made it difficult for determining the origin of a macro (and where to
13772 report bugs about that macro), as well as difficult for the true
13773 namespace owner to add new macros without interference from pre-existing
13774 uses of third-party macros.  Perhaps the best example of this confusion
13775 is the @code{AM_GNU_GETTEXT} macro, which belongs, not to Automake, but
13776 to Gettext.
13778 Most of the Autoconf macros' names follow a structured naming convention
13779 that indicates the kind of feature check by the name.  The macro names
13780 consist of several words, separated by underscores, going from most
13781 general to most specific.  The names of their cache variables use the
13782 same convention (@pxref{Cache Variable Names}, for more information on
13783 them).
13785 The first word of the name after the namespace initials (such as
13786 @samp{AC_}) usually tells the category
13787 of the feature being tested.  Here are the categories used in Autoconf for
13788 specific test macros, the kind of macro that you are more likely to
13789 write.  They are also used for cache variables, in all-lowercase.  Use
13790 them where applicable; where they're not, invent your own categories.
13792 @table @code
13793 @item C
13794 C language builtin features.
13795 @item DECL
13796 Declarations of C variables in header files.
13797 @item FUNC
13798 Functions in libraries.
13799 @item GROUP
13800 Posix group owners of files.
13801 @item HEADER
13802 Header files.
13803 @item LIB
13804 C libraries.
13805 @item PROG
13806 The base names of programs.
13807 @item MEMBER
13808 Members of aggregates.
13809 @item SYS
13810 Operating system features.
13811 @item TYPE
13812 C builtin or declared types.
13813 @item VAR
13814 C variables in libraries.
13815 @end table
13817 After the category comes the name of the particular feature being
13818 tested.  Any further words in the macro name indicate particular aspects
13819 of the feature.  For example, @code{AC_PROG_CC_STDC} checks whether the
13820 C compiler supports ISO Standard C.
13822 An internal macro should have a name that starts with an underscore;
13823 Autoconf internals should therefore start with @samp{_AC_}.
13824 Additionally, a macro that is an internal subroutine of another macro
13825 should have a name that starts with an underscore and the name of that
13826 other macro, followed by one or more words saying what the internal
13827 macro does.  For example, @code{AC_PATH_X} has internal macros
13828 @code{_AC_PATH_X_XMKMF} and @code{_AC_PATH_X_DIRECT}.
13830 @node Reporting Messages
13831 @section Reporting Messages
13832 @cindex Messages, from @command{autoconf}
13834 When macros statically diagnose abnormal situations, benign or fatal, it
13835 is possible to make @command{autoconf} detect the problem, and refuse to
13836 create @file{configure} in the case of an error.  The macros in this
13837 section are considered obsolescent, and new code should use M4sugar
13838 macros for this purpose, see @ref{Diagnostic Macros}.
13840 On the other hand, it is possible to want to detect errors when
13841 @command{configure} is run, which are dependent on the environment of
13842 the user rather than the maintainer.  For dynamic diagnostics, see
13843 @ref{Printing Messages}.
13845 @defmac AC_DIAGNOSE (@var{category}, @var{message})
13846 @acindex{DIAGNOSE}
13847 Report @var{message} as a warning (or as an error if requested by the
13848 user) if warnings of the @var{category} are turned on.  This macro is
13849 obsolescent; you are encouraged to use:
13850 @example
13851 m4_warn([@var{category}], [@var{message}])
13852 @end example
13853 @noindent
13854 instead.  @xref{m4_warn}, for more details, including valid
13855 @var{category} names.
13856 @end defmac
13858 @defmac AC_WARNING (@var{message})
13859 @acindex{WARNING}
13860 Report @var{message} as a syntax warning.  This macro is obsolescent;
13861 you are encouraged to use:
13862 @example
13863 m4_warn([syntax], [@var{message}])
13864 @end example
13865 @noindent
13866 instead.  @xref{m4_warn}, for more details, as well as better
13867 finer-grained categories of warnings (not all problems have to do with
13868 syntax).
13869 @end defmac
13871 @defmac AC_FATAL (@var{message})
13872 @acindex{FATAL}
13873 Report a severe error @var{message}, and have @command{autoconf} die.
13874 This macro is obsolescent; you are encouraged to use:
13875 @example
13876 m4_fatal([@var{message}])
13877 @end example
13878 @noindent
13879 instead.  @xref{m4_fatal}, for more details.
13880 @end defmac
13882 When the user runs @samp{autoconf -W error}, warnings from
13883 @code{m4_warn} (including those issued through @code{AC_DIAGNOSE} and
13884 @code{AC_WARNING}) are reported as errors, see @ref{autoconf Invocation}.
13886 @node Dependencies Between Macros
13887 @section Dependencies Between Macros
13888 @cindex Dependencies between macros
13890 Some Autoconf macros depend on other macros having been called first in
13891 order to work correctly.  Autoconf provides a way to ensure that certain
13892 macros are called if needed and a way to warn the user if macros are
13893 called in an order that might cause incorrect operation.
13895 @menu
13896 * Prerequisite Macros::         Ensuring required information
13897 * Suggested Ordering::          Warning about possible ordering problems
13898 * One-Shot Macros::             Ensuring a macro is called only once
13899 @end menu
13901 @node Prerequisite Macros
13902 @subsection Prerequisite Macros
13903 @cindex Prerequisite macros
13904 @cindex Macros, prerequisites
13906 A macro that you write might need to use values that have previously
13907 been computed by other macros.  For example, @code{AC_DECL_YYTEXT}
13908 examines the output of @code{flex} or @code{lex}, so it depends on
13909 @code{AC_PROG_LEX} having been called first to set the shell variable
13910 @code{LEX}.
13912 Rather than forcing the user of the macros to keep track of the
13913 dependencies between them, you can use the @code{AC_REQUIRE} macro to do
13914 it automatically.  @code{AC_REQUIRE} can ensure that a macro is only
13915 called if it is needed, and only called once.
13917 @defmac AC_REQUIRE (@var{macro-name})
13918 @acindex{REQUIRE}
13919 If the M4 macro @var{macro-name} has not already been called, call it
13920 (without any arguments).  Make sure to quote @var{macro-name} with
13921 square brackets.  @var{macro-name} must have been defined using
13922 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
13923 that it has been called.
13925 @code{AC_REQUIRE} must be used inside a macro defined by @code{AC_DEFUN}; it
13926 must not be called from the top level.  Also, it does not make sense to
13927 require a macro that takes parameters.
13928 @end defmac
13930 @code{AC_REQUIRE} is often misunderstood.  It really implements
13931 dependencies between macros in the sense that if one macro depends upon
13932 another, the latter is expanded @emph{before} the body of the
13933 former.  To be more precise, the required macro is expanded before
13934 the outermost defined macro in the current expansion stack.
13935 In particular, @samp{AC_REQUIRE([FOO])} is not replaced with the body of
13936 @code{FOO}.  For instance, this definition of macros:
13938 @example
13939 @group
13940 AC_DEFUN([TRAVOLTA],
13941 [test "$body_temperature_in_celsius" -gt "38" &&
13942   dance_floor=occupied])
13943 AC_DEFUN([NEWTON_JOHN],
13944 [test "x$hair_style" = xcurly &&
13945   dance_floor=occupied])
13946 @end group
13948 @group
13949 AC_DEFUN([RESERVE_DANCE_FLOOR],
13950 [if date | grep '^Sat.*pm' >/dev/null 2>&1; then
13951   AC_REQUIRE([TRAVOLTA])
13952   AC_REQUIRE([NEWTON_JOHN])
13953 fi])
13954 @end group
13955 @end example
13957 @noindent
13958 with this @file{configure.ac}
13960 @example
13961 AC_INIT([Dance Manager], [1.0], [bug-dance@@example.org])
13962 RESERVE_DANCE_FLOOR
13963 if test "x$dance_floor" = xoccupied; then
13964   AC_MSG_ERROR([cannot pick up here, let's move])
13966 @end example
13968 @noindent
13969 does not leave you with a better chance to meet a kindred soul at
13970 other times than Saturday night since it expands into:
13972 @example
13973 @group
13974 test "$body_temperature_in_Celsius" -gt "38" &&
13975   dance_floor=occupied
13976 test "x$hair_style" = xcurly &&
13977   dance_floor=occupied
13979 if date | grep '^Sat.*pm' >/dev/null 2>&1; then
13983 @end group
13984 @end example
13986 This behavior was chosen on purpose: (i) it prevents messages in
13987 required macros from interrupting the messages in the requiring macros;
13988 (ii) it avoids bad surprises when shell conditionals are used, as in:
13990 @example
13991 @group
13992 if @dots{}; then
13993   AC_REQUIRE([SOME_CHECK])
13995 @dots{}
13996 SOME_CHECK
13997 @end group
13998 @end example
14000 However, this implementation can lead to another class of problems.
14001 Consider the case where an outer macro first expands, then indirectly
14002 requires, an inner macro:
14004 @example
14005 AC_DEFUN([TESTA], [[echo in A
14006 if test -n "$SEEN_A" ; then echo duplicate ; fi
14007 SEEN_A=:]])
14008 AC_DEFUN([TESTB], [AC_REQUIRE([TESTA])[echo in B
14009 if test -z "$SEEN_A" ; then echo bug ; fi]])
14010 AC_DEFUN([TESTC], [AC_REQUIRE([TESTB])[echo in C]])
14011 AC_DEFUN([OUTER], [[echo in OUTER]
14012 TESTA
14013 TESTC])
14014 OUTER
14015 @end example
14017 @noindent
14018 Prior to Autoconf 2.64, the implementation of @code{AC_REQUIRE}
14019 recognized that @code{TESTB} needed to be hoisted prior to the expansion
14020 of @code{OUTER}, but because @code{TESTA} had already been directly
14021 expanded, it failed to hoist @code{TESTA}.  Therefore, the expansion of
14022 @code{TESTB} occurs prior to its prerequisites, leading to the following
14023 output:
14025 @example
14026 in B
14028 in OUTER
14029 in A
14030 in C
14031 @end example
14033 @noindent
14034 Newer Autoconf is smart enough to recognize this situation, and hoists
14035 @code{TESTA} even though it has already been expanded, but issues a
14036 syntax warning in the process.  This is because the hoisted expansion of
14037 @code{TESTA} defeats the purpose of using @code{AC_REQUIRE} to avoid
14038 redundant code, and causes its own set of problems if the hoisted macro
14039 is not idempotent:
14041 @example
14042 in A
14043 in B
14044 in OUTER
14045 in A
14046 duplicate
14047 in C
14048 @end example
14050 The bug is not in Autoconf, but in the macro definitions.  If you ever
14051 pass a particular macro name to @code{AC_REQUIRE}, then you are implying
14052 that the macro only needs to be expanded once.  But to enforce this,
14053 either the macro must be declared with @code{AC_DEFUN_ONCE} (although
14054 this only helps in Autoconf 2.64 or newer), or all
14055 uses of that macro should be through @code{AC_REQUIRE}; directly
14056 expanding the macro defeats the point of using @code{AC_REQUIRE} to
14057 eliminate redundant expansion.  In the example, this rule of thumb was
14058 violated because @code{TESTB} requires @code{TESTA} while @code{OUTER}
14059 directly expands it.  One way of fixing the bug is to factor
14060 @code{TESTA} into two macros, the portion designed for direct and
14061 repeated use (here, named @code{TESTA}), and the portion designed for
14062 one-shot output and used only inside @code{AC_REQUIRE} (here, named
14063 @code{TESTA_PREREQ}).  Then, by fixing all clients to use the correct
14064 calling convention according to their needs:
14066 @example
14067 AC_DEFUN([TESTA], [AC_REQUIRE([TESTA_PREREQ])[echo in A]])
14068 AC_DEFUN([TESTA_PREREQ], [[echo in A_PREREQ
14069 if test -n "$SEEN_A" ; then echo duplicate ; fi
14070 SEEN_A=:]])
14071 AC_DEFUN([TESTB], [AC_REQUIRE([TESTA_PREREQ])[echo in B
14072 if test -z "$SEEN_A" ; then echo bug ; fi]])
14073 AC_DEFUN([TESTC], [AC_REQUIRE([TESTB])[echo in C]])
14074 AC_DEFUN([OUTER], [[echo in OUTER]
14075 TESTA
14076 TESTC])
14077 OUTER
14078 @end example
14080 @noindent
14081 the resulting output will then obey all dependency rules and avoid any
14082 syntax warnings, whether the script is built with old or new Autoconf
14083 versions:
14085 @example
14086 in A_PREREQ
14087 in B
14088 in OUTER
14089 in A
14090 in C
14091 @end example
14093 The helper macros @code{AS_IF} and @code{AS_CASE} may be used to
14094 enforce expansion of required macros outside of shell conditional
14095 constructs.  You are furthermore encouraged, although not required, to
14096 put all @code{AC_REQUIRE} calls
14097 at the beginning of a macro.  You can use @code{dnl} to avoid the empty
14098 lines they leave.
14100 @node Suggested Ordering
14101 @subsection Suggested Ordering
14102 @cindex Macros, ordering
14103 @cindex Ordering macros
14105 Some macros should be run before another macro if both are called, but
14106 neither @emph{requires} that the other be called.  For example, a macro
14107 that changes the behavior of the C compiler should be called before any
14108 macros that run the C compiler.  Many of these dependencies are noted in
14109 the documentation.
14111 Autoconf provides the @code{AC_BEFORE} macro to warn users when macros
14112 with this kind of dependency appear out of order in a
14113 @file{configure.ac} file.  The warning occurs when creating
14114 @command{configure} from @file{configure.ac}, not when running
14115 @command{configure}.
14117 For example, @code{AC_PROG_CPP} checks whether the C compiler
14118 can run the C preprocessor when given the @option{-E} option.  It should
14119 therefore be called after any macros that change which C compiler is
14120 being used, such as @code{AC_PROG_CC}.  So @code{AC_PROG_CC} contains:
14122 @example
14123 AC_BEFORE([$0], [AC_PROG_CPP])dnl
14124 @end example
14126 @noindent
14127 This warns the user if a call to @code{AC_PROG_CPP} has already occurred
14128 when @code{AC_PROG_CC} is called.
14130 @defmac AC_BEFORE (@var{this-macro-name}, @var{called-macro-name})
14131 @acindex{BEFORE}
14132 Make M4 print a warning message to the standard error output if
14133 @var{called-macro-name} has already been called.  @var{this-macro-name}
14134 should be the name of the macro that is calling @code{AC_BEFORE}.  The
14135 macro @var{called-macro-name} must have been defined using
14136 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
14137 that it has been called.
14138 @end defmac
14140 @node One-Shot Macros
14141 @subsection One-Shot Macros
14142 @cindex One-shot macros
14143 @cindex Macros, called once
14145 Some macros should be called only once, either because calling them
14146 multiple time is unsafe, or because it is bad style.  For instance
14147 Autoconf ensures that @code{AC_CANONICAL_BUILD} and cousins
14148 (@pxref{Canonicalizing}) are evaluated only once, because it makes no
14149 sense to run these expensive checks more than once.  Such one-shot
14150 macros can be defined using @code{AC_DEFUN_ONCE}.
14152 @defmac AC_DEFUN_ONCE (@var{macro-name}, @var{macro-body})
14153 @acindex{DEFUN_ONCE}
14154 Declare macro @var{macro-name} like @code{AC_DEFUN} would (@pxref{Macro
14155 Definitions}), but add additional logic that guarantees that only the
14156 first use of the macro (whether by direct expansion or
14157 @code{AC_REQUIRE}) causes an expansion of @var{macro-body}; the
14158 expansion will occur before the start of any enclosing macro defined by
14159 @code{AC_DEFUN}.  Subsequent expansions are silently ignored.
14160 Generally, it does not make sense for @var{macro-body} to use parameters
14161 such as @code{$1}.
14162 @end defmac
14164 Prior to Autoconf 2.64, a macro defined by @code{AC_DEFUN_ONCE} would
14165 emit a warning if it was directly expanded a second time, so for
14166 portability, it is better to use @code{AC_REQUIRE} than direct
14167 invocation of @var{macro-name} inside a macro defined by @code{AC_DEFUN}
14168 (@pxref{Prerequisite Macros}).
14170 @node Obsoleting Macros
14171 @section Obsoleting Macros
14172 @cindex Obsoleting macros
14173 @cindex Macros, obsoleting
14175 Configuration and portability technology has evolved over the years.
14176 Often better ways of solving a particular problem are developed, or
14177 ad-hoc approaches are systematized.  This process has occurred in many
14178 parts of Autoconf.  One result is that some of the macros are now
14179 considered @dfn{obsolete}; they still work, but are no longer considered
14180 the best thing to do, hence they should be replaced with more modern
14181 macros.  Ideally, @command{autoupdate} should replace the old macro calls
14182 with their modern implementation.
14184 Autoconf provides a simple means to obsolete a macro.
14186 @anchor{AU_DEFUN}
14187 @defmac AU_DEFUN (@var{old-macro}, @var{implementation}, @ovar{message})
14188 @auindex{DEFUN}
14189 Define @var{old-macro} as @var{implementation}.  The only difference
14190 with @code{AC_DEFUN} is that the user is warned that
14191 @var{old-macro} is now obsolete.
14193 If she then uses @command{autoupdate}, the call to @var{old-macro} is
14194 replaced by the modern @var{implementation}.  @var{message} should
14195 include information on what to do after running @command{autoupdate};
14196 @command{autoupdate} prints it as a warning, and includes it
14197 in the updated @file{configure.ac} file.
14199 The details of this macro are hairy: if @command{autoconf} encounters an
14200 @code{AU_DEFUN}ed macro, all macros inside its second argument are expanded
14201 as usual.  However, when @command{autoupdate} is run, only M4 and M4sugar
14202 macros are expanded here, while all other macros are disabled and
14203 appear literally in the updated @file{configure.ac}.
14204 @end defmac
14206 @defmac AU_ALIAS (@var{old-name}, @var{new-name})
14207 @auindex{ALIAS}
14208 Used if the @var{old-name} is to be replaced by a call to @var{new-macro}
14209 with the same parameters.  This happens for example if the macro was renamed.
14210 @end defmac
14212 @node Coding Style
14213 @section Coding Style
14214 @cindex Coding style
14216 The Autoconf macros follow a strict coding style.  You are encouraged to
14217 follow this style, especially if you intend to distribute your macro,
14218 either by contributing it to Autoconf itself or the
14219 @uref{http://@/www.gnu.org/@/software/@/autoconf-archive/, Autoconf Macro
14220 Archive}, or by other means.
14222 The first requirement is to pay great attention to the quotation.  For
14223 more details, see @ref{Autoconf Language}, and @ref{M4 Quotation}.
14225 Do not try to invent new interfaces.  It is likely that there is a macro
14226 in Autoconf that resembles the macro you are defining: try to stick to
14227 this existing interface (order of arguments, default values, etc.).  We
14228 @emph{are} conscious that some of these interfaces are not perfect;
14229 nevertheless, when harmless, homogeneity should be preferred over
14230 creativity.
14232 Be careful about clashes both between M4 symbols and between shell
14233 variables.
14235 If you stick to the suggested M4 naming scheme (@pxref{Macro Names}),
14236 you are unlikely to generate conflicts.  Nevertheless, when you need to
14237 set a special value, @emph{avoid using a regular macro name}; rather,
14238 use an ``impossible'' name.  For instance, up to version 2.13, the macro
14239 @code{AC_SUBST} used to remember what @var{symbol} macros were already defined
14240 by setting @code{AC_SUBST_@var{symbol}}, which is a regular macro name.
14241 But since there is a macro named @code{AC_SUBST_FILE}, it was just
14242 impossible to @samp{AC_SUBST(FILE)}!  In this case,
14243 @code{AC_SUBST(@var{symbol})} or @code{_AC_SUBST(@var{symbol})} should
14244 have been used (yes, with the parentheses).
14245 @c or better yet, high-level macros such as @code{m4_expand_once}
14247 No Autoconf macro should ever enter the user-variable name space; i.e.,
14248 except for the variables that are the actual result of running the
14249 macro, all shell variables should start with @code{ac_}.  In
14250 addition, small macros or any macro that is likely to be embedded in
14251 other macros should be careful not to use obvious names.
14253 @cindex @code{dnl}
14254 Do not use @code{dnl} to introduce comments: most of the comments you
14255 are likely to write are either header comments which are not output
14256 anyway, or comments that should make their way into @file{configure}.
14257 There are exceptional cases where you do want to comment special M4
14258 constructs, in which case @code{dnl} is right, but keep in mind that it
14259 is unlikely.
14261 M4 ignores the leading blanks and newlines before each argument.
14262 Use this feature to
14263 indent in such a way that arguments are (more or less) aligned with the
14264 opening parenthesis of the macro being called.  For instance, instead of
14266 @example
14267 AC_CACHE_CHECK(for EMX OS/2 environment,
14268 ac_cv_emxos2,
14269 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, [return __EMX__;])],
14270 [ac_cv_emxos2=yes], [ac_cv_emxos2=no])])
14271 @end example
14273 @noindent
14274 write
14276 @example
14277 AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
14278 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
14279                    [ac_cv_emxos2=yes],
14280                    [ac_cv_emxos2=no])])
14281 @end example
14283 @noindent
14284 or even
14286 @example
14287 AC_CACHE_CHECK([for EMX OS/2 environment],
14288                [ac_cv_emxos2],
14289                [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
14290                                                    [return __EMX__;])],
14291                                   [ac_cv_emxos2=yes],
14292                                   [ac_cv_emxos2=no])])
14293 @end example
14295 When using @code{AC_RUN_IFELSE} or any macro that cannot work when
14296 cross-compiling, provide a pessimistic value (typically @samp{no}).
14298 Feel free to use various tricks to prevent auxiliary tools, such as
14299 syntax-highlighting editors, from behaving improperly.  For instance,
14300 instead of:
14302 @example
14303 m4_bpatsubst([$1], [$"])
14304 @end example
14306 @noindent
14309 @example
14310 m4_bpatsubst([$1], [$""])
14311 @end example
14313 @noindent
14314 so that Emacsen do not open an endless ``string'' at the first quote.
14315 For the same reasons, avoid:
14317 @example
14318 test $[#] != 0
14319 @end example
14321 @noindent
14322 and use:
14324 @example
14325 test $[@@%:@@] != 0
14326 @end example
14328 @noindent
14329 Otherwise, the closing bracket would be hidden inside a @samp{#}-comment,
14330 breaking the bracket-matching highlighting from Emacsen.  Note the
14331 preferred style to escape from M4: @samp{$[1]}, @samp{$[@@]}, etc.  Do
14332 not escape when it is unnecessary.  Common examples of useless quotation
14333 are @samp{[$]$1} (write @samp{$$1}), @samp{[$]var} (use @samp{$var}),
14334 etc.  If you add portability issues to the picture, you'll prefer
14335 @samp{$@{1+"$[@@]"@}} to @samp{"[$]@@"}, and you'll prefer do something
14336 better than hacking Autoconf @code{:-)}.
14338 When using @command{sed}, don't use @option{-e} except for indenting
14339 purposes.  With the @code{s} and @code{y} commands, the preferred
14340 separator is @samp{/} unless @samp{/} itself might appear in the pattern
14341 or replacement, in which case you should use @samp{|}, or optionally
14342 @samp{,} if you know the pattern and replacement cannot contain a file
14343 name.  If none of these characters will do, choose a printable character
14344 that cannot appear in the pattern or replacement.  Characters from the
14345 set @samp{"#$&'()*;<=>?`|~} are good choices if the pattern or
14346 replacement might contain a file name, since they have special meaning
14347 to the shell and are less likely to occur in file names.
14349 @xref{Macro Definitions}, for details on how to define a macro.  If a
14350 macro doesn't use @code{AC_REQUIRE}, is expected to never be the object
14351 of an @code{AC_REQUIRE} directive, and macros required by other macros
14352 inside arguments do not need to be expanded before this macro, then
14353 use @code{m4_define}.  In case of doubt, use @code{AC_DEFUN}.
14354 Also take into account that public third-party macros need to use
14355 @code{AC_DEFUN} in order to be found by @command{aclocal}
14356 (@pxref{Extending aclocal,,, automake, GNU Automake}).
14357 All the @code{AC_REQUIRE} statements should be at the beginning of the
14358 macro, and each statement should be followed by @code{dnl}.
14360 You should not rely on the number of arguments: instead of checking
14361 whether an argument is missing, test that it is not empty.  It provides
14362 both a simpler and a more predictable interface to the user, and saves
14363 room for further arguments.
14365 Unless the macro is short, try to leave the closing @samp{])} at the
14366 beginning of a line, followed by a comment that repeats the name of the
14367 macro being defined.  This introduces an additional newline in
14368 @command{configure}; normally, that is not a problem, but if you want to
14369 remove it you can use @samp{[]dnl} on the last line.  You can similarly
14370 use @samp{[]dnl} after a macro call to remove its newline.  @samp{[]dnl}
14371 is recommended instead of @samp{dnl} to ensure that M4 does not
14372 interpret the @samp{dnl} as being attached to the preceding text or
14373 macro output.  For example, instead of:
14375 @example
14376 AC_DEFUN([AC_PATH_X],
14377 [AC_MSG_CHECKING([for X])
14378 AC_REQUIRE_CPP()
14379 @r{# @dots{}omitted@dots{}}
14380   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
14381 fi])
14382 @end example
14384 @noindent
14385 you would write:
14387 @example
14388 AC_DEFUN([AC_PATH_X],
14389 [AC_REQUIRE_CPP()[]dnl
14390 AC_MSG_CHECKING([for X])
14391 @r{# @dots{}omitted@dots{}}
14392   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
14393 fi[]dnl
14394 ])# AC_PATH_X
14395 @end example
14397 If the macro is long, try to split it into logical chunks.  Typically,
14398 macros that check for a bug in a function and prepare its
14399 @code{AC_LIBOBJ} replacement should have an auxiliary macro to perform
14400 this setup.  Do not hesitate to introduce auxiliary macros to factor
14401 your code.
14403 In order to highlight the recommended coding style, here is a macro
14404 written the old way:
14406 @example
14407 dnl Check for EMX on OS/2.
14408 dnl _AC_EMXOS2
14409 AC_DEFUN(_AC_EMXOS2,
14410 [AC_CACHE_CHECK(for EMX OS/2 environment, ac_cv_emxos2,
14411 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, return __EMX__;)],
14412 ac_cv_emxos2=yes, ac_cv_emxos2=no)])
14413 test "x$ac_cv_emxos2" = xyes && EMXOS2=yes])
14414 @end example
14416 @noindent
14417 and the new way:
14419 @example
14420 # _AC_EMXOS2
14421 # ----------
14422 # Check for EMX on OS/2.
14423 m4_define([_AC_EMXOS2],
14424 [AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
14425 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
14426                    [ac_cv_emxos2=yes],
14427                    [ac_cv_emxos2=no])])
14428 test "x$ac_cv_emxos2" = xyes && EMXOS2=yes[]dnl
14429 ])# _AC_EMXOS2
14430 @end example
14435 @c ============================================= Portable Shell Programming
14437 @node Portable Shell
14438 @chapter Portable Shell Programming
14439 @cindex Portable shell programming
14441 When writing your own checks, there are some shell-script programming
14442 techniques you should avoid in order to make your code portable.  The
14443 Bourne shell and upward-compatible shells like the Korn shell and Bash
14444 have evolved over the years, and many features added to the original
14445 System7 shell are now supported on all interesting porting targets.
14446 However, the following discussion between Russ Allbery and Robert Lipe
14447 is worth reading:
14449 @noindent
14450 Russ Allbery:
14452 @quotation
14453 The GNU assumption that @command{/bin/sh} is the one and only shell
14454 leads to a permanent deadlock.  Vendors don't want to break users'
14455 existing shell scripts, and there are some corner cases in the Bourne
14456 shell that are not completely compatible with a Posix shell.  Thus,
14457 vendors who have taken this route will @emph{never} (OK@dots{}``never say
14458 never'') replace the Bourne shell (as @command{/bin/sh}) with a
14459 Posix shell.
14460 @end quotation
14462 @noindent
14463 Robert Lipe:
14465 @quotation
14466 This is exactly the problem.  While most (at least most System V's) do
14467 have a Bourne shell that accepts shell functions most vendor
14468 @command{/bin/sh} programs are not the Posix shell.
14470 So while most modern systems do have a shell @emph{somewhere} that meets the
14471 Posix standard, the challenge is to find it.
14472 @end quotation
14474 For this reason, part of the job of M4sh (@pxref{Programming in M4sh})
14475 is to find such a shell.  But to prevent trouble, if you're not using
14476 M4sh you should not take advantage of features that were added after Unix
14477 version 7, circa 1977 (@pxref{Systemology}); you should not use aliases,
14478 negated character classes, or even @command{unset}.  @code{#} comments,
14479 while not in Unix version 7, were retrofitted in the original Bourne
14480 shell and can be assumed to be part of the least common denominator.
14482 On the other hand, if you're using M4sh you can assume that the shell
14483 has the features that were added in SVR2 (circa 1984), including shell
14484 functions,
14485 @command{return}, @command{unset}, and I/O redirection for builtins.  For
14486 more information, refer to @uref{http://@/www.in-ulm.de/@/~mascheck/@/bourne/}.
14487 However, some pitfalls have to be avoided for portable use of these
14488 constructs; these will be documented in the rest of this chapter.
14489 See in particular @ref{Shell Functions} and @ref{Limitations of
14490 Builtins, , Limitations of Shell Builtins}.
14492 Some ancient systems have quite
14493 small limits on the length of the @samp{#!} line; for instance, 32
14494 bytes (not including the newline) on SunOS 4.
14495 However, these ancient systems are no longer of practical concern.
14497 The set of external programs you should run in a @command{configure} script
14498 is fairly small.  @xref{Utilities in Makefiles, , Utilities in
14499 Makefiles, standards, GNU Coding Standards}, for the list.  This
14500 restriction allows users to start out with a fairly small set of
14501 programs and build the rest, avoiding too many interdependencies between
14502 packages.
14504 Some of these external utilities have a portable subset of features; see
14505 @ref{Limitations of Usual Tools}.
14507 There are other sources of documentation about shells.  The
14508 specification for the Posix
14509 @uref{http://@/www.opengroup.org/@/susv3/@/utilities/@/xcu_chap02@/.html, Shell
14510 Command Language}, though more generous than the restrictive shell
14511 subset described above, is fairly portable nowadays.  Also please see
14512 @uref{http://@/www.faqs.org/@/faqs/@/unix-faq/@/shell/, the Shell FAQs}.
14514 @menu
14515 * Shellology::                  A zoology of shells
14516 * Here-Documents::              Quirks and tricks
14517 * File Descriptors::            FDs and redirections
14518 * File System Conventions::     File names
14519 * Shell Pattern Matching::      Pattern matching
14520 * Shell Substitutions::         Variable and command expansions
14521 * Assignments::                 Varying side effects of assignments
14522 * Parentheses::                 Parentheses in shell scripts
14523 * Slashes::                     Slashes in shell scripts
14524 * Special Shell Variables::     Variables you should not change
14525 * Shell Functions::             What to look out for if you use them
14526 * Limitations of Builtins::     Portable use of not so portable /bin/sh
14527 * Limitations of Usual Tools::  Portable use of portable tools
14528 @end menu
14530 @node Shellology
14531 @section Shellology
14532 @cindex Shellology
14534 There are several families of shells, most prominently the Bourne family
14535 and the C shell family which are deeply incompatible.  If you want to
14536 write portable shell scripts, avoid members of the C shell family.  The
14537 @uref{http://@/www.faqs.org/@/faqs/@/unix-faq/@/shell/@/shell-differences/, the
14538 Shell difference FAQ} includes a small history of Posix shells, and a
14539 comparison between several of them.
14541 Below we describe some of the members of the Bourne shell family.
14543 @table @asis
14544 @item Ash
14545 @cindex Ash
14546 Ash is often used on GNU/Linux and BSD
14547 systems as a light-weight Bourne-compatible shell.  Ash 0.2 has some
14548 bugs that are fixed in the 0.3.x series, but portable shell scripts
14549 should work around them, since version 0.2 is still shipped with many
14550 GNU/Linux distributions.
14552 To be compatible with Ash 0.2:
14554 @itemize @minus
14555 @item
14556 don't use @samp{$?} after expanding empty or unset variables,
14557 or at the start of an @command{eval}:
14559 @example
14560 foo=
14561 false
14562 $foo
14563 echo "Do not use it: $?"
14564 false
14565 eval 'echo "Do not use it: $?"'
14566 @end example
14568 @item
14569 don't use command substitution within variable expansion:
14571 @example
14572 cat $@{FOO=`bar`@}
14573 @end example
14575 @item
14576 beware that single builtin substitutions are not performed by a
14577 subshell, hence their effect applies to the current shell!  @xref{Shell
14578 Substitutions}, item ``Command Substitution''.
14579 @end itemize
14581 @item Bash
14582 @cindex Bash
14583 To detect whether you are running Bash, test whether
14584 @code{BASH_VERSION} is set.  To require
14585 Posix compatibility, run @samp{set -o posix}.  @xref{Bash POSIX
14586 Mode, , Bash Posix Mode, bash, The GNU Bash Reference
14587 Manual}, for details.
14589 @item Bash 2.05 and later
14590 @cindex Bash 2.05 and later
14591 Versions 2.05 and later of Bash use a different format for the
14592 output of the @command{set} builtin, designed to make evaluating its
14593 output easier.  However, this output is not compatible with earlier
14594 versions of Bash (or with many other shells, probably).  So if
14595 you use Bash 2.05 or higher to execute @command{configure},
14596 you'll need to use Bash 2.05 for all other build tasks as well.
14598 @item Ksh
14599 @cindex Ksh
14600 @cindex Korn shell
14601 @prindex @samp{ksh}
14602 @prindex @samp{ksh88}
14603 @prindex @samp{ksh93}
14604 The Korn shell is compatible with the Bourne family and it mostly
14605 conforms to Posix.  It has two major variants commonly
14606 called @samp{ksh88} and @samp{ksh93}, named after the years of initial
14607 release.  It is usually called @command{ksh}, but is called @command{sh}
14608 on some hosts if you set your path appropriately.
14610 Solaris systems have three variants:
14611 @prindex @command{/usr/bin/ksh} on Solaris
14612 @command{/usr/bin/ksh} is @samp{ksh88}; it is
14613 standard on Solaris 2.0 and later.
14614 @prindex @command{/usr/xpg4/bin/sh} on Solaris
14615 @command{/usr/xpg4/bin/sh} is a Posix-compliant variant of
14616 @samp{ksh88}; it is standard on Solaris 9 and later.
14617 @prindex @command{/usr/dt/bin/dtksh} on Solaris
14618 @command{/usr/dt/bin/dtksh} is @samp{ksh93}.
14619 Variants that are not standard may be parts of optional
14620 packages.  There is no extra charge for these packages, but they are
14621 not part of a minimal OS install and therefore some installations may
14622 not have it.
14624 Starting with Tru64 Version 4.0, the Korn shell @command{/usr/bin/ksh}
14625 is also available as @command{/usr/bin/posix/sh}.  If the environment
14626 variable @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
14627 the standard shell conform to Posix.
14629 @item Pdksh
14630 @prindex @samp{pdksh}
14631 @anchor{pdksh}
14632 A public-domain clone of the Korn shell called @command{pdksh} is widely
14633 available: it has most of the @samp{ksh88} features along with a few of
14634 its own.  It usually sets @code{KSH_VERSION}, except if invoked as
14635 @command{/bin/sh} on OpenBSD, and similarly to Bash you can require
14636 Posix compatibility by running @samp{set -o posix}.  Unfortunately, with
14637 @command{pdksh} 5.2.14 (the latest stable version as of January 2007)
14638 Posix mode is buggy and causes @command{pdksh} to depart from Posix in
14639 at least one respect:
14641 @example
14642 $ @kbd{echo "`echo \"hello\"`"}
14643 hello
14644 $ @kbd{set -o posix}
14645 $ @kbd{echo "`echo \"hello\"`"}
14646 "hello"
14647 @end example
14649 The last line of output contains spurious quotes.  This is yet another
14650 reason why portable shell code should not contain
14651 @code{"`@dots{}\"@dots{}\"@dots{}`"} constructs (@pxref{Shell
14652 Substitutions}).
14654 @item Zsh
14655 @cindex Zsh
14656 To detect whether you are running @command{zsh}, test whether
14657 @code{ZSH_VERSION} is set.  By default @command{zsh} is @emph{not}
14658 compatible with the Bourne shell: you must execute @samp{emulate sh},
14659 and for @command{zsh} versions before 3.1.6-dev-18 you must also
14660 set @code{NULLCMD} to @samp{:}.  @xref{Compatibility, , Compatibility,
14661 zsh, The Z Shell Manual}, for details.
14663 The default Mac OS X @command{sh} was originally Zsh; it was changed to
14664 Bash in Mac OS X 10.2.
14665 @end table
14667 @node Here-Documents
14668 @section Here-Documents
14669 @cindex Here-documents
14670 @cindex Shell here-documents
14672 Don't rely on @samp{\} being preserved just because it has no special
14673 meaning together with the next symbol.  In the native @command{sh}
14674 on OpenBSD 2.7 @samp{\"} expands to @samp{"} in here-documents with
14675 unquoted delimiter.  As a general rule, if @samp{\\} expands to @samp{\}
14676 use @samp{\\} to get @samp{\}.
14678 With OpenBSD 2.7's @command{sh}
14680 @example
14681 @group
14682 $ @kbd{cat <<EOF
14683 > \" \\
14684 > EOF}
14685 " \
14686 @end group
14687 @end example
14689 @noindent
14690 and with Bash:
14692 @example
14693 @group
14694 bash-2.04$ @kbd{cat <<EOF
14695 > \" \\
14696 > EOF}
14697 \" \
14698 @end group
14699 @end example
14701 Some shells mishandle large here-documents: for example,
14702 Solaris 10 @command{dtksh} and the UnixWare 7.1.1 Posix shell, which are
14703 derived from Korn shell version M-12/28/93d, mishandle braced variable
14704 expansion that crosses a 1024- or 4096-byte buffer boundary
14705 within a here-document.  Only the part of the variable name after the boundary
14706 is used.  For example, @code{$@{variable@}} could be replaced by the expansion
14707 of @code{$@{ble@}}.  If the end of the variable name is aligned with the block
14708 boundary, the shell reports an error, as if you used @code{$@{@}}.
14709 Instead of @code{$@{variable-default@}}, the shell may expand
14710 @code{$@{riable-default@}}, or even @code{$@{fault@}}.  This bug can often
14711 be worked around by omitting the braces: @code{$variable}.  The bug was
14712 fixed in
14713 @samp{ksh93g} (1998-04-30) but as of 2006 many operating systems were
14714 still shipping older versions with the bug.
14716 Many shells (including the Bourne shell) implement here-documents
14717 inefficiently.  In particular, some shells can be extremely inefficient when
14718 a single statement contains many here-documents.  For instance if your
14719 @file{configure.ac} includes something like:
14721 @example
14722 @group
14723 if <cross_compiling>; then
14724   assume this and that
14725 else
14726   check this
14727   check that
14728   check something else
14729   @dots{}
14730   on and on forever
14731   @dots{}
14733 @end group
14734 @end example
14736 A shell parses the whole @code{if}/@code{fi} construct, creating
14737 temporary files for each here-document in it.  Some shells create links
14738 for such here-documents on every @code{fork}, so that the clean-up code
14739 they had installed correctly removes them.  It is creating the links
14740 that can take the shell forever.
14742 Moving the tests out of the @code{if}/@code{fi}, or creating multiple
14743 @code{if}/@code{fi} constructs, would improve the performance
14744 significantly.  Anyway, this kind of construct is not exactly the
14745 typical use of Autoconf.  In fact, it's even not recommended, because M4
14746 macros can't look into shell conditionals, so we may fail to expand a
14747 macro when it was expanded before in a conditional path, and the
14748 condition turned out to be false at runtime, and we end up not
14749 executing the macro at all.
14751 Be careful with the use of @samp{<<-} to unindent here-documents.  The
14752 behavior is only portable for stripping leading @key{TAB}s, and things
14753 can silently break if an overzealous editor converts to using leading
14754 spaces (not all shells are nice enough to warn about unterminated
14755 here-documents).
14757 @example
14758 $ @kbd{printf 'cat <<-x\n\t1\n\t 2\n\tx\n' | bash && echo done}
14761 done
14762 $ @kbd{printf 'cat <<-x\n 1\n  2\n x\n' | bash-3.2 && echo done}
14764   2
14766 done
14767 @end example
14769 @node File Descriptors
14770 @section File Descriptors
14771 @cindex Descriptors
14772 @cindex File descriptors
14773 @cindex Shell file descriptors
14775 Most shells, if not all (including Bash, Zsh, Ash), output traces on
14776 stderr, even for subshells.  This might result in undesirable content
14777 if you meant to capture the standard-error output of the inner command:
14779 @example
14780 $ @kbd{ash -x -c '(eval "echo foo >&2") 2>stderr'}
14781 $ @kbd{cat stderr}
14782 + eval echo foo >&2
14783 + echo foo
14785 $ @kbd{bash -x -c '(eval "echo foo >&2") 2>stderr'}
14786 $ @kbd{cat stderr}
14787 + eval 'echo foo >&2'
14788 ++ echo foo
14790 $ @kbd{zsh -x -c '(eval "echo foo >&2") 2>stderr'}
14791 @i{# Traces on startup files deleted here.}
14792 $ @kbd{cat stderr}
14793 +zsh:1> eval echo foo >&2
14794 +zsh:1> echo foo
14796 @end example
14798 @noindent
14799 One workaround is to grep out uninteresting lines, hoping not to remove
14800 good ones.
14802 If you intend to redirect both standard error and standard output,
14803 redirect standard output first.  This works better with HP-UX,
14804 since its shell mishandles tracing if standard error is redirected
14805 first:
14807 @example
14808 $ @kbd{sh -x -c ': 2>err >out'}
14809 + :
14810 + 2> err $ @kbd{cat err}
14811 1> out
14812 @end example
14814 Don't try to redirect the standard error of a command substitution.  It
14815 must be done @emph{inside} the command substitution.  When running
14816 @samp{: `cd /zorglub` 2>/dev/null} expect the error message to
14817 escape, while @samp{: `cd /zorglub 2>/dev/null`} works properly.
14819 On the other hand, some shells, such as Solaris or FreeBSD
14820 @command{/bin/sh}, warn about missing programs before performing
14821 redirections.  Therefore, to silently check whether a program exists, it
14822 is necessary to perform redirections on a subshell:
14824 @example
14825 $ @kbd{/bin/sh -c 'nosuch 2>/dev/null'}
14826 nosuch: not found
14827 $ @kbd{/bin/sh -c '(nosuch) 2>/dev/null'}
14828 $ @kbd{bash -c 'nosuch 2>/dev/null'}
14829 @end example
14831 FreeBSD 6.2 sh may mix the trace output lines from the statements in a
14832 shell pipeline.
14834 It is worth noting that Zsh (but not Ash nor Bash) makes it possible
14835 in assignments though: @samp{foo=`cd /zorglub` 2>/dev/null}.
14837 Some shells, like @command{ash}, don't recognize bi-directional
14838 redirection (@samp{<>}).  And even on shells that recognize it, it is
14839 not portable to use on fifos: Posix does not require read-write support
14840 for named pipes, and Cygwin does not support it:
14842 @example
14843 $ @kbd{mkfifo fifo}
14844 $ @kbd{exec 5<>fifo}
14845 $ @kbd{echo hi >&5}
14846 bash: echo: write error: Communication error on send
14847 @end example
14849 When catering to old systems, don't redirect the same file descriptor
14850 several times, as you are doomed to failure under Ultrix.
14852 @example
14853 ULTRIX V4.4 (Rev. 69) System #31: Thu Aug 10 19:42:23 GMT 1995
14854 UWS V4.4 (Rev. 11)
14855 $ @kbd{eval 'echo matter >fullness' >void}
14856 illegal io
14857 $ @kbd{eval '(echo matter >fullness)' >void}
14858 illegal io
14859 $ @kbd{(eval '(echo matter >fullness)') >void}
14860 Ambiguous output redirect.
14861 @end example
14863 @noindent
14864 In each case the expected result is of course @file{fullness} containing
14865 @samp{matter} and @file{void} being empty.  However, this bug is
14866 probably not of practical concern to modern platforms.
14868 Solaris 10 @command{sh} will try to optimize away a @command{:} command
14869 in a loop after the first iteration, even if it is redirected:
14871 @example
14872 $ @kbd{for i in 1 2 3 ; do : >x$i; done}
14873 $ @kbd{ls}
14875 @end example
14877 @noindent
14878 As a workaround, @command{echo} or @command{eval} can be used.
14880 Don't rely on file descriptors 0, 1, and 2 remaining closed in a
14881 subsidiary program.  If any of these descriptors is closed, the
14882 operating system may open an unspecified file for the descriptor in the
14883 new process image.  Posix says this may be done only if the subsidiary
14884 program is set-user-ID or set-group-ID, but HP-UX 11.23 does
14885 it even for ordinary programs.
14887 Don't rely on open file descriptors being open in child processes.  In
14888 @command{ksh}, file descriptors above 2 which are opened using
14889 @samp{exec @var{n}>file} are closed by a subsequent @samp{exec} (such as
14890 that involved in the fork-and-exec which runs a program or script).
14891 Thus, using @command{sh}, we have:
14893 @example
14894 $ @kbd{cat ./descrips}
14895 #!/bin/sh -
14896 echo hello >&5
14897 $ @kbd{exec 5>t}
14898 $ @kbd{./descrips}
14899 $ @kbd{cat t}
14900 hello
14902 @end example
14904 @noindent
14905 But using ksh:
14907 @example
14908 $ @kbd{exec 5>t}
14909 $ @kbd{./descrips}
14910 hello
14911 $ @kbd{cat t}
14913 @end example
14915 @noindent
14916 Within the process which runs the @samp{descrips} script, file
14917 descriptor 5 is closed.
14919 Don't rely on redirection to a closed file descriptor to cause an
14920 error.  With Solaris @command{/bin/sh}, when the redirection fails, the
14921 output goes to the original file descriptor.
14923 @example
14924 $ @kbd{bash -c 'echo hi >&3' 3>&-; echo $?}
14925 bash: 3: Bad file descriptor
14927 $ @kbd{/bin/sh -c 'echo hi >&3' 3>&-; echo $?}
14930 @end example
14932 DOS variants cannot rename or remove open files, such as in
14933 @samp{mv foo bar >foo} or @samp{rm foo >foo}, even though this is
14934 perfectly portable among Posix hosts.
14936 A few ancient systems reserved some file descriptors.  By convention,
14937 file descriptor 3 was opened to @file{/dev/tty} when you logged into
14938 Eighth Edition (1985) through Tenth Edition Unix (1989).  File
14939 descriptor 4 had a special use on the Stardent/Kubota Titan (circa
14940 1990), though we don't now remember what it was.  Both these systems are
14941 obsolete, so it's now safe to treat file descriptors 3 and 4 like any
14942 other file descriptors.
14944 @node File System Conventions
14945 @section File System Conventions
14946 @cindex File system conventions
14948 Autoconf uses shell-script processing extensively, so the file names
14949 that it processes should not contain characters that are special to the
14950 shell.  Special characters include space, tab, newline, NUL, and
14951 the following:
14953 @example
14954 " # $ & ' ( ) * ; < = > ? [ \ ` |
14955 @end example
14957 Also, file names should not begin with @samp{~} or @samp{-}, and should
14958 contain neither @samp{-} immediately after @samp{/} nor @samp{~}
14959 immediately after @samp{:}.  On Posix-like platforms, directory names
14960 should not contain @samp{:}, as this runs afoul of @samp{:} used as the
14961 path separator.
14963 These restrictions apply not only to the files that you distribute, but
14964 also to the absolute file names of your source, build, and destination
14965 directories.
14967 On some Posix-like platforms, @samp{!} and @samp{^} are special too, so
14968 they should be avoided.
14970 Posix lets implementations treat leading @file{//} specially, but
14971 requires leading @file{///} and beyond to be equivalent to @file{/}.
14972 Most Unix variants treat @file{//} like @file{/}.  However, some treat
14973 @file{//} as a ``super-root'' that can provide access to files that are
14974 not otherwise reachable from @file{/}.  The super-root tradition began
14975 with Apollo Domain/OS, which died out long ago, but unfortunately Cygwin
14976 has revived it.
14978 While @command{autoconf} and friends are usually run on some Posix
14979 variety, they can be used on other systems, most notably DOS
14980 variants.  This impacts several assumptions regarding file names.
14982 @noindent
14983 For example, the following code:
14985 @example
14986 case $foo_dir in
14987   /*) # Absolute
14988      ;;
14989   *)
14990      foo_dir=$dots$foo_dir ;;
14991 esac
14992 @end example
14994 @noindent
14995 fails to properly detect absolute file names on those systems, because
14996 they can use a drivespec, and usually use a backslash as directory
14997 separator.  If you want to be portable to DOS variants (at the
14998 price of rejecting valid but oddball Posix file names like @file{a:\b}),
14999 you can check for absolute file names like this:
15001 @cindex absolute file names, detect
15002 @example
15003 case $foo_dir in
15004   [\\/]* | ?:[\\/]* ) # Absolute
15005      ;;
15006   *)
15007      foo_dir=$dots$foo_dir ;;
15008 esac
15009 @end example
15011 @noindent
15012 Make sure you quote the brackets if appropriate and keep the backslash as
15013 first character (@pxref{case, , Limitations of Shell Builtins}).
15015 Also, because the colon is used as part of a drivespec, these systems don't
15016 use it as path separator.  When creating or accessing paths, you can use the
15017 @code{PATH_SEPARATOR} output variable instead.  @command{configure} sets this
15018 to the appropriate value for the build system (@samp{:} or @samp{;}) when it
15019 starts up.
15021 File names need extra care as well.  While DOS variants
15022 that are Posixy enough to run @command{autoconf} (such as DJGPP)
15023 are usually able to handle long file names properly, there are still
15024 limitations that can seriously break packages.  Several of these issues
15025 can be easily detected by the
15026 @uref{ftp://@/ftp.gnu.org/@/gnu/@/non-gnu/@/doschk/@/doschk-1.1.tar.gz, doschk}
15027 package.
15029 A short overview follows; problems are marked with SFN/LFN to
15030 indicate where they apply: SFN means the issues are only relevant to
15031 plain DOS, not to DOS under Microsoft Windows
15032 variants, while LFN identifies problems that exist even under
15033 Microsoft Windows variants.
15035 @table @asis
15036 @item No multiple dots (SFN)
15037 DOS cannot handle multiple dots in file names.  This is an especially
15038 important thing to remember when building a portable configure script,
15039 as @command{autoconf} uses a .in suffix for template files.
15041 This is perfectly OK on Posix variants:
15043 @example
15044 AC_CONFIG_HEADERS([config.h])
15045 AC_CONFIG_FILES([source.c foo.bar])
15046 AC_OUTPUT
15047 @end example
15049 @noindent
15050 but it causes problems on DOS, as it requires @samp{config.h.in},
15051 @samp{source.c.in} and @samp{foo.bar.in}.  To make your package more portable
15052 to DOS-based environments, you should use this instead:
15054 @example
15055 AC_CONFIG_HEADERS([config.h:config.hin])
15056 AC_CONFIG_FILES([source.c:source.cin foo.bar:foobar.in])
15057 AC_OUTPUT
15058 @end example
15060 @item No leading dot (SFN)
15061 DOS cannot handle file names that start with a dot.  This is usually
15062 not important for @command{autoconf}.
15064 @item Case insensitivity (LFN)
15065 DOS is case insensitive, so you cannot, for example, have both a
15066 file called @samp{INSTALL} and a directory called @samp{install}.  This
15067 also affects @command{make}; if there's a file called @samp{INSTALL} in
15068 the directory, @samp{make install} does nothing (unless the
15069 @samp{install} target is marked as PHONY).
15071 @item The 8+3 limit (SFN)
15072 Because the DOS file system only stores the first 8 characters of
15073 the file name and the first 3 of the extension, those must be unique.
15074 That means that @file{foobar-part1.c}, @file{foobar-part2.c} and
15075 @file{foobar-prettybird.c} all resolve to the same file name
15076 (@file{FOOBAR-P.C}).  The same goes for @file{foo.bar} and
15077 @file{foo.bartender}.
15079 The 8+3 limit is not usually a problem under Microsoft Windows, as it
15080 uses numeric
15081 tails in the short version of file names to make them unique.  However, a
15082 registry setting can turn this behavior off.  While this makes it
15083 possible to share file trees containing long file names between SFN
15084 and LFN environments, it also means the above problem applies there
15085 as well.
15087 @item Invalid characters (LFN)
15088 Some characters are invalid in DOS file names, and should therefore
15089 be avoided.  In a LFN environment, these are @samp{/}, @samp{\},
15090 @samp{?}, @samp{*}, @samp{:}, @samp{<}, @samp{>}, @samp{|} and @samp{"}.
15091 In a SFN environment, other characters are also invalid.  These
15092 include @samp{+}, @samp{,}, @samp{[} and @samp{]}.
15094 @item Invalid names (LFN)
15095 Some DOS file names are reserved, and cause problems if you
15096 try to use files with those names.  These names include @file{CON},
15097 @file{AUX}, @file{COM1}, @file{COM2}, @file{COM3}, @file{COM4},
15098 @file{LPT1}, @file{LPT2}, @file{LPT3}, @file{NUL}, and @file{PRN}.
15099 File names are case insensitive, so even names like
15100 @file{aux/config.guess} are disallowed.
15102 @end table
15104 @node Shell Pattern Matching
15105 @section Shell Pattern Matching
15106 @cindex Shell pattern matching
15108 Nowadays portable patterns can use negated character classes like
15109 @samp{[!-aeiou]}.  The older syntax @samp{[^-aeiou]} is supported by
15110 some shells but not others; hence portable scripts should never use
15111 @samp{^} as the first character of a bracket pattern.
15113 Outside the C locale, patterns like @samp{[a-z]} are problematic since
15114 they may match characters that are not lower-case letters.
15116 @node Shell Substitutions
15117 @section Shell Substitutions
15118 @cindex Shell substitutions
15120 Contrary to a persistent urban legend, the Bourne shell does not
15121 systematically split variables and back-quoted expressions, in particular
15122 on the right-hand side of assignments and in the argument of @code{case}.
15123 For instance, the following code:
15125 @example
15126 case "$given_srcdir" in
15127 .)  top_srcdir="`echo "$dots" | sed 's|/$||'`" ;;
15128 *)  top_srcdir="$dots$given_srcdir" ;;
15129 esac
15130 @end example
15132 @noindent
15133 is more readable when written as:
15135 @example
15136 case $given_srcdir in
15137 .)  top_srcdir=`echo "$dots" | sed 's|/$||'` ;;
15138 *)  top_srcdir=$dots$given_srcdir ;;
15139 esac
15140 @end example
15142 @noindent
15143 and in fact it is even @emph{more} portable: in the first case of the
15144 first attempt, the computation of @code{top_srcdir} is not portable,
15145 since not all shells properly understand @code{"`@dots{}"@dots{}"@dots{}`"},
15146 for example Solaris 10 ksh:
15148 @example
15149 $ @kbd{foo="`echo " bar" | sed 's, ,,'`"}
15150 ksh: : cannot execute
15151 ksh: bar | sed 's, ,,': cannot execute
15152 @end example
15154 @noindent
15155 Worse yet, not all shells understand @code{"`@dots{}\"@dots{}\"@dots{}`"}
15156 the same way (@pxref{pdksh}).  There is just no portable way to use double-quoted
15157 strings inside double-quoted back-quoted expressions (pfew!).
15159 @table @code
15160 @item $@@
15161 @cindex @samp{"$@@"}
15162 One of the most famous shell-portability issues is related to
15163 @samp{"$@@"}.  When there are no positional arguments, Posix says
15164 that @samp{"$@@"} is supposed to be equivalent to nothing, but the
15165 original Unix version 7 Bourne shell treated it as equivalent to
15166 @samp{""} instead, and this behavior survives in later implementations
15167 like Digital Unix 5.0.
15169 The traditional way to work around this portability problem is to use
15170 @samp{$@{1+"$@@"@}}.  Unfortunately this method does not work with
15171 Zsh (3.x and 4.x), which is used on Mac OS X@.  When emulating
15172 the Bourne shell, Zsh performs word splitting on @samp{$@{1+"$@@"@}}:
15174 @example
15175 zsh $ @kbd{emulate sh}
15176 zsh $ @kbd{for i in "$@@"; do echo $i; done}
15177 Hello World
15179 zsh $ @kbd{for i in $@{1+"$@@"@}; do echo $i; done}
15180 Hello
15181 World
15183 @end example
15185 @noindent
15186 Zsh handles plain @samp{"$@@"} properly, but we can't use plain
15187 @samp{"$@@"} because of the portability problems mentioned above.
15188 One workaround relies on Zsh's ``global aliases'' to convert
15189 @samp{$@{1+"$@@"@}} into @samp{"$@@"} by itself:
15191 @example
15192 test "$@{ZSH_VERSION+set@}" = set && alias -g '$@{1+"$@@"@}'='"$@@"'
15193 @end example
15195 Zsh only recognizes this alias when a shell word matches it exactly;
15196 @samp{"foo"$@{1+"$@@"@}} remains subject to word splitting.  Since this
15197 case always yields at least one shell word, use plain @samp{"$@@"}.
15199 A more conservative workaround is to avoid @samp{"$@@"} if it is
15200 possible that there may be no positional arguments.  For example,
15201 instead of:
15203 @example
15204 cat conftest.c "$@@"
15205 @end example
15207 you can use this instead:
15209 @example
15210 case $# in
15211 0) cat conftest.c;;
15212 *) cat conftest.c "$@@";;
15213 esac
15214 @end example
15216 Autoconf macros often use the @command{set} command to update
15217 @samp{$@@}, so if you are writing shell code intended for
15218 @command{configure} you should not assume that the value of @samp{$@@}
15219 persists for any length of time.
15222 @item $@{10@}
15223 @cindex positional parameters
15224 The 10th, 11th, @dots{} positional parameters can be accessed only after
15225 a @code{shift}.  The 7th Edition shell reported an error if given
15226 @code{$@{10@}}, and
15227 Solaris 10 @command{/bin/sh} still acts that way:
15229 @example
15230 $ @kbd{set 1 2 3 4 5 6 7 8 9 10}
15231 $ @kbd{echo $@{10@}}
15232 bad substitution
15233 @end example
15235 @item $@{@var{var}:-@var{value}@}
15236 @c Info cannot handle `:' in index entries.
15237 @ifnotinfo
15238 @cindex $@{@var{var}:-@var{value}@}
15239 @end ifnotinfo
15240 Old BSD shells, including the Ultrix @code{sh}, don't accept the
15241 colon for any shell substitution, and complain and die.
15242 Similarly for $@{@var{var}:=@var{value}@}, $@{@var{var}:?@var{value}@}, etc.
15244 @item $@{@var{var}=@var{literal}@}
15245 @cindex $@{@var{var}=@var{literal}@}
15246 Be sure to quote:
15248 @example
15249 : $@{var='Some words'@}
15250 @end example
15252 @noindent
15253 otherwise some shells, such as on Digital Unix V 5.0, die because
15254 of a ``bad substitution''.
15256 @sp 1
15258 Solaris @command{/bin/sh} has a frightening bug in its interpretation
15259 of this.  Imagine you need set a variable to a string containing
15260 @samp{@}}.  This @samp{@}} character confuses Solaris @command{/bin/sh}
15261 when the affected variable was already set.  This bug can be exercised
15262 by running:
15264 @example
15265 $ @kbd{unset foo}
15266 $ @kbd{foo=$@{foo='@}'@}}
15267 $ @kbd{echo $foo}
15269 $ @kbd{foo=$@{foo='@}'   # no error; this hints to what the bug is}
15270 $ @kbd{echo $foo}
15272 $ @kbd{foo=$@{foo='@}'@}}
15273 $ @kbd{echo $foo}
15274 @}@}
15275  ^ ugh!
15276 @end example
15278 It seems that @samp{@}} is interpreted as matching @samp{$@{}, even
15279 though it is enclosed in single quotes.  The problem doesn't happen
15280 using double quotes.
15282 @item $@{@var{var}=@var{expanded-value}@}
15283 @cindex $@{@var{var}=@var{expanded-value}@}
15284 On Ultrix,
15285 running
15287 @example
15288 default="yu,yaa"
15289 : $@{var="$default"@}
15290 @end example
15292 @noindent
15293 sets @var{var} to @samp{M-yM-uM-,M-yM-aM-a}, i.e., the 8th bit of
15294 each char is set.  You don't observe the phenomenon using a simple
15295 @samp{echo $var} since apparently the shell resets the 8th bit when it
15296 expands $var.  Here are two means to make this shell confess its sins:
15298 @example
15299 $ @kbd{cat -v <<EOF
15300 $var
15301 EOF}
15302 @end example
15304 @noindent
15307 @example
15308 $ @kbd{set | grep '^var=' | cat -v}
15309 @end example
15311 One classic incarnation of this bug is:
15313 @example
15314 default="a b c"
15315 : $@{list="$default"@}
15316 for c in $list; do
15317   echo $c
15318 done
15319 @end example
15321 @noindent
15322 You'll get @samp{a b c} on a single line.  Why?  Because there are no
15323 spaces in @samp{$list}: there are @samp{M- }, i.e., spaces with the 8th
15324 bit set, hence no IFS splitting is performed!!!
15326 One piece of good news is that Ultrix works fine with @samp{:
15327 $@{list=$default@}}; i.e., if you @emph{don't} quote.  The bad news is
15328 then that QNX 4.25 then sets @var{list} to the @emph{last} item of
15329 @var{default}!
15331 The portable way out consists in using a double assignment, to switch
15332 the 8th bit twice on Ultrix:
15334 @example
15335 list=$@{list="$default"@}
15336 @end example
15338 @noindent
15339 @dots{}but beware of the @samp{@}} bug from Solaris (see above).  For safety,
15340 use:
15342 @example
15343 test "$@{var+set@}" = set || var=@var{@{value@}}
15344 @end example
15346 @item $@{#@var{var}@}
15347 @itemx $@{@var{var}%@var{word}@}
15348 @itemx $@{@var{var}%%@var{word}@}
15349 @itemx $@{@var{var}#@var{word}@}
15350 @itemx $@{@var{var}##@var{word}@}
15351 @cindex $@{#@var{var}@}
15352 @cindex $@{@var{var}%@var{word}@}
15353 @cindex $@{@var{var}%%@var{word}@}
15354 @cindex $@{@var{var}#@var{word}@}
15355 @cindex $@{@var{var}##@var{word}@}
15356 Posix requires support for these usages, but they do not work with many
15357 traditional shells, e.g., Solaris 10 @command{/bin/sh}.
15359 Also, @command{pdksh} 5.2.14 mishandles some @var{word} forms.  For
15360 example if @samp{$1} is @samp{a/b} and @samp{$2} is @samp{a}, then
15361 @samp{$@{1#$2@}} should yield @samp{/b}, but with @command{pdksh} it
15362 yields the empty string.
15365 @item `@var{commands}`
15366 @cindex `@var{commands}`
15367 @cindex Command Substitution
15368 Posix requires shells to trim all trailing newlines from command
15369 output before substituting it, so assignments like
15370 @samp{dir=`echo "$file" | tr a A`} do not work as expected if
15371 @samp{$file} ends in a newline.
15373 While in general it makes no sense, do not substitute a single builtin
15374 with side effects, because Ash 0.2, trying to optimize, does not fork a
15375 subshell to perform the command.
15377 For instance, if you wanted to check that @command{cd} is silent, do not
15378 use @samp{test -z "`cd /`"} because the following can happen:
15380 @example
15381 $ @kbd{pwd}
15382 /tmp
15383 $ @kbd{test -z "`cd /`" && pwd}
15385 @end example
15387 @noindent
15388 The result of @samp{foo=`exit 1`} is left as an exercise to the reader.
15390 The MSYS shell leaves a stray byte in the expansion of a double-quoted
15391 command substitution of a native program, if the end of the substitution
15392 is not aligned with the end of the double quote.  This may be worked
15393 around by inserting another pair of quotes:
15395 @example
15396 $ @kbd{echo "`printf 'foo\r\n'` bar" > broken}
15397 $ @kbd{echo "`printf 'foo\r\n'`"" bar" | cmp - broken}
15398 - broken differ: char 4, line 1
15399 @end example
15401 Upon interrupt or SIGTERM, some shells may abort a command substitution,
15402 replace it with a null string, and wrongly evaluate the enclosing
15403 command before entering the trap or ending the script.  This can lead to
15404 spurious errors:
15406 @example
15407 $ @kbd{sh -c 'if test `sleep 5; echo hi` = hi; then echo yes; fi'}
15408 $ @kbd{^C}
15409 sh: test: hi: unexpected operator/operand
15410 @end example
15412 @noindent
15413 You can avoid this by assigning the command substitution to a temporary
15414 variable:
15416 @example
15417 $ @kbd{sh -c 'res=`sleep 5; echo hi`
15418          if test "x$res" = xhi; then echo yes; fi'}
15419 $ @kbd{^C}
15420 @end example
15422 @item $(@var{commands})
15423 @cindex $(@var{commands})
15424 This construct is meant to replace @samp{`@var{commands}`},
15425 and it has most of the problems listed under @code{`@var{commands}`}.
15427 This construct can be
15428 nested while this is impossible to do portably with back quotes.
15429 Unfortunately it is not yet universally supported.  Most notably, even recent
15430 releases of Solaris don't support it:
15432 @example
15433 $ @kbd{showrev -c /bin/sh | grep version}
15434 Command version: SunOS 5.10 Generic 121005-03 Oct 2006
15435 $ @kbd{echo $(echo blah)}
15436 syntax error: `(' unexpected
15437 @end example
15439 @noindent
15440 nor does IRIX 6.5's Bourne shell:
15441 @example
15442 $ @kbd{uname -a}
15443 IRIX firebird-image 6.5 07151432 IP22
15444 $ @kbd{echo $(echo blah)}
15445 $(echo blah)
15446 @end example
15448 If you do use @samp{$(@var{commands})}, make sure that the commands
15449 do not start with a parenthesis, as that would cause confusion with
15450 a different notation @samp{$((@var{expression}))} that in modern
15451 shells is an arithmetic expression not a command.  To avoid the
15452 confusion, insert a space between the two opening parentheses.
15454 Avoid @var{commands} that contain unbalanced parentheses in
15455 here-documents, comments, or case statement patterns, as many shells
15456 mishandle them.  For example, Bash 3.1, @samp{ksh88}, @command{pdksh}
15457 5.2.14, and Zsh 4.2.6 all mishandle the following valid command:
15459 @example
15460 echo $(case x in x) echo hello;; esac)
15461 @end example
15464 @item $((@var{expression}))
15465 @cindex $((@var{expression}))
15466 Arithmetic expansion is not portable as some shells (most
15467 notably Solaris 10 @command{/bin/sh}) don't support it.
15469 Among shells that do support @samp{$(( ))}, not all of them obey the
15470 Posix rule that octal and hexadecimal constants must be recognized:
15472 @example
15473 $ @kbd{bash -c 'echo $(( 010 + 0x10 ))'}
15475 $ @kbd{zsh -c 'echo $(( 010 + 0x10 ))'}
15477 $ @kbd{zsh -c 'emulate sh; echo $(( 010 + 0x10 ))'}
15479 $ @kbd{pdksh -c 'echo $(( 010 + 0x10 ))'}
15480 pdksh:  010 + 0x10 : bad number `0x10'
15481 $ @kbd{pdksh -c 'echo $(( 010 ))'}
15483 @end example
15485 When it is available, using arithmetic expansion provides a noticeable
15486 speedup in script execution; but testing for support requires
15487 @command{eval} to avoid syntax errors.  The following construct is used
15488 by @code{AS_VAR_ARITH} to provide arithmetic computation when all
15489 arguments are provided in decimal and without a leading zero, and all
15490 operators are properly quoted and appear as distinct arguments:
15492 @example
15493 if ( eval 'test $(( 1 + 1 )) = 2' ) 2>/dev/null; then
15494   eval 'func_arith ()
15495   @{
15496     func_arith_result=$(( $* ))
15497   @}'
15498 else
15499   func_arith ()
15500   @{
15501     func_arith_result=`expr "$@@"`
15502   @}
15504 func_arith 1 + 1
15505 foo=$func_arith_result
15506 @end example
15509 @item ^
15510 @cindex ^ quoting
15511 Always quote @samp{^}, otherwise traditional shells such as
15512 @command{/bin/sh} on Solaris 10 treat this like @samp{|}.
15514 @end table
15517 @node Assignments
15518 @section Assignments
15519 @cindex Shell assignments
15521 When setting several variables in a row, be aware that the order of the
15522 evaluation is undefined.  For instance @samp{foo=1 foo=2; echo $foo}
15523 gives @samp{1} with Solaris @command{/bin/sh}, but @samp{2} with Bash.
15524 You must use
15525 @samp{;} to enforce the order: @samp{foo=1; foo=2; echo $foo}.
15527 Don't rely on the following to find @file{subdir/program}:
15529 @example
15530 PATH=subdir$PATH_SEPARATOR$PATH program
15531 @end example
15533 @noindent
15534 as this does not work with Zsh 3.0.6.  Use something like this
15535 instead:
15537 @example
15538 (PATH=subdir$PATH_SEPARATOR$PATH; export PATH; exec program)
15539 @end example
15541 Don't rely on the exit status of an assignment: Ash 0.2 does not change
15542 the status and propagates that of the last statement:
15544 @example
15545 $ @kbd{false || foo=bar; echo $?}
15547 $ @kbd{false || foo=`:`; echo $?}
15549 @end example
15551 @noindent
15552 and to make things even worse, QNX 4.25 just sets the exit status
15553 to 0 in any case:
15555 @example
15556 $ @kbd{foo=`exit 1`; echo $?}
15558 @end example
15560 To assign default values, follow this algorithm:
15562 @enumerate
15563 @item
15564 If the default value is a literal and does not contain any closing
15565 brace, use:
15567 @example
15568 : $@{var='my literal'@}
15569 @end example
15571 @item
15572 If the default value contains no closing brace, has to be expanded, and
15573 the variable being initialized is not intended to be IFS-split
15574 (i.e., it's not a list), then use:
15576 @example
15577 : $@{var="$default"@}
15578 @end example
15580 @item
15581 If the default value contains no closing brace, has to be expanded, and
15582 the variable being initialized is intended to be IFS-split (i.e., it's a list),
15583 then use:
15585 @example
15586 var=$@{var="$default"@}
15587 @end example
15589 @item
15590 If the default value contains a closing brace, then use:
15592 @example
15593 test "$@{var+set@}" = set || var="has a '@}'"
15594 @end example
15595 @end enumerate
15597 In most cases @samp{var=$@{var="$default"@}} is fine, but in case of
15598 doubt, just use the last form.  @xref{Shell Substitutions}, items
15599 @samp{$@{@var{var}:-@var{value}@}} and @samp{$@{@var{var}=@var{value}@}}
15600 for the rationale.
15602 @node Parentheses
15603 @section Parentheses in Shell Scripts
15604 @cindex Shell parentheses
15606 Beware of two opening parentheses in a row, as many shell
15607 implementations treat them specially.  Posix requires that the command
15608 @samp{((cat))} must behave like @samp{(cat)}, but many shells, including
15609 Bash and the Korn shell, treat @samp{((cat))} as an arithmetic
15610 expression equivalent to @samp{let "cat"}, and may or may not report an
15611 error when they detect that @samp{cat} is not a number.  As another
15612 example, @samp{pdksh} 5.2.14 misparses the following code:
15614 @example
15615 if ((true) || false); then
15616   echo ok
15618 @end example
15620 @noindent
15621 To work around this problem, insert a space between the two opening
15622 parentheses.  There is a similar problem and workaround with
15623 @samp{$((}; see @ref{Shell Substitutions}.
15625 @node Slashes
15626 @section Slashes in Shell Scripts
15627 @cindex Shell slashes
15629 Unpatched Tru64 5.1 @command{sh} omits the last slash of command-line
15630 arguments that contain two trailing slashes:
15632 @example
15633 $ @kbd{echo / // /// //// .// //.}
15634 / / // /// ./ //.
15635 $ @kbd{x=//}
15636 $ @kbd{eval "echo \$x"}
15638 $ @kbd{set -x}
15639 $ @kbd{echo abc | tr -t ab //}
15640 + echo abc
15641 + tr -t ab /
15643 @end example
15645 Unpatched Tru64 4.0 @command{sh} adds a slash after @samp{"$var"} if the
15646 variable is empty and the second double-quote is followed by a word that
15647 begins and ends with slash:
15649 @example
15650 $ @kbd{sh -xc 'p=; echo "$p"/ouch/'}
15652 + echo //ouch/
15653 //ouch/
15654 @end example
15656 However, our understanding is that patches are available, so perhaps
15657 it's not worth worrying about working around these horrendous bugs.
15659 @node Special Shell Variables
15660 @section Special Shell Variables
15661 @cindex Shell variables
15662 @cindex Special shell variables
15664 Some shell variables should not be used, since they can have a deep
15665 influence on the behavior of the shell.  In order to recover a sane
15666 behavior from the shell, some variables should be unset; M4sh takes
15667 care of this and provides fallback values, whenever needed, to cater
15668 for a very old @file{/bin/sh} that does not support @command{unset}.
15669 (@pxref{Portable Shell, , Portable Shell Programming}).
15671 As a general rule, shell variable names containing a lower-case letter
15672 are safe; you can define and use these variables without worrying about
15673 their effect on the underlying system, and without worrying about
15674 whether the shell changes them unexpectedly.  (The exception is the
15675 shell variable @code{status}, as described below.)
15677 Here is a list of names that are known to cause trouble.  This list is
15678 not exhaustive, but you should be safe if you avoid the name
15679 @code{status} and names containing only upper-case letters and
15680 underscores.
15682 @c Alphabetical order, case insensitive, `A' before `a'.
15683 @table @code
15684 @item ?
15685 Not all shells correctly reset @samp{$?} after conditionals (@pxref{if,
15686 , Limitations of Shell Builtins}).  Not all shells manage @samp{$?}
15687 correctly in shell functions (@pxref{Shell Functions}) or in traps
15688 (@pxref{trap, , Limitations of Shell Builtins}).  Not all shells reset
15689 @samp{$?} to zero after an empty command.
15691 @example
15692 $ @kbd{bash -c 'false; $empty; echo $?'}
15694 $ @kbd{zsh -c 'false; $empty; echo $?'}
15696 @end example
15698 @item _
15699 @evindex _
15700 Many shells reserve @samp{$_} for various purposes, e.g., the name of
15701 the last command executed.
15703 @item BIN_SH
15704 @evindex BIN_SH
15705 In Tru64, if @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
15706 the standard shell conform to Posix.
15708 @item CDPATH
15709 @evindex CDPATH
15710 When this variable is set it specifies a list of directories to search
15711 when invoking @code{cd} with a relative file name that did not start
15712 with @samp{./} or @samp{../}.  Posix
15713 1003.1-2001 says that if a nonempty directory name from @env{CDPATH}
15714 is used successfully, @code{cd} prints the resulting absolute
15715 file name.  Unfortunately this output can break idioms like
15716 @samp{abs=`cd src && pwd`} because @code{abs} receives the name twice.
15717 Also, many shells do not conform to this part of Posix; for
15718 example, @command{zsh} prints the result only if a directory name
15719 other than @file{.} was chosen from @env{CDPATH}.
15721 In practice the shells that have this problem also support
15722 @command{unset}, so you can work around the problem as follows:
15724 @example
15725 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
15726 @end example
15728 You can also avoid output by ensuring that your directory name is
15729 absolute or anchored at @samp{./}, as in @samp{abs=`cd ./src && pwd`}.
15731 Configure scripts use M4sh, which automatically unsets @env{CDPATH} if
15732 possible, so you need not worry about this problem in those scripts.
15734 @item CLICOLOR_FORCE
15735 @evindex CLICOLOR_FORCE
15736 When this variable is set, some implementations of tools like
15737 @command{ls} attempt to add color to their output via terminal escape
15738 sequences, even when the output is not directed to a terminal, and can
15739 thus cause spurious failures in scripts.  Configure scripts use M4sh,
15740 which automatically unsets this variable.
15742 @item DUALCASE
15743 @evindex DUALCASE
15744 In the MKS shell, case statements and file name generation are
15745 case-insensitive unless @env{DUALCASE} is nonzero.
15746 Autoconf-generated scripts export this variable when they start up.
15748 @item ENV
15749 @itemx MAIL
15750 @itemx MAILPATH
15751 @itemx PS1
15752 @itemx PS2
15753 @itemx PS4
15754 @evindex ENV
15755 @evindex MAIL
15756 @evindex MAILPATH
15757 @evindex PS1
15758 @evindex PS2
15759 @evindex PS4
15760 These variables should not matter for shell scripts, since they are
15761 supposed to affect only interactive shells.  However, at least one
15762 shell (the pre-3.0 UWIN Korn shell) gets confused about
15763 whether it is interactive, which means that (for example) a @env{PS1}
15764 with a side effect can unexpectedly modify @samp{$?}.  To work around
15765 this bug, M4sh scripts (including @file{configure} scripts) do something
15766 like this:
15768 @example
15769 (unset ENV) >/dev/null 2>&1 && unset ENV MAIL MAILPATH
15770 PS1='$ '
15771 PS2='> '
15772 PS4='+ '
15773 @end example
15775 @noindent
15776 (actually, there is some complication due to bugs in @command{unset};
15777 see @pxref{unset, , Limitations of Shell Builtins}).
15779 @item FPATH
15780 @evindex FPATH
15781 The Korn shell uses @env{FPATH} to find shell functions, so avoid
15782 @env{FPATH} in portable scripts.  @env{FPATH} is consulted after
15783 @env{PATH}, but you still need to be wary of tests that use @env{PATH}
15784 to find whether a command exists, since they might report the wrong
15785 result if @env{FPATH} is also set.
15787 @item GREP_OPTIONS
15788 @evindex GREP_OPTIONS
15789 When this variable is set, some implementations of @command{grep} honor
15790 these options, even if the options include direction to enable colored
15791 output via terminal escape sequences, and the result can cause spurious
15792 failures when the output is not directed to a terminal.  Configure
15793 scripts use M4sh, which automatically unsets this variable.
15795 @item IFS
15796 @evindex IFS
15797 Long ago, shell scripts inherited @env{IFS} from the environment,
15798 but this caused many problems so modern shells ignore any environment
15799 settings for @env{IFS}.
15801 Don't set the first character of @env{IFS} to backslash.  Indeed,
15802 Bourne shells use the first character (backslash) when joining the
15803 components in @samp{"$@@"} and some shells then reinterpret (!)@: the
15804 backslash escapes, so you can end up with backspace and other strange
15805 characters.
15807 The proper value for @env{IFS} (in regular code, not when performing
15808 splits) is @samp{@key{SPC}@key{TAB}@key{RET}}.  The first character is
15809 especially important, as it is used to join the arguments in @samp{$*};
15810 however, note that traditional shells, but also bash-2.04, fail to adhere
15811 to this and join with a space anyway.
15813 M4sh guarantees that @env{IFS} will have the default value at the
15814 beginning of a script, and many macros within autoconf rely on this
15815 setting.  It is okay to use blocks of shell code that temporarily change
15816 the value of @env{IFS} in order to split on another character, but
15817 remember to restore it before expanding further macros.
15819 Unsetting @code{IFS} instead of resetting it to the default sequence
15820 is not suggested, since code that tries to save and restore the
15821 variable's value will incorrectly reset it to an empty value, thus
15822 disabling field splitting:
15824 @example
15825 unset IFS
15826 # default separators used for field splitting
15828 save_IFS=$IFS
15829 IFS=:
15830 # ...
15831 IFS=$save_IFS
15832 # no field splitting performed
15833 @end example
15835 @item LANG
15836 @itemx LC_ALL
15837 @itemx LC_COLLATE
15838 @itemx LC_CTYPE
15839 @itemx LC_MESSAGES
15840 @itemx LC_MONETARY
15841 @itemx LC_NUMERIC
15842 @itemx LC_TIME
15843 @evindex LANG
15844 @evindex LC_ALL
15845 @evindex LC_COLLATE
15846 @evindex LC_CTYPE
15847 @evindex LC_MESSAGES
15848 @evindex LC_MONETARY
15849 @evindex LC_NUMERIC
15850 @evindex LC_TIME
15852 You should set all these variables to @samp{C} because so much
15853 configuration code assumes the C locale and Posix requires that locale
15854 environment variables be set to @samp{C} if the C locale is desired;
15855 @file{configure} scripts and M4sh do that for you.
15856 Export these variables after setting them.
15858 @c  However, some older, nonstandard
15859 @c  systems (notably SCO) break if locale environment variables
15860 @c  are set to @samp{C}, so when running on these systems
15861 @c  Autoconf-generated scripts unset the variables instead.
15863 @item LANGUAGE
15864 @evindex LANGUAGE
15866 @env{LANGUAGE} is not specified by Posix, but it is a GNU
15867 extension that overrides @env{LC_ALL} in some cases, so you (or M4sh)
15868 should set it too.
15870 @item LC_ADDRESS
15871 @itemx LC_IDENTIFICATION
15872 @itemx LC_MEASUREMENT
15873 @itemx LC_NAME
15874 @itemx LC_PAPER
15875 @itemx LC_TELEPHONE
15876 @evindex LC_ADDRESS
15877 @evindex LC_IDENTIFICATION
15878 @evindex LC_MEASUREMENT
15879 @evindex LC_NAME
15880 @evindex LC_PAPER
15881 @evindex LC_TELEPHONE
15883 These locale environment variables are GNU extensions.  They
15884 are treated like their Posix brethren (@env{LC_COLLATE},
15885 etc.)@: as described above.
15887 @item LINENO
15888 @evindex LINENO
15889 Most modern shells provide the current line number in @code{LINENO}.
15890 Its value is the line number of the beginning of the current command.
15891 M4sh, and hence Autoconf, attempts to execute @command{configure} with
15892 a shell that supports @code{LINENO}.  If no such shell is available, it
15893 attempts to implement @code{LINENO} with a Sed prepass that replaces each
15894 instance of the string @code{$LINENO} (not followed by an alphanumeric
15895 character) with the line's number.  In M4sh scripts you should execute
15896 @code{AS_LINENO_PREPARE} so that these workarounds are included in
15897 your script; configure scripts do this automatically in @code{AC_INIT}.
15899 You should not rely on @code{LINENO} within @command{eval} or shell
15900 functions, as the behavior differs in practice.  The presence of a
15901 quoted newline within simple commands can alter which line number is
15902 used as the starting point for @code{$LINENO} substitutions within that
15903 command.  Also, the possibility of the Sed prepass means that you should
15904 not rely on @code{$LINENO} when quoted, when in here-documents, or when
15905 line continuations are used.  Subshells should be OK, though.  In the
15906 following example, lines 1, 9, and 14 are portable, but the other
15907 instances of @code{$LINENO} do not have deterministic values:
15909 @example
15910 @group
15911 $ @kbd{cat lineno}
15912 echo 1. $LINENO
15913 echo "2. $LINENO
15914 3. $LINENO"
15915 cat <<EOF
15916 5. $LINENO
15917 6. $LINENO
15918 7. \$LINENO
15920 ( echo 9. $LINENO )
15921 eval 'echo 10. $LINENO'
15922 eval 'echo 11. $LINENO
15923 echo 12. $LINENO'
15924 echo 13. '$LINENO'
15925 echo 14. $LINENO '
15926 15.' $LINENO
15927 f () @{ echo $1 $LINENO;
15928 echo $1 $LINENO @}
15929 f 18.
15930 echo 19. \
15931 $LINENO
15932 @end group
15933 @group
15934 $ @kbd{bash-3.2 ./lineno}
15935 1. 1
15936 2. 3
15937 3. 3
15938 5. 4
15939 6. 4
15940 7. $LINENO
15941 9. 9
15942 10. 10
15943 11. 12
15944 12. 13
15945 13. $LINENO
15946 14. 14
15947 15. 14
15948 18. 16
15949 18. 17
15950 19. 19
15951 @end group
15952 @group
15953 $ @kbd{zsh-4.3.4 ./lineno}
15954 1. 1
15955 2. 2
15956 3. 2
15957 5. 4
15958 6. 4
15959 7. $LINENO
15960 9. 9
15961 10. 1
15962 11. 1
15963 12. 2
15964 13. $LINENO
15965 14. 14
15966 15. 14
15967 18. 0
15968 18. 1
15969 19. 19
15970 @end group
15971 @group
15972 $ @kbd{pdksh-5.2.14 ./lineno}
15973 1. 1
15974 2. 2
15975 3. 2
15976 5. 4
15977 6. 4
15978 7. $LINENO
15979 9. 9
15980 10. 0
15981 11. 0
15982 12. 0
15983 13. $LINENO
15984 14. 14
15985 15. 14
15986 18. 16
15987 18. 17
15988 19. 19
15989 @end group
15990 @group
15991 $ @kbd{sed '=' <lineno |}
15992 > @kbd{  sed '}
15993 > @kbd{    N}
15994 > @kbd{    s,$,-,}
15995 > @kbd{    t loop}
15996 > @kbd{    :loop}
15997 > @kbd{    s,^\([0-9]*\)\(.*\)[$]LINENO\([^a-zA-Z0-9_]\),\1\2\1\3,}
15998 > @kbd{    t loop}
15999 > @kbd{    s,-$,,}
16000 > @kbd{    s,^[0-9]*\n,,}
16001 > @kbd{  ' |}
16002 > @kbd{  sh}
16003 1. 1
16004 2. 2
16005 3. 3
16006 5. 5
16007 6. 6
16008 7. \7
16009 9. 9
16010 10. 10
16011 11. 11
16012 12. 12
16013 13. 13
16014 14. 14
16015 15. 15
16016 18. 16
16017 18. 17
16018 19. 20
16019 @end group
16020 @end example
16022 In particular, note that @file{config.status} (and any other subsidiary
16023 script created by @code{AS_INIT_GENERATED}) might report line numbers
16024 relative to the parent script as a result of the potential Sed pass.
16026 @item NULLCMD
16027 @evindex NULLCMD
16028 When executing the command @samp{>foo}, @command{zsh} executes
16029 @samp{$NULLCMD >foo} unless it is operating in Bourne shell
16030 compatibility mode and the @command{zsh} version is newer
16031 than 3.1.6-dev-18.  If you are using an older @command{zsh}
16032 and forget to set @env{NULLCMD},
16033 your script might be suspended waiting for data on its standard input.
16035 @item PATH_SEPARATOR
16036 @evindex PATH_SEPARATOR
16037 On DJGPP systems, the @env{PATH_SEPARATOR} environment
16038 variable can be set to either @samp{:} or @samp{;} to control the path
16039 separator Bash uses to set up certain environment variables (such as
16040 @env{PATH}).  You can set this variable to @samp{;} if you want
16041 @command{configure} to use @samp{;} as a separator; this might be useful
16042 if you plan to use non-Posix shells to execute files.  @xref{File System
16043 Conventions}, for more information about @code{PATH_SEPARATOR}.
16045 @item PWD
16046 @evindex PWD
16047 Posix 1003.1-2001 requires that @command{cd} and
16048 @command{pwd} must update the @env{PWD} environment variable to point
16049 to the logical name of the current directory, but traditional shells
16050 do not support this.  This can cause confusion if one shell instance
16051 maintains @env{PWD} but a subsidiary and different shell does not know
16052 about @env{PWD} and executes @command{cd}; in this case @env{PWD}
16053 points to the wrong directory.  Use @samp{`pwd`} rather than
16054 @samp{$PWD}.
16056 @item RANDOM
16057 @evindex RANDOM
16058 Many shells provide @code{RANDOM}, a variable that returns a different
16059 integer each time it is used.  Most of the time, its value does not
16060 change when it is not used, but on IRIX 6.5 the value changes all
16061 the time.  This can be observed by using @command{set}.  It is common
16062 practice to use @code{$RANDOM} as part of a file name, but code
16063 shouldn't rely on @code{$RANDOM} expanding to a nonempty string.
16065 @item status
16066 @evindex status
16067 This variable is an alias to @samp{$?} for @code{zsh} (at least 3.1.6),
16068 hence read-only.  Do not use it.
16069 @end table
16071 @node Shell Functions
16072 @section Shell Functions
16073 @cindex Shell Functions
16075 Nowadays, it is difficult to find a shell that does not support
16076 shell functions at all.  However, some differences should be expected.
16078 Inside a shell function, you should not rely on the error status of a
16079 subshell if the last command of that subshell was @code{exit} or
16080 @code{trap}, as this triggers bugs in zsh 4.x; while Autoconf tries to
16081 find a shell that does not exhibit the bug, zsh might be the only shell
16082 present on the user's machine.
16084 Likewise, the state of @samp{$?} is not reliable when entering a shell
16085 function.  This has the effect that using a function as the first
16086 command in a @command{trap} handler can cause problems.
16088 @example
16089 $ @kbd{bash -c 'foo()@{ echo $?; @}; trap foo 0; (exit 2); exit 2'; echo $?}
16092 $ @kbd{ash -c 'foo()@{ echo $?; @}; trap foo 0; (exit 2); exit 2'; echo $?}
16095 @end example
16097 DJGPP bash 2.04 has a bug in that @command{return} from a
16098 shell function which also used a command substitution causes a
16099 segmentation fault.  To work around the issue, you can use
16100 @command{return} from a subshell, or @samp{AS_SET_STATUS} as last command
16101 in the execution flow of the function (@pxref{Common Shell Constructs}).
16103 Not all shells treat shell functions as simple commands impacted by
16104 @samp{set -e}, for example with Solaris 10 @command{bin/sh}:
16106 @example
16107 $ @kbd{bash -c 'f()@{ return 1; @}; set -e; f; echo oops}
16108 $ @kbd{/bin/sh -c 'f()@{ return 1; @}; set -e; f; echo oops}
16109 oops
16110 @end example
16112 Shell variables and functions may share the same namespace, for example
16113 with Solaris 10 @command{/bin/sh}:
16115 @example
16116 $ @kbd{f () @{ :; @}; f=; f}
16117 f: not found
16118 @end example
16120 @noindent
16121 For this reason, Autoconf (actually M4sh, @pxref{Programming in M4sh})
16122 uses the prefix @samp{as_fn_} for its functions.
16124 Handling of positional parameters and shell options varies among shells.
16125 For example, Korn shells reset and restore trace output (@samp{set -x})
16126 and other options upon function entry and exit.  Inside a function,
16127 IRIX sh sets @samp{$0} to the function name.
16129 It is not portable to pass temporary environment variables to shell
16130 functions.  Solaris @command{/bin/sh} does not see the variable.
16131 Meanwhile, not all shells follow the Posix rule that the assignment must
16132 affect the current environment in the same manner as special built-ins.
16134 @example
16135 $ @kbd{/bin/sh -c 'func()@{ echo $a;@}; a=1 func; echo $a'}
16136 @result{}
16137 @result{}
16138 $ @kbd{ash -c 'func()@{ echo $a;@}; a=1 func; echo $a'}
16139 @result{}1
16140 @result{}
16141 $ @kbd{bash -c 'set -o posix; func()@{ echo $a;@}; a=1 func; echo $a'}
16142 @result{}1
16143 @result{}1
16144 @end example
16146 Some ancient Bourne shell variants with function support did not reset
16147 @samp{$@var{i}, @var{i} >= 0}, upon function exit, so effectively the
16148 arguments of the script were lost after the first function invocation.
16149 It is probably not worth worrying about these shells any more.
16151 With AIX sh, a @command{trap} on 0 installed in a shell function
16152 triggers at function exit rather than at script exit, see @xref{trap, ,
16153 Limitations of Shell Builtins}.
16155 @node Limitations of Builtins
16156 @section Limitations of Shell Builtins
16157 @cindex Shell builtins
16158 @cindex Limitations of shell builtins
16160 No, no, we are serious: some shells do have limitations!  :)
16162 You should always keep in mind that any builtin or command may support
16163 options, and therefore differ in behavior with arguments
16164 starting with a dash.  For instance, even the innocent @samp{echo "$word"}
16165 can give unexpected results when @code{word} starts with a dash.  It is
16166 often possible to avoid this problem using @samp{echo "x$word"}, taking
16167 the @samp{x} into account later in the pipe.  Many of these limitations
16168 can be worked around using M4sh (@pxref{Programming in M4sh}).
16170 @c This table includes things like `@command{test} (files)', so we can't
16171 @c use @table @command.
16172 @table @asis
16173 @item @command{.}
16174 @c --------------
16175 @prindex @command{.}
16176 Use @command{.} only with regular files (use @samp{test -f}).  Bash
16177 2.03, for instance, chokes on @samp{. /dev/null}.  Remember that
16178 @command{.} uses @env{PATH} if its argument contains no slashes.  Also,
16179 some shells, including bash 3.2, implicitly append the current directory
16180 to this @env{PATH} search, even though Posix forbids it.  So if you want
16181 to use @command{.} on a file @file{foo} in the current directory, you
16182 must use @samp{. ./foo}.
16184 Not all shells gracefully handle syntax errors within a sourced file.
16185 On one extreme, some non-interactive shells abort the entire script.  On
16186 the other, @command{zsh} 4.3.10 has a bug where it fails to react to the
16187 syntax error.
16189 @example
16190 $ @kbd{echo 'fi' > syntax}
16191 $ @kbd{bash -c '. ./syntax; echo $?'}
16192 ./syntax: line 1: syntax error near unexpected token `fi'
16193 ./syntax: line 1: `fi'
16195 $ @kbd{ash -c '. ./syntax; echo $?'}
16196 ./syntax: 1: Syntax error: "fi" unexpected
16197 $ @kbd{zsh -c '. ./syntax; echo $?'}
16198 ./syntax:1: parse error near `fi'
16200 @end example
16202 @item @command{!}
16203 @c --------------
16204 @prindex @command{!}
16205 The Unix version 7 shell did not support
16206 negating the exit status of commands with @command{!}, and this feature
16207 is still absent from some shells (e.g., Solaris @command{/bin/sh}).
16208 Other shells, such as FreeBSD @command{/bin/sh} or @command{ash}, have
16209 bugs when using @command{!}:
16211 @example
16212 $ @kbd{sh -c '! : | :'; echo $?}
16214 $ @kbd{ash -c '! : | :'; echo $?}
16216 $ @kbd{sh -c '! @{ :; @}'; echo $?}
16218 $ @kbd{ash -c '! @{ :; @}'; echo $?}
16219 @{: not found
16220 Syntax error: "@}" unexpected
16222 @end example
16224 Shell code like this:
16226 @example
16227 if ! cmp file1 file2 >/dev/null 2>&1; then
16228   echo files differ or trouble
16230 @end example
16232 is therefore not portable in practice.  Typically it is easy to rewrite
16233 such code, e.g.:
16235 @example
16236 cmp file1 file2 >/dev/null 2>&1 ||
16237   echo files differ or trouble
16238 @end example
16240 More generally, one can always rewrite @samp{! @var{command}} as:
16242 @example
16243 if @var{command}; then (exit 1); else :; fi
16244 @end example
16247 @item @command{@{...@}}
16248 @c --------------------
16249 @prindex @command{@{...@}}
16250 Bash 3.2 (and earlier versions) sometimes does not properly set
16251 @samp{$?} when failing to write redirected output of a compound command.
16252 This problem is most commonly observed with @samp{@{@dots{}@}}; it does
16253 not occur with @samp{(@dots{})}.  For example:
16255 @example
16256 $ @kbd{bash -c '@{ echo foo; @} >/bad; echo $?'}
16257 bash: line 1: /bad: Permission denied
16259 $ @kbd{bash -c 'while :; do echo; done >/bad; echo $?'}
16260 bash: line 1: /bad: Permission denied
16262 @end example
16264 To work around the bug, prepend @samp{:;}:
16266 @example
16267 $ @kbd{bash -c ':;@{ echo foo; @} >/bad; echo $?'}
16268 bash: line 1: /bad: Permission denied
16270 @end example
16272 Posix requires a syntax error if a brace list has no contents.  However,
16273 not all shells obey this rule; and on shells where empty lists are
16274 permitted, the effect on @samp{$?} is inconsistent.  To avoid problems,
16275 ensure that a brace list is never empty.
16277 @example
16278 $ @kbd{bash -c 'false; @{ @}; echo $?' || echo $?}
16279 bash: line 1: syntax error near unexpected token `@}'
16280 bash: line 1: `false; @{ @}; echo $?'
16282 $ @kbd{zsh -c 'false; @{ @}; echo $?' || echo $?}
16284 $ @kbd{pdksh -c 'false; @{ @}; echo $?' || echo $?}
16286 @end example
16289 @item @command{break}
16290 @c ------------------
16291 @prindex @command{break}
16292 The use of @samp{break 2} etc.@: is safe.
16295 @anchor{case}
16296 @item @command{case}
16297 @c -----------------
16298 @prindex @command{case}
16299 You don't need to quote the argument; no splitting is performed.
16301 You don't need the final @samp{;;}, but you should use it.
16303 Posix requires support for @code{case} patterns with opening
16304 parentheses like this:
16306 @example
16307 case $file_name in
16308   (*.c) echo "C source code";;
16309 esac
16310 @end example
16312 @noindent
16313 but the @code{(} in this example is not portable to many Bourne
16314 shell implementations, which is a pity for those of us using tools that
16315 rely on balanced parentheses.  For instance, with Solaris
16316 @command{/bin/sh}:
16318 @example
16319 $ @kbd{case foo in (foo) echo foo;; esac}
16320 @error{}syntax error: `(' unexpected
16321 @end example
16323 @noindent
16324 The leading @samp{(} can be omitted safely.  Unfortunately, there are
16325 contexts where unbalanced parentheses cause other problems, such as when
16326 using a syntax-highlighting editor that searches for the balancing
16327 counterpart, or more importantly, when using a case statement as an
16328 underquoted argument to an Autoconf macro.  @xref{Balancing
16329 Parentheses}, for tradeoffs involved in various styles of dealing with
16330 unbalanced @samp{)}.
16332 Zsh handles pattern fragments derived from parameter expansions or
16333 command substitutions as though quoted:
16335 @example
16336 $ pat=\?; case aa in ?$pat) echo match;; esac
16337 $ pat=\?; case a? in ?$pat) echo match;; esac
16338 match
16339 @end example
16341 @noindent
16342 Because of a bug in its @code{fnmatch}, Bash fails to properly
16343 handle backslashes in character classes:
16345 @example
16346 bash-2.02$ @kbd{case /tmp in [/\\]*) echo OK;; esac}
16347 bash-2.02$
16348 @end example
16350 @noindent
16351 This is extremely unfortunate, since you are likely to use this code to
16352 handle Posix or MS-DOS absolute file names.  To work around this
16353 bug, always put the backslash first:
16355 @example
16356 bash-2.02$ @kbd{case '\TMP' in [\\/]*) echo OK;; esac}
16358 bash-2.02$ @kbd{case /tmp in [\\/]*) echo OK;; esac}
16360 @end example
16362 Many Bourne shells cannot handle closing brackets in character classes
16363 correctly.
16365 Some shells also have problems with backslash escaping in case you do not want
16366 to match the backslash: both a backslash and the escaped character match this
16367 pattern.  To work around this, specify the character class in a variable, so
16368 that quote removal does not apply afterwards, and the special characters don't
16369 have to be backslash-escaped:
16371 @example
16372 $ @kbd{case '\' in [\<]) echo OK;; esac}
16374 $ @kbd{scanset='[<]'; case '\' in $scanset) echo OK;; esac}
16376 @end example
16378 Even with this, Solaris @command{ksh} matches a backslash if the set
16379 contains any
16380 of the characters @samp{|}, @samp{&}, @samp{(}, or @samp{)}.
16382 Conversely, Tru64 @command{ksh} (circa 2003) erroneously always matches
16383 a closing parenthesis if not specified in a character class:
16385 @example
16386 $ @kbd{case foo in *\)*) echo fail ;; esac}
16387 fail
16388 $ @kbd{case foo in *')'*) echo fail ;; esac}
16389 fail
16390 @end example
16392 Some shells, such as Ash 0.3.8, are confused by an empty
16393 @code{case}/@code{esac}:
16395 @example
16396 ash-0.3.8 $ @kbd{case foo in esac;}
16397 @error{}Syntax error: ";" unexpected (expecting ")")
16398 @end example
16400 Posix requires @command{case} to give an exit status of 0 if no cases
16401 match.  However, @command{/bin/sh} in Solaris 10 does not obey this
16402 rule.  Meanwhile, it is unclear whether a case that matches, but
16403 contains no statements, must also change the exit status to 0.  The M4sh
16404 macro @code{AS_CASE} works around these inconsistencies.
16406 @example
16407 $ @kbd{bash -c 'case `false` in ?) ;; esac; echo $?'}
16409 $ @kbd{/bin/sh -c 'case `false` in ?) ;; esac; echo $?'}
16411 @end example
16414 @item @command{cd}
16415 @c ---------------
16416 @prindex @command{cd}
16417 Posix 1003.1-2001 requires that @command{cd} must support
16418 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
16419 with @option{-L} being the default.  However, traditional shells do
16420 not support these options, and their @command{cd} command has the
16421 @option{-P} behavior.
16423 Portable scripts should assume neither option is supported, and should
16424 assume neither behavior is the default.  This can be a bit tricky,
16425 since the Posix default behavior means that, for example,
16426 @samp{ls ..} and @samp{cd ..} may refer to different directories if
16427 the current logical directory is a symbolic link.  It is safe to use
16428 @code{cd @var{dir}} if @var{dir} contains no @file{..} components.
16429 Also, Autoconf-generated scripts check for this problem when computing
16430 variables like @code{ac_top_srcdir} (@pxref{Configuration Actions}),
16431 so it is safe to @command{cd} to these variables.
16433 See @xref{Special Shell Variables}, for portability problems involving
16434 @command{cd} and the @env{CDPATH} environment variable.
16435 Also please see the discussion of the @command{pwd} command.
16438 @anchor{echo}
16439 @item @command{echo}
16440 @c -----------------
16441 @prindex @command{echo}
16442 The simple @command{echo} is probably the most surprising source of
16443 portability troubles.  It is not possible to use @samp{echo} portably
16444 unless both options and escape sequences are omitted.  Don't expect any
16445 option.
16447 Do not use backslashes in the arguments, as there is no consensus on
16448 their handling.  For @samp{echo '\n' | wc -l}, the @command{sh} of
16449 Solaris outputs 2, but Bash and Zsh (in @command{sh} emulation mode) output 1.
16450 The problem is truly @command{echo}: all the shells
16451 understand @samp{'\n'} as the string composed of a backslash and an
16452 @samp{n}.  Within a command substitution, @samp{echo 'string\c'} will
16453 mess up the internal state of ksh88 on AIX 6.1 so that it will print
16454 the first character @samp{s} only, followed by a newline, and then
16455 entirely drop the output of the next echo in a command substitution.
16457 Because of these problems, do not pass a string containing arbitrary
16458 characters to @command{echo}.  For example, @samp{echo "$foo"} is safe
16459 only if you know that @var{foo}'s value cannot contain backslashes and
16460 cannot start with @samp{-}.
16462 If this may not be true, @command{printf} is in general safer and
16463 easier to use than @command{echo} and @command{echo -n}.  Thus, scripts
16464 where portability is not a major concern should use @command{printf
16465 '%s\n'} whenever @command{echo} could fail, and similarly use
16466 @command{printf %s} instead of @command{echo -n}. For portable shell
16467 scripts, instead, it is suggested to use a here-document like this:
16469 @example
16470 cat <<EOF
16471 $foo
16473 @end example
16475 Alternatively, M4sh provides @code{AS_ECHO} and @code{AS_ECHO_N} macros
16476 which choose between various portable implementations: @samp{echo}
16477 or @samp{print} where they work, @command{printf} if it is available,
16478 or else other creative tricks in order to work around the above problems.
16481 @item @command{eval}
16482 @c -----------------
16483 @prindex @command{eval}
16484 The @command{eval} command is useful in limited circumstances, e.g.,
16485 using commands like @samp{eval table_$key=\$value} and @samp{eval
16486 value=table_$key} to simulate a hash table when the key is known to be
16487 alphanumeric.
16489 You should also be wary of common bugs in @command{eval} implementations.
16490 In some shell implementations (e.g., older @command{ash}, OpenBSD 3.8
16491 @command{sh}, @command{pdksh} v5.2.14 99/07/13.2, and @command{zsh}
16492 4.2.5), the arguments of @samp{eval} are evaluated in a context where
16493 @samp{$?} is 0, so they exhibit behavior like this:
16495 @example
16496 $ @kbd{false; eval 'echo $?'}
16498 @end example
16500 The correct behavior here is to output a nonzero value,
16501 but portable scripts should not rely on this.
16503 You should not rely on @code{LINENO} within @command{eval}.
16504 @xref{Special Shell Variables}.
16506 Note that, even though these bugs are easily avoided,
16507 @command{eval} is tricky to use on arbitrary arguments.
16508 It is obviously unwise to use @samp{eval $cmd} if the string value of
16509 @samp{cmd} was derived from an untrustworthy source.  But even if the
16510 string value is valid, @samp{eval $cmd} might not work as intended,
16511 since it causes field splitting and file name expansion to occur twice,
16512 once for the @command{eval} and once for the command itself.  It is
16513 therefore safer to use @samp{eval "$cmd"}.  For example, if @var{cmd}
16514 has the value @samp{cat test?.c}, @samp{eval $cmd} might expand to the
16515 equivalent of @samp{cat test;.c} if there happens to be a file named
16516 @file{test;.c} in the current directory; and this in turn
16517 mistakenly attempts to invoke @command{cat} on the file @file{test} and
16518 then execute the command @command{.c}.  To avoid this problem, use
16519 @samp{eval "$cmd"} rather than @samp{eval $cmd}.
16521 However, suppose that you want to output the text of the evaluated
16522 command just before executing it.  Assuming the previous example,
16523 @samp{echo "Executing: $cmd"} outputs @samp{Executing: cat test?.c}, but
16524 this output doesn't show the user that @samp{test;.c} is the actual name
16525 of the copied file.  Conversely, @samp{eval "echo Executing: $cmd"}
16526 works on this example, but it fails with @samp{cmd='cat foo >bar'},
16527 since it mistakenly replaces the contents of @file{bar} by the
16528 string @samp{cat foo}.  No simple, general, and portable solution to
16529 this problem is known.
16531 @item @command{exec}
16532 @c -----------------
16533 @prindex @command{exec}
16534 Posix describes several categories of shell built-ins.  Special
16535 built-ins (such as @command{exit}) must impact the environment of the
16536 current shell, and need not be available through @command{exec}.  All
16537 other built-ins are regular, and must not propagate variable assignments
16538 to the environment of the current shell.  However, the group of regular
16539 built-ins is further distinguished by commands that do not require a
16540 @env{PATH} search (such as @command{cd}), in contrast to built-ins that
16541 are offered as a more efficient version of something that must still be
16542 found in a @env{PATH} search (such as @command{echo}).  Posix is not
16543 clear on whether @command{exec} must work with the list of 17 utilities
16544 that are invoked without a @env{PATH} search, and many platforms lack an
16545 executable for some of those built-ins:
16547 @example
16548 $ @kbd{sh -c 'exec cd /tmp'}
16549 sh: line 0: exec: cd: not found
16550 @end example
16552 All other built-ins that provide utilities specified by Posix must have
16553 a counterpart executable that exists on @env{PATH}, although Posix
16554 allows @command{exec} to use the built-in instead of the executable.
16555 For example, contrast @command{bash} 3.2 and @command{pdksh} 5.2.14:
16557 @example
16558 $ @kbd{bash -c 'pwd --version' | head -n1}
16559 bash: line 0: pwd: --: invalid option
16560 pwd: usage: pwd [-LP]
16561 $ @kbd{bash -c 'exec pwd --version' | head -n1}
16562 pwd (GNU coreutils) 6.10
16563 $ @kbd{pdksh -c 'exec pwd --version' | head -n1}
16564 pdksh: pwd: --: unknown option
16565 @end example
16567 When it is desired to avoid a regular shell built-in, the workaround is
16568 to use some other forwarding command, such as @command{env} or
16569 @command{nice}, that will ensure a path search:
16571 @example
16572 $ @kbd{pdksh -c 'exec true --version' | head -n1}
16573 $ @kbd{pdksh -c 'nice true --version' | head -n1}
16574 true (GNU coreutils) 6.10
16575 $ @kbd{pdksh -c 'env true --version' | head -n1}
16576 true (GNU coreutils) 6.10
16577 @end example
16579 @item @command{exit}
16580 @c -----------------
16581 @prindex @command{exit}
16582 The default value of @command{exit} is supposed to be @code{$?};
16583 unfortunately, some shells, such as the DJGPP port of Bash 2.04, just
16584 perform @samp{exit 0}.
16586 @example
16587 bash-2.04$ @kbd{foo=`exit 1` || echo fail}
16588 fail
16589 bash-2.04$ @kbd{foo=`(exit 1)` || echo fail}
16590 fail
16591 bash-2.04$ @kbd{foo=`(exit 1); exit` || echo fail}
16592 bash-2.04$
16593 @end example
16595 Using @samp{exit $?} restores the expected behavior.
16597 Some shell scripts, such as those generated by @command{autoconf}, use a
16598 trap to clean up before exiting.  If the last shell command exited with
16599 nonzero status, the trap also exits with nonzero status so that the
16600 invoker can tell that an error occurred.
16602 Unfortunately, in some shells, such as Solaris @command{/bin/sh}, an exit
16603 trap ignores the @code{exit} command's argument.  In these shells, a trap
16604 cannot determine whether it was invoked by plain @code{exit} or by
16605 @code{exit 1}.  Instead of calling @code{exit} directly, use the
16606 @code{AC_MSG_ERROR} macro that has a workaround for this problem.
16609 @anchor{export}
16610 @item @command{export}
16611 @c -------------------
16612 @prindex @command{export}
16613 The builtin @command{export} dubs a shell variable @dfn{environment
16614 variable}.  Each update of exported variables corresponds to an update
16615 of the environment variables.  Conversely, each environment variable
16616 received by the shell when it is launched should be imported as a shell
16617 variable marked as exported.
16619 Alas, many shells, such as Solaris @command{/bin/sh},
16620 IRIX 6.3, IRIX 5.2,
16621 AIX 4.1.5, and Digital Unix 4.0, forget to
16622 @command{export} the environment variables they receive.  As a result,
16623 two variables coexist: the environment variable and the shell
16624 variable.  The following code demonstrates this failure:
16626 @example
16627 #!/bin/sh
16628 echo $FOO
16629 FOO=bar
16630 echo $FOO
16631 exec /bin/sh $0
16632 @end example
16634 @noindent
16635 when run with @samp{FOO=foo} in the environment, these shells print
16636 alternately @samp{foo} and @samp{bar}, although they should print only
16637 @samp{foo} and then a sequence of @samp{bar}s.
16639 Therefore you should @command{export} again each environment variable
16640 that you update; the export can occur before or after the assignment.
16642 Posix is not clear on whether the @command{export} of an undefined
16643 variable causes the variable to be defined with the value of an empty
16644 string, or merely marks any future definition of a variable by that name
16645 for export.  Various shells behave differently in this regard:
16647 @example
16648 $ @kbd{sh -c 'export foo; env | grep foo'}
16649 $ @kbd{ash -c 'export foo; env | grep foo'}
16650 foo=
16651 @end example
16653 @item @command{false}
16654 @c ------------------
16655 @prindex @command{false}
16656 Don't expect @command{false} to exit with status 1: in native
16657 Solaris @file{/bin/false} exits with status 255.
16660 @item @command{for}
16661 @c ----------------
16662 @prindex @command{for}
16663 To loop over positional arguments, use:
16665 @example
16666 for arg
16668   echo "$arg"
16669 done
16670 @end example
16672 @noindent
16673 You may @emph{not} leave the @code{do} on the same line as @code{for},
16674 since some shells improperly grok:
16676 @example
16677 for arg; do
16678   echo "$arg"
16679 done
16680 @end example
16682 If you want to explicitly refer to the positional arguments, given the
16683 @samp{$@@} bug (@pxref{Shell Substitutions}), use:
16685 @example
16686 for arg in $@{1+"$@@"@}; do
16687   echo "$arg"
16688 done
16689 @end example
16691 @noindent
16692 But keep in mind that Zsh, even in Bourne shell emulation mode, performs
16693 word splitting on @samp{$@{1+"$@@"@}}; see @ref{Shell Substitutions},
16694 item @samp{$@@}, for more.
16697 @anchor{if}
16698 @item @command{if}
16699 @c ---------------
16700 @prindex @command{if}
16701 Using @samp{!} is not portable.  Instead of:
16703 @example
16704 if ! cmp -s file file.new; then
16705   mv file.new file
16707 @end example
16709 @noindent
16710 use:
16712 @example
16713 if cmp -s file file.new; then :; else
16714   mv file.new file
16716 @end example
16718 @noindent
16719 Or, especially if the @dfn{else} branch is short, you can use @code{||}.
16720 In M4sh, the @code{AS_IF} macro provides an easy way to write these kinds
16721 of conditionals:
16723 @example
16724 AS_IF([cmp -s file file.new], [], [mv file.new file])
16725 @end example
16727 This is especially useful in other M4 macros, where the @dfn{then} and
16728 @dfn{else} branches might be macro arguments.
16730 Some very old shells did not reset the exit status from an @command{if}
16731 with no @command{else}:
16733 @example
16734 $ @kbd{if (exit 42); then true; fi; echo $?}
16736 @end example
16738 @noindent
16739 whereas a proper shell should have printed @samp{0}.  But this is no
16740 longer a portability problem; any shell that supports functions gets it
16741 correct.  However, it explains why some makefiles have lengthy
16742 constructs:
16744 @example
16745 if test -f "$file"; then
16746   install "$file" "$dest"
16747 else
16748   :
16750 @end example
16753 @item @command{printf}
16754 @c ------------------
16755 @prindex @command{printf}
16756 A format string starting with a @samp{-} can cause problems.
16757 Bash interprets it as an option and
16758 gives an error.  And @samp{--} to mark the end of options is not good
16759 in the NetBSD Almquist shell (e.g., 0.4.6) which takes that
16760 literally as the format string.  Putting the @samp{-} in a @samp{%c}
16761 or @samp{%s} is probably easiest:
16763 @example
16764 printf %s -foo
16765 @end example
16767 Bash 2.03 mishandles an escape sequence that happens to evaluate to @samp{%}:
16769 @example
16770 $ @kbd{printf '\045'}
16771 bash: printf: `%': missing format character
16772 @end example
16774 Large outputs may cause trouble.  On Solaris 2.5.1 through 10, for
16775 example, @file{/usr/bin/printf} is buggy, so when using
16776 @command{/bin/sh} the command @samp{printf %010000x 123} normally dumps
16777 core.
16779 Since @command{printf} is not always a shell builtin, there is a
16780 potential speed penalty for using @code{printf '%s\n'} as a replacement
16781 for an @command{echo} that does not interpret @samp{\} or leading
16782 @samp{-}. With Solaris @command{ksh}, it is possible to use @code{print
16783 -r --} for this role instead.
16785 For a discussion of portable alternatives to both @command{printf}
16786 and @command{echo}, @xref{echo, , Limitations of Shell Builtins}.
16789 @item @command{pwd}
16790 @c ----------------
16791 @prindex @command{pwd}
16792 With modern shells, plain @command{pwd} outputs a ``logical''
16793 directory name, some of whose components may be symbolic links.  These
16794 directory names are in contrast to ``physical'' directory names, whose
16795 components are all directories.
16797 Posix 1003.1-2001 requires that @command{pwd} must support
16798 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
16799 with @option{-L} being the default.  However, traditional shells do
16800 not support these options, and their @command{pwd} command has the
16801 @option{-P} behavior.
16803 Portable scripts should assume neither option is supported, and should
16804 assume neither behavior is the default.  Also, on many hosts
16805 @samp{/bin/pwd} is equivalent to @samp{pwd -P}, but Posix
16806 does not require this behavior and portable scripts should not rely on
16809 Typically it's best to use plain @command{pwd}.  On modern hosts this
16810 outputs logical directory names, which have the following advantages:
16812 @itemize @bullet
16813 @item
16814 Logical names are what the user specified.
16815 @item
16816 Physical names may not be portable from one installation
16817 host to another due to network file system gymnastics.
16818 @item
16819 On modern hosts @samp{pwd -P} may fail due to lack of permissions to
16820 some parent directory, but plain @command{pwd} cannot fail for this
16821 reason.
16822 @end itemize
16824 Also please see the discussion of the @command{cd} command.
16827 @item @command{read}
16828 @c -----------------
16829 @prindex @command{read}
16830 No options are portable, not even support @option{-r} (Solaris
16831 @command{/bin/sh} for example).
16834 @anchor{set}
16835 @item @command{set}
16836 @c ----------------
16837 @prindex @command{set}
16838 With the FreeBSD 6.0 shell, the @command{set} command (without
16839 any options) does not sort its output.
16841 The @command{set} builtin faces the usual problem with arguments
16842 starting with a
16843 dash.  Modern shells such as Bash or Zsh understand @option{--} to specify
16844 the end of the options (any argument after @option{--} is a parameter,
16845 even @samp{-x} for instance), but many traditional shells (e.g., Solaris
16846 10 @command{/bin/sh}) simply stop option
16847 processing as soon as a non-option argument is found.  Therefore, use
16848 @samp{dummy} or simply @samp{x} to end the option processing, and use
16849 @command{shift} to pop it out:
16851 @example
16852 set x $my_list; shift
16853 @end example
16855 Avoid @samp{set -}, e.g., @samp{set - $my_list}.  Posix no
16856 longer requires support for this command, and in traditional shells
16857 @samp{set - $my_list} resets the @option{-v} and @option{-x} options, which
16858 makes scripts harder to debug.
16860 Some nonstandard shells do not recognize more than one option
16861 (e.g., @samp{set -e -x} assigns @samp{-x} to the command line).  It is
16862 better to combine them:
16864 @example
16865 set -ex
16866 @end example
16868 @cindex @command{set -e}
16869 The option @option{-e} has historically been underspecified, with enough
16870 ambiguities to cause numerous differences across various shell
16871 implementations.  Perhaps the best reference is
16872 @uref{http://www.opengroup.org/@/austin/@/mailarchives/@/ag-review/@/msg03507.html,
16873 this link}, recommending a change to Posix 2008 to match @command{ksh88}
16874 behavior.  Note that mixing @code{set -e} and shell functions is asking
16875 for surprises:
16877 @example
16878 set -e
16879 doit()
16881   rm file
16882   echo one
16884 doit || echo two
16885 @end example
16887 @noindent
16888 According to the recommendation, @samp{one} should always be output
16889 regardless of whether the @command{rm} failed, because it occurs within
16890 the body of the shell function @samp{doit} invoked on the left side of
16891 @samp{||}, where the effects of @samp{set -e} are not enforced.
16892 Likewise, @samp{two} should never be printed, since the failure of
16893 @command{rm} does not abort the function, such that the status of
16894 @samp{doit} is 0.
16896 The BSD shell has had several problems with the @option{-e}
16897 option.  Older versions of the BSD
16898 shell (circa 1990) mishandled @samp{&&}, @samp{||}, @samp{if}, and
16899 @samp{case} when @option{-e} was in effect, causing the shell to exit
16900 unexpectedly in some cases.  This was particularly a problem with
16901 makefiles, and led to circumlocutions like @samp{sh -c 'test -f file ||
16902 touch file'}, where the seemingly-unnecessary @samp{sh -c '@dots{}'}
16903 wrapper works around the bug (@pxref{Failure in Make Rules}).
16905 Even relatively-recent versions of the BSD shell (e.g., OpenBSD 3.4)
16906 wrongly exit with @option{-e} if the last command within a compound
16907 statement fails and is guarded by an @samp{&&} only.  For example:
16909 @example
16910 #! /bin/sh
16911 set -e
16912 foo=''
16913 test -n "$foo" && exit 1
16914 echo one
16915 if :; then
16916   test -n "$foo" && exit 1
16917   echo two
16918   test -n "$foo" && exit 1
16920 echo three
16921 @end example
16923 @noindent
16924 does not print @samp{three}.  One workaround is to change the last
16925 instance of @samp{test -n "$foo" && exit 1} to be @samp{if test -n
16926 "$foo"; then exit 1; fi} instead.  Another possibility is to warn BSD
16927 users not to use @samp{sh -e}.
16929 When @samp{set -e} is in effect, a failed command substitution in
16930 Solaris @command{/bin/sh} cannot be ignored, even with @samp{||}.
16932 @example
16933 $ @kbd{/bin/sh -c 'set -e; foo=`false` || echo foo; echo bar'}
16934 $ @kbd{bash -c 'set -e; foo=`false` || echo foo; echo bar'}
16937 @end example
16939 @noindent
16940 Moreover, a command substitution, successful or not, causes this shell to
16941 exit from a failing outer command even in presence of an @samp{&&} list:
16943 @example
16944 $ @kbd{bash -c 'set -e; false `true` && echo notreached; echo ok'}
16946 $ @kbd{sh -c 'set -e; false `true` && echo notreached; echo ok'}
16948 @end example
16950 Portable scripts should not use @samp{set -e} if @command{trap} is used
16951 to install an exit handler.  This is because Tru64/OSF 5.1 @command{sh}
16952 sometimes enters the trap handler with the exit status of the command
16953 prior to the one that triggered the errexit handler:
16955 @example
16956 $ @kbd{sh -ec 'trap '\''echo $?'\'' 0; false'}
16958 $ @kbd{sh -c 'set -e; trap '\''echo $?'\'' 0; false'}
16960 @end example
16962 @noindent
16963 Thus, when writing a script in M4sh, rather than trying to rely on
16964 @samp{set -e}, it is better to append @samp{|| AS_EXIT} to any
16965 statement where it is desirable to abort on failure.
16967 @cindex @command{set -b}
16968 @cindex @command{set -m}
16969 Job control is not provided by all shells, so the use of @samp{set -m}
16970 or @samp{set -b} must be done with care.  When using @command{zsh} in
16971 native mode, asynchronous notification (@samp{set -b}) is enabled by
16972 default, and using @samp{emulate sh} to switch to Posix mode does not
16973 clear this setting (although asynchronous notification has no impact
16974 unless job monitoring is also enabled).  Also, @command{zsh} 4.3.10 and
16975 earlier have a bug where job control can be manipulated in interactive
16976 shells, but not in subshells or scripts.  Furthermore, some shells, like
16977 @command{pdksh}, fail to treat subshells as interactive, even though the
16978 parent shell was.
16980 @example
16981 $ @kbd{echo $ZSH_VERSION}
16982 4.3.10
16983 $ @kbd{set -m; echo $?}
16985 $ @kbd{zsh -c 'set -m; echo $?'}
16986 set: can't change option: -m
16987 $ @kbd{(set -m); echo $?}
16988 set: can't change option: -m
16990 $ @kbd{pdksh -ci 'echo $-; (echo $-)'}
16993 @end example
16996 @item @command{shift}
16997 @c ------------------
16998 @prindex @command{shift}
16999 Not only is @command{shift}ing a bad idea when there is nothing left to
17000 shift, but in addition it is not portable: the shell of MIPS
17001 RISC/OS 4.52 refuses to do it.
17003 Don't use @samp{shift 2} etc.; while it in the SVR1 shell (1983),
17004 it is also absent in many pre-Posix shells.
17007 @item @command{source}
17008 @c -------------------
17009 @prindex @command{source}
17010 This command is not portable, as Posix does not require it; use
17011 @command{.} instead.
17014 @item @command{test}
17015 @c -----------------
17016 @prindex @command{test}
17017 The @code{test} program is the way to perform many file and string
17018 tests.  It is often invoked by the alternate name @samp{[}, but using
17019 that name in Autoconf code is asking for trouble since it is an M4 quote
17020 character.
17022 The @option{-a}, @option{-o}, @samp{(}, and @samp{)} operands are not
17023 present in all implementations, and have been marked obsolete by Posix
17024 2008.  This is because there are inherent ambiguities in using them.
17025 For example, @samp{test "$1" -a "$2"} looks like a binary operator to
17026 check whether two strings are both non-empty, but if @samp{$1} is the
17027 literal @samp{!}, then some implementations of @command{test} treat it
17028 as a negation of the unary operator @option{-a}.
17030 Thus, portable uses of @command{test} should never have more than four
17031 arguments, and scripts should use shell constructs like @samp{&&} and
17032 @samp{||} instead.  If you combine @samp{&&} and @samp{||} in the same
17033 statement, keep in mind that they have equal precedence, so it is often
17034 better to parenthesize even when this is redundant.  For example:
17036 @smallexample
17037 # Not portable:
17038 test "X$a" = "X$b" -a \
17039   '(' "X$c" != "X$d" -o "X$e" = "X$f" ')'
17041 # Portable:
17042 test "X$a" = "X$b" &&
17043   @{ test "X$c" != "X$d" || test "X$e" = "X$f"; @}
17044 @end smallexample
17046 @command{test} does not process options like most other commands do; for
17047 example, it does not recognize the @option{--} argument as marking the
17048 end of options.
17050 It is safe to use @samp{!} as a @command{test} operator.  For example,
17051 @samp{if test ! -d foo; @dots{}} is portable even though @samp{if ! test
17052 -d foo; @dots{}} is not.
17055 @item @command{test} (files)
17056 @c -------------------------
17057 To enable @command{configure} scripts to support cross-compilation, they
17058 shouldn't do anything that tests features of the build system instead of
17059 the host system.  But occasionally you may find it necessary to check
17060 whether some arbitrary file exists.  To do so, use @samp{test -f} or
17061 @samp{test -r}.  Do not use @samp{test -x}, because 4.3BSD does not
17062 have it.  Do not use @samp{test -e} either, because Solaris @command{/bin/sh}
17063 lacks it.  To test for symbolic links on systems that have them, use
17064 @samp{test -h} rather than @samp{test -L}; either form conforms to
17065 Posix 1003.1-2001, but older shells like Solaris 8
17066 @code{/bin/sh} support only @option{-h}.
17068 @item @command{test} (strings)
17069 @c ---------------------------
17070 Posix says that @samp{test "@var{string}"} succeeds if @var{string} is
17071 not null, but this usage is not portable to traditional platforms like
17072 Solaris 10 @command{/bin/sh}, which mishandle strings like @samp{!} and
17073 @samp{-n}.
17075 Posix also says that @samp{test ! "@var{string}"},
17076 @samp{test -n "@var{string}"} and
17077 @samp{test -z "@var{string}"} work with any string, but many
17078 shells (such as Solaris, AIX 3.2, UNICOS 10.0.0.6,
17079 Digital Unix 4, etc.)@: get confused if
17080 @var{string} looks like an operator:
17082 @example
17083 $ @kbd{test -n =}
17084 test: argument expected
17085 $ @kbd{test ! -n}
17086 test: argument expected
17087 @end example
17089 Similarly, Posix says that both @samp{test "@var{string1}" = "@var{string2"}}
17090 and @samp{test "@var{string1}" != "@var{string2"}} work for any pairs of
17091 strings, but in practice this is not true for troublesome strings that
17092 look like operators or parentheses, or that begin with @samp{-}.
17094 It is best to protect such strings with a leading @samp{X}, e.g.,
17095 @samp{test "X@var{string}" != X} rather than @samp{test -n
17096 "@var{string}"} or @samp{test ! "@var{string}"}.
17098 It is common to find variations of the following idiom:
17100 @example
17101 test -n "`echo $ac_feature | sed 's/[-a-zA-Z0-9_]//g'`" &&
17102   @var{action}
17103 @end example
17105 @noindent
17106 to take an action when a token matches a given pattern.  Such constructs
17107 should be avoided by using:
17109 @example
17110 case $ac_feature in
17111   *[!-a-zA-Z0-9_]*) @var{action};;
17112 esac
17113 @end example
17115 If the pattern is a complicated regular expression that cannot be
17116 expressed as a shell pattern, use something like this instead:
17118 @example
17119 expr "X$ac_feature" : 'X.*[^-a-zA-Z0-9_]' >/dev/null &&
17120   @var{action}
17121 @end example
17123 @samp{expr "X@var{foo}" : "X@var{bar}"} is more robust than @samp{echo
17124 "X@var{foo}" | grep "^X@var{bar}"}, because it avoids problems when
17125 @samp{@var{foo}} contains backslashes.
17128 @anchor{trap}
17129 @item @command{trap}
17130 @c -----------------
17131 @prindex @command{trap}
17132 It is safe to trap at least the signals 1, 2, 13, and 15.  You can also
17133 trap 0, i.e., have the @command{trap} run when the script ends (either via an
17134 explicit @command{exit}, or the end of the script).  The trap for 0 should be
17135 installed outside of a shell function, or AIX 5.3 @command{/bin/sh}
17136 will invoke the trap at the end of this function.
17138 Posix says that @samp{trap - 1 2 13 15} resets the traps for the
17139 specified signals to their default values, but many common shells (e.g.,
17140 Solaris @command{/bin/sh}) misinterpret this and attempt to execute a
17141 ``command'' named @command{-} when the specified conditions arise.
17142 Posix 2008 also added a requirement to support @samp{trap 1 2 13 15} to
17143 reset traps, as this is supported by a larger set of shells, but there
17144 are still shells like @command{dash} that mistakenly try to execute
17145 @command{1} instead of resetting the traps.  Therefore, there is no
17146 portable workaround, except for @samp{trap - 0}, for which
17147 @samp{trap '' 0} is a portable substitute.
17149 Although Posix is not absolutely clear on this point, it is widely
17150 admitted that when entering the trap @samp{$?} should be set to the exit
17151 status of the last command run before the trap.  The ambiguity can be
17152 summarized as: ``when the trap is launched by an @command{exit}, what is
17153 the @emph{last} command run: that before @command{exit}, or
17154 @command{exit} itself?''
17156 Bash considers @command{exit} to be the last command, while Zsh and
17157 Solaris @command{/bin/sh} consider that when the trap is run it is
17158 @emph{still} in the @command{exit}, hence it is the previous exit status
17159 that the trap receives:
17161 @example
17162 $ @kbd{cat trap.sh}
17163 trap 'echo $?' 0
17164 (exit 42); exit 0
17165 $ @kbd{zsh trap.sh}
17167 $ @kbd{bash trap.sh}
17169 @end example
17171 The portable solution is then simple: when you want to @samp{exit 42},
17172 run @samp{(exit 42); exit 42}, the first @command{exit} being used to
17173 set the exit status to 42 for Zsh, and the second to trigger the trap
17174 and pass 42 as exit status for Bash.  In M4sh, this is covered by using
17175 @code{AS_EXIT}.
17177 The shell in FreeBSD 4.0 has the following bug: @samp{$?} is
17178 reset to 0 by empty lines if the code is inside @command{trap}.
17180 @example
17181 $ @kbd{trap 'false}
17183 echo $?' 0
17184 $ @kbd{exit}
17186 @end example
17188 @noindent
17189 Fortunately, this bug only affects @command{trap}.
17191 Several shells fail to execute an exit trap that is defined inside a
17192 subshell, when the last command of that subshell is not a builtin.  A
17193 workaround is to use @samp{exit $?} as the shell builtin.
17195 @example
17196 $ @kbd{bash -c '(trap "echo hi" 0; /bin/true)'}
17198 $ @kbd{/bin/sh -c '(trap "echo hi" 0; /bin/true)'}
17199 $ @kbd{/bin/sh -c '(trap "echo hi" 0; /bin/true; exit $?)'}
17201 @end example
17203 @noindent
17204 Likewise, older implementations of @command{bash} failed to preserve
17205 @samp{$?} across an exit trap consisting of a single cleanup command.
17207 @example
17208 $ @kbd{bash -c 'trap "/bin/true" 0; exit 2'; echo $?}
17210 $ @kbd{bash-2.05b -c 'trap "/bin/true" 0; exit 2'; echo $?}
17212 $ @kbd{bash-2.05b -c 'trap ":; /bin/true" 0; exit 2'; echo $?}
17214 @end example
17216 @item @command{true}
17217 @c -----------------
17218 @prindex @command{true}
17219 @c Info cannot handle `:' in index entries.
17220 @c @prindex @command{:}
17221 Don't worry: as far as we know @command{true} is portable.
17222 Nevertheless, it's not always a builtin (e.g., Bash 1.x), and the
17223 portable shell community tends to prefer using @command{:}.  This has a
17224 funny side effect: when asked whether @command{false} is more portable
17225 than @command{true} Alexandre Oliva answered:
17227 @quotation
17228 In a sense, yes, because if it doesn't exist, the shell will produce an
17229 exit status of failure, which is correct for @command{false}, but not
17230 for @command{true}.
17231 @end quotation
17234 @anchor{unset}
17235 @item @command{unset}
17236 @c ------------------
17237 @prindex @command{unset}
17238 In some nonconforming shells (e.g., Bash 2.05a), @code{unset FOO} fails
17239 when @code{FOO} is not set.  You can use
17241 @smallexample
17242 FOO=; unset FOO
17243 @end smallexample
17245 if you are not sure that @code{FOO} is set.
17247 A few ancient shells lack @command{unset} entirely.  For some variables
17248 such as @code{PS1}, you can use a neutralizing value instead:
17250 @smallexample
17251 PS1='$ '
17252 @end smallexample
17254 Usually, shells that do not support @command{unset} need less effort to
17255 make the environment sane, so for example is not a problem if you cannot
17256 unset @command{CDPATH} on those shells.  However, Bash 2.01 mishandles
17257 @code{unset MAIL} in some cases and dumps core.  So, you should do
17258 something like
17260 @smallexample
17261 ( (unset MAIL) || exit 1) >/dev/null 2>&1 && unset MAIL || :
17262 @end smallexample
17264 @noindent
17265 @xref{Special Shell Variables}, for some neutralizing values.  Also, see
17266 @ref{export, , Limitations of Builtins}, for
17267 the case of environment variables.
17269 @item @command{wait}
17270 @c -----------------
17271 @prindex @command{wait}
17272 The exit status of @command{wait} is not always reliable.
17273 @end table
17275 @node Limitations of Usual Tools
17276 @section Limitations of Usual Tools
17277 @cindex Limitations of usual tools
17279 The small set of tools you can expect to find on any machine can still
17280 include some limitations you should be aware of.
17282 @comment Between this list and the list of builtins above, we should
17283 @comment mention all the tools in GNU Coding Standards ``Utilities in
17284 @comment Makefiles''.
17286 @c This table includes things like `@command{expr} (|)', so we can't
17287 @c use @table @command.
17288 @table @asis
17289 @item @command{awk}
17290 @c ----------------
17291 @prindex @command{awk}
17292 Don't leave white space before the opening parenthesis in a user function call.
17293 Posix does not allow this and GNU Awk rejects it:
17295 @example
17296 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
17297         BEGIN @{ die () @}'}
17298 gawk: cmd. line:2:         BEGIN @{ die () @}
17299 gawk: cmd. line:2:                      ^ parse error
17300 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
17301         BEGIN @{ die() @}'}
17302 Aaaaarg!
17303 @end example
17305 Posix says that if a program contains only @samp{BEGIN} actions, and
17306 contains no instances of @code{getline}, then the program merely
17307 executes the actions without reading input.  However, traditional Awk
17308 implementations (such as Solaris 10 @command{awk}) read and discard
17309 input in this case.  Portable scripts can redirect input from
17310 @file{/dev/null} to work around the problem.  For example:
17312 @example
17313 awk 'BEGIN @{print "hello world"@}' </dev/null
17314 @end example
17316 Posix says that in an @samp{END} action, @samp{$NF} (and presumably,
17317 @samp{$1}) retain their value from the last record read, if no
17318 intervening @samp{getline} occurred.  However, some implementations
17319 (such as Solaris 10 @samp{/usr/bin/awk}, @samp{nawk}, or Darwin
17320 @samp{awk}) reset these variables.  A workaround is to use an
17321 intermediate variable prior to the @samp{END} block.  For example:
17323 @example
17324 $ @kbd{cat end.awk}
17325 @{ tmp = $1 @}
17326 END @{ print "a", $1, $NF, "b", tmp @}
17327 $ @kbd{echo 1 | awk -f end.awk}
17328 a   b 1
17329 $ @kbd{echo 1 | gawk -f end.awk}
17330 a 1 1 b 1
17331 @end example
17333 If you want your program to be deterministic, don't depend on @code{for}
17334 on arrays:
17336 @example
17337 $ @kbd{cat for.awk}
17338 END @{
17339   arr["foo"] = 1
17340   arr["bar"] = 1
17341   for (i in arr)
17342     print i
17344 $ @kbd{gawk -f for.awk </dev/null}
17347 $ @kbd{nawk -f for.awk </dev/null}
17350 @end example
17352 Some Awk implementations, such as HP-UX 11.0's native one,
17353 mishandle anchors:
17355 @example
17356 $ @kbd{echo xfoo | $AWK '/foo|^bar/ @{ print @}'}
17357 $ @kbd{echo bar | $AWK '/foo|^bar/ @{ print @}'}
17359 $ @kbd{echo xfoo | $AWK '/^bar|foo/ @{ print @}'}
17360 xfoo
17361 $ @kbd{echo bar | $AWK '/^bar|foo/ @{ print @}'}
17363 @end example
17365 @noindent
17366 Either do not depend on such patterns (i.e., use @samp{/^(.*foo|bar)/},
17367 or use a simple test to reject such implementations.
17369 On @samp{ia64-hp-hpux11.23}, Awk mishandles @code{printf} conversions
17370 after @code{%u}:
17372 @example
17373 $ @kbd{awk 'BEGIN @{ printf "%u %d\n", 0, -1 @}'}
17374 0 0
17375 @end example
17377 AIX version 5.2 has an arbitrary limit of 399 on the
17378 length of regular expressions and literal strings in an Awk program.
17380 Traditional Awk implementations derived from Unix version 7, such as
17381 Solaris @command{/bin/awk}, have many limitations and do not
17382 conform to Posix.  Nowadays @code{AC_PROG_AWK} (@pxref{Particular
17383 Programs}) finds you an Awk that doesn't have these problems, but if
17384 for some reason you prefer not to use @code{AC_PROG_AWK} you may need to
17385 address them.
17387 Traditional Awk does not support multidimensional arrays or user-defined
17388 functions.
17390 Traditional Awk does not support the @option{-v} option.  You can use
17391 assignments after the program instead, e.g., @code{$AWK '@{print v
17392 $1@}' v=x}; however, don't forget that such assignments are not
17393 evaluated until they are encountered (e.g., after any @code{BEGIN}
17394 action).
17396 Traditional Awk does not support the keywords @code{delete} or @code{do}.
17398 Traditional Awk does not support the expressions
17399 @code{@var{a}?@var{b}:@var{c}}, @code{!@var{a}}, @code{@var{a}^@var{b}},
17400 or @code{@var{a}^=@var{b}}.
17402 Traditional Awk does not support the predefined @code{CONVFMT} variable.
17404 Traditional Awk supports only the predefined functions @code{exp}, @code{index},
17405 @code{int}, @code{length}, @code{log}, @code{split}, @code{sprintf},
17406 @code{sqrt}, and @code{substr}.
17408 Traditional Awk @code{getline} is not at all compatible with Posix;
17409 avoid it.
17411 Traditional Awk has @code{for (i in a) @dots{}} but no other uses of the
17412 @code{in} keyword.  For example, it lacks @code{if (i in a) @dots{}}.
17414 In code portable to both traditional and modern Awk, @code{FS} must be a
17415 string containing just one ordinary character, and similarly for the
17416 field-separator argument to @code{split}.
17418 Traditional Awk has a limit of 99 fields in a record.  Since some Awk
17419 implementations, like Tru64's, split the input even if you don't refer
17420 to any field in the script, to circumvent this problem, set @samp{FS}
17421 to an unusual character and use @code{split}.
17423 Traditional Awk has a limit of at most 99 bytes in a number formatted by
17424 @code{OFMT}; for example, @code{OFMT="%.300e"; print 0.1;} typically
17425 dumps core.
17427 The original version of Awk had a limit of at most 99 bytes per
17428 @code{split} field, 99 bytes per @code{substr} substring, and 99 bytes
17429 per run of non-special characters in a @code{printf} format, but these
17430 bugs have been fixed on all practical hosts that we know of.
17432 HP-UX 11.00 and IRIX 6.5 Awk require that input files have a line length
17433 of at most 3070 bytes.
17435 @item @command{basename}
17436 @c ---------------------
17437 @prindex @command{basename}
17438 Not all hosts have a working @command{basename}.
17439 You can use @command{expr} instead.
17441 @c AS_BASENAME is to be replaced by a better API.
17442 @ignore
17443 Not all hosts have a working @command{basename}, and you should instead
17444 use @code{AS_BASENAME} (@pxref{Programming in M4sh}), followed by
17445 @command{expr} if you need to strip a suffix.  For example:
17447 @example
17448 a=`basename "$aname"`       # This is not portable.
17449 a=`AS_BASENAME(["$aname"])` # This is more portable.
17451 # This is not portable.
17452 c=`basename "$cname" .c`
17454 # This is more portable.
17455 c=`AS_BASENAME(["$cname"])`
17456 case $c in
17457 ?*.c) c=`expr "X$c" : 'X\(.*\)\.c'`;;
17458 esac
17459 @end example
17460 @end ignore
17463 @item @command{cat}
17464 @c ----------------
17465 @prindex @command{cat}
17466 Don't rely on any option.
17469 @item @command{cc}
17470 @c ---------------
17471 @prindex @command{cc}
17472 The command @samp{cc -c foo.c} traditionally produces an object file
17473 named @file{foo.o}.  Most compilers allow @option{-c} to be combined
17474 with @option{-o} to specify a different object file name, but
17475 Posix does not require this combination and a few compilers
17476 lack support for it.  @xref{C Compiler}, for how GNU Make
17477 tests for this feature with @code{AC_PROG_CC_C_O}.
17479 When a compilation such as @samp{cc -o foo foo.c} fails, some compilers
17480 (such as CDS on Reliant Unix) leave a @file{foo.o}.
17482 HP-UX @command{cc} doesn't accept @file{.S} files to preprocess and
17483 assemble.  @samp{cc -c foo.S} appears to succeed, but in fact does
17484 nothing.
17486 The default executable, produced by @samp{cc foo.c}, can be
17488 @itemize
17489 @item @file{a.out} --- usual Posix convention.
17490 @item @file{b.out} --- i960 compilers (including @command{gcc}).
17491 @item @file{a.exe} --- DJGPP port of @command{gcc}.
17492 @item @file{a_out.exe} --- GNV @command{cc} wrapper for DEC C on OpenVMS.
17493 @item @file{foo.exe} --- various MS-DOS compilers.
17494 @end itemize
17496 The C compiler's traditional name is @command{cc}, but other names like
17497 @command{gcc} are common.  Posix 1003.1-2001 specifies the
17498 name @command{c99}, but older Posix editions specified
17499 @command{c89} and anyway these standard names are rarely used in
17500 practice.  Typically the C compiler is invoked from makefiles that use
17501 @samp{$(CC)}, so the value of the @samp{CC} make variable selects the
17502 compiler name.
17504 @item @command{chgrp}
17505 @itemx @command{chown}
17506 @c -------------------
17507 @prindex @command{chgrp}
17508 @prindex @command{chown}
17509 It is not portable to change a file's group to a group that the owner
17510 does not belong to.
17512 @item @command{chmod}
17513 @c ------------------
17514 @prindex @command{chmod}
17515 Avoid usages like @samp{chmod -w file}; use @samp{chmod a-w file}
17516 instead, for two reasons.  First, plain @option{-w} does not necessarily
17517 make the file unwritable, since it does not affect mode bits that
17518 correspond to bits in the file mode creation mask.  Second,
17519 Posix says that the @option{-w} might be interpreted as an
17520 implementation-specific option, not as a mode; Posix suggests
17521 using @samp{chmod -- -w file} to avoid this confusion, but unfortunately
17522 @samp{--} does not work on some older hosts.
17525 @item @command{cmp}
17526 @c ----------------
17527 @prindex @command{cmp}
17528 @command{cmp} performs a raw data comparison of two files, while
17529 @command{diff} compares two text files.  Therefore, if you might compare
17530 DOS files, even if only checking whether two files are different, use
17531 @command{diff} to avoid spurious differences due to differences of
17532 newline encoding.
17535 @item @command{cp}
17536 @c ---------------
17537 @prindex @command{cp}
17538 Avoid the @option{-r} option, since Posix 1003.1-2004 marks it as
17539 obsolescent and its behavior on special files is implementation-defined.
17540 Use @option{-R} instead.  On GNU hosts the two options
17541 are equivalent, but on Solaris hosts (for example) @code{cp -r}
17542 reads from pipes instead of replicating them.
17544 Some @command{cp} implementations (e.g., BSD/OS 4.2) do not allow
17545 trailing slashes at the end of nonexistent destination directories.  To
17546 avoid this problem, omit the trailing slashes.  For example, use
17547 @samp{cp -R source /tmp/newdir} rather than @samp{cp -R source
17548 /tmp/newdir/} if @file{/tmp/newdir} does not exist.
17550 @c This is thanks to Ian.
17551 The ancient SunOS 4 @command{cp} does not support @option{-f}, although
17552 its @command{mv} does.
17554 @cindex timestamp resolution
17555 Traditionally, file timestamps had 1-second resolution, and @samp{cp
17556 -p} copied the timestamps exactly.  However, many modern file systems
17557 have timestamps with 1-nanosecond resolution.  Unfortunately, @samp{cp
17558 -p} implementations truncate timestamps when copying files, so this
17559 can result in the destination file appearing to be older than the
17560 source.  The exact amount of truncation depends on the resolution of
17561 the system calls that @command{cp} uses; traditionally this was
17562 @code{utime}, which has 1-second resolution, but some newer
17563 @command{cp} implementations use @code{utimes}, which has
17564 1-microsecond resolution.  These newer implementations include GNU
17565 Core Utilities 5.0.91 or later, and Solaris 8 (sparc) patch 109933-02 or
17566 later.  Unfortunately as of January 2006 there is still no system
17567 call to set timestamps to the full nanosecond resolution.
17569 Bob Proulx notes that @samp{cp -p} always @emph{tries} to copy
17570 ownerships.  But whether it actually does copy ownerships or not is a
17571 system dependent policy decision implemented by the kernel.  If the
17572 kernel allows it then it happens.  If the kernel does not allow it then
17573 it does not happen.  It is not something @command{cp} itself has control
17574 over.
17576 In Unix System V any user can chown files to any other user, and System
17577 V also has a non-sticky @file{/tmp}.  That probably derives from the
17578 heritage of System V in a business environment without hostile users.
17579 BSD changed this
17580 to be a more secure model where only root can @command{chown} files and
17581 a sticky @file{/tmp} is used.  That undoubtedly derives from the heritage
17582 of BSD in a campus environment.
17584 GNU/Linux and Solaris by default follow BSD, but
17585 can be configured to allow a System V style @command{chown}.  On the
17586 other hand, HP-UX follows System V, but can
17587 be configured to use the modern security model and disallow
17588 @command{chown}.  Since it is an administrator-configurable parameter
17589 you can't use the name of the kernel as an indicator of the behavior.
17593 @item @command{date}
17594 @c -----------------
17595 @prindex @command{date}
17596 Some versions of @command{date} do not recognize special @samp{%} directives,
17597 and unfortunately, instead of complaining, they just pass them through,
17598 and exit with success:
17600 @example
17601 $ @kbd{uname -a}
17602 OSF1 medusa.sis.pasteur.fr V5.1 732 alpha
17603 $ @kbd{date "+%s"}
17605 @end example
17608 @item @command{diff}
17609 @c -----------------
17610 @prindex @command{diff}
17611 Option @option{-u} is nonportable.
17613 Some implementations, such as Tru64's, fail when comparing to
17614 @file{/dev/null}.  Use an empty file instead.
17617 @item @command{dirname}
17618 @c --------------------
17619 @prindex @command{dirname}
17620 Not all hosts have a working @command{dirname}, and you should instead
17621 use @code{AS_DIRNAME} (@pxref{Programming in M4sh}).  For example:
17623 @example
17624 dir=`dirname "$file"`       # This is not portable.
17625 dir=`AS_DIRNAME(["$file"])` # This is more portable.
17626 @end example
17629 @item @command{egrep}
17630 @c ------------------
17631 @prindex @command{egrep}
17632 Posix 1003.1-2001 no longer requires @command{egrep},
17633 but many hosts do not yet support the Posix
17634 replacement @code{grep -E}.  Also, some traditional implementations do
17635 not work on long input lines.  To work around these problems, invoke
17636 @code{AC_PROG_EGREP} and then use @code{$EGREP}.
17638 Portable extended regular expressions should use @samp{\} only to escape
17639 characters in the string @samp{$()*+.?[\^@{|}.  For example, @samp{\@}}
17640 is not portable, even though it typically matches @samp{@}}.
17642 The empty alternative is not portable.  Use @samp{?} instead.  For
17643 instance with Digital Unix v5.0:
17645 @example
17646 > printf "foo\n|foo\n" | $EGREP '^(|foo|bar)$'
17647 |foo
17648 > printf "bar\nbar|\n" | $EGREP '^(foo|bar|)$'
17649 bar|
17650 > printf "foo\nfoo|\n|bar\nbar\n" | $EGREP '^(foo||bar)$'
17652 |bar
17653 @end example
17655 @command{$EGREP} also suffers the limitations of @command{grep}
17656 (@pxref{grep, , Limitations of Usual Tools}).
17658 @item @command{expr}
17659 @c -----------------
17660 @prindex @command{expr}
17661 Not all implementations obey the Posix rule that @samp{--} separates
17662 options from arguments; likewise, not all implementations provide the
17663 extension to Posix that the first argument can be treated as part of a
17664 valid expression rather than an invalid option if it begins with
17665 @samp{-}.  When performing arithmetic, use @samp{expr 0 + $var} if
17666 @samp{$var} might be a negative number, to keep @command{expr} from
17667 interpreting it as an option.
17669 No @command{expr} keyword starts with @samp{X}, so use @samp{expr
17670 X"@var{word}" : 'X@var{regex}'} to keep @command{expr} from
17671 misinterpreting @var{word}.
17673 Don't use @code{length}, @code{substr}, @code{match} and @code{index}.
17675 @item @command{expr} (@samp{|})
17676 @prindex @command{expr} (@samp{|})
17677 You can use @samp{|}.  Although Posix does require that @samp{expr
17678 ''} return the empty string, it does not specify the result when you
17679 @samp{|} together the empty string (or zero) with the empty string.  For
17680 example:
17682 @example
17683 expr '' \| ''
17684 @end example
17686 Posix 1003.2-1992 returns the empty string
17687 for this case, but traditional Unix returns @samp{0} (Solaris is
17688 one such example).  In Posix 1003.1-2001, the specification was
17689 changed to match traditional Unix's behavior (which is
17690 bizarre, but it's too late to fix this).  Please note that the same
17691 problem does arise when the empty string results from a computation,
17692 as in:
17694 @example
17695 expr bar : foo \| foo : bar
17696 @end example
17698 @noindent
17699 Avoid this portability problem by avoiding the empty string.
17702 @item @command{expr} (@samp{:})
17703 @c ----------------------------
17704 @prindex @command{expr}
17705 Portable @command{expr} regular expressions should use @samp{\} to
17706 escape only characters in the string @samp{$()*.0123456789[\^n@{@}}.
17707 For example, alternation, @samp{\|}, is common but Posix does not
17708 require its support, so it should be avoided in portable scripts.
17709 Similarly, @samp{\+} and @samp{\?} should be avoided.
17711 Portable @command{expr} regular expressions should not begin with
17712 @samp{^}.  Patterns are automatically anchored so leading @samp{^} is
17713 not needed anyway.
17715 On the other hand, the behavior of the @samp{$} anchor is not portable
17716 on multi-line strings.  Posix is ambiguous whether the anchor applies to
17717 each line, as was done in older versions of GNU Coreutils, or
17718 whether it applies only to the end of the overall string, as in
17719 Coreutils 6.0 and most other implementations.
17721 @example
17722 $ @kbd{baz='foo}
17723 > @kbd{bar'}
17724 $ @kbd{expr "X$baz" : 'X\(foo\)$'}
17726 $ @kbd{expr-5.97 "X$baz" : 'X\(foo\)$'}
17728 @end example
17730 The Posix standard is ambiguous as to whether
17731 @samp{expr 'a' : '\(b\)'} outputs @samp{0} or the empty string.
17732 In practice, it outputs the empty string on most platforms, but portable
17733 scripts should not assume this.  For instance, the QNX 4.25 native
17734 @command{expr} returns @samp{0}.
17736 One might think that a way to get a uniform behavior would be to use
17737 the empty string as a default value:
17739 @example
17740 expr a : '\(b\)' \| ''
17741 @end example
17743 @noindent
17744 Unfortunately this behaves exactly as the original expression; see the
17745 @command{expr} (@samp{|}) entry for more information.
17747 Some ancient @command{expr} implementations (e.g., SunOS 4 @command{expr} and
17748 Solaris 8 @command{/usr/ucb/expr}) have a silly length limit that causes
17749 @command{expr} to fail if the matched substring is longer than 120
17750 bytes.  In this case, you might want to fall back on @samp{echo|sed} if
17751 @command{expr} fails.  Nowadays this is of practical importance only for
17752 the rare installer who mistakenly puts @file{/usr/ucb} before
17753 @file{/usr/bin} in @env{PATH}.
17755 On Mac OS X 10.4, @command{expr} mishandles the pattern @samp{[^-]} in
17756 some cases.  For example, the command
17757 @example
17758 expr Xpowerpc-apple-darwin8.1.0 : 'X[^-]*-[^-]*-\(.*\)'
17759 @end example
17761 @noindent
17762 outputs @samp{apple-darwin8.1.0} rather than the correct @samp{darwin8.1.0}.
17763 This particular case can be worked around by substituting @samp{[^--]}
17764 for @samp{[^-]}.
17766 Don't leave, there is some more!
17768 The QNX 4.25 @command{expr}, in addition of preferring @samp{0} to
17769 the empty string, has a funny behavior in its exit status: it's always 1
17770 when parentheses are used!
17772 @example
17773 $ @kbd{val=`expr 'a' : 'a'`; echo "$?: $val"}
17774 0: 1
17775 $ @kbd{val=`expr 'a' : 'b'`; echo "$?: $val"}
17776 1: 0
17778 $ @kbd{val=`expr 'a' : '\(a\)'`; echo "?: $val"}
17779 1: a
17780 $ @kbd{val=`expr 'a' : '\(b\)'`; echo "?: $val"}
17781 1: 0
17782 @end example
17784 @noindent
17785 In practice this can be a big problem if you are ready to catch failures
17786 of @command{expr} programs with some other method (such as using
17787 @command{sed}), since you may get twice the result.  For instance
17789 @example
17790 $ @kbd{expr 'a' : '\(a\)' || echo 'a' | sed 's/^\(a\)$/\1/'}
17791 @end example
17793 @noindent
17794 outputs @samp{a} on most hosts, but @samp{aa} on QNX 4.25.  A
17795 simple workaround consists of testing @command{expr} and using a variable
17796 set to @command{expr} or to @command{false} according to the result.
17798 Tru64 @command{expr} incorrectly treats the result as a number, if it
17799 can be interpreted that way:
17801 @example
17802 $ @kbd{expr 00001 : '.*\(...\)'}
17804 @end example
17806 On HP-UX 11, @command{expr} only supports a single
17807 sub-expression.
17809 @example
17810 $ @kbd{expr 'Xfoo' : 'X\(f\(oo\)*\)$'}
17811 expr: More than one '\(' was used.
17812 @end example
17815 @item @command{fgrep}
17816 @c ------------------
17817 @prindex @command{fgrep}
17818 Posix 1003.1-2001 no longer requires @command{fgrep},
17819 but many hosts do not yet support the Posix
17820 replacement @code{grep -F}.  Also, some traditional implementations do
17821 not work on long input lines.  To work around these problems, invoke
17822 @code{AC_PROG_FGREP} and then use @code{$FGREP}.
17824 Tru64/OSF 5.1 @command{fgrep} does not match an empty pattern.
17827 @item @command{find}
17828 @c -----------------
17829 @prindex @command{find}
17830 The option @option{-maxdepth} seems to be GNU specific.
17831 Tru64 v5.1, NetBSD 1.5 and Solaris @command{find}
17832 commands do not understand it.
17834 The replacement of @samp{@{@}} is guaranteed only if the argument is
17835 exactly @emph{@{@}}, not if it's only a part of an argument.  For
17836 instance on DU, and HP-UX 10.20 and HP-UX 11:
17838 @example
17839 $ @kbd{touch foo}
17840 $ @kbd{find . -name foo -exec echo "@{@}-@{@}" \;}
17841 @{@}-@{@}
17842 @end example
17844 @noindent
17845 while GNU @command{find} reports @samp{./foo-./foo}.
17848 @anchor{grep}
17849 @item @command{grep}
17850 @c -----------------
17851 @prindex @command{grep}
17852 Portable scripts can rely on the @command{grep} options @option{-c},
17853 @option{-l}, @option{-n}, and @option{-v}, but should avoid other
17854 options.  For example, don't use @option{-w}, as Posix does not require
17855 it and Irix 6.5.16m's @command{grep} does not support it.  Also,
17856 portable scripts should not combine @option{-c} with @option{-l},
17857 as Posix does not allow this.
17859 Some of the options required by Posix are not portable in practice.
17860 Don't use @samp{grep -q} to suppress output, because many @command{grep}
17861 implementations (e.g., Solaris) do not support @option{-q}.
17862 Don't use @samp{grep -s} to suppress output either, because Posix
17863 says @option{-s} does not suppress output, only some error messages;
17864 also, the @option{-s} option of traditional @command{grep} behaved
17865 like @option{-q} does in most modern implementations.  Instead,
17866 redirect the standard output and standard error (in case the file
17867 doesn't exist) of @code{grep} to @file{/dev/null}.  Check the exit
17868 status of @code{grep} to determine whether it found a match.
17870 The QNX4 implementation fails to count lines with @code{grep -c '$'},
17871 but works with @code{grep -c '^'}.  Other alternatives for counting
17872 lines are to use @code{sed -n '$='} or @code{wc -l}.
17874 Some traditional @command{grep} implementations do not work on long
17875 input lines.  On AIX the default @code{grep} silently truncates long
17876 lines on the input before matching.
17878 Also, many implementations do not support multiple regexps
17879 with @option{-e}: they either reject @option{-e} entirely (e.g., Solaris)
17880 or honor only the last pattern (e.g., IRIX 6.5 and NeXT).  To
17881 work around these problems, invoke @code{AC_PROG_GREP} and then use
17882 @code{$GREP}.
17884 Another possible workaround for the multiple @option{-e} problem is to
17885 separate the patterns by newlines, for example:
17887 @example
17888 grep 'foo
17889 bar' in.txt
17890 @end example
17892 @noindent
17893 except that this fails with traditional @command{grep}
17894 implementations and with OpenBSD 3.8 @command{grep}.
17896 Traditional @command{grep} implementations (e.g., Solaris) do not
17897 support the @option{-E} or @option{-F} options.  To work around these
17898 problems, invoke @code{AC_PROG_EGREP} and then use @code{$EGREP}, and
17899 similarly for @code{AC_PROG_FGREP} and @code{$FGREP}.  Even if you are
17900 willing to require support for Posix @command{grep}, your script should
17901 not use both @option{-E} and @option{-F}, since Posix does not allow
17902 this combination.
17904 Portable @command{grep} regular expressions should use @samp{\} only to
17905 escape characters in the string @samp{$()*.0123456789[\^@{@}}.  For example,
17906 alternation, @samp{\|}, is common but Posix does not require its
17907 support in basic regular expressions, so it should be avoided in
17908 portable scripts.  Solaris and HP-UX @command{grep} do not support it.
17909 Similarly, the following escape sequences should also be avoided:
17910 @samp{\<}, @samp{\>}, @samp{\+}, @samp{\?}, @samp{\`}, @samp{\'},
17911 @samp{\B}, @samp{\b}, @samp{\S}, @samp{\s}, @samp{\W}, and @samp{\w}.
17913 Posix does not specify the behavior of @command{grep} on binary files.
17914 An example where this matters is using BSD @command{grep} to
17915 search text that includes embedded ANSI escape sequences for
17916 colored output to terminals (@samp{\033[m} is the sequence to restore
17917 normal output); the behavior depends on whether input is seekable:
17919 @example
17920 $ @kbd{printf 'esc\033[mape\n' > sample}
17921 $ @kbd{grep . sample}
17922 Binary file sample matches
17923 $ @kbd{cat sample | grep .}
17924 escape
17925 @end example
17928 @item @command{join}
17929 @c -----------------
17930 @prindex @command{join}
17931 Solaris 8 @command{join} has bugs when the second operand is standard
17932 input, and when standard input is a pipe.  For example, the following
17933 shell script causes Solaris 8 @command{join} to loop forever:
17935 @example
17936 cat >file <<'EOF'
17937 1 x
17938 2 y
17940 cat file | join file -
17941 @end example
17943 Use @samp{join - file} instead.
17946 @item @command{ln}
17947 @c ---------------
17948 @prindex @command{ln}
17949 @cindex Symbolic links
17950 Don't rely on @command{ln} having a @option{-f} option.  Symbolic links
17951 are not available on old systems; use @samp{$(LN_S)} as a portable substitute.
17953 For versions of the DJGPP before 2.04,
17954 @command{ln} emulates symbolic links
17955 to executables by generating a stub that in turn calls the real
17956 program.  This feature also works with nonexistent files like in the
17957 Posix spec.  So @samp{ln -s file link} generates @file{link.exe},
17958 which attempts to call @file{file.exe} if run.  But this feature only
17959 works for executables, so @samp{cp -p} is used instead for these
17960 systems.  DJGPP versions 2.04 and later have full support
17961 for symbolic links.
17964 @item @command{ls}
17965 @c ---------------
17966 @prindex @command{ls}
17967 @cindex Listing directories
17968 The portable options are @option{-acdilrtu}.  Current practice is for
17969 @option{-l} to output both owner and group, even though ancient versions
17970 of @command{ls} omitted the group.
17972 On ancient hosts, @samp{ls foo} sent the diagnostic @samp{foo not found}
17973 to standard output if @file{foo} did not exist.  Hence a shell command
17974 like @samp{sources=`ls *.c 2>/dev/null`} did not always work, since it
17975 was equivalent to @samp{sources='*.c not found'} in the absence of
17976 @samp{.c} files.  This is no longer a practical problem, since current
17977 @command{ls} implementations send diagnostics to standard error.
17979 The behavior of @command{ls} on a directory that is being concurrently
17980 modified is not always predictable, because of a data race where cached
17981 information returned by @code{readdir} does not match the current
17982 directory state.  In fact, MacOS 10.5 has an intermittent bug where
17983 @code{readdir}, and thus @command{ls}, sometimes lists a file more than
17984 once if other files were added or removed from the directory immediately
17985 prior to the @command{ls} call.  Since @command{ls} already sorts its
17986 output, the duplicate entries can be avoided by piping the results
17987 through @code{uniq}.
17989 @anchor{mkdir}
17990 @item @command{mkdir}
17991 @c ------------------
17992 @prindex @command{mkdir}
17993 @cindex Making directories
17994 No @command{mkdir} option is portable to older systems.  Instead of
17995 @samp{mkdir -p @var{file-name}}, you should use
17996 @code{AS_MKDIR_P(@var{file-name})} (@pxref{Programming in M4sh})
17997 or @code{AC_PROG_MKDIR_P} (@pxref{Particular Programs}).
17999 Combining the @option{-m} and @option{-p} options, as in @samp{mkdir -m
18000 go-w -p @var{dir}}, often leads to trouble.  FreeBSD
18001 @command{mkdir} incorrectly attempts to change the permissions of
18002 @var{dir} even if it already exists.  HP-UX 11.23 and
18003 IRIX 6.5 @command{mkdir} often assign the wrong permissions to
18004 any newly-created parents of @var{dir}.
18006 Posix does not clearly specify whether @samp{mkdir -p foo}
18007 should succeed when @file{foo} is a symbolic link to an already-existing
18008 directory.  The GNU Core Utilities 5.1.0 @command{mkdir}
18009 succeeds, but Solaris @command{mkdir} fails.
18011 Traditional @code{mkdir -p} implementations suffer from race conditions.
18012 For example, if you invoke @code{mkdir -p a/b} and @code{mkdir -p a/c}
18013 at the same time, both processes might detect that @file{a} is missing,
18014 one might create @file{a}, then the other might try to create @file{a}
18015 and fail with a @code{File exists} diagnostic.  The GNU Core
18016 Utilities (@samp{fileutils} version 4.1), FreeBSD 5.0,
18017 NetBSD 2.0.2, and OpenBSD 2.4 are known to be
18018 race-free when two processes invoke @code{mkdir -p} simultaneously, but
18019 earlier versions are vulnerable.  Solaris @command{mkdir} is still
18020 vulnerable as of Solaris 10, and other traditional Unix systems are
18021 probably vulnerable too.  This possible race is harmful in parallel
18022 builds when several Make rules call @code{mkdir -p} to
18023 construct directories.  You may use
18024 @code{install-sh -d} as a safe replacement, provided this script is
18025 recent enough; the copy shipped with Autoconf 2.60 and Automake 1.10 is
18026 OK, but copies from older versions are vulnerable.
18029 @item @command{mkfifo}
18030 @itemx @command{mknod}
18031 @c -------------------
18032 @prindex @command{mkfifo}
18033 @prindex @command{mknod}
18034 The GNU Coding Standards state that @command{mknod} is safe to use on
18035 platforms where it has been tested to exist; but it is generally portable
18036 only for creating named FIFOs, since device numbers are
18037 platform-specific.  Autotest uses @command{mkfifo} to implement parallel
18038 testsuites.  Posix states that behavior is unspecified when opening a
18039 named FIFO for both reading and writing; on at least Cygwin, this
18040 results in failure on any attempt to read or write to that file
18041 descriptor.
18043 @item @command{mktemp}
18044 @c -------------------
18045 @prindex @command{mktemp}
18046 @cindex Creating temporary files
18047 Shell scripts can use temporary files safely with @command{mktemp}, but
18048 it does not exist on all systems.  A portable way to create a safe
18049 temporary file name is to create a temporary directory with mode 700 and
18050 use a file inside this directory.  Both methods prevent attackers from
18051 gaining control, though @command{mktemp} is far less likely to fail
18052 gratuitously under attack.
18054 Here is sample code to create a new temporary directory safely:
18056 @example
18057 # Create a temporary directory $tmp in $TMPDIR (default /tmp).
18058 # Use mktemp if possible; otherwise fall back on mkdir,
18059 # with $RANDOM to make collisions less likely.
18060 : $@{TMPDIR=/tmp@}
18062   tmp=`
18063     (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
18064   ` &&
18065   test -n "$tmp" && test -d "$tmp"
18066 @} || @{
18067   tmp=$TMPDIR/foo$$-$RANDOM
18068 @c $$ restore font-lock
18069   (umask 077 && mkdir "$tmp")
18070 @} || exit $?
18071 @end example
18074 @item @command{mv}
18075 @c ---------------
18076 @prindex @command{mv}
18077 @cindex Moving open files
18078 The only portable options are @option{-f} and @option{-i}.
18080 Moving individual files between file systems is portable (it was in Unix
18081 version 6),
18082 but it is not always atomic: when doing @samp{mv new existing}, there's
18083 a critical section where neither the old nor the new version of
18084 @file{existing} actually exists.
18086 On some systems moving files from @file{/tmp} can sometimes cause
18087 undesirable (but perfectly valid) warnings, even if you created these
18088 files.  This is because @file{/tmp} belongs to a group that ordinary
18089 users are not members of, and files created in @file{/tmp} inherit
18090 the group of @file{/tmp}.  When the file is copied, @command{mv} issues
18091 a diagnostic without failing:
18093 @smallexample
18094 $ @kbd{touch /tmp/foo}
18095 $ @kbd{mv /tmp/foo .}
18096 @error{}mv: ./foo: set owner/group (was: 100/0): Operation not permitted
18097 $ @kbd{echo $?}
18099 $ @kbd{ls foo}
18101 @end smallexample
18103 @noindent
18104 This annoying behavior conforms to Posix, unfortunately.
18106 Moving directories across mount points is not portable, use @command{cp}
18107 and @command{rm}.
18109 DOS variants cannot rename or remove open files, and do not
18110 support commands like @samp{mv foo bar >foo}, even though this is
18111 perfectly portable among Posix hosts.
18114 @item @command{od}
18115 @c ---------------
18116 @prindex @command{od}
18118 In Mac OS X 10.3, @command{od} does not support the
18119 standard Posix options @option{-A}, @option{-j}, @option{-N}, or
18120 @option{-t}, or the XSI option @option{-s}.  The only
18121 supported Posix option is @option{-v}, and the only supported
18122 XSI options are those in @option{-bcdox}.  The BSD
18123 @command{hexdump} program can be used instead.
18125 This problem no longer exists in Mac OS X 10.4.3.
18128 @item @command{rm}
18129 @c ---------------
18130 @prindex @command{rm}
18131 The @option{-f} and @option{-r} options are portable.
18133 It is not portable to invoke @command{rm} without operands.  For
18134 example, on many systems @samp{rm -f -r} (with no other arguments)
18135 silently succeeds without doing anything, but it fails with a diagnostic
18136 on NetBSD 2.0.2.
18138 A file might not be removed even if its parent directory is writable
18139 and searchable.  Many Posix hosts cannot remove a mount point, a named
18140 stream, a working directory, or a last link to a file that is being
18141 executed.
18143 DOS variants cannot rename or remove open files, and do not
18144 support commands like @samp{rm foo >foo}, even though this is
18145 perfectly portable among Posix hosts.
18147 @item @command{rmdir}
18148 @c ------------------
18149 @prindex @command{rmdir}
18150 Just as with @command{rm}, some platforms refuse to remove a working
18151 directory.
18153 @anchor{sed}
18154 @item @command{sed}
18155 @c ----------------
18156 @prindex @command{sed}
18157 Patterns should not include the separator (unless escaped), even as part
18158 of a character class.  In conformance with Posix, the Cray
18159 @command{sed} rejects @samp{s/[^/]*$//}: use @samp{s%[^/]*$%%}.
18160 Even when escaped, patterns should not include separators that are also
18161 used as @command{sed} metacharacters.  For example, GNU sed 4.0.9 rejects
18162 @samp{s,x\@{1\,\@},,}, while sed 4.1 strips the backslash before the comma
18163 before evaluating the basic regular expression.
18165 Avoid empty patterns within parentheses (i.e., @samp{\(\)}).  Posix does
18166 not require support for empty patterns, and Unicos 9 @command{sed} rejects
18167 them.
18169 Unicos 9 @command{sed} loops endlessly on patterns like @samp{.*\n.*}.
18171 Sed scripts should not use branch labels longer than 7 characters and
18172 should not contain comments.  HP-UX sed has a limit of 99 commands
18173 (not counting @samp{:} commands) and
18174 48 labels, which can not be circumvented by using more than one script
18175 file.  It can execute up to 19 reads with the @samp{r} command per cycle.
18176 Solaris @command{/usr/ucb/sed} rejects usages that exceed a limit of
18177 about 6000 bytes for the internal representation of commands.
18179 Avoid redundant @samp{;}, as some @command{sed} implementations, such as
18180 NetBSD 1.4.2's, incorrectly try to interpret the second
18181 @samp{;} as a command:
18183 @example
18184 $ @kbd{echo a | sed 's/x/x/;;s/x/x/'}
18185 sed: 1: "s/x/x/;;s/x/x/": invalid command code ;
18186 @end example
18188 Input should not have unreasonably long lines, since some @command{sed}
18189 implementations have an input buffer limited to 4000 bytes.  Likewise,
18190 not all @command{sed} implementations can handle embedded @code{NUL} or
18191 a missing trailing newline.
18193 Portable @command{sed} regular expressions should use @samp{\} only to escape
18194 characters in the string @samp{$()*.0123456789[\^n@{@}}.  For example,
18195 alternation, @samp{\|}, is common but Posix does not require its
18196 support, so it should be avoided in portable scripts.  Solaris
18197 @command{sed} does not support alternation; e.g., @samp{sed '/a\|b/d'}
18198 deletes only lines that contain the literal string @samp{a|b}.
18199 Similarly, @samp{\+} and @samp{\?} should be avoided.
18201 Anchors (@samp{^} and @samp{$}) inside groups are not portable.
18203 Nested parentheses in patterns (e.g., @samp{\(\(a*\)b*)\)}) are
18204 quite portable to current hosts, but was not supported by some ancient
18205 @command{sed} implementations like SVR3.
18207 Some @command{sed} implementations, e.g., Solaris, restrict the special
18208 role of the asterisk @samp{*} to one-character regular expressions and
18209 back-references, and the special role of interval expressions
18210 @samp{\@{@var{m}\@}}, @samp{\@{@var{m},\@}}, or @samp{\@{@var{m},@var{n}\@}}
18211 to one-character regular expressions.  This may lead to unexpected behavior:
18213 @example
18214 $ @kbd{echo '1*23*4' | /usr/bin/sed 's/\(.\)*/x/g'}
18215 x2x4
18216 $ @kbd{echo '1*23*4' | /usr/xpg4/bin/sed 's/\(.\)*/x/g'}
18218 @end example
18220 The @option{-e} option is mostly portable.
18221 However, its argument
18222 cannot start with @samp{a}, @samp{c}, or @samp{i},
18223 as this runs afoul of a Tru64 5.1 bug.
18224 Also, its argument cannot be empty, as this fails on AIX 5.3.
18225 Some people prefer to use @samp{-e}:
18227 @example
18228 sed -e '@var{command-1}' \
18229     -e '@var{command-2}'
18230 @end example
18232 @noindent
18233 as opposed to the equivalent:
18235 @example
18236 sed '
18237   @var{command-1}
18238   @var{command-2}
18240 @end example
18242 @noindent
18243 The following usage is sometimes equivalent:
18245 @example
18246 sed '@var{command-1};@var{command-2}'
18247 @end example
18249 but Posix says that this use of a semicolon has undefined effect if
18250 @var{command-1}'s verb is @samp{@{}, @samp{a}, @samp{b}, @samp{c},
18251 @samp{i}, @samp{r}, @samp{t}, @samp{w}, @samp{:}, or @samp{#}, so you
18252 should use semicolon only with simple scripts that do not use these
18253 verbs.
18255 The argument of the @option{-e} option must be a syntactically complete
18256 script.  GNU @command{sed} allows to pass multiple script fragments,
18257 each as argument of a separate @option{-e} option, that are then combined,
18258 with newlines between the fragments.  A future Posix revision may allow
18259 this as well.  But in Posix:2008, this is not allowed, and the
18260 @command{sed} programs on Solaris 10, HP-UX 11, and AIX don't allow it
18261 either:
18263 @example
18264 $ @kbd{echo a | sed -n -e 'i\}
18265 @kbd{0'}
18267 $ @kbd{echo a | sed -n -e 'i\' -e 0}
18268 Unrecognized command: 0
18269 @end example
18271 Commands inside @{ @} brackets are further restricted.  Posix says that
18272 they cannot be preceded by addresses, @samp{!}, or @samp{;}, and that
18273 each command must be followed immediately by a newline, without any
18274 intervening blanks or semicolons.  The closing bracket must be alone on
18275 a line, other than white space preceding or following it.
18277 Contrary to yet another urban legend, you may portably use @samp{&} in
18278 the replacement part of the @code{s} command to mean ``what was
18279 matched''.  All descendants of Unix version 7 @command{sed}
18280 (at least; we
18281 don't have first hand experience with older @command{sed} implementations) have
18282 supported it.
18284 Posix requires that you must not have any white space between
18285 @samp{!} and the following command.  It is OK to have blanks between
18286 the address and the @samp{!}.  For instance, on Solaris:
18288 @example
18289 $ @kbd{echo "foo" | sed -n '/bar/ ! p'}
18290 @error{}Unrecognized command: /bar/ ! p
18291 $ @kbd{echo "foo" | sed -n '/bar/! p'}
18292 @error{}Unrecognized command: /bar/! p
18293 $ @kbd{echo "foo" | sed -n '/bar/ !p'}
18295 @end example
18297 Posix also says that you should not combine @samp{!} and @samp{;}.  If
18298 you use @samp{!}, it is best to put it on a command that is delimited by
18299 newlines rather than @samp{;}.
18301 Also note that Posix requires that the @samp{b}, @samp{t}, @samp{r}, and
18302 @samp{w} commands be followed by exactly one space before their argument.
18303 On the other hand, no white space is allowed between @samp{:} and the
18304 subsequent label name.
18306 If a sed script is specified on the command line and ends in an
18307 @samp{a}, @samp{c}, or @samp{i} command, the last line of inserted text
18308 should be followed by a newline.  Otherwise some @command{sed}
18309 implementations (e.g., OpenBSD 3.9) do not append a newline to the
18310 inserted text.
18312 Many @command{sed} implementations (e.g., MacOS X 10.4,
18313 OpenBSD 3.9, Solaris 10
18314 @command{/usr/ucb/sed}) strip leading white space from the text of
18315 @samp{a}, @samp{c}, and @samp{i} commands.  Prepend a backslash to
18316 work around this incompatibility with Posix:
18318 @example
18319 $ @kbd{echo flushleft | sed 'a\}
18320 > @kbd{   indented}
18321 > @kbd{'}
18322 flushleft
18323 indented
18324 $ @kbd{echo foo | sed 'a\}
18325 > @kbd{\   indented}
18326 > @kbd{'}
18327 flushleft
18328    indented
18329 @end example
18331 Posix requires that with an empty regular expression, the last non-empty
18332 regular expression from either an address specification or substitution
18333 command is applied.  However, busybox 1.6.1 complains when using a
18334 substitution command with a replacement containing a back-reference to
18335 an empty regular expression; the workaround is repeating the regular
18336 expression.
18338 @example
18339 $ @kbd{echo abc | busybox sed '/a\(b\)c/ s//\1/'}
18340 sed: No previous regexp.
18341 $ @kbd{echo abc | busybox sed '/a\(b\)c/ s/a\(b\)c/\1/'}
18343 @end example
18346 @item @command{sed} (@samp{t})
18347 @c ---------------------------
18348 @prindex @command{sed} (@samp{t})
18349 Some old systems have @command{sed} that ``forget'' to reset their
18350 @samp{t} flag when starting a new cycle.  For instance on MIPS
18351 RISC/OS, and on IRIX 5.3, if you run the following @command{sed}
18352 script (the line numbers are not actual part of the texts):
18354 @example
18355 s/keep me/kept/g  # a
18356 t end             # b
18357 s/.*/deleted/g    # c
18358 :end              # d
18359 @end example
18361 @noindent
18364 @example
18365 delete me         # 1
18366 delete me         # 2
18367 keep me           # 3
18368 delete me         # 4
18369 @end example
18371 @noindent
18372 you get
18374 @example
18375 deleted
18376 delete me
18377 kept
18378 deleted
18379 @end example
18381 @noindent
18382 instead of
18384 @example
18385 deleted
18386 deleted
18387 kept
18388 deleted
18389 @end example
18391 Why?  When processing line 1, (c) matches, therefore sets the @samp{t}
18392 flag, and the output is produced.  When processing
18393 line 2, the @samp{t} flag is still set (this is the bug).  Command (a)
18394 fails to match, but @command{sed} is not supposed to clear the @samp{t}
18395 flag when a substitution fails.  Command (b) sees that the flag is set,
18396 therefore it clears it, and jumps to (d), hence you get @samp{delete me}
18397 instead of @samp{deleted}.  When processing line (3), @samp{t} is clear,
18398 (a) matches, so the flag is set, hence (b) clears the flags and jumps.
18399 Finally, since the flag is clear, line 4 is processed properly.
18401 There are two things one should remember about @samp{t} in @command{sed}.
18402 Firstly, always remember that @samp{t} jumps if @emph{some} substitution
18403 succeeded, not only the immediately preceding substitution.  Therefore,
18404 always use a fake @samp{t clear} followed by a @samp{:clear} on the next
18405 line, to reset the @samp{t} flag where needed.
18407 Secondly, you cannot rely on @command{sed} to clear the flag at each new
18408 cycle.
18410 One portable implementation of the script above is:
18412 @example
18413 t clear
18414 :clear
18415 s/keep me/kept/g
18416 t end
18417 s/.*/deleted/g
18418 :end
18419 @end example
18421 @item @command{sleep}
18422 @c ------------------
18423 @prindex @command{sleep}
18424 Using @command{sleep} is generally portable.  However, remember that
18425 adding a @command{sleep} to work around timestamp issues, with a minimum
18426 granularity of one second, doesn't scale well for parallel builds on
18427 modern machines with sub-second process completion.
18429 @item @command{sort}
18430 @c -----------------
18431 @prindex @command{sort}
18432 Remember that sort order is influenced by the current locale.  Inside
18433 @file{configure}, the C locale is in effect, but in Makefile snippets,
18434 you may need to specify @code{LC_ALL=C sort}.
18436 @item @command{tar}
18437 @c ----------------
18438 @prindex @command{tar}
18439 There are multiple file formats for @command{tar}; if you use Automake,
18440 the macro @code{AM_INIT_AUTOMAKE} has some options controlling which
18441 level of portability to use.
18443 @anchor{touch}
18444 @item @command{touch}
18445 @c ------------------
18446 @prindex @command{touch}
18447 @cindex timestamp resolution
18448 If you specify the desired timestamp (e.g., with the @option{-r}
18449 option), @command{touch} typically uses the @code{utime} or
18450 @code{utimes} system call, which can result in the same kind of
18451 timestamp truncation problems that @samp{cp -p} has.
18453 On ancient BSD systems, @command{touch} or any command that
18454 results in an empty file does not update the timestamps, so use a
18455 command like @command{echo} as a workaround.
18456 Also,
18457 GNU @command{touch} 3.16r (and presumably all before that)
18458 fails to work on SunOS 4.1.3 when the empty file is on an
18459 NFS-mounted 4.2 volume.
18460 However, these problems are no longer of practical concern.
18462 @item @command{tr}
18463 @c ---------------
18464 @prindex @command{tr}
18465 @cindex carriage return, deleting
18466 @cindex newline, deleting
18467 @cindex deleting carriage return
18468 Not all versions of @command{tr} handle all backslash character escapes.
18469 For example, Solaris 10 @command{/usr/ucb/tr} falls over, even though
18470 Solaris contains more modern @command{tr} in other locations.
18471 Using octal escapes is more portable for carriage returns, since
18472 @samp{\015} is the same for both ASCII and EBCDIC, and since use of
18473 literal carriage returns in scripts causes a number of other problems.
18474 But for other characters, like newline, using octal escapes ties the
18475 operation to ASCII, so it is better to use literal characters.
18477 @example
18478 $ @kbd{@{ echo moon; echo light; @} | /usr/ucb/tr -d '\n' ; echo}
18480 light
18481 $ @kbd{@{ echo moon; echo light; @} | /usr/bin/tr -d '\n' ; echo}
18482 moonlight
18483 $ @kbd{@{ echo moon; echo light; @} | /usr/ucb/tr -d '\012' ; echo}
18484 moonlight
18485 $ @kbd{nl='}
18486 @kbd{'; @{ echo moon; echo light; @} | /usr/ucb/tr -d "$nl" ; echo}
18487 moonlight
18488 @end example
18490 Not all versions of @command{tr} recognize ranges of characters: at
18491 least Solaris @command{/usr/bin/tr} still fails to do so.  But you can
18492 use @command{/usr/xpg4/bin/tr} instead.
18494 @example
18495 $ @kbd{echo "Hazy Fantazy" | LC_ALL=C /usr/bin/tr a-z A-Z}
18496 HAZy FAntAZy
18497 $ @kbd{echo "Hazy Fantazy" | LC_ALL=C /usr/xpg4/bin/tr a-z A-Z}
18498 HAZY FANTAZY
18499 @end example
18501 When providing two arguments, be sure the second string is at least as
18502 long as the first.
18504 @example
18505 $ @kbd{echo abc | /usr/xpg4/bin/tr bc d}
18507 $ @kbd{echo abc | coreutils/tr bc d}
18509 @end example
18511 Posix requires @command{tr} to operate on binary files.  But at least
18512 Solaris @command{/usr/ucb/tr} and @command{/usr/bin/tr} silently discard
18513 @code{NUL} in the input prior to doing any translation.  When using
18514 @command{tr} to process a binary file that may contain @code{NUL} bytes,
18515 it is necessary to use @command{/usr/xpg4/bin/tr} instead, or
18516 @command{/usr/xpg6/bin/tr} if that is available.
18518 @example
18519 $ @kbd{printf 'a\0b' | /usr/ucb/tr x x | od -An -tx1}
18520  61 62
18521 $ @kbd{printf 'a\0b' | /usr/bin/tr x x | od -An -tx1}
18522  61 62
18523 $ @kbd{printf 'a\0b' | /usr/xpg4/bin/tr x x | od -An -tx1}
18524  61 00 62
18525 @end example
18527 Solaris @command{/usr/ucb/tr} additionally fails to handle @samp{\0} as the
18528 octal escape for @code{NUL}.
18530 @example
18531 $ @kbd{printf 'abc' | /usr/ucb/tr 'bc' '\0d' | od -An -tx1}
18532  61 62 63
18533 $ @kbd{printf 'abc' | /usr/bin/tr 'bc' '\0d' | od -An -tx1}
18534  61 00 64
18535 $ @kbd{printf 'abc' | /usr/xpg4/bin/tr 'bc' '\0d' | od -An -tx1}
18536  61 00 64
18537 @end example
18539 @end table
18542 @node Portable Make
18543 @chapter Portable Make Programming
18544 @prindex @command{make}
18545 @cindex Limitations of @command{make}
18547 Writing portable makefiles is an art.  Since a makefile's commands are
18548 executed by the shell, you must consider the shell portability issues
18549 already mentioned.  However, other issues are specific to @command{make}
18550 itself.
18552 @menu
18553 * $< in Ordinary Make Rules::   $< in ordinary rules
18554 * Failure in Make Rules::       Failing portably in rules
18555 * Special Chars in Names::      Special Characters in Macro Names
18556 * Backslash-Newline-Newline::   Empty last lines in macro definitions
18557 * Backslash-Newline Comments::  Spanning comments across line boundaries
18558 * Long Lines in Makefiles::     Line length limitations
18559 * Macros and Submakes::         @code{make macro=value} and submakes
18560 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
18561 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
18562 * Parallel Make::               Parallel @command{make} quirks
18563 * Comments in Make Rules::      Other problems with Make comments
18564 * obj/ and Make::               Don't name a subdirectory @file{obj}
18565 * make -k Status::              Exit status of @samp{make -k}
18566 * VPATH and Make::              @code{VPATH} woes
18567 * Single Suffix Rules::         Single suffix rules and separated dependencies
18568 * Timestamps and Make::         Subsecond timestamp resolution
18569 @end menu
18571 @node $< in Ordinary Make Rules
18572 @section @code{$<} in Ordinary Make Rules
18574 Posix says that the @samp{$<} construct in makefiles can be
18575 used only in inference rules and in the @samp{.DEFAULT} rule; its
18576 meaning in ordinary rules is unspecified.  Solaris @command{make}
18577 for instance replaces it with the empty string.  OpenBSD (3.0 and
18578 later) @command{make} diagnoses these uses and errors out.
18580 @node Failure in Make Rules
18581 @section Failure in Make Rules
18583 Posix 2008 requires that @command{make} must invoke each command with
18584 the equivalent of a @samp{sh -e -c} subshell, which causes the
18585 subshell to exit immediately if a subsidiary simple-command fails,
18586 although not all @command{make} implementations have historically
18587 followed this rule.  For
18588 example, the command @samp{touch T; rm -f U} may attempt to
18589 remove @file{U} even if the @command{touch} fails, although this is not
18590 permitted with Posix make.  One way to work around failures in simple
18591 commands is to reword them so that they always succeed, e.g., @samp{touch
18592 T || :; rm -f U}.
18593 However, even this approach can run into common bugs in BSD
18594 implementations of the @option{-e} option of @command{sh} and
18595 @command{set} (@pxref{set, , Limitations of Shell Builtins}), so if you
18596 are worried
18597 about porting to buggy BSD shells it may be simpler to migrate
18598 complicated @command{make} actions into separate scripts.
18600 @node Special Chars in Names
18601 @section Special Characters in Make Macro Names
18603 Posix limits macro names to nonempty strings containing only
18604 ASCII letters and digits, @samp{.}, and @samp{_}.  Many
18605 @command{make} implementations allow a wider variety of characters, but
18606 portable makefiles should avoid them.  It is portable to start a name
18607 with a special character, e.g., @samp{$(.FOO)}.
18609 Some ancient @command{make} implementations don't support leading
18610 underscores in macro names.  An example is NEWS-OS 4.2R.
18612 @example
18613 $ @kbd{cat Makefile}
18614 _am_include = #
18615 _am_quote =
18616 all:; @@echo this is test
18617 $ @kbd{make}
18618 Make: Must be a separator on rules line 2.  Stop.
18619 $ @kbd{cat Makefile2}
18620 am_include = #
18621 am_quote =
18622 all:; @@echo this is test
18623 $ @kbd{make -f Makefile2}
18624 this is test
18625 @end example
18627 @noindent
18628 However, this problem is no longer of practical concern.
18630 @node Backslash-Newline-Newline
18631 @section Backslash-Newline-Newline in Make Macro Values
18633 @c  This has been seen on ia64 hpux 11.20, and on one hppa hpux 10.20,
18634 @c  but another hppa hpux 10.20 didn't have it.  Bob Proulx
18635 @c  <bob@proulx.com> thinks it was in hpux 8.0 too.
18636 On some versions of HP-UX, @command{make} reads multiple newlines
18637 following a backslash, continuing to the next non-empty line.  For
18638 example,
18640 @example
18641 FOO = one \
18643 BAR = two
18645 test:
18646         : FOO is "$(FOO)"
18647         : BAR is "$(BAR)"
18648 @end example
18650 @noindent
18651 shows @code{FOO} equal to @code{one BAR = two}.  Other implementations
18652 sensibly let a backslash continue only to the immediately following
18653 line.
18655 @node Backslash-Newline Comments
18656 @section Backslash-Newline in Make Comments
18658 According to Posix, Make comments start with @code{#}
18659 and continue until an unescaped newline is reached.
18661 @example
18662 $ @kbd{cat Makefile}
18663 # A = foo \
18664       bar \
18665       baz
18667 all:
18668         @@echo ok
18669 $ @kbd{make}   # GNU make
18671 @end example
18673 @noindent
18674 However this is not always the case.  Some implementations
18675 discard everything from @code{#} through the end of the line, ignoring any
18676 trailing backslash.
18678 @example
18679 $ @kbd{pmake}  # BSD make
18680 "Makefile", line 3: Need an operator
18681 Fatal errors encountered -- cannot continue
18682 @end example
18684 @noindent
18685 Therefore, if you want to comment out a multi-line definition, prefix each
18686 line with @code{#}, not only the first.
18688 @example
18689 # A = foo \
18690 #     bar \
18691 #     baz
18692 @end example
18694 @node Long Lines in Makefiles
18695 @section Long Lines in Makefiles
18697 Tru64 5.1's @command{make} has been reported to crash when given a
18698 makefile with lines longer than around 20 kB.  Earlier versions are
18699 reported to exit with @code{Line too long} diagnostics.
18701 @node Macros and Submakes
18702 @section @code{make macro=value} and Submakes
18704 A command-line variable definition such as @code{foo=bar} overrides any
18705 definition of @code{foo} in a makefile.  Some @command{make}
18706 implementations (such as GNU @command{make}) propagate this
18707 override to subsidiary invocations of @command{make}.  Some other
18708 implementations do not pass the substitution along to submakes.
18710 @example
18711 $ @kbd{cat Makefile}
18712 foo = foo
18713 one:
18714         @@echo $(foo)
18715         $(MAKE) two
18716 two:
18717         @@echo $(foo)
18718 $ @kbd{make foo=bar}            # GNU make 3.79.1
18720 make two
18721 make[1]: Entering directory `/home/adl'
18723 make[1]: Leaving directory `/home/adl'
18724 $ @kbd{pmake foo=bar}           # BSD make
18726 pmake two
18728 @end example
18730 You have a few possibilities if you do want the @code{foo=bar} override
18731 to propagate to submakes.  One is to use the @option{-e}
18732 option, which causes all environment variables to have precedence over
18733 the makefile macro definitions, and declare foo as an environment
18734 variable:
18736 @example
18737 $ @kbd{env foo=bar make -e}
18738 @end example
18740 The @option{-e} option is propagated to submakes automatically,
18741 and since the environment is inherited between @command{make}
18742 invocations, the @code{foo} macro is overridden in
18743 submakes as expected.
18745 This syntax (@code{foo=bar make -e}) is portable only when used
18746 outside of a makefile, for instance from a script or from the
18747 command line.  When run inside a @command{make} rule, GNU
18748 @command{make} 3.80 and prior versions forget to propagate the
18749 @option{-e} option to submakes.
18751 Moreover, using @option{-e} could have unexpected side effects if your
18752 environment contains some other macros usually defined by the
18753 makefile.  (See also the note about @code{make -e} and @code{SHELL}
18754 below.)
18756 Another way to propagate overrides to submakes is to do it
18757 manually, from your makefile:
18759 @example
18760 foo = foo
18761 one:
18762         @@echo $(foo)
18763         $(MAKE) foo=$(foo) two
18764 two:
18765         @@echo $(foo)
18766 @end example
18768 You need to foresee all macros that a user might want to override if
18769 you do that.
18771 Makefiles generated by @command{automake} expand @code{$(AM_MAKEFLAGS)}
18772 on the command line of submakes, which can be used for propagated overrides
18773 (@pxref{Subdirectories, , Automake, automake, GNU Automake}).
18775 @node The Make Macro MAKEFLAGS
18776 @section The Make Macro MAKEFLAGS
18777 @cindex @code{MAKEFLAGS} and @command{make}
18778 @cindex @command{make} and @code{MAKEFLAGS}
18780 Posix requires @command{make} to use @code{MAKEFLAGS} to affect the
18781 current and recursive invocations of make, but allows implementations
18782 several formats for the variable.  It is tricky to parse
18783 @code{$MAKEFLAGS} to determine whether @option{-s} for silent execution
18784 or @option{-k} for continued execution are in effect.  For example, you
18785 cannot assume that the first space-separated word in @code{$MAKEFLAGS}
18786 contains single-letter options, since in the Cygwin version of
18787 GNU @command{make} it is either @option{--unix} or
18788 @option{--win32} with the second word containing single-letter options.
18790 @example
18791 $ @kbd{cat Makefile}
18792 all:
18793         @@echo MAKEFLAGS = $(MAKEFLAGS)
18794 $ @kbd{make}
18795 MAKEFLAGS = --unix
18796 $ @kbd{make -k}
18797 MAKEFLAGS = --unix -k
18798 @end example
18800 @node The Make Macro SHELL
18801 @section The Make Macro @code{SHELL}
18802 @cindex @code{SHELL} and @command{make}
18803 @cindex @command{make} and @code{SHELL}
18805 Posix-compliant @command{make} internally uses the @code{$(SHELL)}
18806 macro to spawn shell processes and execute Make rules.  This
18807 is a builtin macro supplied by @command{make}, but it can be modified
18808 by a makefile or by a command-line argument.
18810 Not all @command{make} implementations define this @code{SHELL} macro.
18811 Tru64
18812 @command{make} is an example; this implementation always uses
18813 @code{/bin/sh}.  So it's a good idea to always define @code{SHELL} in
18814 your makefiles.  If you use Autoconf, do
18816 @example
18817 SHELL = @@SHELL@@
18818 @end example
18820 @noindent
18821 If you use Automake, this is done for you.
18823 Do not force @code{SHELL = /bin/sh} because that is not correct
18824 everywhere.  Remember, @file{/bin/sh} is not Posix compliant on many
18825 systems, such as FreeBSD 4, NetBSD 3, AIX 3, Solaris 10, or Tru64.
18826 Additionally, DJGPP lacks @code{/bin/sh}, and when its
18827 GNU @command{make} port sees such a setting it enters a
18828 special emulation mode where features like pipes and redirections are
18829 emulated on top of DOS's @command{command.com}.  Unfortunately this
18830 emulation is incomplete; for instance it does not handle command
18831 substitutions.  Using @code{@@SHELL@@} means that your makefile will
18832 benefit from the same improved shell, such as @command{bash} or
18833 @command{ksh}, that was discovered during @command{configure}, so that
18834 you aren't fighting two different sets of shell bugs between the two
18835 contexts.
18837 Posix-compliant @command{make} should never acquire the value of
18838 $(SHELL) from the environment, even when @code{make -e} is used
18839 (otherwise, think about what would happen to your rules if
18840 @code{SHELL=/bin/tcsh}).
18842 However not all @command{make} implementations have this exception.
18843 For instance it's not surprising that Tru64 @command{make} doesn't
18844 protect @code{SHELL}, since it doesn't use it.
18846 @example
18847 $ @kbd{cat Makefile}
18848 SHELL = /bin/sh
18849 FOO = foo
18850 all:
18851         @@echo $(SHELL)
18852         @@echo $(FOO)
18853 $ @kbd{env SHELL=/bin/tcsh FOO=bar make -e}   # Tru64 Make
18854 /bin/tcsh
18856 $ @kbd{env SHELL=/bin/tcsh FOO=bar gmake -e}  # GNU make
18857 /bin/sh
18859 @end example
18861 Conversely, @command{make} is not supposed to export any changes to the
18862 macro @code{SHELL} to child processes.  Again, many implementations
18863 break this rule:
18865 @example
18866 $ @kbd{cat Makefile}
18867 all:
18868         @@echo $(SHELL)
18869         @@printenv SHELL
18870 $ @kbd{env SHELL=sh make -e SHELL=/bin/ksh}   # BSD Make, GNU make 3.80
18871 /bin/ksh
18872 /bin/ksh
18873 $ @kbd{env SHELL=sh gmake -e SHELL=/bin/ksh}  # GNU make 3.81
18874 /bin/ksh
18876 @end example
18878 @node Parallel Make
18879 @section Parallel Make
18880 @cindex Parallel @command{make}
18882 Support for parallel execution in @command{make} implementation varies.
18883 Generally, using GNU make is your best bet.  When NetBSD
18884 @command{make} is invoked with @option{-j@var{N}}, it will reuse the
18885 same shell for multiple commands within one recipe.  This can have
18886 unexpected consequences.@footnote{Note that GNU make has
18887 heuristics to avoid spawning a shell at all if the command is deemed
18888 safe to be executed directly.} For example, change of directories or
18889 variables persist between commands:
18891 @example
18892 all:
18893         @@var=value; cd /; pwd; echo $$var; echo $$$$
18894         @@pwd; echo $$var; echo $$$$
18895 @end example
18897 @noindent
18898 may output the following with @code{make -j1}:
18900 @example
18901 --- all ---
18903 value
18904 32235
18906 value
18907 32235
18908 @end example
18910 while without @option{-j1}, or with @option{-B}, the output looks less
18911 surprising:
18913 @example
18915 value
18916 32238
18917 /tmp
18919 32239
18920 @end example
18922 Another consequence of this is that, if one command in a recipe uses
18923 @code{exit 0} to indicate a successful exit, the shell will be gone
18924 and the remaining commands of this recipe will not be executed.
18926 The above example also shows additional status output NetBSD
18927 @command{make} produces in parallel mode for targets being updated.
18929 Furthermore, parallel NetBSD @command{make} will route standard error
18930 from commands that it spawns into its own standard output, and may
18931 remove leading whitespace from output lines.
18933 You can avoid these issues by using the @option{-B} option to enable
18934 compatibility semantics.  However, that will effectively also disable
18935 all parallelism as that will cause prerequisites to be updated in the
18936 order they are listed in a rule.
18938 @node Comments in Make Rules
18939 @section Comments in Make Rules
18940 @cindex Comments in @file{Makefile} rules
18941 @cindex @file{Makefile} rules and comments
18943 Never put comments in a rule.
18945 Some @command{make} treat anything starting with a tab as a command for
18946 the current rule, even if the tab is immediately followed by a @code{#}.
18947 The @command{make} from Tru64 Unix V5.1 is one of them.  The following
18948 makefile runs @code{# foo} through the shell.
18950 @example
18951 all:
18952         # foo
18953 @end example
18955 @node obj/ and Make
18956 @section The @file{obj/} Subdirectory and Make
18957 @cindex @file{obj/}, subdirectory
18958 @cindex BSD @command{make} and @file{obj/}
18960 Never name one of your subdirectories @file{obj/} if you don't like
18961 surprises.
18963 If an @file{obj/} directory exists, BSD @command{make} enters it
18964 before reading the makefile.  Hence the makefile in the
18965 current directory is not read.
18967 @example
18968 $ @kbd{cat Makefile}
18969 all:
18970         echo Hello
18971 $ @kbd{cat obj/Makefile}
18972 all:
18973         echo World
18974 $ @kbd{make}      # GNU make
18975 echo Hello
18976 Hello
18977 $ @kbd{pmake}     # BSD make
18978 echo World
18979 World
18980 @end example
18982 @node make -k Status
18983 @section Exit Status of @code{make -k}
18984 @cindex @code{make -k}
18986 Do not rely on the exit status of @code{make -k}.  Some implementations
18987 reflect whether they encountered an error in their exit status; other
18988 implementations always succeed.
18990 @example
18991 $ @kbd{cat Makefile}
18992 all:
18993         false
18994 $ @kbd{make -k; echo exit status: $?}    # GNU make
18995 false
18996 make: *** [all] Error 1
18997 exit status: 2
18998 $ @kbd{pmake -k; echo exit status: $?}   # BSD make
18999 false
19000 *** Error code 1 (continuing)
19001 exit status: 0
19002 @end example
19004 @node VPATH and Make
19005 @section @code{VPATH} and Make
19006 @cindex @code{VPATH}
19008 Posix does not specify the semantics of @code{VPATH}.  Typically,
19009 @command{make} supports @code{VPATH}, but its implementation is not
19010 consistent.
19012 Autoconf and Automake support makefiles whose usages of @code{VPATH} are
19013 portable to recent-enough popular implementations of @command{make}, but
19014 to keep the resulting makefiles portable, a package's makefile
19015 prototypes must take the following issues into account.  These issues
19016 are complicated and are often poorly understood, and installers who use
19017 @code{VPATH} should expect to find many bugs in this area.  If you use
19018 @code{VPATH}, the simplest way to avoid these portability bugs is to
19019 stick with GNU @command{make}, since it is the most
19020 commonly-used @command{make} among Autoconf users.
19022 Here are some known issues with some @code{VPATH}
19023 implementations.
19025 @menu
19026 * Variables listed in VPATH::   @code{VPATH} must be literal on ancient hosts
19027 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
19028 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
19029 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
19030 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
19031 * Make Target Lookup::          More details about @code{VPATH} lookup
19032 @end menu
19034 @node Variables listed in VPATH
19035 @subsection Variables listed in @code{VPATH}
19036 @cindex @code{VPATH} and variables
19037 @cindex variables and @code{VPATH}
19039 Do not set @code{VPATH} to the value of another variable, for example
19040 @samp{VPATH = $(srcdir)}, because some ancient versions of
19041 @command{make} do not do variable substitutions on the value of
19042 @code{VPATH}.  For example, use this
19044 @example
19045 srcdir = @@srcdir@@
19046 VPATH = @@srcdir@@
19047 @end example
19049 @noindent
19050 rather than @samp{VPATH = $(srcdir)}.  Note that with GNU
19051 Automake, there is no need to set this yourself.
19053 @node VPATH and Double-colon
19054 @subsection @code{VPATH} and Double-colon Rules
19055 @cindex @code{VPATH} and double-colon rules
19056 @cindex double-colon rules and @code{VPATH}
19058 With ancient versions of Sun @command{make},
19059 any assignment to @code{VPATH} causes @command{make} to execute only
19060 the first set of double-colon rules.
19061 However, this problem is no longer of practical concern.
19063 @node $< in Explicit Rules
19064 @subsection @code{$<} Not Supported in Explicit Rules
19065 @cindex explicit rules, @code{$<}, and @code{VPATH}
19066 @cindex @code{$<}, explicit rules, and @code{VPATH}
19067 @cindex @code{VPATH}, explicit rules, and @code{$<}
19069 Using @code{$<} in explicit rules is not portable.
19070 The prerequisite file must be named explicitly in the rule.  If you want
19071 to find the prerequisite via a @code{VPATH} search, you have to code the
19072 whole thing manually.  @xref{Build Directories}.
19074 @node Automatic Rule Rewriting
19075 @subsection Automatic Rule Rewriting
19076 @cindex @code{VPATH} and automatic rule rewriting
19077 @cindex automatic rule rewriting and @code{VPATH}
19079 Some @command{make} implementations, such as Solaris and Tru64,
19080 search for prerequisites in @code{VPATH} and
19081 then rewrite each occurrence as a plain word in the rule.
19082 For instance:
19084 @example
19085 # This isn't portable to GNU make.
19086 VPATH = ../pkg/src
19087 f.c: if.c
19088         cp if.c f.c
19089 @end example
19091 @noindent
19092 executes @code{cp ../pkg/src/if.c f.c} if @file{if.c} is
19093 found in @file{../pkg/src}.
19095 However, this rule leads to real problems in practice.  For example, if
19096 the source directory contains an ordinary file named @file{test} that is
19097 used in a dependency, Solaris @command{make} rewrites commands like
19098 @samp{if test -r foo; @dots{}} to @samp{if ../pkg/src/test -r foo;
19099 @dots{}}, which is typically undesirable.  To avoid this problem,
19100 portable makefiles should never mention a source file whose name is that
19101 of a shell keyword like @file{until} or a shell command like
19102 @command{cat} or @command{gcc} or @command{test}.
19104 Because of these problems GNU @command{make} and many other
19105 @command{make} implementations do not rewrite commands, so portable
19106 makefiles should
19107 search @code{VPATH} manually.  It is tempting to write this:
19109 @smallexample
19110 # This isn't portable to Solaris make.
19111 VPATH = ../pkg/src
19112 f.c: if.c
19113         cp `test -f if.c || echo $(VPATH)/`if.c f.c
19114 @end smallexample
19116 @noindent
19117 However, the ``prerequisite rewriting'' still applies here.  So if
19118 @file{if.c} is in @file{../pkg/src}, Solaris and Tru64 @command{make}
19119 execute
19121 @smallexample
19122 cp `test -f ../pkg/src/if.c || echo ../pkg/src/`if.c f.c
19123 @end smallexample
19125 @noindent
19126 which reduces to
19128 @example
19129 cp if.c f.c
19130 @end example
19132 @noindent
19133 and thus fails.  Oops.
19135 A simple workaround, and good practice anyway, is to use @samp{$?} and
19136 @samp{$@@} when possible:
19138 @smallexample
19139 VPATH = ../pkg/src
19140 f.c: if.c
19141         cp $? $@@
19142 @end smallexample
19144 @noindent
19145 but this does not generalize well to commands with multiple
19146 prerequisites.  A more general workaround is to rewrite the rule so that
19147 the prerequisite @file{if.c} never appears as a plain word.  For
19148 example, these three rules would be safe, assuming @file{if.c} is in
19149 @file{../pkg/src} and the other files are in the working directory:
19151 @smallexample
19152 VPATH = ../pkg/src
19153 f.c: if.c f1.c
19154         cat `test -f ./if.c || echo $(VPATH)/`if.c f1.c >$@@
19155 g.c: if.c g1.c
19156         cat `test -f 'if.c' || echo $(VPATH)/`if.c g1.c >$@@
19157 h.c: if.c h1.c
19158         cat `test -f "if.c" || echo $(VPATH)/`if.c h1.c >$@@
19159 @end smallexample
19161 Things get worse when your prerequisites are in a macro.
19163 @example
19164 VPATH = ../pkg/src
19165 HEADERS = f.h g.h h.h
19166 install-HEADERS: $(HEADERS)
19167         for i in $(HEADERS); do \
19168           $(INSTALL) -m 644 \
19169             `test -f $$i || echo $(VPATH)/`$$i \
19170             $(DESTDIR)$(includedir)/$$i; \
19171 @c $$ restore font-lock
19172         done
19173 @end example
19175 The above @code{install-HEADERS} rule is not Solaris-proof because @code{for
19176 i in $(HEADERS);} is expanded to @code{for i in f.h g.h h.h;}
19177 where @code{f.h} and @code{g.h} are plain words and are hence
19178 subject to @code{VPATH} adjustments.
19180 If the three files are in @file{../pkg/src}, the rule is run as:
19182 @example
19183 for i in ../pkg/src/f.h ../pkg/src/g.h h.h; do \
19184   install -m 644 \
19185      `test -f $i || echo ../pkg/src/`$i \
19186      /usr/local/include/$i; \
19187 done
19188 @end example
19190 where the two first @command{install} calls fail.  For instance,
19191 consider the @code{f.h} installation:
19193 @example
19194 install -m 644 \
19195   `test -f ../pkg/src/f.h || \
19196     echo ../pkg/src/ \
19197   `../pkg/src/f.h \
19198   /usr/local/include/../pkg/src/f.h;
19199 @end example
19201 @noindent
19202 It reduces to:
19204 @example
19205 install -m 644 \
19206   ../pkg/src/f.h \
19207   /usr/local/include/../pkg/src/f.h;
19208 @end example
19210 Note that the manual @code{VPATH} search did not cause any problems here;
19211 however this command installs @file{f.h} in an incorrect directory.
19213 Trying to quote @code{$(HEADERS)} in some way, as we did for
19214 @code{foo.c} a few makefiles ago, does not help:
19216 @example
19217 install-HEADERS: $(HEADERS)
19218         headers='$(HEADERS)'; \
19219         for i in $$headers; do \
19220           $(INSTALL) -m 644 \
19221             `test -f $$i || echo $(VPATH)/`$$i \
19222             $(DESTDIR)$(includedir)/$$i; \
19223         done
19224 @end example
19226 Now, @code{headers='$(HEADERS)'} macro-expands to:
19228 @example
19229 headers='f.h g.h h.h'
19230 @end example
19232 @noindent
19233 but @code{g.h} is still a plain word.  (As an aside, the idiom
19234 @code{headers='$(HEADERS)'; for i in $$headers;} is a good
19235 idea if @code{$(HEADERS)} can be empty, because some shells diagnose a
19236 syntax error on @code{for i in;}.)
19238 One workaround is to strip this unwanted @file{../pkg/src/} prefix manually:
19240 @example
19241 VPATH = ../pkg/src
19242 HEADERS = f.h g.h h.h
19243 install-HEADERS: $(HEADERS)
19244         headers='$(HEADERS)'; \
19245         for i in $$headers; do \
19246           i=`expr "$$i" : '$(VPATH)/\(.*\)'`;
19247           $(INSTALL) -m 644 \
19248             `test -f $$i || echo $(VPATH)/`$$i \
19249             $(DESTDIR)$(includedir)/$$i; \
19250 @c $$ restore font-lock
19251         done
19252 @end example
19254 Automake does something similar.  However the above hack works only if
19255 the files listed in @code{HEADERS} are in the current directory or a
19256 subdirectory; they should not be in an enclosing directory.  If we had
19257 @code{HEADERS = ../f.h}, the above fragment would fail in a VPATH
19258 build with Tru64 @command{make}.  The reason is that not only does
19259 Tru64 @command{make} rewrite dependencies, but it also simplifies
19260 them.  Hence @code{../f.h} becomes @code{../pkg/f.h} instead of
19261 @code{../pkg/src/../f.h}.  This obviously defeats any attempt to strip
19262 a leading @file{../pkg/src/} component.
19264 The following example makes the behavior of Tru64 @command{make}
19265 more apparent.
19267 @example
19268 $ @kbd{cat Makefile}
19269 VPATH = sub
19270 all: ../foo
19271         echo ../foo
19272 $ @kbd{ls}
19273 Makefile foo
19274 $ @kbd{make}
19275 echo foo
19277 @end example
19279 @noindent
19280 Dependency @file{../foo} was found in @file{sub/../foo}, but Tru64
19281 @command{make} simplified it as @file{foo}.  (Note that the @file{sub/}
19282 directory does not even exist, this just means that the simplification
19283 occurred before the file was checked for.)
19285 For the record here is how SunOS 4 @command{make} behaves on this
19286 example.
19288 @smallexample
19289 $ @kbd{make}
19290 make: Fatal error: Don't know how to make target `../foo'
19291 $ @kbd{mkdir sub}
19292 $ @kbd{make}
19293 echo sub/../foo
19294 sub/../foo
19295 @end smallexample
19298 @node Tru64 Directory Magic
19299 @subsection Tru64 @command{make} Creates Prerequisite Directories Magically
19300 @cindex @code{VPATH} and prerequisite directories
19301 @cindex prerequisite directories and @code{VPATH}
19303 When a prerequisite is a subdirectory of @code{VPATH}, Tru64
19304 @command{make} creates it in the current directory.
19306 @example
19307 $ @kbd{mkdir -p foo/bar build}
19308 $ @kbd{cd build}
19309 $ @kbd{cat >Makefile <<END
19310 VPATH = ..
19311 all: foo/bar
19312 END}
19313 $ @kbd{make}
19314 mkdir foo
19315 mkdir foo/bar
19316 @end example
19318 This can yield unexpected results if a rule uses a manual @code{VPATH}
19319 search as presented before.
19321 @example
19322 VPATH = ..
19323 all : foo/bar
19324         command `test -d foo/bar || echo ../`foo/bar
19325 @end example
19327 The above @command{command} is run on the empty @file{foo/bar}
19328 directory that was created in the current directory.
19330 @node Make Target Lookup
19331 @subsection Make Target Lookup
19332 @cindex @code{VPATH}, resolving target pathnames
19334 GNU @command{make} uses a complex algorithm to decide when it
19335 should use files found via a @code{VPATH} search.  @xref{Search
19336 Algorithm, , How Directory Searches are Performed, make, The GNU Make
19337 Manual}.
19339 If a target needs to be rebuilt, GNU @command{make} discards the
19340 file name found during the @code{VPATH} search for this target, and
19341 builds the file locally using the file name given in the makefile.
19342 If a target does not need to be rebuilt, GNU @command{make} uses the
19343 file name found during the @code{VPATH} search.
19345 Other @command{make} implementations, like NetBSD @command{make}, are
19346 easier to describe: the file name found during the @code{VPATH} search
19347 is used whether the target needs to be rebuilt or not.  Therefore
19348 new files are created locally, but existing files are updated at their
19349 @code{VPATH} location.
19351 OpenBSD and FreeBSD @command{make}, however,
19352 never perform a
19353 @code{VPATH} search for a dependency that has an explicit rule.
19354 This is extremely annoying.
19356 When attempting a @code{VPATH} build for an autoconfiscated package
19357 (e.g., @code{mkdir build && cd build && ../configure}), this means
19359 @command{make} builds everything locally in the @file{build}
19360 directory, while BSD @command{make} builds new files locally and
19361 updates existing files in the source directory.
19363 @example
19364 $ @kbd{cat Makefile}
19365 VPATH = ..
19366 all: foo.x bar.x
19367 foo.x bar.x: newer.x
19368         @@echo Building $@@
19369 $ @kbd{touch ../bar.x}
19370 $ @kbd{touch ../newer.x}
19371 $ @kbd{make}        # GNU make
19372 Building foo.x
19373 Building bar.x
19374 $ @kbd{pmake}       # NetBSD make
19375 Building foo.x
19376 Building ../bar.x
19377 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
19378 Building foo.x
19379 Building bar.x
19380 $ @kbd{tmake}       # Tru64 make
19381 Building foo.x
19382 Building bar.x
19383 $ @kbd{touch ../bar.x}
19384 $ @kbd{make}        # GNU make
19385 Building foo.x
19386 $ @kbd{pmake}       # NetBSD make
19387 Building foo.x
19388 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
19389 Building foo.x
19390 Building bar.x
19391 $ @kbd{tmake}       # Tru64 make
19392 Building foo.x
19393 Building bar.x
19394 @end example
19396 Note how NetBSD @command{make} updates @file{../bar.x} in its
19397 VPATH location, and how FreeBSD, OpenBSD, and Tru64
19398 @command{make} always
19399 update @file{bar.x}, even when @file{../bar.x} is up to date.
19401 Another point worth mentioning is that once GNU @command{make} has
19402 decided to ignore a @code{VPATH} file name (e.g., it ignored
19403 @file{../bar.x} in the above example) it continues to ignore it when
19404 the target occurs as a prerequisite of another rule.
19406 The following example shows that GNU @command{make} does not look up
19407 @file{bar.x} in @code{VPATH} before performing the @code{.x.y} rule,
19408 because it ignored the @code{VPATH} result of @file{bar.x} while running
19409 the @code{bar.x: newer.x} rule.
19411 @example
19412 $ @kbd{cat Makefile}
19413 VPATH = ..
19414 all: bar.y
19415 bar.x: newer.x
19416         @@echo Building $@@
19417 .SUFFIXES: .x .y
19418 .x.y:
19419         cp $< $@@
19420 $ @kbd{touch ../bar.x}
19421 $ @kbd{touch ../newer.x}
19422 $ @kbd{make}        # GNU make
19423 Building bar.x
19424 cp bar.x bar.y
19425 cp: cannot stat `bar.x': No such file or directory
19426 make: *** [bar.y] Error 1
19427 $ @kbd{pmake}       # NetBSD make
19428 Building ../bar.x
19429 cp ../bar.x bar.y
19430 $ @kbd{rm bar.y}
19431 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
19432 echo Building bar.x
19433 cp bar.x bar.y
19434 cp: cannot stat `bar.x': No such file or directory
19435 *** Error code 1
19436 $ @kbd{tmake}       # Tru64 make
19437 Building bar.x
19438 cp: bar.x: No such file or directory
19439 *** Exit 1
19440 @end example
19442 Note that if you drop away the command from the @code{bar.x: newer.x}
19443 rule, GNU @command{make} magically starts to work: it
19444 knows that @code{bar.x} hasn't been updated, therefore it doesn't
19445 discard the result from @code{VPATH} (@file{../bar.x}) in succeeding
19446 uses.  Tru64 also works, but FreeBSD and OpenBSD
19447 still don't.
19449 @example
19450 $ @kbd{cat Makefile}
19451 VPATH = ..
19452 all: bar.y
19453 bar.x: newer.x
19454 .SUFFIXES: .x .y
19455 .x.y:
19456         cp $< $@@
19457 $ @kbd{touch ../bar.x}
19458 $ @kbd{touch ../newer.x}
19459 $ @kbd{make}        # GNU make
19460 cp ../bar.x bar.y
19461 $ @kbd{rm bar.y}
19462 $ @kbd{pmake}       # NetBSD make
19463 cp ../bar.x bar.y
19464 $ @kbd{rm bar.y}
19465 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
19466 cp bar.x bar.y
19467 cp: cannot stat `bar.x': No such file or directory
19468 *** Error code 1
19469 $ @kbd{tmake}       # Tru64 make
19470 cp ../bar.x bar.y
19471 @end example
19473 It seems the sole solution that would please every @command{make}
19474 implementation is to never rely on @code{VPATH} searches for targets.
19475 In other words, @code{VPATH} should be reserved to unbuilt sources.
19478 @node Single Suffix Rules
19479 @section Single Suffix Rules and Separated Dependencies
19480 @cindex Single Suffix Inference Rule
19481 @cindex Rule, Single Suffix Inference
19482 A @dfn{Single Suffix Rule} is basically a usual suffix (inference) rule
19483 (@samp{.from.to:}), but which @emph{destination} suffix is empty
19484 (@samp{.from:}).
19486 @cindex Separated Dependencies
19487 @dfn{Separated dependencies} simply refers to listing the prerequisite
19488 of a target, without defining a rule.  Usually one can list on the one
19489 hand side, the rules, and on the other hand side, the dependencies.
19491 Solaris @command{make} does not support separated dependencies for
19492 targets defined by single suffix rules:
19494 @example
19495 $ @kbd{cat Makefile}
19496 .SUFFIXES: .in
19497 foo: foo.in
19498 .in:
19499         cp $< $@@
19500 $ @kbd{touch foo.in}
19501 $ @kbd{make}
19502 $ @kbd{ls}
19503 Makefile  foo.in
19504 @end example
19506 @noindent
19507 while GNU Make does:
19509 @example
19510 $ @kbd{gmake}
19511 cp foo.in foo
19512 $ @kbd{ls}
19513 Makefile  foo       foo.in
19514 @end example
19516 Note it works without the @samp{foo: foo.in} dependency.
19518 @example
19519 $ @kbd{cat Makefile}
19520 .SUFFIXES: .in
19521 .in:
19522         cp $< $@@
19523 $ @kbd{make foo}
19524 cp foo.in foo
19525 @end example
19527 @noindent
19528 and it works with double suffix inference rules:
19530 @example
19531 $ @kbd{cat Makefile}
19532 foo.out: foo.in
19533 .SUFFIXES: .in .out
19534 .in.out:
19535         cp $< $@@
19536 $ @kbd{make}
19537 cp foo.in foo.out
19538 @end example
19540 As a result, in such a case, you have to write target rules.
19542 @node Timestamps and Make
19543 @section Timestamp Resolution and Make
19544 @cindex timestamp resolution
19545 Traditionally, file timestamps had 1-second resolution, and
19546 @command{make} used those timestamps to determine whether one file was
19547 newer than the other.  However, many modern file systems have
19548 timestamps with 1-nanosecond resolution.  Some @command{make}
19549 implementations look at the entire timestamp; others ignore the
19550 fractional part, which can lead to incorrect results.  Normally this
19551 is not a problem, but in some extreme cases you may need to use tricks
19552 like @samp{sleep 1} to work around timestamp truncation bugs.
19554 Commands like @samp{cp -p} and @samp{touch -r} typically do not copy
19555 file timestamps to their full resolutions (@pxref{touch, , Limitations of Usual
19556 Tools}).  Hence you should be wary of rules like this:
19558 @example
19559 dest: src
19560         cp -p src dest
19561 @end example
19563 as @file{dest} often appears to be older than @file{src} after the
19564 timestamp is truncated, and this can cause @command{make} to do
19565 needless rework the next time it is invoked.  To work around this
19566 problem, you can use a timestamp file, e.g.:
19568 @example
19569 dest-stamp: src
19570         cp -p src dest
19571         date >dest-stamp
19572 @end example
19577 @c ======================================== Portable C and C++ Programming
19579 @node Portable C and C++
19580 @chapter Portable C and C++ Programming
19581 @cindex Portable C and C++ programming
19583 C and C++ programs often use low-level features of the underlying
19584 system, and therefore are often more difficult to make portable to other
19585 platforms.
19587 Several standards have been developed to help make your programs more
19588 portable.  If you write programs with these standards in mind, you can
19589 have greater confidence that your programs work on a wide variety
19590 of systems.
19591 @ifhtml
19592 @uref{http://@/gcc.gnu.org/@/onlinedocs/@/gcc/@/Standards.html, Language
19593 Standards Supported by GCC}
19594 @end ifhtml
19595 @ifnothtml
19596 @xref{Standards, , Language Standards Supported by
19597 GCC, gcc, Using the GNU Compiler Collection
19598 (GCC)},
19599 @end ifnothtml
19600 for a list of C-related standards.  Many programs also assume the
19601 @uref{http://@/www.opengroup.org/@/susv3, Posix standard}.
19603 Some old code is written to be portable to K&R C, which predates any C
19604 standard.  K&R C compilers are no longer of practical interest, though,
19605 and the rest of section assumes at least C89, the first C standard.
19607 Program portability is a huge topic, and this section can only briefly
19608 introduce common pitfalls.  @xref{System Portability, , Portability
19609 between System Types, standards, GNU Coding Standards}, for
19610 more information.
19612 @menu
19613 * Varieties of Unportability::  How to make your programs unportable
19614 * Integer Overflow::            When integers get too large
19615 * Preprocessor Arithmetic::     @code{#if} expression problems
19616 * Null Pointers::               Properties of null pointers
19617 * Buffer Overruns::             Subscript errors and the like
19618 * Volatile Objects::            @code{volatile} and signals
19619 * Floating Point Portability::  Portable floating-point arithmetic
19620 * Exiting Portably::            Exiting and the exit status
19621 @end menu
19623 @node Varieties of Unportability
19624 @section Varieties of Unportability
19625 @cindex portability
19627 Autoconf tests and ordinary programs often need to test what is allowed
19628 on a system, and therefore they may need to deliberately exceed the
19629 boundaries of what the standards allow, if only to see whether an
19630 optional feature is present.  When you write such a program, you should
19631 keep in mind the difference between constraints, unspecified behavior,
19632 and undefined behavior.
19634 In C, a @dfn{constraint} is a rule that the compiler must enforce.  An
19635 example constraint is that C programs must not declare a bit-field with
19636 negative width.  Tests can therefore reliably assume that programs with
19637 negative-width bit-fields are rejected by a compiler that conforms
19638 to the standard.
19640 @dfn{Unspecified behavior} is valid behavior, where the standard allows
19641 multiple possibilities.  For example, the order of evaluation of
19642 function arguments is unspecified.  Some unspecified behavior is
19643 @dfn{implementation-defined}, i.e., documented by the implementation,
19644 but since Autoconf tests cannot read the documentation they cannot
19645 distinguish between implementation-defined and other unspecified
19646 behavior.  It is common for Autoconf tests to probe implementations to
19647 determine otherwise-unspecified behavior.
19649 @dfn{Undefined behavior} is invalid behavior, where the standard allows
19650 the implementation to do anything it pleases.  For example,
19651 dereferencing a null pointer leads to undefined behavior.  If possible,
19652 test programs should avoid undefined behavior, since a program with
19653 undefined behavior might succeed on a test that should fail.
19655 The above rules apply to programs that are intended to conform to the
19656 standard.  However, strictly-conforming programs are quite rare, since
19657 the standards are so limiting.  A major goal of Autoconf is to support
19658 programs that use implementation features not described by the standard,
19659 and it is fairly common for test programs to violate the above rules, if
19660 the programs work well enough in practice.
19662 @node Integer Overflow
19663 @section Integer Overflow
19664 @cindex integer overflow
19665 @cindex overflow, signed integer
19666 @cindex signed integer overflow
19667 @cindex wraparound arithmetic
19669 In practice many portable C programs assume that signed integer overflow wraps
19670 around reliably using two's complement arithmetic.  Yet the C standard
19671 says that program behavior is undefined on overflow, and in a few cases
19672 C programs do not work on some modern implementations because their
19673 overflows do not wrap around as their authors expected.  Conversely, in
19674 signed integer remainder, the C standard requires overflow
19675 behavior that is commonly not implemented.
19677 @menu
19678 * Integer Overflow Basics::      Why integer overflow is a problem
19679 * Signed Overflow Examples::     Examples of code assuming wraparound
19680 * Optimization and Wraparound::  Optimizations that break uses of wraparound
19681 * Signed Overflow Advice::       Practical advice for signed overflow issues
19682 * Signed Integer Division::      @code{INT_MIN / -1} and @code{INT_MIN % -1}
19683 @end menu
19685 @node Integer Overflow Basics
19686 @subsection Basics of Integer Overflow
19687 @cindex integer overflow
19688 @cindex overflow, signed integer
19689 @cindex signed integer overflow
19690 @cindex wraparound arithmetic
19692 In languages like C, unsigned integer overflow reliably wraps around;
19693 e.g., @code{UINT_MAX + 1} yields zero.
19694 This is guaranteed by the C standard and is
19695 portable in practice, unless you specify aggressive,
19696 nonstandard optimization options
19697 suitable only for special applications.
19699 In contrast, the C standard says that signed integer overflow leads to
19700 undefined behavior where a program can do anything, including dumping
19701 core or overrunning a buffer.  The misbehavior can even precede the
19702 overflow.  Such an overflow can occur during addition, subtraction,
19703 multiplication, division, and left shift.
19705 Despite this requirement of the standard, many C programs and Autoconf
19706 tests assume that signed integer overflow silently wraps around modulo a
19707 power of two, using two's complement arithmetic, so long as you cast the
19708 resulting value to a signed integer type or store it into a signed
19709 integer variable.  If you use conservative optimization flags, such
19710 programs are generally portable to the vast majority of modern
19711 platforms, with a few exceptions discussed later.
19713 For historical reasons the C standard also allows implementations with
19714 ones' complement or signed magnitude arithmetic, but it is safe to
19715 assume two's complement nowadays.
19717 Also, overflow can occur when converting an out-of-range value to a
19718 signed integer type.  Here a standard implementation must define what
19719 happens, but this might include raising an exception.  In practice all
19720 known implementations support silent wraparound in this case, so you need
19721 not worry about other possibilities.
19723 @node Signed Overflow Examples
19724 @subsection Examples of Code Assuming Wraparound Overflow
19725 @cindex integer overflow
19726 @cindex overflow, signed integer
19727 @cindex signed integer overflow
19728 @cindex wraparound arithmetic
19730 There has long been a tension between what the C standard requires for
19731 signed integer overflow, and what C programs commonly assume.  The
19732 standard allows aggressive optimizations based on assumptions that
19733 overflow never occurs, but many practical C programs rely on overflow
19734 wrapping around.  These programs do not conform to the standard, but
19735 they commonly work in practice because compiler writers are
19736 understandably reluctant to implement optimizations that would break
19737 many programs, unless perhaps a user specifies aggressive optimization.
19739 The C Standard says that if a program has signed integer overflow its
19740 behavior is undefined, and the undefined behavior can even precede the
19741 overflow.  To take an extreme example:
19743 @c Inspired by Robert Dewar's example in
19744 @c <http://gcc.gnu.org/ml/gcc/2007-01/msg00038.html> (2007-01-01).
19745 @example
19746 if (password == expected_password)
19747   allow_superuser_privileges ();
19748 else if (counter++ == INT_MAX)
19749   abort ();
19750 else
19751   printf ("%d password mismatches\n", counter);
19752 @end example
19754 @noindent
19755 If the @code{int} variable @code{counter} equals @code{INT_MAX},
19756 @code{counter++} must overflow and the behavior is undefined, so the C
19757 standard allows the compiler to optimize away the test against
19758 @code{INT_MAX} and the @code{abort} call.
19759 Worse, if an earlier bug in the program lets the compiler deduce that
19760 @code{counter == INT_MAX} or that @code{counter} previously overflowed,
19761 the C standard allows the compiler to optimize away the password test
19762 and generate code that allows superuser privileges unconditionally.
19764 Despite this requirement by the standard, it has long been common for C
19765 code to assume wraparound arithmetic after signed overflow, and all
19766 known practical C implementations support some C idioms that assume
19767 wraparound signed arithmetic, even if the idioms do not conform
19768 strictly to the standard.  If your code looks like the following
19769 examples it will almost surely work with real-world compilers.
19771 Here is an example derived from the 7th Edition Unix implementation of
19772 @code{atoi} (1979-01-10):
19774 @example
19775 char *p;
19776 int f, n;
19777 @dots{}
19778 while (*p >= '0' && *p <= '9')
19779   n = n * 10 + *p++ - '0';
19780 return (f ? -n : n);
19781 @end example
19783 @noindent
19784 Even if the input string is in range, on most modern machines this has
19785 signed overflow when computing the most negative integer (the @code{-n}
19786 overflows) or a value near an extreme integer (the first @code{+}
19787 overflows).
19789 Here is another example, derived from the 7th Edition implementation of
19790 @code{rand} (1979-01-10).  Here the programmer expects both
19791 multiplication and addition to wrap on overflow:
19793 @example
19794 static long int randx = 1;
19795 @dots{}
19796 randx = randx * 1103515245 + 12345;
19797 return (randx >> 16) & 077777;
19798 @end example
19800 In the following example, derived from the GNU C Library 2.5
19801 implementation of @code{mktime} (2006-09-09), the code assumes
19802 wraparound arithmetic in @code{+} to detect signed overflow:
19804 @example
19805 time_t t, t1, t2;
19806 int sec_requested, sec_adjustment;
19807 @dots{}
19808 t1 = t + sec_requested;
19809 t2 = t1 + sec_adjustment;
19810 if (((t1 < t) != (sec_requested < 0))
19811     | ((t2 < t1) != (sec_adjustment < 0)))
19812   return -1;
19813 @end example
19815 If your code looks like these examples, it is probably safe even though
19816 it does not strictly conform to the C standard.  This might lead one to
19817 believe that one can generally assume wraparound on overflow, but that
19818 is not always true, as can be seen in the next section.
19820 @node Optimization and Wraparound
19821 @subsection Optimizations That Break Wraparound Arithmetic
19822 @cindex loop induction
19824 Compilers sometimes generate code that is incompatible with wraparound
19825 integer arithmetic.  A simple example is an algebraic simplification: a
19826 compiler might translate @code{(i * 2000) / 1000} to @code{i * 2}
19827 because it assumes that @code{i * 2000} does not overflow.  The
19828 translation is not equivalent to the original when overflow occurs:
19829 e.g., in the typical case of 32-bit signed two's complement wraparound
19830 @code{int}, if @code{i} has type @code{int} and value @code{1073742},
19831 the original expression returns @minus{}2147483 but the optimized
19832 version returns the mathematically correct value 2147484.
19834 More subtly, loop induction optimizations often exploit the undefined
19835 behavior of signed overflow.  Consider the following contrived function
19836 @code{sumc}:
19838 @example
19840 sumc (int lo, int hi)
19842   int sum = 0;
19843   int i;
19844   for (i = lo; i <= hi; i++)
19845     sum ^= i * 53;
19846   return sum;
19848 @end example
19850 @noindent
19851 To avoid multiplying by 53 each time through the loop, an optimizing
19852 compiler might internally transform @code{sumc} to the equivalent of the
19853 following:
19855 @example
19857 transformed_sumc (int lo, int hi)
19859   int sum = 0;
19860   int hic = hi * 53;
19861   int ic;
19862   for (ic = lo * 53; ic <= hic; ic += 53)
19863     sum ^= ic;
19864   return sum;
19866 @end example
19868 @noindent
19869 This transformation is allowed by the C standard, but it is invalid for
19870 wraparound arithmetic when @code{INT_MAX / 53 < hi}, because then the
19871 overflow in computing expressions like @code{hi * 53} can cause the
19872 expression @code{i <= hi} to yield a different value from the
19873 transformed expression @code{ic <= hic}.
19875 For this reason, compilers that use loop induction and similar
19876 techniques often do not support reliable wraparound arithmetic when a
19877 loop induction variable like @code{ic} is involved.  Since loop
19878 induction variables are generated by the compiler, and are not visible
19879 in the source code, it is not always trivial to say whether the problem
19880 affects your code.
19882 Hardly any code actually depends on wraparound arithmetic in cases like
19883 these, so in practice these loop induction optimizations are almost
19884 always useful.  However, edge cases in this area can cause problems.
19885 For example:
19887 @example
19888 int j;
19889 for (j = 1; 0 < j; j *= 2)
19890   test (j);
19891 @end example
19893 @noindent
19894 Here, the loop attempts to iterate through all powers of 2 that
19895 @code{int} can represent, but the C standard allows a compiler to
19896 optimize away the comparison and generate an infinite loop,
19897 under the argument that behavior is undefined on overflow.  As of this
19898 writing this optimization is not done by any production version of
19899 GCC with @option{-O2}, but it might be performed by other
19900 compilers, or by more aggressive GCC optimization options,
19901 and the GCC developers have not decided whether it will
19902 continue to work with GCC and @option{-O2}.
19904 @node Signed Overflow Advice
19905 @subsection Practical Advice for Signed Overflow Issues
19906 @cindex integer overflow
19907 @cindex overflow, signed integer
19908 @cindex signed integer overflow
19909 @cindex wraparound arithmetic
19911 Ideally the safest approach is to avoid signed integer overflow
19912 entirely.  For example, instead of multiplying two signed integers, you
19913 can convert them to unsigned integers, multiply the unsigned values,
19914 then test whether the result is in signed range.
19916 Rewriting code in this way will be inconvenient, though, particularly if
19917 the signed values might be negative.  Also, it may hurt
19918 performance.  Using unsigned arithmetic to check for overflow is
19919 particularly painful to do portably and efficiently when dealing with an
19920 integer type like @code{uid_t} whose width and signedness vary from
19921 platform to platform.
19923 Furthermore, many C applications pervasively assume wraparound behavior
19924 and typically it is not easy to find and remove all these assumptions.
19925 Hence it is often useful to maintain nonstandard code that assumes
19926 wraparound on overflow, instead of rewriting the code.  The rest of this
19927 section attempts to give practical advice for this situation.
19929 If your code wants to detect signed integer overflow in @code{sum = a +
19930 b}, it is generally safe to use an expression like @code{(sum < a) != (b
19931 < 0)}.
19933 If your code uses a signed loop index, make sure that the index cannot
19934 overflow, along with all signed expressions derived from the index.
19935 Here is a contrived example of problematic code with two instances of
19936 overflow.
19938 @example
19939 for (i = INT_MAX - 10; i <= INT_MAX; i++)
19940   if (i + 1 < 0)
19941     @{
19942       report_overflow ();
19943       break;
19944     @}
19945 @end example
19947 @noindent
19948 Because of the two overflows, a compiler might optimize away or
19949 transform the two comparisons in a way that is incompatible with the
19950 wraparound assumption.
19952 If your code uses an expression like @code{(i * 2000) / 1000} and you
19953 actually want the multiplication to wrap around on overflow, use
19954 unsigned arithmetic
19955 to do it, e.g., @code{((int) (i * 2000u)) / 1000}.
19957 If your code assumes wraparound behavior and you want to insulate it
19958 against any GCC optimizations that would fail to support that
19959 behavior, you should use GCC's @option{-fwrapv} option, which
19960 causes signed overflow to wrap around reliably (except for division and
19961 remainder, as discussed in the next section).
19963 If you need to port to platforms where signed integer overflow does not
19964 reliably wrap around (e.g., due to hardware overflow checking, or to
19965 highly aggressive optimizations), you should consider debugging with
19966 GCC's @option{-ftrapv} option, which causes signed overflow to
19967 raise an exception.
19969 @node Signed Integer Division
19970 @subsection Signed Integer Division and Integer Overflow
19971 @cindex division, integer
19973 Overflow in signed
19974 integer division is not always harmless: for example, on CPUs of the
19975 i386 family, dividing @code{INT_MIN} by @code{-1} yields a SIGFPE signal
19976 which by default terminates the program.  Worse, taking the remainder
19977 of these two values typically yields the same signal on these CPUs,
19978 even though the C standard requires @code{INT_MIN % -1} to yield zero
19979 because the expression does not overflow.
19981 @node Preprocessor Arithmetic
19982 @section Preprocessor Arithmetic
19983 @cindex preprocessor arithmetic
19985 In C99, preprocessor arithmetic, used for @code{#if} expressions, must
19986 be evaluated as if all signed values are of type @code{intmax_t} and all
19987 unsigned values of type @code{uintmax_t}.  Many compilers are buggy in
19988 this area, though.  For example, as of 2007, Sun C mishandles @code{#if
19989 LLONG_MIN < 0} on a platform with 32-bit @code{long int} and 64-bit
19990 @code{long long int}.  Also, some older preprocessors mishandle
19991 constants ending in @code{LL}.  To work around these problems, you can
19992 compute the value of expressions like @code{LONG_MAX < LLONG_MAX} at
19993 @code{configure}-time rather than at @code{#if}-time.
19995 @node Null Pointers
19996 @section Properties of Null Pointers
19997 @cindex null pointers
19999 Most modern hosts reliably fail when you attempt to dereference a null
20000 pointer.
20002 On almost all modern hosts, null pointers use an all-bits-zero internal
20003 representation, so you can reliably use @code{memset} with 0 to set all
20004 the pointers in an array to null values.
20006 If @code{p} is a null pointer to an object type, the C expression
20007 @code{p + 0} always evaluates to @code{p} on modern hosts, even though
20008 the standard says that it has undefined behavior.
20010 @node Buffer Overruns
20011 @section Buffer Overruns and Subscript Errors
20012 @cindex buffer overruns
20014 Buffer overruns and subscript errors are the most common dangerous
20015 errors in C programs.  They result in undefined behavior because storing
20016 outside an array typically modifies storage that is used by some other
20017 object, and most modern systems lack runtime checks to catch these
20018 errors.  Programs should not rely on buffer overruns being caught.
20020 There is one exception to the usual rule that a portable program cannot
20021 address outside an array.  In C, it is valid to compute the address just
20022 past an object, e.g., @code{&a[N]} where @code{a} has @code{N} elements,
20023 so long as you do not dereference the resulting pointer.  But it is not
20024 valid to compute the address just before an object, e.g., @code{&a[-1]};
20025 nor is it valid to compute two past the end, e.g., @code{&a[N+1]}.  On
20026 most platforms @code{&a[-1] < &a[0] && &a[N] < &a[N+1]}, but this is not
20027 reliable in general, and it is usually easy enough to avoid the
20028 potential portability problem, e.g., by allocating an extra unused array
20029 element at the start or end.
20031 @uref{http://@/valgrind.org/, Valgrind} can catch many overruns.
20033 users might also consider using the @option{-fmudflap} option to catch
20034 overruns.
20036 Buffer overruns are usually caused by off-by-one errors, but there are
20037 more subtle ways to get them.
20039 Using @code{int} values to index into an array or compute array sizes
20040 causes problems on typical 64-bit hosts where an array index might
20041 be @math{2^31} or larger.  Index values of type @code{size_t} avoid this
20042 problem, but cannot be negative.  Index values of type @code{ptrdiff_t}
20043 are signed, and are wide enough in practice.
20045 If you add or multiply two numbers to calculate an array size, e.g.,
20046 @code{malloc (x * sizeof y + z)}, havoc ensues if the addition or
20047 multiplication overflows.
20049 Many implementations of the @code{alloca} function silently misbehave
20050 and can generate buffer overflows if given sizes that are too large.
20051 The size limits are implementation dependent, but are at least 4000
20052 bytes on all platforms that we know about.
20054 The standard functions @code{asctime}, @code{asctime_r}, @code{ctime},
20055 @code{ctime_r}, and @code{gets} are prone to buffer overflows, and
20056 portable code should not use them unless the inputs are known to be
20057 within certain limits.  The time-related functions can overflow their
20058 buffers if given timestamps out of range (e.g., a year less than -999
20059 or greater than 9999).  Time-related buffer overflows cannot happen with
20060 recent-enough versions of the GNU C library, but are possible
20061 with other
20062 implementations.  The @code{gets} function is the worst, since it almost
20063 invariably overflows its buffer when presented with an input line larger
20064 than the buffer.
20066 @node Volatile Objects
20067 @section Volatile Objects
20068 @cindex volatile objects
20070 The keyword @code{volatile} is often misunderstood in portable code.
20071 Its use inhibits some memory-access optimizations, but programmers often
20072 wish that it had a different meaning than it actually does.
20074 @code{volatile} was designed for code that accesses special objects like
20075 memory-mapped device registers whose contents spontaneously change.
20076 Such code is inherently low-level, and it is difficult to specify
20077 portably what @code{volatile} means in these cases.  The C standard
20078 says, ``What constitutes an access to an object that has
20079 volatile-qualified type is implementation-defined,'' so in theory each
20080 implementation is supposed to fill in the gap by documenting what
20081 @code{volatile} means for that implementation.  In practice, though,
20082 this documentation is usually absent or incomplete.
20084 One area of confusion is the distinction between objects defined with
20085 volatile types, and volatile lvalues.  From the C standard's point of
20086 view, an object defined with a volatile type has externally visible
20087 behavior.  You can think of such objects as having little oscilloscope
20088 probes attached to them, so that the user can observe some properties of
20089 accesses to them, just as the user can observe data written to output
20090 files.  However, the standard does not make it clear whether users can
20091 observe accesses by volatile lvalues to ordinary objects.  For example:
20093 @example
20094 /* Declare and access a volatile object.
20095    Accesses to X are "visible" to users.  */
20096 static int volatile x;
20097 x = 1;
20099 /* Access two ordinary objects via a volatile lvalue.
20100    It's not clear whether accesses to *P are "visible".  */
20101 int y;
20102 int *z = malloc (sizeof (int));
20103 int volatile *p;
20104 p = &y;
20105 *p = 1;
20106 p = z;
20107 *p = 1;
20108 @end example
20110 Programmers often wish that @code{volatile} meant ``Perform the memory
20111 access here and now, without merging several memory accesses, without
20112 changing the memory word size, and without reordering.''  But the C
20113 standard does not require this.  For objects defined with a volatile
20114 type, accesses must be done before the next sequence point; but
20115 otherwise merging, reordering, and word-size change is allowed.  Worse,
20116 it is not clear from the standard whether volatile lvalues provide more
20117 guarantees in general than nonvolatile lvalues, if the underlying
20118 objects are ordinary.
20120 Even when accessing objects defined with a volatile type,
20121 the C standard allows only
20122 extremely limited signal handlers: the behavior is undefined if a signal
20123 handler reads any nonlocal object, or writes to any nonlocal object
20124 whose type is not @code{sig_atomic_t volatile}, or calls any standard
20125 library function other than @code{abort}, @code{signal}, and (if C99)
20126 @code{_Exit}.  Hence C compilers need not worry about a signal handler
20127 disturbing ordinary computation, unless the computation accesses a
20128 @code{sig_atomic_t volatile} lvalue that is not a local variable.
20129 (There is an obscure exception for accesses via a pointer to a volatile
20130 character, since it may point into part of a @code{sig_atomic_t
20131 volatile} object.)  Posix
20132 adds to the list of library functions callable from a portable signal
20133 handler, but otherwise is like the C standard in this area.
20135 Some C implementations allow memory-access optimizations within each
20136 translation unit, such that actual behavior agrees with the behavior
20137 required by the standard only when calling a function in some other
20138 translation unit, and a signal handler acts like it was called from a
20139 different translation unit.  The C standard hints that in these
20140 implementations, objects referred to by signal handlers ``would require
20141 explicit specification of @code{volatile} storage, as well as other
20142 implementation-defined restrictions.''  But unfortunately even for this
20143 special case these other restrictions are often not documented well.
20144 @xref{Volatiles, , When is a Volatile Object Accessed?, gcc, Using the
20145 GNU Compiler Collection (GCC)}, for some
20146 restrictions imposed by GCC.  @xref{Defining Handlers, ,
20147 Defining Signal Handlers, libc, The GNU C Library}, for some
20148 restrictions imposed by the GNU C library.  Restrictions
20149 differ on other platforms.
20151 If possible, it is best to use a signal handler that fits within the
20152 limits imposed by the C and Posix standards.
20154 If this is not practical, you can try the following rules of thumb.  A
20155 signal handler should access only volatile lvalues, preferably lvalues
20156 that refer to objects defined with a volatile type, and should not
20157 assume that the accessed objects have an internally consistent state
20158 if they are larger than a machine word.  Furthermore, installers
20159 should employ compilers and compiler options that are commonly used
20160 for building operating system kernels, because kernels often need more
20161 from @code{volatile} than the C Standard requires, and installers who
20162 compile an application in a similar environment can sometimes benefit
20163 from the extra constraints imposed by kernels on compilers.
20164 Admittedly we are handwaving somewhat here, as there are few
20165 guarantees in this area; the rules of thumb may help to fix some bugs
20166 but there is a good chance that they will not fix them all.
20168 For @code{volatile}, C++ has the same problems that C does.
20169 Multithreaded applications have even more problems with @code{volatile},
20170 but they are beyond the scope of this section.
20172 The bottom line is that using @code{volatile} typically hurts
20173 performance but should not hurt correctness.  In some cases its use
20174 does help correctness, but these cases are often so poorly understood
20175 that all too often adding @code{volatile} to a data structure merely
20176 alleviates some symptoms of a bug while not fixing the bug in general.
20178 @node Floating Point Portability
20179 @section Floating Point Portability
20180 @cindex floating point
20182 Almost all modern systems use IEEE-754 floating point, and it is safe to
20183 assume IEEE-754 in most portable code these days.  For more information,
20184 please see David Goldberg's classic paper
20185 @uref{http://@/www.validlab.com/@/goldberg/@/paper.pdf, What Every Computer
20186 Scientist Should Know About Floating-Point Arithmetic}.
20188 @node Exiting Portably
20189 @section Exiting Portably
20190 @cindex exiting portably
20192 A C or C++ program can exit with status @var{N} by returning
20193 @var{N} from the @code{main} function.  Portable programs are supposed
20194 to exit either with status 0 or @code{EXIT_SUCCESS} to succeed, or with
20195 status @code{EXIT_FAILURE} to fail, but in practice it is portable to
20196 fail by exiting with status 1, and test programs that assume Posix can
20197 fail by exiting with status values from 1 through 255.  Programs on
20198 SunOS 2.0 (1985) through 3.5.2 (1988) incorrectly exited with zero
20199 status when @code{main} returned nonzero, but ancient systems like these
20200 are no longer of practical concern.
20202 A program can also exit with status @var{N} by passing @var{N} to the
20203 @code{exit} function, and a program can fail by calling the @code{abort}
20204 function.  If a program is specialized to just some platforms, it can fail
20205 by calling functions specific to those platforms, e.g., @code{_exit}
20206 (Posix) and @code{_Exit} (C99).  However, like other functions, an exit
20207 function should be declared, typically by including a header.  For
20208 example, if a C program calls @code{exit}, it should include @file{stdlib.h}
20209 either directly or via the default includes (@pxref{Default Includes}).
20211 A program can fail due to undefined behavior such as dereferencing a null
20212 pointer, but this is not recommended as undefined behavior allows an
20213 implementation to do whatever it pleases and this includes exiting
20214 successfully.
20217 @c ================================================== Manual Configuration
20219 @node Manual Configuration
20220 @chapter Manual Configuration
20222 A few kinds of features can't be guessed automatically by running test
20223 programs.  For example, the details of the object-file format, or
20224 special options that need to be passed to the compiler or linker.  You
20225 can check for such features using ad-hoc means, such as having
20226 @command{configure} check the output of the @code{uname} program, or
20227 looking for libraries that are unique to particular systems.  However,
20228 Autoconf provides a uniform method for handling unguessable features.
20230 @menu
20231 * Specifying Target Triplets::  Specifying target triplets
20232 * Canonicalizing::              Getting the canonical system type
20233 * Using System Type::           What to do with the system type
20234 @end menu
20236 @node Specifying Target Triplets
20237 @section Specifying target triplets
20238 @cindex System type
20239 @cindex Target triplet
20240 @c This node used to be named Specifying Names.  The @anchor allows old
20241 @c links to still work.
20242 @anchor{Specifying Names}
20244 Autoconf-generated
20245 @command{configure} scripts can make decisions based on a canonical name
20246 for the system type, or @dfn{target triplet}, which has the form:
20247 @samp{@var{cpu}-@var{vendor}-@var{os}}, where @var{os} can be
20248 @samp{@var{system}} or @samp{@var{kernel}-@var{system}}
20250 @command{configure} can usually guess the canonical name for the type of
20251 system it's running on.  To do so it runs a script called
20252 @command{config.guess}, which infers the name using the @code{uname}
20253 command or symbols predefined by the C preprocessor.
20255 Alternately, the user can specify the system type with command line
20256 arguments to @command{configure} (@pxref{System Type}.  Doing so is
20257 necessary when
20258 cross-compiling.  In the most complex case of cross-compiling, three
20259 system types are involved.  The options to specify them are:
20261 @table @option
20262 @item --build=@var{build-type}
20263 the type of system on which the package is being configured and
20264 compiled.  It defaults to the result of running @command{config.guess}.
20266 @item --host=@var{host-type}
20267 the type of system on which the package runs.  By default it is the
20268 same as the build machine.  Specifying it enables the cross-compilation
20269 mode.
20271 @item --target=@var{target-type}
20272 the type of system for which any compiler tools in the package
20273 produce code (rarely needed).  By default, it is the same as host.
20274 @end table
20276 If you mean to override the result of @command{config.guess}, use
20277 @option{--build}, not @option{--host}, since the latter enables
20278 cross-compilation.  For historical reasons,
20279 whenever you specify @option{--host},
20280 be sure to specify @option{--build} too; this will be fixed in the
20281 future.  So, to enter cross-compilation mode, use a command like this
20283 @example
20284 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
20285 @end example
20287 @noindent
20288 Note that if you do not specify @option{--host}, @command{configure}
20289 fails if it can't run the code generated by the specified compiler.  For
20290 example, configuring as follows fails:
20292 @example
20293 ./configure CC=m68k-coff-gcc
20294 @end example
20296 When cross-compiling, @command{configure} will warn about any tools
20297 (compilers, linkers, assemblers) whose name is not prefixed with the
20298 host type.  This is an aid to users performing cross-compilation.
20299 Continuing the example above, if a cross-compiler named @command{cc} is
20300 used with a native @command{pkg-config}, then libraries found by
20301 @command{pkg-config} will likely cause subtle build failures; but using
20302 the names @command{m68k-coff-cc} and @command{m68k-coff-pkg-config}
20303 avoids any confusion.  Avoiding the warning is as simple as creating the
20304 correct symlinks naming the cross tools.
20306 @cindex @command{config.sub}
20307 @command{configure} recognizes short aliases for many system types; for
20308 example, @samp{decstation} can be used instead of
20309 @samp{mips-dec-ultrix4.2}.  @command{configure} runs a script called
20310 @command{config.sub} to canonicalize system type aliases.
20312 This section deliberately omits the description of the obsolete
20313 interface; see @ref{Hosts and Cross-Compilation}.
20316 @node Canonicalizing
20317 @section Getting the Canonical System Type
20318 @cindex System type
20319 @cindex Canonical system type
20321 The following macros make the system type available to @command{configure}
20322 scripts.
20324 @ovindex build_alias
20325 @ovindex host_alias
20326 @ovindex target_alias
20328 The variables @samp{build_alias}, @samp{host_alias}, and
20329 @samp{target_alias} are always exactly the arguments of @option{--build},
20330 @option{--host}, and @option{--target}; in particular, they are left empty
20331 if the user did not use them, even if the corresponding
20332 @code{AC_CANONICAL} macro was run.  Any configure script may use these
20333 variables anywhere.  These are the variables that should be used when in
20334 interaction with the user.
20336 If you need to recognize some special environments based on their system
20337 type, run the following macros to get canonical system names.  These
20338 variables are not set before the macro call.
20340 If you use these macros, you must distribute @command{config.guess} and
20341 @command{config.sub} along with your source code.  @xref{Output}, for
20342 information about the @code{AC_CONFIG_AUX_DIR} macro which you can use
20343 to control in which directory @command{configure} looks for those scripts.
20346 @defmac AC_CANONICAL_BUILD
20347 @acindex{CANONICAL_BUILD}
20348 @ovindex build
20349 @ovindex build_cpu
20350 @ovindex build_vendor
20351 @ovindex build_os
20352 Compute the canonical build-system type variable, @code{build}, and its
20353 three individual parts @code{build_cpu}, @code{build_vendor}, and
20354 @code{build_os}.
20356 If @option{--build} was specified, then @code{build} is the
20357 canonicalization of @code{build_alias} by @command{config.sub},
20358 otherwise it is determined by the shell script @command{config.guess}.
20359 @end defmac
20361 @defmac AC_CANONICAL_HOST
20362 @acindex{CANONICAL_HOST}
20363 @ovindex host
20364 @ovindex host_cpu
20365 @ovindex host_vendor
20366 @ovindex host_os
20367 Compute the canonical host-system type variable, @code{host}, and its
20368 three individual parts @code{host_cpu}, @code{host_vendor}, and
20369 @code{host_os}.
20371 If @option{--host} was specified, then @code{host} is the
20372 canonicalization of @code{host_alias} by @command{config.sub},
20373 otherwise it defaults to @code{build}.
20374 @end defmac
20376 @defmac AC_CANONICAL_TARGET
20377 @acindex{CANONICAL_TARGET}
20378 @ovindex target
20379 @ovindex target_cpu
20380 @ovindex target_vendor
20381 @ovindex target_os
20382 Compute the canonical target-system type variable, @code{target}, and its
20383 three individual parts @code{target_cpu}, @code{target_vendor}, and
20384 @code{target_os}.
20386 If @option{--target} was specified, then @code{target} is the
20387 canonicalization of @code{target_alias} by @command{config.sub},
20388 otherwise it defaults to @code{host}.
20389 @end defmac
20391 Note that there can be artifacts due to the backward compatibility
20392 code.  See @xref{Hosts and Cross-Compilation}, for more.
20394 @node Using System Type
20395 @section Using the System Type
20397 In @file{configure.ac} the system type is generally used by one or more
20398 @code{case} statements to select system-specifics.  Shell wildcards can
20399 be used to match a group of system types.
20401 For example, an extra assembler code object file could be chosen, giving
20402 access to a CPU cycle counter register.  @code{$(CYCLE_OBJ)} in the
20403 following would be used in a makefile to add the object to a
20404 program or library.
20406 @example
20407 AS_CASE([$host],
20408   [alpha*-*-*], [CYCLE_OBJ=rpcc.o],
20409   [i?86-*-*],   [CYCLE_OBJ=rdtsc.o],
20410   [CYCLE_OBJ=""]
20412 AC_SUBST([CYCLE_OBJ])
20413 @end example
20415 @code{AC_CONFIG_LINKS} (@pxref{Configuration Links}) is another good way
20416 to select variant source files, for example optimized code for some
20417 CPUs.  The configured CPU type doesn't always indicate exact CPU types,
20418 so some runtime capability checks may be necessary too.
20420 @example
20421 case $host in
20422   alpha*-*-*)   AC_CONFIG_LINKS([dither.c:alpha/dither.c]) ;;
20423   powerpc*-*-*) AC_CONFIG_LINKS([dither.c:powerpc/dither.c]) ;;
20424   *-*-*)        AC_CONFIG_LINKS([dither.c:generic/dither.c]) ;;
20425 esac
20426 @end example
20428 The host system type can also be used to find cross-compilation tools
20429 with @code{AC_CHECK_TOOL} (@pxref{Generic Programs}).
20431 The above examples all show @samp{$host}, since this is where the code
20432 is going to run.  Only rarely is it necessary to test @samp{$build}
20433 (which is where the build is being done).
20435 Whenever you're tempted to use @samp{$host} it's worth considering
20436 whether some sort of probe would be better.  New system types come along
20437 periodically or previously missing features are added.  Well-written
20438 probes can adapt themselves to such things, but hard-coded lists of
20439 names can't.  Here are some guidelines,
20441 @itemize @bullet
20442 @item
20443 Availability of libraries and library functions should always be checked
20444 by probing.
20445 @item
20446 Variant behavior of system calls is best identified with runtime tests
20447 if possible, but bug workarounds or obscure difficulties might have to
20448 be driven from @samp{$host}.
20449 @item
20450 Assembler code is inevitably highly CPU-specific and is best selected
20451 according to @samp{$host_cpu}.
20452 @item
20453 Assembler variations like underscore prefix on globals or ELF versus
20454 COFF type directives are however best determined by probing, perhaps
20455 even examining the compiler output.
20456 @end itemize
20458 @samp{$target} is for use by a package creating a compiler or similar.
20459 For ordinary packages it's meaningless and should not be used.  It
20460 indicates what the created compiler should generate code for, if it can
20461 cross-compile.  @samp{$target} generally selects various hard-coded CPU
20462 and system conventions, since usually the compiler or tools under
20463 construction themselves determine how the target works.
20466 @c ===================================================== Site Configuration.
20468 @node Site Configuration
20469 @chapter Site Configuration
20471 @command{configure} scripts support several kinds of local configuration
20472 decisions.  There are ways for users to specify where external software
20473 packages are, include or exclude optional features, install programs
20474 under modified names, and set default values for @command{configure}
20475 options.
20477 @menu
20478 * Help Formatting::             Customizing @samp{configure --help}
20479 * External Software::           Working with other optional software
20480 * Package Options::             Selecting optional features
20481 * Pretty Help Strings::         Formatting help string
20482 * Option Checking::             Controlling checking of @command{configure} options
20483 * Site Details::                Configuring site details
20484 * Transforming Names::          Changing program names when installing
20485 * Site Defaults::               Giving @command{configure} local defaults
20486 @end menu
20488 @node Help Formatting
20489 @section Controlling Help Output
20491 Users consult @samp{configure --help} to learn of configuration
20492 decisions specific to your package.  By default, @command{configure}
20493 breaks this output into sections for each type of option; within each
20494 section, help strings appear in the order @file{configure.ac} defines
20495 them:
20497 @example
20498 Optional Features:
20499   @dots{}
20500   --enable-bar            include bar
20502 Optional Packages:
20503   @dots{}
20504   --with-foo              use foo
20505 @end example
20507 @defmac AC_PRESERVE_HELP_ORDER
20508 @acindex{PRESERVE_HELP_ORDER}
20510 Request an alternate @option{--help} format, in which options of all
20511 types appear together, in the order defined.  Call this macro before any
20512 @code{AC_ARG_ENABLE} or @code{AC_ARG_WITH}.
20514 @example
20515 Optional Features and Packages:
20516   @dots{}
20517   --enable-bar            include bar
20518   --with-foo              use foo
20519 @end example
20521 @end defmac
20523 @node External Software
20524 @section Working With External Software
20525 @cindex External software
20527 Some packages require, or can optionally use, other software packages
20528 that are already installed.  The user can give @command{configure}
20529 command line options to specify which such external software to use.
20530 The options have one of these forms:
20532 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
20533 @c awful.
20534 @example
20535 --with-@var{package}@r{[}=@var{arg}@r{]}
20536 --without-@var{package}
20537 @end example
20539 For example, @option{--with-gnu-ld} means work with the GNU linker
20540 instead of some other linker.  @option{--with-x} means work with The X
20541 Window System.
20543 The user can give an argument by following the package name with
20544 @samp{=} and the argument.  Giving an argument of @samp{no} is for
20545 packages that are used by default; it says to @emph{not} use the
20546 package.  An argument that is neither @samp{yes} nor @samp{no} could
20547 include a name or number of a version of the other package, to specify
20548 more precisely which other package this program is supposed to work
20549 with.  If no argument is given, it defaults to @samp{yes}.
20550 @option{--without-@var{package}} is equivalent to
20551 @option{--with-@var{package}=no}.
20553 Normally @command{configure} scripts complain about
20554 @option{--with-@var{package}} options that they do not support.
20555 @xref{Option Checking}, for details, and for how to override the
20556 defaults.
20558 For each external software package that may be used, @file{configure.ac}
20559 should call @code{AC_ARG_WITH} to detect whether the @command{configure}
20560 user asked to use it.  Whether each package is used or not by default,
20561 and which arguments are valid, is up to you.
20563 @anchor{AC_ARG_WITH}
20564 @defmac AC_ARG_WITH (@var{package}, @var{help-string}, @
20565   @ovar{action-if-given}, @ovar{action-if-not-given})
20566 @acindex{ARG_WITH}
20567 If the user gave @command{configure} the option @option{--with-@var{package}}
20568 or @option{--without-@var{package}}, run shell commands
20569 @var{action-if-given}.  If neither option was given, run shell commands
20570 @var{action-if-not-given}.  The name @var{package} indicates another
20571 software package that this program should work with.  It should consist
20572 only of alphanumeric characters, dashes, plus signs, and dots.
20574 The option's argument is available to the shell commands
20575 @var{action-if-given} in the shell variable @code{withval}, which is
20576 actually just the value of the shell variable named
20577 @code{with_@var{package}}, with any non-alphanumeric characters in
20578 @var{package} changed into @samp{_}.  You may use that variable instead,
20579 if you wish.
20581 The argument @var{help-string} is a description of the option that
20582 looks like this:
20583 @example
20584   --with-readline         support fancy command line editing
20585 @end example
20587 @noindent
20588 @var{help-string} may be more than one line long, if more detail is
20589 needed.  Just make sure the columns line up in @samp{configure
20590 --help}.  Avoid tabs in the help string.  The easiest way to provide the
20591 proper leading whitespace is to format your @var{help-string} with the macro
20592 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
20594 The following example shows how to use the @code{AC_ARG_WITH} macro in
20595 a common situation.  You want to let the user decide whether to enable
20596 support for an external library (e.g., the readline library); if the user
20597 specified neither @option{--with-readline} nor @option{--without-readline},
20598 you want to enable support for readline only if the library is available
20599 on the system.
20601 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
20602 @example
20603 AC_ARG_WITH([readline],
20604   [AS_HELP_STRING([--with-readline],
20605     [support fancy command line editing @@<:@@default=check@@:>@@])],
20606   [],
20607   [with_readline=check])
20609 LIBREADLINE=
20610 AS_IF([test "x$with_readline" != xno],
20611   [AC_CHECK_LIB([readline], [main],
20612     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
20613      AC_DEFINE([HAVE_LIBREADLINE], [1],
20614                [Define if you have libreadline])
20615     ],
20616     [if test "x$with_readline" != xcheck; then
20617        AC_MSG_FAILURE(
20618          [--with-readline was given, but test for readline failed])
20619      fi
20620     ], -lncurses)])
20621 @end example
20623 The next example shows how to use @code{AC_ARG_WITH} to give the user the
20624 possibility to enable support for the readline library, in case it is still
20625 experimental and not well tested, and is therefore disabled by default.
20627 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
20628 @example
20629 AC_ARG_WITH([readline],
20630   [AS_HELP_STRING([--with-readline],
20631     [enable experimental support for readline])],
20632   [],
20633   [with_readline=no])
20635 LIBREADLINE=
20636 AS_IF([test "x$with_readline" != xno],
20637   [AC_CHECK_LIB([readline], [main],
20638     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
20639      AC_DEFINE([HAVE_LIBREADLINE], [1],
20640                [Define if you have libreadline])
20641     ],
20642     [AC_MSG_FAILURE(
20643        [--with-readline was given, but test for readline failed])],
20644     [-lncurses])])
20645 @end example
20647 The last example shows how to use @code{AC_ARG_WITH} to give the user the
20648 possibility to disable support for the readline library, given that it is
20649 an important feature and that it should be enabled by default.
20651 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
20652 @example
20653 AC_ARG_WITH([readline],
20654   [AS_HELP_STRING([--without-readline],
20655     [disable support for readline])],
20656   [],
20657   [with_readline=yes])
20659 LIBREADLINE=
20660 AS_IF([test "x$with_readline" != xno],
20661   [AC_CHECK_LIB([readline], [main],
20662     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
20663      AC_DEFINE([HAVE_LIBREADLINE], [1],
20664                [Define if you have libreadline])
20665     ],
20666     [AC_MSG_FAILURE(
20667        [readline test failed (--without-readline to disable)])],
20668     [-lncurses])])
20669 @end example
20671 These three examples can be easily adapted to the case where
20672 @code{AC_ARG_ENABLE} should be preferred to @code{AC_ARG_WITH} (see
20673 @ref{Package Options}).
20674 @end defmac
20676 @node Package Options
20677 @section Choosing Package Options
20678 @cindex Package options
20679 @cindex Options, package
20681 If a software package has optional compile-time features, the user can
20682 give @command{configure} command line options to specify whether to
20683 compile them.  The options have one of these forms:
20685 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
20686 @c awful.
20687 @example
20688 --enable-@var{feature}@r{[}=@var{arg}@r{]}
20689 --disable-@var{feature}
20690 @end example
20692 These options allow users to choose which optional features to build and
20693 install.  @option{--enable-@var{feature}} options should never make a
20694 feature behave differently or cause one feature to replace another.
20695 They should only cause parts of the program to be built rather than left
20696 out.
20698 The user can give an argument by following the feature name with
20699 @samp{=} and the argument.  Giving an argument of @samp{no} requests
20700 that the feature @emph{not} be made available.  A feature with an
20701 argument looks like @option{--enable-debug=stabs}.  If no argument is
20702 given, it defaults to @samp{yes}.  @option{--disable-@var{feature}} is
20703 equivalent to @option{--enable-@var{feature}=no}.
20705 Normally @command{configure} scripts complain about
20706 @option{--enable-@var{package}} options that they do not support.
20707 @xref{Option Checking}, for details, and for how to override the
20708 defaults.
20710 For each optional feature, @file{configure.ac} should call
20711 @code{AC_ARG_ENABLE} to detect whether the @command{configure} user asked
20712 to include it.  Whether each feature is included or not by default, and
20713 which arguments are valid, is up to you.
20715 @anchor{AC_ARG_ENABLE}
20716 @defmac AC_ARG_ENABLE (@var{feature}, @var{help-string}, @
20717   @ovar{action-if-given}, @ovar{action-if-not-given})
20718 @acindex{ARG_ENABLE}
20719 If the user gave @command{configure} the option
20720 @option{--enable-@var{feature}} or @option{--disable-@var{feature}}, run
20721 shell commands @var{action-if-given}.  If neither option was given, run
20722 shell commands @var{action-if-not-given}.  The name @var{feature}
20723 indicates an optional user-level facility.  It should consist only of
20724 alphanumeric characters, dashes, plus signs, and dots.
20726 The option's argument is available to the shell commands
20727 @var{action-if-given} in the shell variable @code{enableval}, which is
20728 actually just the value of the shell variable named
20729 @code{enable_@var{feature}}, with any non-alphanumeric characters in
20730 @var{feature} changed into @samp{_}.  You may use that variable instead,
20731 if you wish.  The @var{help-string} argument is like that of
20732 @code{AC_ARG_WITH} (@pxref{External Software}).
20734 You should format your @var{help-string} with the macro
20735 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
20737 See the examples suggested with the definition of @code{AC_ARG_WITH}
20738 (@pxref{External Software}) to get an idea of possible applications of
20739 @code{AC_ARG_ENABLE}.
20740 @end defmac
20742 @node Pretty Help Strings
20743 @section Making Your Help Strings Look Pretty
20744 @cindex Help strings
20746 Properly formatting the @samp{help strings} which are used in
20747 @code{AC_ARG_WITH} (@pxref{External Software}) and @code{AC_ARG_ENABLE}
20748 (@pxref{Package Options}) can be challenging.  Specifically, you want
20749 your own @samp{help strings} to line up in the appropriate columns of
20750 @samp{configure --help} just like the standard Autoconf @samp{help
20751 strings} do.  This is the purpose of the @code{AS_HELP_STRING} macro.
20753 @anchor{AS_HELP_STRING}
20754 @defmac AS_HELP_STRING (@var{left-hand-side}, @var{right-hand-side} @
20755   @dvar{indent-column, 26}, @dvar{wrap-column, 79})
20756 @asindex{HELP_STRING}
20758 Expands into a help string that looks pretty when the user executes
20759 @samp{configure --help}.  It is typically used in @code{AC_ARG_WITH}
20760 (@pxref{External Software}) or @code{AC_ARG_ENABLE} (@pxref{Package
20761 Options}).  The following example makes this clearer.
20763 @example
20764 AC_ARG_WITH([foo],
20765   [AS_HELP_STRING([--with-foo],
20766      [use foo (default is no)])],
20767   [use_foo=$withval],
20768   [use_foo=no])
20769 @end example
20771 Then the last few lines of @samp{configure --help} appear like
20772 this:
20774 @example
20775 --enable and --with options recognized:
20776   --with-foo              use foo (default is no)
20777 @end example
20779 Macro expansion is performed on the first argument.  However, the second
20780 argument of @code{AS_HELP_STRING} is treated as a whitespace separated
20781 list of text to be reformatted, and is not subject to macro expansion.
20782 Since it is not expanded, it should not be double quoted.
20783 @xref{Autoconf Language}, for a more detailed explanation.
20785 The @code{AS_HELP_STRING} macro is particularly helpful when the
20786 @var{left-hand-side} and/or @var{right-hand-side} are composed of macro
20787 arguments, as shown in the following example.  Be aware that
20788 @var{left-hand-side} may not expand to unbalanced quotes,
20789 although quadrigraphs can be used.
20791 @example
20792 AC_DEFUN([MY_ARG_WITH],
20793   [AC_ARG_WITH(m4_translit([[$1]], [_], [-]),
20794      [AS_HELP_STRING([--with-m4_translit([$1], [_], [-])],
20795                      [use $1 (default is $2)])],
20796      [use_[]$1=$withval],
20797      [use_[]$1=$2])])
20798 MY_ARG_WITH([a_b], [no])
20799 @end example
20800 @noindent
20801 Here, the last few lines of @samp{configure --help} will include:
20803 @example
20804 --enable and --with options recognized:
20805   --with-a-b              use a_b (default is no)
20806 @end example
20808 The parameters @var{indent-column} and @var{wrap-column} were introduced
20809 in Autoconf 2.62.  Generally, they should not be specified; they exist
20810 for fine-tuning of the wrapping.
20811 @example
20812 AS_HELP_STRING([--option], [description of option])
20813 @result{}  --option                description of option
20814 AS_HELP_STRING([--option], [description of option], [15], [30])
20815 @result{}  --option     description of
20816 @result{}               option
20817 @end example
20818 @end defmac
20821 @node Option Checking
20822 @section Controlling Checking of @command{configure} Options
20823 @cindex Options, Package
20825 The @command{configure} script checks its command-line options against a
20826 list of known options, like @option{--help} or @option{--config-cache}.
20827 An unknown option ordinarily indicates a mistake by the user and
20828 @command{configure} halts with an error.  However, by default unknown
20829 @option{--with-@var{package}} and @option{--enable-@var{feature}}
20830 options elicit only a warning, to support configuring entire source
20831 trees.
20833 Source trees often contain multiple packages with a top-level
20834 @command{configure} script that uses the @code{AC_CONFIG_SUBDIRS} macro
20835 (@pxref{Subdirectories}).  Because the packages generally support
20836 different @option{--with-@var{package}} and
20837 @option{--enable-@var{feature}} options, the GNU Coding
20838 Standards say they must accept unrecognized options without halting.
20839 Even a warning message is undesirable here, so @code{AC_CONFIG_SUBDIRS}
20840 automatically disables the warnings.
20842 This default behavior may be modified in two ways.  First, the installer
20843 can invoke @code{configure --disable-option-checking} to disable
20844 these warnings, or invoke @code{configure --enable-option-checking=fatal}
20845 options to turn them into fatal errors, respectively.  Second, the
20846 maintainer can use @code{AC_DISABLE_OPTION_CHECKING}.
20848 @defmac AC_DISABLE_OPTION_CHECKING
20849 @acindex{DISABLE_OPTION_CHECKING}
20851 By default, disable warnings related to any unrecognized
20852 @option{--with-@var{package}} or @option{--enable-@var{feature}}
20853 options.  This is implied by @code{AC_CONFIG_SUBDIRS}.
20855 The installer can override this behavior by passing
20856 @option{--enable-option-checking} (enable warnings) or
20857 @option{--enable-option-checking=fatal} (enable errors) to
20858 @command{configure}.
20859 @end defmac
20862 @node Site Details
20863 @section Configuring Site Details
20864 @cindex Site details
20866 Some software packages require complex site-specific information.  Some
20867 examples are host names to use for certain services, company names, and
20868 email addresses to contact.  Since some configuration scripts generated
20869 by Metaconfig ask for such information interactively, people sometimes
20870 wonder how to get that information in Autoconf-generated configuration
20871 scripts, which aren't interactive.
20873 Such site configuration information should be put in a file that is
20874 edited @emph{only by users}, not by programs.  The location of the file
20875 can either be based on the @code{prefix} variable, or be a standard
20876 location such as the user's home directory.  It could even be specified
20877 by an environment variable.  The programs should examine that file at
20878 runtime, rather than at compile time.  Runtime configuration is more
20879 convenient for users and makes the configuration process simpler than
20880 getting the information while configuring.  @xref{Directory Variables, ,
20881 Variables for Installation Directories, standards, GNU Coding
20882 Standards}, for more information on where to put data files.
20884 @node Transforming Names
20885 @section Transforming Program Names When Installing
20886 @cindex Transforming program names
20887 @cindex Program names, transforming
20889 Autoconf supports changing the names of programs when installing them.
20890 In order to use these transformations, @file{configure.ac} must call the
20891 macro @code{AC_ARG_PROGRAM}.
20893 @defmac AC_ARG_PROGRAM
20894 @acindex{ARG_PROGRAM}
20895 @ovindex program_transform_name
20896 Place in output variable @code{program_transform_name} a sequence of
20897 @code{sed} commands for changing the names of installed programs.
20899 If any of the options described below are given to @command{configure},
20900 program names are transformed accordingly.  Otherwise, if
20901 @code{AC_CANONICAL_TARGET} has been called and a @option{--target} value
20902 is given, the target type followed by a dash is used as a prefix.
20903 Otherwise, no program name transformation is done.
20904 @end defmac
20906 @menu
20907 * Transformation Options::      @command{configure} options to transform names
20908 * Transformation Examples::     Sample uses of transforming names
20909 * Transformation Rules::        Makefile uses of transforming names
20910 @end menu
20912 @node Transformation Options
20913 @subsection Transformation Options
20915 You can specify name transformations by giving @command{configure} these
20916 command line options:
20918 @table @option
20919 @item --program-prefix=@var{prefix}
20920 prepend @var{prefix} to the names;
20922 @item --program-suffix=@var{suffix}
20923 append @var{suffix} to the names;
20925 @item --program-transform-name=@var{expression}
20926 perform @code{sed} substitution @var{expression} on the names.
20927 @end table
20929 @node Transformation Examples
20930 @subsection Transformation Examples
20932 These transformations are useful with programs that can be part of a
20933 cross-compilation development environment.  For example, a
20934 cross-assembler running on a Sun 4 configured with
20935 @option{--target=i960-vxworks} is normally installed as
20936 @file{i960-vxworks-as}, rather than @file{as}, which could be confused
20937 with a native Sun 4 assembler.
20939 You can force a program name to begin with @file{g}, if you don't want
20940 GNU programs installed on your system to shadow other programs with
20941 the same name.  For example, if you configure GNU @code{diff} with
20942 @option{--program-prefix=g}, then when you run @samp{make install} it is
20943 installed as @file{/usr/local/bin/gdiff}.
20945 As a more sophisticated example, you could use
20947 @example
20948 --program-transform-name='s/^/g/; s/^gg/g/; s/^gless/less/'
20949 @end example
20950 @noindent
20952 to prepend @samp{g} to most of the program names in a source tree,
20953 excepting those like @code{gdb} that already have one and those like
20954 @code{less} and @code{lesskey} that aren't GNU programs.  (That is
20955 assuming that you have a source tree containing those programs that is
20956 set up to use this feature.)
20958 One way to install multiple versions of some programs simultaneously is
20959 to append a version number to the name of one or both.  For example, if
20960 you want to keep Autoconf version 1 around for awhile, you can configure
20961 Autoconf version 2 using @option{--program-suffix=2} to install the
20962 programs as @file{/usr/local/bin/autoconf2},
20963 @file{/usr/local/bin/autoheader2}, etc.  Nevertheless, pay attention
20964 that only the binaries are renamed, therefore you'd have problems with
20965 the library files which might overlap.
20967 @node Transformation Rules
20968 @subsection Transformation Rules
20970 Here is how to use the variable @code{program_transform_name} in a
20971 @file{Makefile.in}:
20973 @example
20974 PROGRAMS = cp ls rm
20975 transform = @@program_transform_name@@
20976 install:
20977         for p in $(PROGRAMS); do \
20978           $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p | \
20979                                               sed '$(transform)'`; \
20980         done
20982 uninstall:
20983         for p in $(PROGRAMS); do \
20984           rm -f $(DESTDIR)$(bindir)/`echo $$p | sed '$(transform)'`; \
20985 @c $$ restore font-lock
20986         done
20987 @end example
20989 It is guaranteed that @code{program_transform_name} is never empty, and
20990 that there are no useless separators.  Therefore you may safely embed
20991 @code{program_transform_name} within a sed program using @samp{;}:
20993 @example
20994 transform = @@program_transform_name@@
20995 transform_exe = s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/
20996 @end example
20998 Whether to do the transformations on documentation files (Texinfo or
20999 @code{man}) is a tricky question; there seems to be no perfect answer,
21000 due to the several reasons for name transforming.  Documentation is not
21001 usually particular to a specific architecture, and Texinfo files do not
21002 conflict with system documentation.  But they might conflict with
21003 earlier versions of the same files, and @code{man} pages sometimes do
21004 conflict with system documentation.  As a compromise, it is probably
21005 best to do name transformations on @code{man} pages but not on Texinfo
21006 manuals.
21008 @node Site Defaults
21009 @section Setting Site Defaults
21010 @cindex Site defaults
21011 @cindex config.site
21013 Autoconf-generated @command{configure} scripts allow your site to provide
21014 default values for some configuration values.  You do this by creating
21015 site- and system-wide initialization files.
21017 @evindex CONFIG_SITE
21018 If the environment variable @code{CONFIG_SITE} is set, @command{configure}
21019 uses its value as the name of a shell script to read; it is recommended
21020 that this be an absolute file name.  Otherwise, it
21021 reads the shell script @file{@var{prefix}/share/config.site} if it exists,
21022 then @file{@var{prefix}/etc/config.site} if it exists.  Thus,
21023 settings in machine-specific files override those in machine-independent
21024 ones in case of conflict.
21026 Site files can be arbitrary shell scripts, but only certain kinds of
21027 code are really appropriate to be in them.  Because @command{configure}
21028 reads any cache file after it has read any site files, a site file can
21029 define a default cache file to be shared between all Autoconf-generated
21030 @command{configure} scripts run on that system (@pxref{Cache Files}).  If
21031 you set a default cache file in a site file, it is a good idea to also
21032 set the output variable @code{CC} in that site file, because the cache
21033 file is only valid for a particular compiler, but many systems have
21034 several available.
21036 You can examine or override the value set by a command line option to
21037 @command{configure} in a site file; options set shell variables that have
21038 the same names as the options, with any dashes turned into underscores.
21039 The exceptions are that @option{--without-} and @option{--disable-} options
21040 are like giving the corresponding @option{--with-} or @option{--enable-}
21041 option and the value @samp{no}.  Thus, @option{--cache-file=localcache}
21042 sets the variable @code{cache_file} to the value @samp{localcache};
21043 @option{--enable-warnings=no} or @option{--disable-warnings} sets the variable
21044 @code{enable_warnings} to the value @samp{no}; @option{--prefix=/usr} sets the
21045 variable @code{prefix} to the value @samp{/usr}; etc.
21047 Site files are also good places to set default values for other output
21048 variables, such as @code{CFLAGS}, if you need to give them non-default
21049 values: anything you would normally do, repetitively, on the command
21050 line.  If you use non-default values for @var{prefix} or
21051 @var{exec_prefix} (wherever you locate the site file), you can set them
21052 in the site file if you specify it with the @code{CONFIG_SITE}
21053 environment variable.
21055 You can set some cache values in the site file itself.  Doing this is
21056 useful if you are cross-compiling, where it is impossible to check features
21057 that require running a test program.  You could ``prime the cache'' by
21058 setting those values correctly for that system in
21059 @file{@var{prefix}/etc/config.site}.  To find out the names of the cache
21060 variables you need to set, see the documentation of the respective
21061 Autoconf macro.  If the variables or their semantics are undocumented,
21062 you may need to look for shell variables with @samp{_cv_} in their names
21063 in the affected @command{configure} scripts, or in the Autoconf M4
21064 source code for those macros; but in that case, their name or semantics
21065 may change in a future Autoconf version.
21067 The cache file is careful to not override any variables set in the site
21068 files.  Similarly, you should not override command-line options in the
21069 site files.  Your code should check that variables such as @code{prefix}
21070 and @code{cache_file} have their default values (as set near the top of
21071 @command{configure}) before changing them.
21073 Here is a sample file @file{/usr/share/local/@/gnu/share/@/config.site}.  The
21074 command @samp{configure --prefix=/usr/share/local/gnu} would read this
21075 file (if @code{CONFIG_SITE} is not set to a different file).
21077 @example
21078 # /usr/share/local/gnu/share/config.site for configure
21080 # Change some defaults.
21081 test "$prefix" = NONE && prefix=/usr/share/local/gnu
21082 test "$exec_prefix" = NONE && exec_prefix=/usr/local/gnu
21083 test "$sharedstatedir" = '$@{prefix@}/com' && sharedstatedir=/var
21084 test "$localstatedir" = '$@{prefix@}/var' && localstatedir=/var
21086 # Give Autoconf 2.x generated configure scripts a shared default
21087 # cache file for feature test results, architecture-specific.
21088 if test "$cache_file" = /dev/null; then
21089   cache_file="$prefix/var/config.cache"
21090   # A cache file is only valid for one C compiler.
21091   CC=gcc
21093 @end example
21095 @cindex Filesystem Hierarchy Standard
21096 @cindex FHS
21098 Another use of @file{config.site} is for priming the directory variables
21099 in a manner consistent with the Filesystem Hierarchy Standard
21100 (FHS).  Once the following file is installed at
21101 @file{/usr/share/config.site}, a user can execute simply
21102 @code{./configure --prefix=/usr} to get all the directories chosen in
21103 the locations recommended by FHS.
21105 @example
21106 # /usr/share/config.site for FHS defaults when installing below /usr,
21107 # and the respective settings were not changed on the command line.
21108 if test "$prefix" = /usr; then
21109   test "$sysconfdir" = '$@{prefix@}/etc' && sysconfdir=/etc
21110   test "$sharedstatedir" = '$@{prefix@}/com' && sharedstatedir=/var
21111   test "$localstatedir" = '$@{prefix@}/var' && localstatedir=/var
21113 @end example
21115 @cindex lib64
21116 @cindex 64-bit libraries
21117 Likewise, on platforms where 64-bit libraries are built by default, then
21118 installed in @file{/usr/local/@/lib64} instead of @file{/usr/local/@/lib},
21119 it is appropriate to install @file{/usr/local/@/share/config.site}:
21121 @example
21122 # /usr/local/share/config.site for platforms that prefer
21123 # the directory /usr/local/lib64 over /usr/local/lib.
21124 test "$libdir" = '$@{exec_prefix@}/lib' && libdir='$@{exec_prefix@}/lib64'
21125 @end example
21128 @c ============================================== Running configure Scripts.
21130 @node Running configure Scripts
21131 @chapter Running @command{configure} Scripts
21132 @cindex @command{configure}
21134 Below are instructions on how to configure a package that uses a
21135 @command{configure} script, suitable for inclusion as an @file{INSTALL}
21136 file in the package.  A plain-text version of @file{INSTALL} which you
21137 may use comes with Autoconf.
21139 @menu
21140 * Basic Installation::          Instructions for typical cases
21141 * Compilers and Options::       Selecting compilers and optimization
21142 * Multiple Architectures::      Compiling for multiple architectures at once
21143 * Installation Names::          Installing in different directories
21144 * Optional Features::           Selecting optional features
21145 * Particular Systems::          Particular systems
21146 * System Type::                 Specifying the system type
21147 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
21148 * Defining Variables::          Specifying the compiler etc.
21149 * configure Invocation::        Changing how @command{configure} runs
21150 @end menu
21152 @set autoconf
21153 @include install.texi
21156 @c ============================================== config.status Invocation
21158 @node config.status Invocation
21159 @chapter config.status Invocation
21160 @cindex @command{config.status}
21162 The @command{configure} script creates a file named @file{config.status},
21163 which actually configures, @dfn{instantiates}, the template files.  It
21164 also records the configuration options that were specified when the
21165 package was last configured in case reconfiguring is needed.
21167 Synopsis:
21168 @example
21169 ./config.status @ovar{option}@dots{} @ovar{tag}@dots{}
21170 @end example
21172 It configures each @var{tag}; if none are specified, all the templates
21173 are instantiated.  A @var{tag} refers to a file or other tag associated
21174 with a configuration action, as specified by an @code{AC_CONFIG_@var{ITEMS}}
21175 macro (@pxref{Configuration Actions}).  The files must be specified
21176 without their dependencies, as in
21178 @example
21179 ./config.status foobar
21180 @end example
21182 @noindent
21185 @example
21186 ./config.status foobar:foo.in:bar.in
21187 @end example
21189 The supported options are:
21191 @table @option
21192 @item --help
21193 @itemx -h
21194 Print a summary of the command line options, the list of the template
21195 files, and exit.
21197 @item --version
21198 @itemx -V
21199 Print the version number of Autoconf and the configuration settings,
21200 and exit.
21202 @item --config
21203 Print the configuration settings in reusable way, quoted for the shell,
21204 and exit.  For example, for a debugging build that otherwise reuses the
21205 configuration from a different build directory @var{build-dir} of a
21206 package in @var{src-dir}, you could use the following:
21208 @example
21209 args=`@var{build-dir}/config.status --config`
21210 eval @var{src-dir}/configure "$args" CFLAGS=-g --srcdir=@var{src-dir}
21211 @end example
21213 @noindent
21214 Note that it may be necessary to override a @option{--srcdir} setting
21215 that was saved in the configuration, if the arguments are used in a
21216 different build directory.
21218 @item --silent
21219 @itemx --quiet
21220 @itemx -q
21221 Do not print progress messages.
21223 @item --debug
21224 @itemx -d
21225 Don't remove the temporary files.
21227 @item --file=@var{file}[:@var{template}]
21228 Require that @var{file} be instantiated as if
21229 @samp{AC_CONFIG_FILES(@var{file}:@var{template})} was used.  Both
21230 @var{file} and @var{template} may be @samp{-} in which case the standard
21231 output and/or standard input, respectively, is used.  If a
21232 @var{template} file name is relative, it is first looked for in the build
21233 tree, and then in the source tree.  @xref{Configuration Actions}, for
21234 more details.
21236 This option and the following ones provide one way for separately
21237 distributed packages to share the values computed by @command{configure}.
21238 Doing so can be useful if some of the packages need a superset of the
21239 features that one of them, perhaps a common library, does.  These
21240 options allow a @file{config.status} file to create files other than the
21241 ones that its @file{configure.ac} specifies, so it can be used for a
21242 different package, or for extracting a subset of values.  For example,
21244 @example
21245 echo '@@CC@@' | ./config.status --file=-
21246 @end example
21248 @noindent
21249 provides the value of @code{@@CC@@} on standard output.
21251 @item --header=@var{file}[:@var{template}]
21252 Same as @option{--file} above, but with @samp{AC_CONFIG_HEADERS}.
21254 @item --recheck
21255 Ask @file{config.status} to update itself and exit (no instantiation).
21256 This option is useful if you change @command{configure}, so that the
21257 results of some tests might be different from the previous run.  The
21258 @option{--recheck} option reruns @command{configure} with the same arguments
21259 you used before, plus the @option{--no-create} option, which prevents
21260 @command{configure} from running @file{config.status} and creating
21261 @file{Makefile} and other files, and the @option{--no-recursion} option,
21262 which prevents @command{configure} from running other @command{configure}
21263 scripts in subdirectories.  (This is so other Make rules can
21264 run @file{config.status} when it changes; @pxref{Automatic Remaking},
21265 for an example).
21266 @end table
21268 @file{config.status} checks several optional environment variables that
21269 can alter its behavior:
21271 @anchor{CONFIG_SHELL}
21272 @defvar CONFIG_SHELL
21273 @evindex CONFIG_SHELL
21274 The shell with which to run @command{configure} for the @option{--recheck}
21275 option.  It must be Bourne-compatible.  The default is a shell that
21276 supports @code{LINENO} if available, and @file{/bin/sh} otherwise.
21277 Invoking @command{configure} by hand bypasses this setting, so you may
21278 need to use a command like @samp{CONFIG_SHELL=/bin/bash /bin/bash ./configure}
21279 to insure that the same shell is used everywhere.  The absolute name of the
21280 shell should be passed.
21281 @end defvar
21283 @defvar CONFIG_STATUS
21284 @evindex CONFIG_STATUS
21285 The file name to use for the shell script that records the
21286 configuration.  The default is @file{./config.status}.  This variable is
21287 useful when one package uses parts of another and the @command{configure}
21288 scripts shouldn't be merged because they are maintained separately.
21289 @end defvar
21291 You can use @file{./config.status} in your makefiles.  For example, in
21292 the dependencies given above (@pxref{Automatic Remaking}),
21293 @file{config.status} is run twice when @file{configure.ac} has changed.
21294 If that bothers you, you can make each run only regenerate the files for
21295 that rule:
21296 @example
21297 @group
21298 config.h: stamp-h
21299 stamp-h: config.h.in config.status
21300         ./config.status config.h
21301         echo > stamp-h
21303 Makefile: Makefile.in config.status
21304         ./config.status Makefile
21305 @end group
21306 @end example
21308 The calling convention of @file{config.status} has changed; see
21309 @ref{Obsolete config.status Use}, for details.
21312 @c =================================================== Obsolete Constructs
21314 @node Obsolete Constructs
21315 @chapter Obsolete Constructs
21316 @cindex Obsolete constructs
21318 Autoconf changes, and throughout the years some constructs have been
21319 obsoleted.  Most of the changes involve the macros, but in some cases
21320 the tools themselves, or even some concepts, are now considered
21321 obsolete.
21323 You may completely skip this chapter if you are new to Autoconf.  Its
21324 intention is mainly to help maintainers updating their packages by
21325 understanding how to move to more modern constructs.
21327 @menu
21328 * Obsolete config.status Use::  Obsolete convention for @command{config.status}
21329 * acconfig Header::             Additional entries in @file{config.h.in}
21330 * autoupdate Invocation::       Automatic update of @file{configure.ac}
21331 * Obsolete Macros::             Backward compatibility macros
21332 * Autoconf 1::                  Tips for upgrading your files
21333 * Autoconf 2.13::               Some fresher tips
21334 @end menu
21336 @node Obsolete config.status Use
21337 @section Obsolete @file{config.status} Invocation
21339 @file{config.status} now supports arguments to specify the files to
21340 instantiate; see @ref{config.status Invocation}, for more details.
21341 Before, environment variables had to be used.
21343 @defvar CONFIG_COMMANDS
21344 @evindex CONFIG_COMMANDS
21345 The tags of the commands to execute.  The default is the arguments given
21346 to @code{AC_OUTPUT} and @code{AC_CONFIG_COMMANDS} in
21347 @file{configure.ac}.
21348 @end defvar
21350 @defvar CONFIG_FILES
21351 @evindex CONFIG_FILES
21352 The files in which to perform @samp{@@@var{variable}@@} substitutions.
21353 The default is the arguments given to @code{AC_OUTPUT} and
21354 @code{AC_CONFIG_FILES} in @file{configure.ac}.
21355 @end defvar
21357 @defvar CONFIG_HEADERS
21358 @evindex CONFIG_HEADERS
21359 The files in which to substitute C @code{#define} statements.  The
21360 default is the arguments given to @code{AC_CONFIG_HEADERS}; if that
21361 macro was not called, @file{config.status} ignores this variable.
21362 @end defvar
21364 @defvar CONFIG_LINKS
21365 @evindex CONFIG_LINKS
21366 The symbolic links to establish.  The default is the arguments given to
21367 @code{AC_CONFIG_LINKS}; if that macro was not called,
21368 @file{config.status} ignores this variable.
21369 @end defvar
21371 In @ref{config.status Invocation}, using this old interface, the example
21372 would be:
21374 @example
21375 @group
21376 config.h: stamp-h
21377 stamp-h: config.h.in config.status
21378         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_FILES= \
21379           CONFIG_HEADERS=config.h ./config.status
21380         echo > stamp-h
21382 Makefile: Makefile.in config.status
21383         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_HEADERS= \
21384           CONFIG_FILES=Makefile ./config.status
21385 @end group
21386 @end example
21388 @noindent
21389 (If @file{configure.ac} does not call @code{AC_CONFIG_HEADERS}, there is
21390 no need to set @code{CONFIG_HEADERS} in the @command{make} rules.  Equally
21391 for @code{CONFIG_COMMANDS}, etc.)
21394 @node acconfig Header
21395 @section @file{acconfig.h}
21397 @cindex @file{acconfig.h}
21398 @cindex @file{config.h.top}
21399 @cindex @file{config.h.bot}
21401 In order to produce @file{config.h.in}, @command{autoheader} needs to
21402 build or to find templates for each symbol.  Modern releases of Autoconf
21403 use @code{AH_VERBATIM} and @code{AH_TEMPLATE} (@pxref{Autoheader
21404 Macros}), but in older releases a file, @file{acconfig.h}, contained the
21405 list of needed templates.  @command{autoheader} copied comments and
21406 @code{#define} and @code{#undef} statements from @file{acconfig.h} in
21407 the current directory, if present.  This file used to be mandatory if
21408 you @code{AC_DEFINE} any additional symbols.
21410 Modern releases of Autoconf also provide @code{AH_TOP} and
21411 @code{AH_BOTTOM} if you need to prepend/append some information to
21412 @file{config.h.in}.  Ancient versions of Autoconf had a similar feature:
21413 if @file{./acconfig.h} contains the string @samp{@@TOP@@},
21414 @command{autoheader} copies the lines before the line containing
21415 @samp{@@TOP@@} into the top of the file that it generates.  Similarly,
21416 if @file{./acconfig.h} contains the string @samp{@@BOTTOM@@},
21417 @command{autoheader} copies the lines after that line to the end of the
21418 file it generates.  Either or both of those strings may be omitted.  An
21419 even older alternate way to produce the same effect in ancient versions
21420 of Autoconf is to create the files @file{@var{file}.top} (typically
21421 @file{config.h.top}) and/or @file{@var{file}.bot} in the current
21422 directory.  If they exist, @command{autoheader} copies them to the
21423 beginning and end, respectively, of its output.
21425 In former versions of Autoconf, the files used in preparing a software
21426 package for distribution were:
21427 @example
21428 @group
21429 configure.ac --.   .------> autoconf* -----> configure
21430                +---+
21431 [aclocal.m4] --+   `---.
21432 [acsite.m4] ---'       |
21433                        +--> [autoheader*] -> [config.h.in]
21434 [acconfig.h] ----.     |
21435                  +-----'
21436 [config.h.top] --+
21437 [config.h.bot] --'
21438 @end group
21439 @end example
21441 Using only the @code{AH_} macros, @file{configure.ac} should be
21442 self-contained, and should not depend upon @file{acconfig.h} etc.
21445 @node autoupdate Invocation
21446 @section Using @command{autoupdate} to Modernize @file{configure.ac}
21447 @cindex @command{autoupdate}
21449 The @command{autoupdate} program updates a @file{configure.ac} file that
21450 calls Autoconf macros by their old names to use the current macro names.
21451 In version 2 of Autoconf, most of the macros were renamed to use a more
21452 uniform and descriptive naming scheme.  @xref{Macro Names}, for a
21453 description of the new scheme.  Although the old names still work
21454 (@pxref{Obsolete Macros}, for a list of the old macros and the corresponding
21455 new names), you can make your @file{configure.ac} files more readable
21456 and make it easier to use the current Autoconf documentation if you
21457 update them to use the new macro names.
21459 @evindex SIMPLE_BACKUP_SUFFIX
21460 If given no arguments, @command{autoupdate} updates @file{configure.ac},
21461 backing up the original version with the suffix @file{~} (or the value
21462 of the environment variable @code{SIMPLE_BACKUP_SUFFIX}, if that is
21463 set).  If you give @command{autoupdate} an argument, it reads that file
21464 instead of @file{configure.ac} and writes the updated file to the
21465 standard output.
21467 @noindent
21468 @command{autoupdate} accepts the following options:
21470 @table @option
21471 @item --help
21472 @itemx -h
21473 Print a summary of the command line options and exit.
21475 @item --version
21476 @itemx -V
21477 Print the version number of Autoconf and exit.
21479 @item --verbose
21480 @itemx -v
21481 Report processing steps.
21483 @item --debug
21484 @itemx -d
21485 Don't remove the temporary files.
21487 @item --force
21488 @itemx -f
21489 Force the update even if the file has not changed.  Disregard the cache.
21491 @item --include=@var{dir}
21492 @itemx -I @var{dir}
21493 Also look for input files in @var{dir}.  Multiple invocations accumulate.
21494 Directories are browsed from last to first.
21496 @item --prepend-include=@var{dir}
21497 @itemx -B @var{dir}
21498 Prepend directory @var{dir} to the search path.  This is used to include
21499 the language-specific files before any third-party macros.
21500 @end table
21502 @node Obsolete Macros
21503 @section Obsolete Macros
21505 Several macros are obsoleted in Autoconf, for various reasons (typically
21506 they failed to quote properly, couldn't be extended for more recent
21507 issues, etc.).  They are still supported, but deprecated: their use
21508 should be avoided.
21510 During the jump from Autoconf version 1 to version 2, most of the
21511 macros were renamed to use a more uniform and descriptive naming scheme,
21512 but their signature did not change.  @xref{Macro Names}, for a
21513 description of the new naming scheme.  Below, if there is just the mapping
21514 from old names to new names for these macros, the reader is invited to
21515 refer to the definition of the new macro for the signature and the
21516 description.
21518 @defmac AC_AIX
21519 @acindex{AIX}
21520 @cvindex _ALL_SOURCE
21521 This macro is a platform-specific subset of
21522 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
21523 @end defmac
21525 @defmac AC_ALLOCA
21526 @acindex{ALLOCA}
21527 Replaced by @code{AC_FUNC_ALLOCA} (@pxref{AC_FUNC_ALLOCA}).
21528 @end defmac
21530 @defmac AC_ARG_ARRAY
21531 @acindex{ARG_ARRAY}
21532 Removed because of limited usefulness.
21533 @end defmac
21535 @defmac AC_C_CROSS
21536 @acindex{C_CROSS}
21537 This macro is obsolete; it does nothing.
21538 @end defmac
21540 @defmac AC_C_LONG_DOUBLE
21541 @acindex{C_LONG_DOUBLE}
21542 @cvindex HAVE_LONG_DOUBLE
21543 If the C compiler supports a working @code{long double} type with more
21544 range or precision than the @code{double} type, define
21545 @code{HAVE_LONG_DOUBLE}.
21547 You should use @code{AC_TYPE_LONG_DOUBLE} or
21548 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
21549 @end defmac
21551 @defmac AC_CANONICAL_SYSTEM
21552 @acindex{CANONICAL_SYSTEM}
21553 Determine the system type and set output variables to the names of the
21554 canonical system types.  @xref{Canonicalizing}, for details about the
21555 variables this macro sets.
21557 The user is encouraged to use either @code{AC_CANONICAL_BUILD}, or
21558 @code{AC_CANONICAL_HOST}, or @code{AC_CANONICAL_TARGET}, depending on
21559 the needs.  Using @code{AC_CANONICAL_TARGET} is enough to run the two
21560 other macros (@pxref{Canonicalizing}).
21561 @end defmac
21563 @defmac AC_CHAR_UNSIGNED
21564 @acindex{CHAR_UNSIGNED}
21565 Replaced by @code{AC_C_CHAR_UNSIGNED} (@pxref{AC_C_CHAR_UNSIGNED}).
21566 @end defmac
21568 @defmac AC_CHECK_TYPE (@var{type}, @var{default})
21569 @acindex{CHECK_TYPE}
21570 Autoconf, up to 2.13, used to provide this version of
21571 @code{AC_CHECK_TYPE}, deprecated because of its flaws.  First, although
21572 it is a member of the @code{CHECK} clan, it does
21573 more than just checking.  Secondly, missing types are defined
21574 using @code{#define}, not @code{typedef}, and this can lead to
21575 problems in the case of pointer types.
21577 This use of @code{AC_CHECK_TYPE} is obsolete and discouraged; see
21578 @ref{Generic Types}, for the description of the current macro.
21580 If the type @var{type} is not defined, define it to be the C (or C++)
21581 builtin type @var{default}, e.g., @samp{short int} or @samp{unsigned int}.
21583 This macro is equivalent to:
21585 @example
21586 AC_CHECK_TYPE([@var{type}], [],
21587   [AC_DEFINE_UNQUOTED([@var{type}], [@var{default}],
21588      [Define to `@var{default}'
21589       if <sys/types.h> does not define.])])
21590 @end example
21592 In order to keep backward compatibility, the two versions of
21593 @code{AC_CHECK_TYPE} are implemented, selected using these heuristics:
21595 @enumerate
21596 @item
21597 If there are three or four arguments, the modern version is used.
21599 @item
21600 If the second argument appears to be a C or C++ type, then the
21601 obsolete version is used.  This happens if the argument is a C or C++
21602 @emph{builtin} type or a C identifier ending in @samp{_t}, optionally
21603 followed by one of @samp{[(* } and then by a string of zero or more
21604 characters taken from the set @samp{[]()* _a-zA-Z0-9}.
21606 @item
21607 If the second argument is spelled with the alphabet of valid C and C++
21608 types, the user is warned and the modern version is used.
21610 @item
21611 Otherwise, the modern version is used.
21612 @end enumerate
21614 @noindent
21615 You are encouraged either to use a valid builtin type, or to use the
21616 equivalent modern code (see above), or better yet, to use
21617 @code{AC_CHECK_TYPES} together with
21619 @example
21620 #ifndef HAVE_LOFF_T
21621 typedef loff_t off_t;
21622 #endif
21623 @end example
21624 @end defmac
21625 @c end of AC_CHECK_TYPE
21627 @defmac AC_CHECKING (@var{feature-description})
21628 @acindex{CHECKING}
21629 Same as
21631 @example
21632 AC_MSG_NOTICE([checking @var{feature-description}@dots{}]
21633 @end example
21635 @noindent
21636 @xref{AC_MSG_NOTICE}.
21637 @end defmac
21639 @defmac AC_COMPILE_CHECK (@var{echo-text}, @var{includes}, @
21640   @var{function-body}, @var{action-if-true}, @ovar{action-if-false})
21641 @acindex{COMPILE_CHECK}
21642 This is an obsolete version of @code{AC_TRY_COMPILE} itself replaced by
21643 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}), with the
21644 addition that it prints @samp{checking for @var{echo-text}} to the
21645 standard output first, if @var{echo-text} is non-empty.  Use
21646 @code{AC_MSG_CHECKING} and @code{AC_MSG_RESULT} instead to print
21647 messages (@pxref{Printing Messages}).
21648 @end defmac
21650 @defmac AC_CONST
21651 @acindex{CONST}
21652 Replaced by @code{AC_C_CONST} (@pxref{AC_C_CONST}).
21653 @end defmac
21655 @defmac AC_CROSS_CHECK
21656 @acindex{CROSS_CHECK}
21657 Same as @code{AC_C_CROSS}, which is obsolete too, and does nothing
21658 @code{:-)}.
21659 @end defmac
21661 @defmac AC_CYGWIN
21662 @acindex{CYGWIN}
21663 @evindex CYGWIN
21664 Check for the Cygwin environment in which case the shell variable
21665 @code{CYGWIN} is set to @samp{yes}.  Don't use this macro, the dignified
21666 means to check the nature of the host is using @code{AC_CANONICAL_HOST}
21667 (@pxref{Canonicalizing}).  As a matter of fact this macro is defined as:
21669 @example
21670 AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
21671 case $host_os in
21672   *cygwin* ) CYGWIN=yes;;
21673          * ) CYGWIN=no;;
21674 esac
21675 @end example
21677 Beware that the variable @env{CYGWIN} has a special meaning when
21678 running Cygwin, and should not be changed.  That's yet another reason
21679 not to use this macro.
21680 @end defmac
21682 @defmac AC_DECL_SYS_SIGLIST
21683 @acindex{DECL_SYS_SIGLIST}
21684 @cvindex SYS_SIGLIST_DECLARED
21685 Same as:
21687 @example
21688 AC_CHECK_DECLS([sys_siglist], [], [],
21689 [#include <signal.h>
21690 /* NetBSD declares sys_siglist in unistd.h.  */
21691 #ifdef HAVE_UNISTD_H
21692 # include <unistd.h>
21693 #endif
21695 @end example
21697 @noindent
21698 @xref{AC_CHECK_DECLS}.
21699 @end defmac
21701 @defmac AC_DECL_YYTEXT
21702 @acindex{DECL_YYTEXT}
21703 Does nothing, now integrated in @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
21704 @end defmac
21706 @defmac AC_DIR_HEADER
21707 @acindex{DIR_HEADER}
21708 @cvindex DIRENT
21709 @cvindex SYSNDIR
21710 @cvindex SYSDIR
21711 @cvindex NDIR
21712 Like calling @code{AC_FUNC_CLOSEDIR_VOID}
21713 (@pxref{AC_FUNC_CLOSEDIR_VOID}) and @code{AC_HEADER_DIRENT}
21714 (@pxref{AC_HEADER_DIRENT}),
21715 but defines a different set of C preprocessor macros to indicate which
21716 header file is found:
21718 @multitable {@file{sys/ndir.h}} {Old Symbol} {@code{HAVE_SYS_NDIR_H}}
21719 @item Header            @tab Old Symbol     @tab New Symbol
21720 @item @file{dirent.h}   @tab @code{DIRENT}  @tab @code{HAVE_DIRENT_H}
21721 @item @file{sys/ndir.h} @tab @code{SYSNDIR} @tab @code{HAVE_SYS_NDIR_H}
21722 @item @file{sys/dir.h}  @tab @code{SYSDIR}  @tab @code{HAVE_SYS_DIR_H}
21723 @item @file{ndir.h}     @tab @code{NDIR}    @tab @code{HAVE_NDIR_H}
21724 @end multitable
21725 @end defmac
21727 @defmac AC_DYNIX_SEQ
21728 @acindex{DYNIX_SEQ}
21729 If on DYNIX/ptx, add @option{-lseq} to output variable
21730 @code{LIBS}.  This macro used to be defined as
21732 @example
21733 AC_CHECK_LIB([seq], [getmntent], [LIBS="-lseq $LIBS"])
21734 @end example
21736 @noindent
21737 now it is just @code{AC_FUNC_GETMNTENT} (@pxref{AC_FUNC_GETMNTENT}).
21738 @end defmac
21740 @defmac AC_EXEEXT
21741 @acindex{EXEEXT}
21742 @ovindex EXEEXT
21743 Defined the output variable @code{EXEEXT} based on the output of the
21744 compiler, which is now done automatically.  Typically set to empty
21745 string if Posix and @samp{.exe} if a DOS variant.
21746 @end defmac
21748 @defmac AC_EMXOS2
21749 @acindex{EMXOS2}
21750 Similar to @code{AC_CYGWIN} but checks for the EMX environment on OS/2
21751 and sets @code{EMXOS2}.  Don't use this macro, the dignified means to
21752 check the nature of the host is using @code{AC_CANONICAL_HOST}
21753 (@pxref{Canonicalizing}).
21754 @end defmac
21756 @defmac AC_ENABLE (@var{feature}, @var{action-if-given}, @
21757   @ovar{action-if-not-given})
21758 @acindex{ENABLE}
21759 This is an obsolete version of @code{AC_ARG_ENABLE} that does not
21760 support providing a help string (@pxref{AC_ARG_ENABLE}).
21761 @end defmac
21763 @defmac AC_ERROR
21764 @acindex{ERROR}
21765 Replaced by @code{AC_MSG_ERROR} (@pxref{AC_MSG_ERROR}).
21766 @end defmac
21768 @defmac AC_FIND_X
21769 @acindex{FIND_X}
21770 Replaced by @code{AC_PATH_X} (@pxref{AC_PATH_X}).
21771 @end defmac
21773 @defmac AC_FIND_XTRA
21774 @acindex{FIND_XTRA}
21775 Replaced by @code{AC_PATH_XTRA} (@pxref{AC_PATH_XTRA}).
21776 @end defmac
21778 @defmac AC_FOREACH
21779 @acindex{FOREACH}
21780 Replaced by @code{m4_foreach_w} (@pxref{m4_foreach_w}).
21781 @end defmac
21783 @defmac AC_FUNC_CHECK
21784 @acindex{FUNC_CHECK}
21785 Replaced by @code{AC_CHECK_FUNC} (@pxref{AC_CHECK_FUNC}).
21786 @end defmac
21788 @anchor{AC_FUNC_SETVBUF_REVERSED}
21789 @defmac AC_FUNC_SETVBUF_REVERSED
21790 @acindex{FUNC_SETVBUF_REVERSED}
21791 @cvindex SETVBUF_REVERSED
21792 @c @fuindex setvbuf
21793 @prindex @code{setvbuf}
21794 Do nothing.  Formerly, this macro checked whether @code{setvbuf} takes
21795 the buffering type as its second argument and the buffer pointer as the
21796 third, instead of the other way around, and defined
21797 @code{SETVBUF_REVERSED}.  However, the last systems to have the problem
21798 were those based on SVR2, which became obsolete in 1987, and the macro
21799 is no longer needed.
21800 @end defmac
21802 @defmac AC_FUNC_WAIT3
21803 @acindex{FUNC_WAIT3}
21804 @cvindex HAVE_WAIT3
21805 @c @fuindex wait3
21806 @prindex @code{wait3}
21807 If @code{wait3} is found and fills in the contents of its third argument
21808 (a @samp{struct rusage *}), which HP-UX does not do, define
21809 @code{HAVE_WAIT3}.
21811 These days portable programs should use @code{waitpid}, not
21812 @code{wait3}, as @code{wait3} has been removed from Posix.
21813 @end defmac
21815 @defmac AC_GCC_TRADITIONAL
21816 @acindex{GCC_TRADITIONAL}
21817 Replaced by @code{AC_PROG_GCC_TRADITIONAL} (@pxref{AC_PROG_GCC_TRADITIONAL}).
21818 @end defmac
21820 @defmac AC_GETGROUPS_T
21821 @acindex{GETGROUPS_T}
21822 Replaced by @code{AC_TYPE_GETGROUPS} (@pxref{AC_TYPE_GETGROUPS}).
21823 @end defmac
21825 @defmac AC_GETLOADAVG
21826 @acindex{GETLOADAVG}
21827 Replaced by @code{AC_FUNC_GETLOADAVG} (@pxref{AC_FUNC_GETLOADAVG}).
21828 @end defmac
21830 @defmac AC_GNU_SOURCE
21831 @acindex{GNU_SOURCE}
21832 @cvindex _GNU_SOURCE
21833 This macro is a platform-specific subset of
21834 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
21835 @end defmac
21837 @defmac AC_HAVE_FUNCS
21838 @acindex{HAVE_FUNCS}
21839 Replaced by @code{AC_CHECK_FUNCS} (@pxref{AC_CHECK_FUNCS}).
21840 @end defmac
21842 @defmac AC_HAVE_HEADERS
21843 @acindex{HAVE_HEADERS}
21844 Replaced by @code{AC_CHECK_HEADERS} (@pxref{AC_CHECK_HEADERS}).
21845 @end defmac
21847 @defmac AC_HAVE_LIBRARY (@var{library}, @ovar{action-if-found}, @
21848   @ovar{action-if-not-found}, @ovar{other-libraries})
21849 @acindex{HAVE_LIBRARY}
21850 This macro is equivalent to calling @code{AC_CHECK_LIB} with a
21851 @var{function} argument of @code{main}.  In addition, @var{library} can
21852 be written as any of @samp{foo}, @option{-lfoo}, or @samp{libfoo.a}.  In
21853 all of those cases, the compiler is passed @option{-lfoo}.  However,
21854 @var{library} cannot be a shell variable; it must be a literal name.
21855 @xref{AC_CHECK_LIB}.
21856 @end defmac
21858 @defmac AC_HAVE_POUNDBANG
21859 @acindex{HAVE_POUNDBANG}
21860 Replaced by @code{AC_SYS_INTERPRETER} (@pxref{AC_SYS_INTERPRETER}).
21861 @end defmac
21863 @defmac AC_HEADER_CHECK
21864 @acindex{HEADER_CHECK}
21865 Replaced by @code{AC_CHECK_HEADER} (@pxref{AC_CHECK_HEADER}).
21866 @end defmac
21868 @defmac AC_HEADER_EGREP
21869 @acindex{HEADER_EGREP}
21870 Replaced by @code{AC_EGREP_HEADER} (@pxref{AC_EGREP_HEADER}).
21871 @end defmac
21873 @defmac AC_HELP_STRING
21874 @acindex{HELP_STRING}
21875 Replaced by @code{AS_HELP_STRING} (@pxref{AS_HELP_STRING}).
21876 @end defmac
21878 @defmac AC_INIT (@var{unique-file-in-source-dir})
21879 @acindex{INIT}
21880 Formerly @code{AC_INIT} used to have a single argument, and was
21881 equivalent to:
21883 @example
21884 AC_INIT
21885 AC_CONFIG_SRCDIR(@var{unique-file-in-source-dir})
21886 @end example
21887 See @ref{AC_INIT} and @ref{AC_CONFIG_SRCDIR}.
21888 @end defmac
21890 @defmac AC_INLINE
21891 @acindex{INLINE}
21892 Replaced by @code{AC_C_INLINE} (@pxref{AC_C_INLINE}).
21893 @end defmac
21895 @defmac AC_INT_16_BITS
21896 @acindex{INT_16_BITS}
21897 @cvindex INT_16_BITS
21898 If the C type @code{int} is 16 bits wide, define @code{INT_16_BITS}.
21899 Use @samp{AC_CHECK_SIZEOF(int)} instead (@pxref{AC_CHECK_SIZEOF}).
21900 @end defmac
21902 @defmac AC_IRIX_SUN
21903 @acindex{IRIX_SUN}
21904 If on IRIX (Silicon Graphics Unix), add @option{-lsun} to output
21905 @code{LIBS}.  If you were using it to get @code{getmntent}, use
21906 @code{AC_FUNC_GETMNTENT} instead.  If you used it for the NIS versions
21907 of the password and group functions, use @samp{AC_CHECK_LIB(sun,
21908 getpwnam)}.  Up to Autoconf 2.13, it used to be
21910 @example
21911 AC_CHECK_LIB([sun], [getmntent], [LIBS="-lsun $LIBS"])
21912 @end example
21914 @noindent
21915 now it is defined as
21917 @example
21918 AC_FUNC_GETMNTENT
21919 AC_CHECK_LIB([sun], [getpwnam])
21920 @end example
21922 @noindent
21923 See @ref{AC_FUNC_GETMNTENT} and @ref{AC_CHECK_LIB}.
21924 @end defmac
21926 @defmac AC_ISC_POSIX
21927 @acindex{ISC_POSIX}
21928 @ovindex LIBS
21929 This macro adds @option{-lcposix} to output variable @code{LIBS} if
21930 necessary for Posix facilities.  Sun dropped support for the obsolete
21931 INTERACTIVE Systems Corporation Unix on 2006-07-23.  New programs
21932 need not use this macro.  It is implemented as
21933 @code{AC_SEARCH_LIBS([strerror], [cposix])} (@pxref{AC_SEARCH_LIBS}).
21934 @end defmac
21936 @defmac AC_LANG_C
21937 @acindex{LANG_C}
21938 Same as @samp{AC_LANG([C])} (@pxref{AC_LANG}).
21939 @end defmac
21941 @defmac AC_LANG_CPLUSPLUS
21942 @acindex{LANG_CPLUSPLUS}
21943 Same as @samp{AC_LANG([C++])} (@pxref{AC_LANG}).
21944 @end defmac
21946 @defmac AC_LANG_FORTRAN77
21947 @acindex{LANG_FORTRAN77}
21948 Same as @samp{AC_LANG([Fortran 77])} (@pxref{AC_LANG}).
21949 @end defmac
21951 @defmac AC_LANG_RESTORE
21952 @acindex{LANG_RESTORE}
21953 Select the @var{language} that is saved on the top of the stack, as set
21954 by @code{AC_LANG_SAVE}, remove it from the stack, and call
21955 @code{AC_LANG(@var{language})}.  @xref{Language Choice}, for the
21956 preferred way to change languages.
21957 @end defmac
21959 @defmac AC_LANG_SAVE
21960 @acindex{LANG_SAVE}
21961 Remember the current language (as set by @code{AC_LANG}) on a stack.
21962 The current language does not change.  @code{AC_LANG_PUSH} is preferred
21963 (@pxref{AC_LANG_PUSH}).
21964 @end defmac
21966 @defmac AC_LINK_FILES (@var{source}@dots{}, @var{dest}@dots{})
21967 @acindex{LINK_FILES}
21968 This is an obsolete version of @code{AC_CONFIG_LINKS}
21969 (@pxref{AC_CONFIG_LINKS}.  An updated version of:
21971 @example
21972 AC_LINK_FILES(config/$machine.h config/$obj_format.h,
21973               host.h            object.h)
21974 @end example
21976 @noindent
21979 @example
21980 AC_CONFIG_LINKS([host.h:config/$machine.h
21981                 object.h:config/$obj_format.h])
21982 @end example
21983 @end defmac
21985 @defmac AC_LN_S
21986 @acindex{LN_S}
21987 Replaced by @code{AC_PROG_LN_S} (@pxref{AC_PROG_LN_S}).
21988 @end defmac
21990 @defmac AC_LONG_64_BITS
21991 @acindex{LONG_64_BITS}
21992 @cvindex LONG_64_BITS
21993 Define @code{LONG_64_BITS} if the C type @code{long int} is 64 bits wide.
21994 Use the generic macro @samp{AC_CHECK_SIZEOF([long int])} instead
21995 (@pxref{AC_CHECK_SIZEOF}).
21996 @end defmac
21998 @defmac AC_LONG_DOUBLE
21999 @acindex{LONG_DOUBLE}
22000 If the C compiler supports a working @code{long double} type with more
22001 range or precision than the @code{double} type, define
22002 @code{HAVE_LONG_DOUBLE}.
22004 You should use @code{AC_TYPE_LONG_DOUBLE} or
22005 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
22006 @end defmac
22008 @defmac AC_LONG_FILE_NAMES
22009 @acindex{LONG_FILE_NAMES}
22010 Replaced by
22011 @example
22012 AC_SYS_LONG_FILE_NAMES
22013 @end example
22014 @noindent
22015 @xref{AC_SYS_LONG_FILE_NAMES}.
22016 @end defmac
22018 @defmac AC_MAJOR_HEADER
22019 @acindex{MAJOR_HEADER}
22020 Replaced by @code{AC_HEADER_MAJOR} (@pxref{AC_HEADER_MAJOR}).
22021 @end defmac
22023 @defmac AC_MEMORY_H
22024 @acindex{MEMORY_H}
22025 @cvindex NEED_MEMORY_H
22026 Used to define @code{NEED_MEMORY_H} if the @code{mem} functions were
22027 defined in @file{memory.h}.  Today it is equivalent to
22028 @samp{AC_CHECK_HEADERS([memory.h])} (@pxref{AC_CHECK_HEADERS}).  Adjust
22029 your code to depend upon
22030 @code{HAVE_MEMORY_H}, not @code{NEED_MEMORY_H}; see @ref{Standard
22031 Symbols}.
22032 @end defmac
22034 @defmac AC_MINGW32
22035 @acindex{MINGW32}
22036 Similar to @code{AC_CYGWIN} but checks for the MinGW compiler
22037 environment and sets @code{MINGW32}.  Don't use this macro, the
22038 dignified means to check the nature of the host is using
22039 @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
22040 @end defmac
22042 @defmac AC_MINIX
22043 @acindex{MINIX}
22044 @cvindex _MINIX
22045 @cvindex _POSIX_SOURCE
22046 @cvindex _POSIX_1_SOURCE
22047 This macro is a platform-specific subset of
22048 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
22049 @end defmac
22051 @defmac AC_MINUS_C_MINUS_O
22052 @acindex{MINUS_C_MINUS_O}
22053 Replaced by @code{AC_PROG_CC_C_O} (@pxref{AC_PROG_CC_C_O}).
22054 @end defmac
22056 @defmac AC_MMAP
22057 @acindex{MMAP}
22058 Replaced by @code{AC_FUNC_MMAP} (@pxref{AC_FUNC_MMAP}).
22059 @end defmac
22061 @defmac AC_MODE_T
22062 @acindex{MODE_T}
22063 Replaced by @code{AC_TYPE_MODE_T} (@pxref{AC_TYPE_MODE_T}).
22064 @end defmac
22066 @defmac AC_OBJEXT
22067 @acindex{OBJEXT}
22068 @ovindex OBJEXT
22069 Defined the output variable @code{OBJEXT} based on the output of the
22070 compiler, after .c files have been excluded.  Typically set to @samp{o}
22071 if Posix, @samp{obj} if a DOS variant.
22072 Now the compiler checking macros handle
22073 this automatically.
22074 @end defmac
22076 @defmac AC_OBSOLETE (@var{this-macro-name}, @ovar{suggestion})
22077 @acindex{OBSOLETE}
22078 Make M4 print a message to the standard error output warning that
22079 @var{this-macro-name} is obsolete, and giving the file and line number
22080 where it was called.  @var{this-macro-name} should be the name of the
22081 macro that is calling @code{AC_OBSOLETE}.  If @var{suggestion} is given,
22082 it is printed at the end of the warning message; for example, it can be
22083 a suggestion for what to use instead of @var{this-macro-name}.
22085 For instance
22087 @example
22088 AC_OBSOLETE([$0], [; use AC_CHECK_HEADERS(unistd.h) instead])dnl
22089 @end example
22091 @noindent
22092 You are encouraged to use @code{AU_DEFUN} instead, since it gives better
22093 services to the user (@pxref{AU_DEFUN}).
22094 @end defmac
22096 @defmac AC_OFF_T
22097 @acindex{OFF_T}
22098 Replaced by @code{AC_TYPE_OFF_T} (@pxref{AC_TYPE_OFF_T}).
22099 @end defmac
22101 @defmac AC_OUTPUT (@ovar{file}@dots{}, @ovar{extra-cmds}, @ovar{init-cmds})
22102 @acindex{OUTPUT}
22103 The use of @code{AC_OUTPUT} with arguments is deprecated.  This obsoleted
22104 interface is equivalent to:
22106 @example
22107 @group
22108 AC_CONFIG_FILES(@var{file}@dots{})
22109 AC_CONFIG_COMMANDS([default],
22110                    @var{extra-cmds}, @var{init-cmds})
22111 AC_OUTPUT
22112 @end group
22113 @end example
22115 @noindent
22116 See @ref{AC_CONFIG_FILES}, @ref{AC_CONFIG_COMMANDS}, and @ref{AC_OUTPUT}.
22117 @end defmac
22119 @defmac AC_OUTPUT_COMMANDS (@var{extra-cmds}, @ovar{init-cmds})
22120 @acindex{OUTPUT_COMMANDS}
22121 Specify additional shell commands to run at the end of
22122 @file{config.status}, and shell commands to initialize any variables
22123 from @command{configure}.  This macro may be called multiple times.  It is
22124 obsolete, replaced by @code{AC_CONFIG_COMMANDS} (@pxref{AC_CONFIG_COMMANDS}).
22126 Here is an unrealistic example:
22128 @example
22129 fubar=27
22130 AC_OUTPUT_COMMANDS([echo this is extra $fubar, and so on.],
22131                    [fubar=$fubar])
22132 AC_OUTPUT_COMMANDS([echo this is another, extra, bit],
22133                    [echo init bit])
22134 @end example
22136 Aside from the fact that @code{AC_CONFIG_COMMANDS} requires an
22137 additional key, an important difference is that
22138 @code{AC_OUTPUT_COMMANDS} is quoting its arguments twice, unlike
22139 @code{AC_CONFIG_COMMANDS}.  This means that @code{AC_CONFIG_COMMANDS}
22140 can safely be given macro calls as arguments:
22142 @example
22143 AC_CONFIG_COMMANDS(foo, [my_FOO()])
22144 @end example
22146 @noindent
22147 Conversely, where one level of quoting was enough for literal strings
22148 with @code{AC_OUTPUT_COMMANDS}, you need two with
22149 @code{AC_CONFIG_COMMANDS}.  The following lines are equivalent:
22151 @example
22152 @group
22153 AC_OUTPUT_COMMANDS([echo "Square brackets: []"])
22154 AC_CONFIG_COMMANDS([default], [[echo "Square brackets: []"]])
22155 @end group
22156 @end example
22157 @end defmac
22159 @defmac AC_PID_T
22160 @acindex{PID_T}
22161 Replaced by @code{AC_TYPE_PID_T} (@pxref{AC_TYPE_PID_T}).
22162 @end defmac
22164 @defmac AC_PREFIX
22165 @acindex{PREFIX}
22166 Replaced by @code{AC_PREFIX_PROGRAM} (@pxref{AC_PREFIX_PROGRAM}).
22167 @end defmac
22169 @defmac AC_PROGRAMS_CHECK
22170 @acindex{PROGRAMS_CHECK}
22171 Replaced by @code{AC_CHECK_PROGS} (@pxref{AC_CHECK_PROGS}).
22172 @end defmac
22174 @defmac AC_PROGRAMS_PATH
22175 @acindex{PROGRAMS_PATH}
22176 Replaced by @code{AC_PATH_PROGS} (@pxref{AC_PATH_PROGS}).
22177 @end defmac
22179 @defmac AC_PROGRAM_CHECK
22180 @acindex{PROGRAM_CHECK}
22181 Replaced by @code{AC_CHECK_PROG} (@pxref{AC_CHECK_PROG}).
22182 @end defmac
22184 @defmac AC_PROGRAM_EGREP
22185 @acindex{PROGRAM_EGREP}
22186 Replaced by @code{AC_EGREP_CPP} (@pxref{AC_EGREP_CPP}).
22187 @end defmac
22189 @defmac AC_PROGRAM_PATH
22190 @acindex{PROGRAM_PATH}
22191 Replaced by @code{AC_PATH_PROG} (@pxref{AC_PATH_PROG}).
22192 @end defmac
22194 @defmac AC_REMOTE_TAPE
22195 @acindex{REMOTE_TAPE}
22196 Removed because of limited usefulness.
22197 @end defmac
22199 @defmac AC_RESTARTABLE_SYSCALLS
22200 @acindex{RESTARTABLE_SYSCALLS}
22201 This macro was renamed @code{AC_SYS_RESTARTABLE_SYSCALLS}.  However,
22202 these days portable programs should use @code{sigaction} with
22203 @code{SA_RESTART} if they want restartable system calls.  They should
22204 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
22205 system call is restartable is a dynamic issue, not a configuration-time
22206 issue.
22207 @end defmac
22209 @defmac AC_RETSIGTYPE
22210 @acindex{RETSIGTYPE}
22211 Replaced by @code{AC_TYPE_SIGNAL} (@pxref{AC_TYPE_SIGNAL}), which itself
22212 is obsolete when assuming C89 or better.
22213 @end defmac
22215 @defmac AC_RSH
22216 @acindex{RSH}
22217 Removed because of limited usefulness.
22218 @end defmac
22220 @defmac AC_SCO_INTL
22221 @acindex{SCO_INTL}
22222 @ovindex LIBS
22223 If on SCO Unix, add @option{-lintl} to output variable @code{LIBS}.  This
22224 macro used to do this:
22226 @example
22227 AC_CHECK_LIB([intl], [strftime], [LIBS="-lintl $LIBS"])
22228 @end example
22230 @noindent
22231 Now it just calls @code{AC_FUNC_STRFTIME} instead (@pxref{AC_FUNC_STRFTIME}).
22232 @end defmac
22234 @defmac AC_SETVBUF_REVERSED
22235 @acindex{SETVBUF_REVERSED}
22236 Replaced by
22237 @example
22238 AC_FUNC_SETVBUF_REVERSED
22239 @end example
22240 @noindent
22241 @xref{AC_FUNC_SETVBUF_REVERSED}.
22242 @end defmac
22244 @defmac AC_SET_MAKE
22245 @acindex{SET_MAKE}
22246 Replaced by @code{AC_PROG_MAKE_SET} (@pxref{AC_PROG_MAKE_SET}).
22247 @end defmac
22249 @defmac AC_SIZEOF_TYPE
22250 @acindex{SIZEOF_TYPE}
22251 Replaced by @code{AC_CHECK_SIZEOF} (@pxref{AC_CHECK_SIZEOF}).
22252 @end defmac
22254 @defmac AC_SIZE_T
22255 @acindex{SIZE_T}
22256 Replaced by @code{AC_TYPE_SIZE_T} (@pxref{AC_TYPE_SIZE_T}).
22257 @end defmac
22259 @defmac AC_STAT_MACROS_BROKEN
22260 @acindex{STAT_MACROS_BROKEN}
22261 Replaced by @code{AC_HEADER_STAT} (@pxref{AC_HEADER_STAT}).
22262 @end defmac
22264 @defmac AC_STDC_HEADERS
22265 @acindex{STDC_HEADERS}
22266 Replaced by @code{AC_HEADER_STDC} (@pxref{AC_HEADER_STDC}).
22267 @end defmac
22269 @defmac AC_STRCOLL
22270 @acindex{STRCOLL}
22271 Replaced by @code{AC_FUNC_STRCOLL} (@pxref{AC_FUNC_STRCOLL}).
22272 @end defmac
22274 @defmac AC_STRUCT_ST_BLKSIZE
22275 @acindex{STRUCT_ST_BLKSIZE}
22276 @cvindex HAVE_STRUCT_STAT_ST_BLKSIZE
22277 @cvindex HAVE_ST_BLKSIZE
22278 If @code{struct stat} contains an @code{st_blksize} member, define
22279 @code{HAVE_STRUCT_STAT_ST_BLKSIZE}.  The former name,
22280 @code{HAVE_ST_BLKSIZE} is to be avoided, as its support will cease in
22281 the future.  This macro is obsoleted, and should be replaced by
22283 @example
22284 AC_CHECK_MEMBERS([struct stat.st_blksize])
22285 @end example
22286 @noindent
22287 @xref{AC_CHECK_MEMBERS}.
22288 @end defmac
22290 @defmac AC_STRUCT_ST_RDEV
22291 @acindex{STRUCT_ST_RDEV}
22292 @cvindex HAVE_ST_RDEV
22293 @cvindex HAVE_STRUCT_STAT_ST_RDEV
22294 If @code{struct stat} contains an @code{st_rdev} member, define
22295 @code{HAVE_STRUCT_STAT_ST_RDEV}.  The former name for this macro,
22296 @code{HAVE_ST_RDEV}, is to be avoided as it will cease to be supported
22297 in the future.  Actually, even the new macro is obsolete and should be
22298 replaced by:
22299 @example
22300 AC_CHECK_MEMBERS([struct stat.st_rdev])
22301 @end example
22302 @noindent
22303 @xref{AC_CHECK_MEMBERS}.
22304 @end defmac
22306 @defmac AC_ST_BLKSIZE
22307 @acindex{ST_BLKSIZE}
22308 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
22309 @end defmac
22311 @defmac AC_ST_BLOCKS
22312 @acindex{ST_BLOCKS}
22313 Replaced by @code{AC_STRUCT_ST_BLOCKS} (@pxref{AC_STRUCT_ST_BLOCKS}).
22314 @end defmac
22316 @defmac AC_ST_RDEV
22317 @acindex{ST_RDEV}
22318 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
22319 @end defmac
22321 @defmac AC_SYS_RESTARTABLE_SYSCALLS
22322 @acindex{SYS_RESTARTABLE_SYSCALLS}
22323 @cvindex HAVE_RESTARTABLE_SYSCALLS
22324 If the system automatically restarts a system call that is interrupted
22325 by a signal, define @code{HAVE_RESTARTABLE_SYSCALLS}.  This macro does
22326 not check whether system calls are restarted in general---it checks whether a
22327 signal handler installed with @code{signal} (but not @code{sigaction})
22328 causes system calls to be restarted.  It does not check whether system calls
22329 can be restarted when interrupted by signals that have no handler.
22331 These days portable programs should use @code{sigaction} with
22332 @code{SA_RESTART} if they want restartable system calls.  They should
22333 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
22334 system call is restartable is a dynamic issue, not a configuration-time
22335 issue.
22336 @end defmac
22338 @defmac AC_SYS_SIGLIST_DECLARED
22339 @acindex{SYS_SIGLIST_DECLARED}
22340 This macro was renamed @code{AC_DECL_SYS_SIGLIST}.  However, even that
22341 name is obsolete, as the same functionality is now acheived via
22342 @code{AC_CHECK_DECLS} (@pxref{AC_CHECK_DECLS}).
22343 @end defmac
22345 @defmac AC_TEST_CPP
22346 @acindex{TEST_CPP}
22347 This macro was renamed @code{AC_TRY_CPP}, which in turn was replaced by
22348 @code{AC_PREPROC_IFELSE} (@pxref{AC_PREPROC_IFELSE}).
22349 @end defmac
22351 @defmac AC_TEST_PROGRAM
22352 @acindex{TEST_PROGRAM}
22353 This macro was renamed @code{AC_TRY_RUN}, which in turn was replaced by
22354 @code{AC_RUN_IFELSE} (@pxref{AC_RUN_IFELSE}).
22355 @end defmac
22357 @defmac AC_TIMEZONE
22358 @acindex{TIMEZONE}
22359 Replaced by @code{AC_STRUCT_TIMEZONE} (@pxref{AC_STRUCT_TIMEZONE}).
22360 @end defmac
22362 @defmac AC_TIME_WITH_SYS_TIME
22363 @acindex{TIME_WITH_SYS_TIME}
22364 Replaced by @code{AC_HEADER_TIME} (@pxref{AC_HEADER_TIME}).
22365 @end defmac
22367 @defmac AC_TRY_COMPILE (@var{includes}, @var{function-body}, @
22368   @ovar{action-if-true}, @ovar{action-if-false})
22369 @acindex{TRY_COMPILE}
22370 Same as:
22372 @example
22373 AC_COMPILE_IFELSE(
22374   [AC_LANG_PROGRAM([[@var{includes}]],
22375      [[@var{function-body}]])],
22376   [@var{action-if-true}],
22377   [@var{action-if-false}])
22378 @end example
22380 @noindent
22381 @xref{Running the Compiler}.
22383 This macro double quotes both @var{includes} and @var{function-body}.
22385 For C and C++, @var{includes} is any @code{#include} statements needed
22386 by the code in @var{function-body} (@var{includes} is ignored if
22387 the currently selected language is Fortran or Fortran 77).  The compiler
22388 and compilation flags are determined by the current language
22389 (@pxref{Language Choice}).
22390 @end defmac
22392 @defmac AC_TRY_CPP (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
22393 @acindex{TRY_CPP}
22394 Same as:
22396 @example
22397 AC_PREPROC_IFELSE(
22398   [AC_LANG_SOURCE([[@var{input}]])],
22399   [@var{action-if-true}],
22400   [@var{action-if-false}])
22401 @end example
22403 @noindent
22404 @xref{Running the Preprocessor}.
22406 This macro double quotes the @var{input}.
22407 @end defmac
22409 @defmac AC_TRY_LINK (@var{includes}, @var{function-body}, @
22410   @ovar{action-if-true}, @ovar{action-if-false})
22411 @acindex{TRY_LINK}
22412 Same as:
22414 @example
22415 AC_LINK_IFELSE(
22416   [AC_LANG_PROGRAM([[@var{includes}]],
22417      [[@var{function-body}]])],
22418   [@var{action-if-true}],
22419   [@var{action-if-false}])
22420 @end example
22422 @noindent
22423 @xref{Running the Compiler}.
22425 This macro double quotes both @var{includes} and @var{function-body}.
22427 Depending on the current language (@pxref{Language Choice}), create a
22428 test program to see whether a function whose body consists of
22429 @var{function-body} can be compiled and linked.  If the file compiles
22430 and links successfully, run shell commands @var{action-if-found},
22431 otherwise run @var{action-if-not-found}.
22433 This macro double quotes both @var{includes} and @var{function-body}.
22435 For C and C++, @var{includes} is any @code{#include} statements needed
22436 by the code in @var{function-body} (@var{includes} is ignored if
22437 the currently selected language is Fortran or Fortran 77).  The compiler
22438 and compilation flags are determined by the current language
22439 (@pxref{Language Choice}), and in addition @code{LDFLAGS} and
22440 @code{LIBS} are used for linking.
22441 @end defmac
22443 @defmac AC_TRY_LINK_FUNC (@var{function}, @ovar{action-if-found}, @
22444   @ovar{action-if-not-found})
22445 @acindex{TRY_LINK_FUNC}
22446 This macro is equivalent to
22447 @example
22448 AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])],
22449   [@var{action-if-found}], [@var{action-if-not-found}])
22450 @end example
22451 @noindent
22452 @xref{AC_LINK_IFELSE}.
22453 @end defmac
22455 @defmac AC_TRY_RUN (@var{program}, @ovar{action-if-true}, @
22456   @ovar{action-if-false}, @ovar{action-if-cross-compiling})
22457 @acindex{TRY_RUN}
22458 Same as:
22460 @example
22461 AC_RUN_IFELSE(
22462   [AC_LANG_SOURCE([[@var{program}]])],
22463   [@var{action-if-true}],
22464   [@var{action-if-false}],
22465   [@var{action-if-cross-compiling}])
22466 @end example
22468 @noindent
22469 @xref{Runtime}.
22470 @end defmac
22472 @anchor{AC_TYPE_SIGNAL}
22473 @defmac AC_TYPE_SIGNAL
22474 @acindex{TYPE_SIGNAL}
22475 @cvindex RETSIGTYPE
22476 @hdrindex{signal.h}
22477 If @file{signal.h} declares @code{signal} as returning a pointer to a
22478 function returning @code{void}, define @code{RETSIGTYPE} to be
22479 @code{void}; otherwise, define it to be @code{int}.  These days, it is
22480 portable to assume C89, and that signal handlers return @code{void},
22481 without needing to use this macro or @code{RETSIGTYPE}.
22483 When targetting older K&R C, it is possible to define signal handlers as
22484 returning type @code{RETSIGTYPE}, and omit a return statement:
22486 @example
22487 @group
22488 RETSIGTYPE
22489 hup_handler ()
22491 @dots{}
22493 @end group
22494 @end example
22495 @end defmac
22497 @defmac AC_UID_T
22498 @acindex{UID_T}
22499 Replaced by @code{AC_TYPE_UID_T} (@pxref{AC_TYPE_UID_T}).
22500 @end defmac
22502 @defmac AC_UNISTD_H
22503 @acindex{UNISTD_H}
22504 Same as @samp{AC_CHECK_HEADERS([unistd.h])} (@pxref{AC_CHECK_HEADERS}).
22505 @end defmac
22507 @defmac AC_USG
22508 @acindex{USG}
22509 @cvindex USG
22510 Define @code{USG} if the BSD string functions are defined in
22511 @file{strings.h}.  You should no longer depend upon @code{USG}, but on
22512 @code{HAVE_STRING_H}; see @ref{Standard Symbols}.
22513 @end defmac
22515 @defmac AC_UTIME_NULL
22516 @acindex{UTIME_NULL}
22517 Replaced by @code{AC_FUNC_UTIME_NULL} (@pxref{AC_FUNC_UTIME_NULL}).
22518 @end defmac
22520 @defmac AC_VALIDATE_CACHED_SYSTEM_TUPLE (@ovar{cmd})
22521 @acindex{VALIDATE_CACHED_SYSTEM_TUPLE}
22522 If the cache file is inconsistent with the current host, target and
22523 build system types, it used to execute @var{cmd} or print a default
22524 error message.  This is now handled by default.
22525 @end defmac
22527 @defmac AC_VERBOSE (@var{result-description})
22528 @acindex{VERBOSE}
22529 Replaced by @code{AC_MSG_RESULT} (@pxref{AC_MSG_RESULT}).
22530 @end defmac
22532 @defmac AC_VFORK
22533 @acindex{VFORK}
22534 Replaced by @code{AC_FUNC_FORK} (@pxref{AC_FUNC_FORK}).
22535 @end defmac
22537 @defmac AC_VPRINTF
22538 @acindex{VPRINTF}
22539 Replaced by @code{AC_FUNC_VPRINTF} (@pxref{AC_FUNC_VPRINTF}).
22540 @end defmac
22542 @defmac AC_WAIT3
22543 @acindex{WAIT3}
22544 This macro was renamed @code{AC_FUNC_WAIT3}.  However, these days
22545 portable programs should use @code{waitpid}, not @code{wait3}, as
22546 @code{wait3} has been removed from Posix.
22547 @end defmac
22549 @defmac AC_WARN
22550 @acindex{WARN}
22551 Replaced by @code{AC_MSG_WARN} (@pxref{AC_MSG_WARN}).
22552 @end defmac
22554 @defmac AC_WITH (@var{package}, @var{action-if-given}, @
22555   @ovar{action-if-not-given})
22556 @acindex{WITH}
22557 This is an obsolete version of @code{AC_ARG_WITH} that does not
22558 support providing a help string (@pxref{AC_ARG_WITH}).
22559 @end defmac
22561 @defmac AC_WORDS_BIGENDIAN
22562 @acindex{WORDS_BIGENDIAN}
22563 Replaced by @code{AC_C_BIGENDIAN} (@pxref{AC_C_BIGENDIAN}).
22564 @end defmac
22566 @defmac AC_XENIX_DIR
22567 @acindex{XENIX_DIR}
22568 @ovindex LIBS
22569 This macro used to add @option{-lx} to output variable @code{LIBS} if on
22570 Xenix.  Also, if @file{dirent.h} is being checked for, added
22571 @option{-ldir} to @code{LIBS}.  Now it is merely an alias of
22572 @code{AC_HEADER_DIRENT} instead, plus some code to detect whether
22573 running XENIX on which you should not depend:
22575 @example
22576 AC_MSG_CHECKING([for Xenix])
22577 AC_EGREP_CPP([yes],
22578 [#if defined M_XENIX && !defined M_UNIX
22579   yes
22580 #endif],
22581              [AC_MSG_RESULT([yes]); XENIX=yes],
22582              [AC_MSG_RESULT([no]); XENIX=])
22583 @end example
22584 @noindent
22585 Don't use this macro, the dignified means to check the nature of the
22586 host is using @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
22587 @end defmac
22589 @defmac AC_YYTEXT_POINTER
22590 @acindex{YYTEXT_POINTER}
22591 This macro was renamed @code{AC_DECL_YYTEXT}, which in turn was
22592 integrated into @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
22593 @end defmac
22595 @node Autoconf 1
22596 @section Upgrading From Version 1
22597 @cindex Upgrading autoconf
22598 @cindex Autoconf upgrading
22600 Autoconf version 2 is mostly backward compatible with version 1.
22601 However, it introduces better ways to do some things, and doesn't
22602 support some of the ugly things in version 1.  So, depending on how
22603 sophisticated your @file{configure.ac} files are, you might have to do
22604 some manual work in order to upgrade to version 2.  This chapter points
22605 out some problems to watch for when upgrading.  Also, perhaps your
22606 @command{configure} scripts could benefit from some of the new features in
22607 version 2; the changes are summarized in the file @file{NEWS} in the
22608 Autoconf distribution.
22610 @menu
22611 * Changed File Names::          Files you might rename
22612 * Changed Makefiles::           New things to put in @file{Makefile.in}
22613 * Changed Macros::              Macro calls you might replace
22614 * Changed Results::             Changes in how to check test results
22615 * Changed Macro Writing::       Better ways to write your own macros
22616 @end menu
22618 @node Changed File Names
22619 @subsection Changed File Names
22621 If you have an @file{aclocal.m4} installed with Autoconf (as opposed to
22622 in a particular package's source directory), you must rename it to
22623 @file{acsite.m4}.  @xref{autoconf Invocation}.
22625 If you distribute @file{install.sh} with your package, rename it to
22626 @file{install-sh} so @command{make} builtin rules don't inadvertently
22627 create a file called @file{install} from it.  @code{AC_PROG_INSTALL}
22628 looks for the script under both names, but it is best to use the new name.
22630 If you were using @file{config.h.top}, @file{config.h.bot}, or
22631 @file{acconfig.h}, you still can, but you have less clutter if you
22632 use the @code{AH_} macros.  @xref{Autoheader Macros}.
22634 @node Changed Makefiles
22635 @subsection Changed Makefiles
22637 Add @samp{@@CFLAGS@@}, @samp{@@CPPFLAGS@@}, and @samp{@@LDFLAGS@@} in
22638 your @file{Makefile.in} files, so they can take advantage of the values
22639 of those variables in the environment when @command{configure} is run.
22640 Doing this isn't necessary, but it's a convenience for users.
22642 Also add @samp{@@configure_input@@} in a comment to each input file for
22643 @code{AC_OUTPUT}, so that the output files contain a comment saying
22644 they were produced by @command{configure}.  Automatically selecting the
22645 right comment syntax for all the kinds of files that people call
22646 @code{AC_OUTPUT} on became too much work.
22648 Add @file{config.log} and @file{config.cache} to the list of files you
22649 remove in @code{distclean} targets.
22651 If you have the following in @file{Makefile.in}:
22653 @example
22654 prefix = /usr/local
22655 exec_prefix = $(prefix)
22656 @end example
22658 @noindent
22659 you must change it to:
22661 @example
22662 prefix = @@prefix@@
22663 exec_prefix = @@exec_prefix@@
22664 @end example
22666 @noindent
22667 The old behavior of replacing those variables without @samp{@@}
22668 characters around them has been removed.
22670 @node Changed Macros
22671 @subsection Changed Macros
22673 Many of the macros were renamed in Autoconf version 2.  You can still
22674 use the old names, but the new ones are clearer, and it's easier to find
22675 the documentation for them.  @xref{Obsolete Macros}, for a table showing the
22676 new names for the old macros.  Use the @command{autoupdate} program to
22677 convert your @file{configure.ac} to using the new macro names.
22678 @xref{autoupdate Invocation}.
22680 Some macros have been superseded by similar ones that do the job better,
22681 but are not call-compatible.  If you get warnings about calling obsolete
22682 macros while running @command{autoconf}, you may safely ignore them, but
22683 your @command{configure} script generally works better if you follow
22684 the advice that is printed about what to replace the obsolete macros with.  In
22685 particular, the mechanism for reporting the results of tests has
22686 changed.  If you were using @command{echo} or @code{AC_VERBOSE} (perhaps
22687 via @code{AC_COMPILE_CHECK}), your @command{configure} script's output
22688 looks better if you switch to @code{AC_MSG_CHECKING} and
22689 @code{AC_MSG_RESULT}.  @xref{Printing Messages}.  Those macros work best
22690 in conjunction with cache variables.  @xref{Caching Results}.
22694 @node Changed Results
22695 @subsection Changed Results
22697 If you were checking the results of previous tests by examining the
22698 shell variable @code{DEFS}, you need to switch to checking the values of
22699 the cache variables for those tests.  @code{DEFS} no longer exists while
22700 @command{configure} is running; it is only created when generating output
22701 files.  This difference from version 1 is because properly quoting the
22702 contents of that variable turned out to be too cumbersome and
22703 inefficient to do every time @code{AC_DEFINE} is called.  @xref{Cache
22704 Variable Names}.
22706 For example, here is a @file{configure.ac} fragment written for Autoconf
22707 version 1:
22709 @example
22710 AC_HAVE_FUNCS(syslog)
22711 case "$DEFS" in
22712 *-DHAVE_SYSLOG*) ;;
22713 *) # syslog is not in the default libraries.  See if it's in some other.
22714   saved_LIBS="$LIBS"
22715   for lib in bsd socket inet; do
22716     AC_CHECKING(for syslog in -l$lib)
22717     LIBS="-l$lib $saved_LIBS"
22718     AC_HAVE_FUNCS(syslog)
22719     case "$DEFS" in
22720     *-DHAVE_SYSLOG*) break ;;
22721     *) ;;
22722     esac
22723     LIBS="$saved_LIBS"
22724   done ;;
22725 esac
22726 @end example
22728 Here is a way to write it for version 2:
22730 @example
22731 AC_CHECK_FUNCS([syslog])
22732 if test "x$ac_cv_func_syslog" = xno; then
22733   # syslog is not in the default libraries.  See if it's in some other.
22734   for lib in bsd socket inet; do
22735     AC_CHECK_LIB([$lib], [syslog], [AC_DEFINE([HAVE_SYSLOG])
22736       LIBS="-l$lib $LIBS"; break])
22737   done
22739 @end example
22741 If you were working around bugs in @code{AC_DEFINE_UNQUOTED} by adding
22742 backslashes before quotes, you need to remove them.  It now works
22743 predictably, and does not treat quotes (except back quotes) specially.
22744 @xref{Setting Output Variables}.
22746 All of the Boolean shell variables set by Autoconf macros now use
22747 @samp{yes} for the true value.  Most of them use @samp{no} for false,
22748 though for backward compatibility some use the empty string instead.  If
22749 you were relying on a shell variable being set to something like 1 or
22750 @samp{t} for true, you need to change your tests.
22752 @node Changed Macro Writing
22753 @subsection Changed Macro Writing
22755 When defining your own macros, you should now use @code{AC_DEFUN}
22756 instead of @code{define}.  @code{AC_DEFUN} automatically calls
22757 @code{AC_PROVIDE} and ensures that macros called via @code{AC_REQUIRE}
22758 do not interrupt other macros, to prevent nested @samp{checking@dots{}}
22759 messages on the screen.  There's no actual harm in continuing to use the
22760 older way, but it's less convenient and attractive.  @xref{Macro
22761 Definitions}.
22763 You probably looked at the macros that came with Autoconf as a guide for
22764 how to do things.  It would be a good idea to take a look at the new
22765 versions of them, as the style is somewhat improved and they take
22766 advantage of some new features.
22768 If you were doing tricky things with undocumented Autoconf internals
22769 (macros, variables, diversions), check whether you need to change
22770 anything to account for changes that have been made.  Perhaps you can
22771 even use an officially supported technique in version 2 instead of
22772 kludging.  Or perhaps not.
22774 To speed up your locally written feature tests, add caching to them.
22775 See whether any of your tests are of general enough usefulness to
22776 encapsulate them into macros that you can share.
22779 @node Autoconf 2.13
22780 @section Upgrading From Version 2.13
22781 @cindex Upgrading autoconf
22782 @cindex Autoconf upgrading
22784 The introduction of the previous section (@pxref{Autoconf 1}) perfectly
22785 suits this section@enddots{}
22787 @quotation
22788 Autoconf version 2.50 is mostly backward compatible with version 2.13.
22789 However, it introduces better ways to do some things, and doesn't
22790 support some of the ugly things in version 2.13.  So, depending on how
22791 sophisticated your @file{configure.ac} files are, you might have to do
22792 some manual work in order to upgrade to version 2.50.  This chapter
22793 points out some problems to watch for when upgrading.  Also, perhaps
22794 your @command{configure} scripts could benefit from some of the new
22795 features in version 2.50; the changes are summarized in the file
22796 @file{NEWS} in the Autoconf distribution.
22797 @end quotation
22799 @menu
22800 * Changed Quotation::           Broken code which used to work
22801 * New Macros::                  Interaction with foreign macros
22802 * Hosts and Cross-Compilation::  Bugward compatibility kludges
22803 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
22804 * AC_ACT_IFELSE vs AC_TRY_ACT::  A more generic scheme for testing sources
22805 @end menu
22807 @node Changed Quotation
22808 @subsection Changed Quotation
22810 The most important changes are invisible to you: the implementation of
22811 most macros have completely changed.  This allowed more factorization of
22812 the code, better error messages, a higher uniformity of the user's
22813 interface etc.  Unfortunately, as a side effect, some construct which
22814 used to (miraculously) work might break starting with Autoconf 2.50.
22815 The most common culprit is bad quotation.
22817 For instance, in the following example, the message is not properly
22818 quoted:
22820 @example
22821 AC_INIT
22822 AC_CHECK_HEADERS(foo.h, ,
22823   AC_MSG_ERROR(cannot find foo.h, bailing out))
22824 AC_OUTPUT
22825 @end example
22827 @noindent
22828 Autoconf 2.13 simply ignores it:
22830 @example
22831 $ @kbd{autoconf-2.13; ./configure --silent}
22832 creating cache ./config.cache
22833 configure: error: cannot find foo.h
22835 @end example
22837 @noindent
22838 while Autoconf 2.50 produces a broken @file{configure}:
22840 @example
22841 $ @kbd{autoconf-2.50; ./configure --silent}
22842 configure: error: cannot find foo.h
22843 ./configure: exit: bad non-numeric arg `bailing'
22844 ./configure: exit: bad non-numeric arg `bailing'
22846 @end example
22848 The message needs to be quoted, and the @code{AC_MSG_ERROR} invocation
22849 too!
22851 @example
22852 AC_INIT([Example], [1.0], [bug-example@@example.org])
22853 AC_CHECK_HEADERS([foo.h], [],
22854   [AC_MSG_ERROR([cannot find foo.h, bailing out])])
22855 AC_OUTPUT
22856 @end example
22858 Many many (and many more) Autoconf macros were lacking proper quotation,
22859 including no less than@dots{} @code{AC_DEFUN} itself!
22861 @example
22862 $ @kbd{cat configure.in}
22863 AC_DEFUN([AC_PROG_INSTALL],
22864 [# My own much better version
22866 AC_INIT
22867 AC_PROG_INSTALL
22868 AC_OUTPUT
22869 $ @kbd{autoconf-2.13}
22870 autoconf: Undefined macros:
22871 ***BUG in Autoconf--please report*** AC_FD_MSG
22872 ***BUG in Autoconf--please report*** AC_EPI
22873 configure.in:1:AC_DEFUN([AC_PROG_INSTALL],
22874 configure.in:5:AC_PROG_INSTALL
22875 $ @kbd{autoconf-2.50}
22877 @end example
22880 @node New Macros
22881 @subsection New Macros
22883 @cindex undefined macro
22884 @cindex @code{_m4_divert_diversion}
22886 While Autoconf was relatively dormant in the late 1990s, Automake
22887 provided Autoconf-like macros for a while.  Starting with Autoconf 2.50
22888 in 2001, Autoconf provided
22889 versions of these macros, integrated in the @code{AC_} namespace,
22890 instead of @code{AM_}.  But in order to ease the upgrading via
22891 @command{autoupdate}, bindings to such @code{AM_} macros are provided.
22893 Unfortunately older versions of Automake (e.g., Automake 1.4)
22894 did not quote the names of these macros.
22895 Therefore, when @command{m4} finds something like
22896 @samp{AC_DEFUN(AM_TYPE_PTRDIFF_T, @dots{})} in @file{aclocal.m4},
22897 @code{AM_TYPE_PTRDIFF_T} is
22898 expanded, replaced with its Autoconf definition.
22900 Fortunately Autoconf catches pre-@code{AC_INIT} expansions, and
22901 complains, in its own words:
22903 @example
22904 $ @kbd{cat configure.ac}
22905 AC_INIT([Example], [1.0], [bug-example@@example.org])
22906 AM_TYPE_PTRDIFF_T
22907 $ @kbd{aclocal-1.4}
22908 $ @kbd{autoconf}
22909 aclocal.m4:17: error: m4_defn: undefined macro: _m4_divert_diversion
22910 aclocal.m4:17: the top level
22911 autom4te: m4 failed with exit status: 1
22913 @end example
22915 Modern versions of Automake no longer define most of these
22916 macros, and properly quote the names of the remaining macros.
22917 If you must use an old Automake, do not depend upon macros from Automake
22918 as it is simply not its job
22919 to provide macros (but the one it requires itself):
22921 @example
22922 $ @kbd{cat configure.ac}
22923 AC_INIT([Example], [1.0], [bug-example@@example.org])
22924 AM_TYPE_PTRDIFF_T
22925 $ @kbd{rm aclocal.m4}
22926 $ @kbd{autoupdate}
22927 autoupdate: `configure.ac' is updated
22928 $ @kbd{cat configure.ac}
22929 AC_INIT([Example], [1.0], [bug-example@@example.org])
22930 AC_CHECK_TYPES([ptrdiff_t])
22931 $ @kbd{aclocal-1.4}
22932 $ @kbd{autoconf}
22934 @end example
22937 @node Hosts and Cross-Compilation
22938 @subsection Hosts and Cross-Compilation
22939 @cindex Cross compilation
22941 Based on the experience of compiler writers, and after long public
22942 debates, many aspects of the cross-compilation chain have changed:
22944 @itemize @minus
22945 @item
22946 the relationship between the build, host, and target architecture types,
22948 @item
22949 the command line interface for specifying them to @command{configure},
22951 @item
22952 the variables defined in @command{configure},
22954 @item
22955 the enabling of cross-compilation mode.
22956 @end itemize
22958 @sp 1
22960 The relationship between build, host, and target have been cleaned up:
22961 the chain of default is now simply: target defaults to host, host to
22962 build, and build to the result of @command{config.guess}.  Nevertheless,
22963 in order to ease the transition from 2.13 to 2.50, the following
22964 transition scheme is implemented.  @emph{Do not rely on it}, as it will
22965 be completely disabled in a couple of releases (we cannot keep it, as it
22966 proves to cause more problems than it cures).
22968 They all default to the result of running @command{config.guess}, unless
22969 you specify either @option{--build} or @option{--host}.  In this case,
22970 the default becomes the system type you specified.  If you specify both,
22971 and they're different, @command{configure} enters cross compilation
22972 mode, so it doesn't run any tests that require execution.
22974 Hint: if you mean to override the result of @command{config.guess},
22975 prefer @option{--build} over @option{--host}.  In the future,
22976 @option{--host} will not override the name of the build system type.
22977 Whenever you specify @option{--host}, be sure to specify @option{--build}
22978 too.
22980 @sp 1
22982 For backward compatibility, @command{configure} accepts a system
22983 type as an option by itself.  Such an option overrides the
22984 defaults for build, host, and target system types.  The following
22985 configure statement configures a cross toolchain that runs on
22986 NetBSD/alpha but generates code for GNU Hurd/sparc,
22987 which is also the build platform.
22989 @example
22990 ./configure --host=alpha-netbsd sparc-gnu
22991 @end example
22993 @sp 1
22995 In Autoconf 2.13 and before, the variables @code{build}, @code{host},
22996 and @code{target} had a different semantics before and after the
22997 invocation of @code{AC_CANONICAL_BUILD} etc.  Now, the argument of
22998 @option{--build} is strictly copied into @code{build_alias}, and is left
22999 empty otherwise.  After the @code{AC_CANONICAL_BUILD}, @code{build} is
23000 set to the canonicalized build type.  To ease the transition, before,
23001 its contents is the same as that of @code{build_alias}.  Do @emph{not}
23002 rely on this broken feature.
23004 For consistency with the backward compatibility scheme exposed above,
23005 when @option{--host} is specified but @option{--build} isn't, the build
23006 system is assumed to be the same as @option{--host}, and
23007 @samp{build_alias} is set to that value.  Eventually, this
23008 historically incorrect behavior will go away.
23010 @sp 1
23012 The former scheme to enable cross-compilation proved to cause more harm
23013 than good, in particular, it used to be triggered too easily, leaving
23014 regular end users puzzled in front of cryptic error messages.
23015 @command{configure} could even enter cross-compilation mode only
23016 because the compiler was not functional.  This is mainly because
23017 @command{configure} used to try to detect cross-compilation, instead of
23018 waiting for an explicit flag from the user.
23020 Now, @command{configure} enters cross-compilation mode if and only if
23021 @option{--host} is passed.
23023 That's the short documentation.  To ease the transition between 2.13 and
23024 its successors, a more complicated scheme is implemented.  @emph{Do not
23025 rely on the following}, as it will be removed in the near future.
23027 If you specify @option{--host}, but not @option{--build}, when
23028 @command{configure} performs the first compiler test it tries to run
23029 an executable produced by the compiler.  If the execution fails, it
23030 enters cross-compilation mode.  This is fragile.  Moreover, by the time
23031 the compiler test is performed, it may be too late to modify the
23032 build-system type: other tests may have already been performed.
23033 Therefore, whenever you specify @option{--host}, be sure to specify
23034 @option{--build} too.
23036 @example
23037 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
23038 @end example
23040 @noindent
23041 enters cross-compilation mode.  The former interface, which
23042 consisted in setting the compiler to a cross-compiler without informing
23043 @command{configure} is obsolete.  For instance, @command{configure}
23044 fails if it can't run the code generated by the specified compiler if you
23045 configure as follows:
23047 @example
23048 ./configure CC=m68k-coff-gcc
23049 @end example
23052 @node AC_LIBOBJ vs LIBOBJS
23053 @subsection @code{AC_LIBOBJ} vs.@: @code{LIBOBJS}
23055 Up to Autoconf 2.13, the replacement of functions was triggered via the
23056 variable @code{LIBOBJS}.  Since Autoconf 2.50, the macro
23057 @code{AC_LIBOBJ} should be used instead (@pxref{Generic Functions}).
23058 Starting at Autoconf 2.53, the use of @code{LIBOBJS} is an error.
23060 This change is mandated by the unification of the GNU Build System
23061 components.  In particular, the various fragile techniques used to parse
23062 a @file{configure.ac} are all replaced with the use of traces.  As a
23063 consequence, any action must be traceable, which obsoletes critical
23064 variable assignments.  Fortunately, @code{LIBOBJS} was the only problem,
23065 and it can even be handled gracefully (read, ``without your having to
23066 change something'').
23068 There were two typical uses of @code{LIBOBJS}: asking for a replacement
23069 function, and adjusting @code{LIBOBJS} for Automake and/or Libtool.
23071 @sp 1
23073 As for function replacement, the fix is immediate: use
23074 @code{AC_LIBOBJ}.  For instance:
23076 @example
23077 LIBOBJS="$LIBOBJS fnmatch.o"
23078 LIBOBJS="$LIBOBJS malloc.$ac_objext"
23079 @end example
23081 @noindent
23082 should be replaced with:
23084 @example
23085 AC_LIBOBJ([fnmatch])
23086 AC_LIBOBJ([malloc])
23087 @end example
23089 @sp 1
23091 @ovindex LIBOBJDIR
23092 When used with Automake 1.10 or newer, a suitable value for
23093 @code{LIBOBJDIR} is set so that the @code{LIBOBJS} and @code{LTLIBOBJS}
23094 can be referenced from any @file{Makefile.am}.  Even without Automake,
23095 arranging for @code{LIBOBJDIR} to be set correctly enables
23096 referencing @code{LIBOBJS} and @code{LTLIBOBJS} in another directory.
23097 The @code{LIBOBJDIR} feature is experimental.
23100 @node AC_ACT_IFELSE vs AC_TRY_ACT
23101 @subsection @code{AC_@var{ACT}_IFELSE} vs.@: @code{AC_TRY_@var{ACT}}
23102 @c the anchor keeps the old node name, to try to avoid breaking links
23103 @anchor{AC_FOO_IFELSE vs AC_TRY_FOO}
23105 @acindex{@var{ACT}_IFELSE}
23106 @acindex{TRY_@var{ACT}}
23107 Since Autoconf 2.50, internal codes uses @code{AC_PREPROC_IFELSE},
23108 @code{AC_COMPILE_IFELSE}, @code{AC_LINK_IFELSE}, and
23109 @code{AC_RUN_IFELSE} on one hand and @code{AC_LANG_SOURCES},
23110 and @code{AC_LANG_PROGRAM} on the other hand instead of the deprecated
23111 @code{AC_TRY_CPP}, @code{AC_TRY_COMPILE}, @code{AC_TRY_LINK}, and
23112 @code{AC_TRY_RUN}.  The motivations where:
23113 @itemize @minus
23114 @item
23115 a more consistent interface: @code{AC_TRY_COMPILE} etc.@: were double
23116 quoting their arguments;
23118 @item
23119 the combinatoric explosion is solved by decomposing on the one hand the
23120 generation of sources, and on the other hand executing the program;
23122 @item
23123 this scheme helps supporting more languages than plain C and C++.
23124 @end itemize
23126 In addition to the change of syntax, the philosophy has changed too:
23127 while emphasis was put on speed at the expense of accuracy, today's
23128 Autoconf promotes accuracy of the testing framework at, ahem@dots{}, the
23129 expense of speed.
23132 As a perfect example of what is @emph{not} to be done, here is how to
23133 find out whether a header file contains a particular declaration, such
23134 as a typedef, a structure, a structure member, or a function.  Use
23135 @code{AC_EGREP_HEADER} instead of running @code{grep} directly on the
23136 header file; on some systems the symbol might be defined in another
23137 header file that the file you are checking includes.
23139 As a (bad) example, here is how you should not check for C preprocessor
23140 symbols, either defined by header files or predefined by the C
23141 preprocessor: using @code{AC_EGREP_CPP}:
23143 @example
23144 @group
23145 AC_EGREP_CPP(yes,
23146 [#ifdef _AIX
23147   yes
23148 #endif
23149 ], is_aix=yes, is_aix=no)
23150 @end group
23151 @end example
23153 The above example, properly written would (i) use
23154 @code{AC_LANG_PROGRAM}, and (ii) run the compiler:
23156 @example
23157 @group
23158 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
23159 [[#ifndef _AIX
23160  error: This isn't AIX!
23161 #endif
23162 ]])],
23163                    [is_aix=yes],
23164                    [is_aix=no])
23165 @end group
23166 @end example
23169 @c ============================= Generating Test Suites with Autotest
23171 @node Using Autotest
23172 @chapter Generating Test Suites with Autotest
23174 @cindex Autotest
23176 @display
23177 @strong{N.B.: This section describes a feature which is still
23178 stabilizing.  Although we believe that Autotest is useful as-is, this
23179 documentation describes an interface which might change in the future:
23180 do not depend upon Autotest without subscribing to the Autoconf mailing
23181 lists.}
23182 @end display
23184 It is paradoxical that portable projects depend on nonportable tools
23185 to run their test suite.  Autoconf by itself is the paragon of this
23186 problem: although it aims at perfectly portability, up to 2.13 its
23187 test suite was using DejaGNU, a rich and complex testing
23188 framework, but which is far from being standard on Posix systems.
23189 Worse yet, it was likely to be missing on the most fragile platforms,
23190 the very platforms that are most likely to torture Autoconf and
23191 exhibit deficiencies.
23193 To circumvent this problem, many package maintainers have developed their
23194 own testing framework, based on simple shell scripts whose sole outputs
23195 are exit status values describing whether the test succeeded.  Most of
23196 these tests share common patterns, and this can result in lots of
23197 duplicated code and tedious maintenance.
23199 Following exactly the same reasoning that yielded to the inception of
23200 Autoconf, Autotest provides a test suite generation framework, based on
23201 M4 macros building a portable shell script.  The suite itself is
23202 equipped with automatic logging and tracing facilities which greatly
23203 diminish the interaction with bug reporters, and simple timing reports.
23205 Autoconf itself has been using Autotest for years, and we do attest that
23206 it has considerably improved the strength of the test suite and the
23207 quality of bug reports.  Other projects are known to use some generation
23208 of Autotest, such as Bison, Free Recode, Free Wdiff, GNU Tar, each of
23209 them with different needs, and this usage has validated Autotest as a general
23210 testing framework.
23212 Nonetheless, compared to DejaGNU, Autotest is inadequate for
23213 interactive tool testing, which is probably its main limitation.
23215 @menu
23216 * Using an Autotest Test Suite::  Autotest and the user
23217 * Writing Testsuites::          Autotest macros
23218 * testsuite Invocation::        Running @command{testsuite} scripts
23219 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
23220 @end menu
23222 @node Using an Autotest Test Suite
23223 @section Using an Autotest Test Suite
23225 @menu
23226 * testsuite Scripts::           The concepts of Autotest
23227 * Autotest Logs::               Their contents
23228 @end menu
23230 @node testsuite Scripts
23231 @subsection @command{testsuite} Scripts
23233 @cindex @command{testsuite}
23235 Generating testing or validation suites using Autotest is rather easy.
23236 The whole validation suite is held in a file to be processed through
23237 @command{autom4te}, itself using GNU M4 under the hood, to
23238 produce a stand-alone Bourne shell script which then gets distributed.
23239 Neither @command{autom4te} nor GNU M4 are needed at
23240 the installer's end.
23242 @cindex test group
23243 Each test of the validation suite should be part of some test group.  A
23244 @dfn{test group} is a sequence of interwoven tests that ought to be
23245 executed together, usually because one test in the group creates data
23246 files than a later test in the same group needs to read.  Complex test
23247 groups make later debugging more tedious.  It is much better to
23248 keep only a few tests per test group.  Ideally there is only one test
23249 per test group.
23251 For all but the simplest packages, some file such as @file{testsuite.at}
23252 does not fully hold all test sources, as these are often easier to
23253 maintain in separate files.  Each of these separate files holds a single
23254 test group, or a sequence of test groups all addressing some common
23255 functionality in the package.  In such cases, @file{testsuite.at}
23256 merely initializes the validation suite, and sometimes does elementary
23257 health checking, before listing include statements for all other test
23258 files.  The special file @file{package.m4}, containing the
23259 identification of the package, is automatically included if found.
23261 A convenient alternative consists in moving all the global issues
23262 (local Autotest macros, elementary health checking, and @code{AT_INIT}
23263 invocation) into the file @code{local.at}, and making
23264 @file{testsuite.at} be a simple list of @code{m4_include} of sub test
23265 suites.  In such case, generating the whole test suite or pieces of it
23266 is only a matter of choosing the @command{autom4te} command line
23267 arguments.
23269 The validation scripts that Autotest produces are by convention called
23270 @command{testsuite}.  When run, @command{testsuite} executes each test
23271 group in turn, producing only one summary line per test to say if that
23272 particular test succeeded or failed.  At end of all tests, summarizing
23273 counters get printed.  One debugging directory is left for each test
23274 group which failed, if any: such directories are named
23275 @file{testsuite.dir/@var{nn}}, where @var{nn} is the sequence number of
23276 the test group, and they include:
23278 @itemize @bullet
23279 @item a debugging script named @file{run} which reruns the test in
23280 @dfn{debug mode} (@pxref{testsuite Invocation}).  The automatic generation
23281 of debugging scripts has the purpose of easing the chase for bugs.
23283 @item all the files created with @code{AT_DATA}
23285 @item all the Erlang source code files created with @code{AT_CHECK_EUNIT}
23287 @item a log of the run, named @file{testsuite.log}
23288 @end itemize
23290 In the ideal situation, none of the tests fail, and consequently no
23291 debugging directory is left behind for validation.
23293 It often happens in practice that individual tests in the validation
23294 suite need to get information coming out of the configuration process.
23295 Some of this information, common for all validation suites, is provided
23296 through the file @file{atconfig}, automatically created by
23297 @code{AC_CONFIG_TESTDIR}.  For configuration informations which your
23298 testing environment specifically needs, you might prepare an optional
23299 file named @file{atlocal.in}, instantiated by @code{AC_CONFIG_FILES}.
23300 The configuration process produces @file{atconfig} and @file{atlocal}
23301 out of these two input files, and these two produced files are
23302 automatically read by the @file{testsuite} script.
23304 Here is a diagram showing the relationship between files.
23306 @noindent
23307 Files used in preparing a software package for distribution:
23309 @example
23310                 [package.m4] -->.
23311                                  \
23312 subfile-1.at ->.  [local.at] ---->+
23313     ...         \                  \
23314 subfile-i.at ---->-- testsuite.at -->-- autom4te* -->testsuite
23315     ...         /
23316 subfile-n.at ->'
23317 @end example
23319 @noindent
23320 Files used in configuring a software package:
23322 @example
23323                                      .--> atconfig
23324                                     /
23325 [atlocal.in] -->  config.status* --<
23326                                     \
23327                                      `--> [atlocal]
23328 @end example
23330 @noindent
23331 Files created during test suite execution:
23333 @example
23334 atconfig -->.                    .--> testsuite.log
23335              \                  /
23336               >-- testsuite* --<
23337              /                  \
23338 [atlocal] ->'                    `--> [testsuite.dir]
23339 @end example
23342 @node Autotest Logs
23343 @subsection Autotest Logs
23345 When run, the test suite creates a log file named after itself, e.g., a
23346 test suite named @command{testsuite} creates @file{testsuite.log}.  It
23347 contains a lot of information, usually more than maintainers actually
23348 need, but therefore most of the time it contains all that is needed:
23350 @table @asis
23351 @item command line arguments
23352 A bad but unfortunately widespread habit consists of
23353 setting environment variables before the command, such as in
23354 @samp{CC=my-home-grown-cc ./testsuite}.  The test suite does not
23355 know this change, hence (i) it cannot report it to you, and (ii)
23356 it cannot preserve the value of @code{CC} for subsequent runs.
23357 Autoconf faced exactly the same problem, and solved it by asking
23358 users to pass the variable definitions as command line arguments.
23359 Autotest requires this rule, too, but has no means to enforce it; the log
23360 then contains a trace of the variables that were changed by the user.
23362 @item @file{ChangeLog} excerpts
23363 The topmost lines of all the @file{ChangeLog} files found in the source
23364 hierarchy.  This is especially useful when bugs are reported against
23365 development versions of the package, since the version string does not
23366 provide sufficient information to know the exact state of the sources
23367 the user compiled.  Of course, this relies on the use of a
23368 @file{ChangeLog}.
23370 @item build machine
23371 Running a test suite in a cross-compile environment is not an easy task,
23372 since it would mean having the test suite run on a machine @var{build},
23373 while running programs on a machine @var{host}.  It is much simpler to
23374 run both the test suite and the programs on @var{host}, but then, from
23375 the point of view of the test suite, there remains a single environment,
23376 @var{host} = @var{build}.  The log contains relevant information on the
23377 state of the @var{build} machine, including some important environment
23378 variables.
23379 @c FIXME: How about having an M4sh macro to say `hey, log the value
23380 @c of `@dots{}'?  This would help both Autoconf and Autotest.
23382 @item tested programs
23383 The absolute file name and answers to @option{--version} of the tested
23384 programs (see @ref{Writing Testsuites}, @code{AT_TESTED}).
23386 @item configuration log
23387 The contents of @file{config.log}, as created by @command{configure},
23388 are appended.  It contains the configuration flags and a detailed report
23389 on the configuration itself.
23390 @end table
23393 @node Writing Testsuites
23394 @section Writing @file{testsuite.at}
23396 The @file{testsuite.at} is a Bourne shell script making use of special
23397 Autotest M4 macros.  It often contains a call to @code{AT_INIT} near
23398 its beginning followed by one call to @code{m4_include} per source file
23399 for tests.  Each such included file, or the remainder of
23400 @file{testsuite.at} if include files are not used, contain a sequence of
23401 test groups.  Each test group begins with a call to @code{AT_SETUP},
23402 then an arbitrary number of shell commands or calls to @code{AT_CHECK},
23403 and then completes with a call to @code{AT_CLEANUP}.  Multiple test
23404 groups can be categorized by a call to @code{AT_BANNER}.
23406 All of the public Autotest macros have all-uppercase names in the
23407 namespace @samp{^AT_} to prevent them from accidentally conflicting with
23408 other text; Autoconf also reserves the namespace @samp{^_AT_} for
23409 internal macros.  All shell variables used in the testsuite for internal
23410 purposes have mostly-lowercase names starting with @samp{at_}.  Autotest
23411 also uses here-document delimiters in the namespace @samp{^_AT[A-Z]}, and
23412 makes use of the file system namespace @samp{^at-}.
23414 Since Autoconf is built on top of M4sugar (@pxref{Programming in
23415 M4sugar}) and M4sh (@pxref{Programming in M4sh}), you must also be aware
23416 of those namespaces (@samp{^_?\(m4\|AS\)_}).  In general, you
23417 @emph{should not use} the namespace of a package that does not own the
23418 macro or shell code you are writing.
23420 @defmac AT_INIT (@ovar{name})
23421 @atindex{INIT}
23422 @c FIXME: Not clear, plus duplication of the information.
23423 Initialize Autotest.  Giving a @var{name} to the test suite is
23424 encouraged if your package includes several test suites.  Before this
23425 macro is called, @code{AT_PACKAGE_STRING} and
23426 @code{AT_PACKAGE_BUGREPORT} must be defined, which are used to display
23427 information about the testsuite to the user.  Typically, these macros
23428 are provided by a file @file{package.m4} built by @command{make}
23429 (@pxref{Making testsuite Scripts}), in order to inherit the package
23430 name, version, and bug reporting address from @file{configure.ac}.
23431 @end defmac
23433 @defmac AT_COPYRIGHT (@var{copyright-notice})
23434 @atindex{COPYRIGHT}
23435 @cindex Copyright Notice
23436 State that, in addition to the Free Software Foundation's copyright on
23437 the Autotest macros, parts of your test suite are covered by
23438 @var{copyright-notice}.
23440 The @var{copyright-notice} shows up in both the head of
23441 @command{testsuite} and in @samp{testsuite --version}.
23442 @end defmac
23444 @defmac AT_ARG_OPTION (@var{options}, @var{help-text}, @
23445   @ovar{action-if-given}, @ovar{action-if-not-given})
23446 @atindex{ARG_OPTION}
23447 Accept options from the space-separated list @var{options}, a list that
23448 has leading dashes removed from the options.  Long options will be
23449 prefixed with @samp{--}, single-character options with @samp{-}.  The
23450 first word in this list is the primary @var{option}, any others are
23451 assumed to be short-hand aliases.  The variable associated with it
23452 is @code{at_arg_@var{option}}, with any dashes in @var{option} replaced
23453 with underscores.
23455 If the user passes @option{--@var{option}} to the @command{testsuite},
23456 the variable will be set to @samp{:}.  If the user does not pass the
23457 option, or passes @option{--no-@var{option}}, then the variable will be
23458 set to @samp{false}.
23460 @var{action-if-given} is run each time the option is encountered; here,
23461 the variable @code{at_optarg} will be set to @samp{:} or @samp{false} as
23462 appropriate.  @code{at_optarg} is actually just a copy of
23463 @code{at_arg_@var{option}}.
23465 @var{action-if-not-given} will be run once after option parsing is
23466 complete and if no option from @var{options} was used.
23468 @var{help-text} is added to the end of the list of options shown in
23469 @command{testsuite --help} (@pxref{AS_HELP_STRING}).
23471 It it recommended that you use a package-specific prefix to @var{options}
23472 names in order to avoid clashes with future Autotest built-in options.
23473 @end defmac
23475 @defmac AT_ARG_OPTION_ARG (@var{options}, @var{help-text}, @
23476   @ovar{action-if-given}, @ovar{action-if-not-given})
23477 @atindex{ARG_OPTION_ARG}
23478 Accept options with arguments from the space-separated list
23479 @var{options}, a list that has leading dashes removed from the options.
23480 Long options will be prefixed with @samp{--}, single-character options
23481 with @samp{-}.  The first word in this list is the primary @var{option},
23482 any others are assumed to be short-hand aliases.  The variable associated
23483 with it is @code{at_arg_@var{option}}, with any dashes in @var{option}
23484 replaced with underscores.
23486 If the user passes @option{--@var{option}=@var{arg}} or
23487 @option{--@var{option} @var{arg}} to the @command{testsuite}, the
23488 variable will be set to @samp{@var{arg}}.
23490 @var{action-if-given} is run each time the option is encountered; here,
23491 the variable @code{at_optarg} will be set to @samp{@var{arg}}.
23492 @code{at_optarg} is actually just a copy of @code{at_arg_@var{option}}.
23494 @var{action-if-not-given} will be run once after option parsing is
23495 complete and if no option from @var{options} was used.
23497 @var{help-text} is added to the end of the list of options shown in
23498 @command{testsuite --help} (@pxref{AS_HELP_STRING}).
23500 It it recommended that you use a package-specific prefix to @var{options}
23501 names in order to avoid clashes with future Autotest built-in options.
23502 @end defmac
23504 @defmac AT_COLOR_TESTS
23505 @atindex{COLOR_TESTS}
23506 Enable colored test results by default when the output is connected to
23507 a terminal.
23508 @end defmac
23510 @defmac AT_TESTED (@var{executables})
23511 @atindex{TESTED}
23512 Log the file name and answer to @option{--version} of each program in
23513 space-separated list @var{executables}.  Several invocations register
23514 new executables, in other words, don't fear registering one program
23515 several times.
23517 Autotest test suites rely on @env{PATH} to find the tested program.
23518 This avoids the need to generate absolute names of the various tools, and
23519 makes it possible to test installed programs.  Therefore, knowing which
23520 programs are being exercised is crucial to understanding problems in
23521 the test suite itself, or its occasional misuses.  It is a good idea to
23522 also subscribe foreign programs you depend upon, to avoid incompatible
23523 diagnostics.
23524 @end defmac
23526 @sp 1
23528 @defmac AT_BANNER (@var{test-category-name})
23529 @atindex{BANNER}
23530 This macro identifies the start of a category of related test groups.
23531 When the resulting @file{testsuite} is invoked with more than one test
23532 group to run, its output will include a banner containing
23533 @var{test-category-name} prior to any tests run from that category.  The
23534 banner should be no more than about 40 or 50 characters.  A blank banner
23535 will not print, effectively ending a category and letting subsequent
23536 test groups behave as though they are uncategorized when run in
23537 isolation.
23538 @end defmac
23540 @defmac AT_SETUP (@var{test-group-name})
23541 @atindex{SETUP}
23542 This macro starts a group of related tests, all to be executed in the
23543 same subshell.  It accepts a single argument, which holds a few words
23544 (no more than about 30 or 40 characters) quickly describing the purpose
23545 of the test group being started.  @var{test-group-name} must not expand
23546 to unbalanced quotes, although quadrigraphs can be used.
23547 @end defmac
23549 @defmac AT_KEYWORDS (@var{keywords})
23550 @atindex{KEYWORDS}
23551 Associate the space-separated list of @var{keywords} to the enclosing
23552 test group.  This makes it possible to run ``slices'' of the test suite.
23553 For instance, if some of your test groups exercise some @samp{foo}
23554 feature, then using @samp{AT_KEYWORDS(foo)} lets you run
23555 @samp{./testsuite -k foo} to run exclusively these test groups.  The
23556 @var{test-group-name} of the test group is automatically recorded to
23557 @code{AT_KEYWORDS}.
23559 Several invocations within a test group accumulate new keywords.  In
23560 other words, don't fear registering the same keyword several times in a
23561 test group.
23562 @end defmac
23564 @defmac AT_CAPTURE_FILE (@var{file})
23565 @atindex{CAPTURE_FILE}
23566 If the current test group fails, log the contents of @var{file}.
23567 Several identical calls within one test group have no additional effect.
23568 @end defmac
23570 @defmac AT_FAIL_IF (@var{shell-condition})
23571 @atindex{FAIL_IF}
23572 Make the test group fail and skip the rest of its execution, if
23573 @var{shell-condition} is true.  @var{shell-condition} is a shell expression
23574 such as a @code{test} command.  Tests before @command{AT_FAIL_IF}
23575 will be executed and may still cause the test group to be skipped.
23576 You can instantiate this macro many times from within the same test group.
23578 You should use this macro only for very simple failure conditions.  If the
23579 @var{shell-condition} could emit any kind of output you should instead
23580 use @command{AT_CHECK} like
23581 @example
23582 AT_CHECK([if @var{shell-condition}; then exit 99; fi])
23583 @end example
23584 @noindent
23585 so that such output is properly recorded in the @file{testsuite.log}
23586 file.
23587 @end defmac
23589 @defmac AT_SKIP_IF (@var{shell-condition})
23590 @atindex{SKIP_IF}
23591 Determine whether the test should be skipped because it requires
23592 features that are unsupported on the machine under test.
23593 @var{shell-condition} is a shell expression such as a @code{test}
23594 command.  Tests before @command{AT_SKIP_IF} will be executed
23595 and may still cause the test group to fail.  You can instantiate this
23596 macro many times from within the same test group.
23598 You should use this macro only for very simple skip conditions.  If the
23599 @var{shell-condition} could emit any kind of output you should instead
23600 use @command{AT_CHECK} like
23601 @example
23602 AT_CHECK([if @var{shell-condition}; then exit 77; fi])
23603 @end example
23604 @noindent
23605 so that such output is properly recorded in the @file{testsuite.log}
23606 file.
23607 @end defmac
23609 @defmac AT_XFAIL_IF (@var{shell-condition})
23610 @atindex{XFAIL_IF}
23611 Determine whether the test is expected to fail because it is a known
23612 bug (for unsupported features, you should skip the test).
23613 @var{shell-condition} is a shell expression such as a @code{test}
23614 command; you can instantiate this macro many times from within the
23615 same test group, and one of the conditions is enough to turn
23616 the test into an expected failure.
23617 @end defmac
23619 @defmac AT_CLEANUP
23620 @atindex{CLEANUP}
23621 End the current test group.
23622 @end defmac
23624 @sp 1
23626 @defmac AT_DATA (@var{file}, @var{contents})
23627 @atindex{DATA}
23628 Initialize an input data @var{file} with given @var{contents}.  Of
23629 course, the @var{contents} have to be properly quoted between square
23630 brackets to protect against included commas or spurious M4
23631 expansion.  The contents must end with an end of line.  @var{file} must
23632 be a single shell word that expands into a single file name.
23633 @end defmac
23635 @defmac AT_CHECK (@var{commands}, @dvar{status, 0}, @ovar{stdout}, @
23636   @ovar{stderr}, @ovar{run-if-fail}, @ovar{run-if-pass})
23637 @defmacx AT_CHECK_UNQUOTED (@var{commands}, @dvar{status, 0}, @ovar{stdout}, @
23638   @ovar{stderr}, @ovar{run-if-fail}, @ovar{run-if-pass})
23639 @atindex{CHECK}
23640 @atindex{CHECK_UNQUOTED}
23641 Execute a test by performing given shell @var{commands}.  @var{commands}
23642 is output as-is, so shell expansions are honored.  These commands
23643 should normally exit with @var{status}, while producing expected
23644 @var{stdout} and @var{stderr} contents.  If @var{commands} exit with
23645 unexpected status 77, then the rest of the test group is skipped.  If
23646 @var{commands} exit with unexpected status 99, then the test group is
23647 immediately failed.  Otherwise, if this test
23648 fails, run shell commands @var{run-if-fail} or, if this test passes, run shell
23649 commands @var{run-if-pass}.
23651 This macro must be invoked in between @code{AT_SETUP} and @code{AT_CLEANUP}.
23653 If @var{status} is the literal @samp{ignore}, then the corresponding
23654 exit status is not checked, except for the special cases of 77 (skip)
23655 and 99 (hard failure).  The existence of hard failures allows one to
23656 mark a test as an expected failure with @code{AT_XFAIL_IF} because a
23657 feature has not yet been implemented, but to still distinguish between
23658 gracefully handling the missing feature and dumping core.  A hard
23659 failure also inhibits post-test actions in @var{run-if-fail}.
23661 If the value of the @var{stdout} or @var{stderr} parameter is one of the
23662 literals in the following table, then the test treats the output
23663 according to the rules of that literal.  Otherwise, the value of the
23664 parameter is treated as text that must exactly match the output given by
23665 @var{commands} on standard output and standard error (including an empty
23666 parameter for no output); any differences are captured in the testsuite
23667 log and the test is failed (unless an unexpected exit status of 77
23668 skipped the test instead).  The difference between @code{AT_CHECK} and
23669 @code{AT_CHECK_UNQUOTED} is that only the latter performs shell variable
23670 expansion (@samp{$}), command substitution (@samp{`}), and backslash
23671 escaping (@samp{\}) on comparison text given in the @var{stdout} and
23672 @var{stderr} arguments; if the text includes a trailing newline, this
23673 would be the same as if it were specified via an unquoted
23674 here-document.  (However, there is no difference in the interpretation
23675 of @var{commands}).
23677 @table @samp
23678 @item ignore
23679 The content of the output is ignored, but still captured in the test
23680 group log (if the testsuite is run with option @option{-v}, the test
23681 group log is displayed as the test is run; if the test group later
23682 fails, the test group log is also copied into the overall testsuite
23683 log).  This action is valid for both @var{stdout} and @var{stderr}.
23685 @item ignore-nolog
23686 The content of the output is ignored, and nothing is captured in the log
23687 files.  If @var{commands} are likely to produce binary output (including
23688 long lines) or large amounts of output, then logging the output can make
23689 it harder to locate details related to subsequent tests within the
23690 group, and could potentially corrupt terminal display of a user running
23691 @command{testsuite -v}.
23693 @item stdout
23694 For the @var{stdout} parameter, capture the content of standard output
23695 to both the file @file{stdout} and the test group log.  Subsequent
23696 commands in the test group can then post-process the file.  This action
23697 is often used when it is desired to use @command{grep} to look for a
23698 substring in the output, or when the output must be post-processed to
23699 normalize error messages into a common form.
23701 @item stderr
23702 Like @samp{stdout}, except that it only works for the @var{stderr}
23703 parameter, and the standard error capture file will be named
23704 @file{stderr}.
23706 @item stdout-nolog
23707 @itemx stderr-nolog
23708 Like @samp{stdout} or @samp{stderr}, except that the captured output is
23709 not duplicated into the test group log.  This action is particularly
23710 useful for an intermediate check that produces large amounts of data,
23711 which will be followed by another check that filters down to the
23712 relevant data, as it makes it easier to locate details in the log.
23714 @item expout
23715 For the @var{stdout} parameter, compare standard output contents with
23716 the previously created file @file{expout}, and list any differences in
23717 the testsuite log.
23719 @item experr
23720 Like @samp{expout}, except that it only works for the @var{stderr}
23721 parameter, and the standard error contents are compared with
23722 @file{experr}.
23723 @end table
23724 @end defmac
23726 @defmac AT_CHECK_EUNIT (@var{module}, @var{test-spec}, @ovar{erlflags}, @
23727   @ovar{run-if-fail}, @ovar{run-if-pass})
23728 @atindex{CHECK_EUNIT}
23729 Initialize and execute an Erlang module named @var{module} that performs
23730 tests following the @var{test-spec} EUnit test specification.
23731 @var{test-spec} must be a valid EUnit test specification, as defined in
23732 the @uref{http://@/erlang.org/@/doc/@/apps/@/eunit/@/index.html, EUnit
23733 Reference Manual}.  @var{erlflags} are optional command-line options
23734 passed to the Erlang interpreter to execute the test Erlang module.
23735 Typically, @var{erlflags} defines at least the paths to directories
23736 containing the compiled Erlang modules under test, as @samp{-pa path1
23737 path2 ...}.
23739 For example, the unit tests associated with Erlang module @samp{testme},
23740 which compiled code is in subdirectory @file{src}, can be performed
23741 with:
23743 @example
23744 AT_CHECK_EUNIT([testme_testsuite], [@{module, testme@}],
23745                [-pa "$@{abs_top_builddir@}/src"])
23746 @end example
23748 This macro must be invoked in between @code{AT_SETUP} and @code{AT_CLEANUP}.
23750 Variables @code{ERL}, @code{ERLC}, and (optionally) @code{ERLCFLAGS}
23751 must be defined as the path of the Erlang interpreter, the path of the
23752 Erlang compiler, and the command-line flags to pass to the compiler,
23753 respectively.  Those variables should be configured in
23754 @file{configure.ac} using the @command{AC_ERLANG_PATH_ERL} and
23755 @command{AC_ERLANG_PATH_ERLC} macros, and the configured values of those
23756 variables are automatically defined in the testsuite.  If @code{ERL} or
23757 @code{ERLC} is not defined, the test group is skipped.
23759 If the EUnit library cannot be found, i.e. if module @code{eunit} cannot
23760 be loaded, the test group is skipped.  Otherwise, if @var{test-spec} is
23761 an invalid EUnit test specification, the test group fails.  Otherwise,
23762 if the EUnit test passes, shell commands @var{run-if-pass} are executed
23763 or, if the EUnit test fails, shell commands @var{run-if-fail} are
23764 executed and the test group fails.
23766 Only the generated test Erlang module is automatically compiled and
23767 executed.  If @var{test-spec} involves testing other Erlang modules,
23768 e.g. module @samp{testme} in the example above, those modules must be
23769 already compiled.
23771 If the testsuite is run in verbose mode, with option @option{--verbose},
23772 EUnit is also run in verbose mode to output more details about
23773 individual unit tests.
23774 @end defmac
23777 @node testsuite Invocation
23778 @section Running @command{testsuite} Scripts
23779 @cindex @command{testsuite}
23781 Autotest test suites support the following options:
23783 @table @option
23784 @item --help
23785 @itemx -h
23786 Display the list of options and exit successfully.
23788 @item --version
23789 @itemx -V
23790 Display the version of the test suite and exit successfully.
23792 @item --directory=@var{dir}
23793 @itemx -C @var{dir}
23794 Change the current directory to @var{dir} before creating any files.
23795 Useful for running the testsuite in a subdirectory from a top-level
23796 Makefile.
23798 @item --jobs@r{[}=@var{n}@r{]}
23799 @itemx -j@ovar{n}
23800 Run @var{n} tests in parallel, if possible.  If @var{n} is not given,
23801 run all given tests in parallel.  Note that there should be no space
23802 before the argument to @option{-j}, as @option{-j @var{number}} denotes
23803 the separate arguments @option{-j} and @option{@var{number}}, see below.
23805 In parallel mode, the standard input device of the testsuite script is
23806 not available to commands inside a test group.  Furthermore, banner
23807 lines are not printed, and the summary line for each test group is
23808 output after the test group completes.  Summary lines may appear
23809 unordered.  If verbose and trace output are enabled (see below), they
23810 may appear intermixed from concurrently running tests.
23812 Parallel mode requires the @command{mkfifo} command to work, and will be
23813 silently disabled otherwise.
23815 @item --clean
23816 @itemx -c
23817 Remove all the files the test suite might have created and exit.  Meant
23818 for @code{clean} Make targets.
23820 @item --list
23821 @itemx -l
23822 List all the tests (or only the selection), including their possible
23823 keywords.
23824 @end table
23826 @sp 1
23828 By default all tests are performed (or described with @option{--list})
23829 silently in the default environment, but the environment, set of tests,
23830 and verbosity level can be tuned:
23832 @table @samp
23833 @item @var{variable}=@var{value}
23834 Set the environment @var{variable} to @var{value}.  Use this rather
23835 than @samp{FOO=foo ./testsuite} as debugging scripts would then run in a
23836 different environment.
23838 @cindex @code{AUTOTEST_PATH}
23839 The variable @code{AUTOTEST_PATH} specifies the testing path to prepend
23840 to @env{PATH}.  Relative directory names (not starting with
23841 @samp{/}) are considered to be relative to the top level of the
23842 package being built.  All directories are made absolute, first
23843 starting from the top level @emph{build} tree, then from the
23844 @emph{source} tree.  For instance @samp{./testsuite
23845 AUTOTEST_PATH=tests:bin} for a @file{/src/foo-1.0} source package built
23846 in @file{/tmp/foo} results in @samp{/tmp/foo/tests:/tmp/foo/bin} and
23847 then @samp{/src/foo-1.0/tests:/src/foo-1.0/bin} being prepended to
23848 @env{PATH}.
23850 @item @var{number}
23851 @itemx @var{number}-@var{number}
23852 @itemx @var{number}-
23853 @itemx -@var{number}
23854 Add the corresponding test groups, with obvious semantics, to the
23855 selection.
23857 @item --keywords=@var{keywords}
23858 @itemx -k @var{keywords}
23859 Add to the selection the test groups with title or keywords (arguments
23860 to @code{AT_SETUP} or @code{AT_KEYWORDS}) that match @emph{all} keywords
23861 of the comma separated list @var{keywords}, case-insensitively.  Use
23862 @samp{!} immediately before the keyword to invert the selection for this
23863 keyword.  By default, the keywords match whole words; enclose them in
23864 @samp{.*} to also match parts of words.
23866 For example, running
23868 @example
23869 @kbd{./testsuite -k 'autoupdate,.*FUNC.*'}
23870 @end example
23872 @noindent
23873 selects all tests tagged @samp{autoupdate} @emph{and} with tags
23874 containing @samp{FUNC} (as in @samp{AC_CHECK_FUNC}, @samp{AC_FUNC_ALLOCA},
23875 etc.), while
23877 @example
23878 @kbd{./testsuite -k '!autoupdate' -k '.*FUNC.*'}
23879 @end example
23881 @noindent
23882 selects all tests not tagged @samp{autoupdate} @emph{or} with tags
23883 containing @samp{FUNC}.
23885 @item --errexit
23886 @itemx -e
23887 If any test fails, immediately abort testing.  This implies
23888 @option{--debug}: post test group clean up, and top-level logging
23889 are inhibited.  This option is meant for the full test
23890 suite, it is not really useful for generated debugging scripts.
23891 If the testsuite is run in parallel mode using @option{--jobs},
23892 then concurrently running tests will finish before exiting.
23894 @item --verbose
23895 @itemx -v
23896 Force more verbosity in the detailed output of what is being done.  This
23897 is the default for debugging scripts.
23899 @item --color
23900 @itemx --color@r{[}=never@r{|}auto@r{|}always@r{]}
23901 Enable colored test results.  Without an argument, or with @samp{always},
23902 test results will be colored.  With @samp{never}, color mode is turned
23903 off.  Otherwise, if either the macro @code{AT_COLOR_TESTS} is used by
23904 the testsuite author, or the argument @samp{auto} is given, then test
23905 results are colored if standard output is connected to a terminal.
23907 @item --debug
23908 @itemx -d
23909 Do not remove the files after a test group was performed---but they are
23910 still removed @emph{before}, therefore using this option is sane when
23911 running several test groups.  Create debugging scripts.  Do not
23912 overwrite the top-level
23913 log (in order to preserve a supposedly existing full log file).  This is
23914 the default for debugging scripts, but it can also be useful to debug
23915 the testsuite itself.
23917 @item --recheck
23918 Add to the selection all test groups that failed or passed unexpectedly
23919 during the last non-debugging test run.
23921 @item --trace
23922 @itemx -x
23923 Trigger shell tracing of the test groups.
23924 @end table
23926 Besides these options accepted by every Autotest testsuite, the
23927 testsuite author might have added package-specific options
23928 via the @code{AT_ARG_OPTION} and @code{AT_ARG_OPTION_ARG} macros
23929 (@pxref{Writing Testsuites}); refer to @command{testsuite --help} and
23930 the package documentation for details.
23933 @node Making testsuite Scripts
23934 @section Making @command{testsuite} Scripts
23936 For putting Autotest into movement, you need some configuration and
23937 makefile machinery.  We recommend, at least if your package uses deep or
23938 shallow hierarchies, that you use @file{tests/} as the name of the
23939 directory holding all your tests and their makefile.  Here is a
23940 check list of things to do.
23942 @itemize @minus
23944 @item
23945 @cindex @file{package.m4}
23946 @atindex{PACKAGE_STRING}
23947 @atindex{PACKAGE_BUGREPORT}
23948 @atindex{PACKAGE_NAME}
23949 @atindex{PACKAGE_TARNAME}
23950 @atindex{PACKAGE_VERSION}
23951 @atindex{PACKAGE_URL}
23952 Make sure to create the file @file{package.m4}, which defines the
23953 identity of the package.  It must define @code{AT_PACKAGE_STRING}, the
23954 full signature of the package, and @code{AT_PACKAGE_BUGREPORT}, the
23955 address to which bug reports should be sent.  For sake of completeness,
23956 we suggest that you also define @code{AT_PACKAGE_NAME},
23957 @code{AT_PACKAGE_TARNAME}, @code{AT_PACKAGE_VERSION}, and
23958 @code{AT_PACKAGE_URL}.
23959 @xref{Initializing configure}, for a description of these variables.
23960 Be sure to distribute @file{package.m4} and to put it into the source
23961 hierarchy: the test suite ought to be shipped!  See below for an example
23962 @file{Makefile} excerpt.
23964 @item
23965 Invoke @code{AC_CONFIG_TESTDIR}.
23967 @defmac AC_CONFIG_TESTDIR (@var{directory}, @dvar{test-path, directory})
23968 @acindex{CONFIG_TESTDIR}
23969 An Autotest test suite is to be configured in @var{directory}.  This
23970 macro requires the instantiation of @file{@var{directory}/atconfig} from
23971 @file{@var{directory}/atconfig.in}, and sets the default
23972 @code{AUTOTEST_PATH} to @var{test-path} (@pxref{testsuite Invocation}).
23973 @end defmac
23975 @item
23976 Still within @file{configure.ac}, as appropriate, ensure that some
23977 @code{AC_CONFIG_FILES} command includes substitution for
23978 @file{tests/atlocal}.
23980 @item
23981 The appropriate @file{Makefile} should be modified so the validation in
23982 your package is triggered by @samp{make check}.  An example is provided
23983 below.
23984 @end itemize
23986 With Automake, here is a minimal example for inclusion in
23987 @file{tests/Makefile.am}, in order to link @samp{make check} with a
23988 validation suite.
23990 @example
23991 # The `:;' works around a Bash 3.2 bug when the output is not writeable.
23992 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
23993         :;@{ \
23994           echo '# Signature of the current package.' && \
23995           echo 'm4_define([AT_PACKAGE_NAME],' && \
23996           echo '  [$(PACKAGE_NAME)])' && \
23997           echo 'm4_define([AT_PACKAGE_TARNAME],' && \
23998           echo '  [$(PACKAGE_TARNAME)])' && \
23999           echo 'm4_define([AT_PACKAGE_VERSION],' && \
24000           echo '  [$(PACKAGE_VERSION)])' && \
24001           echo 'm4_define([AT_PACKAGE_STRING],' && \
24002           echo '  [$(PACKAGE_STRING)])' && \
24003           echo 'm4_define([AT_PACKAGE_BUGREPORT],' && \
24004           echo '  [$(PACKAGE_BUGREPORT)])'; \
24005           echo 'm4_define([AT_PACKAGE_URL],' && \
24006           echo '  [$(PACKAGE_URL)])'; \
24007         @} >'$(srcdir)/package.m4'
24009 EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) atlocal.in
24010 TESTSUITE = $(srcdir)/testsuite
24012 check-local: atconfig atlocal $(TESTSUITE)
24013         $(SHELL) '$(TESTSUITE)' $(TESTSUITEFLAGS)
24015 installcheck-local: atconfig atlocal $(TESTSUITE)
24016         $(SHELL) '$(TESTSUITE)' AUTOTEST_PATH='$(bindir)' \
24017           $(TESTSUITEFLAGS)
24019 clean-local:
24020         test ! -f '$(TESTSUITE)' || \
24021          $(SHELL) '$(TESTSUITE)' --clean
24023 AUTOM4TE = $(SHELL) $(srcdir)/build-aux/missing --run autom4te
24024 AUTOTEST = $(AUTOM4TE) --language=autotest
24025 $(TESTSUITE): $(srcdir)/testsuite.at $(srcdir)/package.m4
24026         $(AUTOTEST) -I '$(srcdir)' -o $@@.tmp $@@.at
24027         mv $@@.tmp $@@
24028 @end example
24030 Note that the built testsuite is distributed; this is necessary because
24031 users might not have Autoconf installed, and thus would not be able to
24032 rebuild it.  Likewise, the use of @file{missing} provides the user with
24033 a nicer error message if they modify a source file to the testsuite, and
24034 accidentally trigger the rebuild rules.
24036 You might want to list explicitly the dependencies, i.e., the list of
24037 the files @file{testsuite.at} includes.
24039 If you don't use Automake, you should include the above example in
24040 @file{tests/@/Makefile.in}, along with additional lines inspired from
24041 the following:
24043 @example
24044 subdir = tests
24045 PACKAGE_NAME = @@PACKAGE_NAME@@
24046 PACKAGE_TARNAME = @@PACKAGE_TARNAME@@
24047 PACKAGE_VERSION = @@PACKAGE_VERSION@@
24048 PACKAGE_STRING = @@PACKAGE_STRING@@
24049 PACKAGE_BUGREPORT = @@PACKAGE_BUGREPORT@@
24050 PACKAGE_URL = @@PACKAGE_URL@@
24052 atconfig: $(top_builddir)/config.status
24053         cd $(top_builddir) && \
24054            $(SHELL) ./config.status $(subdir)/$@@
24056 atlocal: $(srcdir)/atlocal.in $(top_builddir)/config.status
24057         cd $(top_builddir) && \
24058            $(SHELL) ./config.status $(subdir)/$@@
24059 @end example
24061 @noindent
24062 and manage to have @code{$(EXTRA_DIST)} distributed.  You will also want
24063 to distribute the file @file{build-aux/@/missing} from the Automake
24064 project; a copy of this file resides in the Autoconf source tree.
24066 With all this in place, and if you have not initialized @samp{TESTSUITEFLAGS}
24067 within your makefile, you can fine-tune test suite execution with this
24068 variable, for example:
24070 @example
24071 make check TESTSUITEFLAGS='-v -d -x 75 -k AC_PROG_CC CFLAGS=-g'
24072 @end example
24076 @c =============================== Frequent Autoconf Questions, with answers
24078 @node FAQ
24079 @chapter Frequent Autoconf Questions, with answers
24081 Several questions about Autoconf come up occasionally.  Here some of them
24082 are addressed.
24084 @menu
24085 * Distributing::                Distributing @command{configure} scripts
24086 * Why GNU M4::                  Why not use the standard M4?
24087 * Bootstrapping::               Autoconf and GNU M4 require each other?
24088 * Why Not Imake::               Why GNU uses @command{configure} instead of Imake
24089 * Defining Directories::        Passing @code{datadir} to program
24090 * Autom4te Cache::              What is it?  Can I remove it?
24091 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
24092 * Expanded Before Required::    Expanded Before Required
24093 * Debugging::                   Debugging @command{configure} scripts
24094 @end menu
24096 @node Distributing
24097 @section Distributing @command{configure} Scripts
24098 @cindex License
24100 @display
24101 What are the restrictions on distributing @command{configure}
24102 scripts that Autoconf generates?  How does that affect my
24103 programs that use them?
24104 @end display
24106 There are no restrictions on how the configuration scripts that Autoconf
24107 produces may be distributed or used.  In Autoconf version 1, they were
24108 covered by the GNU General Public License.  We still encourage
24109 software authors to distribute their work under terms like those of the
24110 GPL, but doing so is not required to use Autoconf.
24112 Of the other files that might be used with @command{configure},
24113 @file{config.h.in} is under whatever copyright you use for your
24114 @file{configure.ac}.  @file{config.sub} and @file{config.guess} have an
24115 exception to the GPL when they are used with an Autoconf-generated
24116 @command{configure} script, which permits you to distribute them under the
24117 same terms as the rest of your package.  @file{install-sh} is from the X
24118 Consortium and is not copyrighted.
24120 @node Why GNU M4
24121 @section Why Require GNU M4?
24123 @display
24124 Why does Autoconf require GNU M4?
24125 @end display
24127 Many M4 implementations have hard-coded limitations on the size and
24128 number of macros that Autoconf exceeds.  They also lack several
24129 builtin macros that it would be difficult to get along without in a
24130 sophisticated application like Autoconf, including:
24132 @example
24133 m4_builtin
24134 m4_indir
24135 m4_bpatsubst
24136 __file__
24137 __line__
24138 @end example
24140 Autoconf requires version 1.4.6 or later of GNU M4.
24142 Since only software maintainers need to use Autoconf, and since GNU
24143 M4 is simple to configure and install, it seems reasonable to require
24144 GNU M4 to be installed also.  Many maintainers of GNU and
24145 other free software already have most of the GNU utilities
24146 installed, since they prefer them.
24148 @node Bootstrapping
24149 @section How Can I Bootstrap?
24150 @cindex Bootstrap
24152 @display
24153 If Autoconf requires GNU M4 and GNU M4 has an Autoconf
24154 @command{configure} script, how do I bootstrap?  It seems like a chicken
24155 and egg problem!
24156 @end display
24158 This is a misunderstanding.  Although GNU M4 does come with a
24159 @command{configure} script produced by Autoconf, Autoconf is not required
24160 in order to run the script and install GNU M4.  Autoconf is only
24161 required if you want to change the M4 @command{configure} script, which few
24162 people have to do (mainly its maintainer).
24164 @node Why Not Imake
24165 @section Why Not Imake?
24166 @cindex Imake
24168 @display
24169 Why not use Imake instead of @command{configure} scripts?
24170 @end display
24172 Several people have written addressing this question, so I include
24173 adaptations of their explanations here.
24175 The following answer is based on one written by Richard Pixley:
24177 @quotation
24178 Autoconf generated scripts frequently work on machines that it has
24179 never been set up to handle before.  That is, it does a good job of
24180 inferring a configuration for a new system.  Imake cannot do this.
24182 Imake uses a common database of host specific data.  For X11, this makes
24183 sense because the distribution is made as a collection of tools, by one
24184 central authority who has control over the database.
24186 GNU tools are not released this way.  Each GNU tool has a
24187 maintainer; these maintainers are scattered across the world.  Using a
24188 common database would be a maintenance nightmare.  Autoconf may appear
24189 to be this kind of database, but in fact it is not.  Instead of listing
24190 host dependencies, it lists program requirements.
24192 If you view the GNU suite as a collection of native tools, then the
24193 problems are similar.  But the GNU development tools can be
24194 configured as cross tools in almost any host+target permutation.  All of
24195 these configurations can be installed concurrently.  They can even be
24196 configured to share host independent files across hosts.  Imake doesn't
24197 address these issues.
24199 Imake templates are a form of standardization.  The GNU coding
24200 standards address the same issues without necessarily imposing the same
24201 restrictions.
24202 @end quotation
24205 Here is some further explanation, written by Per Bothner:
24207 @quotation
24208 One of the advantages of Imake is that it easy to generate large
24209 makefiles using the @samp{#include} and macro mechanisms of @command{cpp}.
24210 However, @code{cpp} is not programmable: it has limited conditional
24211 facilities, and no looping.  And @code{cpp} cannot inspect its
24212 environment.
24214 All of these problems are solved by using @code{sh} instead of
24215 @code{cpp}.  The shell is fully programmable, has macro substitution,
24216 can execute (or source) other shell scripts, and can inspect its
24217 environment.
24218 @end quotation
24221 Paul Eggert elaborates more:
24223 @quotation
24224 With Autoconf, installers need not assume that Imake itself is already
24225 installed and working well.  This may not seem like much of an advantage
24226 to people who are accustomed to Imake.  But on many hosts Imake is not
24227 installed or the default installation is not working well, and requiring
24228 Imake to install a package hinders the acceptance of that package on
24229 those hosts.  For example, the Imake template and configuration files
24230 might not be installed properly on a host, or the Imake build procedure
24231 might wrongly assume that all source files are in one big directory
24232 tree, or the Imake configuration might assume one compiler whereas the
24233 package or the installer needs to use another, or there might be a
24234 version mismatch between the Imake expected by the package and the Imake
24235 supported by the host.  These problems are much rarer with Autoconf,
24236 where each package comes with its own independent configuration
24237 processor.
24239 Also, Imake often suffers from unexpected interactions between
24240 @command{make} and the installer's C preprocessor.  The fundamental problem
24241 here is that the C preprocessor was designed to preprocess C programs,
24242 not makefiles.  This is much less of a problem with Autoconf,
24243 which uses the general-purpose preprocessor M4, and where the
24244 package's author (rather than the installer) does the preprocessing in a
24245 standard way.
24246 @end quotation
24249 Finally, Mark Eichin notes:
24251 @quotation
24252 Imake isn't all that extensible, either.  In order to add new features to
24253 Imake, you need to provide your own project template, and duplicate most
24254 of the features of the existing one.  This means that for a sophisticated
24255 project, using the vendor-provided Imake templates fails to provide any
24256 leverage---since they don't cover anything that your own project needs
24257 (unless it is an X11 program).
24259 On the other side, though:
24261 The one advantage that Imake has over @command{configure}:
24262 @file{Imakefile} files tend to be much shorter (likewise, less redundant)
24263 than @file{Makefile.in} files.  There is a fix to this, however---at least
24264 for the Kerberos V5 tree, we've modified things to call in common
24265 @file{post.in} and @file{pre.in} makefile fragments for the
24266 entire tree.  This means that a lot of common things don't have to be
24267 duplicated, even though they normally are in @command{configure} setups.
24268 @end quotation
24271 @node Defining Directories
24272 @section How Do I @code{#define} Installation Directories?
24274 @display
24275 My program needs library files, installed in @code{datadir} and
24276 similar.  If I use
24278 @example
24279 AC_DEFINE_UNQUOTED([DATADIR], [$datadir],
24280   [Define to the read-only architecture-independent
24281    data directory.])
24282 @end example
24284 @noindent
24285 I get
24287 @example
24288 #define DATADIR "$@{prefix@}/share"
24289 @end example
24290 @end display
24292 As already explained, this behavior is on purpose, mandated by the
24293 GNU Coding Standards, see @ref{Installation Directory
24294 Variables}.  There are several means to achieve a similar goal:
24296 @itemize @minus
24297 @item
24298 Do not use @code{AC_DEFINE} but use your makefile to pass the
24299 actual value of @code{datadir} via compilation flags.
24300 @xref{Installation Directory Variables}, for the details.
24302 @item
24303 This solution can be simplified when compiling a program: you may either
24304 extend the @code{CPPFLAGS}:
24306 @example
24307 CPPFLAGS = -DDATADIR='"$(datadir)"' @@CPPFLAGS@@
24308 @end example
24310 @noindent
24311 If you are using Automake, you should use @code{AM_CPPFLAGS} instead:
24313 @example
24314 AM_CPPFLAGS = -DDATADIR='"$(datadir)"'
24315 @end example
24317 @noindent
24318 Alternatively, create a dedicated header file:
24320 @example
24321 DISTCLEANFILES = myprog-paths.h
24322 myprog-paths.h: Makefile
24323         echo '#define DATADIR "$(datadir)"' >$@@
24324 @end example
24326 @item
24327 Use @code{AC_DEFINE} but have @command{configure} compute the literal
24328 value of @code{datadir} and others.  Many people have wrapped macros to
24329 automate this task; for an example, see the macro @code{AC_DEFINE_DIR} from
24330 the @uref{http://@/www.gnu.org/@/software/@/autoconf-archive/, Autoconf Macro
24331 Archive}.
24333 This solution does not conform to the GNU Coding Standards.
24335 @item
24336 Note that all the previous solutions hard wire the absolute name of
24337 these directories in the executables, which is not a good property.  You
24338 may try to compute the names relative to @code{prefix}, and try to
24339 find @code{prefix} at runtime, this way your package is relocatable.
24340 @end itemize
24343 @node Autom4te Cache
24344 @section What is @file{autom4te.cache}?
24346 @display
24347 What is this directory @file{autom4te.cache}?  Can I safely remove it?
24348 @end display
24350 In the GNU Build System, @file{configure.ac} plays a central
24351 role and is read by many tools: @command{autoconf} to create
24352 @file{configure}, @command{autoheader} to create @file{config.h.in},
24353 @command{automake} to create @file{Makefile.in}, @command{autoscan} to
24354 check the completeness of @file{configure.ac}, @command{autoreconf} to
24355 check the GNU Build System components that are used.  To
24356 ``read @file{configure.ac}'' actually means to compile it with M4,
24357 which can be a long process for complex @file{configure.ac}.
24359 This is why all these tools, instead of running directly M4, invoke
24360 @command{autom4te} (@pxref{autom4te Invocation}) which, while answering to
24361 a specific demand, stores additional information in
24362 @file{autom4te.cache} for future runs.  For instance, if you run
24363 @command{autoconf}, behind the scenes, @command{autom4te} also
24364 stores information for the other tools, so that when you invoke
24365 @command{autoheader} or @command{automake} etc., reprocessing
24366 @file{configure.ac} is not needed.  The speed up is frequently 30%,
24367 and is increasing with the size of @file{configure.ac}.
24369 But it is and remains being simply a cache: you can safely remove it.
24371 @sp 1
24373 @display
24374 Can I permanently get rid of it?
24375 @end display
24377 The creation of this cache can be disabled from
24378 @file{~/.autom4te.cfg}, see @ref{Customizing autom4te}, for more
24379 details.  You should be aware that disabling the cache slows down the
24380 Autoconf test suite by 40%.  The more GNU Build System
24381 components are used, the more the cache is useful; for instance
24382 running @samp{autoreconf -f} on the Core Utilities is twice slower without
24383 the cache @emph{although @option{--force} implies that the cache is
24384 not fully exploited}, and eight times slower than without
24385 @option{--force}.
24388 @node Present But Cannot Be Compiled
24389 @section Header Present But Cannot Be Compiled
24391 The most important guideline to bear in mind when checking for
24392 features is to mimic as much as possible the intended use.
24393 Unfortunately, old versions of @code{AC_CHECK_HEADER} and
24394 @code{AC_CHECK_HEADERS} failed to follow this idea, and called
24395 the preprocessor, instead of the compiler, to check for headers.  As a
24396 result, incompatibilities between headers went unnoticed during
24397 configuration, and maintainers finally had to deal with this issue
24398 elsewhere.
24400 The transition began with Autoconf 2.56.  As of Autoconf 2.64 both
24401 checks are performed, and @command{configure} complains loudly if the
24402 compiler and the preprocessor do not agree.  However, only the compiler
24403 result is considered.
24405 Consider the following example:
24407 @smallexample
24408 $ @kbd{cat number.h}
24409 typedef int number;
24410 $ @kbd{cat pi.h}
24411 const number pi = 3;
24412 $ @kbd{cat configure.ac}
24413 AC_INIT([Example], [1.0], [bug-example@@example.org])
24414 AC_CHECK_HEADERS([pi.h])
24415 $ @kbd{autoconf -Wall}
24416 $ @kbd{./configure}
24417 checking for gcc... gcc
24418 checking for C compiler default output file name... a.out
24419 checking whether the C compiler works... yes
24420 checking whether we are cross compiling... no
24421 checking for suffix of executables...
24422 checking for suffix of object files... o
24423 checking whether we are using the GNU C compiler... yes
24424 checking whether gcc accepts -g... yes
24425 checking for gcc option to accept ISO C89... none needed
24426 checking how to run the C preprocessor... gcc -E
24427 checking for grep that handles long lines and -e... grep
24428 checking for egrep... grep -E
24429 checking for ANSI C header files... yes
24430 checking for sys/types.h... yes
24431 checking for sys/stat.h... yes
24432 checking for stdlib.h... yes
24433 checking for string.h... yes
24434 checking for memory.h... yes
24435 checking for strings.h... yes
24436 checking for inttypes.h... yes
24437 checking for stdint.h... yes
24438 checking for unistd.h... yes
24439 checking pi.h usability... no
24440 checking pi.h presence... yes
24441 configure: WARNING: pi.h: present but cannot be compiled
24442 configure: WARNING: pi.h:     check for missing prerequisite headers?
24443 configure: WARNING: pi.h: see the Autoconf documentation
24444 configure: WARNING: pi.h:     section "Present But Cannot Be Compiled"
24445 configure: WARNING: pi.h: proceeding with the compiler's result
24446 configure: WARNING:     ## -------------------------------------- ##
24447 configure: WARNING:     ## Report this to bug-example@@example.org ##
24448 configure: WARNING:     ## -------------------------------------- ##
24449 checking for pi.h... yes
24450 @end smallexample
24452 @noindent
24453 The proper way the handle this case is using the fourth argument
24454 (@pxref{Generic Headers}):
24456 @example
24457 $ @kbd{cat configure.ac}
24458 AC_INIT([Example], [1.0], [bug-example@@example.org])
24459 AC_CHECK_HEADERS([number.h pi.h], [], [],
24460 [[#ifdef HAVE_NUMBER_H
24461 # include <number.h>
24462 #endif
24464 $ @kbd{autoconf -Wall}
24465 $ @kbd{./configure}
24466 checking for gcc... gcc
24467 checking for C compiler default output... a.out
24468 checking whether the C compiler works... yes
24469 checking whether we are cross compiling... no
24470 checking for suffix of executables...
24471 checking for suffix of object files... o
24472 checking whether we are using the GNU C compiler... yes
24473 checking whether gcc accepts -g... yes
24474 checking for gcc option to accept ANSI C... none needed
24475 checking for number.h... yes
24476 checking for pi.h... yes
24477 @end example
24479 See @ref{Particular Headers}, for a list of headers with their
24480 prerequisites.
24482 @node Expanded Before Required
24483 @section Expanded Before Required
24485 @cindex expanded before required
24486 Older versions of Autoconf silently built files with incorrect ordering
24487 between dependent macros if an outer macro first expanded, then later
24488 indirectly required, an inner macro.  Starting with Autoconf 2.64, this
24489 situation no longer generates out-of-order code, but results in
24490 duplicate output and a syntax warning:
24492 @example
24493 $ @kbd{cat configure.ac}
24494 @result{}AC_DEFUN([TESTA], [[echo in A
24495 @result{}if test -n "$SEEN_A" ; then echo duplicate ; fi
24496 @result{}SEEN_A=:]])
24497 @result{}AC_DEFUN([TESTB], [AC_REQUIRE([TESTA])[echo in B
24498 @result{}if test -z "$SEEN_A" ; then echo bug ; fi]])
24499 @result{}AC_DEFUN([TESTC], [AC_REQUIRE([TESTB])[echo in C]])
24500 @result{}AC_DEFUN([OUTER], [[echo in OUTER]
24501 @result{}TESTA
24502 @result{}TESTC])
24503 @result{}AC_INIT
24504 @result{}OUTER
24505 @result{}AC_OUTPUT
24506 $ @kbd{autoconf}
24507 @result{}configure.ac:11: warning: AC_REQUIRE:
24508 @result{} `TESTA' was expanded before it was required
24509 @result{}configure.ac:4: TESTB is expanded from...
24510 @result{}configure.ac:6: TESTC is expanded from...
24511 @result{}configure.ac:7: OUTER is expanded from...
24512 @result{}configure.ac:11: the top level
24513 @end example
24515 @noindent
24516 To avoid this warning, decide what purpose the macro in question serves.
24517 If it only needs to be expanded once (for example, if it provides
24518 initialization text used by later macros), then the simplest fix is to
24519 change the macro to be declared with @code{AC_DEFUN_ONCE}
24520 (@pxref{One-Shot Macros}), although this only works in Autoconf 2.64 and
24521 newer.  A more portable fix is to change all
24522 instances of direct calls to instead go through @code{AC_REQUIRE}
24523 (@pxref{Prerequisite Macros}).  If, instead, the macro is parameterized
24524 by arguments or by the current definition of other macros in the m4
24525 environment, then the macro should always be directly expanded instead
24526 of required.
24528 For another case study, consider this example trimmed down from an
24529 actual package.  Originally, the package contained shell code and
24530 multiple macro invocations at the top level of @file{configure.ac}:
24532 @example
24533 AC_DEFUN([FOO], [AC_COMPILE_IFELSE([@dots{}])])
24534 foobar=
24535 AC_PROG_CC
24537 @end example
24539 @noindent
24540 but that was getting complex, so the author wanted to offload some of
24541 the text into a new macro in another file included via
24542 @file{aclocal.m4}.  The na@"ive approach merely wraps the text in a new
24543 macro:
24545 @example
24546 AC_DEFUN([FOO], [AC_COMPILE_IFELSE([@dots{}])])
24547 AC_DEFUN([BAR], [
24548 foobar=
24549 AC_PROG_CC
24553 @end example
24555 @noindent
24556 With older versions of Autoconf, the setting of @samp{foobar=} occurs
24557 before the single compiler check, as the author intended.  But with
24558 Autoconf 2.64, this issues the ``expanded before it was required''
24559 warning for @code{AC_PROG_CC}, and outputs two copies of the compiler
24560 check, one before @samp{foobar=}, and one after.  To understand why this
24561 is happening, remember that the use of @code{AC_COMPILE_IFELSE} includes
24562 a call to @code{AC_REQUIRE([AC_PROG_CC])} under the hood.  According to
24563 the documented semantics of @code{AC_REQUIRE}, this means that
24564 @code{AC_PROG_CC} @emph{must} occur before the body of the outermost
24565 @code{AC_DEFUN}, which in this case is @code{BAR}, thus preceeding the
24566 use of @samp{foobar=}.  The older versions of Autoconf were broken with
24567 regards to the rules of @code{AC_REQUIRE}, which explains why the code
24568 changed from one over to two copies of @code{AC_PROG_CC} when upgrading
24569 autoconf.  In other words, the author was unknowingly relying on a bug
24570 exploit to get the desired results, and that exploit broke once the bug
24571 was fixed.
24573 So, what recourse does the author have, to restore their intended
24574 semantics of setting @samp{foobar=} prior to a single compiler check,
24575 regardless of whether Autoconf 2.63 or 2.64 is used?  One idea is to
24576 remember that only @code{AC_DEFUN} is impacted by @code{AC_REQUIRE};
24577 there is always the possibility of using the lower-level
24578 @code{m4_define}:
24580 @example
24581 AC_DEFUN([FOO], [AC_COMPILE_IFELSE([@dots{}])])
24582 m4_define([BAR], [
24583 foobar=
24584 AC_PROG_CC
24588 @end example
24590 @noindent
24591 This works great if everything is in the same file.  However, it does
24592 not help in the case where the author wants to have @command{aclocal}
24593 find the definition of @code{BAR} from its own file, since
24594 @command{aclocal} requires the use of @code{AC_DEFUN}.  In this case, a
24595 better fix is to recognize that if @code{BAR} also uses
24596 @code{AC_REQUIRE}, then there will no longer be direct expansion prior
24597 to a subsequent require.  Then, by creating yet another helper macro,
24598 the author can once again guarantee a single invocation of
24599 @code{AC_PROG_CC}, which will still occur after @code{foobar=}.  The
24600 author can also use @code{AC_BEFORE} to make sure no other macro
24601 appearing before @code{BAR} has triggered an unwanted expansion of
24602 @code{AC_PROG_CC}.
24604 @example
24605 AC_DEFUN([FOO], [AC_COMPILE_IFELSE([@dots{}])])
24606 AC_DEFUN([BEFORE_CC], [
24607 foobar=
24609 AC_DEFUN([BAR], [
24610 AC_BEFORE([$0], [AC_PROG_CC])dnl
24611 AC_REQUIRE([BEFORE_CC])dnl
24612 AC_REQUIRE([AC_PROG_CC])dnl
24616 @end example
24619 @node Debugging
24620 @section Debugging @command{configure} scripts
24622 While in general, @command{configure} scripts generated by Autoconf
24623 strive to be fairly portable to various systems, compilers, shells, and
24624 other tools, it may still be necessary to debug a failing test, broken
24625 script or makefile, or fix or override an incomplete, faulty, or erroneous
24626 test, especially during macro development.  Failures can occur at all levels,
24627 in M4 syntax or semantics, shell script issues, or due to bugs in the
24628 test or the tools invoked by @command{configure}.  Together with the
24629 rather arcane error message that @command{m4} and @command{make} may
24630 produce when their input contains syntax errors, this can make debugging
24631 rather painful.
24633 Nevertheless, here is a list of hints and strategies that may help:
24635 @itemize
24636 @item
24637 When @command{autoconf} fails, common causes for error include:
24639 @itemize
24640 @item
24641 mismatched or unbalanced parentheses or braces (@pxref{Balancing
24642 Parentheses}),
24644 @item under- or overquoted macro arguments (@pxref{Autoconf
24645 Language}, @pxref{Quoting and Parameters}, @pxref{Quotation and Nested
24646 Macros}),
24648 @item spaces between macro name and opening parenthesis (@pxref{Autoconf
24649 Language}).
24650 @end itemize
24652 Typically, it helps to go back to the last working version of the input
24653 and compare the differences for each of these errors.  Another
24654 possibility is to sprinkle pairs of @code{m4_traceon} and
24655 @code{m4_traceoff} judiciously in the code, either without a parameter
24656 or listing some macro names and watch @command{m4} expand its input
24657 verbosely (@pxref{Debugging via autom4te}).
24659 @item
24660 Sometimes @command{autoconf} succeeds but the generated
24661 @command{configure} script has invalid shell syntax.  You can detect this
24662 case by running @samp{bash -n configure} or @samp{sh -n configure}.
24663 If this command fails, the same tips apply, as if @command{autoconf} had
24664 failed.
24666 @item
24667 Debugging @command{configure} script execution may be done by sprinkling
24668 pairs of @code{set -x} and @code{set +x} into the shell script before
24669 and after the region that contains a bug.  Running the whole script with
24670 @samp{@var{shell} ./configure -vx 2>&1 | tee @var{log-file}} with a decent
24671 @var{shell} may work, but produces lots of output.  Here, it can help to
24672 search for markers like @samp{checking for} a particular test in the
24673 @var{log-file}.
24675 @item
24676 When @command{configure} tests produce invalid results for your system,
24677 it may be necessary to override them:
24679 @itemize
24680 @item
24681 For programs, tools or libraries variables, preprocessor, compiler, or
24682 linker flags, it is often sufficient to override them at @command{make}
24683 run time with some care (@pxref{Macros and Submakes}).  Since this
24684 normally won't cause @command{configure} to be run again with these
24685 changed settings, it may fail if the changed variable would have caused
24686 different test results from @command{configure}, so this may work only
24687 for simple differences.
24689 @item
24690 Most tests which produce their result in a substituted variable allow to
24691 override the test by setting the variable on the @command{configure}
24692 command line (@pxref{Compilers and Options}, @pxref{Defining Variables},
24693 @pxref{Particular Systems}).
24695 @item
24696 Many tests store their result in a cache variable (@pxref{Caching
24697 Results}).  This lets you override them either on the
24698 @command{configure} command line as above, or through a primed cache or
24699 site file (@pxref{Cache Files}, @pxref{Site Defaults}).  The name of a
24700 cache variable is documented with a test macro or may be inferred from
24701 @ref{Cache Variable Names}; the precise semantics of undocumented
24702 variables are often internal details, subject to change.
24703 @end itemize
24705 @item
24706 Alternatively, @command{configure} may produce invalid results because
24707 of uncaught programming errors, in your package or in an upstream
24708 library package.  For example, when @code{AC_CHECK_LIB} fails to find a
24709 library with a specified function, always check @file{config.log}.  This
24710 will reveal the exact error that produced the failing result: the
24711 library linked by @code{AC_CHECK_LIB} probably has a fatal bug.
24712 @end itemize
24714 Conversely, as macro author, you can make it easier for users of your
24715 macro:
24717 @itemize
24718 @item
24719 by minimizing dependencies between tests and between test results as far
24720 as possible,
24722 @item
24723 by using @command{make} variables to factorize and allow
24724 override of settings at @command{make} run time,
24726 @item
24727 by honoring the GNU Coding Standards and not overriding flags
24728 reserved for the user except temporarily during @command{configure}
24729 tests,
24731 @item
24732 by not requiring users of your macro to use the cache variables.
24733 Instead, expose the result of the test via @var{run-if-true} and
24734 @var{run-if-false} parameters.  If the result is not a boolean,
24735 then provide it through documented shell variables.
24736 @end itemize
24739 @c ===================================================== History of Autoconf.
24741 @node History
24742 @chapter History of Autoconf
24743 @cindex History of autoconf
24745 You may be wondering, Why was Autoconf originally written?  How did it
24746 get into its present form?  (Why does it look like gorilla spit?)  If
24747 you're not wondering, then this chapter contains no information useful
24748 to you, and you might as well skip it.  If you @emph{are} wondering,
24749 then let there be light@enddots{}
24751 @menu
24752 * Genesis::                     Prehistory and naming of @command{configure}
24753 * Exodus::                      The plagues of M4 and Perl
24754 * Leviticus::                   The priestly code of portability arrives
24755 * Numbers::                     Growth and contributors
24756 * Deuteronomy::                 Approaching the promises of easy configuration
24757 @end menu
24759 @node Genesis
24760 @section Genesis
24762 In June 1991 I was maintaining many of the GNU utilities for the
24763 Free Software Foundation.  As they were ported to more platforms and
24764 more programs were added, the number of @option{-D} options that users
24765 had to select in the makefile (around 20) became burdensome.
24766 Especially for me---I had to test each new release on a bunch of
24767 different systems.  So I wrote a little shell script to guess some of
24768 the correct settings for the fileutils package, and released it as part
24769 of fileutils 2.0.  That @command{configure} script worked well enough that
24770 the next month I adapted it (by hand) to create similar @command{configure}
24771 scripts for several other GNU utilities packages.  Brian Berliner
24772 also adapted one of my scripts for his CVS revision control system.
24774 Later that summer, I learned that Richard Stallman and Richard Pixley
24775 were developing similar scripts to use in the GNU compiler tools;
24776 so I adapted my @command{configure} scripts to support their evolving
24777 interface: using the file name @file{Makefile.in} as the templates;
24778 adding @samp{+srcdir}, the first option (of many); and creating
24779 @file{config.status} files.
24781 @node Exodus
24782 @section Exodus
24784 As I got feedback from users, I incorporated many improvements, using
24785 Emacs to search and replace, cut and paste, similar changes in each of
24786 the scripts.  As I adapted more GNU utilities packages to use
24787 @command{configure} scripts, updating them all by hand became impractical.
24788 Rich Murphey, the maintainer of the GNU graphics utilities, sent me
24789 mail saying that the @command{configure} scripts were great, and asking if
24790 I had a tool for generating them that I could send him.  No, I thought,
24791 but I should!  So I started to work out how to generate them.  And the
24792 journey from the slavery of hand-written @command{configure} scripts to the
24793 abundance and ease of Autoconf began.
24795 Cygnus @command{configure}, which was being developed at around that time,
24796 is table driven; it is meant to deal mainly with a discrete number of
24797 system types with a small number of mainly unguessable features (such as
24798 details of the object file format).  The automatic configuration system
24799 that Brian Fox had developed for Bash takes a similar approach.  For
24800 general use, it seems to me a hopeless cause to try to maintain an
24801 up-to-date database of which features each variant of each operating
24802 system has.  It's easier and more reliable to check for most features on
24803 the fly---especially on hybrid systems that people have hacked on
24804 locally or that have patches from vendors installed.
24806 I considered using an architecture similar to that of Cygnus
24807 @command{configure}, where there is a single @command{configure} script that
24808 reads pieces of @file{configure.in} when run.  But I didn't want to have
24809 to distribute all of the feature tests with every package, so I settled
24810 on having a different @command{configure} made from each
24811 @file{configure.in} by a preprocessor.  That approach also offered more
24812 control and flexibility.
24814 I looked briefly into using the Metaconfig package, by Larry Wall,
24815 Harlan Stenn, and Raphael Manfredi, but I decided not to for several
24816 reasons.  The @command{Configure} scripts it produces are interactive,
24817 which I find quite inconvenient; I didn't like the ways it checked for
24818 some features (such as library functions); I didn't know that it was
24819 still being maintained, and the @command{Configure} scripts I had
24820 seen didn't work on many modern systems (such as System V R4 and NeXT);
24821 it wasn't flexible in what it could do in response to a feature's
24822 presence or absence; I found it confusing to learn; and it was too big
24823 and complex for my needs (I didn't realize then how much Autoconf would
24824 eventually have to grow).
24826 I considered using Perl to generate my style of @command{configure}
24827 scripts, but decided that M4 was better suited to the job of simple
24828 textual substitutions: it gets in the way less, because output is
24829 implicit.  Plus, everyone already has it.  (Initially I didn't rely on
24830 the GNU extensions to M4.)  Also, some of my friends at the
24831 University of Maryland had recently been putting M4 front ends on
24832 several programs, including @code{tvtwm}, and I was interested in trying
24833 out a new language.
24835 @node Leviticus
24836 @section Leviticus
24838 Since my @command{configure} scripts determine the system's capabilities
24839 automatically, with no interactive user intervention, I decided to call
24840 the program that generates them Autoconfig.  But with a version number
24841 tacked on, that name would be too long for old Unix file systems,
24842 so I shortened it to Autoconf.
24844 In the fall of 1991 I called together a group of fellow questers after
24845 the Holy Grail of portability (er, that is, alpha testers) to give me
24846 feedback as I encapsulated pieces of my handwritten scripts in M4 macros
24847 and continued to add features and improve the techniques used in the
24848 checks.  Prominent among the testers were Fran@,{c}ois Pinard, who came up
24849 with the idea of making an Autoconf shell script to run M4
24850 and check for unresolved macro calls; Richard Pixley, who suggested
24851 running the compiler instead of searching the file system to find
24852 include files and symbols, for more accurate results; Karl Berry, who
24853 got Autoconf to configure @TeX{} and added the macro index to the
24854 documentation; and Ian Lance Taylor, who added support for creating a C
24855 header file as an alternative to putting @option{-D} options in a
24856 makefile, so he could use Autoconf for his UUCP package.
24857 The alpha testers cheerfully adjusted their files again and again as the
24858 names and calling conventions of the Autoconf macros changed from
24859 release to release.  They all contributed many specific checks, great
24860 ideas, and bug fixes.
24862 @node Numbers
24863 @section Numbers
24865 In July 1992, after months of alpha testing, I released Autoconf 1.0,
24866 and converted many GNU packages to use it.  I was surprised by how
24867 positive the reaction to it was.  More people started using it than I
24868 could keep track of, including people working on software that wasn't
24869 part of the GNU Project (such as TCL, FSP, and Kerberos V5).
24870 Autoconf continued to improve rapidly, as many people using the
24871 @command{configure} scripts reported problems they encountered.
24873 Autoconf turned out to be a good torture test for M4 implementations.
24874 Unix M4 started to dump core because of the length of the
24875 macros that Autoconf defined, and several bugs showed up in GNU
24876 M4 as well.  Eventually, we realized that we needed to use some
24877 features that only GNU M4 has.  4.3BSD M4, in
24878 particular, has an impoverished set of builtin macros; the System V
24879 version is better, but still doesn't provide everything we need.
24881 More development occurred as people put Autoconf under more stresses
24882 (and to uses I hadn't anticipated).  Karl Berry added checks for X11.
24883 david zuhn contributed C++ support.  Fran@,{c}ois Pinard made it diagnose
24884 invalid arguments.  Jim Blandy bravely coerced it into configuring
24885 GNU Emacs, laying the groundwork for several later improvements.
24886 Roland McGrath got it to configure the GNU C Library, wrote the
24887 @command{autoheader} script to automate the creation of C header file
24888 templates, and added a @option{--verbose} option to @command{configure}.
24889 Noah Friedman added the @option{--autoconf-dir} option and
24890 @code{AC_MACRODIR} environment variable.  (He also coined the term
24891 @dfn{autoconfiscate} to mean ``adapt a software package to use
24892 Autoconf''.)  Roland and Noah improved the quoting protection in
24893 @code{AC_DEFINE} and fixed many bugs, especially when I got sick of
24894 dealing with portability problems from February through June, 1993.
24896 @node Deuteronomy
24897 @section Deuteronomy
24899 A long wish list for major features had accumulated, and the effect of
24900 several years of patching by various people had left some residual
24901 cruft.  In April 1994, while working for Cygnus Support, I began a major
24902 revision of Autoconf.  I added most of the features of the Cygnus
24903 @command{configure} that Autoconf had lacked, largely by adapting the
24904 relevant parts of Cygnus @command{configure} with the help of david zuhn
24905 and Ken Raeburn.  These features include support for using
24906 @file{config.sub}, @file{config.guess}, @option{--host}, and
24907 @option{--target}; making links to files; and running @command{configure}
24908 scripts in subdirectories.  Adding these features enabled Ken to convert
24909 GNU @code{as}, and Rob Savoye to convert DejaGNU, to using
24910 Autoconf.
24912 I added more features in response to other peoples' requests.  Many
24913 people had asked for @command{configure} scripts to share the results of
24914 the checks between runs, because (particularly when configuring a large
24915 source tree, like Cygnus does) they were frustratingly slow.  Mike
24916 Haertel suggested adding site-specific initialization scripts.  People
24917 distributing software that had to unpack on MS-DOS asked for a way to
24918 override the @file{.in} extension on the file names, which produced file
24919 names like @file{config.h.in} containing two dots.  Jim Avera did an
24920 extensive examination of the problems with quoting in @code{AC_DEFINE}
24921 and @code{AC_SUBST}; his insights led to significant improvements.
24922 Richard Stallman asked that compiler output be sent to @file{config.log}
24923 instead of @file{/dev/null}, to help people debug the Emacs
24924 @command{configure} script.
24926 I made some other changes because of my dissatisfaction with the quality
24927 of the program.  I made the messages showing results of the checks less
24928 ambiguous, always printing a result.  I regularized the names of the
24929 macros and cleaned up coding style inconsistencies.  I added some
24930 auxiliary utilities that I had developed to help convert source code
24931 packages to use Autoconf.  With the help of Fran@,{c}ois Pinard, I made
24932 the macros not interrupt each others' messages.  (That feature revealed
24933 some performance bottlenecks in GNU M4, which he hastily
24934 corrected!)  I reorganized the documentation around problems people want
24935 to solve.  And I began a test suite, because experience had shown that
24936 Autoconf has a pronounced tendency to regress when we change it.
24938 Again, several alpha testers gave invaluable feedback, especially
24939 Fran@,{c}ois Pinard, Jim Meyering, Karl Berry, Rob Savoye, Ken Raeburn,
24940 and Mark Eichin.
24942 Finally, version 2.0 was ready.  And there was much rejoicing.  (And I
24943 have free time again.  I think.  Yeah, right.)
24946 @c ========================================================== Appendices
24949 @node GNU Free Documentation License
24950 @appendix GNU Free Documentation License
24952 @include fdl.texi
24954 @node Indices
24955 @appendix Indices
24957 @menu
24958 * Environment Variable Index::  Index of environment variables used
24959 * Output Variable Index::       Index of variables set in output files
24960 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
24961 * Cache Variable Index::        Index of documented cache variables
24962 * Autoconf Macro Index::        Index of Autoconf macros
24963 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
24964 * Autotest Macro Index::        Index of Autotest macros
24965 * Program & Function Index::    Index of those with portability problems
24966 * Concept Index::               General index
24967 @end menu
24969 @node Environment Variable Index
24970 @appendixsec Environment Variable Index
24972 This is an alphabetical list of the environment variables that might
24973 influence Autoconf checks.
24975 @printindex ev
24977 @node Output Variable Index
24978 @appendixsec Output Variable Index
24980 This is an alphabetical list of the variables that Autoconf can
24981 substitute into files that it creates, typically one or more
24982 makefiles.  @xref{Setting Output Variables}, for more information
24983 on how this is done.
24985 @printindex ov
24987 @node Preprocessor Symbol Index
24988 @appendixsec Preprocessor Symbol Index
24990 This is an alphabetical list of the C preprocessor symbols that the
24991 Autoconf macros define.  To work with Autoconf, C source code needs to
24992 use these names in @code{#if} or @code{#ifdef} directives.
24994 @printindex cv
24996 @node Cache Variable Index
24997 @appendixsec Cache Variable Index
24999 This is an alphabetical list of documented cache variables used
25000 by macros defined in Autoconf.  Autoconf macros may use additional cache
25001 variables internally.
25002 @ifset shortindexflag
25003 To make the list easier to use, the variables are listed without their
25004 preceding @samp{ac_cv_}.
25005 @end ifset
25007 @printindex CA
25009 @node Autoconf Macro Index
25010 @appendixsec Autoconf Macro Index
25012 This is an alphabetical list of the Autoconf macros.
25013 @ifset shortindexflag
25014 To make the list easier to use, the macros are listed without their
25015 preceding @samp{AC_}.
25016 @end ifset
25018 @printindex AC
25020 @node M4 Macro Index
25021 @appendixsec M4 Macro Index
25023 This is an alphabetical list of the M4, M4sugar, and M4sh macros.
25024 @ifset shortindexflag
25025 To make the list easier to use, the macros are listed without their
25026 preceding @samp{m4_} or @samp{AS_}.
25027 @end ifset
25029 @printindex MS
25031 @node Autotest Macro Index
25032 @appendixsec Autotest Macro Index
25034 This is an alphabetical list of the Autotest macros.
25035 @ifset shortindexflag
25036 To make the list easier to use, the macros are listed without their
25037 preceding @samp{AT_}.
25038 @end ifset
25040 @printindex AT
25042 @node Program & Function Index
25043 @appendixsec Program and Function Index
25045 This is an alphabetical list of the programs and functions whose
25046 portability is discussed in this document.
25048 @printindex pr
25050 @node Concept Index
25051 @appendixsec Concept Index
25053 This is an alphabetical list of the files, tools, and concepts
25054 introduced in this document.
25056 @printindex cp
25058 @bye
25060 @c  LocalWords:  texinfo setfilename autoconf texi settitle setchapternewpage
25061 @c  LocalWords:  setcontentsaftertitlepage finalout ARG ovar varname dvar acx
25062 @c  LocalWords:  makeinfo dvi defcodeindex ev ov CPP cv Autotest mv defindex fn
25063 @c  LocalWords:  shortindexflag iftex ifset acindex ACindex ifclear ahindex fu
25064 @c  LocalWords:  asindex MSindex atindex ATindex auindex hdrindex prindex FIXME
25065 @c  LocalWords:  msindex alloca fnindex Aaarg indices FSF's dircategory ifnames
25066 @c  LocalWords:  direntry autoscan autoreconf autoheader autoupdate config FDs
25067 @c  LocalWords:  testsuite titlepage Elliston Demaille vskip filll ifnottex hmm
25068 @c  LocalWords:  insertcopying Autoconf's detailmenu Automake Libtool Posix ois
25069 @c  LocalWords:  Systemology Checkpointing Changequote INTERCAL changequote dfn
25070 @c  LocalWords:  Quadrigraphs builtins Shellology acconfig Bugward LIBOBJ Imake
25071 @c  LocalWords:  LIBOBJS IFELSE cindex flushright Pinard Metaconfig uref Simons
25072 @c  LocalWords:  distclean uninstall noindent versioning Tromey dir
25073 @c  LocalWords:  SAMS samp aclocal acsite underquoted emph itemx prepend SUBST
25074 @c  LocalWords:  evindex automake Gettext autopoint gettext symlink libtoolize
25075 @c  LocalWords:  defmac INIT tarname ovindex cvindex BUGREPORT PREREQ asis PROG
25076 @c  LocalWords:  SRCDIR srcdir globbing afterwards cmds foos fooo foooo init cd
25077 @c  LocalWords:  builddir timestamp src Imakefile chmod defvar CFLAGS CPPFLAGS
25078 @c  LocalWords:  CXXFLAGS DEFS DHAVE defvarx FCFLAGS FFLAGS LDFLAGS bindir GCC
25079 @c  LocalWords:  datadir datarootdir docdir dvidir htmldir libdir ifnothtml kbd
25080 @c  LocalWords:  includedir infodir libexecdir localedir localstatedir mandir
25081 @c  LocalWords:  oldincludedir pdfdir PDF psdir PostScript sbindir sysconfdir
25082 @c  LocalWords:  sharedstatedir DDATADIR sed tmp pkgdatadir VPATH conf unistd
25083 @c  LocalWords:  undef endif builtin FUNCS ifndef STACKSEG getb GETB YMP fubar
25084 @c  LocalWords:  PRE dest SUBDIRS subdirs fi struct STDC stdlib stddef INTTYPES
25085 @c  LocalWords:  inttypes STDINT stdint AWK AIX Solaris NeXT env EGREP FGREP yy
25086 @c  LocalWords:  LEXLIB YYTEXT lfl nonportable Automake's LN RANLIB byacc INETD
25087 @c  LocalWords:  inetd prog PROGS progs ranlib lmp lXt lX nsl gethostbyname UX
25088 @c  LocalWords:  NextStep isinf isnan glibc IRIX sunmath lm lsunmath pre sizeof
25089 @c  LocalWords:  ld inline malloc putenv setenv FreeBSD realloc SunOS MinGW
25090 @c  LocalWords:  snprintf vsnprintf sprintf vsprintf sscanf gcc strerror ifdef
25091 @c  LocalWords:  strnlen sysconf PAGESIZE unsetenv va fallback memcpy dst FUNC
25092 @c  LocalWords:  PowerPC GNUC libPW pragma Olibcalls CHOWN chown CLOSEDIR VFORK
25093 @c  LocalWords:  closedir FNMATCH fnmatch vfork FSEEKO LARGEFILE fseeko SVR sc
25094 @c  LocalWords:  largefile GETGROUPS getgroups GETLOADAVG DGUX UMAX NLIST KMEM
25095 @c  LocalWords:  SETGID getloadavg nlist GETMNTENT irix
25096 @c  LocalWords:  getmntent UnixWare GETPGRP getpgid getpgrp Posix's pid LSTAT
25097 @c  LocalWords:  lstat rpl MEMCMP memcmp OpenStep MBRTOWC mbrtowc MKTIME mktime
25098 @c  LocalWords:  localtime MMAP mmap OBSTACK obstack obstacks ARGTYPES timeval
25099 @c  LocalWords:  SETPGRP setpgrp defmacx Hurd SETVBUF setvbuf STRCOLL strcoll
25100 @c  LocalWords:  STRTOD strtod DECL STRFTIME strftime SCO UTIME utime VPRINTF
25101 @c  LocalWords:  DOPRNT vprintf doprnt sp unfixable LIBSOURCE LIBSOURCES Eggert
25102 @c  LocalWords:  linux netinet ia Tru XFree DIRENT NDIR dirent ndir multitable
25103 @c  LocalWords:  NAMLEN strlen namlen MKDEV SYSMACROS makedev RESOLV resolv DNS
25104 @c  LocalWords:  inet structs NAMESER arpa NETDB netdb UTekV UTS GCC's kB
25105 @c  LocalWords:  STDBOOL BOOL stdbool conformant cplusplus bool Bool stdarg tm
25106 @c  LocalWords:  ctype strchr strrchr rindex bcopy memmove memchr WEXITSTATUS
25107 @c  LocalWords:  WIFEXITED TIOCGWINSZ GWINSZ termios preprocess preprocessable
25108 @c  LocalWords:  DECLS strdup calloc BLKSIZE blksize RDEV rdev TZNAME tzname pw
25109 @c  LocalWords:  passwd gecos pwd MBSTATE mbstate wchar RETSIGTYPE hup UID uid
25110 @c  LocalWords:  gid ptrdiff uintmax EXEEXT OBJEXT Ae conftest AXP str
25111 @c  LocalWords:  ALIGNOF WERROR Werror cpp HP's WorkShop egcs un fied stdc CXX
25112 @c  LocalWords:  varargs BIGENDIAN Endianness SPARC endianness grep'ed CONST FC
25113 @c  LocalWords:  const STRINGIZE stringizing PARAMS unprotoize protos KCC cxx
25114 @c  LocalWords:  xlC aCC CXXCPP FREEFORM xlf FLIBS FCLIBS ish SRCEXT XTRA LFS
25115 @c  LocalWords:  ISC lcposix MINIX Minix conditionalized inlines hw dD confdefs
25116 @c  LocalWords:  fputs stdout PREPROC ar UFS HFS QNX realtime fstype STATVFS se
25117 @c  LocalWords:  statvfs STATFS statfs func machfile hdr lelf raboof DEFUN GTK
25118 @c  LocalWords:  GTKMM Grmph ified ine defn baz EOF qar Ahhh changecom algol io
25119 @c  LocalWords:  changeword quadrigraphs quadrigraph dnl SGI atoi overquoting
25120 @c  LocalWords:  Aas Wcross sep args namespace undefine bpatsubst popdef dquote
25121 @c  LocalWords:  bregexp Overquote overquotation meisch maisch meische maische
25122 @c  LocalWords:  miscian DIRNAME dirname MKDIR CATFILE XMKMF TRAVOLTA celsius
25123 @c  LocalWords:  EMX emxos Emacsen Korn DYNIX subshell posix Ksh ksh Pdksh Zsh
25124 @c  LocalWords:  pdksh zsh Allbery Lipe Kubota UWS zorglub stderr eval esac lfn
25125 @c  LocalWords:  drivespec Posixy DJGPP doschk prettybird LPT pfew Zsh's yu yaa
25126 @c  LocalWords:  yM uM aM firebird IP subdir misparses ok Unpatched abc bc zA
25127 @c  LocalWords:  CDPATH DUALCASE LINENO prepass Subshells lineno NULLCMD cmp wc
25128 @c  LocalWords:  MAILPATH scanset arg NetBSD Almquist printf expr cp
25129 @c  LocalWords:  Oliva awk Aaaaarg cmd regex xfoo GNV OpenVMS VM
25130 @c  LocalWords:  sparc Proulx nbar nfoo maxdepth acdilrtu TWG mc
25131 @c  LocalWords:  mkdir exe uname OpenBSD Fileutils mktemp umask TMPDIR guid os
25132 @c  LocalWords:  fooXXXXXX Unicos utimes hpux hppa unescaped
25133 @c  LocalWords:  pmake DOS's gmake ifoo DESTDIR autoconfiscated pc coff mips gg
25134 @c  LocalWords:  dec ultrix cpu wildcards rpcc rdtsc powerpc readline
25135 @c  LocalWords:  withval vxworks gless localcache usr LOFF loff CYGWIN Cygwin
25136 @c  LocalWords:  cygwin SIGLIST siglist SYSNDIR SYSDIR ptx lseq rusage elif MSC
25137 @c  LocalWords:  lfoo POUNDBANG lsun NIS getpwnam SYSCALLS RSH INTL lintl aix
25138 @c  LocalWords:  intl lx ldir syslog bsd EPI toolchain netbsd objext de KNR nn
25139 @c  LocalWords:  fication LTLIBOBJS Wdiff TESTDIR atconfig atlocal akim XFAIL
25140 @c  LocalWords:  ChangeLog prepended errexit smallexample TESTSUITEFLAGS GPL er
25141 @c  LocalWords:  installcheck autotest indir Pixley Bothner Eichin Kerberos adl
25142 @c  LocalWords:  DISTCLEANFILES preprocessor's fileutils Stallman Murphey Stenn
25143 @c  LocalWords:  Manfredi Autoconfig TCL FSP david zuhn Blandy MACRODIR Raeburn
25144 @c  LocalWords:  autoconfiscate Savoye Haertel Avera Meyering fdl appendixsec
25145 @c  LocalWords:  printindex american LIBOBJDIR LibdirTest ERLCFLAGS OBJCFLAGS
25146 @c  LocalWords:  VER Gnulib online xyes strcpy TYPEOF typeof OBJC objcc objc ln
25147 @c  LocalWords:  GOBJC OTP ERLC erl valloc decr dumpdef errprint incr
25148 @c  LocalWords:  esyscmd len maketemp pushdef substr syscmd sysval translit txt
25149 @c  LocalWords:  sinclude foreach myvar tolower toupper uniq BASENAME STDIN
25150 @c  LocalWords:  Dynix descrips basename aname cname macroexpands xno xcheck
25151 @c  LocalWords:  LIBREADLINE lreadline lncurses libreadline
25153 @c Local Variables:
25154 @c fill-column: 72
25155 @c ispell-local-dictionary: "american"
25156 @c indent-tabs-mode: nil
25157 @c whitespace-check-buffer-indent: nil
25158 @c End: