Merge branch 'master' of /home/eggert/src/gnu/autoconf-savannah/
[autoconf.git] / doc / autoconf.texi
blob369b555624ae710fd128591d93a0a4ed99455a75
1 \input texinfo @c -*-texinfo-*-
2 @comment ========================================================
3 @comment %**start of header
4 @setfilename autoconf.info
5 @include version.texi
6 @settitle Autoconf
7 @setchapternewpage odd
8 @ifnothtml
9 @setcontentsaftertitlepage
10 @end ifnothtml
11 @finalout
13 @c @ovar(ARG, DEFAULT)
14 @c -------------------
15 @c The ARG is an optional argument.  To be used for macro arguments in
16 @c their documentation (@defmac).
17 @macro ovar{varname}
18 @r{[}@var{\varname\}@r{]}
19 @end macro
21 @c @dvar(ARG, DEFAULT)
22 @c -------------------
23 @c The ARG is an optional argument, defaulting to DEFAULT.  To be used
24 @c for macro arguments in their documentation (@defmac).
25 @macro dvar{varname, default}
26 @r{[}@var{\varname\} = @samp{\default\}@r{]}
27 @end macro
29 @c Handling the indexes with Texinfo yields several different problems.
31 @c Because we want to drop out the AC_ part of the macro names in the
32 @c printed manual, but not in the other outputs, we need a layer above
33 @c the usual @acindex{} etc.  That's why we first define indexes such as
34 @c acx meant to become the macro @acindex.  First of all, using ``ac_''
35 @c does not work with makeinfo, and using ``ac1'' doesn't work with TeX.
36 @c So use something more regular ``acx''.  Then you finish with a printed
37 @c index saying ``index is not existent''.  Of course: you ought to use
38 @c two letters :(  So you use capitals.
40 @c Second, when defining a macro in the TeX world, following spaces are
41 @c eaten.  But then, since we embed @acxindex commands that use the end
42 @c of line as an end marker, the whole things wrecks itself.  So make
43 @c sure you do *force* an additional end of line, add a ``@c''.
45 @c Finally, you might want to get rid of TeX expansion, using --expand
46 @c with texi2dvi.  But then you wake up an old problem: we use macros
47 @c in @defmac etc. where TeX does perform the expansion, but not makeinfo.
49 @c Define an environment variable index.
50 @defcodeindex ev
51 @c Define an output variable index.
52 @defcodeindex ov
53 @c Define a CPP variable index.
54 @defcodeindex cv
55 @c Define an Autoconf macro index that @defmac doesn't write to.
56 @defcodeindex AC
57 @c Define an Autotest macro index that @defmac doesn't write to.
58 @defcodeindex AT
59 @c Define an M4sugar macro index that @defmac doesn't write to.
60 @defcodeindex MS
61 @c Define an index for *foreign* programs: `mv' etc.  Used for the
62 @c portability sections and so on.
63 @defindex pr
65 @c shortindexflag
66 @c --------------
67 @c Shall we factor AC_ out of the Autoconf macro index etc.?
68 @iftex
69 @set shortindexflag
70 @end iftex
72 @c @acindex{MACRO}
73 @c ---------------
74 @c Registering an AC_\MACRO\.
75 @ifset shortindexflag
76 @macro acindex{macro}
77 @ACindex \macro\
79 @end macro
80 @end ifset
81 @ifclear shortindexflag
82 @macro acindex{macro}
83 @ACindex AC_\macro\
84 @end macro
85 @end ifclear
87 @c @ahindex{MACRO}
88 @c ---------------
89 @c Registering an AH_\MACRO\.
90 @macro ahindex{macro}
91 @ACindex AH_\macro\
93 @end macro
95 @c @asindex{MACRO}
96 @c ---------------
97 @c Registering an AS_\MACRO\.
98 @ifset shortindexflag
99 @macro asindex{macro}
100 @MSindex \macro\
102 @end macro
103 @end ifset
104 @ifclear shortindexflag
105 @macro asindex{macro}
106 @MSindex AS_\macro\
107 @end macro
108 @end ifclear
110 @c @atindex{MACRO}
111 @c ---------------
112 @c Registering an AT_\MACRO\.
113 @ifset shortindexflag
114 @macro atindex{macro}
115 @ATindex \macro\
117 @end macro
118 @end ifset
119 @ifclear shortindexflag
120 @macro atindex{macro}
121 @ATindex AT_\macro\
122 @end macro
123 @end ifclear
125 @c @auindex{MACRO}
126 @c ---------------
127 @c Registering an AU_\MACRO\.
128 @macro auindex{macro}
129 @ACindex AU_\macro\
131 @end macro
133 @c @hdrindex{MACRO}
134 @c ----------------
135 @c Indexing a header.
136 @macro hdrindex{macro}
137 @prindex @file{\macro\}
139 @end macro
141 @c @msindex{MACRO}
142 @c ---------------
143 @c Registering an m4_\MACRO\.
144 @ifset shortindexflag
145 @macro msindex{macro}
146 @MSindex \macro\
148 @end macro
149 @end ifset
150 @ifclear shortindexflag
151 @macro msindex{macro}
152 @MSindex m4_\macro\
153 @end macro
154 @end ifclear
157 @c Define an index for functions: `alloca' etc.  Used for the
158 @c portability sections and so on.  We can't use `fn' (aka `fnindex),
159 @c since `@defmac' goes into it => we'd get all the macros too.
161 @c   FIXME: Aaarg!  It seems there are too many indices for TeX :(
163 @c   ! No room for a new @write .
164 @c   l.112 @defcodeindex fu
166 @c   so don't define yet another one :(  Just put some tags before each
167 @c   @prindex which is actually a @funindex.
169 @c   @defcodeindex fu
172 @c   @c Put the programs and functions into their own index.
173 @c   @syncodeindex fu pr
175 @comment %**end of header
176 @comment ========================================================
178 @copying
180 This manual is for @acronym{GNU} Autoconf
181 (version @value{VERSION}, @value{UPDATED}),
182 a package for creating scripts to configure source code packages using
183 templates and an M4 macro package.
185 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
186 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
188 @quotation
189 Permission is granted to copy, distribute and/or modify this document
190 under the terms of the @acronym{GNU} Free Documentation License,
191 Version 1.2 or any later version published by the Free Software
192 Foundation; with no Invariant Sections, with the Front-Cover texts
193 being ``A @acronym{GNU} Manual,'' and with the Back-Cover Texts as in
194 (a) below.  A copy of the license is included in the section entitled
195 ``@acronym{GNU} Free Documentation License.''
197 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and
198 modify this @acronym{GNU} Manual, like @acronym{GNU} software.  Copies
199 published by the Free Software Foundation raise funds for
200 @acronym{GNU} development.''
201 @end quotation
202 @end copying
206 @dircategory Software development
207 @direntry
208 * Autoconf: (autoconf).         Create source code configuration scripts.
209 @end direntry
211 @dircategory Individual utilities
212 @direntry
213 * autoscan: (autoconf)autoscan Invocation.
214                                 Semi-automatic @file{configure.ac} writing
215 * ifnames: (autoconf)ifnames Invocation.        Listing conditionals in source.
216 * autoconf-invocation: (autoconf)autoconf Invocation.
217                                 How to create configuration scripts
218 * autoreconf: (autoconf)autoreconf Invocation.
219                                 Remaking multiple @command{configure} scripts
220 * autoheader: (autoconf)autoheader Invocation.
221                                 How to create configuration templates
222 * autom4te: (autoconf)autom4te Invocation.
223                                 The Autoconf executables backbone
224 * configure: (autoconf)configure Invocation.    Configuring a package.
225 * autoupdate: (autoconf)autoupdate Invocation.
226                                 Automatic update of @file{configure.ac}
227 * config.status: (autoconf)config.status Invocation. Recreating configurations.
228 * testsuite: (autoconf)testsuite Invocation.    Running an Autotest test suite.
229 @end direntry
231 @titlepage
232 @title Autoconf
233 @subtitle Creating Automatic Configuration Scripts
234 @subtitle for version @value{VERSION}, @value{UPDATED}
235 @author David MacKenzie
236 @author Ben Elliston
237 @author Akim Demaille
238 @page
239 @vskip 0pt plus 1filll
240 @insertcopying
241 @end titlepage
243 @contents
246 @ifnottex
247 @node Top
248 @top Autoconf
249 @insertcopying
250 @end ifnottex
252 @c The master menu, created with texinfo-master-menu, goes here.
254 @menu
255 * Introduction::                Autoconf's purpose, strengths, and weaknesses
256 * The GNU Build System::        A set of tools for portable software packages
257 * Making configure Scripts::    How to organize and produce Autoconf scripts
258 * Setup::                       Initialization and output
259 * Existing Tests::              Macros that check for particular features
260 * Writing Tests::               How to write new feature checks
261 * Results::                     What to do with results from feature checks
262 * Programming in M4::           Layers on top of which Autoconf is written
263 * Writing Autoconf Macros::     Adding new macros to Autoconf
264 * Portable Shell::              Shell script portability pitfalls
265 * Portable Make::               Makefile portability pitfalls
266 * Portable C and C++::          C and C++ portability pitfalls
267 * Manual Configuration::        Selecting features that can't be guessed
268 * Site Configuration::          Local defaults for @command{configure}
269 * Running configure Scripts::   How to use the Autoconf output
270 * config.status Invocation::    Recreating a configuration
271 * Obsolete Constructs::         Kept for backward compatibility
272 * Using Autotest::              Creating portable test suites
273 * FAQ::                         Frequent Autoconf Questions, with answers
274 * History::                     History of Autoconf
275 * GNU Free Documentation License::  License for copying this manual
276 * Indices::                     Indices of symbols, concepts, etc.
278 @detailmenu
279  --- The Detailed Node Listing ---
281 The @acronym{GNU} Build System
283 * Automake::                    Escaping makefile hell
284 * Gnulib::                      The @acronym{GNU} portability library
285 * Libtool::                     Building libraries portably
286 * Pointers::                    More info on the @acronym{GNU} build system
288 Making @command{configure} Scripts
290 * Writing Autoconf Input::      What to put in an Autoconf input file
291 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
292 * ifnames Invocation::          Listing the conditionals in source code
293 * autoconf Invocation::         How to create configuration scripts
294 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
296 Writing @file{configure.ac}
298 * Shell Script Compiler::       Autoconf as solution of a problem
299 * Autoconf Language::           Programming in Autoconf
300 * Autoconf Input Layout::       Standard organization of @file{configure.ac}
302 Initialization and Output Files
304 * Initializing configure::      Option processing etc.
305 * Versioning::                  Dealing with Autoconf versions
306 * Notices::                     Copyright, version numbers in @command{configure}
307 * Input::                       Where Autoconf should find files
308 * Output::                      Outputting results from the configuration
309 * Configuration Actions::       Preparing the output based on results
310 * Configuration Files::         Creating output files
311 * Makefile Substitutions::      Using output variables in makefiles
312 * Configuration Headers::       Creating a configuration header file
313 * Configuration Commands::      Running arbitrary instantiation commands
314 * Configuration Links::         Links depending on the configuration
315 * Subdirectories::              Configuring independent packages together
316 * Default Prefix::              Changing the default installation prefix
318 Substitutions in Makefiles
320 * Preset Output Variables::     Output variables that are always set
321 * Installation Directory Variables::  Other preset output variables
322 * Changed Directory Variables:: Warnings about @file{datarootdir}
323 * Build Directories::           Supporting multiple concurrent compiles
324 * Automatic Remaking::          Makefile rules for configuring
326 Configuration Header Files
328 * Header Templates::            Input for the configuration headers
329 * autoheader Invocation::       How to create configuration templates
330 * Autoheader Macros::           How to specify CPP templates
332 Existing Tests
334 * Common Behavior::             Macros' standard schemes
335 * Alternative Programs::        Selecting between alternative programs
336 * Files::                       Checking for the existence of files
337 * Libraries::                   Library archives that might be missing
338 * Library Functions::           C library functions that might be missing
339 * Header Files::                Header files that might be missing
340 * Declarations::                Declarations that may be missing
341 * Structures::                  Structures or members that might be missing
342 * Types::                       Types that might be missing
343 * Compilers and Preprocessors::  Checking for compiling programs
344 * System Services::             Operating system services
345 * Posix Variants::              Special kludges for specific Posix variants
346 * Erlang Libraries::            Checking for the existence of Erlang libraries
348 Common Behavior
350 * Standard Symbols::            Symbols defined by the macros
351 * Default Includes::            Includes used by the generic macros
353 Alternative Programs
355 * Particular Programs::         Special handling to find certain programs
356 * Generic Programs::            How to find other programs
358 Library Functions
360 * Function Portability::        Pitfalls with usual functions
361 * Particular Functions::        Special handling to find certain functions
362 * Generic Functions::           How to find other functions
364 Header Files
366 * Header Portability::          Collected knowledge on common headers
367 * Particular Headers::          Special handling to find certain headers
368 * Generic Headers::             How to find other headers
370 Declarations
372 * Particular Declarations::     Macros to check for certain declarations
373 * Generic Declarations::        How to find other declarations
375 Structures
377 * Particular Structures::       Macros to check for certain structure members
378 * Generic Structures::          How to find other structure members
380 Types
382 * Particular Types::            Special handling to find certain types
383 * Generic Types::               How to find other types
385 Compilers and Preprocessors
387 * Specific Compiler Characteristics::  Some portability issues
388 * Generic Compiler Characteristics::  Language independent tests and features
389 * C Compiler::                  Checking its characteristics
390 * C++ Compiler::                Likewise
391 * Objective C Compiler::        Likewise
392 * Erlang Compiler and Interpreter::  Likewise
393 * Fortran Compiler::            Likewise
395 Writing Tests
397 * Language Choice::             Selecting which language to use for testing
398 * Writing Test Programs::       Forging source files for compilers
399 * Running the Preprocessor::    Detecting preprocessor symbols
400 * Running the Compiler::        Detecting language or header features
401 * Running the Linker::          Detecting library features
402 * Runtime::                     Testing for runtime features
403 * Systemology::                 A zoology of operating systems
404 * Multiple Cases::              Tests for several possible values
406 Writing Test Programs
408 * Guidelines::                  General rules for writing test programs
409 * Test Functions::              Avoiding pitfalls in test programs
410 * Generating Sources::          Source program boilerplate
412 Results of Tests
414 * Defining Symbols::            Defining C preprocessor symbols
415 * Setting Output Variables::    Replacing variables in output files
416 * Special Chars in Variables::  Characters to beware of in variables
417 * Caching Results::             Speeding up subsequent @command{configure} runs
418 * Printing Messages::           Notifying @command{configure} users
420 Caching Results
422 * Cache Variable Names::        Shell variables used in caches
423 * Cache Files::                 Files @command{configure} uses for caching
424 * Cache Checkpointing::         Loading and saving the cache file
426 Programming in M4
428 * M4 Quotation::                Protecting macros from unwanted expansion
429 * Using autom4te::              The Autoconf executables backbone
430 * Programming in M4sugar::      Convenient pure M4 macros
431 * Programming in M4sh::         Common shell Constructs
432 * File Descriptor Macros::      File descriptor macros for input and output
434 M4 Quotation
436 * Active Characters::           Characters that change the behavior of M4
437 * One Macro Call::              Quotation and one macro call
438 * Quoting and Parameters::      M4 vs. shell parameters
439 * Quotation and Nested Macros::  Macros calling macros
440 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
441 * Quadrigraphs::                Another way to escape special characters
442 * Quotation Rule Of Thumb::     One parenthesis, one quote
444 Using @command{autom4te}
446 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
447 * Customizing autom4te::        Customizing the Autoconf package
449 Programming in M4sugar
451 * Redefined M4 Macros::         M4 builtins changed in M4sugar
452 * Diagnostic Macros::           Diagnostic messages from M4sugar
453 * Diversion support::           Diversions in M4sugar
454 * Conditional constructs::      Conditions in M4
455 * Looping constructs::          Iteration in M4
456 * Evaluation Macros::           More quotation and evaluation control
457 * Text processing Macros::      String manipulation in M4
458 * Number processing Macros::    Arithmetic computation in M4
459 * Forbidden Patterns::          Catching unexpanded macros
461 Writing Autoconf Macros
463 * Macro Definitions::           Basic format of an Autoconf macro
464 * Macro Names::                 What to call your new macros
465 * Reporting Messages::          Notifying @command{autoconf} users
466 * Dependencies Between Macros::  What to do when macros depend on other macros
467 * Obsoleting Macros::           Warning about old ways of doing things
468 * Coding Style::                Writing Autoconf macros @`a la Autoconf
470 Dependencies Between Macros
472 * Prerequisite Macros::         Ensuring required information
473 * Suggested Ordering::          Warning about possible ordering problems
474 * One-Shot Macros::             Ensuring a macro is called only once
476 Portable Shell Programming
478 * Shellology::                  A zoology of shells
479 * Here-Documents::              Quirks and tricks
480 * File Descriptors::            FDs and redirections
481 * File System Conventions::     File names
482 * Shell Pattern Matching::      Pattern matching
483 * Shell Substitutions::         Variable and command expansions
484 * Assignments::                 Varying side effects of assignments
485 * Parentheses::                 Parentheses in shell scripts
486 * Slashes::                     Slashes in shell scripts
487 * Special Shell Variables::     Variables you should not change
488 * Limitations of Builtins::     Portable use of not so portable /bin/sh
489 * Limitations of Usual Tools::  Portable use of portable tools
491 Portable Make Programming
493 * $< in Ordinary Make Rules::   $< in ordinary rules
494 * Failure in Make Rules::       Failing portably in rules
495 * Special Chars in Names::      Special Characters in Macro Names
496 * Backslash-Newline-Newline::   Empty last lines in macro definitions
497 * Backslash-Newline Comments::  Spanning comments across line boundaries
498 * Long Lines in Makefiles::     Line length limitations
499 * Macros and Submakes::         @code{make macro=value} and submakes
500 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
501 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
502 * Comments in Make Rules::      Other problems with Make comments
503 * obj/ and Make::               Don't name a subdirectory @file{obj}
504 * make -k Status::              Exit status of @samp{make -k}
505 * VPATH and Make::              @code{VPATH} woes
506 * Single Suffix Rules::         Single suffix rules and separated dependencies
507 * Timestamps and Make::         Subsecond timestamp resolution
509 @code{VPATH} and Make
511 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
512 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
513 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
514 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
515 * Make Target Lookup::          More details about @code{VPATH} lookup
517 Portable C and C++ Programming
519 * Varieties of Unportability::  How to make your programs unportable
520 * Integer Overflow::            When integers get too large
521 * Null Pointers::               Properties of null pointers
522 * Buffer Overruns::             Subscript errors and the like
523 * Volatile Objects::            @code{volatile} and signals
524 * Floating Point Portability::  Portable floating-point arithmetic
525 * Exiting Portably::            Exiting and the exit status
527 Manual Configuration
529 * Specifying Names::            Specifying the system type
530 * Canonicalizing::              Getting the canonical system type
531 * Using System Type::           What to do with the system type
533 Site Configuration
535 * Help Formatting::             Customizing @samp{configure --help}
536 * External Software::           Working with other optional software
537 * Package Options::             Selecting optional features
538 * Pretty Help Strings::         Formatting help string
539 * Option Checking::             Controlling checking of @command{configure} options
540 * Site Details::                Configuring site details
541 * Transforming Names::          Changing program names when installing
542 * Site Defaults::               Giving @command{configure} local defaults
544 Transforming Program Names When Installing
546 * Transformation Options::      @command{configure} options to transform names
547 * Transformation Examples::     Sample uses of transforming names
548 * Transformation Rules::        Makefile uses of transforming names
550 Running @command{configure} Scripts
552 * Basic Installation::          Instructions for typical cases
553 * Compilers and Options::       Selecting compilers and optimization
554 * Multiple Architectures::      Compiling for multiple architectures at once
555 * Installation Names::          Installing in different directories
556 * Optional Features::           Selecting optional features
557 * System Type::                 Specifying the system type
558 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
559 * Defining Variables::          Specifying the compiler etc.
560 * configure Invocation::        Changing how @command{configure} runs
562 Obsolete Constructs
564 * Obsolete config.status Use::  Obsolete convention for @command{config.status}
565 * acconfig Header::             Additional entries in @file{config.h.in}
566 * autoupdate Invocation::       Automatic update of @file{configure.ac}
567 * Obsolete Macros::             Backward compatibility macros
568 * Autoconf 1::                  Tips for upgrading your files
569 * Autoconf 2.13::               Some fresher tips
571 Upgrading From Version 1
573 * Changed File Names::          Files you might rename
574 * Changed Makefiles::           New things to put in @file{Makefile.in}
575 * Changed Macros::              Macro calls you might replace
576 * Changed Results::             Changes in how to check test results
577 * Changed Macro Writing::       Better ways to write your own macros
579 Upgrading From Version 2.13
581 * Changed Quotation::           Broken code which used to work
582 * New Macros::                  Interaction with foreign macros
583 * Hosts and Cross-Compilation::  Bugward compatibility kludges
584 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
585 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
587 Generating Test Suites with Autotest
589 * Using an Autotest Test Suite::  Autotest and the user
590 * Writing Testsuites::          Autotest macros
591 * testsuite Invocation::        Running @command{testsuite} scripts
592 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
594 Using an Autotest Test Suite
596 * testsuite Scripts::           The concepts of Autotest
597 * Autotest Logs::               Their contents
599 Frequent Autoconf Questions, with answers
601 * Distributing::                Distributing @command{configure} scripts
602 * Why GNU M4::                  Why not use the standard M4?
603 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
604 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
605 * Defining Directories::        Passing @code{datadir} to program
606 * Autom4te Cache::              What is it?  Can I remove it?
607 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
609 History of Autoconf
611 * Genesis::                     Prehistory and naming of @command{configure}
612 * Exodus::                      The plagues of M4 and Perl
613 * Leviticus::                   The priestly code of portability arrives
614 * Numbers::                     Growth and contributors
615 * Deuteronomy::                 Approaching the promises of easy configuration
617 Indices
619 * Environment Variable Index::  Index of environment variables used
620 * Output Variable Index::       Index of variables set in output files
621 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
622 * Autoconf Macro Index::        Index of Autoconf macros
623 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
624 * Autotest Macro Index::        Index of Autotest macros
625 * Program & Function Index::    Index of those with portability problems
626 * Concept Index::               General index
628 @end detailmenu
629 @end menu
631 @c ============================================================= Introduction.
633 @node Introduction
634 @chapter Introduction
635 @cindex Introduction
637 @flushright
638 A physicist, an engineer, and a computer scientist were discussing the
639 nature of God.  ``Surely a Physicist,'' said the physicist, ``because
640 early in the Creation, God made Light; and you know, Maxwell's
641 equations, the dual nature of electromagnetic waves, the relativistic
642 consequences@dots{}'' ``An Engineer!,'' said the engineer, ``because
643 before making Light, God split the Chaos into Land and Water; it takes a
644 hell of an engineer to handle that big amount of mud, and orderly
645 separation of solids from liquids@dots{}'' The computer scientist
646 shouted: ``And the Chaos, where do you think it was coming from, hmm?''
648 ---Anonymous
649 @end flushright
650 @c (via Franc,ois Pinard)
652 Autoconf is a tool for producing shell scripts that automatically
653 configure software source code packages to adapt to many kinds of
654 Posix-like systems.  The configuration scripts produced by Autoconf
655 are independent of Autoconf when they are run, so their users do not
656 need to have Autoconf.
658 The configuration scripts produced by Autoconf require no manual user
659 intervention when run; they do not normally even need an argument
660 specifying the system type.  Instead, they individually test for the
661 presence of each feature that the software package they are for might need.
662 (Before each check, they print a one-line message stating what they are
663 checking for, so the user doesn't get too bored while waiting for the
664 script to finish.)  As a result, they deal well with systems that are
665 hybrids or customized from the more common Posix variants.  There is
666 no need to maintain files that list the features supported by each
667 release of each variant of Posix.
669 For each software package that Autoconf is used with, it creates a
670 configuration script from a template file that lists the system features
671 that the package needs or can use.  After the shell code to recognize
672 and respond to a system feature has been written, Autoconf allows it to
673 be shared by many software packages that can use (or need) that feature.
674 If it later turns out that the shell code needs adjustment for some
675 reason, it needs to be changed in only one place; all of the
676 configuration scripts can be regenerated automatically to take advantage
677 of the updated code.
679 The Metaconfig package is similar in purpose to Autoconf, but the
680 scripts it produces require manual user intervention, which is quite
681 inconvenient when configuring large source trees.  Unlike Metaconfig
682 scripts, Autoconf scripts can support cross-compiling, if some care is
683 taken in writing them.
685 Autoconf does not solve all problems related to making portable
686 software packages---for a more complete solution, it should be used in
687 concert with other @acronym{GNU} build tools like Automake and
688 Libtool.  These other tools take on jobs like the creation of a
689 portable, recursive makefile with all of the standard targets,
690 linking of shared libraries, and so on.  @xref{The GNU Build System},
691 for more information.
693 Autoconf imposes some restrictions on the names of macros used with
694 @code{#if} in C programs (@pxref{Preprocessor Symbol Index}).
696 Autoconf requires @acronym{GNU} M4 version 1.4.5 or later in order to
697 generate the scripts.  It uses features that some versions of M4,
698 including @acronym{GNU} M4 1.3, do not have.  Autoconf works better
699 with @acronym{GNU} M4 version 1.4.8 or later, though this is not
700 required.
702 @xref{Autoconf 1}, for information about upgrading from version 1.
703 @xref{History}, for the story of Autoconf's development.  @xref{FAQ},
704 for answers to some common questions about Autoconf.
706 See the @uref{http://www.gnu.org/software/autoconf/,
707 Autoconf web page} for up-to-date information, details on the mailing
708 lists, pointers to a list of known bugs, etc.
710 Mail suggestions to @email{autoconf@@gnu.org, the Autoconf mailing
711 list}.  Past suggestions are
712 @uref{http://lists.gnu.org/archive/html/autoconf/, archived}.
714 Mail bug reports to @email{bug-autoconf@@gnu.org, the
715 Autoconf Bugs mailing list}.  Past bug reports are
716 @uref{http://lists.gnu.org/archive/html/bug-autoconf/, archived}.
718 If possible, first check that your bug is
719 not already solved in current development versions, and that it has not
720 been reported yet.  Be sure to include all the needed information and a
721 short @file{configure.ac} that demonstrates the problem.
723 Autoconf's development tree is accessible via anonymous @acronym{CVS}; see the
724 @uref{http://savannah.gnu.org/projects/autoconf/, Autoconf
725 Summary} for details.  Patches relative to the
726 current @acronym{CVS} version can be sent for review to the
727 @email{autoconf-patches@@gnu.org, Autoconf Patches mailing list}.
728 Past patches are
729 @uref{http://lists.gnu.org/@/archive/@/html/@/autoconf-patches/, archived}.
731 Because of its mission, the Autoconf package itself
732 includes only a set of often-used
733 macros that have already demonstrated their usefulness.  Nevertheless,
734 if you wish to share your macros, or find existing ones, see the
735 @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
736 Archive}, which is kindly run by @email{simons@@cryp.to,
737 Peter Simons}.
740 @c ================================================= The GNU Build System
742 @node The GNU Build System
743 @chapter The @acronym{GNU} Build System
744 @cindex @acronym{GNU} build system
746 Autoconf solves an important problem---reliable discovery of
747 system-specific build and runtime information---but this is only one
748 piece of the puzzle for the development of portable software.  To this
749 end, the @acronym{GNU} project has developed a suite of integrated
750 utilities to finish the job Autoconf started: the @acronym{GNU} build
751 system, whose most important components are Autoconf, Automake, and
752 Libtool.  In this chapter, we introduce you to those tools, point you
753 to sources of more information, and try to convince you to use the
754 entire @acronym{GNU} build system for your software.
756 @menu
757 * Automake::                    Escaping makefile hell
758 * Gnulib::                      The @acronym{GNU} portability library
759 * Libtool::                     Building libraries portably
760 * Pointers::                    More info on the @acronym{GNU} build system
761 @end menu
763 @node Automake
764 @section Automake
766 The ubiquity of @command{make} means that a makefile is almost the
767 only viable way to distribute automatic build rules for software, but
768 one quickly runs into its numerous limitations.  Its lack of
769 support for automatic dependency tracking, recursive builds in
770 subdirectories, reliable timestamps (e.g., for network file systems), and
771 so on, mean that developers must painfully (and often incorrectly)
772 reinvent the wheel for each project.  Portability is non-trivial, thanks
773 to the quirks of @command{make} on many systems.  On top of all this is the
774 manual labor required to implement the many standard targets that users
775 have come to expect (@code{make install}, @code{make distclean},
776 @code{make uninstall}, etc.).  Since you are, of course, using Autoconf,
777 you also have to insert repetitive code in your @code{Makefile.in} to
778 recognize @code{@@CC@@}, @code{@@CFLAGS@@}, and other substitutions
779 provided by @command{configure}.  Into this mess steps @dfn{Automake}.
780 @cindex Automake
782 Automake allows you to specify your build needs in a @code{Makefile.am}
783 file with a vastly simpler and more powerful syntax than that of a plain
784 makefile, and then generates a portable @code{Makefile.in} for
785 use with Autoconf.  For example, the @code{Makefile.am} to build and
786 install a simple ``Hello world'' program might look like:
788 @example
789 bin_PROGRAMS = hello
790 hello_SOURCES = hello.c
791 @end example
793 @noindent
794 The resulting @code{Makefile.in} (~400 lines) automatically supports all
795 the standard targets, the substitutions provided by Autoconf, automatic
796 dependency tracking, @code{VPATH} building, and so on.  @command{make}
797 builds the @code{hello} program, and @code{make install} installs it
798 in @file{/usr/local/bin} (or whatever prefix was given to
799 @command{configure}, if not @file{/usr/local}).
801 The benefits of Automake increase for larger packages (especially ones
802 with subdirectories), but even for small programs the added convenience
803 and portability can be substantial.  And that's not all@enddots{}
805 @node Gnulib
806 @section Gnulib
808 @acronym{GNU} software has a well-deserved reputation for running on
809 many different types of systems.  While our primary goal is to write
810 software for the @acronym{GNU} system, many users and developers have
811 been introduced to us through the systems that they were already using.
813 @cindex Gnulib
814 Gnulib is a central location for common @acronym{GNU} code, intended to
815 be shared among free software packages.  Its components are typically
816 shared at the source level, rather than being a library that gets built,
817 installed, and linked against.  The idea is to copy files from Gnulib
818 into your own source tree.  There is no distribution tarball; developers
819 should just grab source modules from the repository.  The source files
820 are available online, under various licenses, mostly @acronym{GNU}
821 @acronym{GPL} or @acronym{GNU} @acronym{LGPL}.
823 Gnulib modules typically contain C source code along with Autoconf
824 macros used to configure the source code.  For example, the Gnulib
825 @code{stdbool} module implements a @file{stdbool.h} header that nearly
826 conforms to C99, even on old-fashioned hosts that lack @file{stdbool.h}.
827 This module contains a source file for the replacement header, along
828 with an Autoconf macro that arranges to use the replacement header on
829 old-fashioned systems.
831 @node Libtool
832 @section Libtool
834 Often, one wants to build not only programs, but libraries, so that
835 other programs can benefit from the fruits of your labor.  Ideally, one
836 would like to produce @emph{shared} (dynamically linked) libraries,
837 which can be used by multiple programs without duplication on disk or in
838 memory and can be updated independently of the linked programs.
839 Producing shared libraries portably, however, is the stuff of
840 nightmares---each system has its own incompatible tools, compiler flags,
841 and magic incantations.  Fortunately, @acronym{GNU} provides a solution:
842 @dfn{Libtool}.
843 @cindex Libtool
845 Libtool handles all the requirements of building shared libraries for
846 you, and at this time seems to be the @emph{only} way to do so with any
847 portability.  It also handles many other headaches, such as: the
848 interaction of Make rules with the variable suffixes of
849 shared libraries, linking reliably with shared libraries before they are
850 installed by the superuser, and supplying a consistent versioning system
851 (so that different versions of a library can be installed or upgraded
852 without breaking binary compatibility).  Although Libtool, like
853 Autoconf, can be used without Automake, it is most simply utilized in
854 conjunction with Automake---there, Libtool is used automatically
855 whenever shared libraries are needed, and you need not know its syntax.
857 @node Pointers
858 @section Pointers
860 Developers who are used to the simplicity of @command{make} for small
861 projects on a single system might be daunted at the prospect of
862 learning to use Automake and Autoconf.  As your software is
863 distributed to more and more users, however, you otherwise
864 quickly find yourself putting lots of effort into reinventing the
865 services that the @acronym{GNU} build tools provide, and making the
866 same mistakes that they once made and overcame.  (Besides, since
867 you're already learning Autoconf, Automake is a piece of cake.)
869 There are a number of places that you can go to for more information on
870 the @acronym{GNU} build tools.
872 @itemize @minus
874 @item Web
876 The home pages for
877 @uref{http://www.gnu.org/@/software/@/autoconf/, Autoconf},
878 @uref{http://www.gnu.org/@/software/@/automake/, Automake},
879 @uref{http://www.gnu.org/@/software/@/gnulib/, Gnulib}, and
880 @uref{http://www.gnu.org/@/software/@/libtool/, Libtool}.
882 @item Automake Manual
884 @xref{Top, , Automake, automake, @acronym{GNU} Automake}, for more
885 information on Automake.
887 @item Books
889 The book @cite{@acronym{GNU} Autoconf, Automake and
890 Libtool}@footnote{@cite{@acronym{GNU} Autoconf, Automake and Libtool},
891 by G. V. Vaughan, B. Elliston, T. Tromey, and I. L. Taylor.  SAMS (originally
892 New Riders), 2000, ISBN 1578701902.} describes the complete @acronym{GNU}
893 build environment.  You can also find
894 @uref{http://sources.redhat.com/@/autobook/, the entire book on-line}.
896 @end itemize
898 @c ================================================= Making configure Scripts.
900 @node Making configure Scripts
901 @chapter Making @command{configure} Scripts
902 @cindex @file{aclocal.m4}
903 @cindex @command{configure}
905 The configuration scripts that Autoconf produces are by convention
906 called @command{configure}.  When run, @command{configure} creates several
907 files, replacing configuration parameters in them with appropriate
908 values.  The files that @command{configure} creates are:
910 @itemize @minus
911 @item
912 one or more @file{Makefile} files, usually one in each subdirectory of the
913 package (@pxref{Makefile Substitutions});
915 @item
916 optionally, a C header file, the name of which is configurable,
917 containing @code{#define} directives (@pxref{Configuration Headers});
919 @item
920 a shell script called @file{config.status} that, when run, recreates
921 the files listed above (@pxref{config.status Invocation});
923 @item
924 an optional shell script normally called @file{config.cache}
925 (created when using @samp{configure --config-cache}) that
926 saves the results of running many of the tests (@pxref{Cache Files});
928 @item
929 a file called @file{config.log} containing any messages produced by
930 compilers, to help debugging if @command{configure} makes a mistake.
931 @end itemize
933 @cindex @file{configure.in}
934 @cindex @file{configure.ac}
935 To create a @command{configure} script with Autoconf, you need to write an
936 Autoconf input file @file{configure.ac} (or @file{configure.in}) and run
937 @command{autoconf} on it.  If you write your own feature tests to
938 supplement those that come with Autoconf, you might also write files
939 called @file{aclocal.m4} and @file{acsite.m4}.  If you use a C header
940 file to contain @code{#define} directives, you might also run
941 @command{autoheader}, and you can distribute the generated file
942 @file{config.h.in} with the package.
944 Here is a diagram showing how the files that can be used in
945 configuration are produced.  Programs that are executed are suffixed by
946 @samp{*}.  Optional files are enclosed in square brackets (@samp{[]}).
947 @command{autoconf} and @command{autoheader} also read the installed Autoconf
948 macro files (by reading @file{autoconf.m4}).
950 @noindent
951 Files used in preparing a software package for distribution:
952 @example
953 your source files --> [autoscan*] --> [configure.scan] --> configure.ac
955 @group
956 configure.ac --.
957                |   .------> autoconf* -----> configure
958 [aclocal.m4] --+---+
959                |   `-----> [autoheader*] --> [config.h.in]
960 [acsite.m4] ---'
961 @end group
963 Makefile.in -------------------------------> Makefile.in
964 @end example
966 @noindent
967 Files used in configuring a software package:
968 @example
969 @group
970                        .-------------> [config.cache]
971 configure* ------------+-------------> config.log
972                        |
973 [config.h.in] -.       v            .-> [config.h] -.
974                +--> config.status* -+               +--> make*
975 Makefile.in ---'                    `-> Makefile ---'
976 @end group
977 @end example
979 @menu
980 * Writing Autoconf Input::      What to put in an Autoconf input file
981 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
982 * ifnames Invocation::          Listing the conditionals in source code
983 * autoconf Invocation::         How to create configuration scripts
984 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
985 @end menu
987 @node Writing Autoconf Input
988 @section Writing @file{configure.ac}
990 To produce a @command{configure} script for a software package, create a
991 file called @file{configure.ac} that contains invocations of the
992 Autoconf macros that test the system features your package needs or can
993 use.  Autoconf macros already exist to check for many features; see
994 @ref{Existing Tests}, for their descriptions.  For most other features,
995 you can use Autoconf template macros to produce custom checks; see
996 @ref{Writing Tests}, for information about them.  For especially tricky
997 or specialized features, @file{configure.ac} might need to contain some
998 hand-crafted shell commands; see @ref{Portable Shell}.  The
999 @command{autoscan} program can give you a good start in writing
1000 @file{configure.ac} (@pxref{autoscan Invocation}, for more information).
1002 Previous versions of Autoconf promoted the name @file{configure.in},
1003 which is somewhat ambiguous (the tool needed to process this file is not
1004 described by its extension), and introduces a slight confusion with
1005 @file{config.h.in} and so on (for which @samp{.in} means ``to be
1006 processed by @command{configure}'').  Using @file{configure.ac} is now
1007 preferred.
1009 @menu
1010 * Shell Script Compiler::       Autoconf as solution of a problem
1011 * Autoconf Language::           Programming in Autoconf
1012 * Autoconf Input Layout::       Standard organization of @file{configure.ac}
1013 @end menu
1015 @node Shell Script Compiler
1016 @subsection A Shell Script Compiler
1018 Just as for any other computer language, in order to properly program
1019 @file{configure.ac} in Autoconf you must understand @emph{what} problem
1020 the language tries to address and @emph{how} it does so.
1022 The problem Autoconf addresses is that the world is a mess.  After all,
1023 you are using Autoconf in order to have your package compile easily on
1024 all sorts of different systems, some of them being extremely hostile.
1025 Autoconf itself bears the price for these differences: @command{configure}
1026 must run on all those systems, and thus @command{configure} must limit itself
1027 to their lowest common denominator of features.
1029 Naturally, you might then think of shell scripts; who needs
1030 @command{autoconf}?  A set of properly written shell functions is enough to
1031 make it easy to write @command{configure} scripts by hand.  Sigh!
1032 Unfortunately, shell functions do not belong to the least common
1033 denominator; therefore, where you would like to define a function and
1034 use it ten times, you would instead need to copy its body ten times.
1036 So, what is really needed is some kind of compiler, @command{autoconf},
1037 that takes an Autoconf program, @file{configure.ac}, and transforms it
1038 into a portable shell script, @command{configure}.
1040 How does @command{autoconf} perform this task?
1042 There are two obvious possibilities: creating a brand new language or
1043 extending an existing one.  The former option is attractive: all
1044 sorts of optimizations could easily be implemented in the compiler and
1045 many rigorous checks could be performed on the Autoconf program
1046 (e.g., rejecting any non-portable construct).  Alternatively, you can
1047 extend an existing language, such as the @code{sh} (Bourne shell)
1048 language.
1050 Autoconf does the latter: it is a layer on top of @code{sh}.  It was
1051 therefore most convenient to implement @command{autoconf} as a macro
1052 expander: a program that repeatedly performs @dfn{macro expansions} on
1053 text input, replacing macro calls with macro bodies and producing a pure
1054 @code{sh} script in the end.  Instead of implementing a dedicated
1055 Autoconf macro expander, it is natural to use an existing
1056 general-purpose macro language, such as M4, and implement the extensions
1057 as a set of M4 macros.
1060 @node Autoconf Language
1061 @subsection The Autoconf Language
1062 @cindex quotation
1064 The Autoconf language differs from many other computer
1065 languages because it treats actual code the same as plain text.  Whereas
1066 in C, for instance, data and instructions have different syntactic
1067 status, in Autoconf their status is rigorously the same.  Therefore, we
1068 need a means to distinguish literal strings from text to be expanded:
1069 quotation.
1071 When calling macros that take arguments, there must not be any white
1072 space between the macro name and the open parenthesis.  Arguments should
1073 be enclosed within the M4 quote characters @samp{[} and @samp{]}, and be
1074 separated by commas.  Any leading blanks or newlines in arguments are ignored,
1075 unless they are quoted.  You should always quote an argument that
1076 might contain a macro name, comma, parenthesis, or a leading blank or
1077 newline.  This rule applies recursively for every macro
1078 call, including macros called from other macros.
1080 For instance:
1082 @example
1083 AC_CHECK_HEADER([stdio.h],
1084                 [AC_DEFINE([HAVE_STDIO_H], [1],
1085                    [Define to 1 if you have <stdio.h>.])],
1086                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1087 @end example
1089 @noindent
1090 is quoted properly.  You may safely simplify its quotation to:
1092 @example
1093 AC_CHECK_HEADER([stdio.h],
1094                 [AC_DEFINE([HAVE_STDIO_H], 1,
1095                    [Define to 1 if you have <stdio.h>.])],
1096                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1097 @end example
1099 @noindent
1100 because @samp{1} cannot contain a macro call.  Here, the argument of
1101 @code{AC_MSG_ERROR} must be quoted; otherwise, its comma would be
1102 interpreted as an argument separator.  Also, the second and third arguments
1103 of @samp{AC_CHECK_HEADER} must be quoted, since they contain
1104 macro calls.  The three arguments @samp{HAVE_STDIO_H}, @samp{stdio.h},
1105 and @samp{Define to 1 if you have <stdio.h>.} do not need quoting, but
1106 if you unwisely defined a macro with a name like @samp{Define} or
1107 @samp{stdio} then they would need quoting.  Cautious Autoconf users
1108 would keep the quotes, but many Autoconf users find such precautions
1109 annoying, and would rewrite the example as follows:
1111 @example
1112 AC_CHECK_HEADER(stdio.h,
1113                 [AC_DEFINE(HAVE_STDIO_H, 1,
1114                    [Define to 1 if you have <stdio.h>.])],
1115                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1116 @end example
1118 @noindent
1119 This is safe, so long as you adopt good naming conventions and do not
1120 define macros with names like @samp{HAVE_STDIO_H}, @samp{stdio}, or
1121 @samp{h}.  Though it is also safe here to omit the quotes around
1122 @samp{Define to 1 if you have <stdio.h>.} this is not recommended, as
1123 message strings are more likely to inadvertently contain commas.
1125 The following example is wrong and dangerous, as it is underquoted:
1127 @example
1128 AC_CHECK_HEADER(stdio.h,
1129                 AC_DEFINE(HAVE_STDIO_H, 1,
1130                    Define to 1 if you have <stdio.h>.),
1131                 AC_MSG_ERROR([Sorry, can't do anything for you]))
1132 @end example
1134 In other cases, you may have to use text that also resembles a macro
1135 call.  You must quote that text even when it is not passed as a macro
1136 argument:
1138 @example
1139 echo "Hard rock was here!  --[AC_DC]"
1140 @end example
1142 @noindent
1143 which results in:
1145 @example
1146 echo "Hard rock was here!  --AC_DC"
1147 @end example
1149 @noindent
1150 When you use the same text in a macro argument, you must therefore have
1151 an extra quotation level (since one is stripped away by the macro
1152 substitution).  In general, then, it is a good idea to @emph{use double
1153 quoting for all literal string arguments}:
1155 @example
1156 AC_MSG_WARN([[AC_DC stinks  --Iron Maiden]])
1157 @end example
1159 You are now able to understand one of the constructs of Autoconf that
1160 has been continually misunderstood@dots{}  The rule of thumb is that
1161 @emph{whenever you expect macro expansion, expect quote expansion};
1162 i.e., expect one level of quotes to be lost.  For instance:
1164 @example
1165 AC_COMPILE_IFELSE([char b[10];], [], [AC_MSG_ERROR([you lose])])
1166 @end example
1168 @noindent
1169 is incorrect: here, the first argument of @code{AC_COMPILE_IFELSE} is
1170 @samp{char b[10];} and is expanded once, which results in
1171 @samp{char b10;}.  (There was an idiom common in Autoconf's past to
1172 address this issue via the M4 @code{changequote} primitive, but do not
1173 use it!)  Let's take a closer look: the author meant the first argument
1174 to be understood as a literal, and therefore it must be quoted twice:
1176 @example
1177 AC_COMPILE_IFELSE([[char b[10];]], [], [AC_MSG_ERROR([you lose])])
1178 @end example
1180 @noindent
1181 Voil@`a, you actually produce @samp{char b[10];} this time!
1183 On the other hand, descriptions (e.g., the last parameter of
1184 @code{AC_DEFINE} or @code{AS_HELP_STRING}) are not literals---they
1185 are subject to line breaking, for example---and should not be double quoted.
1186 Even if these descriptions are short and are not actually broken, double
1187 quoting them yields weird results.
1189 Some macros take optional arguments, which this documentation represents
1190 as @ovar{arg} (not to be confused with the quote characters).  You may
1191 just leave them empty, or use @samp{[]} to make the emptiness of the
1192 argument explicit, or you may simply omit the trailing commas.  The
1193 three lines below are equivalent:
1195 @example
1196 AC_CHECK_HEADERS([stdio.h], [], [], [])
1197 AC_CHECK_HEADERS([stdio.h],,,)
1198 AC_CHECK_HEADERS([stdio.h])
1199 @end example
1201 It is best to put each macro call on its own line in
1202 @file{configure.ac}.  Most of the macros don't add extra newlines; they
1203 rely on the newline after the macro call to terminate the commands.
1204 This approach makes the generated @command{configure} script a little
1205 easier to read by not inserting lots of blank lines.  It is generally
1206 safe to set shell variables on the same line as a macro call, because
1207 the shell allows assignments without intervening newlines.
1209 You can include comments in @file{configure.ac} files by starting them
1210 with the @samp{#}.  For example, it is helpful to begin
1211 @file{configure.ac} files with a line like this:
1213 @example
1214 # Process this file with autoconf to produce a configure script.
1215 @end example
1217 @node Autoconf Input Layout
1218 @subsection Standard @file{configure.ac} Layout
1220 The order in which @file{configure.ac} calls the Autoconf macros is not
1221 important, with a few exceptions.  Every @file{configure.ac} must
1222 contain a call to @code{AC_INIT} before the checks, and a call to
1223 @code{AC_OUTPUT} at the end (@pxref{Output}).  Additionally, some macros
1224 rely on other macros having been called first, because they check
1225 previously set values of some variables to decide what to do.  These
1226 macros are noted in the individual descriptions (@pxref{Existing
1227 Tests}), and they also warn you when @command{configure} is created if they
1228 are called out of order.
1230 To encourage consistency, here is a suggested order for calling the
1231 Autoconf macros.  Generally speaking, the things near the end of this
1232 list are those that could depend on things earlier in it.  For example,
1233 library functions could be affected by types and libraries.
1235 @display
1236 @group
1237 Autoconf requirements
1238 @code{AC_INIT(@var{package}, @var{version}, @var{bug-report-address})}
1239 information on the package
1240 checks for programs
1241 checks for libraries
1242 checks for header files
1243 checks for types
1244 checks for structures
1245 checks for compiler characteristics
1246 checks for library functions
1247 checks for system services
1248 @code{AC_CONFIG_FILES(@r{[}@var{file@dots{}}@r{]})}
1249 @code{AC_OUTPUT}
1250 @end group
1251 @end display
1254 @node autoscan Invocation
1255 @section Using @command{autoscan} to Create @file{configure.ac}
1256 @cindex @command{autoscan}
1258 The @command{autoscan} program can help you create and/or maintain a
1259 @file{configure.ac} file for a software package.  @command{autoscan}
1260 examines source files in the directory tree rooted at a directory given
1261 as a command line argument, or the current directory if none is given.
1262 It searches the source files for common portability problems and creates
1263 a file @file{configure.scan} which is a preliminary @file{configure.ac}
1264 for that package, and checks a possibly existing @file{configure.ac} for
1265 completeness.
1267 When using @command{autoscan} to create a @file{configure.ac}, you
1268 should manually examine @file{configure.scan} before renaming it to
1269 @file{configure.ac}; it probably needs some adjustments.
1270 Occasionally, @command{autoscan} outputs a macro in the wrong order
1271 relative to another macro, so that @command{autoconf} produces a warning;
1272 you need to move such macros manually.  Also, if you want the package to
1273 use a configuration header file, you must add a call to
1274 @code{AC_CONFIG_HEADERS} (@pxref{Configuration Headers}).  You might
1275 also have to change or add some @code{#if} directives to your program in
1276 order to make it work with Autoconf (@pxref{ifnames Invocation}, for
1277 information about a program that can help with that job).
1279 When using @command{autoscan} to maintain a @file{configure.ac}, simply
1280 consider adding its suggestions.  The file @file{autoscan.log}
1281 contains detailed information on why a macro is requested.
1283 @command{autoscan} uses several data files (installed along with Autoconf)
1284 to determine which macros to output when it finds particular symbols in
1285 a package's source files.  These data files all have the same format:
1286 each line consists of a symbol, one or more blanks, and the Autoconf macro to
1287 output if that symbol is encountered.  Lines starting with @samp{#} are
1288 comments.
1290 @command{autoscan} accepts the following options:
1292 @table @option
1293 @item --help
1294 @itemx -h
1295 Print a summary of the command line options and exit.
1297 @item --version
1298 @itemx -V
1299 Print the version number of Autoconf and exit.
1301 @item --verbose
1302 @itemx -v
1303 Print the names of the files it examines and the potentially interesting
1304 symbols it finds in them.  This output can be voluminous.
1306 @item --include=@var{dir}
1307 @itemx -I @var{dir}
1308 Append @var{dir} to the include path.  Multiple invocations accumulate.
1310 @item --prepend-include=@var{dir}
1311 @item -B @var{dir}
1312 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1313 @end table
1315 @node ifnames Invocation
1316 @section Using @command{ifnames} to List Conditionals
1317 @cindex @command{ifnames}
1319 @command{ifnames} can help you write @file{configure.ac} for a software
1320 package.  It prints the identifiers that the package already uses in C
1321 preprocessor conditionals.  If a package has already been set up to have
1322 some portability, @command{ifnames} can thus help you figure out what its
1323 @command{configure} needs to check for.  It may help fill in some gaps in a
1324 @file{configure.ac} generated by @command{autoscan} (@pxref{autoscan
1325 Invocation}).
1327 @command{ifnames} scans all of the C source files named on the command line
1328 (or the standard input, if none are given) and writes to the standard
1329 output a sorted list of all the identifiers that appear in those files
1330 in @code{#if}, @code{#elif}, @code{#ifdef}, or @code{#ifndef}
1331 directives.  It prints each identifier on a line, followed by a
1332 space-separated list of the files in which that identifier occurs.
1334 @noindent
1335 @command{ifnames} accepts the following options:
1337 @table @option
1338 @item --help
1339 @itemx -h
1340 Print a summary of the command line options and exit.
1342 @item --version
1343 @itemx -V
1344 Print the version number of Autoconf and exit.
1345 @end table
1347 @node autoconf Invocation
1348 @section Using @command{autoconf} to Create @command{configure}
1349 @cindex @command{autoconf}
1351 To create @command{configure} from @file{configure.ac}, run the
1352 @command{autoconf} program with no arguments.  @command{autoconf} processes
1353 @file{configure.ac} with the M4 macro processor, using the
1354 Autoconf macros.  If you give @command{autoconf} an argument, it reads that
1355 file instead of @file{configure.ac} and writes the configuration script
1356 to the standard output instead of to @command{configure}.  If you give
1357 @command{autoconf} the argument @option{-}, it reads from the standard
1358 input instead of @file{configure.ac} and writes the configuration script
1359 to the standard output.
1361 The Autoconf macros are defined in several files.  Some of the files are
1362 distributed with Autoconf; @command{autoconf} reads them first.  Then it
1363 looks for the optional file @file{acsite.m4} in the directory that
1364 contains the distributed Autoconf macro files, and for the optional file
1365 @file{aclocal.m4} in the current directory.  Those files can contain
1366 your site's or the package's own Autoconf macro definitions
1367 (@pxref{Writing Autoconf Macros}, for more information).  If a macro is
1368 defined in more than one of the files that @command{autoconf} reads, the
1369 last definition it reads overrides the earlier ones.
1371 @command{autoconf} accepts the following options:
1373 @table @option
1374 @item --help
1375 @itemx -h
1376 Print a summary of the command line options and exit.
1378 @item --version
1379 @itemx -V
1380 Print the version number of Autoconf and exit.
1382 @item --verbose
1383 @itemx -v
1384 Report processing steps.
1386 @item --debug
1387 @itemx -d
1388 Don't remove the temporary files.
1390 @item --force
1391 @itemx -f
1392 Remake @file{configure} even if newer than its input files.
1394 @item --include=@var{dir}
1395 @itemx -I @var{dir}
1396 Append @var{dir} to the include path.  Multiple invocations accumulate.
1398 @item --prepend-include=@var{dir}
1399 @item -B @var{dir}
1400 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1402 @item --output=@var{file}
1403 @itemx -o @var{file}
1404 Save output (script or trace) to @var{file}.  The file @option{-} stands
1405 for the standard output.
1407 @item --warnings=@var{category}
1408 @itemx -W @var{category}
1409 @evindex WARNINGS
1410 Report the warnings related to @var{category} (which can actually be a
1411 comma separated list).  @xref{Reporting Messages}, macro
1412 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
1413 values include:
1415 @table @samp
1416 @item all
1417 report all the warnings
1419 @item none
1420 report none
1422 @item error
1423 treats warnings as errors
1425 @item no-@var{category}
1426 disable warnings falling into @var{category}
1427 @end table
1429 Warnings about @samp{syntax} are enabled by default, and the environment
1430 variable @env{WARNINGS}, a comma separated list of categories, is
1431 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1432 you had passed @option{--warnings syntax,$WARNINGS,@var{category}}.  If
1433 you want to disable the defaults and @env{WARNINGS}, but (for example)
1434 enable the warnings about obsolete constructs, you would use @option{-W
1435 none,obsolete}.
1437 @cindex Back trace
1438 @cindex Macro invocation stack
1439 Because @command{autoconf} uses @command{autom4te} behind the scenes, it
1440 displays a back trace for errors, but not for warnings; if you want
1441 them, just pass @option{-W error}.  @xref{autom4te Invocation}, for some
1442 examples.
1444 @item --trace=@var{macro}[:@var{format}]
1445 @itemx -t @var{macro}[:@var{format}]
1446 Do not create the @command{configure} script, but list the calls to
1447 @var{macro} according to the @var{format}.  Multiple @option{--trace}
1448 arguments can be used to list several macros.  Multiple @option{--trace}
1449 arguments for a single macro are not cumulative; instead, you should
1450 just make @var{format} as long as needed.
1452 The @var{format} is a regular string, with newlines if desired, and
1453 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}; see
1454 @ref{autom4te Invocation}, for details on the @var{format}.
1456 @item --initialization
1457 @itemx -i
1458 By default, @option{--trace} does not trace the initialization of the
1459 Autoconf macros (typically the @code{AC_DEFUN} definitions).  This
1460 results in a noticeable speedup, but can be disabled by this option.
1461 @end table
1464 It is often necessary to check the content of a @file{configure.ac}
1465 file, but parsing it yourself is extremely fragile and error-prone.  It
1466 is suggested that you rely upon @option{--trace} to scan
1467 @file{configure.ac}.  For instance, to find the list of variables that
1468 are substituted, use:
1470 @example
1471 @group
1472 $ @kbd{autoconf -t AC_SUBST}
1473 configure.ac:2:AC_SUBST:ECHO_C
1474 configure.ac:2:AC_SUBST:ECHO_N
1475 configure.ac:2:AC_SUBST:ECHO_T
1476 @i{More traces deleted}
1477 @end group
1478 @end example
1480 @noindent
1481 The example below highlights the difference between @samp{$@@},
1482 @samp{$*}, and @samp{$%}.
1484 @example
1485 @group
1486 $ @kbd{cat configure.ac}
1487 AC_DEFINE(This, is, [an
1488 [example]])
1489 $ @kbd{autoconf -t 'AC_DEFINE:@@: $@@}
1490 *: $*
1491 %: $%'
1492 @@: [This],[is],[an
1493 [example]]
1494 *: This,is,an
1495 [example]
1496 %: This:is:an [example]
1497 @end group
1498 @end example
1500 @noindent
1501 The @var{format} gives you a lot of freedom:
1503 @example
1504 @group
1505 $ @kbd{autoconf -t 'AC_SUBST:$$ac_subst@{"$1"@} = "$f:$l";'}
1506 $ac_subst@{"ECHO_C"@} = "configure.ac:2";
1507 $ac_subst@{"ECHO_N"@} = "configure.ac:2";
1508 $ac_subst@{"ECHO_T"@} = "configure.ac:2";
1509 @i{More traces deleted}
1510 @end group
1511 @end example
1513 @noindent
1514 A long @var{separator} can be used to improve the readability of complex
1515 structures, and to ease their parsing (for instance when no single
1516 character is suitable as a separator):
1518 @example
1519 @group
1520 $ @kbd{autoconf -t 'AM_MISSING_PROG:$@{|:::::|@}*'}
1521 ACLOCAL|:::::|aclocal|:::::|$missing_dir
1522 AUTOCONF|:::::|autoconf|:::::|$missing_dir
1523 AUTOMAKE|:::::|automake|:::::|$missing_dir
1524 @i{More traces deleted}
1525 @end group
1526 @end example
1528 @node autoreconf Invocation
1529 @section Using @command{autoreconf} to Update @command{configure} Scripts
1530 @cindex @command{autoreconf}
1532 Installing the various components of the @acronym{GNU} Build System can be
1533 tedious: running @command{autopoint} for Gettext, @command{automake} for
1534 @file{Makefile.in} etc.@: in each directory.  It may be needed either
1535 because some tools such as @command{automake} have been updated on your
1536 system, or because some of the sources such as @file{configure.ac} have
1537 been updated, or finally, simply in order to install the @acronym{GNU} Build
1538 System in a fresh tree.
1540 @command{autoreconf} runs @command{autoconf}, @command{autoheader},
1541 @command{aclocal}, @command{automake}, @command{libtoolize}, and
1542 @command{autopoint} (when appropriate) repeatedly to update the
1543 @acronym{GNU} Build System in the specified directories and their
1544 subdirectories (@pxref{Subdirectories}).  By default, it only remakes
1545 those files that are older than their sources.
1547 If you install a new version of some tool, you can make
1548 @command{autoreconf} remake @emph{all} of the files by giving it the
1549 @option{--force} option.
1551 @xref{Automatic Remaking}, for Make rules to automatically
1552 remake @command{configure} scripts when their source files change.  That
1553 method handles the timestamps of configuration header templates
1554 properly, but does not pass @option{--autoconf-dir=@var{dir}} or
1555 @option{--localdir=@var{dir}}.
1557 @cindex Gettext
1558 @cindex @command{autopoint}
1559 Gettext supplies the @command{autopoint} command to add translation
1560 infrastructure to a source package.  If you use @command{autopoint},
1561 your @file{configure.ac} should invoke both @code{AM_GNU_GETTEXT} and
1562 @code{AM_GNU_GETTEXT_VERSION(@var{gettext-version})}.  @xref{autopoint
1563 Invocation, , Invoking the @code{autopoint} Program, gettext,
1564 @acronym{GNU} @code{gettext} utilities}, for further details.
1566 @noindent
1567 @command{autoreconf} accepts the following options:
1569 @table @option
1570 @item --help
1571 @itemx -h
1572 Print a summary of the command line options and exit.
1574 @item --version
1575 @itemx -V
1576 Print the version number of Autoconf and exit.
1578 @item --verbose
1579 Print the name of each directory @command{autoreconf} examines and the
1580 commands it runs.  If given two or more times, pass @option{--verbose}
1581 to subordinate tools that support it.
1583 @item --debug
1584 @itemx -d
1585 Don't remove the temporary files.
1587 @item --force
1588 @itemx -f
1589 Remake even @file{configure} scripts and configuration headers that are
1590 newer than their input files (@file{configure.ac} and, if present,
1591 @file{aclocal.m4}).
1593 @item --install
1594 @itemx -i
1595 Install the missing auxiliary files in the package.  By default, files
1596 are copied; this can be changed with @option{--symlink}.
1598 If deemed appropriate, this option triggers calls to
1599 @samp{automake --add-missing},
1600 @samp{libtoolize}, @samp{autopoint}, etc.
1602 @item --no-recursive
1603 Do not rebuild files in subdirectories to configure (see @ref{Subdirectories},
1604 macro @code{AC_CONFIG_SUBDIRS}).
1606 @item --symlink
1607 @itemx -s
1608 When used with @option{--install}, install symbolic links to the missing
1609 auxiliary files instead of copying them.
1611 @item --make
1612 @itemx -m
1613 When the directories were configured, update the configuration by
1614 running @samp{./config.status --recheck && ./config.status}, and then
1615 run @samp{make}.
1617 @item --include=@var{dir}
1618 @itemx -I @var{dir}
1619 Append @var{dir} to the include path.  Multiple invocations accumulate.
1620 Passed on to @command{autoconf} and @command{autoheader} internally.
1622 @item --prepend-include=@var{dir}
1623 @item -B @var{dir}
1624 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1625 Passed on to @command{autoconf} and @command{autoheader} internally.
1627 @item --warnings=@var{category}
1628 @itemx -W @var{category}
1629 @evindex WARNINGS
1630 Report the warnings related to @var{category} (which can actually be a
1631 comma separated list).
1633 @table @samp
1634 @item cross
1635 related to cross compilation issues.
1637 @item obsolete
1638 report the uses of obsolete constructs.
1640 @item portability
1641 portability issues
1643 @item syntax
1644 dubious syntactic constructs.
1646 @item all
1647 report all the warnings
1649 @item none
1650 report none
1652 @item error
1653 treats warnings as errors
1655 @item no-@var{category}
1656 disable warnings falling into @var{category}
1657 @end table
1659 Warnings about @samp{syntax} are enabled by default, and the environment
1660 variable @env{WARNINGS}, a comma separated list of categories, is
1661 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1662 you had passed @option{--warnings syntax,$WARNINGS,@var{category}}.  If
1663 you want to disable the defaults and @env{WARNINGS}, but (for example)
1664 enable the warnings about obsolete constructs, you would use @option{-W
1665 none,obsolete}.
1666 @end table
1668 If you want @command{autoreconf} to pass flags that are not listed here
1669 on to @command{aclocal}, set @code{ACLOCAL_AMFLAGS} in your @file{Makefile.am}.
1670 Due to a limitation in the Autoconf implementation these flags currently
1671 must be set on a single line in @file{Makefile.am}, without any
1672 backslash-newlines.
1674 @c ========================================= Initialization and Output Files.
1676 @node Setup
1677 @chapter Initialization and Output Files
1679 Autoconf-generated @command{configure} scripts need some information about
1680 how to initialize, such as how to find the package's source files and
1681 about the output files to produce.  The following sections describe the
1682 initialization and the creation of output files.
1684 @menu
1685 * Initializing configure::      Option processing etc.
1686 * Versioning::                  Dealing with Autoconf versions
1687 * Notices::                     Copyright, version numbers in @command{configure}
1688 * Input::                       Where Autoconf should find files
1689 * Output::                      Outputting results from the configuration
1690 * Configuration Actions::       Preparing the output based on results
1691 * Configuration Files::         Creating output files
1692 * Makefile Substitutions::      Using output variables in makefiles
1693 * Configuration Headers::       Creating a configuration header file
1694 * Configuration Commands::      Running arbitrary instantiation commands
1695 * Configuration Links::         Links depending on the configuration
1696 * Subdirectories::              Configuring independent packages together
1697 * Default Prefix::              Changing the default installation prefix
1698 @end menu
1700 @node Initializing configure
1701 @section Initializing @command{configure}
1703 Every @command{configure} script must call @code{AC_INIT} before doing
1704 anything else.  The only other required macro is @code{AC_OUTPUT}
1705 (@pxref{Output}).
1707 @anchor{AC_INIT}
1708 @defmac AC_INIT (@var{package}, @var{version}, @ovar{bug-report}, @
1709   @ovar{tarname})
1710 @acindex{INIT}
1711 Process any command-line arguments and perform various initializations
1712 and verifications.
1714 Set the name of the @var{package} and its @var{version}.  These are
1715 typically used in @option{--version} support, including that of
1716 @command{configure}.  The optional argument @var{bug-report} should be
1717 the email to which users should send bug reports.  The package
1718 @var{tarname} differs from @var{package}: the latter designates the full
1719 package name (e.g., @samp{GNU Autoconf}), while the former is meant for
1720 distribution tar ball names (e.g., @samp{autoconf}).  It defaults to
1721 @var{package} with @samp{GNU } stripped, lower-cased, and all characters
1722 other than alphanumerics and underscores are changed to @samp{-}.
1724 It is preferable that the arguments of @code{AC_INIT} be static, i.e.,
1725 there should not be any shell computation, but they can be computed by
1728 The following M4 macros (e.g., @code{AC_PACKAGE_NAME}), output variables
1729 (e.g., @code{PACKAGE_NAME}), and preprocessor symbols (e.g.,
1730 @code{PACKAGE_NAME}), are defined by @code{AC_INIT}:
1732 @table @asis
1733 @item @code{AC_PACKAGE_NAME}, @code{PACKAGE_NAME}
1734 @acindex{PACKAGE_NAME}
1735 @ovindex PACKAGE_NAME
1736 @cvindex PACKAGE_NAME
1737 Exactly @var{package}.
1739 @item @code{AC_PACKAGE_TARNAME}, @code{PACKAGE_TARNAME}
1740 @acindex{PACKAGE_TARNAME}
1741 @ovindex PACKAGE_TARNAME
1742 @cvindex PACKAGE_TARNAME
1743 Exactly @var{tarname}.
1745 @item @code{AC_PACKAGE_VERSION}, @code{PACKAGE_VERSION}
1746 @acindex{PACKAGE_VERSION}
1747 @ovindex PACKAGE_VERSION
1748 @cvindex PACKAGE_VERSION
1749 Exactly @var{version}.
1751 @item @code{AC_PACKAGE_STRING}, @code{PACKAGE_STRING}
1752 @acindex{PACKAGE_STRING}
1753 @ovindex PACKAGE_STRING
1754 @cvindex PACKAGE_STRING
1755 Exactly @samp{@var{package} @var{version}}.
1757 @item @code{AC_PACKAGE_BUGREPORT}, @code{PACKAGE_BUGREPORT}
1758 @acindex{PACKAGE_BUGREPORT}
1759 @ovindex PACKAGE_BUGREPORT
1760 @cvindex PACKAGE_BUGREPORT
1761 Exactly @var{bug-report}.
1762 @end table
1763 @end defmac
1765 If your @command{configure} script does its own option processing, it
1766 should inspect @samp{$@@} or @samp{$*} immediately after calling
1767 @code{AC_INIT}, because other Autoconf macros liberally use the
1768 @command{set} command to process strings, and this has the side effect
1769 of updating @samp{$@@} and @samp{$*}.  However, we suggest that you use
1770 standard macros like @code{AC_ARG_ENABLE} instead of attempting to
1771 implement your own option processing.  @xref{Site Configuration}.
1773 @node Versioning
1774 @section Dealing with Autoconf versions
1775 @cindex Autoconf version
1776 @cindex version, Autoconf
1778 The following optional macros can be used to help choose the minimum
1779 version of Autoconf that can successfully compile a given
1780 @file{configure.ac}.
1782 @defmac AC_PREREQ (@var{version})
1783 @acindex{PREREQ}
1784 @cindex Version
1785 Ensure that a recent enough version of Autoconf is being used.  If the
1786 version of Autoconf being used to create @command{configure} is
1787 earlier than @var{version}, print an error message to the standard
1788 error output and exit with failure (exit status is 63).  For example:
1790 @example
1791 AC_PREREQ([@value{VERSION}])
1792 @end example
1794 This macro is the only macro that may be used before @code{AC_INIT}, but
1795 for consistency, you are invited not to do so.
1796 @end defmac
1798 @defmac AC_AUTOCONF_VERSION
1799 @acindex{AUTOCONF_VERSION}
1800 This macro was introduced in Autoconf 2.62.  It identifies the version
1801 of Autoconf that is currently parsing the input file, in a format
1802 suitable for @code{m4_version_compare} (@pxref{m4_version_compare}); in
1803 other words, for this release of Autoconf, its value is
1804 @samp{@value{VERSION}}.  One potential use of this macro is for writing
1805 conditional fallbacks based on when a feature was added to Autoconf,
1806 rather than using @code{AC_PREREQ} to require the newer version of
1807 Autoconf.  However, remember that the Autoconf philosophy favors feature
1808 checks over version checks.
1809 @end defmac
1811 @node Notices
1812 @section Notices in @command{configure}
1813 @cindex Notices in @command{configure}
1815 The following macros manage version numbers for @command{configure}
1816 scripts.  Using them is optional.
1818 @defmac AC_COPYRIGHT (@var{copyright-notice})
1819 @acindex{COPYRIGHT}
1820 @cindex Copyright Notice
1821 State that, in addition to the Free Software Foundation's copyright on
1822 the Autoconf macros, parts of your @command{configure} are covered by the
1823 @var{copyright-notice}.
1825 The @var{copyright-notice} shows up in both the head of
1826 @command{configure} and in @samp{configure --version}.
1827 @end defmac
1830 @defmac AC_REVISION (@var{revision-info})
1831 @acindex{REVISION}
1832 @cindex Revision
1833 Copy revision stamp @var{revision-info} into the @command{configure}
1834 script, with any dollar signs or double-quotes removed.  This macro lets
1835 you put a revision stamp from @file{configure.ac} into @command{configure}
1836 without @acronym{RCS} or @acronym{CVS} changing it when you check in
1837 @command{configure}.  That way, you can determine easily which revision of
1838 @file{configure.ac} a particular @command{configure} corresponds to.
1840 For example, this line in @file{configure.ac}:
1842 @c The asis prevents RCS from changing the example in the manual.
1843 @example
1844 AC_REVISION([$@asis{Revision: 1.30 }$])
1845 @end example
1847 @noindent
1848 produces this in @command{configure}:
1850 @example
1851 #!/bin/sh
1852 # From configure.ac Revision: 1.30
1853 @end example
1854 @end defmac
1857 @node Input
1858 @section Finding @command{configure} Input
1860 @anchor{AC_CONFIG_SRCDIR}
1861 @defmac AC_CONFIG_SRCDIR (@var{unique-file-in-source-dir})
1862 @acindex{CONFIG_SRCDIR}
1863 @var{unique-file-in-source-dir} is some file that is in the package's
1864 source directory; @command{configure} checks for this file's existence to
1865 make sure that the directory that it is told contains the source code in
1866 fact does.  Occasionally people accidentally specify the wrong directory
1867 with @option{--srcdir}; this is a safety check.  @xref{configure
1868 Invocation}, for more information.
1869 @end defmac
1872 @c FIXME: Remove definitively once --install explained.
1874 @c Small packages may store all their macros in @code{aclocal.m4}.  As the
1875 @c set of macros grows, or for maintenance reasons, a maintainer may prefer
1876 @c to split the macros in several files.  In this case, Autoconf must be
1877 @c told which files to load, and in which order.
1879 @c @defmac AC_INCLUDE (@var{file}@dots{})
1880 @c @acindex{INCLUDE}
1881 @c @c FIXME: There is no longer shell globbing.
1882 @c Read the macro definitions that appear in the listed files.  A list of
1883 @c space-separated file names or shell globbing patterns is expected.  The
1884 @c files are read in the order they're listed.
1886 @c Because the order of definition of macros is important (only the last
1887 @c definition of a macro is used), beware that it is @code{AC_INIT} that
1888 @c loads @file{acsite.m4} and @file{aclocal.m4}.  Note that
1889 @c @code{AC_INCLUDE}ing a file before @code{AC_INIT} or within
1890 @c @file{aclocal.m4} is different from doing so after @code{AC_INIT}: in
1891 @c the latter case, non-macro lines from included files may end up in the
1892 @c @file{configure} script, whereas in the former case, they'd be discarded
1893 @c just like any text that appear before @code{AC_INIT}.
1894 @c @end defmac
1896 Packages that do manual configuration or use the @command{install} program
1897 might need to tell @command{configure} where to find some other shell
1898 scripts by calling @code{AC_CONFIG_AUX_DIR}, though the default places
1899 it looks are correct for most cases.
1901 @defmac AC_CONFIG_AUX_DIR (@var{dir})
1902 @acindex{CONFIG_AUX_DIR}
1903 Use the auxiliary build tools (e.g., @file{install-sh},
1904 @file{config.sub}, @file{config.guess}, Cygnus @command{configure},
1905 Automake and Libtool scripts, etc.)@: that are in directory @var{dir}.
1906 These are auxiliary files used in configuration.  @var{dir} can be
1907 either absolute or relative to @file{@var{srcdir}}.  The default is
1908 @file{@var{srcdir}} or @file{@var{srcdir}/..} or
1909 @file{@var{srcdir}/../..}, whichever is the first that contains
1910 @file{install-sh}.  The other files are not checked for, so that using
1911 @code{AC_PROG_INSTALL} does not automatically require distributing the
1912 other auxiliary files.  It checks for @file{install.sh} also, but that
1913 name is obsolete because some @code{make} have a rule that creates
1914 @file{install} from it if there is no makefile.
1916 The auxiliary directory is commonly named @file{build-aux}.
1917 If you need portability to @acronym{DOS} variants, do not name the
1918 auxiliary directory @file{aux}.  @xref{File System Conventions}.
1919 @end defmac
1921 @defmac AC_REQUIRE_AUX_FILE (@var{file})
1922 @acindex{REQUIRE_AUX_FILE}
1923 Declares that @var{file} is expected in the directory defined above.  In
1924 Autoconf proper, this macro does nothing: its sole purpose is to be
1925 traced by third-party tools to produce a list of expected auxiliary
1926 files.  For instance it is called by macros like @code{AC_PROG_INSTALL}
1927 (@pxref{Particular Programs}) or @code{AC_CANONICAL_BUILD}
1928 (@pxref{Canonicalizing}) to register the auxiliary files they need.
1929 @end defmac
1931 Similarly, packages that use @command{aclocal} should declare where
1932 local macros can be found using @code{AC_CONFIG_MACRO_DIR}.
1934 @defmac AC_CONFIG_MACRO_DIR (@var{dir})
1935 @acindex{CONFIG_MACRO_DIR}
1936 Specify @var{dir} as the location of additional local Autoconf macros.
1937 This macro is intended for use by future versions of commands like
1938 @command{autoreconf} that trace macro calls.  It should be called
1939 directly from @file{configure.ac} so that tools that install macros for
1940 @command{aclocal} can find the macros' declarations.
1941 @end defmac
1944 @node Output
1945 @section Outputting Files
1946 @cindex Outputting files
1948 Every Autoconf script, e.g., @file{configure.ac}, should finish by
1949 calling @code{AC_OUTPUT}.  That is the macro that generates and runs
1950 @file{config.status}, which in turn creates the makefiles and any
1951 other files resulting from configuration.  This is the only required
1952 macro besides @code{AC_INIT} (@pxref{Input}).
1954 @anchor{AC_OUTPUT}
1955 @defmac AC_OUTPUT
1956 @acindex{OUTPUT}
1957 @cindex Instantiation
1958 Generate @file{config.status} and launch it.  Call this macro once, at
1959 the end of @file{configure.ac}.
1961 @file{config.status} performs all the configuration actions: all the
1962 output files (see @ref{Configuration Files}, macro
1963 @code{AC_CONFIG_FILES}), header files (see @ref{Configuration Headers},
1964 macro @code{AC_CONFIG_HEADERS}), commands (see @ref{Configuration
1965 Commands}, macro @code{AC_CONFIG_COMMANDS}), links (see
1966 @ref{Configuration Links}, macro @code{AC_CONFIG_LINKS}), subdirectories
1967 to configure (see @ref{Subdirectories}, macro @code{AC_CONFIG_SUBDIRS})
1968 are honored.
1970 The location of your @code{AC_OUTPUT} invocation is the exact point
1971 where configuration actions are taken: any code afterwards is
1972 executed by @code{configure} once @command{config.status} was run.  If
1973 you want to bind actions to @command{config.status} itself
1974 (independently of whether @command{configure} is being run), see
1975 @ref{Configuration Commands, , Running Arbitrary Configuration
1976 Commands}.
1977 @end defmac
1979 Historically, the usage of @code{AC_OUTPUT} was somewhat different.
1980 @xref{Obsolete Macros}, for a description of the arguments that
1981 @code{AC_OUTPUT} used to support.
1984 If you run @command{make} in subdirectories, you should run it using the
1985 @code{make} variable @code{MAKE}.  Most versions of @command{make} set
1986 @code{MAKE} to the name of the @command{make} program plus any options it
1987 was given.  (But many do not include in it the values of any variables
1988 set on the command line, so those are not passed on automatically.)
1989 Some old versions of @command{make} do not set this variable.  The
1990 following macro allows you to use it even with those versions.
1992 @anchor{AC_PROG_MAKE_SET}
1993 @defmac AC_PROG_MAKE_SET
1994 @acindex{PROG_MAKE_SET}
1995 @ovindex SET_MAKE
1996 If the Make command, @code{$MAKE} if set or else @samp{make}, predefines
1997 @code{$(MAKE)}, define output variable @code{SET_MAKE} to be empty.
1998 Otherwise, define @code{SET_MAKE} to a macro definition that sets
1999 @code{$(MAKE)}, such as @samp{MAKE=make}.  Calls @code{AC_SUBST} for
2000 @code{SET_MAKE}.
2001 @end defmac
2003 If you use this macro, place a line like this in each @file{Makefile.in}
2004 that runs @code{MAKE} on other directories:
2006 @example
2007 @@SET_MAKE@@
2008 @end example
2012 @node Configuration Actions
2013 @section Performing Configuration Actions
2014 @cindex Configuration actions
2016 @file{configure} is designed so that it appears to do everything itself,
2017 but there is actually a hidden slave: @file{config.status}.
2018 @file{configure} is in charge of examining your system, but it is
2019 @file{config.status} that actually takes the proper actions based on the
2020 results of @file{configure}.  The most typical task of
2021 @file{config.status} is to @emph{instantiate} files.
2023 This section describes the common behavior of the four standard
2024 instantiating macros: @code{AC_CONFIG_FILES}, @code{AC_CONFIG_HEADERS},
2025 @code{AC_CONFIG_COMMANDS} and @code{AC_CONFIG_LINKS}.  They all
2026 have this prototype:
2028 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
2029 @c awful.
2030 @example
2031 AC_CONFIG_FOOS(@var{tag}@dots{}, [@var{commands}], [@var{init-cmds}])
2032 @end example
2034 @noindent
2035 where the arguments are:
2037 @table @var
2038 @item tag@dots{}
2039 A blank-or-newline-separated list of tags, which are typically the names of
2040 the files to instantiate.
2042 You are encouraged to use literals as @var{tags}.  In particular, you
2043 should avoid
2045 @example
2046 @dots{} && my_foos="$my_foos fooo"
2047 @dots{} && my_foos="$my_foos foooo"
2048 AC_CONFIG_FOOS([$my_foos])
2049 @end example
2051 @noindent
2052 and use this instead:
2054 @example
2055 @dots{} && AC_CONFIG_FOOS([fooo])
2056 @dots{} && AC_CONFIG_FOOS([foooo])
2057 @end example
2059 The macros @code{AC_CONFIG_FILES} and @code{AC_CONFIG_HEADERS} use
2060 special @var{tag} values: they may have the form @samp{@var{output}} or
2061 @samp{@var{output}:@var{inputs}}.  The file @var{output} is instantiated
2062 from its templates, @var{inputs} (defaulting to @samp{@var{output}.in}).
2064 @samp{AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk)]},
2065 for example, asks for
2066 the creation of the file @file{Makefile} that contains the expansion of the
2067 output variables in the concatenation of @file{boiler/top.mk} and
2068 @file{boiler/bot.mk}.
2070 The special value @samp{-} might be used to denote the standard output
2071 when used in @var{output}, or the standard input when used in the
2072 @var{inputs}.  You most probably don't need to use this in
2073 @file{configure.ac}, but it is convenient when using the command line
2074 interface of @file{./config.status}, see @ref{config.status Invocation},
2075 for more details.
2077 The @var{inputs} may be absolute or relative file names.  In the latter
2078 case they are first looked for in the build tree, and then in the source
2079 tree.
2081 @item commands
2082 Shell commands output literally into @file{config.status}, and
2083 associated with a tag that the user can use to tell @file{config.status}
2084 which the commands to run.  The commands are run each time a @var{tag}
2085 request is given to @file{config.status}, typically each time the file
2086 @file{@var{tag}} is created.
2088 The variables set during the execution of @command{configure} are
2089 @emph{not} available here: you first need to set them via the
2090 @var{init-cmds}.  Nonetheless the following variables are precomputed:
2092 @table @code
2093 @item srcdir
2094 The name of the top source directory, assuming that the working
2095 directory is the top build directory.  This
2096 is what the @command{configure} option @option{--srcdir} sets.
2098 @item ac_top_srcdir
2099 The name of the top source directory, assuming that the working
2100 directory is the current build directory.
2103 @item ac_top_build_prefix
2104 The name of the top build directory, assuming that the working
2105 directory is the current build directory.
2106 It can be empty, or else ends with a slash, so that you may concatenate
2109 @item ac_srcdir
2110 The name of the corresponding source directory, assuming that the
2111 working directory is the current build directory.
2112 @end table
2114 @noindent
2115 The @dfn{current} directory refers to the directory (or
2116 pseudo-directory) containing the input part of @var{tags}.  For
2117 instance, running
2119 @example
2120 AC_CONFIG_COMMANDS([deep/dir/out:in/in.in], [@dots{}], [@dots{}])
2121 @end example
2123 @noindent
2124  with @option{--srcdir=../package} produces the following values:
2126 @example
2127 # Argument of --srcdir
2128 srcdir='../package'
2129 # Reversing deep/dir
2130 ac_top_build_prefix='../../'
2131 # Concatenation of $ac_top_build_prefix and srcdir
2132 ac_top_srcdir='../../../package'
2133 # Concatenation of $ac_top_srcdir and deep/dir
2134 ac_srcdir='../../../package/deep/dir'
2135 @end example
2137 @noindent
2138 independently of @samp{in/in.in}.
2140 @item init-cmds
2141 Shell commands output @emph{unquoted} near the beginning of
2142 @file{config.status}, and executed each time @file{config.status} runs
2143 (regardless of the tag).  Because they are unquoted, for example,
2144 @samp{$var} is output as the value of @code{var}.  @var{init-cmds}
2145 is typically used by @file{configure} to give @file{config.status} some
2146 variables it needs to run the @var{commands}.
2148 You should be extremely cautious in your variable names: all the
2149 @var{init-cmds} share the same name space and may overwrite each other
2150 in unpredictable ways.  Sorry@enddots{}
2151 @end table
2153 All these macros can be called multiple times, with different
2154 @var{tag} values, of course!
2157 @node Configuration Files
2158 @section Creating Configuration Files
2159 @cindex Creating configuration files
2160 @cindex Configuration file creation
2162 Be sure to read the previous section, @ref{Configuration Actions}.
2164 @anchor{AC_CONFIG_FILES}
2165 @defmac AC_CONFIG_FILES (@var{file}@dots{}, @ovar{cmds}, @ovar{init-cmds})
2166 @acindex{CONFIG_FILES}
2167 Make @code{AC_OUTPUT} create each @file{@var{file}} by copying an input
2168 file (by default @file{@var{file}.in}), substituting the output variable
2169 values.
2170 @c Before we used to have this feature, which was later rejected
2171 @c because it complicates the writing of makefiles:
2172 @c If the file would be unchanged, it is left untouched, to preserve
2173 @c timestamp.
2174 This macro is one of the instantiating macros; see @ref{Configuration
2175 Actions}.  @xref{Makefile Substitutions}, for more information on using
2176 output variables.  @xref{Setting Output Variables}, for more information
2177 on creating them.  This macro creates the directory that the file is in
2178 if it doesn't exist.  Usually, makefiles are created this way,
2179 but other files, such as @file{.gdbinit}, can be specified as well.
2181 Typical calls to @code{AC_CONFIG_FILES} look like this:
2183 @example
2184 AC_CONFIG_FILES([Makefile src/Makefile man/Makefile X/Imakefile])
2185 AC_CONFIG_FILES([autoconf], [chmod +x autoconf])
2186 @end example
2188 You can override an input file name by appending to @var{file} a
2189 colon-separated list of input files.  Examples:
2191 @example
2192 AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk]
2193                 [lib/Makefile:boiler/lib.mk])
2194 @end example
2196 @noindent
2197 Doing this allows you to keep your file names acceptable to
2198 @acronym{DOS} variants, or
2199 to prepend and/or append boilerplate to the file.
2200 @end defmac
2204 @node Makefile Substitutions
2205 @section Substitutions in Makefiles
2206 @cindex Substitutions in makefiles
2207 @cindex Makefile substitutions
2209 Each subdirectory in a distribution that contains something to be
2210 compiled or installed should come with a file @file{Makefile.in}, from
2211 which @command{configure} creates a file @file{Makefile} in that directory.
2212 To create @file{Makefile}, @command{configure} performs a simple variable
2213 substitution, replacing occurrences of @samp{@@@var{variable}@@} in
2214 @file{Makefile.in} with the value that @command{configure} has determined
2215 for that variable.  Variables that are substituted into output files in
2216 this way are called @dfn{output variables}.  They are ordinary shell
2217 variables that are set in @command{configure}.  To make @command{configure}
2218 substitute a particular variable into the output files, the macro
2219 @code{AC_SUBST} must be called with that variable name as an argument.
2220 Any occurrences of @samp{@@@var{variable}@@} for other variables are
2221 left unchanged.  @xref{Setting Output Variables}, for more information
2222 on creating output variables with @code{AC_SUBST}.
2224 A software package that uses a @command{configure} script should be
2225 distributed with a file @file{Makefile.in}, but no makefile; that
2226 way, the user has to properly configure the package for the local system
2227 before compiling it.
2229 @xref{Makefile Conventions, , Makefile Conventions, standards, The
2230 @acronym{GNU} Coding Standards}, for more information on what to put in
2231 makefiles.
2233 @menu
2234 * Preset Output Variables::     Output variables that are always set
2235 * Installation Directory Variables::  Other preset output variables
2236 * Changed Directory Variables:: Warnings about @file{datarootdir}
2237 * Build Directories::           Supporting multiple concurrent compiles
2238 * Automatic Remaking::          Makefile rules for configuring
2239 @end menu
2241 @node Preset Output Variables
2242 @subsection Preset Output Variables
2243 @cindex Output variables
2245 Some output variables are preset by the Autoconf macros.  Some of the
2246 Autoconf macros set additional output variables, which are mentioned in
2247 the descriptions for those macros.  @xref{Output Variable Index}, for a
2248 complete list of output variables.  @xref{Installation Directory
2249 Variables}, for the list of the preset ones related to installation
2250 directories.  Below are listed the other preset ones.  They all are
2251 precious variables (@pxref{Setting Output Variables},
2252 @code{AC_ARG_VAR}).
2254 @c Just say no to ASCII sorting!  We're humans, not computers.
2255 @c These variables are listed as they would be in a dictionary:
2256 @c actor
2257 @c Actress
2258 @c actress
2260 @defvar CFLAGS
2261 @ovindex CFLAGS
2262 Debugging and optimization options for the C compiler.  If it is not set
2263 in the environment when @command{configure} runs, the default value is set
2264 when you call @code{AC_PROG_CC} (or empty if you don't).  @command{configure}
2265 uses this variable when compiling or linking programs to test for C features.
2267 If a compiler option affects only the behavior of the preprocessor
2268 (e.g., @option{-D @var{name}}), it should be put into @code{CPPFLAGS}
2269 instead.  If it affects only the linker (e.g., @option{-L
2270 @var{directory}}), it should be put into @code{LDFLAGS} instead.  If it
2271 affects only the compiler proper, @code{CFLAGS} is the natural home for
2272 it.  If an option affects multiple phases of the compiler, though,
2273 matters get tricky.  One approach to put such options directly into
2274 @code{CC}, e.g., @code{CC='gcc -m64'}.  Another is to put them into both
2275 @code{CPPFLAGS} and @code{LDFLAGS}, but not into @code{CFLAGS}.
2277 @end defvar
2279 @defvar configure_input
2280 @ovindex configure_input
2281 A comment saying that the file was generated automatically by
2282 @command{configure} and giving the name of the input file.
2283 @code{AC_OUTPUT} adds a comment line containing this variable to the top
2284 of every makefile it creates.  For other files, you should
2285 reference this variable in a comment at the top of each input file.  For
2286 example, an input shell script should begin like this:
2288 @example
2289 #!/bin/sh
2290 # @@configure_input@@
2291 @end example
2293 @noindent
2294 The presence of that line also reminds people editing the file that it
2295 needs to be processed by @command{configure} in order to be used.
2296 @end defvar
2298 @defvar CPPFLAGS
2299 @ovindex CPPFLAGS
2300 Preprocessor options for the C, C++, and Objective C preprocessors and
2301 compilers.  If
2302 it is not set in the environment when @command{configure} runs, the default
2303 value is empty.  @command{configure} uses this variable when preprocessing
2304 or compiling programs to test for C, C++, and Objective C features.
2306 This variable's contents should contain options like @option{-I},
2307 @option{-D}, and @option{-U} that affect only the behavior of the
2308 preprocessor.  Please see the explanation of @code{CFLAGS} for what you
2309 can do if an option affects other phases of the compiler as well.
2311 Currently, @command{configure} always links as part of a single
2312 invocation of the compiler that also preprocesses and compiles, so it
2313 uses this variable also when linking programs.  However, it is unwise to
2314 depend on this behavior because the @acronym{GNU} coding standards do
2315 not require it and many packages do not use @code{CPPFLAGS} when linking
2316 programs.
2318 @xref{Special Chars in Variables}, for limitations that @code{CPPFLAGS}
2319 might run into.
2320 @end defvar
2322 @defvar CXXFLAGS
2323 @ovindex CXXFLAGS
2324 Debugging and optimization options for the C++ compiler.  It acts like
2325 @code{CFLAGS}, but for C++ instead of C.
2326 @end defvar
2328 @defvar DEFS
2329 @ovindex DEFS
2330 @option{-D} options to pass to the C compiler.  If @code{AC_CONFIG_HEADERS}
2331 is called, @command{configure} replaces @samp{@@DEFS@@} with
2332 @option{-DHAVE_CONFIG_H} instead (@pxref{Configuration Headers}).  This
2333 variable is not defined while @command{configure} is performing its tests,
2334 only when creating the output files.  @xref{Setting Output Variables}, for
2335 how to check the results of previous tests.
2336 @end defvar
2338 @defvar ECHO_C
2339 @defvarx ECHO_N
2340 @defvarx ECHO_T
2341 @ovindex ECHO_C
2342 @ovindex ECHO_N
2343 @ovindex ECHO_T
2344 How does one suppress the trailing newline from @command{echo} for
2345 question-answer message pairs?  These variables provide a way:
2347 @example
2348 echo $ECHO_N "And the winner is... $ECHO_C"
2349 sleep 100000000000
2350 echo "$@{ECHO_T@}dead."
2351 @end example
2353 @noindent
2354 Some old and uncommon @command{echo} implementations offer no means to
2355 achieve this, in which case @code{ECHO_T} is set to tab.  You might not
2356 want to use it.
2357 @end defvar
2359 @defvar ERLCFLAGS
2360 @ovindex ERLCFLAGS
2361 Debugging and optimization options for the Erlang compiler.  If it is not set
2362 in the environment when @command{configure} runs, the default value is empty.
2363 @command{configure} uses this variable when compiling
2364 programs to test for Erlang features.
2365 @end defvar
2367 @defvar FCFLAGS
2368 @ovindex FCFLAGS
2369 Debugging and optimization options for the Fortran compiler.  If it
2370 is not set in the environment when @command{configure} runs, the default
2371 value is set when you call @code{AC_PROG_FC} (or empty if you don't).
2372 @command{configure} uses this variable when compiling or linking
2373 programs to test for Fortran features.
2374 @end defvar
2376 @defvar FFLAGS
2377 @ovindex FFLAGS
2378 Debugging and optimization options for the Fortran 77 compiler.  If it
2379 is not set in the environment when @command{configure} runs, the default
2380 value is set when you call @code{AC_PROG_F77} (or empty if you don't).
2381 @command{configure} uses this variable when compiling or linking
2382 programs to test for Fortran 77 features.
2383 @end defvar
2385 @defvar LDFLAGS
2386 @ovindex LDFLAGS
2387 Options for the linker.  If it is not set
2388 in the environment when @command{configure} runs, the default value is empty.
2389 @command{configure} uses this variable when linking programs to test for
2390 C, C++, Objective C, and Fortran features.
2392 This variable's contents should contain options like @option{-s} and
2393 @option{-L} that affect only the behavior of the linker.  Please see the
2394 explanation of @code{CFLAGS} for what you can do if an option also
2395 affects other phases of the compiler.
2397 Don't use this variable to pass library names
2398 (@option{-l}) to the linker; use @code{LIBS} instead.
2399 @end defvar
2401 @defvar LIBS
2402 @ovindex LIBS
2403 @option{-l} options to pass to the linker.  The default value is empty,
2404 but some Autoconf macros may prepend extra libraries to this variable if
2405 those libraries are found and provide necessary functions, see
2406 @ref{Libraries}.  @command{configure} uses this variable when linking
2407 programs to test for C, C++, and Fortran features.
2408 @end defvar
2410 @defvar OBJCFLAGS
2411 @ovindex OBJCFLAGS
2412 Debugging and optimization options for the Objective C compiler.  It
2413 acts like @code{CFLAGS}, but for Objective C instead of C.
2414 @end defvar
2416 @defvar builddir
2417 @ovindex builddir
2418 Rigorously equal to @samp{.}.  Added for symmetry only.
2419 @end defvar
2421 @defvar abs_builddir
2422 @ovindex abs_builddir
2423 Absolute name of @code{builddir}.
2424 @end defvar
2426 @defvar top_builddir
2427 @ovindex top_builddir
2428 The relative name of the top level of the current build tree.  In the
2429 top-level directory, this is the same as @code{builddir}.
2430 @end defvar
2432 @defvar abs_top_builddir
2433 @ovindex abs_top_builddir
2434 Absolute name of @code{top_builddir}.
2435 @end defvar
2437 @defvar srcdir
2438 @ovindex srcdir
2439 The name of the directory that contains the source code for
2440 that makefile.
2441 @end defvar
2443 @defvar abs_srcdir
2444 @ovindex abs_srcdir
2445 Absolute name of @code{srcdir}.
2446 @end defvar
2448 @defvar top_srcdir
2449 @ovindex top_srcdir
2450 The name of the top-level source code directory for the
2451 package.  In the top-level directory, this is the same as @code{srcdir}.
2452 @end defvar
2454 @defvar abs_top_srcdir
2455 @ovindex abs_top_srcdir
2456 Absolute name of @code{top_srcdir}.
2457 @end defvar
2459 @node Installation Directory Variables
2460 @subsection Installation Directory Variables
2461 @cindex Installation directories
2462 @cindex Directories, installation
2464 The following variables specify the directories for
2465 package installation, see @ref{Directory Variables, , Variables for
2466 Installation Directories, standards, The @acronym{GNU} Coding
2467 Standards}, for more information.  Each variable corresponds to an
2468 argument of @command{configure}; trailing slashes are stripped so that
2469 expressions such as @samp{$@{prefix@}/lib} expand with only one slash
2470 between directory names.  See the end of this section for
2471 details on when and how to use these variables.
2473 @defvar bindir
2474 @ovindex bindir
2475 The directory for installing executables that users run.
2476 @end defvar
2478 @defvar datadir
2479 @ovindex datadir
2480 The directory for installing idiosyncratic read-only
2481 architecture-independent data.
2482 @end defvar
2484 @defvar datarootdir
2485 @ovindex datarootdir
2486 The root of the directory tree for read-only architecture-independent
2487 data files.
2488 @end defvar
2490 @defvar docdir
2491 @ovindex docdir
2492 The directory for installing documentation files (other than Info and
2493 man).
2494 @end defvar
2496 @defvar dvidir
2497 @ovindex dvidir
2498 The directory for installing documentation files in DVI format.
2499 @end defvar
2501 @defvar exec_prefix
2502 @ovindex exec_prefix
2503 The installation prefix for architecture-dependent files.  By default
2504 it's the same as @var{prefix}.  You should avoid installing anything
2505 directly to @var{exec_prefix}.  However, the default value for
2506 directories containing architecture-dependent files should be relative
2507 to @var{exec_prefix}.
2508 @end defvar
2510 @defvar htmldir
2511 @ovindex htmldir
2512 The directory for installing HTML documentation.
2513 @end defvar
2515 @defvar includedir
2516 @ovindex includedir
2517 The directory for installing C header files.
2518 @end defvar
2520 @defvar infodir
2521 @ovindex infodir
2522 The directory for installing documentation in Info format.
2523 @end defvar
2525 @defvar libdir
2526 @ovindex libdir
2527 The directory for installing object code libraries.
2528 @end defvar
2530 @defvar libexecdir
2531 @ovindex libexecdir
2532 The directory for installing executables that other programs run.
2533 @end defvar
2535 @defvar localedir
2536 @ovindex localedir
2537 The directory for installing locale-dependent but
2538 architecture-independent data, such as message catalogs.  This directory
2539 usually has a subdirectory per locale.
2540 @end defvar
2542 @defvar localstatedir
2543 @ovindex localstatedir
2544 The directory for installing modifiable single-machine data.
2545 @end defvar
2547 @defvar mandir
2548 @ovindex mandir
2549 The top-level directory for installing documentation in man format.
2550 @end defvar
2552 @defvar oldincludedir
2553 @ovindex oldincludedir
2554 The directory for installing C header files for non-@acronym{GCC} compilers.
2555 @end defvar
2557 @defvar pdfdir
2558 @ovindex pdfdir
2559 The directory for installing PDF documentation.
2560 @end defvar
2562 @defvar prefix
2563 @ovindex prefix
2564 The common installation prefix for all files.  If @var{exec_prefix}
2565 is defined to a different value, @var{prefix} is used only for
2566 architecture-independent files.
2567 @end defvar
2569 @defvar psdir
2570 @ovindex psdir
2571 The directory for installing PostScript documentation.
2572 @end defvar
2574 @defvar sbindir
2575 @ovindex sbindir
2576 The directory for installing executables that system
2577 administrators run.
2578 @end defvar
2580 @defvar sharedstatedir
2581 @ovindex sharedstatedir
2582 The directory for installing modifiable architecture-independent data.
2583 @end defvar
2585 @defvar sysconfdir
2586 @ovindex sysconfdir
2587 The directory for installing read-only single-machine data.
2588 @end defvar
2591 Most of these variables have values that rely on @code{prefix} or
2592 @code{exec_prefix}.  It is deliberate that the directory output
2593 variables keep them unexpanded: typically @samp{@@datarootdir@@} is
2594 replaced by @samp{$@{prefix@}/share}, not @samp{/usr/local/share}, and
2595 @samp{@@datadir@@} is replaced by @samp{$@{datarootdir@}}.
2597 This behavior is mandated by the @acronym{GNU} coding standards, so that when
2598 the user runs:
2600 @table @samp
2601 @item make
2602 she can still specify a different prefix from the one specified to
2603 @command{configure}, in which case, if needed, the package should hard
2604 code dependencies corresponding to the make-specified prefix.
2606 @item make install
2607 she can specify a different installation location, in which case the
2608 package @emph{must} still depend on the location which was compiled in
2609 (i.e., never recompile when @samp{make install} is run).  This is an
2610 extremely important feature, as many people may decide to install all
2611 the files of a package grouped together, and then install links from
2612 the final locations to there.
2613 @end table
2615 In order to support these features, it is essential that
2616 @code{datarootdir} remains being defined as @samp{$@{prefix@}/share} to
2617 depend upon the current value of @code{prefix}.
2619 A corollary is that you should not use these variables except in
2620 makefiles.  For instance, instead of trying to evaluate @code{datadir}
2621 in @file{configure} and hard-coding it in makefiles using
2622 e.g., @samp{AC_DEFINE_UNQUOTED([DATADIR], ["$datadir"], [Data directory.])},
2623 you should add
2624 @option{-DDATADIR='$(datadir)'} to your makefile's definition of
2625 @code{CPPFLAGS} (@code{AM_CPPFLAGS} if you are also using Automake).
2627 Similarly, you should not rely on @code{AC_CONFIG_FILES} to replace
2628 @code{datadir} and friends in your shell scripts and other files; instead,
2629 let @command{make} manage their replacement.  For instance Autoconf
2630 ships templates of its shell scripts ending with @samp{.in}, and uses a
2631 makefile snippet similar to the following to build scripts like
2632 @command{autoheader} and @command{autom4te}:
2634 @example
2635 @group
2636 edit = sed \
2637         -e 's|@@datadir[@@]|$(pkgdatadir)|g' \
2638         -e 's|@@prefix[@@]|$(prefix)|g'
2639 @end group
2641 @group
2642 autoheader autom4te: Makefile
2643         rm -f $@@ $@@.tmp
2644         $(edit) '$(srcdir)/$@@.in' >$@@.tmp
2645         chmod +x $@@.tmp
2646         chmod a-w $@@.tmp
2647         mv $@@.tmp $@@
2648 @end group
2650 @group
2651 autoheader: $(srcdir)/autoheader.in
2652 autom4te: $(srcdir)/autom4te.in
2653 @end group
2654 @end example
2656 Some details are noteworthy:
2658 @table @asis
2659 @item @samp{@@datadir[@@]}
2660 The brackets prevent @command{configure} from replacing
2661 @samp{@@datadir@@} in the Sed expression itself.
2662 Brackets are preferable to a backslash here, since
2663 Posix says @samp{\@@} is not portable.
2665 @item @samp{$(pkgdatadir)}
2666 Don't use @samp{@@pkgdatadir@@}!  Use the matching makefile variable
2667 instead.
2669 @item @samp{/}
2670 Don't use @samp{/} in the Sed expressions that replace file names since
2671 most likely the
2672 variables you use, such as @samp{$(pkgdatadir)}, contain @samp{/}.
2673 Use a shell metacharacter instead, such as @samp{|}.
2675 @item special characters
2676 File names, file name components, and the value of @code{VPATH} should
2677 not contain shell metacharacters or white
2678 space.  @xref{Special Chars in Variables}.
2680 @item dependency on @file{Makefile}
2681 Since @code{edit} uses values that depend on the configuration specific
2682 values (@code{prefix}, etc.)@: and not only on @code{VERSION} and so forth,
2683 the output depends on @file{Makefile}, not @file{configure.ac}.
2685 @item @samp{$@@}
2686 The main rule is generic, and uses @samp{$@@} extensively to
2687 avoid the need for multiple copies of the rule.
2689 @item Separated dependencies and single suffix rules
2690 You can't use them!  The above snippet cannot be (portably) rewritten
2693 @example
2694 autoconf autoheader: Makefile
2695 @group
2696 .in:
2697         rm -f $@@ $@@.tmp
2698         $(edit) $< >$@@.tmp
2699         chmod +x $@@.tmp
2700         mv $@@.tmp $@@
2701 @end group
2702 @end example
2704 @xref{Single Suffix Rules}, for details.
2706 @item @samp{$(srcdir)}
2707 Be sure to specify the name of the source directory,
2708 otherwise the package won't support separated builds.
2709 @end table
2711 For the more specific installation of Erlang libraries, the following variables
2712 are defined:
2714 @defvar ERLANG_INSTALL_LIB_DIR
2715 @ovindex ERLANG_INSTALL_LIB_DIR
2716 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
2717 The common parent directory of Erlang library installation directories.
2718 This variable is set by calling the @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR}
2719 macro in @file{configure.ac}.
2720 @end defvar
2722 @defvar ERLANG_INSTALL_LIB_DIR_@var{library}
2723 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
2724 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
2725 The installation directory for Erlang library @var{library}.
2726 This variable is set by calling the
2727 @samp{AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR(@var{library}, @var{version}}
2728 macro in @file{configure.ac}.
2729 @end defvar
2731 @xref{Erlang Libraries}, for details.
2734 @node Changed Directory Variables
2735 @subsection Changed Directory Variables
2736 @cindex @file{datarootdir}
2738 In Autoconf 2.60, the set of directory variables has changed, and the
2739 defaults of some variables have been adjusted
2740 (@pxref{Installation Directory Variables}) to changes in the
2741 @acronym{GNU} Coding Standards.  Notably, @file{datadir}, @file{infodir}, and
2742 @file{mandir} are now expressed in terms of @file{datarootdir}.  If you are
2743 upgrading from an earlier Autoconf version, you may need to adjust your files
2744 to ensure that the directory variables are substituted correctly
2745 (@pxref{Defining Directories}), and that a definition of @file{datarootdir} is
2746 in place.  For example, in a @file{Makefile.in}, adding
2748 @example
2749 datarootdir = @@datarootdir@@
2750 @end example
2752 @noindent
2753 is usually sufficient.  If you use Automake to create @file{Makefile.in},
2754 it will add this for you.
2756 To help with the transition, Autoconf warns about files that seem to use
2757 @code{datarootdir} without defining it.  In some cases, it then expands
2758 the value of @code{$datarootdir} in substitutions of the directory
2759 variables.  The following example shows such a warning:
2761 @example
2762 $ @kbd{cat configure.ac}
2763 AC_INIT
2764 AC_CONFIG_FILES([Makefile])
2765 AC_OUTPUT
2766 $ @kbd{cat Makefile.in}
2767 prefix = @@prefix@@
2768 datadir = @@datadir@@
2769 $ @kbd{autoconf}
2770 $ @kbd{configure}
2771 configure: creating ./config.status
2772 config.status: creating Makefile
2773 config.status: WARNING:
2774                Makefile.in seems to ignore the --datarootdir setting
2775 $ @kbd{cat Makefile}
2776 prefix = /usr/local
2777 datadir = $@{prefix@}/share
2778 @end example
2780 Usually one can easily change the file to accommodate both older and newer
2781 Autoconf releases:
2783 @example
2784 $ @kbd{cat Makefile.in}
2785 prefix = @@prefix@@
2786 datarootdir = @@datarootdir@@
2787 datadir = @@datadir@@
2788 $ @kbd{configure}
2789 configure: creating ./config.status
2790 config.status: creating Makefile
2791 $ @kbd{cat Makefile}
2792 prefix = /usr/local
2793 datarootdir = $@{prefix@}/share
2794 datadir = $@{datarootdir@}
2795 @end example
2797 @acindex{DATAROOTDIR_CHECKED}
2798 In some cases, however, the checks may not be able to detect that a suitable
2799 initialization of @code{datarootdir} is in place, or they may fail to detect
2800 that such an initialization is necessary in the output file.  If, after
2801 auditing your package, there are still spurious @file{configure} warnings about
2802 @code{datarootdir}, you may add the line
2804 @example
2805 AC_DEFUN([AC_DATAROOTDIR_CHECKED])
2806 @end example
2808 @noindent
2809 to your @file{configure.ac} to disable the warnings.  This is an exception
2810 to the usual rule that you should not define a macro whose name begins with
2811 @code{AC_} (@pxref{Macro Names}).
2815 @node Build Directories
2816 @subsection Build Directories
2817 @cindex Build directories
2818 @cindex Directories, build
2820 You can support compiling a software package for several architectures
2821 simultaneously from the same copy of the source code.  The object files
2822 for each architecture are kept in their own directory.
2824 To support doing this, @command{make} uses the @code{VPATH} variable to
2825 find the files that are in the source directory.  @acronym{GNU} Make
2826 can do this.  Most other recent @command{make} programs can do this as
2827 well, though they may have difficulties and it is often simpler to
2828 recommend @acronym{GNU} @command{make} (@pxref{VPATH and Make}).  Older
2829 @command{make} programs do not support @code{VPATH}; when using them, the
2830 source code must be in the same directory as the object files.
2832 To support @code{VPATH}, each @file{Makefile.in} should contain two
2833 lines that look like:
2835 @example
2836 srcdir = @@srcdir@@
2837 VPATH = @@srcdir@@
2838 @end example
2840 Do not set @code{VPATH} to the value of another variable, for example
2841 @samp{VPATH = $(srcdir)}, because some versions of @command{make} do not do
2842 variable substitutions on the value of @code{VPATH}.
2844 @command{configure} substitutes the correct value for @code{srcdir} when
2845 it produces @file{Makefile}.
2847 Do not use the @code{make} variable @code{$<}, which expands to the
2848 file name of the file in the source directory (found with @code{VPATH}),
2849 except in implicit rules.  (An implicit rule is one such as @samp{.c.o},
2850 which tells how to create a @file{.o} file from a @file{.c} file.)  Some
2851 versions of @command{make} do not set @code{$<} in explicit rules; they
2852 expand it to an empty value.
2854 Instead, Make command lines should always refer to source
2855 files by prefixing them with @samp{$(srcdir)/}.  For example:
2857 @example
2858 time.info: time.texinfo
2859         $(MAKEINFO) '$(srcdir)/time.texinfo'
2860 @end example
2862 @node Automatic Remaking
2863 @subsection Automatic Remaking
2864 @cindex Automatic remaking
2865 @cindex Remaking automatically
2867 You can put rules like the following in the top-level @file{Makefile.in}
2868 for a package to automatically update the configuration information when
2869 you change the configuration files.  This example includes all of the
2870 optional files, such as @file{aclocal.m4} and those related to
2871 configuration header files.  Omit from the @file{Makefile.in} rules for
2872 any of these files that your package does not use.
2874 The @samp{$(srcdir)/} prefix is included because of limitations in the
2875 @code{VPATH} mechanism.
2877 The @file{stamp-} files are necessary because the timestamps of
2878 @file{config.h.in} and @file{config.h} are not changed if remaking
2879 them does not change their contents.  This feature avoids unnecessary
2880 recompilation.  You should include the file @file{stamp-h.in} your
2881 package's distribution, so that @command{make} considers
2882 @file{config.h.in} up to date.  Don't use @command{touch}
2883 (@pxref{Limitations of Usual Tools}); instead, use @command{echo} (using
2884 @command{date} would cause needless differences, hence @acronym{CVS}
2885 conflicts, etc.).
2887 @example
2888 @group
2889 $(srcdir)/configure: configure.ac aclocal.m4
2890         cd '$(srcdir)' && autoconf
2892 # autoheader might not change config.h.in, so touch a stamp file.
2893 $(srcdir)/config.h.in: stamp-h.in
2894 $(srcdir)/stamp-h.in: configure.ac aclocal.m4
2895         cd '$(srcdir)' && autoheader
2896         echo timestamp > '$(srcdir)/stamp-h.in'
2898 config.h: stamp-h
2899 stamp-h: config.h.in config.status
2900         ./config.status
2902 Makefile: Makefile.in config.status
2903         ./config.status
2905 config.status: configure
2906         ./config.status --recheck
2907 @end group
2908 @end example
2910 @noindent
2911 (Be careful if you copy these lines directly into your makefile, as you
2912 need to convert the indented lines to start with the tab character.)
2914 In addition, you should use
2916 @example
2917 AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h])
2918 @end example
2920 @noindent
2921 so @file{config.status} ensures that @file{config.h} is considered up to
2922 date.  @xref{Output}, for more information about @code{AC_OUTPUT}.
2924 @xref{config.status Invocation}, for more examples of handling
2925 configuration-related dependencies.
2927 @node Configuration Headers
2928 @section Configuration Header Files
2929 @cindex Configuration Header
2930 @cindex @file{config.h}
2932 When a package contains more than a few tests that define C preprocessor
2933 symbols, the command lines to pass @option{-D} options to the compiler
2934 can get quite long.  This causes two problems.  One is that the
2935 @command{make} output is hard to visually scan for errors.  More
2936 seriously, the command lines can exceed the length limits of some
2937 operating systems.  As an alternative to passing @option{-D} options to
2938 the compiler, @command{configure} scripts can create a C header file
2939 containing @samp{#define} directives.  The @code{AC_CONFIG_HEADERS}
2940 macro selects this kind of output.  Though it can be called anywhere
2941 between @code{AC_INIT} and @code{AC_OUTPUT}, it is customary to call
2942 it right after @code{AC_INIT}.
2944 The package should @samp{#include} the configuration header file before
2945 any other header files, to prevent inconsistencies in declarations (for
2946 example, if it redefines @code{const}).
2948 To provide for VPATH builds, remember to pass the C compiler a @option{-I.}
2949 option (or @option{-I..}; whichever directory contains @file{config.h}).
2950 Even if you use @samp{#include "config.h"}, the preprocessor searches only
2951 the directory of the currently read file, i.e., the source directory, not
2952 the build directory.
2954 With the appropriate @option{-I} option, you can use
2955 @samp{#include <config.h>}.  Actually, it's a good habit to use it,
2956 because in the rare case when the source directory contains another
2957 @file{config.h}, the build directory should be searched first.
2960 @defmac AC_CONFIG_HEADERS (@var{header} @dots{}, @ovar{cmds}, @ovar{init-cmds})
2961 @acindex{CONFIG_HEADERS}
2962 @cvindex HAVE_CONFIG_H
2963 This macro is one of the instantiating macros; see @ref{Configuration
2964 Actions}.  Make @code{AC_OUTPUT} create the file(s) in the
2965 blank-or-newline-separated list @var{header} containing C preprocessor
2966 @code{#define} statements, and replace @samp{@@DEFS@@} in generated
2967 files with @option{-DHAVE_CONFIG_H} instead of the value of @code{DEFS}.
2968 The usual name for @var{header} is @file{config.h}.
2970 If @var{header} already exists and its contents are identical to what
2971 @code{AC_OUTPUT} would put in it, it is left alone.  Doing this allows
2972 making some changes in the configuration without needlessly causing
2973 object files that depend on the header file to be recompiled.
2975 Usually the input file is named @file{@var{header}.in}; however, you can
2976 override the input file name by appending to @var{header} a
2977 colon-separated list of input files.  For example, you might need to make
2978 the input file name acceptable to @acronym{DOS} variants:
2980 @example
2981 AC_CONFIG_HEADERS([config.h:config.hin])
2982 @end example
2984 @end defmac
2986 @defmac AH_HEADER
2987 @ahindex{HEADER}
2988 This macro is defined as the name of the first declared config header
2989 and undefined if no config headers have been declared up to this point.
2990 A third-party macro may, for example, require use of a config header
2991 without invoking AC_CONFIG_HEADERS twice, like this:
2993 @example
2994 AC_CONFIG_COMMANDS_PRE(
2995         [m4_ifndef([AH_HEADER], [AC_CONFIG_HEADERS([config.h])])])
2996 @end example
2998 @end defmac
3000 @xref{Configuration Actions}, for more details on @var{header}.
3002 @menu
3003 * Header Templates::            Input for the configuration headers
3004 * autoheader Invocation::       How to create configuration templates
3005 * Autoheader Macros::           How to specify CPP templates
3006 @end menu
3008 @node Header Templates
3009 @subsection Configuration Header Templates
3010 @cindex Configuration Header Template
3011 @cindex Header templates
3012 @cindex @file{config.h.in}
3014 Your distribution should contain a template file that looks as you want
3015 the final header file to look, including comments, with @code{#undef}
3016 statements which are used as hooks.  For example, suppose your
3017 @file{configure.ac} makes these calls:
3019 @example
3020 AC_CONFIG_HEADERS([conf.h])
3021 AC_CHECK_HEADERS([unistd.h])
3022 @end example
3024 @noindent
3025 Then you could have code like the following in @file{conf.h.in}.  On
3026 systems that have @file{unistd.h}, @command{configure} defines
3027 @samp{HAVE_UNISTD_H} to 1.  On other systems, the whole line is
3028 commented out (in case the system predefines that symbol).
3030 @example
3031 @group
3032 /* Define as 1 if you have unistd.h.  */
3033 #undef HAVE_UNISTD_H
3034 @end group
3035 @end example
3037 Pay attention that @samp{#undef} is in the first column, and there is
3038 nothing after @samp{HAVE_UNISTD_H}, not even white space.  You can
3039 then decode the configuration header using the preprocessor directives:
3041 @example
3042 @group
3043 #include <conf.h>
3045 #ifdef HAVE_UNISTD_H
3046 # include <unistd.h>
3047 #else
3048 /* We are in trouble.  */
3049 #endif
3050 @end group
3051 @end example
3053 The use of old form templates, with @samp{#define} instead of
3054 @samp{#undef} is strongly discouraged.  Similarly with old templates
3055 with comments on the same line as the @samp{#undef}.  Anyway, putting
3056 comments in preprocessor macros has never been a good idea.
3058 Since it is a tedious task to keep a template header up to date, you may
3059 use @command{autoheader} to generate it, see @ref{autoheader Invocation}.
3062 @node autoheader Invocation
3063 @subsection Using @command{autoheader} to Create @file{config.h.in}
3064 @cindex @command{autoheader}
3066 The @command{autoheader} program can create a template file of C
3067 @samp{#define} statements for @command{configure} to use.
3068 It searches for the first invocation of @code{AC_CONFIG_HEADERS} in
3069 @file{configure} sources to determine the name of the template.
3070 (If the first call of @code{AC_CONFIG_HEADERS} specifies more than one
3071 input file name, @command{autoheader} uses the first one.)
3073 It is recommended that only one input file is used.  If you want to append
3074 a boilerplate code, it is preferable to use
3075 @samp{AH_BOTTOM([#include <conf_post.h>])}.
3076 File @file{conf_post.h} is not processed during the configuration then,
3077 which make things clearer.  Analogically, @code{AH_TOP} can be used to
3078 prepend a boilerplate code.
3080 In order to do its job, @command{autoheader} needs you to document all
3081 of the symbols that you might use.  Typically this is done via an
3082 @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED} call whose first argument
3083 is a literal symbol and whose third argument describes the symbol
3084 (@pxref{Defining Symbols}).  Alternatively, you can use
3085 @code{AH_TEMPLATE} (@pxref{Autoheader Macros}), or you can supply a
3086 suitable input file for a subsequent configuration header file.
3087 Symbols defined by Autoconf's builtin tests are already documented properly;
3088 you need to document only those that you
3089 define yourself.
3091 You might wonder why @command{autoheader} is needed: after all, why
3092 would @command{configure} need to ``patch'' a @file{config.h.in} to
3093 produce a @file{config.h} instead of just creating @file{config.h} from
3094 scratch?  Well, when everything rocks, the answer is just that we are
3095 wasting our time maintaining @command{autoheader}: generating
3096 @file{config.h} directly is all that is needed.  When things go wrong,
3097 however, you'll be thankful for the existence of @command{autoheader}.
3099 The fact that the symbols are documented is important in order to
3100 @emph{check} that @file{config.h} makes sense.  The fact that there is a
3101 well-defined list of symbols that should be defined (or not) is
3102 also important for people who are porting packages to environments where
3103 @command{configure} cannot be run: they just have to @emph{fill in the
3104 blanks}.
3106 But let's come back to the point: the invocation of @command{autoheader}@dots{}
3108 If you give @command{autoheader} an argument, it uses that file instead
3109 of @file{configure.ac} and writes the header file to the standard output
3110 instead of to @file{config.h.in}.  If you give @command{autoheader} an
3111 argument of @option{-}, it reads the standard input instead of
3112 @file{configure.ac} and writes the header file to the standard output.
3114 @command{autoheader} accepts the following options:
3116 @table @option
3117 @item --help
3118 @itemx -h
3119 Print a summary of the command line options and exit.
3121 @item --version
3122 @itemx -V
3123 Print the version number of Autoconf and exit.
3125 @item --verbose
3126 @itemx -v
3127 Report processing steps.
3129 @item --debug
3130 @itemx -d
3131 Don't remove the temporary files.
3133 @item --force
3134 @itemx -f
3135 Remake the template file even if newer than its input files.
3137 @item --include=@var{dir}
3138 @itemx -I @var{dir}
3139 Append @var{dir} to the include path.  Multiple invocations accumulate.
3141 @item --prepend-include=@var{dir}
3142 @item -B @var{dir}
3143 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
3145 @item --warnings=@var{category}
3146 @itemx -W @var{category}
3147 @evindex WARNINGS
3148 Report the warnings related to @var{category} (which can actually be a
3149 comma separated list).  Current categories include:
3151 @table @samp
3152 @item obsolete
3153 report the uses of obsolete constructs
3155 @item all
3156 report all the warnings
3158 @item none
3159 report none
3161 @item error
3162 treats warnings as errors
3164 @item no-@var{category}
3165 disable warnings falling into @var{category}
3166 @end table
3168 @end table
3172 @node Autoheader Macros
3173 @subsection Autoheader Macros
3174 @cindex Autoheader macros
3176 @command{autoheader} scans @file{configure.ac} and figures out which C
3177 preprocessor symbols it might define.  It knows how to generate
3178 templates for symbols defined by @code{AC_CHECK_HEADERS},
3179 @code{AC_CHECK_FUNCS} etc., but if you @code{AC_DEFINE} any additional
3180 symbol, you must define a template for it.  If there are missing
3181 templates, @command{autoheader} fails with an error message.
3183 The template for a @var{symbol} is created
3184 by @command{autoheader} from
3185 the @var{description} argument to an @code{AC_DEFINE};
3186 see @ref{Defining Symbols}.
3188 For special needs, you can use the following macros.
3191 @defmac AH_TEMPLATE (@var{key}, @var{description})
3192 @ahindex{TEMPLATE}
3193 Tell @command{autoheader} to generate a template for @var{key}.  This macro
3194 generates standard templates just like @code{AC_DEFINE} when a
3195 @var{description} is given.
3197 For example:
3199 @example
3200 AH_TEMPLATE([CRAY_STACKSEG_END],
3201             [Define to one of _getb67, GETB67, getb67
3202              for Cray-2 and Cray-YMP systems.  This
3203              function is required for alloca.c support
3204              on those systems.])
3205 @end example
3207 @noindent
3208 generates the following template, with the description properly
3209 justified.
3211 @example
3212 /* Define to one of _getb67, GETB67, getb67 for Cray-2 and
3213    Cray-YMP systems.  This function is required for alloca.c
3214    support on those systems.  */
3215 #undef CRAY_STACKSEG_END
3216 @end example
3217 @end defmac
3220 @defmac AH_VERBATIM (@var{key}, @var{template})
3221 @ahindex{VERBATIM}
3222 Tell @command{autoheader} to include the @var{template} as-is in the header
3223 template file.  This @var{template} is associated with the @var{key},
3224 which is used to sort all the different templates and guarantee their
3225 uniqueness.  It should be a symbol that can be defined via @code{AC_DEFINE}.
3226 @end defmac
3229 @defmac AH_TOP (@var{text})
3230 @ahindex{TOP}
3231 Include @var{text} at the top of the header template file.
3232 @end defmac
3235 @defmac AH_BOTTOM (@var{text})
3236 @ahindex{BOTTOM}
3237 Include @var{text} at the bottom of the header template file.
3238 @end defmac
3241 Please note that @var{text} gets included ``verbatim'' to the template file,
3242 not to the resulting config header, so it can easily get mangled when the
3243 template is processed.  There is rarely a need for something other than
3245 @example
3246 AH_BOTTOM([#include <custom.h>])
3247 @end example
3251 @node Configuration Commands
3252 @section Running Arbitrary Configuration Commands
3253 @cindex Configuration commands
3254 @cindex Commands for configuration
3256 You can execute arbitrary commands before, during, and after
3257 @file{config.status} is run.  The three following macros accumulate the
3258 commands to run when they are called multiple times.
3259 @code{AC_CONFIG_COMMANDS} replaces the obsolete macro
3260 @code{AC_OUTPUT_COMMANDS}; see @ref{Obsolete Macros}, for details.
3262 @anchor{AC_CONFIG_COMMANDS}
3263 @defmac AC_CONFIG_COMMANDS (@var{tag}@dots{}, @ovar{cmds}, @ovar{init-cmds})
3264 @acindex{CONFIG_COMMANDS}
3265 Specify additional shell commands to run at the end of
3266 @file{config.status}, and shell commands to initialize any variables
3267 from @command{configure}.  Associate the commands with @var{tag}.
3268 Since typically the @var{cmds} create a file, @var{tag} should
3269 naturally be the name of that file.  If needed, the directory hosting
3270 @var{tag} is created.  This macro is one of the instantiating macros;
3271 see @ref{Configuration Actions}.
3273 Here is an unrealistic example:
3274 @example
3275 fubar=42
3276 AC_CONFIG_COMMANDS([fubar],
3277                    [echo this is extra $fubar, and so on.],
3278                    [fubar=$fubar])
3279 @end example
3281 Here is a better one:
3282 @example
3283 AC_CONFIG_COMMANDS([timestamp], [date >timestamp])
3284 @end example
3285 @end defmac
3287 The following two macros look similar, but in fact they are not of the same
3288 breed: they are executed directly by @file{configure}, so you cannot use
3289 @file{config.status} to rerun them.
3291 @c Yet it is good to leave them here.  The user sees them together and
3292 @c decides which best fits their needs.
3294 @defmac AC_CONFIG_COMMANDS_PRE (@var{cmds})
3295 @acindex{CONFIG_COMMANDS_PRE}
3296 Execute the @var{cmds} right before creating @file{config.status}.
3298 This macro presents the last opportunity to call @code{AC_SUBST},
3299 @code{AC_DEFINE}, or @code{AC_CONFIG_FOOS} macros.
3300 @end defmac
3302 @defmac AC_CONFIG_COMMANDS_POST (@var{cmds})
3303 @acindex{CONFIG_COMMANDS_POST}
3304 Execute the @var{cmds} right after creating @file{config.status}.
3305 @end defmac
3310 @node Configuration Links
3311 @section Creating Configuration Links
3312 @cindex Configuration links
3313 @cindex Links for configuration
3315 You may find it convenient to create links whose destinations depend upon
3316 results of tests.  One can use @code{AC_CONFIG_COMMANDS} but the
3317 creation of relative symbolic links can be delicate when the package is
3318 built in a directory different from the source directory.
3320 @anchor{AC_CONFIG_LINKS}
3321 @defmac AC_CONFIG_LINKS (@var{dest}:@var{source}@dots{}, @ovar{cmds}, @
3322   @ovar{init-cmds})
3323 @acindex{CONFIG_LINKS}
3324 @cindex Links
3325 Make @code{AC_OUTPUT} link each of the existing files @var{source} to
3326 the corresponding link name @var{dest}.  Makes a symbolic link if
3327 possible, otherwise a hard link if possible, otherwise a copy.  The
3328 @var{dest} and @var{source} names should be relative to the top level
3329 source or build directory.  This macro is one of the instantiating
3330 macros; see @ref{Configuration Actions}.
3332 For example, this call:
3334 @example
3335 AC_CONFIG_LINKS([host.h:config/$machine.h
3336                 object.h:config/$obj_format.h])
3337 @end example
3339 @noindent
3340 creates in the current directory @file{host.h} as a link to
3341 @file{@var{srcdir}/config/$machine.h}, and @file{object.h} as a
3342 link to @file{@var{srcdir}/config/$obj_format.h}.
3344 The tempting value @samp{.} for @var{dest} is invalid: it makes it
3345 impossible for @samp{config.status} to guess the links to establish.
3347 One can then run:
3348 @example
3349 ./config.status host.h object.h
3350 @end example
3351 @noindent
3352 to create the links.
3353 @end defmac
3357 @node Subdirectories
3358 @section Configuring Other Packages in Subdirectories
3359 @cindex Configure subdirectories
3360 @cindex Subdirectory configure
3362 In most situations, calling @code{AC_OUTPUT} is sufficient to produce
3363 makefiles in subdirectories.  However, @command{configure} scripts
3364 that control more than one independent package can use
3365 @code{AC_CONFIG_SUBDIRS} to run @command{configure} scripts for other
3366 packages in subdirectories.
3368 @defmac AC_CONFIG_SUBDIRS (@var{dir} @dots{})
3369 @acindex{CONFIG_SUBDIRS}
3370 @ovindex subdirs
3371 Make @code{AC_OUTPUT} run @command{configure} in each subdirectory
3372 @var{dir} in the given blank-or-newline-separated list.  Each @var{dir} should
3373 be a literal, i.e., please do not use:
3375 @example
3376 if test "$package_foo_enabled" = yes; then
3377   $my_subdirs="$my_subdirs foo"
3379 AC_CONFIG_SUBDIRS([$my_subdirs])
3380 @end example
3382 @noindent
3383 because this prevents @samp{./configure --help=recursive} from
3384 displaying the options of the package @code{foo}.  Instead, you should
3385 write:
3387 @example
3388 if test "$package_foo_enabled" = yes; then
3389   AC_CONFIG_SUBDIRS([foo])
3391 @end example
3393 If a given @var{dir} is not found, an error is reported: if the
3394 subdirectory is optional, write:
3396 @example
3397 if test -d "$srcdir/foo"; then
3398   AC_CONFIG_SUBDIRS([foo])
3400 @end example
3402 @c NB: Yes, below we mean configure.in, not configure.ac.
3403 If a given @var{dir} contains @command{configure.gnu}, it is run instead
3404 of @command{configure}.  This is for packages that might use a
3405 non-Autoconf script @command{Configure}, which can't be called through a
3406 wrapper @command{configure} since it would be the same file on
3407 case-insensitive file systems.  Likewise, if a @var{dir} contains
3408 @file{configure.in} but no @command{configure}, the Cygnus
3409 @command{configure} script found by @code{AC_CONFIG_AUX_DIR} is used.
3411 The subdirectory @command{configure} scripts are given the same command
3412 line options that were given to this @command{configure} script, with minor
3413 changes if needed, which include:
3415 @itemize @minus
3416 @item
3417 adjusting a relative name for the cache file;
3419 @item
3420 adjusting a relative name for the source directory;
3422 @item
3423 propagating the current value of @code{$prefix}, including if it was
3424 defaulted, and if the default values of the top level and of the subdirectory
3425 @file{configure} differ.
3426 @end itemize
3428 This macro also sets the output variable @code{subdirs} to the list of
3429 directories @samp{@var{dir} @dots{}}.  Make rules can use
3430 this variable to determine which subdirectories to recurse into.
3432 This macro may be called multiple times.
3433 @end defmac
3435 @node Default Prefix
3436 @section Default Prefix
3437 @cindex Install prefix
3438 @cindex Prefix for install
3440 By default, @command{configure} sets the prefix for files it installs to
3441 @file{/usr/local}.  The user of @command{configure} can select a different
3442 prefix using the @option{--prefix} and @option{--exec-prefix} options.
3443 There are two ways to change the default: when creating
3444 @command{configure}, and when running it.
3446 Some software packages might want to install in a directory other than
3447 @file{/usr/local} by default.  To accomplish that, use the
3448 @code{AC_PREFIX_DEFAULT} macro.
3450 @defmac AC_PREFIX_DEFAULT (@var{prefix})
3451 @acindex{PREFIX_DEFAULT}
3452 Set the default installation prefix to @var{prefix} instead of
3453 @file{/usr/local}.
3454 @end defmac
3456 It may be convenient for users to have @command{configure} guess the
3457 installation prefix from the location of a related program that they
3458 have already installed.  If you wish to do that, you can call
3459 @code{AC_PREFIX_PROGRAM}.
3461 @anchor{AC_PREFIX_PROGRAM}
3462 @defmac AC_PREFIX_PROGRAM (@var{program})
3463 @acindex{PREFIX_PROGRAM}
3464 If the user did not specify an installation prefix (using the
3465 @option{--prefix} option), guess a value for it by looking for
3466 @var{program} in @env{PATH}, the way the shell does.  If @var{program}
3467 is found, set the prefix to the parent of the directory containing
3468 @var{program}, else default the prefix as described above
3469 (@file{/usr/local} or @code{AC_PREFIX_DEFAULT}).  For example, if
3470 @var{program} is @code{gcc} and the @env{PATH} contains
3471 @file{/usr/local/gnu/bin/gcc}, set the prefix to @file{/usr/local/gnu}.
3472 @end defmac
3476 @c ======================================================== Existing tests
3478 @node Existing Tests
3479 @chapter Existing Tests
3481 These macros test for particular system features that packages might
3482 need or want to use.  If you need to test for a kind of feature that
3483 none of these macros check for, you can probably do it by calling
3484 primitive test macros with appropriate arguments (@pxref{Writing
3485 Tests}).
3487 These tests print messages telling the user which feature they're
3488 checking for, and what they find.  They cache their results for future
3489 @command{configure} runs (@pxref{Caching Results}).
3491 Some of these macros set output variables.  @xref{Makefile
3492 Substitutions}, for how to get their values.  The phrase ``define
3493 @var{name}'' is used below as a shorthand to mean ``define the C
3494 preprocessor symbol @var{name} to the value 1''.  @xref{Defining
3495 Symbols}, for how to get those symbol definitions into your program.
3497 @menu
3498 * Common Behavior::             Macros' standard schemes
3499 * Alternative Programs::        Selecting between alternative programs
3500 * Files::                       Checking for the existence of files
3501 * Libraries::                   Library archives that might be missing
3502 * Library Functions::           C library functions that might be missing
3503 * Header Files::                Header files that might be missing
3504 * Declarations::                Declarations that may be missing
3505 * Structures::                  Structures or members that might be missing
3506 * Types::                       Types that might be missing
3507 * Compilers and Preprocessors::  Checking for compiling programs
3508 * System Services::             Operating system services
3509 * Posix Variants::              Special kludges for specific Posix variants
3510 * Erlang Libraries::            Checking for the existence of Erlang libraries
3511 @end menu
3513 @node Common Behavior
3514 @section Common Behavior
3515 @cindex Common autoconf behavior
3517 Much effort has been expended to make Autoconf easy to learn.  The most
3518 obvious way to reach this goal is simply to enforce standard interfaces
3519 and behaviors, avoiding exceptions as much as possible.  Because of
3520 history and inertia, unfortunately, there are still too many exceptions
3521 in Autoconf; nevertheless, this section describes some of the common
3522 rules.
3524 @menu
3525 * Standard Symbols::            Symbols defined by the macros
3526 * Default Includes::            Includes used by the generic macros
3527 @end menu
3529 @node Standard Symbols
3530 @subsection Standard Symbols
3531 @cindex Standard symbols
3533 All the generic macros that @code{AC_DEFINE} a symbol as a result of
3534 their test transform their @var{argument} values to a standard alphabet.
3535 First, @var{argument} is converted to upper case and any asterisks
3536 (@samp{*}) are each converted to @samp{P}.  Any remaining characters
3537 that are not alphanumeric are converted to underscores.
3539 For instance,
3541 @example
3542 AC_CHECK_TYPES([struct $Expensive*])
3543 @end example
3545 @noindent
3546 defines the symbol @samp{HAVE_STRUCT__EXPENSIVEP} if the check
3547 succeeds.
3550 @node Default Includes
3551 @subsection Default Includes
3552 @cindex Default includes
3553 @cindex Includes, default
3555 Several tests depend upon a set of header files.  Since these headers
3556 are not universally available, tests actually have to provide a set of
3557 protected includes, such as:
3559 @example
3560 @group
3561 #ifdef TIME_WITH_SYS_TIME
3562 # include <sys/time.h>
3563 # include <time.h>
3564 #else
3565 # ifdef HAVE_SYS_TIME_H
3566 #  include <sys/time.h>
3567 # else
3568 #  include <time.h>
3569 # endif
3570 #endif
3571 @end group
3572 @end example
3574 @noindent
3575 Unless you know exactly what you are doing, you should avoid using
3576 unconditional includes, and check the existence of the headers you
3577 include beforehand (@pxref{Header Files}).
3579 Most generic macros use the following macro to provide the default set
3580 of includes:
3582 @defmac AC_INCLUDES_DEFAULT (@ovar{include-directives})
3583 @acindex{INCLUDES_DEFAULT}
3584 Expand to @var{include-directives} if defined, otherwise to:
3586 @example
3587 @group
3588 #include <stdio.h>
3589 #ifdef HAVE_SYS_TYPES_H
3590 # include <sys/types.h>
3591 #endif
3592 #ifdef HAVE_SYS_STAT_H
3593 # include <sys/stat.h>
3594 #endif
3595 #ifdef STDC_HEADERS
3596 # include <stdlib.h>
3597 # include <stddef.h>
3598 #else
3599 # ifdef HAVE_STDLIB_H
3600 #  include <stdlib.h>
3601 # endif
3602 #endif
3603 #ifdef HAVE_STRING_H
3604 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
3605 #  include <memory.h>
3606 # endif
3607 # include <string.h>
3608 #endif
3609 #ifdef HAVE_STRINGS_H
3610 # include <strings.h>
3611 #endif
3612 #ifdef HAVE_INTTYPES_H
3613 # include <inttypes.h>
3614 #endif
3615 #ifdef HAVE_STDINT_H
3616 # include <stdint.h>
3617 #endif
3618 #ifdef HAVE_UNISTD_H
3619 # include <unistd.h>
3620 #endif
3621 @end group
3622 @end example
3624 If the default includes are used, then check for the presence of these
3625 headers and their compatibility, i.e., you don't need to run
3626 @code{AC_HEADER_STDC}, nor check for @file{stdlib.h} etc.
3628 These headers are checked for in the same order as they are included.
3629 For instance, on some systems @file{string.h} and @file{strings.h} both
3630 exist, but conflict.  Then @code{HAVE_STRING_H} is defined, not
3631 @code{HAVE_STRINGS_H}.
3632 @end defmac
3634 @node Alternative Programs
3635 @section Alternative Programs
3636 @cindex Programs, checking
3638 These macros check for the presence or behavior of particular programs.
3639 They are used to choose between several alternative programs and to
3640 decide what to do once one has been chosen.  If there is no macro
3641 specifically defined to check for a program you need, and you don't need
3642 to check for any special properties of it, then you can use one of the
3643 general program-check macros.
3645 @menu
3646 * Particular Programs::         Special handling to find certain programs
3647 * Generic Programs::            How to find other programs
3648 @end menu
3650 @node Particular Programs
3651 @subsection Particular Program Checks
3653 These macros check for particular programs---whether they exist, and
3654 in some cases whether they support certain features.
3656 @defmac AC_PROG_AWK
3657 @acindex{PROG_AWK}
3658 @ovindex AWK
3659 Check for @code{gawk}, @code{mawk}, @code{nawk}, and @code{awk}, in that
3660 order, and set output variable @code{AWK} to the first one that is found.
3661 It tries @code{gawk} first because that is reported to be the
3662 best implementation.
3663 @end defmac
3665 @defmac AC_PROG_GREP
3666 @acindex{PROG_GREP}
3667 @ovindex GREP
3668 Look for the best available @code{grep} or @code{ggrep} that accepts the
3669 longest input lines possible, and that supports multiple @option{-e} options.
3670 Set the output variable @code{GREP} to whatever is chosen.
3671 @xref{Limitations of Usual Tools}, for more information about
3672 portability problems with the @command{grep} command family.
3673 @end defmac
3675 @defmac AC_PROG_EGREP
3676 @acindex{PROG_EGREP}
3677 @ovindex EGREP
3678 Check whether @code{$GREP -E} works, or else look for the best available
3679 @code{egrep} or @code{gegrep} that accepts the longest input lines possible.
3680 Set the output variable @code{EGREP} to whatever is chosen.
3681 @end defmac
3683 @defmac AC_PROG_FGREP
3684 @acindex{PROG_FGREP}
3685 @ovindex FGREP
3686 Check whether @code{$GREP -F} works, or else look for the best available
3687 @code{fgrep} or @code{gfgrep} that accepts the longest input lines possible.
3688 Set the output variable @code{FGREP} to whatever is chosen.
3689 @end defmac
3691 @defmac AC_PROG_INSTALL
3692 @acindex{PROG_INSTALL}
3693 @ovindex INSTALL
3694 @ovindex INSTALL_PROGRAM
3695 @ovindex INSTALL_DATA
3696 @ovindex INSTALL_SCRIPT
3697 Set output variable @code{INSTALL} to the name of a @acronym{BSD}-compatible
3698 @command{install} program, if one is found in the current @env{PATH}.
3699 Otherwise, set @code{INSTALL} to @samp{@var{dir}/install-sh -c},
3700 checking the directories specified to @code{AC_CONFIG_AUX_DIR} (or its
3701 default directories) to determine @var{dir} (@pxref{Output}).  Also set
3702 the variables @code{INSTALL_PROGRAM} and @code{INSTALL_SCRIPT} to
3703 @samp{$@{INSTALL@}} and @code{INSTALL_DATA} to @samp{$@{INSTALL@} -m 644}.
3705 @samp{@@INSTALL@@} is special, as its value may vary for different
3706 configuration files.
3708 This macro screens out various instances of @command{install} known not to
3709 work.  It prefers to find a C program rather than a shell script, for
3710 speed.  Instead of @file{install-sh}, it can also use @file{install.sh},
3711 but that name is obsolete because some @command{make} programs have a rule
3712 that creates @file{install} from it if there is no makefile.
3714 Autoconf comes with a copy of @file{install-sh} that you can use.  If
3715 you use @code{AC_PROG_INSTALL}, you must include either
3716 @file{install-sh} or @file{install.sh} in your distribution; otherwise
3717 @command{configure} produces an error message saying it can't find
3718 them---even if the system you're on has a good @command{install} program.
3719 This check is a safety measure to prevent you from accidentally leaving
3720 that file out, which would prevent your package from installing on
3721 systems that don't have a @acronym{BSD}-compatible @command{install} program.
3723 If you need to use your own installation program because it has features
3724 not found in standard @command{install} programs, there is no reason to use
3725 @code{AC_PROG_INSTALL}; just put the file name of your program into your
3726 @file{Makefile.in} files.
3727 @end defmac
3729 @defmac AC_PROG_MKDIR_P
3730 @acindex{PROG_MKDIR_P}
3731 @ovindex MKDIR_P
3732 Set output variable @code{MKDIR_P} to a program that ensures that for
3733 each argument, a directory named by this argument exists, creating it
3734 and its parent directories if needed, and without race conditions when
3735 two instances of the program attempt to make the same directory at
3736 nearly the same time.
3738 This macro uses the @samp{mkdir -p} command if possible.  Otherwise, it
3739 falls back on invoking @command{install-sh} with the @option{-d} option,
3740 so your package should
3741 contain @file{install-sh} as described under @code{AC_PROG_INSTALL}.
3742 An @file{install-sh} file that predates Autoconf 2.60 or Automake 1.10
3743 is vulnerable to race conditions, so if you want to support parallel
3744 installs from
3745 different packages into the same directory you need to make sure you
3746 have an up-to-date @file{install-sh}.  In particular, be careful about
3747 using @samp{autoreconf -if} if your Automake predates Automake 1.10.
3749 This macro is related to the @code{AS_MKDIR_P} macro (@pxref{Programming
3750 in M4sh}), but it sets an output variable intended for use in other
3751 files, whereas @code{AS_MKDIR_P} is intended for use in scripts like
3752 @command{configure}.  Also, @code{AS_MKDIR_P} does not accept options,
3753 but @code{MKDIR_P} supports the @option{-m} option, e.g., a makefile
3754 might invoke @code{$(MKDIR_P) -m 0 dir} to create an inaccessible
3755 directory, and conversely a makefile should use @code{$(MKDIR_P) --
3756 $(FOO)} if @var{FOO} might yield a value that begins with @samp{-}.
3757 Finally, @code{AS_MKDIR_P} does not check for race condition
3758 vulnerability, whereas @code{AC_PROG_MKDIR_P} does.
3760 @samp{@@MKDIR_P@@} is special, as its value may vary for different
3761 configuration files.
3762 @end defmac
3764 @anchor{AC_PROG_LEX}
3765 @defmac AC_PROG_LEX
3766 @acindex{PROG_LEX}
3767 @ovindex LEX
3768 @ovindex LEXLIB
3769 @cvindex YYTEXT_POINTER
3770 @ovindex LEX_OUTPUT_ROOT
3771 If @code{flex} is found, set output variable @code{LEX} to @samp{flex}
3772 and @code{LEXLIB} to @option{-lfl}, if that library is in a standard
3773 place.  Otherwise set @code{LEX} to @samp{lex} and @code{LEXLIB} to
3774 @option{-ll}.
3776 Define @code{YYTEXT_POINTER} if @code{yytext} defaults to @samp{char *} instead
3777 of to @samp{char []}.  Also set output variable @code{LEX_OUTPUT_ROOT} to
3778 the base of the file name that the lexer generates; usually
3779 @file{lex.yy}, but sometimes something else.  These results vary
3780 according to whether @code{lex} or @code{flex} is being used.
3782 You are encouraged to use Flex in your sources, since it is both more
3783 pleasant to use than plain Lex and the C source it produces is portable.
3784 In order to ensure portability, however, you must either provide a
3785 function @code{yywrap} or, if you don't use it (e.g., your scanner has
3786 no @samp{#include}-like feature), simply include a @samp{%noyywrap}
3787 statement in the scanner's source.  Once this done, the scanner is
3788 portable (unless @emph{you} felt free to use nonportable constructs) and
3789 does not depend on any library.  In this case, and in this case only, it
3790 is suggested that you use this Autoconf snippet:
3792 @example
3793 AC_PROG_LEX
3794 if test "$LEX" != flex; then
3795   LEX="$SHELL $missing_dir/missing flex"
3796   AC_SUBST([LEX_OUTPUT_ROOT], [lex.yy])
3797   AC_SUBST([LEXLIB], [''])
3799 @end example
3801 The shell script @command{missing} can be found in the Automake
3802 distribution.
3804 To ensure backward compatibility, Automake's @code{AM_PROG_LEX} invokes
3805 (indirectly) this macro twice, which causes an annoying but benign
3806 ``@code{AC_PROG_LEX} invoked multiple times'' warning.  Future versions
3807 of Automake will fix this issue; meanwhile, just ignore this message.
3809 As part of running the test, this macro may delete any file in the
3810 configuration directory named @file{lex.yy.c} or @file{lexyy.c}.
3811 @end defmac
3813 @anchor{AC_PROG_LN_S}
3814 @defmac AC_PROG_LN_S
3815 @acindex{PROG_LN_S}
3816 @ovindex LN_S
3817 If @samp{ln -s} works on the current file system (the operating system
3818 and file system support symbolic links), set the output variable
3819 @code{LN_S} to @samp{ln -s}; otherwise, if @samp{ln} works, set
3820 @code{LN_S} to @samp{ln}, and otherwise set it to @samp{cp -p}.
3822 If you make a link in a directory other than the current directory, its
3823 meaning depends on whether @samp{ln} or @samp{ln -s} is used.  To safely
3824 create links using @samp{$(LN_S)}, either find out which form is used
3825 and adjust the arguments, or always invoke @code{ln} in the directory
3826 where the link is to be created.
3828 In other words, it does not work to do:
3829 @example
3830 $(LN_S) foo /x/bar
3831 @end example
3833 Instead, do:
3835 @example
3836 (cd /x && $(LN_S) foo bar)
3837 @end example
3838 @end defmac
3840 @defmac AC_PROG_RANLIB
3841 @acindex{PROG_RANLIB}
3842 @ovindex RANLIB
3843 Set output variable @code{RANLIB} to @samp{ranlib} if @code{ranlib}
3844 is found, and otherwise to @samp{:} (do nothing).
3845 @end defmac
3847 @defmac AC_PROG_SED
3848 @acindex{PROG_SED}
3849 @ovindex SED
3850 Set output variable @code{SED} to a Sed implementation that conforms to
3851 Posix and does not have arbitrary length limits.  Report an error if no
3852 acceptable Sed is found.  @xref{Limitations of Usual Tools}, for more
3853 information about portability problems with Sed.
3854 @end defmac
3856 @defmac AC_PROG_YACC
3857 @acindex{PROG_YACC}
3858 @ovindex YACC
3859 If @code{bison} is found, set output variable @code{YACC} to @samp{bison
3860 -y}.  Otherwise, if @code{byacc} is found, set @code{YACC} to
3861 @samp{byacc}.  Otherwise set @code{YACC} to @samp{yacc}.
3862 @end defmac
3864 @node Generic Programs
3865 @subsection Generic Program and File Checks
3867 These macros are used to find programs not covered by the ``particular''
3868 test macros.  If you need to check the behavior of a program as well as
3869 find out whether it is present, you have to write your own test for it
3870 (@pxref{Writing Tests}).  By default, these macros use the environment
3871 variable @env{PATH}.  If you need to check for a program that might not
3872 be in the user's @env{PATH}, you can pass a modified path to use
3873 instead, like this:
3875 @example
3876 AC_PATH_PROG([INETD], [inetd], [/usr/libexec/inetd],
3877              [$PATH$PATH_SEPARATOR/usr/libexec$PATH_SEPARATOR]dnl
3878 [/usr/sbin$PATH_SEPARATOR/usr/etc$PATH_SEPARATOR/etc])
3879 @end example
3881 You are strongly encouraged to declare the @var{variable} passed to
3882 @code{AC_CHECK_PROG} etc.@: as precious, @xref{Setting Output Variables},
3883 @code{AC_ARG_VAR}, for more details.
3885 @anchor{AC_CHECK_PROG}
3886 @defmac AC_CHECK_PROG (@var{variable}, @var{prog-to-check-for}, @
3887   @var{value-if-found}, @ovar{value-if-not-found}, @dvar{path, $PATH}, @
3888   @ovar{reject})
3889 @acindex{CHECK_PROG}
3890 Check whether program @var{prog-to-check-for} exists in @var{path}.  If
3891 it is found, set @var{variable} to @var{value-if-found}, otherwise to
3892 @var{value-if-not-found}, if given.  Always pass over @var{reject} (an
3893 absolute file name) even if it is the first found in the search path; in
3894 that case, set @var{variable} using the absolute file name of the
3895 @var{prog-to-check-for} found that is not @var{reject}.  If
3896 @var{variable} was already set, do nothing.  Calls @code{AC_SUBST} for
3897 @var{variable}.
3898 @end defmac
3900 @anchor{AC_CHECK_PROGS}
3901 @defmac AC_CHECK_PROGS (@var{variable}, @var{progs-to-check-for}, @
3902   @ovar{value-if-not-found}, @dvar{path, $PATH})
3903 @acindex{CHECK_PROGS}
3904 Check for each program in the blank-separated list
3905 @var{progs-to-check-for} existing in the @var{path}.  If one is found, set
3906 @var{variable} to the name of that program.  Otherwise, continue
3907 checking the next program in the list.  If none of the programs in the
3908 list are found, set @var{variable} to @var{value-if-not-found}; if
3909 @var{value-if-not-found} is not specified, the value of @var{variable}
3910 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3911 @end defmac
3913 @defmac AC_CHECK_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
3914   @ovar{value-if-not-found}, @dvar{path, $PATH})
3915 @acindex{CHECK_TARGET_TOOL}
3916 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3917 with a prefix of the target type as determined by
3918 @code{AC_CANONICAL_TARGET}, followed by a dash (@pxref{Canonicalizing}).
3919 If the tool cannot be found with a prefix, and if the build and target
3920 types are equal, then it is also searched for without a prefix.
3922 As noted in @ref{Specifying Names, , Specifying the system type}, the
3923 target is rarely specified, because most of the time it is the same
3924 as the host: it is the type of system for which any compiler tool in
3925 the package produces code.  What this macro looks for is,
3926 for example, @emph{a tool @r{(assembler, linker, etc.)}@: that the
3927 compiler driver @r{(@command{gcc} for the @acronym{GNU} C Compiler)}
3928 uses to produce objects, archives or executables}.
3929 @end defmac
3931 @defmac AC_CHECK_TOOL (@var{variable}, @var{prog-to-check-for}, @
3932   @ovar{value-if-not-found}, @dvar{path, $PATH})
3933 @acindex{CHECK_TOOL}
3934 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3935 with a prefix of the host type as determined by
3936 @code{AC_CANONICAL_HOST}, followed by a dash (@pxref{Canonicalizing}).
3937 For example, if the user runs @samp{configure --host=i386-gnu}, then
3938 this call:
3939 @example
3940 AC_CHECK_TOOL([RANLIB], [ranlib], [:])
3941 @end example
3942 @noindent
3943 sets @code{RANLIB} to @file{i386-gnu-ranlib} if that program exists in
3944 @var{path}, or otherwise to @samp{ranlib} if that program exists in
3945 @var{path}, or to @samp{:} if neither program exists.
3947 In the future, when cross-compiling this macro will @emph{only}
3948 accept program names that are prefixed with the host type.
3949 For more information, see @ref{Specifying Names, , Specifying the
3950 system type}.
3951 @end defmac
3953 @defmac AC_CHECK_TARGET_TOOLS (@var{variable}, @var{progs-to-check-for}, @
3954   @ovar{value-if-not-found}, @dvar{path, $PATH})
3955 @acindex{CHECK_TARGET_TOOLS}
3956 Like @code{AC_CHECK_TARGET_TOOL}, each of the tools in the list
3957 @var{progs-to-check-for} are checked with a prefix of the target type as
3958 determined by @code{AC_CANONICAL_TARGET}, followed by a dash
3959 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3960 prefix, and if the build and target types are equal, then the first one
3961 without a prefix is used.  If a tool is found, set @var{variable} to
3962 the name of that program.  If none of the tools in the list are found,
3963 set @var{variable} to @var{value-if-not-found}; if @var{value-if-not-found}
3964 is not specified, the value of @var{variable} is not changed.  Calls
3965 @code{AC_SUBST} for @var{variable}.
3966 @end defmac
3968 @defmac AC_CHECK_TOOLS (@var{variable}, @var{progs-to-check-for}, @
3969   @ovar{value-if-not-found}, @dvar{path, $PATH})
3970 @acindex{CHECK_TOOLS}
3971 Like @code{AC_CHECK_TOOL}, each of the tools in the list
3972 @var{progs-to-check-for} are checked with a prefix of the host type as
3973 determined by @code{AC_CANONICAL_HOST}, followed by a dash
3974 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3975 prefix, then the first one without a prefix is used.  If a tool is found,
3976 set @var{variable} to the name of that program.  If none of the tools in
3977 the list are found, set @var{variable} to @var{value-if-not-found}; if
3978 @var{value-if-not-found} is not specified, the value of @var{variable}
3979 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3981 In the future, when cross-compiling this macro will @emph{not}
3982 accept program names that are not prefixed with the host type.
3983 @end defmac
3985 @anchor{AC_PATH_PROG}
3986 @defmac AC_PATH_PROG (@var{variable}, @var{prog-to-check-for}, @
3987   @ovar{value-if-not-found}, @dvar{path, $PATH})
3988 @acindex{PATH_PROG}
3989 Like @code{AC_CHECK_PROG}, but set @var{variable} to the absolute
3990 name of @var{prog-to-check-for} if found.
3991 @end defmac
3993 @anchor{AC_PATH_PROGS}
3994 @defmac AC_PATH_PROGS (@var{variable}, @var{progs-to-check-for}, @
3995   @ovar{value-if-not-found}, @dvar{path, $PATH})
3996 @acindex{PATH_PROGS}
3997 Like @code{AC_CHECK_PROGS}, but if any of @var{progs-to-check-for}
3998 are found, set @var{variable} to the absolute name of the program
3999 found.
4000 @end defmac
4002 @defmac AC_PATH_PROGS_FEATURE_CHECK (@var{variable}, @
4003   @var{progs-to-check-for}, @var{feature-test}, @
4004   @ovar{action-if-not-found}, @dvar{path, $PATH})
4005 @acindex{PATH_PROGS_FEATURE_CHECK}
4006 This macro was introduced in Autoconf 2.62.  If @var{variable} is not
4007 empty, then set the cache variable @code{$ac_cv_path_@var{variable}} to
4008 its value.  Otherwise, check for each program in the blank-separated
4009 list @var{progs-to-check-for} existing in @var{path}.  For each program
4010 found, execute @var{feature-test} with @code{$ac_path_@var{variable}}
4011 set to the absolute name of the candidate program.  If no invocation of
4012 @var{feature-test} sets the shell variable
4013 @code{$ac_cv_path_@var{variable}}, then @var{action-if-not-found} is
4014 executed.  @var{feature-test} will be run even when
4015 @code{ac_cv_path_@var{variable}} is set, to provide the ability to
4016 choose a better candidate found later in @var{path}; to accept the
4017 current setting and bypass all futher checks, @var{feature-test} can
4018 execute @code{ac_path_@var{variable}_found=:}.
4020 Note that this macro has some subtle differences from
4021 @code{AC_CHECK_PROGS}.  It is designed to be run inside
4022 @code{AC_CACHE_VAL}, therefore, it should have no side effects.  In
4023 particular, @var{variable} is not set to the final value of
4024 @code{ac_cv_path_@var{variable}}, nor is @code{AC_SUBST} automatically
4025 run.  Also, on failure, any action can be performed, whereas
4026 @code{AC_CHECK_PROGS} only performs
4027 @code{@var{variable}=@var{value-if-not-found}}.
4029 Here is an example, similar to what Autoconf uses in its own configure
4030 script.  It will search for an implementation of @command{m4} that
4031 supports the @code{indir} builtin, even if it goes by the name
4032 @command{gm4} or is not the first implementation on @env{PATH}.
4034 @example
4035 AC_CACHE_CHECK([for m4 that supports indir], [ac_cv_path_M4],
4036   [AC_PATH_PROGS_FEATURE_CHECK([M4], [m4 gm4],
4037     [[m4out=`echo 'changequote([,])indir([divnum])' | $ac_path_M4`
4038       test "x$m4out" = x0 \
4039       && ac_cv_path_M4=$ac_path_M4 ac_path_M4_found=:]],
4040     [AC_MSG_ERROR([could not find m4 that supports indir])])])
4041 AC_SUBST([M4], [$ac_cv_path_M4])
4042 @end example
4043 @end defmac
4045 @defmac AC_PATH_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
4046   @ovar{value-if-not-found}, @dvar{path, $PATH})
4047 @acindex{PATH_TARGET_TOOL}
4048 Like @code{AC_CHECK_TARGET_TOOL}, but set @var{variable} to the absolute
4049 name of the program if it is found.
4050 @end defmac
4052 @defmac AC_PATH_TOOL (@var{variable}, @var{prog-to-check-for}, @
4053   @ovar{value-if-not-found}, @dvar{path, $PATH})
4054 @acindex{PATH_TOOL}
4055 Like @code{AC_CHECK_TOOL}, but set @var{variable} to the absolute
4056 name of the program if it is found.
4058 In the future, when cross-compiling this macro will @emph{not}
4059 accept program names that are not prefixed with the host type.
4060 @end defmac
4063 @node Files
4064 @section Files
4065 @cindex File, checking
4067 You might also need to check for the existence of files.  Before using
4068 these macros, ask yourself whether a runtime test might not be a better
4069 solution.  Be aware that, like most Autoconf macros, they test a feature
4070 of the host machine, and therefore, they die when cross-compiling.
4072 @defmac AC_CHECK_FILE (@var{file}, @ovar{action-if-found}, @
4073   @ovar{action-if-not-found})
4074 @acindex{CHECK_FILE}
4075 Check whether file @var{file} exists on the native system.  If it is
4076 found, execute @var{action-if-found}, otherwise do
4077 @var{action-if-not-found}, if given.
4078 @end defmac
4080 @defmac AC_CHECK_FILES (@var{files}, @ovar{action-if-found}, @
4081   @ovar{action-if-not-found})
4082 @acindex{CHECK_FILES}
4083 Executes @code{AC_CHECK_FILE} once for each file listed in @var{files}.
4084 Additionally, defines @samp{HAVE_@var{file}} (@pxref{Standard Symbols})
4085 for each file found.
4086 @end defmac
4089 @node Libraries
4090 @section Library Files
4091 @cindex Library, checking
4093 The following macros check for the presence of certain C, C++, or Fortran
4094 library archive files.
4096 @anchor{AC_CHECK_LIB}
4097 @defmac AC_CHECK_LIB (@var{library}, @var{function}, @
4098   @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4099 @acindex{CHECK_LIB}
4100 Test whether the library @var{library} is available by trying to link
4101 a test program that calls function @var{function} with the library.
4102 @var{function} should be a function provided by the library.
4103 Use the base
4104 name of the library; e.g., to check for @option{-lmp}, use @samp{mp} as
4105 the @var{library} argument.
4107 @var{action-if-found} is a list of shell commands to run if the link
4108 with the library succeeds; @var{action-if-not-found} is a list of shell
4109 commands to run if the link fails.  If @var{action-if-found} is not
4110 specified, the default action prepends @option{-l@var{library}} to
4111 @code{LIBS} and defines @samp{HAVE_LIB@var{library}} (in all
4112 capitals).  This macro is intended to support building @code{LIBS} in
4113 a right-to-left (least-dependent to most-dependent) fashion such that
4114 library dependencies are satisfied as a natural side effect of
4115 consecutive tests.  Linkers are sensitive to library ordering
4116 so the order in which @code{LIBS} is generated is important to reliable
4117 detection of libraries.
4119 If linking with @var{library} results in unresolved symbols that would
4120 be resolved by linking with additional libraries, give those libraries
4121 as the @var{other-libraries} argument, separated by spaces:
4122 e.g., @option{-lXt -lX11}.  Otherwise, this macro fails to detect
4123 that @var{library} is present, because linking the test program
4124 always fails with unresolved symbols.  The @var{other-libraries} argument
4125 should be limited to cases where it is desirable to test for one library
4126 in the presence of another that is not already in @code{LIBS}.
4128 @code{AC_CHECK_LIB} requires some care in usage, and should be avoided
4129 in some common cases.  Many standard functions like @code{gethostbyname}
4130 appear in the standard C library on some hosts, and in special libraries
4131 like @code{nsl} on other hosts.  On some hosts the special libraries
4132 contain variant implementations that you may not want to use.  These
4133 days it is normally better to use @code{AC_SEARCH_LIBS([gethostbyname],
4134 [nsl])} instead of @code{AC_CHECK_LIB([nsl], [gethostbyname])}.
4135 @end defmac
4137 @anchor{AC_SEARCH_LIBS}
4138 @defmac AC_SEARCH_LIBS (@var{function}, @var{search-libs}, @
4139   @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4140 @acindex{SEARCH_LIBS}
4141 Search for a library defining @var{function} if it's not already
4142 available.  This equates to calling
4143 @samp{AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])])} first with
4144 no libraries, then for each library listed in @var{search-libs}.
4146 Add @option{-l@var{library}} to @code{LIBS} for the first library found
4147 to contain @var{function}, and run @var{action-if-found}.  If the
4148 function is not found, run @var{action-if-not-found}.
4150 If linking with @var{library} results in unresolved symbols that would
4151 be resolved by linking with additional libraries, give those libraries
4152 as the @var{other-libraries} argument, separated by spaces:
4153 e.g., @option{-lXt -lX11}.  Otherwise, this macro fails to detect
4154 that @var{function} is present, because linking the test program
4155 always fails with unresolved symbols.
4156 @end defmac
4160 @node Library Functions
4161 @section Library Functions
4163 The following macros check for particular C library functions.
4164 If there is no macro specifically defined to check for a function you need,
4165 and you don't need to check for any special properties of
4166 it, then you can use one of the general function-check macros.
4168 @menu
4169 * Function Portability::        Pitfalls with usual functions
4170 * Particular Functions::        Special handling to find certain functions
4171 * Generic Functions::           How to find other functions
4172 @end menu
4174 @node Function Portability
4175 @subsection Portability of C Functions
4176 @cindex Portability of C functions
4177 @cindex C function portability
4179 Most usual functions can either be missing, or be buggy, or be limited
4180 on some architectures.  This section tries to make an inventory of these
4181 portability issues.  By definition, this list always requires
4182 additions.  Please help us keeping it as complete as possible.
4184 @table @asis
4185 @item @code{exit}
4186 @c @fuindex exit
4187 @prindex @code{exit}
4188 On ancient hosts, @code{exit} returned @code{int}.
4189 This is because @code{exit} predates @code{void}, and there was a long
4190 tradition of it returning @code{int}.
4192 On current hosts, the problem more likely is that @code{exit} is not
4193 declared, due to C++ problems of some sort or another.  For this reason
4194 we suggest that test programs not invoke @code{exit}, but return from
4195 @code{main} instead.
4197 @item @code{free}
4198 @c @fuindex free
4199 @prindex @code{free}
4200 The C standard says a call @code{free (NULL)} does nothing, but
4201 some old systems don't support this (e.g., NextStep).
4203 @item @code{isinf}
4204 @itemx @code{isnan}
4205 @c @fuindex isinf
4206 @c @fuindex isnan
4207 @prindex @code{isinf}
4208 @prindex @code{isnan}
4209 The C99 standard says that @code{isinf} and @code{isnan} are
4210 macros.  On some systems just macros are available
4211 (e.g., @acronym{HP-UX} and Solaris 10), on
4212 some systems both macros and functions (e.g., glibc 2.3.2), and on some
4213 systems only functions (e.g., IRIX 6 and Solaris 9).  In some cases
4214 these functions are declared in nonstandard headers like
4215 @code{<sunmath.h>} and defined in non-default libraries like
4216 @option{-lm} or @option{-lsunmath}.
4218 The C99 @code{isinf} and @code{isnan} macros work correctly with
4219 @code{long double} arguments, but pre-C99 systems that use functions
4220 typically assume @code{double} arguments.  On such a system,
4221 @code{isinf} incorrectly returns true for a finite @code{long double}
4222 argument that is outside the range of @code{double}.
4224 To work around this porting mess, you can use code like the following.
4226 @smallexample
4227 #include <math.h>
4229 #ifndef isnan
4230 # define isnan(x) \
4231     (sizeof (x) == sizeof (long double) ? isnan_ld (x) \
4232      : sizeof (x) == sizeof (double) ? isnan_d (x) \
4233      : isnan_f (x))
4234 static inline int isnan_f  (float       x) @{ return x != x; @}
4235 static inline int isnan_d  (double      x) @{ return x != x; @}
4236 static inline int isnan_ld (long double x) @{ return x != x; @}
4237 #endif
4239 #ifndef isinf
4240 # define isinf(x) \
4241     (sizeof (x) == sizeof (long double) ? isinf_ld (x) \
4242      : sizeof (x) == sizeof (double) ? isinf_d (x) \
4243      : isinf_f (x))
4244 static inline int isinf_f  (float       x) @{ return isnan (x - x); @}
4245 static inline int isinf_d  (double      x) @{ return isnan (x - x); @}
4246 static inline int isinf_ld (long double x) @{ return isnan (x - x); @}
4247 #endif
4248 @end smallexample
4250 Use @code{AC_C_INLINE} (@pxref{C Compiler}) so that this code works on
4251 compilers that lack the @code{inline} keyword.  Some optimizing
4252 compilers mishandle these definitions, but systems with that bug
4253 typically have missing or broken @code{isnan} functions anyway, so it's
4254 probably not worth worrying about.
4256 @item @code{malloc}
4257 @c @fuindex malloc
4258 @prindex @code{malloc}
4259 The C standard says a call @code{malloc (0)} is implementation
4260 dependent.  It can return either @code{NULL} or a new non-null pointer.
4261 The latter is more common (e.g., the @acronym{GNU} C Library) but is by
4262 no means universal.  @code{AC_FUNC_MALLOC}
4263 can be used to insist on non-@code{NULL} (@pxref{Particular Functions}).
4265 @item @code{putenv}
4266 @c @fuindex putenv
4267 @prindex @code{putenv}
4268 Posix prefers @code{setenv} to @code{putenv}; among other things,
4269 @code{putenv} is not required of all Posix implementations, but
4270 @code{setenv} is.
4272 Posix specifies that @code{putenv} puts the given string directly in
4273 @code{environ}, but some systems make a copy of it instead (e.g.,
4274 glibc 2.0, or @acronym{BSD}).  And when a copy is made, @code{unsetenv} might
4275 not free it, causing a memory leak (e.g., Free@acronym{BSD} 4).
4277 On some systems @code{putenv ("FOO")} removes @samp{FOO} from the
4278 environment, but this is not standard usage and it dumps core
4279 on some systems (e.g., AIX).
4281 On MinGW, a call @code{putenv ("FOO=")} removes @samp{FOO} from the
4282 environment, rather than inserting it with an empty value.
4284 @item @code{realloc}
4285 @c @fuindex realloc
4286 @prindex @code{realloc}
4287 The C standard says a call @code{realloc (NULL, size)} is equivalent
4288 to @code{malloc (size)}, but some old systems don't support this (e.g.,
4289 NextStep).
4291 @item @code{signal} handler
4292 @c @fuindex signal
4293 @prindex @code{signal}
4294 Normally @code{signal} takes a handler function with a return type of
4295 @code{void}, but some old systems required @code{int} instead.  Any
4296 actual @code{int} value returned is not used; this is only a
4297 difference in the function prototype demanded.
4299 All systems we know of in current use return @code{void}.  The
4300 @code{int} was to support K&R C, where of course @code{void} is not
4301 available.  @code{AC_TYPE_SIGNAL} (@pxref{Particular Types}) can be
4302 used to establish the correct type in all cases.
4304 @item @code{snprintf}
4305 @c @fuindex snprintf
4306 @prindex @code{snprintf}
4307 @c @fuindex vsnprintf
4308 @prindex @code{vsnprintf}
4309 The C99 standard says that if the output array isn't big enough
4310 and if no other errors occur, @code{snprintf} and @code{vsnprintf}
4311 truncate the output and return the number of bytes that ought to have
4312 been produced.  Some older systems return the truncated length (e.g.,
4313 @acronym{GNU} C Library 2.0.x or @sc{irix} 6.5), some a negative value
4314 (e.g., earlier @acronym{GNU} C Library versions), and some the buffer
4315 length without truncation (e.g., 32-bit Solaris 7).  Also, some buggy
4316 older systems ignore the length and overrun the buffer (e.g., 64-bit
4317 Solaris 7).
4319 @item @code{sprintf}
4320 @c @fuindex sprintf
4321 @prindex @code{sprintf}
4322 @c @fuindex vsprintf
4323 @prindex @code{vsprintf}
4324 The C standard says @code{sprintf} and @code{vsprintf} return the
4325 number of bytes written.  On some ancient systems (SunOS 4 for
4326 instance) they return the buffer pointer instead, but these no
4327 longer need to be worried about.
4329 @item @code{sscanf}
4330 @c @fuindex sscanf
4331 @prindex @code{sscanf}
4332 On various old systems, e.g., @acronym{HP-UX} 9, @code{sscanf} requires
4333 that its
4334 input string be writable (though it doesn't actually change it).  This
4335 can be a problem when using @command{gcc} since it normally puts
4336 constant strings in read-only memory (@pxref{Incompatibilities,
4337 Incompatibilities of @acronym{GCC}, , gcc, Using and
4338 Porting the @acronym{GNU} Compiler Collection}).  Apparently in some cases even
4339 having format strings read-only can be a problem.
4341 @item @code{strerror_r}
4342 @c @fuindex strerror_r
4343 @prindex @code{strerror_r}
4344 Posix specifies that @code{strerror_r} returns an @code{int}, but many
4345 systems (e.g., @acronym{GNU} C Library version 2.2.4) provide a
4346 different version returning a @code{char *}.  @code{AC_FUNC_STRERROR_R}
4347 can detect which is in use (@pxref{Particular Functions}).
4349 @item @code{strnlen}
4350 @c @fuindex strnlen
4351 @prindex @code{strnlen}
4352 @acronym{AIX} 4.3 provides a broken version which produces the
4353 following results:
4355 @example
4356 strnlen ("foobar", 0) = 0
4357 strnlen ("foobar", 1) = 3
4358 strnlen ("foobar", 2) = 2
4359 strnlen ("foobar", 3) = 1
4360 strnlen ("foobar", 4) = 0
4361 strnlen ("foobar", 5) = 6
4362 strnlen ("foobar", 6) = 6
4363 strnlen ("foobar", 7) = 6
4364 strnlen ("foobar", 8) = 6
4365 strnlen ("foobar", 9) = 6
4366 @end example
4368 @item @code{sysconf}
4369 @c @fuindex sysconf
4370 @prindex @code{sysconf}
4371 @code{_SC_PAGESIZE} is standard, but some older systems (e.g., @acronym{HP-UX}
4372 9) have @code{_SC_PAGE_SIZE} instead.  This can be tested with
4373 @code{#ifdef}.
4375 @item @code{unlink}
4376 @c @fuindex unlink
4377 @prindex @code{unlink}
4378 The Posix spec says that @code{unlink} causes the given file to be
4379 removed only after there are no more open file handles for it.  Some
4380 non-Posix hosts have trouble with this requirement, though,
4381 and some @acronym{DOS} variants even corrupt the file system.
4383 @item @code{unsetenv}
4384 @c @fuindex unsetenv
4385 @prindex @code{unsetenv}
4386 On MinGW, @code{unsetenv} is not available, but a variable @samp{FOO}
4387 can be removed with a call @code{putenv ("FOO=")}, as described under
4388 @code{putenv} above.
4390 @item @code{va_copy}
4391 @c @fuindex va_copy
4392 @prindex @code{va_copy}
4393 The C99 standard provides @code{va_copy} for copying
4394 @code{va_list} variables.  It may be available in older environments
4395 too, though possibly as @code{__va_copy} (e.g., @command{gcc} in strict
4396 pre-C99 mode).  These can be tested with @code{#ifdef}.  A fallback to
4397 @code{memcpy (&dst, &src, sizeof (va_list))} gives maximum
4398 portability.
4400 @item @code{va_list}
4401 @c @fuindex va_list
4402 @prindex @code{va_list}
4403 @code{va_list} is not necessarily just a pointer.  It can be a
4404 @code{struct} (e.g., @command{gcc} on Alpha), which means @code{NULL} is
4405 not portable.  Or it can be an array (e.g., @command{gcc} in some
4406 PowerPC configurations), which means as a function parameter it can be
4407 effectively call-by-reference and library routines might modify the
4408 value back in the caller (e.g., @code{vsnprintf} in the @acronym{GNU} C Library
4409 2.1).
4411 @item Signed @code{>>}
4412 Normally the C @code{>>} right shift of a signed type replicates the
4413 high bit, giving a so-called ``arithmetic'' shift.  But care should be
4414 taken since Standard C doesn't require that behavior.  On those
4415 few processors without a native arithmetic shift (for instance Cray
4416 vector systems) zero bits may be shifted in, the same as a shift of an
4417 unsigned type.
4419 @item Integer @code{/}
4420 C divides signed integers by truncating their quotient toward zero,
4421 yielding the same result as Fortran.  However, before C99 the standard
4422 allowed C implementations to take the floor or ceiling of the quotient
4423 in some cases.  Hardly any implementations took advantage of this
4424 freedom, though, and it's probably not worth worrying about this issue
4425 nowadays.
4426 @end table
4429 @node Particular Functions
4430 @subsection Particular Function Checks
4431 @cindex Function, checking
4433 These macros check for particular C functions---whether they exist, and
4434 in some cases how they respond when given certain arguments.
4436 @anchor{AC_FUNC_ALLOCA}
4437 @defmac AC_FUNC_ALLOCA
4438 @acindex{FUNC_ALLOCA}
4439 @cvindex C_ALLOCA
4440 @cvindex HAVE_ALLOCA_H
4441 @ovindex ALLOCA
4442 @c @fuindex alloca
4443 @prindex @code{alloca}
4444 @hdrindex{alloca.h}
4445 Check how to get @code{alloca}.  Tries to get a builtin version by
4446 checking for @file{alloca.h} or the predefined C preprocessor macros
4447 @code{__GNUC__} and @code{_AIX}.  If this macro finds @file{alloca.h},
4448 it defines @code{HAVE_ALLOCA_H}.
4450 If those attempts fail, it looks for the function in the standard C
4451 library.  If any of those methods succeed, it defines
4452 @code{HAVE_ALLOCA}.  Otherwise, it sets the output variable
4453 @code{ALLOCA} to @samp{$@{LIBOBJDIR@}alloca.o} and defines
4454 @code{C_ALLOCA} (so programs can periodically call @samp{alloca (0)} to
4455 garbage collect).  This variable is separate from @code{LIBOBJS} so
4456 multiple programs can share the value of @code{ALLOCA} without needing
4457 to create an actual library, in case only some of them use the code in
4458 @code{LIBOBJS}.  The @samp{$@{LIBOBJDIR@}} prefix serves the same
4459 purpose as in @code{LIBOBJS} (@pxref{AC_LIBOBJ vs LIBOBJS}).
4461 This macro does not try to get @code{alloca} from the System V R3
4462 @file{libPW} or the System V R4 @file{libucb} because those libraries
4463 contain some incompatible functions that cause trouble.  Some versions
4464 do not even contain @code{alloca} or contain a buggy version.  If you
4465 still want to use their @code{alloca}, use @code{ar} to extract
4466 @file{alloca.o} from them instead of compiling @file{alloca.c}.
4468 Source files that use @code{alloca} should start with a piece of code
4469 like the following, to declare it properly.
4471 @example
4472 @group
4473 #ifdef HAVE_ALLOCA_H
4474 # include <alloca.h>
4475 #elif defined __GNUC__
4476 # define alloca __builtin_alloca
4477 #elif defined _AIX
4478 # define alloca __alloca
4479 #elif defined _MSC_VER
4480 # include <malloc.h>
4481 # define alloca _alloca
4482 #else
4483 # include <stddef.h>
4484 # ifdef  __cplusplus
4485 extern "C"
4486 # endif
4487 void *alloca (size_t);
4488 #endif
4489 @end group
4490 @end example
4491 @end defmac
4493 @defmac AC_FUNC_CHOWN
4494 @acindex{FUNC_CHOWN}
4495 @cvindex HAVE_CHOWN
4496 @c @fuindex chown
4497 @prindex @code{chown}
4498 If the @code{chown} function is available and works (in particular, it
4499 should accept @option{-1} for @code{uid} and @code{gid}), define
4500 @code{HAVE_CHOWN}.
4501 @end defmac
4503 @anchor{AC_FUNC_CLOSEDIR_VOID}
4504 @defmac AC_FUNC_CLOSEDIR_VOID
4505 @acindex{FUNC_CLOSEDIR_VOID}
4506 @cvindex CLOSEDIR_VOID
4507 @c @fuindex closedir
4508 @prindex @code{closedir}
4509 If the @code{closedir} function does not return a meaningful value,
4510 define @code{CLOSEDIR_VOID}.  Otherwise, callers ought to check its
4511 return value for an error indicator.
4513 Currently this test is implemented by running a test program.  When
4514 cross compiling the pessimistic assumption that @code{closedir} does not
4515 return a meaningful value is made.
4517 This macro is obsolescent, as @code{closedir} returns a meaningful value
4518 on current systems.  New programs need not use this macro.
4519 @end defmac
4521 @defmac AC_FUNC_ERROR_AT_LINE
4522 @acindex{FUNC_ERROR_AT_LINE}
4523 @c @fuindex error_at_line
4524 @prindex @code{error_at_line}
4525 If the @code{error_at_line} function is not found, require an
4526 @code{AC_LIBOBJ} replacement of @samp{error}.
4527 @end defmac
4529 @defmac AC_FUNC_FNMATCH
4530 @acindex{FUNC_FNMATCH}
4531 @c @fuindex fnmatch
4532 @prindex @code{fnmatch}
4533 If the @code{fnmatch} function conforms to Posix, define
4534 @code{HAVE_FNMATCH}.  Detect common implementation bugs, for example,
4535 the bugs in Solaris 2.4.
4537 Unlike the other specific
4538 @code{AC_FUNC} macros, @code{AC_FUNC_FNMATCH} does not replace a
4539 broken/missing @code{fnmatch}.  This is for historical reasons.
4540 See @code{AC_REPLACE_FNMATCH} below.
4542 This macro is obsolescent.  New programs should use Gnulib's
4543 @code{fnmatch-posix} module.  @xref{Gnulib}.
4544 @end defmac
4546 @defmac AC_FUNC_FNMATCH_GNU
4547 @acindex{FUNC_FNMATCH_GNU}
4548 @c @fuindex fnmatch
4549 @prindex @code{fnmatch}
4550 Behave like @code{AC_REPLACE_FNMATCH} (@emph{replace}) but also test
4551 whether @code{fnmatch} supports @acronym{GNU} extensions.  Detect common
4552 implementation bugs, for example, the bugs in the @acronym{GNU} C
4553 Library 2.1.
4555 This macro is obsolescent.  New programs should use Gnulib's
4556 @code{fnmatch-gnu} module.  @xref{Gnulib}.
4557 @end defmac
4559 @anchor{AC_FUNC_FORK}
4560 @defmac AC_FUNC_FORK
4561 @acindex{FUNC_FORK}
4562 @cvindex HAVE_VFORK_H
4563 @cvindex HAVE_WORKING_FORK
4564 @cvindex HAVE_WORKING_VFORK
4565 @cvindex vfork
4566 @c @fuindex fork
4567 @prindex @code{fork}
4568 @c @fuindex vfork
4569 @prindex @code{vfork}
4570 @hdrindex{vfork.h}
4571 This macro checks for the @code{fork} and @code{vfork} functions.  If a
4572 working @code{fork} is found, define @code{HAVE_WORKING_FORK}.  This macro
4573 checks whether @code{fork} is just a stub by trying to run it.
4575 If @file{vfork.h} is found, define @code{HAVE_VFORK_H}.  If a working
4576 @code{vfork} is found, define @code{HAVE_WORKING_VFORK}.  Otherwise,
4577 define @code{vfork} to be @code{fork} for backward compatibility with
4578 previous versions of @command{autoconf}.  This macro checks for several known
4579 errors in implementations of @code{vfork} and considers the system to not
4580 have a working @code{vfork} if it detects any of them.  It is not considered
4581 to be an implementation error if a child's invocation of @code{signal}
4582 modifies the parent's signal handler, since child processes rarely change
4583 their signal handlers.
4585 Since this macro defines @code{vfork} only for backward compatibility with
4586 previous versions of @command{autoconf} you're encouraged to define it
4587 yourself in new code:
4588 @example
4589 @group
4590 #ifndef HAVE_WORKING_VFORK
4591 # define vfork fork
4592 #endif
4593 @end group
4594 @end example
4595 @end defmac
4597 @defmac AC_FUNC_FSEEKO
4598 @acindex{FUNC_FSEEKO}
4599 @cvindex _LARGEFILE_SOURCE
4600 @cvindex HAVE_FSEEKO
4601 @c @fuindex fseeko
4602 @prindex @code{fseeko}
4603 If the @code{fseeko} function is available, define @code{HAVE_FSEEKO}.
4604 Define @code{_LARGEFILE_SOURCE} if necessary to make the prototype
4605 visible on some systems (e.g., glibc 2.2).  Otherwise linkage problems
4606 may occur when compiling with @code{AC_SYS_LARGEFILE} on
4607 largefile-sensitive systems where @code{off_t} does not default to a
4608 64bit entity.
4609 @end defmac
4611 @defmac AC_FUNC_GETGROUPS
4612 @acindex{FUNC_GETGROUPS}
4613 @cvindex HAVE_GETGROUPS
4614 @ovindex GETGROUPS_LIBS
4615 @c @fuindex getgroups
4616 @prindex @code{getgroups}
4617 If the @code{getgroups} function is available and works (unlike on
4618 Ultrix 4.3, where @samp{getgroups (0, 0)} always fails), define
4619 @code{HAVE_GETGROUPS}.  Set @code{GETGROUPS_LIBS} to any libraries
4620 needed to get that function.  This macro runs @code{AC_TYPE_GETGROUPS}.
4621 @end defmac
4623 @anchor{AC_FUNC_GETLOADAVG}
4624 @defmac AC_FUNC_GETLOADAVG
4625 @acindex{FUNC_GETLOADAVG}
4626 @cvindex SVR4
4627 @cvindex DGUX
4628 @cvindex UMAX
4629 @cvindex UMAX4_3
4630 @cvindex HAVE_NLIST_H
4631 @cvindex NLIST_NAME_UNION
4632 @cvindex GETLOADAVG_PRIVILEGED
4633 @cvindex NEED_SETGID
4634 @cvindex C_GETLOADAVG
4635 @ovindex LIBOBJS
4636 @ovindex NEED_SETGID
4637 @ovindex KMEM_GROUP
4638 @ovindex GETLOADAVG_LIBS
4639 @c @fuindex getloadavg
4640 @prindex @code{getloadavg}
4641 Check how to get the system load averages.  To perform its tests
4642 properly, this macro needs the file @file{getloadavg.c}; therefore, be
4643 sure to set the @code{AC_LIBOBJ} replacement directory properly (see
4644 @ref{Generic Functions}, @code{AC_CONFIG_LIBOBJ_DIR}).
4646 If the system has the @code{getloadavg} function, define
4647 @code{HAVE_GETLOADAVG}, and set @code{GETLOADAVG_LIBS} to any libraries
4648 necessary to get that function.  Also add @code{GETLOADAVG_LIBS} to
4649 @code{LIBS}.  Otherwise, require an @code{AC_LIBOBJ} replacement for
4650 @samp{getloadavg} with source code in @file{@var{dir}/getloadavg.c}, and
4651 possibly define several other C preprocessor macros and output
4652 variables:
4654 @enumerate
4655 @item
4656 Define @code{C_GETLOADAVG}.
4658 @item
4659 Define @code{SVR4}, @code{DGUX}, @code{UMAX}, or @code{UMAX4_3} if on
4660 those systems.
4662 @item
4663 @hdrindex{nlist.h}
4664 If @file{nlist.h} is found, define @code{HAVE_NLIST_H}.
4666 @item
4667 If @samp{struct nlist} has an @samp{n_un.n_name} member, define
4668 @code{HAVE_STRUCT_NLIST_N_UN_N_NAME}.  The obsolete symbol
4669 @code{NLIST_NAME_UNION} is still defined, but do not depend upon it.
4671 @item
4672 Programs may need to be installed set-group-ID (or set-user-ID) for
4673 @code{getloadavg} to work.  In this case, define
4674 @code{GETLOADAVG_PRIVILEGED}, set the output variable @code{NEED_SETGID}
4675 to @samp{true} (and otherwise to @samp{false}), and set
4676 @code{KMEM_GROUP} to the name of the group that should own the installed
4677 program.
4678 @end enumerate
4680 The @code{AC_FUNC_GETLOADAVG} macro is obsolescent.  New programs should
4681 use Gnulib's @code{getloadavg} module.  @xref{Gnulib}.
4682 @end defmac
4684 @anchor{AC_FUNC_GETMNTENT}
4685 @defmac AC_FUNC_GETMNTENT
4686 @acindex{FUNC_GETMNTENT}
4687 @cvindex HAVE_GETMNTENT
4688 @c @fuindex getmntent
4689 @prindex @code{getmntent}
4690 Check for @code{getmntent} in the standard C library, and then in the
4691 @file{sun}, @file{seq}, and @file{gen} libraries, for @sc{unicos},
4692 @sc{irix} 4, @sc{ptx}, and UnixWare, respectively.  Then, if
4693 @code{getmntent} is available, define @code{HAVE_GETMNTENT}.
4694 @end defmac
4696 @defmac AC_FUNC_GETPGRP
4697 @acindex{FUNC_GETPGRP}
4698 @cvindex GETPGRP_VOID
4699 @c @fuindex getpgid
4700 @c @fuindex getpgrp
4701 @prindex @code{getpgid}
4702 @prindex @code{getpgrp}
4703 Define @code{GETPGRP_VOID} if it is an error to pass 0 to
4704 @code{getpgrp}; this is the Posix behavior.  On older @acronym{BSD}
4705 systems, you must pass 0 to @code{getpgrp}, as it takes an argument and
4706 behaves like Posix's @code{getpgid}.
4708 @example
4709 #ifdef GETPGRP_VOID
4710   pid = getpgrp ();
4711 #else
4712   pid = getpgrp (0);
4713 #endif
4714 @end example
4716 This macro does not check whether
4717 @code{getpgrp} exists at all; if you need to work in that situation,
4718 first call @code{AC_CHECK_FUNC} for @code{getpgrp}.
4720 This macro is obsolescent, as current systems have a @code{getpgrp}
4721 whose signature conforms to Posix.  New programs need not use this macro.
4722 @end defmac
4724 @defmac AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
4725 @acindex{FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK}
4726 @cvindex LSTAT_FOLLOWS_SLASHED_SYMLINK
4727 @c @fuindex lstat
4728 @prindex @code{lstat}
4729 If @file{link} is a symbolic link, then @code{lstat} should treat
4730 @file{link/} the same as @file{link/.}.  However, many older
4731 @code{lstat} implementations incorrectly ignore trailing slashes.
4733 It is safe to assume that if @code{lstat} incorrectly ignores
4734 trailing slashes, then other symbolic-link-aware functions like
4735 @code{unlink} also incorrectly ignore trailing slashes.
4737 If @code{lstat} behaves properly, define
4738 @code{LSTAT_FOLLOWS_SLASHED_SYMLINK}, otherwise require an
4739 @code{AC_LIBOBJ} replacement of @code{lstat}.
4740 @end defmac
4742 @defmac AC_FUNC_MALLOC
4743 @acindex{FUNC_MALLOC}
4744 @cvindex HAVE_MALLOC
4745 @cvindex malloc
4746 @c @fuindex malloc
4747 @prindex @code{malloc}
4748 If the @code{malloc} function is compatible with the @acronym{GNU} C
4749 library @code{malloc} (i.e., @samp{malloc (0)} returns a valid
4750 pointer), define @code{HAVE_MALLOC} to 1.  Otherwise define
4751 @code{HAVE_MALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4752 @samp{malloc}, and define @code{malloc} to @code{rpl_malloc} so that the
4753 native @code{malloc} is not used in the main project.
4755 Typically, the replacement file @file{malloc.c} should look like (note
4756 the @samp{#undef malloc}):
4758 @verbatim
4759 #ifdef HAVE_CONFIG_H
4760 # include <config.h>
4761 #endif
4762 #undef malloc
4764 #include <sys/types.h>
4766 void *malloc ();
4768 /* Allocate an N-byte block of memory from the heap.
4769    If N is zero, allocate a 1-byte block.  */
4771 void *
4772 rpl_malloc (size_t n)
4774   if (n == 0)
4775     n = 1;
4776   return malloc (n);
4778 @end verbatim
4779 @end defmac
4781 @defmac AC_FUNC_MEMCMP
4782 @acindex{FUNC_MEMCMP}
4783 @ovindex LIBOBJS
4784 @c @fuindex memcmp
4785 @prindex @code{memcmp}
4786 If the @code{memcmp} function is not available, or does not work on
4787 8-bit data (like the one on SunOS 4.1.3), or fails when comparing 16
4788 bytes or more and with at least one buffer not starting on a 4-byte
4789 boundary (such as the one on NeXT x86 OpenStep), require an
4790 @code{AC_LIBOBJ} replacement for @samp{memcmp}.
4792 This macro is obsolescent, as current systems have a working
4793 @code{memcmp}.  New programs need not use this macro.
4794 @end defmac
4796 @defmac AC_FUNC_MBRTOWC
4797 @acindex{FUNC_MBRTOWC}
4798 @cvindex HAVE_MBRTOWC
4799 @c @fuindex mbrtowc
4800 @prindex @code{mbrtowc}
4801 Define @code{HAVE_MBRTOWC} to 1 if the function @code{mbrtowc} and the
4802 type @code{mbstate_t} are properly declared.
4803 @end defmac
4805 @defmac AC_FUNC_MKTIME
4806 @acindex{FUNC_MKTIME}
4807 @ovindex LIBOBJS
4808 @c @fuindex mktime
4809 @prindex @code{mktime}
4810 If the @code{mktime} function is not available, or does not work
4811 correctly, require an @code{AC_LIBOBJ} replacement for @samp{mktime}.
4812 For the purposes of this test, @code{mktime} should conform to the
4813 Posix standard and should be the inverse of
4814 @code{localtime}.
4815 @end defmac
4817 @anchor{AC_FUNC_MMAP}
4818 @defmac AC_FUNC_MMAP
4819 @acindex{FUNC_MMAP}
4820 @cvindex HAVE_MMAP
4821 @c @fuindex mmap
4822 @prindex @code{mmap}
4823 If the @code{mmap} function exists and works correctly, define
4824 @code{HAVE_MMAP}.  This checks only private fixed mapping of already-mapped
4825 memory.
4826 @end defmac
4828 @defmac AC_FUNC_OBSTACK
4829 @acindex{FUNC_OBSTACK}
4830 @cvindex HAVE_OBSTACK
4831 @cindex obstack
4832 If the obstacks are found, define @code{HAVE_OBSTACK}, else require an
4833 @code{AC_LIBOBJ} replacement for @samp{obstack}.
4834 @end defmac
4836 @defmac AC_FUNC_REALLOC
4837 @acindex{FUNC_REALLOC}
4838 @cvindex HAVE_REALLOC
4839 @cvindex realloc
4840 @c @fuindex realloc
4841 @prindex @code{realloc}
4842 If the @code{realloc} function is compatible with the @acronym{GNU} C
4843 library @code{realloc} (i.e., @samp{realloc (NULL, 0)} returns a
4844 valid pointer), define @code{HAVE_REALLOC} to 1.  Otherwise define
4845 @code{HAVE_REALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4846 @samp{realloc}, and define @code{realloc} to @code{rpl_realloc} so that
4847 the native @code{realloc} is not used in the main project.  See
4848 @code{AC_FUNC_MALLOC} for details.
4849 @end defmac
4851 @defmac AC_FUNC_SELECT_ARGTYPES
4852 @acindex{FUNC_SELECT_ARGTYPES}
4853 @cvindex SELECT_TYPE_ARG1
4854 @cvindex SELECT_TYPE_ARG234
4855 @cvindex SELECT_TYPE_ARG5
4856 @c @fuindex select
4857 @prindex @code{select}
4858 Determines the correct type to be passed for each of the
4859 @code{select} function's arguments, and defines those types
4860 in @code{SELECT_TYPE_ARG1}, @code{SELECT_TYPE_ARG234}, and
4861 @code{SELECT_TYPE_ARG5} respectively.  @code{SELECT_TYPE_ARG1} defaults
4862 to @samp{int}, @code{SELECT_TYPE_ARG234} defaults to @samp{int *},
4863 and @code{SELECT_TYPE_ARG5} defaults to @samp{struct timeval *}.
4865 This macro is obsolescent, as current systems have a @code{select} whose
4866 signature conforms to Posix.  New programs need not use this macro.
4867 @end defmac
4869 @defmac AC_FUNC_SETPGRP
4870 @acindex{FUNC_SETPGRP}
4871 @cvindex SETPGRP_VOID
4872 @c @fuindex setpgrp
4873 @prindex @code{setpgrp}
4874 If @code{setpgrp} takes no argument (the Posix version), define
4875 @code{SETPGRP_VOID}.  Otherwise, it is the @acronym{BSD} version, which takes
4876 two process IDs as arguments.  This macro does not check whether
4877 @code{setpgrp} exists at all; if you need to work in that situation,
4878 first call @code{AC_CHECK_FUNC} for @code{setpgrp}.
4880 This macro is obsolescent, as current systems have a @code{setpgrp}
4881 whose signature conforms to Posix.  New programs need not use this macro.
4882 @end defmac
4884 @defmac AC_FUNC_STAT
4885 @defmacx AC_FUNC_LSTAT
4886 @acindex{FUNC_STAT}
4887 @acindex{FUNC_LSTAT}
4888 @cvindex HAVE_STAT_EMPTY_STRING_BUG
4889 @cvindex HAVE_LSTAT_EMPTY_STRING_BUG
4890 @c @fuindex stat
4891 @prindex @code{stat}
4892 @c @fuindex lstat
4893 @prindex @code{lstat}
4894 Determine whether @code{stat} or @code{lstat} have the bug that it
4895 succeeds when given the zero-length file name as argument.  The @code{stat}
4896 and @code{lstat} from SunOS 4.1.4 and the Hurd (as of 1998-11-01) do
4897 this.
4899 If it does, then define @code{HAVE_STAT_EMPTY_STRING_BUG} (or
4900 @code{HAVE_LSTAT_EMPTY_STRING_BUG}) and ask for an @code{AC_LIBOBJ}
4901 replacement of it.
4903 These macros are obsolescent, as no current systems have the bug.
4904 New programs need not use these macros.
4905 @end defmac
4907 @anchor{AC_FUNC_STRCOLL}
4908 @defmac AC_FUNC_STRCOLL
4909 @acindex{FUNC_STRCOLL}
4910 @cvindex HAVE_STRCOLL
4911 @c @fuindex strcoll
4912 @prindex @code{strcoll}
4913 If the @code{strcoll} function exists and works correctly, define
4914 @code{HAVE_STRCOLL}.  This does a bit more than
4915 @samp{AC_CHECK_FUNCS(strcoll)}, because some systems have incorrect
4916 definitions of @code{strcoll} that should not be used.
4917 @end defmac
4919 @defmac AC_FUNC_STRERROR_R
4920 @acindex{FUNC_STRERROR_R}
4921 @cvindex HAVE_STRERROR_R
4922 @cvindex HAVE_DECL_STRERROR_R
4923 @cvindex STRERROR_R_CHAR_P
4924 @c @fuindex strerror_r
4925 @prindex @code{strerror_r}
4926 If @code{strerror_r} is available, define @code{HAVE_STRERROR_R}, and if
4927 it is declared, define @code{HAVE_DECL_STRERROR_R}.  If it returns a
4928 @code{char *} message, define @code{STRERROR_R_CHAR_P}; otherwise it
4929 returns an @code{int} error number.  The Thread-Safe Functions option of
4930 Posix requires @code{strerror_r} to return @code{int}, but
4931 many systems (including, for example, version 2.2.4 of the @acronym{GNU} C
4932 Library) return a @code{char *} value that is not necessarily equal to
4933 the buffer argument.
4934 @end defmac
4936 @anchor{AC_FUNC_STRFTIME}
4937 @defmac AC_FUNC_STRFTIME
4938 @acindex{FUNC_STRFTIME}
4939 @cvindex HAVE_STRFTIME
4940 @c @fuindex strftime
4941 @prindex @code{strftime}
4942 Check for @code{strftime} in the @file{intl} library, for SCO Unix.
4943 Then, if @code{strftime} is available, define @code{HAVE_STRFTIME}.
4945 This macro is obsolescent, as no current systems require the @file{intl}
4946 library for @code{strftime}.  New programs need not use this macro.
4947 @end defmac
4949 @defmac AC_FUNC_STRTOD
4950 @acindex{FUNC_STRTOD}
4951 @ovindex POW_LIB
4952 @c @fuindex strtod
4953 @prindex @code{strtod}
4954 If the @code{strtod} function does not exist or doesn't work correctly,
4955 ask for an @code{AC_LIBOBJ} replacement of @samp{strtod}.  In this case,
4956 because @file{strtod.c} is likely to need @samp{pow}, set the output
4957 variable @code{POW_LIB} to the extra library needed.
4958 @end defmac
4960 @defmac AC_FUNC_STRTOLD
4961 @acindex{FUNC_STRTOLD}
4962 @cvindex HAVE_STRTOLD
4963 @prindex @code{strtold}
4964 If the @code{strtold} function exists and conforms to C99, define
4965 @code{HAVE_STRTOLD}.
4966 @end defmac
4968 @defmac AC_FUNC_STRNLEN
4969 @acindex{FUNC_STRNLEN}
4970 @cvindex HAVE_STRNLEN
4971 @c @fuindex strnlen
4972 @prindex @code{strnlen}
4973 If the @code{strnlen} function is not available, or is buggy (like the one
4974 from @acronym{AIX} 4.3), require an @code{AC_LIBOBJ} replacement for it.
4975 @end defmac
4977 @anchor{AC_FUNC_UTIME_NULL}
4978 @defmac AC_FUNC_UTIME_NULL
4979 @acindex{FUNC_UTIME_NULL}
4980 @cvindex HAVE_UTIME_NULL
4981 @c @fuindex utime
4982 @prindex @code{utime}
4983 If @samp{utime (@var{file}, NULL)} sets @var{file}'s timestamp to
4984 the present, define @code{HAVE_UTIME_NULL}.
4986 This macro is obsolescent, as all current systems have a @code{utime}
4987 that behaves this way.  New programs need not use this macro.
4988 @end defmac
4990 @anchor{AC_FUNC_VPRINTF}
4991 @defmac AC_FUNC_VPRINTF
4992 @acindex{FUNC_VPRINTF}
4993 @cvindex HAVE_VPRINTF
4994 @cvindex HAVE_DOPRNT
4995 @c @fuindex vprintf
4996 @prindex @code{vprintf}
4997 If @code{vprintf} is found, define @code{HAVE_VPRINTF}.  Otherwise, if
4998 @code{_doprnt} is found, define @code{HAVE_DOPRNT}.  (If @code{vprintf}
4999 is available, you may assume that @code{vfprintf} and @code{vsprintf}
5000 are also available.)
5002 This macro is obsolescent, as all current systems have @code{vprintf}.
5003 New programs need not use this macro.
5004 @end defmac
5006 @defmac AC_REPLACE_FNMATCH
5007 @acindex{REPLACE_FNMATCH}
5008 @c @fuindex fnmatch
5009 @prindex @code{fnmatch}
5010 @hdrindex{fnmatch.h}
5011 If the @code{fnmatch} function does not conform to Posix (see
5012 @code{AC_FUNC_FNMATCH}), ask for its @code{AC_LIBOBJ} replacement.
5014 The files @file{fnmatch.c}, @file{fnmatch_loop.c}, and @file{fnmatch_.h}
5015 in the @code{AC_LIBOBJ} replacement directory are assumed to contain a
5016 copy of the source code of @acronym{GNU} @code{fnmatch}.  If necessary,
5017 this source code is compiled as an @code{AC_LIBOBJ} replacement, and the
5018 @file{fnmatch_.h} file is linked to @file{fnmatch.h} so that it can be
5019 included in place of the system @code{<fnmatch.h>}.
5021 This macro is obsolescent, as it assumes the use of particular source
5022 files.  New programs should use Gnulib's @code{fnmatch-posix} module,
5023 which provides this macro along with the source files.  @xref{Gnulib}.
5024 @end defmac
5028 @node Generic Functions
5029 @subsection Generic Function Checks
5031 These macros are used to find functions not covered by the ``particular''
5032 test macros.  If the functions might be in libraries other than the
5033 default C library, first call @code{AC_CHECK_LIB} for those libraries.
5034 If you need to check the behavior of a function as well as find out
5035 whether it is present, you have to write your own test for
5036 it (@pxref{Writing Tests}).
5038 @anchor{AC_CHECK_FUNC}
5039 @defmac AC_CHECK_FUNC (@var{function}, @ovar{action-if-found}, @
5040   @ovar{action-if-not-found})
5041 @acindex{CHECK_FUNC}
5042 If C function @var{function} is available, run shell commands
5043 @var{action-if-found}, otherwise @var{action-if-not-found}.  If you just
5044 want to define a symbol if the function is available, consider using
5045 @code{AC_CHECK_FUNCS} instead.  This macro checks for functions with C
5046 linkage even when @code{AC_LANG(C++)} has been called, since C is more
5047 standardized than C++.  (@pxref{Language Choice}, for more information
5048 about selecting the language for checks.)
5049 @end defmac
5051 @anchor{AC_CHECK_FUNCS}
5052 @defmac AC_CHECK_FUNCS (@var{function}@dots{}, @ovar{action-if-found}, @
5053   @ovar{action-if-not-found})
5054 @acindex{CHECK_FUNCS}
5055 @cvindex HAVE_@var{function}
5056 For each @var{function} enumerated in the blank-or-newline-separated argument
5057 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5058 If @var{action-if-found} is given, it is additional shell code to
5059 execute when one of the functions is found.  You can give it a value of
5060 @samp{break} to break out of the loop on the first match.  If
5061 @var{action-if-not-found} is given, it is executed when one of the
5062 functions is not found.
5063 @end defmac
5065 @defmac AC_CHECK_FUNCS_ONCE (@var{function}@dots{})
5066 @acindex{CHECK_FUNCS_ONCE}
5067 @cvindex HAVE_@var{function}
5068 For each @var{function} enumerated in the blank-or-newline-separated argument
5069 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5070 This is a once-only variant of @code{AC_CHECK_FUNCS}.  It generates the
5071 checking code at most once, so that @command{configure} is smaller and
5072 faster; but the checks cannot be conditionalized and are always done once,
5073 early during the @command{configure} run.
5074 @end defmac
5076 @sp 1
5078 Autoconf follows a philosophy that was formed over the years by those
5079 who have struggled for portability: isolate the portability issues in
5080 specific files, and then program as if you were in a Posix
5081 environment.  Some functions may be missing or unfixable, and your
5082 package must be ready to replace them.
5084 Suitable replacements for many such problem functions are available from
5085 Gnulib (@pxref{Gnulib}).
5087 @defmac AC_LIBOBJ (@var{function})
5088 @acindex{LIBOBJ}
5089 @ovindex LIBOBJS
5090 Specify that @samp{@var{function}.c} must be included in the executables
5091 to replace a missing or broken implementation of @var{function}.
5093 Technically, it adds @samp{@var{function}.$ac_objext} to the output
5094 variable @code{LIBOBJS} if it is not already in, and calls
5095 @code{AC_LIBSOURCE} for @samp{@var{function}.c}.  You should not
5096 directly change @code{LIBOBJS}, since this is not traceable.
5097 @end defmac
5099 @defmac AC_LIBSOURCE (@var{file})
5100 @acindex{LIBSOURCE}
5101 Specify that @var{file} might be needed to compile the project.  If you
5102 need to know what files might be needed by a @file{configure.ac}, you
5103 should trace @code{AC_LIBSOURCE}.  @var{file} must be a literal.
5105 This macro is called automatically from @code{AC_LIBOBJ}, but you must
5106 call it explicitly if you pass a shell variable to @code{AC_LIBOBJ}.  In
5107 that case, since shell variables cannot be traced statically, you must
5108 pass to @code{AC_LIBSOURCE} any possible files that the shell variable
5109 might cause @code{AC_LIBOBJ} to need.  For example, if you want to pass
5110 a variable @code{$foo_or_bar} to @code{AC_LIBOBJ} that holds either
5111 @code{"foo"} or @code{"bar"}, you should do:
5113 @example
5114 AC_LIBSOURCE([foo.c])
5115 AC_LIBSOURCE([bar.c])
5116 AC_LIBOBJ([$foo_or_bar])
5117 @end example
5119 @noindent
5120 There is usually a way to avoid this, however, and you are encouraged to
5121 simply call @code{AC_LIBOBJ} with literal arguments.
5123 Note that this macro replaces the obsolete @code{AC_LIBOBJ_DECL}, with
5124 slightly different semantics: the old macro took the function name,
5125 e.g., @code{foo}, as its argument rather than the file name.
5126 @end defmac
5128 @defmac AC_LIBSOURCES (@var{files})
5129 @acindex{LIBSOURCES}
5130 Like @code{AC_LIBSOURCE}, but accepts one or more @var{files} in a
5131 comma-separated M4 list.  Thus, the above example might be rewritten:
5133 @example
5134 AC_LIBSOURCES([foo.c, bar.c])
5135 AC_LIBOBJ([$foo_or_bar])
5136 @end example
5137 @end defmac
5139 @defmac AC_CONFIG_LIBOBJ_DIR (@var{directory})
5140 @acindex{CONFIG_LIBOBJ_DIR}
5141 Specify that @code{AC_LIBOBJ} replacement files are to be found in
5142 @var{directory}, a name relative to the top level of the
5143 source tree.  The replacement directory defaults to @file{.}, the top
5144 level directory, and the most typical value is @file{lib}, corresponding
5145 to @samp{AC_CONFIG_LIBOBJ_DIR([lib])}.
5147 @command{configure} might need to know the replacement directory for the
5148 following reasons: (i) some checks use the replacement files, (ii) some
5149 macros bypass broken system headers by installing links to the
5150 replacement headers (iii) when used in conjunction with Automake,
5151 within each makefile, @var{directory} is used as a relative path
5152 from @code{$(top_srcdir)} to each object named in @code{LIBOBJS} and
5153 @code{LTLIBOBJS}, etc.
5154 @end defmac
5156 @sp 1
5158 It is common to merely check for the existence of a function, and ask
5159 for its @code{AC_LIBOBJ} replacement if missing.  The following macro is
5160 a convenient shorthand.
5162 @defmac AC_REPLACE_FUNCS (@var{function}@dots{})
5163 @acindex{REPLACE_FUNCS}
5164 @cvindex HAVE_@var{function}
5165 @ovindex LIBOBJS
5166 Like @code{AC_CHECK_FUNCS}, but uses @samp{AC_LIBOBJ(@var{function})} as
5167 @var{action-if-not-found}.  You can declare your replacement function by
5168 enclosing the prototype in @samp{#ifndef HAVE_@var{function}}.  If the
5169 system has the function, it probably declares it in a header file you
5170 should be including, so you shouldn't redeclare it lest your declaration
5171 conflict.
5172 @end defmac
5174 @node Header Files
5175 @section Header Files
5176 @cindex Header, checking
5178 The following macros check for the presence of certain C header files.
5179 If there is no macro specifically defined to check for a header file you need,
5180 and you don't need to check for any special properties of
5181 it, then you can use one of the general header-file check macros.
5183 @menu
5184 * Header Portability::          Collected knowledge on common headers
5185 * Particular Headers::          Special handling to find certain headers
5186 * Generic Headers::             How to find other headers
5187 @end menu
5189 @node Header Portability
5190 @subsection Portability of Headers
5191 @cindex Portability of headers
5192 @cindex Header portability
5194 This section tries to collect knowledge about common headers, and the
5195 problems they cause.  By definition, this list always requires
5196 additions.  Please help us keeping it as complete as possible.
5198 @table @asis
5200 @item @file{limits.h}
5201 C99 says that @file{limits.h} defines @code{LLONG_MIN},
5202 @code{LLONG_MAX}, and @code{ULLONG_MAX}, but many almost-C99
5203 environments (e.g., default @acronym{GCC} 4.0.2 + glibc 2.4) do not
5204 define them.
5206 @item @file{inttypes.h} vs.@: @file{stdint.h}
5207 @hdrindex{inttypes.h}
5208 @hdrindex{stdint.h}
5209 The C99 standard says that @file{inttypes.h} includes
5210 @file{stdint.h}, so there's no need to include @file{stdint.h}
5211 separately in a standard environment.  Some implementations have
5212 @file{inttypes.h} but not @file{stdint.h} (e.g., Solaris 7), but we don't
5213 know of any implementation that has @file{stdint.h} but not
5214 @file{inttypes.h}.
5216 @item @file{linux/irda.h}
5217 @hdrindex{linux/irda.h}
5218 It requires @file{linux/types.h} and @file{sys/socket.h}.
5220 @item @file{linux/random.h}
5221 @hdrindex{linux/random.h}
5222 It requires @file{linux/types.h}.
5224 @item @file{net/if.h}
5225 @hdrindex{net/if.h}
5226 On Darwin, this file requires that @file{sys/socket.h} be included
5227 beforehand.  One should run:
5229 @example
5230 AC_CHECK_HEADERS([sys/socket.h])
5231 AC_CHECK_HEADERS([net/if.h], [], [],
5232 [#include <stdio.h>
5233 #ifdef STDC_HEADERS
5234 # include <stdlib.h>
5235 # include <stddef.h>
5236 #else
5237 # ifdef HAVE_STDLIB_H
5238 #  include <stdlib.h>
5239 # endif
5240 #endif
5241 #ifdef HAVE_SYS_SOCKET_H
5242 # include <sys/socket.h>
5243 #endif
5245 @end example
5247 @item @file{netinet/if_ether.h}
5248 @hdrindex{netinet/if_ether.h}
5249 On Darwin, this file requires that @file{stdio.h} and
5250 @file{sys/socket.h} be included beforehand.  One should run:
5252 @example
5253 AC_CHECK_HEADERS([sys/socket.h])
5254 AC_CHECK_HEADERS([netinet/if_ether.h], [], [],
5255 [#include <stdio.h>
5256 #ifdef STDC_HEADERS
5257 # include <stdlib.h>
5258 # include <stddef.h>
5259 #else
5260 # ifdef HAVE_STDLIB_H
5261 #  include <stdlib.h>
5262 # endif
5263 #endif
5264 #ifdef HAVE_SYS_SOCKET_H
5265 # include <sys/socket.h>
5266 #endif
5268 @end example
5270 @item @file{stdint.h}
5271 See above, item @file{inttypes.h} vs.@: @file{stdint.h}.
5273 @item @file{stdlib.h}
5274 @hdrindex{stdlib.h}
5275 On many systems (e.g., Darwin), @file{stdio.h} is a prerequisite.
5277 @item @file{sys/mount.h}
5278 @hdrindex{sys/mount.h}
5279 On Free@acronym{BSD} 4.8 on ia32 and using gcc version 2.95.4,
5280 @file{sys/params.h} is a prerequisite.
5282 @item @file{sys/ptem.h}
5283 @hdrindex{sys/ptem.h}
5284 On Solaris 8, @file{sys/stream.h} is a prerequisite.
5286 @item @file{sys/socket.h}
5287 @hdrindex{sys/socket.h}
5288 On Darwin, @file{stdlib.h} is a prerequisite.
5290 @item @file{sys/ucred.h}
5291 @hdrindex{sys/ucred.h}
5292 On Tru64 5.1, @file{sys/types.h} is a prerequisite.
5294 @item @file{X11/extensions/scrnsaver.h}
5295 @hdrindex{X11/extensions/scrnsaver.h}
5296 Using XFree86, this header requires @file{X11/Xlib.h}, which is probably
5297 so required that you might not even consider looking for it.
5299 @example
5300 AC_CHECK_HEADERS([X11/extensions/scrnsaver.h], [], [],
5301 [[#include <X11/Xlib.h>
5303 @end example
5304 @end table
5307 @node Particular Headers
5308 @subsection Particular Header Checks
5310 These macros check for particular system header files---whether they
5311 exist, and in some cases whether they declare certain symbols.
5313 @defmac AC_HEADER_ASSERT
5314 @acindex{HEADER_ASSERT}
5315 @cvindex NDEBUG
5316 @hdrindex{assert.h}
5317 Check whether to enable assertions in the style of @file{assert.h}.
5318 Assertions are enabled by default, but the user can override this by
5319 invoking @command{configure} with the @option{--disable-assert} option.
5320 @end defmac
5322 @anchor{AC_HEADER_DIRENT}
5323 @defmac AC_HEADER_DIRENT
5324 @acindex{HEADER_DIRENT}
5325 @cvindex HAVE_DIRENT_H
5326 @cvindex HAVE_NDIR_H
5327 @cvindex HAVE_SYS_DIR_H
5328 @cvindex HAVE_SYS_NDIR_H
5329 @hdrindex{dirent.h}
5330 @hdrindex{sys/ndir.h}
5331 @hdrindex{sys/dir.h}
5332 @hdrindex{ndir.h}
5333 Check for the following header files.  For the first one that is
5334 found and defines @samp{DIR}, define the listed C preprocessor macro:
5336 @multitable {@file{sys/ndir.h}} {@code{HAVE_SYS_NDIR_H}}
5337 @item @file{dirent.h}   @tab @code{HAVE_DIRENT_H}
5338 @item @file{sys/ndir.h} @tab @code{HAVE_SYS_NDIR_H}
5339 @item @file{sys/dir.h}  @tab @code{HAVE_SYS_DIR_H}
5340 @item @file{ndir.h}     @tab @code{HAVE_NDIR_H}
5341 @end multitable
5343 The directory-library declarations in your source code should look
5344 something like the following:
5346 @example
5347 @group
5348 #include <sys/types.h>
5349 #ifdef HAVE_DIRENT_H
5350 # include <dirent.h>
5351 # define NAMLEN(dirent) strlen ((dirent)->d_name)
5352 #else
5353 # define dirent direct
5354 # define NAMLEN(dirent) ((dirent)->d_namlen)
5355 # ifdef HAVE_SYS_NDIR_H
5356 #  include <sys/ndir.h>
5357 # endif
5358 # ifdef HAVE_SYS_DIR_H
5359 #  include <sys/dir.h>
5360 # endif
5361 # ifdef HAVE_NDIR_H
5362 #  include <ndir.h>
5363 # endif
5364 #endif
5365 @end group
5366 @end example
5368 Using the above declarations, the program would declare variables to be
5369 of type @code{struct dirent}, not @code{struct direct}, and would access
5370 the length of a directory entry name by passing a pointer to a
5371 @code{struct dirent} to the @code{NAMLEN} macro.
5373 This macro also checks for the SCO Xenix @file{dir} and @file{x} libraries.
5375 This macro is obsolescent, as all current systems with directory
5376 libraries have @code{<dirent.h>}.  New programs need not use this macro.
5378 Also see @code{AC_STRUCT_DIRENT_D_INO} and
5379 @code{AC_STRUCT_DIRENT_D_TYPE} (@pxref{Particular Structures}).
5380 @end defmac
5382 @anchor{AC_HEADER_MAJOR}
5383 @defmac AC_HEADER_MAJOR
5384 @acindex{HEADER_MAJOR}
5385 @cvindex MAJOR_IN_MKDEV
5386 @cvindex MAJOR_IN_SYSMACROS
5387 @hdrindex{sys/mkdev.h}
5388 @hdrindex{sys/sysmacros.h}
5389 If @file{sys/types.h} does not define @code{major}, @code{minor}, and
5390 @code{makedev}, but @file{sys/mkdev.h} does, define
5391 @code{MAJOR_IN_MKDEV}; otherwise, if @file{sys/sysmacros.h} does, define
5392 @code{MAJOR_IN_SYSMACROS}.
5393 @end defmac
5395 @defmac AC_HEADER_RESOLV
5396 @acindex{HEADER_RESOLV}
5397 @cvindex HAVE_RESOLV_H
5398 @hdrindex{resolv.h}
5399 Checks for header @file{resolv.h}, checking for prerequisites first.
5400 To properly use @file{resolv.h}, your code should contain something like
5401 the following:
5403 @verbatim
5404 #ifdef HAVE_SYS_TYPES_H
5405 #  include <sys/types.h>
5406 #endif
5407 #ifdef HAVE_NETINET_IN_H
5408 #  include <netinet/in.h>   /* inet_ functions / structs */
5409 #endif
5410 #ifdef HAVE_ARPA_NAMESER_H
5411 #  include <arpa/nameser.h> /* DNS HEADER struct */
5412 #endif
5413 #ifdef HAVE_NETDB_H
5414 #  include <netdb.h>
5415 #endif
5416 #include <resolv.h>
5417 @end verbatim
5418 @end defmac
5420 @anchor{AC_HEADER_STAT}
5421 @defmac AC_HEADER_STAT
5422 @acindex{HEADER_STAT}
5423 @cvindex STAT_MACROS_BROKEN
5424 @hdrindex{sys/stat.h}
5425 If the macros @code{S_ISDIR}, @code{S_ISREG}, etc.@: defined in
5426 @file{sys/stat.h} do not work properly (returning false positives),
5427 define @code{STAT_MACROS_BROKEN}.  This is the case on Tektronix UTekV,
5428 Amdahl UTS and Motorola System V/88.
5430 This macro is obsolescent, as no current systems have the bug.
5431 New programs need not use this macro.
5432 @end defmac
5434 @defmac AC_HEADER_STDBOOL
5435 @acindex{HEADER_STDBOOL}
5436 @cvindex HAVE_STDBOOL_H
5437 @cvindex HAVE__BOOL
5438 @hdrindex{stdbool.h}
5439 @hdrindex{system.h}
5440 If @file{stdbool.h} exists and conforms to C99, define
5441 @code{HAVE_STDBOOL_H} to 1; if the type @code{_Bool} is defined, define
5442 @code{HAVE__BOOL} to 1.  To fulfill the C99 requirements, your
5443 @file{system.h} could contain the following code:
5445 @verbatim
5446 #ifdef HAVE_STDBOOL_H
5447 # include <stdbool.h>
5448 #else
5449 # ifndef HAVE__BOOL
5450 #  ifdef __cplusplus
5451 typedef bool _Bool;
5452 #  else
5453 #   define _Bool signed char
5454 #  endif
5455 # endif
5456 # define bool _Bool
5457 # define false 0
5458 # define true 1
5459 # define __bool_true_false_are_defined 1
5460 #endif
5461 @end verbatim
5463 Alternatively you can use the @samp{stdbool} package of Gnulib
5464 (@pxref{Gnulib}); it packages the above code into a replacement header
5465 and contains a few other bells and whistles.
5467 @end defmac
5469 @anchor{AC_HEADER_STDC}
5470 @defmac AC_HEADER_STDC
5471 @acindex{HEADER_STDC}
5472 @cvindex STDC_HEADERS
5473 @hdrindex{stdlib.h}
5474 @hdrindex{stdarg.h}
5475 @hdrindex{string.h}
5476 @hdrindex{float.h}
5477 @hdrindex{ctype.h}
5478 Define @code{STDC_HEADERS} if the system has C header files
5479 conforming to @acronym{ANSI} C89 (@acronym{ISO} C90).
5480 Specifically, this macro checks for @file{stdlib.h}, @file{stdarg.h},
5481 @file{string.h}, and @file{float.h}; if the system has those, it
5482 probably has the rest of the C89 header files.  This macro also
5483 checks whether @file{string.h} declares @code{memchr} (and thus
5484 presumably the other @code{mem} functions), whether @file{stdlib.h}
5485 declare @code{free} (and thus presumably @code{malloc} and other related
5486 functions), and whether the @file{ctype.h} macros work on characters
5487 with the high bit set, as the C standard requires.
5489 If you use this macro, your code can refer to @code{STDC_HEADERS} to
5490 determine whether the system has conforming header files (and probably C
5491 library functions).
5493 This macro is obsolescent, as current systems have conforming header
5494 files.  New programs need not use this macro.
5496 @hdrindex{string.h}
5497 @hdrindex{strings.h}
5498 Nowadays @file{string.h} is part of the C standard and declares functions like
5499 @code{strcpy}, and @file{strings.h} is standardized by Posix and declares
5500 @acronym{BSD} functions like @code{bcopy}; but
5501 historically, string functions were a major sticking point in this area.
5502 If you still want to worry about portability to ancient systems without
5503 standard headers, there is so much variation
5504 that it is probably easier to declare the functions you use than to
5505 figure out exactly what the system header files declare.  Some ancient systems
5506 contained a mix of functions from the C standard and from @acronym{BSD};
5507 some were mostly standard but lacked @samp{memmove}; some defined the
5508 @acronym{BSD} functions as macros in @file{string.h} or
5509 @file{strings.h}; some had only the @acronym{BSD} functions but
5510 @file{string.h}; some declared the memory functions in @file{memory.h},
5511 some in @file{string.h}; etc.  It is probably sufficient to check for
5512 one string function and one memory function; if the library had the
5513 standard versions of those then it probably had most of the others.
5514 If you put the following in @file{configure.ac}:
5516 @example
5517 # This example is obsolescent.
5518 # Nowadays you can omit these macro calls.
5519 AC_HEADER_STDC
5520 AC_CHECK_FUNCS([strchr memcpy])
5521 @end example
5523 @noindent
5524 then, in your code, you can use declarations like this:
5526 @example
5527 @group
5528 /* This example is obsolescent.
5529    Nowadays you can just #include <string.h>.  */
5530 #ifdef STDC_HEADERS
5531 # include <string.h>
5532 #else
5533 # ifndef HAVE_STRCHR
5534 #  define strchr index
5535 #  define strrchr rindex
5536 # endif
5537 char *strchr (), *strrchr ();
5538 # ifndef HAVE_MEMCPY
5539 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
5540 #  define memmove(d, s, n) bcopy ((s), (d), (n))
5541 # endif
5542 #endif
5543 @end group
5544 @end example
5546 @noindent
5547 If you use a function like @code{memchr}, @code{memset}, @code{strtok},
5548 or @code{strspn}, which have no @acronym{BSD} equivalent, then macros don't
5549 suffice to port to ancient hosts; you must provide an implementation of
5550 each function.  An easy
5551 way to incorporate your implementations only when needed (since the ones
5552 in system C libraries may be hand optimized) is to, taking @code{memchr}
5553 for example, put it in @file{memchr.c} and use
5554 @samp{AC_REPLACE_FUNCS([memchr])}.
5555 @end defmac
5557 @defmac AC_HEADER_SYS_WAIT
5558 @acindex{HEADER_SYS_WAIT}
5559 @cvindex HAVE_SYS_WAIT_H
5560 @hdrindex{sys/wait.h}
5561 If @file{sys/wait.h} exists and is compatible with Posix, define
5562 @code{HAVE_SYS_WAIT_H}.  Incompatibility can occur if @file{sys/wait.h}
5563 does not exist, or if it uses the old @acronym{BSD} @code{union wait} instead
5564 of @code{int} to store a status value.  If @file{sys/wait.h} is not
5565 Posix compatible, then instead of including it, define the
5566 Posix macros with their usual interpretations.  Here is an
5567 example:
5569 @example
5570 @group
5571 #include <sys/types.h>
5572 #ifdef HAVE_SYS_WAIT_H
5573 # include <sys/wait.h>
5574 #endif
5575 #ifndef WEXITSTATUS
5576 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
5577 #endif
5578 #ifndef WIFEXITED
5579 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
5580 #endif
5581 @end group
5582 @end example
5584 @noindent
5585 This macro is obsolescent, as current systems are compatible with Posix.
5586 New programs need not use this macro.
5587 @end defmac
5589 @cvindex _POSIX_VERSION
5590 @hdrindex{unistd.h}
5591 @code{_POSIX_VERSION} is defined when @file{unistd.h} is included on
5592 Posix systems.  If there is no @file{unistd.h}, it is definitely
5593 not a Posix system.  However, some non-Posix systems do
5594 have @file{unistd.h}.
5596 The way to check whether the system supports Posix is:
5598 @example
5599 @group
5600 #ifdef HAVE_UNISTD_H
5601 # include <sys/types.h>
5602 # include <unistd.h>
5603 #endif
5605 #ifdef _POSIX_VERSION
5606 /* Code for Posix systems.  */
5607 #endif
5608 @end group
5609 @end example
5611 @anchor{AC_HEADER_TIME}
5612 @defmac AC_HEADER_TIME
5613 @acindex{HEADER_TIME}
5614 @cvindex TIME_WITH_SYS_TIME
5615 @hdrindex{time.h}
5616 @hdrindex{sys/time.h}
5617 If a program may include both @file{time.h} and @file{sys/time.h},
5618 define @code{TIME_WITH_SYS_TIME}.  On some ancient systems,
5619 @file{sys/time.h} included @file{time.h}, but @file{time.h} was not
5620 protected against multiple inclusion, so programs could not explicitly
5621 include both files.  This macro is useful in programs that use, for
5622 example, @code{struct timeval} as well as
5623 @code{struct tm}.  It is best used in conjunction with
5624 @code{HAVE_SYS_TIME_H}, which can be checked for using
5625 @code{AC_CHECK_HEADERS([sys/time.h])}.
5627 @example
5628 @group
5629 #ifdef TIME_WITH_SYS_TIME
5630 # include <sys/time.h>
5631 # include <time.h>
5632 #else
5633 # ifdef HAVE_SYS_TIME_H
5634 #  include <sys/time.h>
5635 # else
5636 #  include <time.h>
5637 # endif
5638 #endif
5639 @end group
5640 @end example
5642 @noindent
5643 This macro is obsolescent, as current systems can include both files
5644 when they exist.  New programs need not use this macro.
5645 @end defmac
5648 @defmac AC_HEADER_TIOCGWINSZ
5649 @acindex{HEADER_TIOCGWINSZ}
5650 @cvindex GWINSZ_IN_SYS_IOCTL
5651 @hdrindex{sys/ioctl.h}
5652 @hdrindex{termios.h}
5653 @c FIXME: I need clarifications from Jim.
5654 If the use of @code{TIOCGWINSZ} requires @file{<sys/ioctl.h>}, then
5655 define @code{GWINSZ_IN_SYS_IOCTL}.  Otherwise @code{TIOCGWINSZ} can be
5656 found in @file{<termios.h>}.
5658 Use:
5660 @example
5661 @group
5662 #ifdef HAVE_TERMIOS_H
5663 # include <termios.h>
5664 #endif
5666 #ifdef GWINSZ_IN_SYS_IOCTL
5667 # include <sys/ioctl.h>
5668 #endif
5669 @end group
5670 @end example
5671 @end defmac
5673 @node Generic Headers
5674 @subsection Generic Header Checks
5676 These macros are used to find system header files not covered by the
5677 ``particular'' test macros.  If you need to check the contents of a header
5678 as well as find out whether it is present, you have to write your own
5679 test for it (@pxref{Writing Tests}).
5681 @anchor{AC_CHECK_HEADER}
5682 @defmac AC_CHECK_HEADER (@var{header-file}, @ovar{action-if-found}, @
5683   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5684 @acindex{CHECK_HEADER}
5685 If the system header file @var{header-file} is compilable, execute shell
5686 commands @var{action-if-found}, otherwise execute
5687 @var{action-if-not-found}.  If you just want to define a symbol if the
5688 header file is available, consider using @code{AC_CHECK_HEADERS}
5689 instead.
5691 For compatibility issues with older versions of Autoconf, please read
5692 below.
5693 @end defmac
5695 @anchor{AC_CHECK_HEADERS}
5696 @defmac AC_CHECK_HEADERS (@var{header-file}@dots{}, @
5697   @ovar{action-if-found}, @ovar{action-if-not-found}, @
5698   @dvar{includes, default-includes})
5699 @acindex{CHECK_HEADERS}
5700 @cvindex HAVE_@var{header}
5701 For each given system header file @var{header-file} in the
5702 blank-separated argument list that exists, define
5703 @code{HAVE_@var{header-file}} (in all capitals).  If @var{action-if-found}
5704 is given, it is additional shell code to execute when one of the header
5705 files is found.  You can give it a value of @samp{break} to break out of
5706 the loop on the first match.  If @var{action-if-not-found} is given, it
5707 is executed when one of the header files is not found.
5709 For compatibility issues with older versions of Autoconf, please read
5710 below.
5711 @end defmac
5713 Previous versions of Autoconf merely checked whether the header was
5714 accepted by the preprocessor.  This was changed because the old test was
5715 inappropriate for typical uses.  Headers are typically used to compile,
5716 not merely to preprocess, and the old behavior sometimes accepted
5717 headers that clashed at compile-time.  If you need to check whether a
5718 header is preprocessable, you can use @code{AC_PREPROC_IFELSE}
5719 (@pxref{Running the Preprocessor}).
5721 This scheme, which improves the robustness of the test, also requires
5722 that you make sure that headers that must be included before the
5723 @var{header-file} be part of the @var{includes}, (@pxref{Default
5724 Includes}).  If looking for @file{bar.h}, which requires that
5725 @file{foo.h} be included before if it exists, we suggest the following
5726 scheme:
5728 @verbatim
5729 AC_CHECK_HEADERS([foo.h])
5730 AC_CHECK_HEADERS([bar.h], [], [],
5731 [#ifdef HAVE_FOO_H
5732 # include <foo.h>
5733 # endif
5735 @end verbatim
5737 The following variant generates smaller, faster @command{configure}
5738 files if you do not need the full power of @code{AC_CHECK_HEADERS}.
5740 @defmac AC_CHECK_HEADERS_ONCE (@var{header-file}@dots{})
5741 @acindex{CHECK_HEADERS_ONCE}
5742 @cvindex HAVE_@var{header}
5743 For each given system header file @var{header-file} in the
5744 blank-separated argument list that exists, define
5745 @code{HAVE_@var{header-file}} (in all capitals).
5746 This is a once-only variant of @code{AC_CHECK_HEADERS}.  It generates the
5747 checking code at most once, so that @command{configure} is smaller and
5748 faster; but the checks cannot be conditionalized and are always done once,
5749 early during the @command{configure} run.
5750 @end defmac
5752 @node Declarations
5753 @section Declarations
5754 @cindex Declaration, checking
5756 The following macros check for the declaration of variables and
5757 functions.  If there is no macro specifically defined to check for a
5758 symbol you need, then you can use the general macros (@pxref{Generic
5759 Declarations}) or, for more complex tests, you may use
5760 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5762 @menu
5763 * Particular Declarations::     Macros to check for certain declarations
5764 * Generic Declarations::        How to find other declarations
5765 @end menu
5767 @node Particular Declarations
5768 @subsection Particular Declaration Checks
5770 There are no specific macros for declarations.
5772 @node Generic Declarations
5773 @subsection Generic Declaration Checks
5775 These macros are used to find declarations not covered by the ``particular''
5776 test macros.
5778 @defmac AC_CHECK_DECL (@var{symbol}, @ovar{action-if-found}, @
5779   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5780 @acindex{CHECK_DECL}
5781 If @var{symbol} (a function, variable, or constant) is not declared in
5782 @var{includes} and a declaration is needed, run the shell commands
5783 @var{action-if-not-found}, otherwise @var{action-if-found}.  If no
5784 @var{includes} are specified, the default includes are used
5785 (@pxref{Default Includes}).
5787 This macro actually tests whether @var{symbol} is defined as a macro or
5788 can be used as an r-value, not whether it is really declared, because it
5789 is much safer to avoid
5790 introducing extra declarations when they are not needed.
5791 @end defmac
5793 @anchor{AC_CHECK_DECLS}
5794 @defmac AC_CHECK_DECLS (@var{symbols}, @ovar{action-if-found}, @
5795   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5796 @acindex{CHECK_DECLS}
5797 @cvindex HAVE_DECL_@var{symbol}
5798 For each of the @var{symbols} (@emph{comma}-separated list), define
5799 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5800 @var{symbol} is declared, otherwise to @samp{0}.  If
5801 @var{action-if-not-found} is given, it is additional shell code to
5802 execute when one of the function declarations is needed, otherwise
5803 @var{action-if-found} is executed.
5805 This macro uses an M4 list as first argument:
5806 @example
5807 AC_CHECK_DECLS([strdup])
5808 AC_CHECK_DECLS([strlen])
5809 AC_CHECK_DECLS([malloc, realloc, calloc, free])
5810 @end example
5812 Unlike the other @samp{AC_CHECK_*S} macros, when a @var{symbol} is not
5813 declared, @code{HAVE_DECL_@var{symbol}} is defined to @samp{0} instead
5814 of leaving @code{HAVE_DECL_@var{symbol}} undeclared.  When you are
5815 @emph{sure} that the check was performed, use
5816 @code{HAVE_DECL_@var{symbol}} in @code{#if}:
5818 @example
5819 #if !HAVE_DECL_SYMBOL
5820 extern char *symbol;
5821 #endif
5822 @end example
5824 @noindent
5825 If the test may have not been performed, however, because it is safer
5826 @emph{not} to declare a symbol than to use a declaration that conflicts
5827 with the system's one, you should use:
5829 @example
5830 #if defined HAVE_DECL_MALLOC && !HAVE_DECL_MALLOC
5831 void *malloc (size_t *s);
5832 #endif
5833 @end example
5835 @noindent
5836 You fall into the second category only in extreme situations: either
5837 your files may be used without being configured, or they are used during
5838 the configuration.  In most cases the traditional approach is enough.
5839 @end defmac
5841 @defmac AC_CHECK_DECLS_ONCE (@var{symbols})
5842 @acindex{CHECK_DECLS_ONCE}
5843 @cvindex HAVE_DECL_@var{symbol}
5844 For each of the @var{symbols} (@emph{comma}-separated list), define
5845 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5846 @var{symbol} is declared in the default include files, otherwise to
5847 @samp{0}.  This is a once-only variant of @code{AC_CHECK_DECLS}.  It
5848 generates the checking code at most once, so that @command{configure} is
5849 smaller and faster; but the checks cannot be conditionalized and are
5850 always done once, early during the @command{configure} run.
5851 @end defmac
5854 @node Structures
5855 @section Structures
5856 @cindex Structure, checking
5858 The following macros check for the presence of certain members in C
5859 structures.  If there is no macro specifically defined to check for a
5860 member you need, then you can use the general structure-member macros
5861 (@pxref{Generic Structures}) or, for more complex tests, you may use
5862 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5864 @menu
5865 * Particular Structures::       Macros to check for certain structure members
5866 * Generic Structures::          How to find other structure members
5867 @end menu
5869 @node Particular Structures
5870 @subsection Particular Structure Checks
5872 The following macros check for certain structures or structure members.
5874 @defmac AC_STRUCT_DIRENT_D_INO
5875 @acindex{STRUCT_DIRENT_D_INO}
5876 @cvindex HAVE_STRUCT_DIRENT_D_INO
5877 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5878 Headers}).  Then, if @code{struct dirent} contains a @code{d_ino}
5879 member, define @code{HAVE_STRUCT_DIRENT_D_INO}.
5881 @code{HAVE_STRUCT_DIRENT_D_INO} indicates only the presence of
5882 @code{d_ino}, not whether its contents are always reliable.
5883 Traditionally, a zero @code{d_ino} indicated a deleted directory entry,
5884 though current systems hide this detail from the user and never return
5885 zero @code{d_ino} values.
5886 Many current systems report an incorrect @code{d_ino} for a directory
5887 entry that is a mount point.
5888 @end defmac
5890 @defmac AC_STRUCT_DIRENT_D_TYPE
5891 @acindex{STRUCT_DIRENT_D_TYPE}
5892 @cvindex HAVE_STRUCT_DIRENT_D_TYPE
5893 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5894 Headers}).  Then, if @code{struct dirent} contains a @code{d_type}
5895 member, define @code{HAVE_STRUCT_DIRENT_D_TYPE}.
5896 @end defmac
5898 @anchor{AC_STRUCT_ST_BLOCKS}
5899 @defmac AC_STRUCT_ST_BLOCKS
5900 @acindex{STRUCT_ST_BLOCKS}
5901 @cvindex HAVE_STRUCT_STAT_ST_BLOCKS
5902 @cvindex HAVE_ST_BLOCKS
5903 @ovindex LIBOBJS
5904 If @code{struct stat} contains an @code{st_blocks} member, define
5905 @code{HAVE_STRUCT_STAT_ST_BLOCKS}.  Otherwise, require an
5906 @code{AC_LIBOBJ} replacement of @samp{fileblocks}.  The former name,
5907 @code{HAVE_ST_BLOCKS} is to be avoided, as its support will cease in the
5908 future.
5909 @end defmac
5911 @defmac AC_STRUCT_TM
5912 @acindex{STRUCT_TM}
5913 @cvindex TM_IN_SYS_TIME
5914 @hdrindex{time.h}
5915 @hdrindex{sys/time.h}
5916 If @file{time.h} does not define @code{struct tm}, define
5917 @code{TM_IN_SYS_TIME}, which means that including @file{sys/time.h}
5918 had better define @code{struct tm}.
5920 This macro is obsolescent, as @file{time.h} defines @code{struct tm} in
5921 current systems.  New programs need not use this macro.
5922 @end defmac
5924 @anchor{AC_STRUCT_TIMEZONE}
5925 @defmac AC_STRUCT_TIMEZONE
5926 @acindex{STRUCT_TIMEZONE}
5927 @cvindex HAVE_DECL_TZNAME
5928 @cvindex HAVE_STRUCT_TM_TM_ZONE
5929 @cvindex HAVE_TM_ZONE
5930 @cvindex HAVE_TZNAME
5931 Figure out how to get the current timezone.  If @code{struct tm} has a
5932 @code{tm_zone} member, define @code{HAVE_STRUCT_TM_TM_ZONE} (and the
5933 obsoleted @code{HAVE_TM_ZONE}).  Otherwise, if the external array
5934 @code{tzname} is found, define @code{HAVE_TZNAME}; if it is declared,
5935 define @code{HAVE_DECL_TZNAME}.
5936 @end defmac
5938 @node Generic Structures
5939 @subsection Generic Structure Checks
5941 These macros are used to find structure members not covered by the
5942 ``particular'' test macros.
5944 @defmac AC_CHECK_MEMBER (@var{aggregate}.@var{member}, @
5945   @ovar{action-if-found}, @ovar{action-if-not-found}, @
5946   @dvar{includes, default-includes})
5947 @acindex{CHECK_MEMBER}
5948 Check whether @var{member} is a member of the aggregate @var{aggregate}.
5949 If no @var{includes} are specified, the default includes are used
5950 (@pxref{Default Includes}).
5952 @example
5953 AC_CHECK_MEMBER([struct passwd.pw_gecos], [],
5954                 [AC_MSG_ERROR([We need `passwd.pw_gecos'!])],
5955                 [#include <pwd.h>])
5956 @end example
5958 You can use this macro for submembers:
5960 @example
5961 AC_CHECK_MEMBER(struct top.middle.bot)
5962 @end example
5963 @end defmac
5965 @anchor{AC_CHECK_MEMBERS}
5966 @defmac AC_CHECK_MEMBERS (@var{members}, @ovar{action-if-found}, @
5967   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5968 @acindex{CHECK_MEMBERS}
5969 @cvindex HAVE_@var{aggregate}_@var{member}
5970 Check for the existence of each @samp{@var{aggregate}.@var{member}} of
5971 @var{members} using the previous macro.  When @var{member} belongs to
5972 @var{aggregate}, define @code{HAVE_@var{aggregate}_@var{member}} (in all
5973 capitals, with spaces and dots replaced by underscores).  If
5974 @var{action-if-found} is given, it is executed for each of the found
5975 members.  If @var{action-if-not-found} is given, it is executed for each
5976 of the members that could not be found.
5978 This macro uses M4 lists:
5979 @example
5980 AC_CHECK_MEMBERS([struct stat.st_rdev, struct stat.st_blksize])
5981 @end example
5982 @end defmac
5985 @node Types
5986 @section Types
5987 @cindex Types
5988 @cindex C types
5990 The following macros check for C types, either builtin or typedefs.  If
5991 there is no macro specifically defined to check for a type you need, and
5992 you don't need to check for any special properties of it, then you can
5993 use a general type-check macro.
5995 @menu
5996 * Particular Types::            Special handling to find certain types
5997 * Generic Types::               How to find other types
5998 @end menu
6000 @node Particular Types
6001 @subsection Particular Type Checks
6003 @hdrindex{sys/types.h}
6004 @hdrindex{stdlib.h}
6005 @hdrindex{stdint.h}
6006 @hdrindex{inttypes.h}
6007 These macros check for particular C types in @file{sys/types.h},
6008 @file{stdlib.h}, @file{stdint.h}, @file{inttypes.h} and others, if they
6009 exist.
6011 The Gnulib @code{stdint} module is an alternate way to define many of
6012 these symbols; it is useful if you prefer your code to assume a
6013 C99-or-better environment.  @xref{Gnulib}.
6015 @anchor{AC_TYPE_GETGROUPS}
6016 @defmac AC_TYPE_GETGROUPS
6017 @acindex{TYPE_GETGROUPS}
6018 @cvindex GETGROUPS_T
6019 Define @code{GETGROUPS_T} to be whichever of @code{gid_t} or @code{int}
6020 is the base type of the array argument to @code{getgroups}.
6021 @end defmac
6023 @defmac AC_TYPE_INT8_T
6024 @acindex{TYPE_INT8_T}
6025 @cvindex HAVE_INT8_T
6026 @cvindex int8_t
6027 If @file{stdint.h} or @file{inttypes.h} does not define the type
6028 @code{int8_t}, define @code{int8_t} to a signed
6029 integer type that is exactly 8 bits wide and that uses two's complement
6030 representation, if such a type exists.
6031 If you are worried about porting to hosts that lack such a type, you can
6032 use the results of this macro in C89-or-later code as follows:
6034 @example
6035 #if HAVE_STDINT_H
6036 # include <stdint.h>
6037 #endif
6038 #if defined INT8_MAX || defined int8_t
6039  @emph{code using int8_t}
6040 #else
6041  @emph{complicated alternative using >8-bit 'signed char'}
6042 #endif
6043 @end example
6044 @end defmac
6046 @defmac AC_TYPE_INT16_T
6047 @acindex{TYPE_INT16_T}
6048 @cvindex HAVE_INT16_T
6049 @cvindex int16_t
6050 This is like @code{AC_TYPE_INT8_T}, except for 16-bit integers.
6051 @end defmac
6053 @defmac AC_TYPE_INT32_T
6054 @acindex{TYPE_INT32_T}
6055 @cvindex HAVE_INT32_T
6056 @cvindex int32_t
6057 This is like @code{AC_TYPE_INT8_T}, except for 32-bit integers.
6058 @end defmac
6060 @defmac AC_TYPE_INT64_T
6061 @acindex{TYPE_INT64_T}
6062 @cvindex HAVE_INT64_T
6063 @cvindex int64_t
6064 This is like @code{AC_TYPE_INT8_T}, except for 64-bit integers.
6065 @end defmac
6067 @defmac AC_TYPE_INTMAX_T
6068 @acindex{TYPE_INTMAX_T}
6069 @cvindex HAVE_INTMAX_T
6070 @cvindex intmax_t
6071 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intmax_t},
6072 define @code{HAVE_INTMAX_T}.  Otherwise, define @code{intmax_t} to the
6073 widest signed integer type.
6074 @end defmac
6076 @defmac AC_TYPE_INTPTR_T
6077 @acindex{TYPE_INTPTR_T}
6078 @cvindex HAVE_INTPTR_T
6079 @cvindex intptr_t
6080 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intptr_t},
6081 define @code{HAVE_INTPTR_T}.  Otherwise, define @code{intptr_t} to a
6082 signed integer type wide enough to hold a pointer, if such a type
6083 exists.
6084 @end defmac
6086 @defmac AC_TYPE_LONG_DOUBLE
6087 @acindex{TYPE_LONG_DOUBLE}
6088 @cvindex HAVE_LONG_DOUBLE
6089 If the C compiler supports a working @code{long double} type, define
6090 @code{HAVE_LONG_DOUBLE}.  The @code{long double} type might have the
6091 same range and precision as @code{double}.
6093 This macro is obsolescent, as current C compilers support @code{long
6094 double}.  New programs need not use this macro.
6095 @end defmac
6097 @defmac AC_TYPE_LONG_DOUBLE_WIDER
6098 @acindex{TYPE_LONG_DOUBLE_WIDER}
6099 @cvindex HAVE_LONG_DOUBLE_WIDER
6100 If the C compiler supports a working @code{long double} type with more
6101 range or precision than the @code{double} type, define
6102 @code{HAVE_LONG_DOUBLE_WIDER}.
6103 @end defmac
6105 @defmac AC_TYPE_LONG_LONG_INT
6106 @acindex{TYPE_LONG_LONG_INT}
6107 @cvindex HAVE_LONG_LONG_INT
6108 If the C compiler supports a working @code{long long int} type, define
6109 @code{HAVE_LONG_LONG_INT}.
6110 @end defmac
6112 @defmac AC_TYPE_MBSTATE_T
6113 @acindex{TYPE_MBSTATE_T}
6114 @cvindex mbstate_t
6115 @hdrindex{wchar.h}
6116 Define @code{HAVE_MBSTATE_T} if @code{<wchar.h>} declares the
6117 @code{mbstate_t} type.  Also, define @code{mbstate_t} to be a type if
6118 @code{<wchar.h>} does not declare it.
6119 @end defmac
6121 @anchor{AC_TYPE_MODE_T}
6122 @defmac AC_TYPE_MODE_T
6123 @acindex{TYPE_MODE_T}
6124 @cvindex mode_t
6125 Define @code{mode_t} to a suitable type, if standard headers do not
6126 define it.
6127 @end defmac
6129 @anchor{AC_TYPE_OFF_T}
6130 @defmac AC_TYPE_OFF_T
6131 @acindex{TYPE_OFF_T}
6132 @cvindex off_t
6133 Define @code{off_t} to a suitable type, if standard headers do not
6134 define it.
6135 @end defmac
6137 @anchor{AC_TYPE_PID_T}
6138 @defmac AC_TYPE_PID_T
6139 @acindex{TYPE_PID_T}
6140 @cvindex pid_t
6141 Define @code{pid_t} to a suitable type, if standard headers do not
6142 define it.
6143 @end defmac
6145 @anchor{AC_TYPE_SIGNAL}
6146 @defmac AC_TYPE_SIGNAL
6147 @acindex{TYPE_SIGNAL}
6148 @cvindex RETSIGTYPE
6149 @hdrindex{signal.h}
6150 If @file{signal.h} declares @code{signal} as returning a pointer to a
6151 function returning @code{void}, define @code{RETSIGTYPE} to be
6152 @code{void}; otherwise, define it to be @code{int}.
6154 Define signal handlers as returning type @code{RETSIGTYPE}:
6156 @example
6157 @group
6158 RETSIGTYPE
6159 hup_handler ()
6161 @dots{}
6163 @end group
6164 @end example
6165 @end defmac
6167 @anchor{AC_TYPE_SIZE_T}
6168 @defmac AC_TYPE_SIZE_T
6169 @acindex{TYPE_SIZE_T}
6170 @cvindex size_t
6171 Define @code{size_t} to a suitable type, if standard headers do not
6172 define it.
6173 @end defmac
6175 @defmac AC_TYPE_SSIZE_T
6176 @acindex{TYPE_SSIZE_T}
6177 @cvindex ssize_t
6178 Define @code{ssize_t} to a suitable type, if standard headers do not
6179 define it.
6180 @end defmac
6182 @anchor{AC_TYPE_UID_T}
6183 @defmac AC_TYPE_UID_T
6184 @acindex{TYPE_UID_T}
6185 @cvindex uid_t
6186 @cvindex gid_t
6187 Define @code{uid_t} and @code{gid_t} to suitable types, if standard
6188 headers do not define them.
6189 @end defmac
6191 @defmac AC_TYPE_UINT8_T
6192 @acindex{TYPE_UINT8_T}
6193 @cvindex HAVE_UINT8_T
6194 @cvindex uint8_t
6195 If @file{stdint.h} or @file{inttypes.h} does not define the type
6196 @code{uint8_t}, define @code{uint8_t} to an
6197 unsigned integer type that is exactly 8 bits wide, if such a type
6198 exists.
6199 This is like @code{AC_TYPE_INT8_T}, except for unsigned integers.
6200 @end defmac
6202 @defmac AC_TYPE_UINT16_T
6203 @acindex{TYPE_UINT16_T}
6204 @cvindex HAVE_UINT16_T
6205 @cvindex uint16_t
6206 This is like @code{AC_TYPE_UINT8_T}, except for 16-bit integers.
6207 @end defmac
6209 @defmac AC_TYPE_UINT32_T
6210 @acindex{TYPE_UINT32_T}
6211 @cvindex HAVE_UINT32_T
6212 @cvindex uint32_t
6213 This is like @code{AC_TYPE_UINT8_T}, except for 32-bit integers.
6214 @end defmac
6216 @defmac AC_TYPE_UINT64_T
6217 @acindex{TYPE_UINT64_T}
6218 @cvindex HAVE_UINT64_T
6219 @cvindex uint64_t
6220 This is like @code{AC_TYPE_UINT8_T}, except for 64-bit integers.
6221 @end defmac
6223 @defmac AC_TYPE_UINTMAX_T
6224 @acindex{TYPE_UINTMAX_T}
6225 @cvindex HAVE_UINTMAX_T
6226 @cvindex uintmax_t
6227 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintmax_t},
6228 define @code{HAVE_UINTMAX_T}.  Otherwise, define @code{uintmax_t} to the
6229 widest unsigned integer type.
6230 @end defmac
6232 @defmac AC_TYPE_UINTPTR_T
6233 @acindex{TYPE_UINTPTR_T}
6234 @cvindex HAVE_UINTPTR_T
6235 @cvindex uintptr_t
6236 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintptr_t},
6237 define @code{HAVE_UINTPTR_T}.  Otherwise, define @code{uintptr_t} to an
6238 unsigned integer type wide enough to hold a pointer, if such a type
6239 exists.
6240 @end defmac
6242 @defmac AC_TYPE_UNSIGNED_LONG_LONG_INT
6243 @acindex{TYPE_UNSIGNED_LONG_LONG_INT}
6244 @cvindex HAVE_UNSIGNED_LONG_LONG_INT
6245 If the C compiler supports a working @code{unsigned long long int} type,
6246 define @code{HAVE_UNSIGNED_LONG_LONG_INT}.
6247 @end defmac
6249 @node Generic Types
6250 @subsection Generic Type Checks
6252 These macros are used to check for types not covered by the ``particular''
6253 test macros.
6255 @defmac AC_CHECK_TYPE (@var{type}, @ovar{action-if-found}, @
6256   @ovar{action-if-not-found}, @dvar{includes, default-includes})
6257 @acindex{CHECK_TYPE}
6258 Check whether @var{type} is defined.  It may be a compiler builtin type
6259 or defined by the @var{includes} (@pxref{Default Includes}).
6261 In C, @var{type} must be a type-name, so that the expression @samp{sizeof
6262 (@var{type})} is valid (but @samp{sizeof ((@var{type}))} is not).  The
6263 same test is applied when compiling for C++, which means that in C++
6264 @var{type} should be a type-id and should not be an anonymous
6265 @samp{struct} or @samp{union}.
6266 @end defmac
6269 @defmac AC_CHECK_TYPES (@var{types}, @ovar{action-if-found}, @
6270   @ovar{action-if-not-found}, @dvar{includes, default-includes})
6271 @acindex{CHECK_TYPES}
6272 @cvindex HAVE_@var{type}
6273 For each @var{type} of the @var{types} that is defined, define
6274 @code{HAVE_@var{type}} (in all capitals).  Each @var{type} must follow
6275 the rules of @code{AC_CHECK_TYPE}.  If no @var{includes} are
6276 specified, the default includes are used (@pxref{Default Includes}).  If
6277 @var{action-if-found} is given, it is additional shell code to execute
6278 when one of the types is found.  If @var{action-if-not-found} is given,
6279 it is executed when one of the types is not found.
6281 This macro uses M4 lists:
6282 @example
6283 AC_CHECK_TYPES([ptrdiff_t])
6284 AC_CHECK_TYPES([unsigned long long int, uintmax_t])
6285 @end example
6287 @end defmac
6289 Autoconf, up to 2.13, used to provide to another version of
6290 @code{AC_CHECK_TYPE}, broken by design.  In order to keep backward
6291 compatibility, a simple heuristic, quite safe but not totally, is
6292 implemented.  In case of doubt, read the documentation of the former
6293 @code{AC_CHECK_TYPE}, see @ref{Obsolete Macros}.
6296 @node Compilers and Preprocessors
6297 @section Compilers and Preprocessors
6298 @cindex Compilers
6299 @cindex Preprocessors
6301 @ovindex EXEEXT
6302 All the tests for compilers (@code{AC_PROG_CC}, @code{AC_PROG_CXX},
6303 @code{AC_PROG_F77}) define the output variable @code{EXEEXT} based on
6304 the output of the compiler, typically to the empty string if
6305 Posix and @samp{.exe} if a @acronym{DOS} variant.
6307 @ovindex OBJEXT
6308 They also define the output variable @code{OBJEXT} based on the
6309 output of the compiler, after @file{.c} files have been excluded, typically
6310 to @samp{o} if Posix, @samp{obj} if a @acronym{DOS} variant.
6312 If the compiler being used does not produce executables, the tests fail.  If
6313 the executables can't be run, and cross-compilation is not enabled, they
6314 fail too.  @xref{Manual Configuration}, for more on support for cross
6315 compiling.
6317 @menu
6318 * Specific Compiler Characteristics::  Some portability issues
6319 * Generic Compiler Characteristics::  Language independent tests and features
6320 * C Compiler::                  Checking its characteristics
6321 * C++ Compiler::                Likewise
6322 * Objective C Compiler::        Likewise
6323 * Erlang Compiler and Interpreter::  Likewise
6324 * Fortran Compiler::            Likewise
6325 @end menu
6327 @node Specific Compiler Characteristics
6328 @subsection Specific Compiler Characteristics
6330 Some compilers exhibit different behaviors.
6332 @table @asis
6333 @item Static/Dynamic Expressions
6334 Autoconf relies on a trick to extract one bit of information from the C
6335 compiler: using negative array sizes.  For instance the following
6336 excerpt of a C source demonstrates how to test whether @samp{int} objects are 4
6337 bytes wide:
6339 @example
6340 static int test_array[sizeof (int) == 4 ? 1 : -1];
6341 @end example
6343 @noindent
6344 To our knowledge, there is a single compiler that does not support this
6345 trick: the @acronym{HP} C compilers (the real ones, not only the
6346 ``bundled'') on @acronym{HP-UX} 11.00.
6347 They incorrectly reject the above program with the diagnostic
6348 ``Variable-length arrays cannot have static storage.''
6349 This bug comes from @acronym{HP} compilers' mishandling of @code{sizeof (int)},
6350 not from the @code{? 1 : -1}, and
6351 Autoconf works around this problem by casting @code{sizeof (int)} to
6352 @code{long int} before comparing it.
6353 @end table
6355 @node Generic Compiler Characteristics
6356 @subsection Generic Compiler Characteristics
6358 @anchor{AC_CHECK_SIZEOF}
6359 @defmac AC_CHECK_SIZEOF (@var{type-or-expr}, @ovar{unused}, @
6360   @dvar{includes, default-includes})
6361 @acindex{CHECK_SIZEOF}
6362 @cvindex SIZEOF_@var{type-or-expr}
6363 Define @code{SIZEOF_@var{type-or-expr}} (@pxref{Standard Symbols}) to be
6364 the size in bytes of @var{type-or-expr}, which may be either a type or
6365 an expression returning a value that has a size.  If the expression
6366 @samp{sizeof (@var{type-or-expr})} is invalid, the result is 0.  If no
6367 @var{includes} are specified, the default includes are used
6368 (@pxref{Default Includes}).
6370 This macro now works even when cross-compiling.  The @var{unused}
6371 argument was used when cross-compiling.
6373 For example, the call
6375 @example
6376 AC_CHECK_SIZEOF([int *])
6377 @end example
6379 @noindent
6380 defines @code{SIZEOF_INT_P} to be 8 on DEC Alpha AXP systems.
6381 @end defmac
6383 @defmac AC_CHECK_ALIGNOF (@var{type}, @dvar{includes, default-includes})
6384 @acindex{CHECK_ALIGNOF}
6385 @cvindex ALIGNOF_@var{type}
6386 Define @code{ALIGNOF_@var{type}} (@pxref{Standard Symbols}) to be the
6387 alignment in bytes of @var{type}.  @samp{@var{type} y;} must be valid as
6388 a structure member declaration.  If @samp{type} is unknown, the result
6389 is 0.  If no @var{includes} are specified, the default includes are used
6390 (@pxref{Default Includes}).
6391 @end defmac
6393 @defmac AC_COMPUTE_INT (@var{var}, @var{expression}, @
6394   @dvar{includes, default-includes}, @ovar{action-if-fails})
6395 @acindex{COMPUTE_INT}
6396 Store into the shell variable @var{var} the value of the integer
6397 @var{expression}.  The
6398 value should fit in an initializer in a C variable of type @code{signed
6399 long}.  To support cross compilation (in which case, the macro only works on
6400 hosts that use twos-complement arithmetic), it should be possible to evaluate
6401 the expression at compile-time.  If no @var{includes} are specified, the
6402 default includes are used (@pxref{Default Includes}).
6404 Execute @var{action-if-fails} if the value cannot be determined correctly.
6405 @end defmac
6407 @defmac AC_LANG_WERROR
6408 @acindex{LANG_WERROR}
6409 Normally Autoconf ignores warnings generated by the compiler, linker, and
6410 preprocessor.  If this macro is used, warnings count as fatal
6411 errors for the current language.  This macro is useful when the
6412 results of configuration are used where warnings are unacceptable; for
6413 instance, if parts of a program are built with the @acronym{GCC}
6414 @option{-Werror}
6415 option.  If the whole program is built using @option{-Werror} it is
6416 often simpler to put @option{-Werror} in the compiler flags (@code{CFLAGS},
6417 etc.).
6418 @end defmac
6420 @defmac AC_OPENMP
6421 @acindex{OPENMP}
6422 @cvindex _OPENMP
6423 @ovindex OPENMP_CFLAGS
6424 @ovindex OPENMP_CXXFLAGS
6425 @ovindex OPENMP_FFLAGS
6426 @ovindex OPENMP_FCFLAGS
6427 OpenMP (@url{http://www.openmp.org/}) specifies extensions of C, C++,
6428 and Fortran that simplify optimization of shared memory parallelism,
6429 which is a common problem on multicore CPUs.
6431 If the current language is C, the macro @code{AC_OPENMP} sets the
6432 variable @code{OPENMP_CFLAGS} to the C compiler flags needed for
6433 supporting OpenMP@.  @code{OPENMP_CFLAGS} is set to empty if the
6434 compiler already supports OpenMP, if it has no way to activate OpenMP
6435 support, or if the user rejects OpenMP support by invoking
6436 @samp{configure} with the @samp{--disable-openmp} option.
6438 @code{OPENMP_CFLAGS} needs to be used when compiling programs, when
6439 preprocessing program source, and when linking programs.  Therefore you
6440 need to add @code{$(OPENMP_CFLAGS)} to the @code{CFLAGS} of C programs
6441 that use OpenMP@.  If you preprocess OpenMP-specific C code, you also
6442 need to add @code{$(OPENMP_CFLAGS)} to @code{CPPFLAGS}.  The presence of
6443 OpenMP support is revealed at compile time by the preprocessor macro
6444 @code{_OPENMP}.
6446 Linking a program with @code{OPENMP_CFLAGS} typically adds one more
6447 shared library to the program's dependencies, so its use is recommended
6448 only on programs that actually require OpenMP.
6450 If the current language is C++, @code{AC_OPENMP} sets the variable
6451 @code{OPENMP_CXXFLAGS}, suitably for the C++ compiler.  The same remarks
6452 hold as for C.
6454 If the current language is Fortran 77 or Fortran, @code{AC_OPENMP} sets
6455 the variable @code{OPENMP_FFLAGS} or @code{OPENMP_FCFLAGS},
6456 respectively.  Similar remarks as for C hold, except that
6457 @code{CPPFLAGS} is not used for Fortran, and no preprocessor macro
6458 signals OpenMP support.
6459 @end defmac
6461 @node C Compiler
6462 @subsection C Compiler Characteristics
6464 The following macros provide ways to find and exercise a C Compiler.
6465 There are a few constructs that ought to be avoided, but do not deserve
6466 being checked for, since they can easily be worked around.
6468 @table @asis
6469 @item Don't use lines containing solitary backslashes
6470 They tickle a bug in the @acronym{HP-UX} C compiler (checked on
6471 @acronym{HP-UX} 10.20,
6472 11.00, and 11i).  When given the following source:
6474 @example
6475 #ifdef __STDC__
6477 * A comment with backslash-newlines in it.  %@{ %@} *\
6480 char str[] = "\\
6481 " A string with backslash-newlines in it %@{ %@} \\
6483 char apostrophe = '\\
6487 #endif
6488 @end example
6490 @noindent
6491 the compiler incorrectly fails with the diagnostics ``Non-terminating
6492 comment at end of file'' and ``Missing @samp{#endif} at end of file.''
6493 Removing the lines with solitary backslashes solves the problem.
6495 @item Don't compile several files at once if output matters to you
6496 Some compilers, such as @acronym{HP}'s, report names of files being
6497 compiled when given more than one file operand.  For instance:
6499 @example
6500 $ @kbd{cc a.c b.c}
6501 a.c:
6502 b.c:
6503 @end example
6505 @noindent
6506 This can cause problems if you observe the output of the compiler to
6507 detect failures.  Invoking @samp{cc -c a.c && cc -c b.c && cc -o c a.o
6508 b.o} solves the issue.
6510 @item Don't rely on @code{#error} failing
6511 The @sc{irix} C compiler does not fail when #error is preprocessed; it
6512 simply emits a diagnostic and continues, exiting successfully.  So,
6513 instead of an error directive like @code{#error "Unsupported word size"}
6514 it is more portable to use an invalid directive like @code{#Unsupported
6515 word size} in Autoconf tests.  In ordinary source code, @code{#error} is
6516 OK, since installers with inadequate compilers like @sc{irix} can simply
6517 examine these compilers' diagnostic output.
6519 @item Don't rely on correct @code{#line} support
6520 On Solaris, @command{c89} (at least Sun C 5.3 through 5.8)
6521 diagnoses @code{#line} directives whose line
6522 numbers are greater than 32767.  Nothing in Posix
6523 makes this invalid.  That is why Autoconf stopped issuing
6524 @code{#line} directives.
6525 @end table
6527 @defmac AC_PROG_CC (@ovar{compiler-search-list})
6528 @acindex{PROG_CC}
6529 @ovindex CC
6530 @ovindex CFLAGS
6531 Determine a C compiler to use.  If @code{CC} is not already set in the
6532 environment, check for @code{gcc} and @code{cc}, then for other C
6533 compilers.  Set output variable @code{CC} to the name of the compiler
6534 found.
6536 This macro may, however, be invoked with an optional first argument
6537 which, if specified, must be a blank-separated list of C compilers to
6538 search for.  This just gives the user an opportunity to specify an
6539 alternative search list for the C compiler.  For example, if you didn't
6540 like the default order, then you could invoke @code{AC_PROG_CC} like
6541 this:
6543 @example
6544 AC_PROG_CC([gcc cl cc])
6545 @end example
6547 If the C compiler does not handle function prototypes correctly by
6548 default, try to add an option to output variable @code{CC} to make it
6549 so.  This macro tries various options that select standard-conformance
6550 modes on various systems.
6552 After calling this macro you can check whether the C compiler has been
6553 set to accept @acronym{ANSI} C89 (@acronym{ISO} C90); if not, the shell
6554 variable
6555 @code{ac_cv_prog_cc_c89} is set to @samp{no}.  See also
6556 @code{AC_C_PROTOTYPES} below.
6558 If using the @acronym{GNU} C compiler, set shell variable @code{GCC} to
6559 @samp{yes}.  If output variable @code{CFLAGS} was not already set, set
6560 it to @option{-g -O2} for the @acronym{GNU} C compiler (@option{-O2} on systems
6561 where @acronym{GCC} does not accept @option{-g}), or @option{-g} for
6562 other compilers.
6563 @end defmac
6565 @anchor{AC_PROG_CC_C_O}
6566 @defmac AC_PROG_CC_C_O
6567 @acindex{PROG_CC_C_O}
6568 @cvindex NO_MINUS_C_MINUS_O
6569 If the C compiler does not accept the @option{-c} and @option{-o} options
6570 simultaneously, define @code{NO_MINUS_C_MINUS_O}.  This macro actually
6571 tests both the compiler found by @code{AC_PROG_CC}, and, if different,
6572 the first @code{cc} in the path.  The test fails if one fails.  This
6573 macro was created for @acronym{GNU} Make to choose the default C compilation
6574 rule.
6575 @end defmac
6578 @defmac AC_PROG_CPP
6579 @acindex{PROG_CPP}
6580 @ovindex CPP
6581 Set output variable @code{CPP} to a command that runs the
6582 C preprocessor.  If @samp{$CC -E} doesn't work, @file{/lib/cpp} is used.
6583 It is only portable to run @code{CPP} on files with a @file{.c}
6584 extension.
6586 Some preprocessors don't indicate missing include files by the error
6587 status.  For such preprocessors an internal variable is set that causes
6588 other macros to check the standard error from the preprocessor and
6589 consider the test failed if any warnings have been reported.
6590 For most preprocessors, though, warnings do not cause include-file
6591 tests to fail unless @code{AC_PROG_CPP_WERROR} is also specified.
6592 @end defmac
6594 @defmac AC_PROG_CPP_WERROR
6595 @acindex{PROG_CPP_WERROR}
6596 @ovindex CPP
6597 This acts like @code{AC_PROG_CPP}, except it treats warnings from the
6598 preprocessor as errors even if the preprocessor exit status indicates
6599 success.  This is useful for avoiding headers that generate mandatory
6600 warnings, such as deprecation notices.
6601 @end defmac
6604 The following macros check for C compiler or machine architecture
6605 features.  To check for characteristics not listed here, use
6606 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
6607 @code{AC_RUN_IFELSE} (@pxref{Runtime}).
6609 @defmac AC_PROG_CC_STDC
6610 @acindex{PROG_CC_STDC}
6611 If the C compiler cannot compile @acronym{ISO} Standard C (currently
6612 C99), try to add an option to output variable @code{CC} to make it work.
6613 If the compiler does not support C99, fall back to supporting
6614 @acronym{ANSI} C89 (@acronym{ISO} C90).
6616 After calling this macro you can check whether the C compiler has been
6617 set to accept Standard C; if not, the shell variable
6618 @code{ac_cv_prog_cc_stdc} is set to @samp{no}.
6619 @end defmac
6621 @defmac AC_PROG_CC_C89
6622 @acindex{PROG_CC_C89}
6623 If the C compiler is not in @acronym{ANSI} C89 (@acronym{ISO} C90) mode by
6624 default, try to add an option to output variable @code{CC} to make it
6625 so.  This macro tries various options that select @acronym{ANSI} C89 on
6626 some system or another.  It considers the compiler to be in
6627 @acronym{ANSI} C89 mode if it handles function prototypes correctly.
6629 After calling this macro you can check whether the C compiler has been
6630 set to accept @acronym{ANSI} C89; if not, the shell variable
6631 @code{ac_cv_prog_cc_c89} is set to @samp{no}.
6633 This macro is called automatically by @code{AC_PROG_CC}.
6634 @end defmac
6636 @defmac AC_PROG_CC_C99
6637 @acindex{PROG_CC_C99}
6638 If the C compiler is not in C99 mode by default, try to add an
6639 option to output variable @code{CC} to make it so.  This macro tries
6640 various options that select C99 on some system or another.  It
6641 considers the compiler to be in C99 mode if it handles @code{_Bool},
6642 @code{//} comments, flexible array members, @code{inline}, signed and
6643 unsigned @code{long long int}, mixed code and declarations, named
6644 initialization of structs,
6645 @code{restrict}, @code{va_copy}, varargs macros, variable declarations
6646 in @code{for} loops, and variable length arrays.
6648 After calling this macro you can check whether the C compiler has been
6649 set to accept C99; if not, the shell variable
6650 @code{ac_cv_prog_cc_c99} is set to @samp{no}.
6651 @end defmac
6653 @defmac AC_C_BACKSLASH_A
6654 @acindex{C_BACKSLASH_A}
6655 @cvindex HAVE_C_BACKSLASH_A
6656 Define @samp{HAVE_C_BACKSLASH_A} to 1 if the C compiler understands
6657 @samp{\a}.
6659 This macro is obsolescent, as current C compilers understand @samp{\a}.
6660 New programs need not use this macro.
6661 @end defmac
6663 @anchor{AC_C_BIGENDIAN}
6664 @defmac AC_C_BIGENDIAN (@ovar{action-if-true}, @ovar{action-if-false}, @
6665   @ovar{action-if-unknown}, @ovar{action-if-universal})
6666 @acindex{C_BIGENDIAN}
6667 @cvindex WORDS_BIGENDIAN
6668 @cindex Endianness
6669 If words are stored with the most significant byte first (like Motorola
6670 and SPARC CPUs), execute @var{action-if-true}.  If words are stored with
6671 the least significant byte first (like Intel and VAX CPUs), execute
6672 @var{action-if-false}.
6674 This macro runs a test-case if endianness cannot be determined from the
6675 system header files.  When cross-compiling, the test-case is not run but
6676 grep'ed for some magic values.  @var{action-if-unknown} is executed if
6677 the latter case fails to determine the byte sex of the host system.
6679 In some cases a single run of a compiler can generate code for multiple
6680 architectures.  This can happen, for example, when generating Mac OS X
6681 universal binary files, which work on both PowerPC and Intel
6682 architectures.  In this case, the different variants might be for
6683 different architectures whose endiannesses differ.  If
6684 @command{configure} detects this, it executes @var{action-if-universal}
6685 instead of @var{action-if-unknown}.
6687 The default for @var{action-if-true} is to define
6688 @samp{WORDS_BIGENDIAN}.  The default for @var{action-if-false} is to do
6689 nothing.  The default for @var{action-if-unknown} is to
6690 abort configure and tell the installer how to bypass this test.
6691 And finally, the default for @var{action-if-universal} is to define
6692 @samp{WORDS_BIGENDIAN} or not, depending on the architecture that the
6693 code is being generated for.
6695 If you use this macro without specifying @var{action-if-universal}, you
6696 should also use @code{AC_CONFIG_HEADERS}; otherwise
6697 @samp{WORDS_BIGENDIAN} may be set incorrectly for Mac OS X universal
6698 binary files.
6699 @end defmac
6701 @anchor{AC_C_CONST}
6702 @defmac AC_C_CONST
6703 @acindex{C_CONST}
6704 @cvindex const
6705 If the C compiler does not fully support the @code{const} keyword,
6706 define @code{const} to be empty.  Some C compilers that do
6707 not define @code{__STDC__} do support @code{const}; some compilers that
6708 define @code{__STDC__} do not completely support @code{const}.  Programs
6709 can simply use @code{const} as if every C compiler supported it; for
6710 those that don't, the makefile or configuration header file
6711 defines it as empty.
6713 Occasionally installers use a C++ compiler to compile C code, typically
6714 because they lack a C compiler.  This causes problems with @code{const},
6715 because C and C++ treat @code{const} differently.  For example:
6717 @example
6718 const int foo;
6719 @end example
6721 @noindent
6722 is valid in C but not in C++.  These differences unfortunately cannot be
6723 papered over by defining @code{const} to be empty.
6725 If @command{autoconf} detects this situation, it leaves @code{const} alone,
6726 as this generally yields better results in practice.  However, using a
6727 C++ compiler to compile C code is not recommended or supported, and
6728 installers who run into trouble in this area should get a C compiler
6729 like @acronym{GCC} to compile their C code.
6731 This macro is obsolescent, as current C compilers support @code{const}.
6732 New programs need not use this macro.
6733 @end defmac
6735 @defmac AC_C_RESTRICT
6736 @acindex{C_RESTRICT}
6737 @cvindex restrict
6738 If the C compiler recognizes a variant spelling for the @code{restrict}
6739 keyword (@code{__restrict}, @code{__restrict__}, or @code{_Restrict}),
6740 then define @code{restrict} to that; this is more likely to do the right
6741 thing with compilers that support language variants where plain
6742 @code{restrict} is not a keyword.  Otherwise, if the C compiler
6743 recognizes the @code{restrict} keyword, don't do anything.
6744 Otherwise, define @code{restrict} to be empty.
6745 Thus, programs may simply use @code{restrict} as if every C compiler
6746 supported it; for those that do not, the makefile
6747 or configuration header defines it away.
6749 Although support in C++ for the @code{restrict} keyword is not
6750 required, several C++ compilers do accept the keyword.
6751 This macro works for them, too.
6752 @end defmac
6754 @defmac AC_C_VOLATILE
6755 @acindex{C_VOLATILE}
6756 @cvindex volatile
6757 If the C compiler does not understand the keyword @code{volatile},
6758 define @code{volatile} to be empty.  Programs can simply use
6759 @code{volatile} as if every C compiler supported it; for those that do
6760 not, the makefile or configuration header defines it as
6761 empty.
6763 If the correctness of your program depends on the semantics of
6764 @code{volatile}, simply defining it to be empty does, in a sense, break
6765 your code.  However, given that the compiler does not support
6766 @code{volatile}, you are at its mercy anyway.  At least your
6767 program compiles, when it wouldn't before.
6768 @xref{Volatile Objects}, for more about @code{volatile}.
6770 In general, the @code{volatile} keyword is a standard C feature, so
6771 you might expect that @code{volatile} is available only when
6772 @code{__STDC__} is defined.  However, Ultrix 4.3's native compiler does
6773 support volatile, but does not define @code{__STDC__}.
6775 This macro is obsolescent, as current C compilers support @code{volatile}.
6776 New programs need not use this macro.
6777 @end defmac
6779 @anchor{AC_C_INLINE}
6780 @defmac AC_C_INLINE
6781 @acindex{C_INLINE}
6782 @cvindex inline
6783 If the C compiler supports the keyword @code{inline}, do nothing.
6784 Otherwise define @code{inline} to @code{__inline__} or @code{__inline}
6785 if it accepts one of those, otherwise define @code{inline} to be empty.
6786 @end defmac
6788 @anchor{AC_C_CHAR_UNSIGNED}
6789 @defmac AC_C_CHAR_UNSIGNED
6790 @acindex{C_CHAR_UNSIGNED}
6791 @cvindex __CHAR_UNSIGNED__
6792 If the C type @code{char} is unsigned, define @code{__CHAR_UNSIGNED__},
6793 unless the C compiler predefines it.
6794 @end defmac
6796 @defmac AC_C_STRINGIZE
6797 @acindex{C_STRINGIZE}
6798 @cvindex HAVE_STRINGIZE
6799 If the C preprocessor supports the stringizing operator, define
6800 @code{HAVE_STRINGIZE}.  The stringizing operator is @samp{#} and is
6801 found in macros such as this:
6803 @example
6804 #define x(y) #y
6805 @end example
6807 This macro is obsolescent, as current C compilers support the
6808 stringizing operator.  New programs need not use this macro.
6809 @end defmac
6811 @defmac AC_C_FLEXIBLE_ARRAY_MEMBER
6812 @acindex{C_FLEXIBLE_ARRAY_MEMBER}
6813 @cvindex FLEXIBLE_ARRAY_MEMBER
6814 If the C compiler supports flexible array members, define
6815 @code{FLEXIBLE_ARRAY_MEMBER} to nothing; otherwise define it to 1.
6816 That way, a declaration like this:
6818 @example
6819 struct s
6820   @{
6821     size_t n_vals;
6822     double val[FLEXIBLE_ARRAY_MEMBER];
6823   @};
6824 @end example
6826 @noindent
6827 will let applications use the ``struct hack'' even with compilers that
6828 do not support flexible array members.  To allocate and use such an
6829 object, you can use code like this:
6831 @example
6832 size_t i;
6833 size_t n = compute_value_count ();
6834 struct s *p =
6835    malloc (offsetof (struct s, val)
6836            + n * sizeof (double));
6837 p->n_vals = n;
6838 for (i = 0; i < n; i++)
6839   p->val[i] = compute_value (i);
6840 @end example
6841 @end defmac
6843 @defmac AC_C_VARARRAYS
6844 @acindex{C_VARARRAYS}
6845 @cvindex HAVE_C_VARARRAYS
6846 If the C compiler supports variable-length arrays, define
6847 @code{HAVE_C_VARARRAYS}.  A variable-length array is an array of automatic
6848 storage duration whose length is determined at run time, when the array
6849 is declared.
6850 @end defmac
6852 @defmac AC_C_TYPEOF
6853 @acindex{C_TYPEOF}
6854 @cvindex HAVE_TYPEOF
6855 @cvindex typeof
6856 If the C compiler supports @acronym{GCC}'s @code{typeof} syntax either
6857 directly or
6858 through a different spelling of the keyword (e.g., @code{__typeof__}),
6859 define @code{HAVE_TYPEOF}.  If the support is available only through a
6860 different spelling, define @code{typeof} to that spelling.
6861 @end defmac
6863 @defmac AC_C_PROTOTYPES
6864 @acindex{C_PROTOTYPES}
6865 @cvindex PROTOTYPES
6866 @cvindex __PROTOTYPES
6867 @cvindex PARAMS
6868 If function prototypes are understood by the compiler (as determined by
6869 @code{AC_PROG_CC}), define @code{PROTOTYPES} and @code{__PROTOTYPES}.
6870 Defining @code{__PROTOTYPES} is for the benefit of
6871 header files that cannot use macros that infringe on user name space.
6873 This macro is obsolescent, as current C compilers support prototypes.
6874 New programs need not use this macro.
6875 @end defmac
6877 @anchor{AC_PROG_GCC_TRADITIONAL}
6878 @defmac AC_PROG_GCC_TRADITIONAL
6879 @acindex{PROG_GCC_TRADITIONAL}
6880 @ovindex CC
6881 Add @option{-traditional} to output variable @code{CC} if using the
6882 @acronym{GNU} C compiler and @code{ioctl} does not work properly without
6883 @option{-traditional}.  That usually happens when the fixed header files
6884 have not been installed on an old system.
6886 This macro is obsolescent, since current versions of the @acronym{GNU} C
6887 compiler fix the header files automatically when installed.
6888 @end defmac
6891 @node C++ Compiler
6892 @subsection C++ Compiler Characteristics
6895 @defmac AC_PROG_CXX (@ovar{compiler-search-list})
6896 @acindex{PROG_CXX}
6897 @ovindex CXX
6898 @ovindex CXXFLAGS
6899 Determine a C++ compiler to use.  Check whether the environment variable
6900 @code{CXX} or @code{CCC} (in that order) is set; if so, then set output
6901 variable @code{CXX} to its value.
6903 Otherwise, if the macro is invoked without an argument, then search for
6904 a C++ compiler under the likely names (first @code{g++} and @code{c++}
6905 then other names).  If none of those checks succeed, then as a last
6906 resort set @code{CXX} to @code{g++}.
6908 This macro may, however, be invoked with an optional first argument
6909 which, if specified, must be a blank-separated list of C++ compilers to
6910 search for.  This just gives the user an opportunity to specify an
6911 alternative search list for the C++ compiler.  For example, if you
6912 didn't like the default order, then you could invoke @code{AC_PROG_CXX}
6913 like this:
6915 @example
6916 AC_PROG_CXX([gcc cl KCC CC cxx cc++ xlC aCC c++ g++])
6917 @end example
6919 If using the @acronym{GNU} C++ compiler, set shell variable @code{GXX} to
6920 @samp{yes}.  If output variable @code{CXXFLAGS} was not already set, set
6921 it to @option{-g -O2} for the @acronym{GNU} C++ compiler (@option{-O2} on
6922 systems where G++ does not accept @option{-g}), or @option{-g} for other
6923 compilers.
6924 @end defmac
6926 @defmac AC_PROG_CXXCPP
6927 @acindex{PROG_CXXCPP}
6928 @ovindex CXXCPP
6929 Set output variable @code{CXXCPP} to a command that runs the C++
6930 preprocessor.  If @samp{$CXX -E} doesn't work, @file{/lib/cpp} is used.
6931 It is portable to run @code{CXXCPP} only on files with a @file{.c},
6932 @file{.C}, @file{.cc}, or @file{.cpp} extension.
6934 Some preprocessors don't indicate missing include files by the error
6935 status.  For such preprocessors an internal variable is set that causes
6936 other macros to check the standard error from the preprocessor and
6937 consider the test failed if any warnings have been reported.  However,
6938 it is not known whether such broken preprocessors exist for C++.
6939 @end defmac
6941 @defmac AC_PROG_CXX_C_O
6942 @acindex{PROG_CXX_C_O}
6943 @cvindex CXX_NO_MINUS_C_MINUS_O
6944 Test whether the C++ compiler accepts the options @option{-c} and
6945 @option{-o} simultaneously, and define @code{CXX_NO_MINUS_C_MINUS_O},
6946 if it does not.
6947 @end defmac
6950 @node Objective C Compiler
6951 @subsection Objective C Compiler Characteristics
6954 @defmac AC_PROG_OBJC (@ovar{compiler-search-list})
6955 @acindex{PROG_OBJC}
6956 @ovindex OBJC
6957 @ovindex OBJCFLAGS
6958 Determine an Objective C compiler to use.  If @code{OBJC} is not already
6959 set in the environment, check for Objective C compilers.  Set output
6960 variable @code{OBJC} to the name of the compiler found.
6962 This macro may, however, be invoked with an optional first argument
6963 which, if specified, must be a blank-separated list of Objective C compilers to
6964 search for.  This just gives the user an opportunity to specify an
6965 alternative search list for the Objective C compiler.  For example, if you
6966 didn't like the default order, then you could invoke @code{AC_PROG_OBJC}
6967 like this:
6969 @example
6970 AC_PROG_OBJC([gcc objcc objc])
6971 @end example
6973 If using the @acronym{GNU} Objective C compiler, set shell variable
6974 @code{GOBJC} to @samp{yes}.  If output variable @code{OBJCFLAGS} was not
6975 already set, set it to @option{-g -O2} for the @acronym{GNU} Objective C
6976 compiler (@option{-O2} on systems where @command{gcc} does not accept
6977 @option{-g}), or @option{-g} for other compilers.
6978 @end defmac
6980 @defmac AC_PROG_OBJCPP
6981 @acindex{PROG_OBJCPP}
6982 @ovindex OBJCPP
6983 Set output variable @code{OBJCPP} to a command that runs the Objective C
6984 preprocessor.  If @samp{$OBJC -E} doesn't work, @file{/lib/cpp} is used.
6985 @end defmac
6988 @node Erlang Compiler and Interpreter
6989 @subsection Erlang Compiler and Interpreter Characteristics
6990 @cindex Erlang
6992 Autoconf defines the following macros for determining paths to the essential
6993 Erlang/OTP programs:
6995 @defmac AC_ERLANG_PATH_ERLC (@ovar{value-if-not-found}, @dvar{path, $PATH})
6996 @acindex{ERLANG_PATH_ERLC}
6997 @ovindex ERLC
6998 @ovindex ERLCFLAGS
6999 Determine an Erlang compiler to use.  If @code{ERLC} is not already set in the
7000 environment, check for @command{erlc}.  Set output variable @code{ERLC} to the
7001 complete path of the compiler command found.  In addition, if @code{ERLCFLAGS}
7002 is not set in the environment, set it to an empty value.
7004 The two optional arguments have the same meaning as the two last arguments of
7005 macro @code{AC_PROG_PATH} for looking for the @command{erlc} program.  For
7006 example, to look for @command{erlc} only in the @file{/usr/lib/erlang/bin}
7007 directory:
7009 @example
7010 AC_ERLANG_PATH_ERLC([not found], [/usr/lib/erlang/bin])
7011 @end example
7012 @end defmac
7014 @defmac AC_ERLANG_NEED_ERLC (@dvar{path, $PATH})
7015 @acindex{ERLANG_NEED_ERLC}
7016 A simplified variant of the @code{AC_ERLANG_PATH_ERLC} macro, that prints an
7017 error message and exits the @command{configure} script if the @command{erlc}
7018 program is not found.
7019 @end defmac
7021 @defmac AC_ERLANG_PATH_ERL (@ovar{value-if-not-found}, @dvar{path, $PATH})
7022 @acindex{ERLANG_PATH_ERL}
7023 @ovindex ERL
7024 Determine an Erlang interpreter to use.  If @code{ERL} is not already
7025 set in the
7026 environment, check for @command{erl}.  Set output variable @code{ERL} to the
7027 complete path of the interpreter command found.
7029 The two optional arguments have the same meaning as the two last arguments of
7030 macro @code{AC_PROG_PATH} for looking for the @command{erl} program.  For
7031 example, to look for @command{erl} only in the @file{/usr/lib/erlang/bin}
7032 directory:
7034 @example
7035 AC_ERLANG_PATH_ERL([not found], [/usr/lib/erlang/bin])
7036 @end example
7037 @end defmac
7039 @defmac AC_ERLANG_NEED_ERL (@dvar{path, $PATH})
7040 @acindex{ERLANG_NEED_ERL}
7041 A simplified variant of the @code{AC_ERLANG_PATH_ERL} macro, that prints an
7042 error message and exits the @command{configure} script if the @command{erl}
7043 program is not found.
7044 @end defmac
7047 @node Fortran Compiler
7048 @subsection Fortran Compiler Characteristics
7049 @cindex Fortran
7050 @cindex F77
7052 The Autoconf Fortran support is divided into two categories: legacy
7053 Fortran 77 macros (@code{F77}), and modern Fortran macros (@code{FC}).
7054 The former are intended for traditional Fortran 77 code, and have output
7055 variables like @code{F77}, @code{FFLAGS}, and @code{FLIBS}.  The latter
7056 are for newer programs that can (or must) compile under the newer
7057 Fortran standards, and have output variables like @code{FC},
7058 @code{FCFLAGS}, and @code{FCLIBS}.
7060 Except for two new macros @code{AC_FC_SRCEXT} and
7061 @code{AC_FC_FREEFORM} (see below), the @code{FC} and @code{F77} macros
7062 behave almost identically, and so they are documented together in this
7063 section.
7066 @defmac AC_PROG_F77 (@ovar{compiler-search-list})
7067 @acindex{PROG_F77}
7068 @ovindex F77
7069 @ovindex FFLAGS
7070 Determine a Fortran 77 compiler to use.  If @code{F77} is not already
7071 set in the environment, then check for @code{g77} and @code{f77}, and
7072 then some other names.  Set the output variable @code{F77} to the name
7073 of the compiler found.
7075 This macro may, however, be invoked with an optional first argument
7076 which, if specified, must be a blank-separated list of Fortran 77
7077 compilers to search for.  This just gives the user an opportunity to
7078 specify an alternative search list for the Fortran 77 compiler.  For
7079 example, if you didn't like the default order, then you could invoke
7080 @code{AC_PROG_F77} like this:
7082 @example
7083 AC_PROG_F77([fl32 f77 fort77 xlf g77 f90 xlf90])
7084 @end example
7086 If using @code{g77} (the @acronym{GNU} Fortran 77 compiler), then
7087 set the shell variable @code{G77} to @samp{yes}.
7088 If the output variable @code{FFLAGS} was not already set in the
7089 environment, then set it to @option{-g -02} for @code{g77} (or @option{-O2}
7090 where @code{g77} does not accept @option{-g}).  Otherwise, set
7091 @code{FFLAGS} to @option{-g} for all other Fortran 77 compilers.
7092 @end defmac
7094 @defmac AC_PROG_FC (@ovar{compiler-search-list}, @ovar{dialect})
7095 @acindex{PROG_FC}
7096 @ovindex FC
7097 @ovindex FCFLAGS
7098 Determine a Fortran compiler to use.  If @code{FC} is not already set in
7099 the environment, then @code{dialect} is a hint to indicate what Fortran
7100 dialect to search for; the default is to search for the newest available
7101 dialect.  Set the output variable @code{FC} to the name of the compiler
7102 found.
7104 By default, newer dialects are preferred over older dialects, but if
7105 @code{dialect} is specified then older dialects are preferred starting
7106 with the specified dialect.  @code{dialect} can currently be one of
7107 Fortran 77, Fortran 90, or Fortran 95.  However, this is only a hint of
7108 which compiler @emph{name} to prefer (e.g., @code{f90} or @code{f95}),
7109 and no attempt is made to guarantee that a particular language standard
7110 is actually supported.  Thus, it is preferable that you avoid the
7111 @code{dialect} option, and use AC_PROG_FC only for code compatible with
7112 the latest Fortran standard.
7114 This macro may, alternatively, be invoked with an optional first argument
7115 which, if specified, must be a blank-separated list of Fortran
7116 compilers to search for, just as in @code{AC_PROG_F77}.
7118 If the output variable @code{FCFLAGS} was not already set in the
7119 environment, then set it to @option{-g -02} for @acronym{GNU} @code{g77} (or
7120 @option{-O2} where @code{g77} does not accept @option{-g}).  Otherwise,
7121 set @code{FCFLAGS} to @option{-g} for all other Fortran compilers.
7122 @end defmac
7124 @defmac AC_PROG_F77_C_O
7125 @defmacx AC_PROG_FC_C_O
7126 @acindex{PROG_F77_C_O}
7127 @acindex{PROG_FC_C_O}
7128 @cvindex F77_NO_MINUS_C_MINUS_O
7129 @cvindex FC_NO_MINUS_C_MINUS_O
7130 Test whether the Fortran compiler accepts the options @option{-c} and
7131 @option{-o} simultaneously, and define @code{F77_NO_MINUS_C_MINUS_O} or
7132 @code{FC_NO_MINUS_C_MINUS_O}, respectively, if it does not.
7133 @end defmac
7135 The following macros check for Fortran compiler characteristics.
7136 To check for characteristics not listed here, use
7137 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
7138 @code{AC_RUN_IFELSE} (@pxref{Runtime}), making sure to first set the
7139 current language to Fortran 77 or Fortran via @code{AC_LANG([Fortran 77])}
7140 or @code{AC_LANG(Fortran)} (@pxref{Language Choice}).
7143 @defmac AC_F77_LIBRARY_LDFLAGS
7144 @defmacx AC_FC_LIBRARY_LDFLAGS
7145 @acindex{F77_LIBRARY_LDFLAGS}
7146 @ovindex FLIBS
7147 @acindex{FC_LIBRARY_LDFLAGS}
7148 @ovindex FCLIBS
7149 Determine the linker flags (e.g., @option{-L} and @option{-l}) for the
7150 @dfn{Fortran intrinsic and runtime libraries} that are required to
7151 successfully link a Fortran program or shared library.  The output
7152 variable @code{FLIBS} or @code{FCLIBS} is set to these flags (which
7153 should be included after @code{LIBS} when linking).
7155 This macro is intended to be used in those situations when it is
7156 necessary to mix, e.g., C++ and Fortran source code in a single
7157 program or shared library (@pxref{Mixing Fortran 77 With C and C++, , ,
7158 automake, @acronym{GNU} Automake}).
7160 For example, if object files from a C++ and Fortran compiler must be
7161 linked together, then the C++ compiler/linker must be used for linking
7162 (since special C++-ish things need to happen at link time like calling
7163 global constructors, instantiating templates, enabling exception
7164 support, etc.).
7166 However, the Fortran intrinsic and runtime libraries must be linked in
7167 as well, but the C++ compiler/linker doesn't know by default how to add
7168 these Fortran 77 libraries.  Hence, this macro was created to determine
7169 these Fortran libraries.
7171 The macros @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
7172 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} are probably also necessary to
7173 link C/C++ with Fortran; see below.
7174 @end defmac
7176 @defmac AC_F77_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
7177 @defmacx AC_FC_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
7178 @acindex{F77_DUMMY_MAIN}
7179 @cvindex F77_DUMMY_MAIN
7180 With many compilers, the Fortran libraries detected by
7181 @code{AC_F77_LIBRARY_LDFLAGS} or @code{AC_FC_LIBRARY_LDFLAGS} provide
7182 their own @code{main} entry function that initializes things like
7183 Fortran I/O, and which then calls a user-provided entry function named
7184 (say) @code{MAIN__} to run the user's program.  The
7185 @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
7186 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros figure out how to deal with
7187 this interaction.
7189 When using Fortran for purely numerical functions (no I/O, etc.)@: often
7190 one prefers to provide one's own @code{main} and skip the Fortran
7191 library initializations.  In this case, however, one may still need to
7192 provide a dummy @code{MAIN__} routine in order to prevent linking errors
7193 on some systems.  @code{AC_F77_DUMMY_MAIN} or @code{AC_FC_DUMMY_MAIN}
7194 detects whether any such routine is @emph{required} for linking, and
7195 what its name is; the shell variable @code{F77_DUMMY_MAIN} or
7196 @code{FC_DUMMY_MAIN} holds this name, @code{unknown} when no solution
7197 was found, and @code{none} when no such dummy main is needed.
7199 By default, @var{action-if-found} defines @code{F77_DUMMY_MAIN} or
7200 @code{FC_DUMMY_MAIN} to the name of this routine (e.g., @code{MAIN__})
7201 @emph{if} it is required.  @var{action-if-not-found} defaults to
7202 exiting with an error.
7204 In order to link with Fortran routines, the user's C/C++ program should
7205 then include the following code to define the dummy main if it is
7206 needed:
7208 @example
7209 #ifdef F77_DUMMY_MAIN
7210 #  ifdef __cplusplus
7211      extern "C"
7212 #  endif
7213    int F77_DUMMY_MAIN() @{ return 1; @}
7214 #endif
7215 @end example
7217 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7219 Note that this macro is called automatically from @code{AC_F77_WRAPPERS}
7220 or @code{AC_FC_WRAPPERS}; there is generally no need to call it
7221 explicitly unless one wants to change the default actions.
7222 @end defmac
7224 @defmac AC_F77_MAIN
7225 @defmacx AC_FC_MAIN
7226 @acindex{F77_MAIN}
7227 @cvindex F77_MAIN
7228 @acindex{FC_MAIN}
7229 @cvindex FC_MAIN
7230 As discussed above, many Fortran libraries allow you to provide an entry
7231 point called (say) @code{MAIN__} instead of the usual @code{main}, which
7232 is then called by a @code{main} function in the Fortran libraries that
7233 initializes things like Fortran I/O@.  The
7234 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros detect whether it is
7235 @emph{possible} to utilize such an alternate main function, and defines
7236 @code{F77_MAIN} and @code{FC_MAIN} to the name of the function.  (If no
7237 alternate main function name is found, @code{F77_MAIN} and @code{FC_MAIN} are
7238 simply defined to @code{main}.)
7240 Thus, when calling Fortran routines from C that perform things like I/O,
7241 one should use this macro and declare the "main" function like so:
7243 @example
7244 #ifdef __cplusplus
7245   extern "C"
7246 #endif
7247 int F77_MAIN(int argc, char *argv[]);
7248 @end example
7250 (Again, replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7251 @end defmac
7253 @defmac AC_F77_WRAPPERS
7254 @defmacx AC_FC_WRAPPERS
7255 @acindex{F77_WRAPPERS}
7256 @cvindex F77_FUNC
7257 @cvindex F77_FUNC_
7258 @acindex{FC_WRAPPERS}
7259 @cvindex FC_FUNC
7260 @cvindex FC_FUNC_
7261 Defines C macros @code{F77_FUNC (name, NAME)}, @code{FC_FUNC (name, NAME)},
7262 @code{F77_FUNC_(name, NAME)}, and @code{FC_FUNC_(name, NAME)} to properly
7263 mangle the names of C/C++ identifiers, and identifiers with underscores,
7264 respectively, so that they match the name-mangling scheme used by the
7265 Fortran compiler.
7267 Fortran is case-insensitive, and in order to achieve this the Fortran
7268 compiler converts all identifiers into a canonical case and format.  To
7269 call a Fortran subroutine from C or to write a C function that is
7270 callable from Fortran, the C program must explicitly use identifiers in
7271 the format expected by the Fortran compiler.  In order to do this, one
7272 simply wraps all C identifiers in one of the macros provided by
7273 @code{AC_F77_WRAPPERS} or @code{AC_FC_WRAPPERS}.  For example, suppose
7274 you have the following Fortran 77 subroutine:
7276 @example
7277       subroutine foobar (x, y)
7278       double precision x, y
7279       y = 3.14159 * x
7280       return
7281       end
7282 @end example
7284 You would then declare its prototype in C or C++ as:
7286 @example
7287 #define FOOBAR_F77 F77_FUNC (foobar, FOOBAR)
7288 #ifdef __cplusplus
7289 extern "C"  /* prevent C++ name mangling */
7290 #endif
7291 void FOOBAR_F77(double *x, double *y);
7292 @end example
7294 Note that we pass both the lowercase and uppercase versions of the
7295 function name to @code{F77_FUNC} so that it can select the right one.
7296 Note also that all parameters to Fortran 77 routines are passed as
7297 pointers (@pxref{Mixing Fortran 77 With C and C++, , , automake, @acronym{GNU}
7298 Automake}).
7300 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7302 Although Autoconf tries to be intelligent about detecting the
7303 name-mangling scheme of the Fortran compiler, there may be Fortran
7304 compilers that it doesn't support yet.  In this case, the above code
7305 generates a compile-time error, but some other behavior
7306 (e.g., disabling Fortran-related features) can be induced by checking
7307 whether @code{F77_FUNC} or @code{FC_FUNC} is defined.
7309 Now, to call that routine from a C program, we would do something like:
7311 @example
7313     double x = 2.7183, y;
7314     FOOBAR_F77 (&x, &y);
7316 @end example
7318 If the Fortran identifier contains an underscore (e.g., @code{foo_bar}),
7319 you should use @code{F77_FUNC_} or @code{FC_FUNC_} instead of
7320 @code{F77_FUNC} or @code{FC_FUNC} (with the same arguments).  This is
7321 because some Fortran compilers mangle names differently if they contain
7322 an underscore.
7323 @end defmac
7325 @defmac AC_F77_FUNC (@var{name}, @ovar{shellvar})
7326 @defmacx AC_FC_FUNC (@var{name}, @ovar{shellvar})
7327 @acindex{F77_FUNC}
7328 @acindex{FC_FUNC}
7329 Given an identifier @var{name}, set the shell variable @var{shellvar} to
7330 hold the mangled version @var{name} according to the rules of the
7331 Fortran linker (see also @code{AC_F77_WRAPPERS} or
7332 @code{AC_FC_WRAPPERS}).  @var{shellvar} is optional; if it is not
7333 supplied, the shell variable is simply @var{name}.  The purpose of
7334 this macro is to give the caller a way to access the name-mangling
7335 information other than through the C preprocessor as above, for example,
7336 to call Fortran routines from some language other than C/C++.
7337 @end defmac
7339 @defmac AC_FC_SRCEXT (@var{ext}, @ovar{action-if-success}, @
7340   @ovar{action-if-failure})
7341 @acindex{FC_SRCEXT}
7342 By default, the @code{FC} macros perform their tests using a @file{.f}
7343 extension for source-code files.  Some compilers, however, only enable
7344 newer language features for appropriately named files, e.g., Fortran 90
7345 features only for @file{.f90} files.  On the other hand, some other
7346 compilers expect all source files to end in @file{.f} and require
7347 special flags to support other file name extensions.  The
7348 @code{AC_FC_SRCEXT} macro deals with both of these issues.
7350 The @code{AC_FC_SRCEXT} tries to get the @code{FC} compiler to accept files
7351 ending with the extension .@var{ext} (i.e., @var{ext} does @emph{not}
7352 contain the dot).  If any special compiler flags are needed for this, it
7353 stores them in the output variable @code{FCFLAGS_}@var{ext}.  This
7354 extension and these flags are then used for all subsequent @code{FC} tests
7355 (until @code{AC_FC_SRCEXT} is called again).
7357 For example, you would use @code{AC_FC_SRCEXT(f90)} to employ the
7358 @file{.f90} extension in future tests, and it would set a
7359 @code{FCFLAGS_f90} output variable with any extra flags that are needed
7360 to compile such files.
7362 The @code{FCFLAGS_}@var{ext} can @emph{not} be simply absorbed into
7363 @code{FCFLAGS}, for two reasons based on the limitations of some
7364 compilers.  First, only one @code{FCFLAGS_}@var{ext} can be used at a
7365 time, so files with different extensions must be compiled separately.
7366 Second, @code{FCFLAGS_}@var{ext} must appear @emph{immediately} before
7367 the source-code file name when compiling.  So, continuing the example
7368 above, you might compile a @file{foo.f90} file in your makefile with the
7369 command:
7371 @example
7372 foo.o: foo.f90
7373      $(FC) -c $(FCFLAGS) $(FCFLAGS_f90) '$(srcdir)/foo.f90'
7374 @end example
7376 If @code{AC_FC_SRCEXT} succeeds in compiling files with the @var{ext}
7377 extension, it calls @var{action-if-success} (defaults to nothing).  If
7378 it fails, and cannot find a way to make the @code{FC} compiler accept such
7379 files, it calls @var{action-if-failure} (defaults to exiting with an
7380 error message).
7382 @end defmac
7384 @defmac AC_FC_FREEFORM (@ovar{action-if-success}, @ovar{action-if-failure})
7385 @acindex{FC_FREEFORM}
7387 The @code{AC_FC_FREEFORM} tries to ensure that the Fortran compiler
7388 (@code{$FC}) allows free-format source code (as opposed to the older
7389 fixed-format style from Fortran 77).  If necessary, it may add some
7390 additional flags to @code{FCFLAGS}.
7392 This macro is most important if you are using the default @file{.f}
7393 extension, since many compilers interpret this extension as indicating
7394 fixed-format source unless an additional flag is supplied.  If you
7395 specify a different extension with @code{AC_FC_SRCEXT}, such as
7396 @file{.f90} or @file{.f95}, then @code{AC_FC_FREEFORM} ordinarily
7397 succeeds without modifying @code{FCFLAGS}.
7399 If @code{AC_FC_FREEFORM} succeeds in compiling free-form source, it
7400 calls @var{action-if-success} (defaults to nothing).  If it fails, it
7401 calls @var{action-if-failure} (defaults to exiting with an error
7402 message).
7403 @end defmac
7405 @node System Services
7406 @section System Services
7408 The following macros check for operating system services or capabilities.
7410 @anchor{AC_PATH_X}
7411 @defmac AC_PATH_X
7412 @acindex{PATH_X}
7413 @evindex XMKMF
7414 @cindex X Window System
7415 Try to locate the X Window System include files and libraries.  If the
7416 user gave the command line options @option{--x-includes=@var{dir}} and
7417 @option{--x-libraries=@var{dir}}, use those directories.
7419 If either or both were not given, get the missing values by running
7420 @code{xmkmf} (or an executable pointed to by the @code{XMKMF}
7421 environment variable) on a trivial @file{Imakefile} and examining the
7422 makefile that it produces.  Setting @code{XMKMF} to @samp{false}
7423 disables this method.
7425 If this method fails to find the X Window System, @command{configure}
7426 looks for the files in several directories where they often reside.
7427 If either method is successful, set the shell variables
7428 @code{x_includes} and @code{x_libraries} to their locations, unless they
7429 are in directories the compiler searches by default.
7431 If both methods fail, or the user gave the command line option
7432 @option{--without-x}, set the shell variable @code{no_x} to @samp{yes};
7433 otherwise set it to the empty string.
7434 @end defmac
7436 @anchor{AC_PATH_XTRA}
7437 @defmac AC_PATH_XTRA
7438 @acindex{PATH_XTRA}
7439 @ovindex X_CFLAGS
7440 @ovindex X_LIBS
7441 @ovindex X_EXTRA_LIBS
7442 @ovindex X_PRE_LIBS
7443 @cvindex X_DISPLAY_MISSING
7444 An enhanced version of @code{AC_PATH_X}.  It adds the C compiler flags
7445 that X needs to output variable @code{X_CFLAGS}, and the X linker flags
7446 to @code{X_LIBS}.  Define @code{X_DISPLAY_MISSING} if X is not
7447 available.
7449 This macro also checks for special libraries that some systems need in
7450 order to compile X programs.  It adds any that the system needs to
7451 output variable @code{X_EXTRA_LIBS}.  And it checks for special X11R6
7452 libraries that need to be linked with before @option{-lX11}, and adds
7453 any found to the output variable @code{X_PRE_LIBS}.
7455 @c This is an incomplete kludge.  Make a real way to do it.
7456 @c If you need to check for other X functions or libraries yourself, then
7457 @c after calling this macro, add the contents of @code{X_EXTRA_LIBS} to
7458 @c @code{LIBS} temporarily, like this: (FIXME - add example)
7459 @end defmac
7461 @anchor{AC_SYS_INTERPRETER}
7462 @defmac AC_SYS_INTERPRETER
7463 @acindex{SYS_INTERPRETER}
7464 Check whether the system supports starting scripts with a line of the
7465 form @samp{#!/bin/sh} to select the interpreter to use for the script.
7466 After running this macro, shell code in @file{configure.ac} can check
7467 the shell variable @code{interpval}; it is set to @samp{yes}
7468 if the system supports @samp{#!}, @samp{no} if not.
7469 @end defmac
7471 @defmac AC_SYS_LARGEFILE
7472 @acindex{SYS_LARGEFILE}
7473 @cvindex _FILE_OFFSET_BITS
7474 @cvindex _LARGE_FILES
7475 @ovindex CC
7476 @cindex Large file support
7477 @cindex LFS
7478 Arrange for
7479 @uref{http://www.unix-systems.org/@/version2/@/whatsnew/@/lfs20mar.html,
7480 large-file support}.  On some hosts, one must use special compiler
7481 options to build programs that can access large files.  Append any such
7482 options to the output variable @code{CC}.  Define
7483 @code{_FILE_OFFSET_BITS} and @code{_LARGE_FILES} if necessary.
7485 Large-file support can be disabled by configuring with the
7486 @option{--disable-largefile} option.
7488 If you use this macro, check that your program works even when
7489 @code{off_t} is wider than @code{long int}, since this is common when
7490 large-file support is enabled.  For example, it is not correct to print
7491 an arbitrary @code{off_t} value @code{X} with @code{printf ("%ld",
7492 (long int) X)}.
7494 The LFS introduced the @code{fseeko} and @code{ftello} functions to
7495 replace their C counterparts @code{fseek} and @code{ftell} that do not
7496 use @code{off_t}.  Take care to use @code{AC_FUNC_FSEEKO} to make their
7497 prototypes available when using them and large-file support is
7498 enabled.
7499 @end defmac
7501 @anchor{AC_SYS_LONG_FILE_NAMES}
7502 @defmac AC_SYS_LONG_FILE_NAMES
7503 @acindex{SYS_LONG_FILE_NAMES}
7504 @cvindex HAVE_LONG_FILE_NAMES
7505 If the system supports file names longer than 14 characters, define
7506 @code{HAVE_LONG_FILE_NAMES}.
7507 @end defmac
7509 @defmac AC_SYS_POSIX_TERMIOS
7510 @acindex{SYS_POSIX_TERMIOS}
7511 @cindex Posix termios headers
7512 @cindex termios Posix headers
7513 Check to see if the Posix termios headers and functions are available on the
7514 system.  If so, set the shell variable @code{ac_cv_sys_posix_termios} to
7515 @samp{yes}.  If not, set the variable to @samp{no}.
7516 @end defmac
7518 @node Posix Variants
7519 @section Posix Variants
7521 The following macro makes it possible to use features of Posix that are
7522 extensions to C, as well as platform extensions not defined by Posix.
7524 @anchor{AC_USE_SYSTEM_EXTENSIONS}
7525 @defmac AC_USE_SYSTEM_EXTENSIONS
7526 @acindex{USE_SYSTEM_EXTENSIONS}
7527 @cvindex _ALL_SOURCE
7528 @cvindex _GNU_SOURCE
7529 @cvindex _MINIX
7530 @cvindex _POSIX_1_SOURCE
7531 @cvindex _POSIX_PTHREAD_SEMANTICS
7532 @cvindex _POSIX_SOURCE
7533 @cvindex _TANDEM_SOURCE
7534 @cvindex __EXTENSIONS__
7535 This macro was introduced in Autoconf 2.60.  If possible, enable
7536 extensions to C or Posix on hosts that normally disable the extensions,
7537 typically due to standards-conformance namespace issues.  This should be
7538 called before any macros that run the C compiler.  The following
7539 preprocessor macros are defined where appropriate:
7541 @table @code
7542 @item _GNU_SOURCE
7543 Enable extensions on @acronym{GNU}/Linux.
7544 @item __EXTENSIONS__
7545 Enable general extensions on Solaris.
7546 @item _POSIX_PTHREAD_SEMANTICS
7547 Enable threading extensions on Solaris.
7548 @item _TANDEM_SOURCE
7549 Enable extensions for the @acronym{HP} NonStop platform.
7550 @item _ALL_SOURCE
7551 Enable extensions for @acronym{AIX} 3, and for Interix.
7552 @item _POSIX_SOURCE
7553 Enable Posix functions for Minix.
7554 @item _POSIX_1_SOURCE
7555 Enable additional Posix functions for Minix.
7556 @item _MINIX
7557 Identify Minix platform.  This particular preprocessor macro is
7558 obsolescent, and may be removed in a future release of Autoconf.
7559 @end table
7560 @end defmac
7563 @node Erlang Libraries
7564 @section Erlang Libraries
7565 @cindex Erlang, Library, checking
7567 The following macros check for an installation of Erlang/OTP, and for the
7568 presence of certain Erlang libraries.  All those macros require the
7569 configuration of an Erlang interpreter and an Erlang compiler
7570 (@pxref{Erlang Compiler and Interpreter}).
7572 @defmac AC_ERLANG_SUBST_ROOT_DIR
7573 @acindex{ERLANG_SUBST_ROOT_DIR}
7574 @ovindex ERLANG_ROOT_DIR
7576 Set the output variable @code{ERLANG_ROOT_DIR} to the path to the base
7577 directory in which Erlang/OTP is installed (as returned by Erlang's
7578 @code{code:root_dir/0} function).  The result of this test is cached if
7579 caching is enabled when running @command{configure}.
7580 @end defmac
7582 @defmac AC_ERLANG_SUBST_LIB_DIR
7583 @acindex{ERLANG_SUBST_LIB_DIR}
7584 @ovindex ERLANG_LIB_DIR
7586 Set the output variable @code{ERLANG_LIB_DIR} to the path of the library
7587 directory of Erlang/OTP (as returned by Erlang's
7588 @code{code:lib_dir/0} function), which subdirectories each contain an installed
7589 Erlang/OTP library.  The result of this test is cached if caching is enabled
7590 when running @command{configure}.
7591 @end defmac
7593 @defmac AC_ERLANG_CHECK_LIB (@var{library}, @ovar{action-if-found}, @
7594   @ovar{action-if-not-found})
7595 @acindex{ERLANG_CHECK_LIB}
7596 @ovindex ERLANG_LIB_DIR_@var{library}
7597 @ovindex ERLANG_LIB_VER_@var{library}
7599 Test whether the Erlang/OTP library @var{library} is installed by
7600 calling Erlang's @code{code:lib_dir/1} function.  The result of this
7601 test is cached if caching is enabled when running @command{configure}.
7602 @var{action-if-found} is a list of shell commands to run if the library
7603 is installed; @var{action-if-not-found} is a list of shell commands to
7604 run if it is not.  Additionally, if the library is installed, the output
7605 variable @samp{ERLANG_LIB_DIR_@var{library}} is set to the path to the
7606 library installation directory, and the output variable
7607 @samp{ERLANG_LIB_VER_@var{library}} is set to the version number that is
7608 part of the subdirectory name, if it is in the standard form
7609 (@code{@var{library}-@var{version}}).  If the directory name does not
7610 have a version part, @samp{ERLANG_LIB_VER_@var{library}} is set to the
7611 empty string.  If the library is not installed,
7612 @samp{ERLANG_LIB_DIR_@var{library}} and
7613 @samp{ERLANG_LIB_VER_@var{library}} are set to @code{"not found"}.  For
7614 example, to check if library @code{stdlib} is installed:
7616 @example
7617 AC_ERLANG_CHECK_LIB([stdlib],
7618   [echo "stdlib version \"$ERLANG_LIB_VER_stdlib\""
7619    echo "is installed in \"$ERLANG_LIB_DIR_stdlib\""],
7620   [AC_MSG_ERROR([stdlib was not found!])])
7621 @end example
7622 @end defmac
7624 In addition to the above macros, which test installed Erlang libraries, the
7625 following macros determine the paths to the directories into which newly built
7626 Erlang libraries are to be installed:
7628 @defmac AC_ERLANG_SUBST_INSTALL_LIB_DIR
7629 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
7630 @ovindex ERLANG_INSTALL_LIB_DIR
7632 Set the @code{ERLANG_INSTALL_LIB_DIR} output variable to the directory into
7633 which every built Erlang library should be installed in a separate
7634 subdirectory.
7635 If this variable is not set in the environment when @command{configure} runs,
7636 its default value is @code{$ERLANG_LIB_DIR}, which value is set by the
7637 @code{AC_ERLANG_SUBST_LIB_DIR} macro.
7638 @end defmac
7640 @defmac AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR (@var{library}, @var{version})
7641 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
7642 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
7644 Set the @samp{ERLANG_INSTALL_LIB_DIR_@var{library}} output variable to the
7645 directory into which the built Erlang library @var{library} version
7646 @var{version} should be installed.  If this variable is not set in the
7647 environment when @command{configure} runs, its default value is
7648 @samp{$ERLANG_INSTALL_LIB_DIR/@var{library}-@var{version}}, the value of the
7649 @code{ERLANG_INSTALL_LIB_DIR} variable being set by the
7650 @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR} macro.
7651 @end defmac
7657 @c ========================================================= Writing Tests
7659 @node Writing Tests
7660 @chapter Writing Tests
7662 If the existing feature tests don't do something you need, you have to
7663 write new ones.  These macros are the building blocks.  They provide
7664 ways for other macros to check whether various kinds of features are
7665 available and report the results.
7667 This chapter contains some suggestions and some of the reasons why the
7668 existing tests are written the way they are.  You can also learn a lot
7669 about how to write Autoconf tests by looking at the existing ones.  If
7670 something goes wrong in one or more of the Autoconf tests, this
7671 information can help you understand the assumptions behind them, which
7672 might help you figure out how to best solve the problem.
7674 These macros check the output of the compiler system of the current
7675 language (@pxref{Language Choice}).  They do not cache the results of
7676 their tests for future use (@pxref{Caching Results}), because they don't
7677 know enough about the information they are checking for to generate a
7678 cache variable name.  They also do not print any messages, for the same
7679 reason.  The checks for particular kinds of features call these macros
7680 and do cache their results and print messages about what they're
7681 checking for.
7683 When you write a feature test that could be applicable to more than one
7684 software package, the best thing to do is encapsulate it in a new macro.
7685 @xref{Writing Autoconf Macros}, for how to do that.
7687 @menu
7688 * Language Choice::             Selecting which language to use for testing
7689 * Writing Test Programs::       Forging source files for compilers
7690 * Running the Preprocessor::    Detecting preprocessor symbols
7691 * Running the Compiler::        Detecting language or header features
7692 * Running the Linker::          Detecting library features
7693 * Runtime::                     Testing for runtime features
7694 * Systemology::                 A zoology of operating systems
7695 * Multiple Cases::              Tests for several possible values
7696 @end menu
7698 @node Language Choice
7699 @section Language Choice
7700 @cindex Language
7702 Autoconf-generated @command{configure} scripts check for the C compiler and
7703 its features by default.  Packages that use other programming languages
7704 (maybe more than one, e.g., C and C++) need to test features of the
7705 compilers for the respective languages.  The following macros determine
7706 which programming language is used in the subsequent tests in
7707 @file{configure.ac}.
7709 @anchor{AC_LANG}
7710 @defmac AC_LANG (@var{language})
7711 Do compilation tests using the compiler, preprocessor, and file
7712 extensions for the specified @var{language}.
7714 Supported languages are:
7716 @table @samp
7717 @item C
7718 Do compilation tests using @code{CC} and @code{CPP} and use extension
7719 @file{.c} for test programs.  Use compilation flags: @code{CPPFLAGS} with
7720 @code{CPP}, and both @code{CPPFLAGS} and @code{CFLAGS} with @code{CC}.
7722 @item C++
7723 Do compilation tests using @code{CXX} and @code{CXXCPP} and use
7724 extension @file{.C} for test programs.  Use compilation flags:
7725 @code{CPPFLAGS} with @code{CXXCPP}, and both @code{CPPFLAGS} and
7726 @code{CXXFLAGS} with @code{CXX}.
7728 @item Fortran 77
7729 Do compilation tests using @code{F77} and use extension @file{.f} for
7730 test programs.  Use compilation flags: @code{FFLAGS}.
7732 @item Fortran
7733 Do compilation tests using @code{FC} and use extension @file{.f} (or
7734 whatever has been set by @code{AC_FC_SRCEXT}) for test programs.  Use
7735 compilation flags: @code{FCFLAGS}.
7737 @item Erlang
7738 @ovindex ERLC
7739 @ovindex ERL
7740 @ovindex ERLCFLAGS
7741 Compile and execute tests using @code{ERLC} and @code{ERL} and use extension
7742 @file{.erl} for test Erlang modules.  Use compilation flags: @code{ERLCFLAGS}.
7744 @item Objective C
7745 Do compilation tests using @code{OBJC} and @code{OBJCPP} and use
7746 extension @file{.m} for test programs.  Use compilation flags:
7747 @code{CPPFLAGS} with @code{OBJCPP}, and both @code{CPPFLAGS} and
7748 @code{OBJCFLAGS} with @code{OBJC}.
7749 @end table
7750 @end defmac
7752 @anchor{AC_LANG_PUSH}
7753 @defmac AC_LANG_PUSH (@var{language})
7754 @acindex{LANG_PUSH}
7755 Remember the current language (as set by @code{AC_LANG}) on a stack, and
7756 then select the @var{language}.  Use this macro and @code{AC_LANG_POP}
7757 in macros that need to temporarily switch to a particular language.
7758 @end defmac
7760 @defmac AC_LANG_POP (@ovar{language})
7761 @acindex{LANG_POP}
7762 Select the language that is saved on the top of the stack, as set by
7763 @code{AC_LANG_PUSH}, and remove it from the stack.
7765 If given, @var{language} specifies the language we just @emph{quit}.  It
7766 is a good idea to specify it when it's known (which should be the
7767 case@dots{}), since Autoconf detects inconsistencies.
7769 @example
7770 AC_LANG_PUSH([Fortran 77])
7771 # Perform some tests on Fortran 77.
7772 # @dots{}
7773 AC_LANG_POP([Fortran 77])
7774 @end example
7775 @end defmac
7777 @defmac AC_LANG_ASSERT (@var{language})
7778 @acindex{LANG_ASSERT} Check statically that the current language is
7779 @var{language}.  You should use this in your language specific macros
7780 to avoid that they be called with an inappropriate language.
7782 This macro runs only at @command{autoconf} time, and incurs no cost at
7783 @command{configure} time.  Sadly enough and because Autoconf is a two
7784 layer language @footnote{Because M4 is not aware of Sh code,
7785 especially conditionals, some optimizations that look nice statically
7786 may produce incorrect results at runtime.}, the macros
7787 @code{AC_LANG_PUSH} and @code{AC_LANG_POP} cannot be ``optimizing'',
7788 therefore as much as possible you ought to avoid using them to wrap
7789 your code, rather, require from the user to run the macro with a
7790 correct current language, and check it with @code{AC_LANG_ASSERT}.
7791 And anyway, that may help the user understand she is running a Fortran
7792 macro while expecting a result about her Fortran 77 compiler@dots{}
7793 @end defmac
7796 @defmac AC_REQUIRE_CPP
7797 @acindex{REQUIRE_CPP}
7798 Ensure that whichever preprocessor would currently be used for tests has
7799 been found.  Calls @code{AC_REQUIRE} (@pxref{Prerequisite Macros}) with an
7800 argument of either @code{AC_PROG_CPP} or @code{AC_PROG_CXXCPP},
7801 depending on which language is current.
7802 @end defmac
7805 @node Writing Test Programs
7806 @section Writing Test Programs
7808 Autoconf tests follow a common scheme: feed some program with some
7809 input, and most of the time, feed a compiler with some source file.
7810 This section is dedicated to these source samples.
7812 @menu
7813 * Guidelines::                  General rules for writing test programs
7814 * Test Functions::              Avoiding pitfalls in test programs
7815 * Generating Sources::          Source program boilerplate
7816 @end menu
7818 @node Guidelines
7819 @subsection Guidelines for Test Programs
7821 The most important rule to follow when writing testing samples is:
7823 @center @emph{Look for realism.}
7825 This motto means that testing samples must be written with the same
7826 strictness as real programs are written.  In particular, you should
7827 avoid ``shortcuts'' and simplifications.
7829 Don't just play with the preprocessor if you want to prepare a
7830 compilation.  For instance, using @command{cpp} to check whether a header is
7831 functional might let your @command{configure} accept a header which
7832 causes some @emph{compiler} error.  Do not hesitate to check a header with
7833 other headers included before, especially required headers.
7835 Make sure the symbols you use are properly defined, i.e., refrain for
7836 simply declaring a function yourself instead of including the proper
7837 header.
7839 Test programs should not write to standard output.  They
7840 should exit with status 0 if the test succeeds, and with status 1
7841 otherwise, so that success
7842 can be distinguished easily from a core dump or other failure;
7843 segmentation violations and other failures produce a nonzero exit
7844 status.  Unless you arrange for @code{exit} to be declared, test
7845 programs should @code{return}, not @code{exit}, from @code{main},
7846 because on many systems @code{exit} is not declared by default.
7848 Test programs can use @code{#if} or @code{#ifdef} to check the values of
7849 preprocessor macros defined by tests that have already run.  For
7850 example, if you call @code{AC_HEADER_STDBOOL}, then later on in
7851 @file{configure.ac} you can have a test program that includes
7852 @file{stdbool.h} conditionally:
7854 @example
7855 @group
7856 #ifdef HAVE_STDBOOL_H
7857 # include <stdbool.h>
7858 #endif
7859 @end group
7860 @end example
7862 Both @code{#if HAVE_STDBOOL_H} and @code{#ifdef HAVE_STDBOOL_H} will
7863 work with any standard C compiler.  Some developers prefer @code{#if}
7864 because it is easier to read, while others prefer @code{#ifdef} because
7865 it avoids diagnostics with picky compilers like @acronym{GCC} with the
7866 @option{-Wundef} option.
7868 If a test program needs to use or create a data file, give it a name
7869 that starts with @file{conftest}, such as @file{conftest.data}.  The
7870 @command{configure} script cleans up by running @samp{rm -f -r conftest*}
7871 after running test programs and if the script is interrupted.
7873 @node Test Functions
7874 @subsection Test Functions
7876 These days it's safe to assume support for function prototypes
7877 (introduced in C89).
7879 Functions that test programs declare should also be conditionalized for
7880 C++, which requires @samp{extern "C"} prototypes.  Make sure to not
7881 include any header files containing clashing prototypes.
7883 @example
7884 #ifdef __cplusplus
7885 extern "C"
7886 #endif
7887 void *valloc (size_t);
7888 @end example
7890 If a test program calls a function with invalid parameters (just to see
7891 whether it exists), organize the program to ensure that it never invokes
7892 that function.  You can do this by calling it in another function that is
7893 never invoked.  You can't do it by putting it after a call to
7894 @code{exit}, because @acronym{GCC} version 2 knows that @code{exit}
7895 never returns
7896 and optimizes out any code that follows it in the same block.
7898 If you include any header files, be sure to call the functions
7899 relevant to them with the correct number of arguments, even if they are
7900 just 0, to avoid compilation errors due to prototypes.  @acronym{GCC}
7901 version 2
7902 has internal prototypes for several functions that it automatically
7903 inlines; for example, @code{memcpy}.  To avoid errors when checking for
7904 them, either pass them the correct number of arguments or redeclare them
7905 with a different return type (such as @code{char}).
7908 @node Generating Sources
7909 @subsection Generating Sources
7911 Autoconf provides a set of macros that can be used to generate test
7912 source files.  They are written to be language generic, i.e., they
7913 actually depend on the current language (@pxref{Language Choice}) to
7914 ``format'' the output properly.
7917 @defmac AC_LANG_CONFTEST (@var{source})
7918 @acindex{LANG_CONFTEST}
7919 Save the @var{source} text in the current test source file:
7920 @file{conftest.@var{extension}} where the @var{extension} depends on the
7921 current language.
7923 Note that the @var{source} is evaluated exactly once, like regular
7924 Autoconf macro arguments, and therefore (i) you may pass a macro
7925 invocation, (ii) if not, be sure to double quote if needed.
7926 @end defmac
7928 @defmac AC_LANG_SOURCE (@var{source})
7929 @acindex{LANG_SOURCE}
7930 Expands into the @var{source}, with the definition of
7931 all the @code{AC_DEFINE} performed so far.
7932 @end defmac
7934 For instance executing (observe the double quotation!):
7936 @example
7937 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7938 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7939   [Greetings string.])
7940 AC_LANG(C)
7941 AC_LANG_CONFTEST(
7942    [AC_LANG_SOURCE([[const char hw[] = "Hello, World\n";]])])
7943 gcc -E -dD -o - conftest.c
7944 @end example
7946 @noindent
7947 results in:
7949 @example
7950 @dots{}
7951 # 1 "conftest.c"
7953 #define PACKAGE_NAME "Hello"
7954 #define PACKAGE_TARNAME "hello"
7955 #define PACKAGE_VERSION "1.0"
7956 #define PACKAGE_STRING "Hello 1.0"
7957 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
7958 #define HELLO_WORLD "Hello, World\n"
7960 const char hw[] = "Hello, World\n";
7961 @end example
7963 When the test language is Fortran or Erlang, the @code{AC_DEFINE} definitions
7964 are not automatically translated into constants in the source code by this
7965 macro.
7967 @defmac AC_LANG_PROGRAM (@var{prologue}, @var{body})
7968 @acindex{LANG_PROGRAM}
7969 Expands into a source file which consists of the @var{prologue}, and
7970 then @var{body} as body of the main function (e.g., @code{main} in
7971 C).  Since it uses @code{AC_LANG_SOURCE}, the features of the latter are
7972 available.
7973 @end defmac
7975 For instance:
7977 @example
7978 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7979 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7980   [Greetings string.])
7981 AC_LANG_CONFTEST(
7982 [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
7983                  [[fputs (hw, stdout);]])])
7984 gcc -E -dD -o - conftest.c
7985 @end example
7987 @noindent
7988 results in:
7990 @example
7991 @dots{}
7992 # 1 "conftest.c"
7994 #define PACKAGE_NAME "Hello"
7995 #define PACKAGE_TARNAME "hello"
7996 #define PACKAGE_VERSION "1.0"
7997 #define PACKAGE_STRING "Hello 1.0"
7998 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
7999 #define HELLO_WORLD "Hello, World\n"
8001 const char hw[] = "Hello, World\n";
8003 main ()
8005 fputs (hw, stdout);
8006   ;
8007   return 0;
8009 @end example
8011 In Erlang tests, the created source file is that of an Erlang module called
8012 @code{conftest} (@file{conftest.erl}).  This module defines and exports
8013 at least
8014 one @code{start/0} function, which is called to perform the test.  The
8015 @var{prologue} is optional code that is inserted between the module header and
8016 the @code{start/0} function definition.  @var{body} is the body of the
8017 @code{start/0} function without the final period (@pxref{Runtime}, about
8018 constraints on this function's behavior).
8020 For instance:
8022 @example
8023 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
8024 AC_LANG(Erlang)
8025 AC_LANG_CONFTEST(
8026 [AC_LANG_PROGRAM([[-define(HELLO_WORLD, "Hello, world!").]],
8027                  [[io:format("~s~n", [?HELLO_WORLD])]])])
8028 cat conftest.erl
8029 @end example
8031 @noindent
8032 results in:
8034 @example
8035 -module(conftest).
8036 -export([start/0]).
8037 -define(HELLO_WORLD, "Hello, world!").
8038 start() ->
8039 io:format("~s~n", [?HELLO_WORLD])
8041 @end example
8043 @defmac AC_LANG_CALL (@var{prologue}, @var{function})
8044 @acindex{LANG_CALL}
8045 Expands into a source file which consists of the @var{prologue}, and
8046 then a call to the @var{function} as body of the main function (e.g.,
8047 @code{main} in C).  Since it uses @code{AC_LANG_PROGRAM}, the feature
8048 of the latter are available.
8050 This function will probably be replaced in the future by a version
8051 which would enable specifying the arguments.  The use of this macro is
8052 not encouraged, as it violates strongly the typing system.
8054 This macro cannot be used for Erlang tests.
8055 @end defmac
8057 @defmac AC_LANG_FUNC_LINK_TRY (@var{function})
8058 @acindex{LANG_FUNC_LINK_TRY}
8059 Expands into a source file which uses the @var{function} in the body of
8060 the main function (e.g., @code{main} in C).  Since it uses
8061 @code{AC_LANG_PROGRAM}, the features of the latter are available.
8063 As @code{AC_LANG_CALL}, this macro is documented only for completeness.
8064 It is considered to be severely broken, and in the future will be
8065 removed in favor of actual function calls (with properly typed
8066 arguments).
8068 This macro cannot be used for Erlang tests.
8069 @end defmac
8071 @node Running the Preprocessor
8072 @section Running the Preprocessor
8074 Sometimes one might need to run the preprocessor on some source file.
8075 @emph{Usually it is a bad idea}, as you typically need to @emph{compile}
8076 your project, not merely run the preprocessor on it; therefore you
8077 certainly want to run the compiler, not the preprocessor.  Resist the
8078 temptation of following the easiest path.
8080 Nevertheless, if you need to run the preprocessor, then use
8081 @code{AC_PREPROC_IFELSE}.
8083 The macros described in this section cannot be used for tests in Erlang or
8084 Fortran, since those languages require no preprocessor.
8086 @anchor{AC_PREPROC_IFELSE}
8087 @defmac AC_PREPROC_IFELSE (@var{input}, @ovar{action-if-true}, @
8088   @ovar{action-if-false})
8089 @acindex{PREPROC_IFELSE}
8090 Run the preprocessor of the current language (@pxref{Language Choice})
8091 on the @var{input}, run the shell commands @var{action-if-true} on
8092 success, @var{action-if-false} otherwise.  The @var{input} can be made
8093 by @code{AC_LANG_PROGRAM} and friends.
8095 This macro uses @code{CPPFLAGS}, but not @code{CFLAGS}, because
8096 @option{-g}, @option{-O}, etc.@: are not valid options to many C
8097 preprocessors.
8099 It is customary to report unexpected failures with
8100 @code{AC_MSG_FAILURE}.
8101 @end defmac
8103 For instance:
8105 @example
8106 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
8107 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
8108   [Greetings string.])
8109 AC_PREPROC_IFELSE(
8110    [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
8111                     [[fputs (hw, stdout);]])],
8112    [AC_MSG_RESULT([OK])],
8113    [AC_MSG_FAILURE([unexpected preprocessor failure])])
8114 @end example
8116 @noindent
8117 results in:
8119 @example
8120 checking for gcc... gcc
8121 checking for C compiler default output file name... a.out
8122 checking whether the C compiler works... yes
8123 checking whether we are cross compiling... no
8124 checking for suffix of executables...
8125 checking for suffix of object files... o
8126 checking whether we are using the GNU C compiler... yes
8127 checking whether gcc accepts -g... yes
8128 checking for gcc option to accept ISO C89... none needed
8129 checking how to run the C preprocessor... gcc -E
8131 @end example
8133 @sp 1
8135 The macro @code{AC_TRY_CPP} (@pxref{Obsolete Macros}) used to play the
8136 role of @code{AC_PREPROC_IFELSE}, but double quotes its argument, making
8137 it impossible to use it to elaborate sources.  You are encouraged to
8138 get rid of your old use of the macro @code{AC_TRY_CPP} in favor of
8139 @code{AC_PREPROC_IFELSE}, but, in the first place, are you sure you need
8140 to run the @emph{preprocessor} and not the compiler?
8142 @anchor{AC_EGREP_HEADER}
8143 @defmac AC_EGREP_HEADER (@var{pattern}, @var{header-file}, @
8144   @var{action-if-found}, @ovar{action-if-not-found})
8145 @acindex{EGREP_HEADER}
8146 If the output of running the preprocessor on the system header file
8147 @var{header-file} matches the extended regular expression
8148 @var{pattern}, execute shell commands @var{action-if-found}, otherwise
8149 execute @var{action-if-not-found}.
8150 @end defmac
8152 @anchor{AC_EGREP_CPP}
8153 @defmac AC_EGREP_CPP (@var{pattern}, @var{program}, @
8154   @ovar{action-if-found}, @ovar{action-if-not-found})
8155 @acindex{EGREP_CPP}
8156 @var{program} is the text of a C or C++ program, on which shell
8157 variable, back quote, and backslash substitutions are performed.  If the
8158 output of running the preprocessor on @var{program} matches the
8159 extended regular expression @var{pattern}, execute shell commands
8160 @var{action-if-found}, otherwise execute @var{action-if-not-found}.
8161 @end defmac
8165 @node Running the Compiler
8166 @section Running the Compiler
8168 To check for a syntax feature of the current language's (@pxref{Language
8169 Choice}) compiler, such as whether it recognizes a certain keyword, or
8170 simply to try some library feature, use @code{AC_COMPILE_IFELSE} to try
8171 to compile a small program that uses that feature.
8173 @defmac AC_COMPILE_IFELSE (@var{input}, @ovar{action-if-true}, @
8174   @ovar{action-if-false})
8175 @acindex{COMPILE_IFELSE}
8176 Run the compiler and compilation flags of the current language
8177 (@pxref{Language Choice}) on the @var{input}, run the shell commands
8178 @var{action-if-true} on success, @var{action-if-false} otherwise.  The
8179 @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
8181 It is customary to report unexpected failures with
8182 @code{AC_MSG_FAILURE}.  This macro does not try to link; use
8183 @code{AC_LINK_IFELSE} if you need to do that (@pxref{Running the
8184 Linker}).
8185 @end defmac
8187 @ovindex ERL
8188 For tests in Erlang, the @var{input} must be the source code of a module named
8189 @code{conftest}.  @code{AC_COMPILE_IFELSE} generates a @file{conftest.beam}
8190 file that can be interpreted by the Erlang virtual machine (@code{ERL}).  It is
8191 recommended to use @code{AC_LANG_PROGRAM} to specify the test program,
8192 to ensure that the Erlang module has the right name.
8194 @node Running the Linker
8195 @section Running the Linker
8197 To check for a library, a function, or a global variable, Autoconf
8198 @command{configure} scripts try to compile and link a small program that
8199 uses it.  This is unlike Metaconfig, which by default uses @code{nm} or
8200 @code{ar} on the C library to try to figure out which functions are
8201 available.  Trying to link with the function is usually a more reliable
8202 approach because it avoids dealing with the variations in the options
8203 and output formats of @code{nm} and @code{ar} and in the location of the
8204 standard libraries.  It also allows configuring for cross-compilation or
8205 checking a function's runtime behavior if needed.  On the other hand,
8206 it can be slower than scanning the libraries once, but accuracy is more
8207 important than speed.
8209 @code{AC_LINK_IFELSE} is used to compile test programs to test for
8210 functions and global variables.  It is also used by @code{AC_CHECK_LIB}
8211 to check for libraries (@pxref{Libraries}), by adding the library being
8212 checked for to @code{LIBS} temporarily and trying to link a small
8213 program.
8215 @anchor{AC_LINK_IFELSE}
8216 @defmac AC_LINK_IFELSE (@var{input}, @ovar{action-if-true}, @
8217   @ovar{action-if-false})
8218 @acindex{LINK_IFELSE}
8219 Run the compiler (and compilation flags) and the linker of the current
8220 language (@pxref{Language Choice}) on the @var{input}, run the shell
8221 commands @var{action-if-true} on success, @var{action-if-false}
8222 otherwise.  The @var{input} can be made by @code{AC_LANG_PROGRAM} and
8223 friends.
8225 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
8226 current compilation flags.
8228 It is customary to report unexpected failures with
8229 @code{AC_MSG_FAILURE}.  This macro does not try to execute the program;
8230 use @code{AC_RUN_IFELSE} if you need to do that (@pxref{Runtime}).
8231 @end defmac
8233 The @code{AC_LINK_IFELSE} macro cannot be used for Erlang tests, since Erlang
8234 programs are interpreted and do not require linking.
8238 @node Runtime
8239 @section Checking Runtime Behavior
8241 Sometimes you need to find out how a system performs at runtime, such
8242 as whether a given function has a certain capability or bug.  If you
8243 can, make such checks when your program runs instead of when it is
8244 configured.  You can check for things like the machine's endianness when
8245 your program initializes itself.
8247 If you really need to test for a runtime behavior while configuring,
8248 you can write a test program to determine the result, and compile and
8249 run it using @code{AC_RUN_IFELSE}.  Avoid running test programs if
8250 possible, because this prevents people from configuring your package for
8251 cross-compiling.
8253 @anchor{AC_RUN_IFELSE}
8254 @defmac AC_RUN_IFELSE (@var{input}, @ovar{action-if-true}, @
8255   @ovar{action-if-false}, @ovar{action-if-cross-compiling})
8256 @acindex{RUN_IFELSE}
8257 If @var{program} compiles and links successfully and returns an exit
8258 status of 0 when executed, run shell commands @var{action-if-true}.
8259 Otherwise, run shell commands @var{action-if-false}.
8261 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
8262 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
8263 compilation flags of the current language (@pxref{Language Choice}).
8265 If the compiler being used does not produce executables that run on the
8266 system where @command{configure} is being run, then the test program is
8267 not run.  If the optional shell commands @var{action-if-cross-compiling}
8268 are given, they are run instead.  Otherwise, @command{configure} prints
8269 an error message and exits.
8271 In the @var{action-if-false} section, the failing exit status is
8272 available in the shell variable @samp{$?}.  This exit status might be
8273 that of a failed compilation, or it might be that of a failed program
8274 execution.
8276 It is customary to report unexpected failures with
8277 @code{AC_MSG_FAILURE}.
8278 @end defmac
8280 Try to provide a pessimistic default value to use when cross-compiling
8281 makes runtime tests impossible.  You do this by passing the optional
8282 last argument to @code{AC_RUN_IFELSE}.  @command{autoconf} prints a
8283 warning message when creating @command{configure} each time it
8284 encounters a call to @code{AC_RUN_IFELSE} with no
8285 @var{action-if-cross-compiling} argument given.  You may ignore the
8286 warning, though users cannot configure your package for
8287 cross-compiling.  A few of the macros distributed with Autoconf produce
8288 this warning message.
8290 To configure for cross-compiling you can also choose a value for those
8291 parameters based on the canonical system name (@pxref{Manual
8292 Configuration}).  Alternatively, set up a test results cache file with
8293 the correct values for the host system (@pxref{Caching Results}).
8295 @ovindex cross_compiling
8296 To provide a default for calls of @code{AC_RUN_IFELSE} that are embedded
8297 in other macros, including a few of the ones that come with Autoconf,
8298 you can test whether the shell variable @code{cross_compiling} is set to
8299 @samp{yes}, and then use an alternate method to get the results instead
8300 of calling the macros.
8302 A C or C++ runtime test should be portable.
8303 @xref{Portable C and C++}.
8305 Erlang tests must exit themselves the Erlang VM by calling the @code{halt/1}
8306 function: the given status code is used to determine the success of the test
8307 (status is @code{0}) or its failure (status is different than @code{0}), as
8308 explained above.  It must be noted that data output through the standard output
8309 (e.g., using @code{io:format/2}) may be truncated when halting the VM.
8310 Therefore, if a test must output configuration information, it is recommended
8311 to create and to output data into the temporary file named @file{conftest.out},
8312 using the functions of module @code{file}.  The @code{conftest.out} file is
8313 automatically deleted by the @code{AC_RUN_IFELSE} macro.  For instance, a
8314 simplified implementation of Autoconf's @code{AC_ERLANG_SUBST_LIB_DIR}
8315 macro is:
8317 @example
8318 AC_INIT([LibdirTest], [1.0], [bug-libdirtest@@example.org])
8319 AC_ERLANG_NEED_ERL
8320 AC_LANG(Erlang)
8321 AC_RUN_IFELSE(
8322   [AC_LANG_PROGRAM([], [dnl
8323     file:write_file("conftest.out", code:lib_dir()),
8324     halt(0)])],
8325   [echo "code:lib_dir() returned: `cat conftest.out`"],
8326   [AC_MSG_FAILURE([test Erlang program execution failed])])
8327 @end example
8330 @node Systemology
8331 @section Systemology
8332 @cindex Systemology
8334 This section aims at presenting some systems and pointers to
8335 documentation.  It may help you addressing particular problems reported
8336 by users.
8338 @uref{http://www.opengroup.org/susv3, Posix-conforming systems} are
8339 derived from the @uref{http://www.bell-labs.com/history/unix/, Unix
8340 operating system}.
8342 The @uref{http://bhami.com/rosetta.html, Rosetta Stone for Unix}
8343 contains a table correlating the features of various Posix-conforming
8344 systems.  @uref{http://www.levenez.com/unix/, Unix History} is a
8345 simplified diagram of how many Unix systems were derived from each
8346 other.
8348 @uref{http://heirloom.sourceforge.net/, The Heirloom Project}
8349 provides some variants of traditional implementations of Unix utilities.
8351 @table @asis
8352 @item Darwin
8353 @cindex Darwin
8354 Darwin is also known as Mac OS X@.  Beware that the file system @emph{can} be
8355 case-preserving, but case insensitive.  This can cause nasty problems,
8356 since for instance the installation attempt for a package having an
8357 @file{INSTALL} file can result in @samp{make install} report that
8358 nothing was to be done!
8360 That's all dependent on whether the file system is a UFS (case
8361 sensitive) or HFS+ (case preserving).  By default Apple wants you to
8362 install the OS on HFS+.  Unfortunately, there are some pieces of
8363 software which really need to be built on UFS@.  We may want to rebuild
8364 Darwin to have both UFS and HFS+ available (and put the /local/build
8365 tree on the UFS).
8367 @item @acronym{QNX} 4.25
8368 @cindex @acronym{QNX} 4.25
8369 @c FIXME: Please, if you feel like writing something more precise,
8370 @c it'd be great.  In particular, I can't understand the difference with
8371 @c QNX Neutrino.
8372 @acronym{QNX} is a realtime operating system running on Intel architecture
8373 meant to be scalable from the small embedded systems to the hundred
8374 processor super-computer.  It claims to be Posix certified.  More
8375 information is available on the
8376 @uref{http://www.qnx.com/, @acronym{QNX} home page}.
8378 @item Tru64
8379 @cindex Tru64
8380 @uref{http://h30097.www3.hp.com/@/docs/,
8381 Documentation of several versions of Tru64} is available in different
8382 formats.
8384 @item Unix version 7
8385 @cindex Unix version 7
8386 @cindex V7
8387 Officially this was called the ``Seventh Edition'' of ``the @sc{unix}
8388 time-sharing system'' but we use the more-common name ``Unix version 7''.
8389 Documentation is available in the
8390 @uref{http://plan9.bell-labs.com/@/7thEdMan/, Unix Seventh Edition Manual}.
8391 Previous versions of Unix are called ``Unix version 6'', etc., but
8392 they were not as widely used.
8393 @end table
8396 @node Multiple Cases
8397 @section Multiple Cases
8399 Some operations are accomplished in several possible ways, depending on
8400 the OS variant.  Checking for them essentially requires a ``case
8401 statement''.  Autoconf does not directly provide one; however, it is
8402 easy to simulate by using a shell variable to keep track of whether a
8403 way to perform the operation has been found yet.
8405 Here is an example that uses the shell variable @code{fstype} to keep
8406 track of whether the remaining cases need to be checked.
8408 @example
8409 @group
8410 AC_MSG_CHECKING([how to get file system type])
8411 fstype=no
8412 # The order of these tests is important.
8413 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statvfs.h>
8414 #include <sys/fstyp.h>]])],
8415                   [AC_DEFINE([FSTYPE_STATVFS], [1],
8416                      [Define if statvfs exists.])
8417                    fstype=SVR4])
8418 if test $fstype = no; then
8419   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
8420 #include <sys/fstyp.h>]])],
8421                   [AC_DEFINE([FSTYPE_USG_STATFS], [1],
8422                      [Define if USG statfs.])
8423                    fstype=SVR3])
8425 if test $fstype = no; then
8426   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
8427 #include <sys/vmount.h>]])]),
8428                   [AC_DEFINE([FSTYPE_AIX_STATFS], [1],
8429                      [Define if AIX statfs.])
8430                    fstype=AIX])
8432 # (more cases omitted here)
8433 AC_MSG_RESULT([$fstype])
8434 @end group
8435 @end example
8437 @c ====================================================== Results of Tests.
8439 @node Results
8440 @chapter Results of Tests
8442 Once @command{configure} has determined whether a feature exists, what can
8443 it do to record that information?  There are four sorts of things it can
8444 do: define a C preprocessor symbol, set a variable in the output files,
8445 save the result in a cache file for future @command{configure} runs, and
8446 print a message letting the user know the result of the test.
8448 @menu
8449 * Defining Symbols::            Defining C preprocessor symbols
8450 * Setting Output Variables::    Replacing variables in output files
8451 * Special Chars in Variables::  Characters to beware of in variables
8452 * Caching Results::             Speeding up subsequent @command{configure} runs
8453 * Printing Messages::           Notifying @command{configure} users
8454 @end menu
8456 @node Defining Symbols
8457 @section Defining C Preprocessor Symbols
8459 A common action to take in response to a feature test is to define a C
8460 preprocessor symbol indicating the results of the test.  That is done by
8461 calling @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}.
8463 By default, @code{AC_OUTPUT} places the symbols defined by these macros
8464 into the output variable @code{DEFS}, which contains an option
8465 @option{-D@var{symbol}=@var{value}} for each symbol defined.  Unlike in
8466 Autoconf version 1, there is no variable @code{DEFS} defined while
8467 @command{configure} is running.  To check whether Autoconf macros have
8468 already defined a certain C preprocessor symbol, test the value of the
8469 appropriate cache variable, as in this example:
8471 @example
8472 AC_CHECK_FUNC([vprintf], [AC_DEFINE([HAVE_VPRINTF], [1],
8473                           [Define if vprintf exists.])])
8474 if test "$ac_cv_func_vprintf" != yes; then
8475   AC_CHECK_FUNC([_doprnt], [AC_DEFINE([HAVE_DOPRNT], [1],
8476                             [Define if _doprnt exists.])])
8478 @end example
8480 If @code{AC_CONFIG_HEADERS} has been called, then instead of creating
8481 @code{DEFS}, @code{AC_OUTPUT} creates a header file by substituting the
8482 correct values into @code{#define} statements in a template file.
8483 @xref{Configuration Headers}, for more information about this kind of
8484 output.
8486 @defmac AC_DEFINE (@var{variable}, @var{value}, @ovar{description})
8487 @defmacx AC_DEFINE (@var{variable})
8488 @cvindex @var{variable}
8489 @acindex{DEFINE}
8490 Define @var{variable} to @var{value} (verbatim), by defining a C
8491 preprocessor macro for @var{variable}.  @var{variable} should be a C
8492 identifier, optionally suffixed by a parenthesized argument list to
8493 define a C preprocessor macro with arguments.  The macro argument list,
8494 if present, should be a comma-separated list of C identifiers, possibly
8495 terminated by an ellipsis @samp{...} if C99 syntax is employed.
8496 @var{variable} should not contain comments, white space, trigraphs,
8497 backslash-newlines, universal character names, or non-@acronym{ASCII}
8498 characters.
8500 @var{value} may contain backslash-escaped newlines, which will be
8501 preserved if you use @code{AC_CONFIG_HEADERS} but flattened if passed
8502 via @code{@@DEFS@@} (with no effect on the compilation, since the
8503 preprocessor sees only one line in the first place).  @var{value} should
8504 not contain raw newlines.  If you are not using
8505 @code{AC_CONFIG_HEADERS}, @var{value} should not contain any @samp{#}
8506 characters, as @command{make} tends to eat them.  To use a shell
8507 variable, use @code{AC_DEFINE_UNQUOTED} instead.
8509 @var{description} is only useful if you are using
8510 @code{AC_CONFIG_HEADERS}.  In this case, @var{description} is put into
8511 the generated @file{config.h.in} as the comment before the macro define.
8512 The following example defines the C preprocessor variable
8513 @code{EQUATION} to be the string constant @samp{"$a > $b"}:
8515 @example
8516 AC_DEFINE([EQUATION], ["$a > $b"],
8517   [Equation string.])
8518 @end example
8520 If neither @var{value} nor @var{description} are given, then
8521 @var{value} defaults to 1 instead of to the empty string.  This is for
8522 backwards compatibility with older versions of Autoconf, but this usage
8523 is obsolescent and may be withdrawn in future versions of Autoconf.
8525 If the @var{variable} is a literal string, it is passed to
8526 @code{m4_pattern_allow} (@pxref{Forbidden Patterns}).
8528 If multiple @code{AC_DEFINE} statements are executed for the same
8529 @var{variable} name (not counting any parenthesized argument list),
8530 the last one wins.
8531 @end defmac
8533 @defmac AC_DEFINE_UNQUOTED (@var{variable}, @var{value}, @ovar{description})
8534 @defmacx AC_DEFINE_UNQUOTED (@var{variable})
8535 @acindex{DEFINE_UNQUOTED}
8536 @cvindex @var{variable}
8537 Like @code{AC_DEFINE}, but three shell expansions are
8538 performed---once---on @var{variable} and @var{value}: variable expansion
8539 (@samp{$}), command substitution (@samp{`}), and backslash escaping
8540 (@samp{\}).  Single and double quote characters in the value have no
8541 special meaning.  Use this macro instead of @code{AC_DEFINE} when
8542 @var{variable} or @var{value} is a shell variable.  Examples:
8544 @example
8545 AC_DEFINE_UNQUOTED([config_machfile], ["$machfile"],
8546   [Configuration machine file.])
8547 AC_DEFINE_UNQUOTED([GETGROUPS_T], [$ac_cv_type_getgroups],
8548   [getgroups return type.])
8549 AC_DEFINE_UNQUOTED([$ac_tr_hdr], [1],
8550   [Translated header name.])
8551 @end example
8552 @end defmac
8554 Due to a syntactical bizarreness of the Bourne shell, do not use
8555 semicolons to separate @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}
8556 calls from other macro calls or shell code; that can cause syntax errors
8557 in the resulting @command{configure} script.  Use either blanks or
8558 newlines.  That is, do this:
8560 @example
8561 AC_CHECK_HEADER([elf.h],
8562   [AC_DEFINE([SVR4], [1], [System V Release 4]) LIBS="-lelf $LIBS"])
8563 @end example
8565 @noindent
8566 or this:
8568 @example
8569 AC_CHECK_HEADER([elf.h],
8570   [AC_DEFINE([SVR4], [1], [System V Release 4])
8571    LIBS="-lelf $LIBS"])
8572 @end example
8574 @noindent
8575 instead of this:
8577 @example
8578 AC_CHECK_HEADER([elf.h],
8579   [AC_DEFINE([SVR4], [1], [System V Release 4]); LIBS="-lelf $LIBS"])
8580 @end example
8582 @node Setting Output Variables
8583 @section Setting Output Variables
8584 @cindex Output variables
8586 Another way to record the results of tests is to set @dfn{output
8587 variables}, which are shell variables whose values are substituted into
8588 files that @command{configure} outputs.  The two macros below create new
8589 output variables.  @xref{Preset Output Variables}, for a list of output
8590 variables that are always available.
8592 @defmac AC_SUBST (@var{variable}, @ovar{value})
8593 @acindex{SUBST}
8594 Create an output variable from a shell variable.  Make @code{AC_OUTPUT}
8595 substitute the variable @var{variable} into output files (typically one
8596 or more makefiles).  This means that @code{AC_OUTPUT}
8597 replaces instances of @samp{@@@var{variable}@@} in input files with the
8598 value that the shell variable @var{variable} has when @code{AC_OUTPUT}
8599 is called.  The value can contain any non-@code{NUL} character, including
8600 newline.
8601 Variable occurrences should not overlap: e.g., an input file should
8602 not contain @samp{@@@var{var1}@@@var{var2}@@} if @var{var1} and @var{var2}
8603 are variable names.
8604 The substituted value is not rescanned for more output variables;
8605 occurrences of @samp{@@@var{variable}@@} in the value are inserted
8606 literally into the output file.  (The algorithm uses the special marker
8607 @code{|#_!!_#|} internally, so neither the substituted value nor the
8608 output file may contain @code{|#_!!_#|}.)
8610 If @var{value} is given, in addition assign it to @var{variable}.
8612 The string @var{variable} is passed to @code{m4_pattern_allow}
8613 (@pxref{Forbidden Patterns}).
8614 @end defmac
8616 @defmac AC_SUBST_FILE (@var{variable})
8617 @acindex{SUBST_FILE}
8618 Another way to create an output variable from a shell variable.  Make
8619 @code{AC_OUTPUT} insert (without substitutions) the contents of the file
8620 named by shell variable @var{variable} into output files.  This means
8621 that @code{AC_OUTPUT} replaces instances of
8622 @samp{@@@var{variable}@@} in output files (such as @file{Makefile.in})
8623 with the contents of the file that the shell variable @var{variable}
8624 names when @code{AC_OUTPUT} is called.  Set the variable to
8625 @file{/dev/null} for cases that do not have a file to insert.
8626 This substitution occurs only when the @samp{@@@var{variable}@@} is on a
8627 line by itself, optionally surrounded by spaces and tabs.  The
8628 substitution replaces the whole line, including the spaces, tabs, and
8629 the terminating newline.
8631 This macro is useful for inserting makefile fragments containing
8632 special dependencies or other @code{make} directives for particular host
8633 or target types into makefiles.  For example, @file{configure.ac}
8634 could contain:
8636 @example
8637 AC_SUBST_FILE([host_frag])
8638 host_frag=$srcdir/conf/sun4.mh
8639 @end example
8641 @noindent
8642 and then a @file{Makefile.in} could contain:
8644 @example
8645 @@host_frag@@
8646 @end example
8648 The string @var{variable} is passed to @code{m4_pattern_allow}
8649 (@pxref{Forbidden Patterns}).
8650 @end defmac
8652 @cindex Precious Variable
8653 @cindex Variable, Precious
8654 Running @command{configure} in varying environments can be extremely
8655 dangerous.  If for instance the user runs @samp{CC=bizarre-cc
8656 ./configure}, then the cache, @file{config.h}, and many other output
8657 files depend upon @command{bizarre-cc} being the C compiler.  If
8658 for some reason the user runs @command{./configure} again, or if it is
8659 run via @samp{./config.status --recheck}, (@xref{Automatic Remaking},
8660 and @pxref{config.status Invocation}), then the configuration can be
8661 inconsistent, composed of results depending upon two different
8662 compilers.
8664 Environment variables that affect this situation, such as @samp{CC}
8665 above, are called @dfn{precious variables}, and can be declared as such
8666 by @code{AC_ARG_VAR}.
8668 @defmac AC_ARG_VAR (@var{variable}, @var{description})
8669 @acindex{ARG_VAR}
8670 Declare @var{variable} is a precious variable, and include its
8671 @var{description} in the variable section of @samp{./configure --help}.
8673 Being precious means that
8674 @itemize @minus
8675 @item
8676 @var{variable} is substituted via @code{AC_SUBST}.
8678 @item
8679 The value of @var{variable} when @command{configure} was launched is
8680 saved in the cache, including if it was not specified on the command
8681 line but via the environment.  Indeed, while @command{configure} can
8682 notice the definition of @code{CC} in @samp{./configure CC=bizarre-cc},
8683 it is impossible to notice it in @samp{CC=bizarre-cc ./configure},
8684 which, unfortunately, is what most users do.
8686 We emphasize that it is the @emph{initial} value of @var{variable} which
8687 is saved, not that found during the execution of @command{configure}.
8688 Indeed, specifying @samp{./configure FOO=foo} and letting
8689 @samp{./configure} guess that @code{FOO} is @code{foo} can be two
8690 different things.
8692 @item
8693 @var{variable} is checked for consistency between two
8694 @command{configure} runs.  For instance:
8696 @example
8697 $ @kbd{./configure --silent --config-cache}
8698 $ @kbd{CC=cc ./configure --silent --config-cache}
8699 configure: error: `CC' was not set in the previous run
8700 configure: error: changes in the environment can compromise \
8701 the build
8702 configure: error: run `make distclean' and/or \
8703 `rm config.cache' and start over
8704 @end example
8706 @noindent
8707 and similarly if the variable is unset, or if its content is changed.
8710 @item
8711 @var{variable} is kept during automatic reconfiguration
8712 (@pxref{config.status Invocation}) as if it had been passed as a command
8713 line argument, including when no cache is used:
8715 @example
8716 $ @kbd{CC=/usr/bin/cc ./configure var=raboof --silent}
8717 $ @kbd{./config.status --recheck}
8718 running CONFIG_SHELL=/bin/sh /bin/sh ./configure var=raboof \
8719   CC=/usr/bin/cc  --no-create --no-recursion
8720 @end example
8721 @end itemize
8722 @end defmac
8724 @node Special Chars in Variables
8725 @section Special Characters in Output Variables
8726 @cindex Output variables, special characters in
8728 Many output variables are intended to be evaluated both by
8729 @command{make} and by the shell.  Some characters are expanded
8730 differently in these two contexts, so to avoid confusion these
8731 variables' values should not contain any of the following characters:
8733 @example
8734 " # $ & ' ( ) * ; < > ? [ \ ^ ` |
8735 @end example
8737 Also, these variables' values should neither contain newlines, nor start
8738 with @samp{~}, nor contain white space or @samp{:} immediately followed
8739 by @samp{~}.  The values can contain nonempty sequences of white space
8740 characters like tabs and spaces, but each such sequence might
8741 arbitrarily be replaced by a single space during substitution.
8743 These restrictions apply both to the values that @command{configure}
8744 computes, and to the values set directly by the user.  For example, the
8745 following invocations of @command{configure} are problematic, since they
8746 attempt to use special characters within @code{CPPFLAGS} and white space
8747 within @code{$(srcdir)}:
8749 @example
8750 CPPFLAGS='-DOUCH="&\"#$*?"' '../My Source/ouch-1.0/configure'
8752 '../My Source/ouch-1.0/configure' CPPFLAGS='-DOUCH="&\"#$*?"'
8753 @end example
8755 @node Caching Results
8756 @section Caching Results
8757 @cindex Cache
8759 To avoid checking for the same features repeatedly in various
8760 @command{configure} scripts (or in repeated runs of one script),
8761 @command{configure} can optionally save the results of many checks in a
8762 @dfn{cache file} (@pxref{Cache Files}).  If a @command{configure} script
8763 runs with caching enabled and finds a cache file, it reads the results
8764 of previous runs from the cache and avoids rerunning those checks.  As a
8765 result, @command{configure} can then run much faster than if it had to
8766 perform all of the checks every time.
8768 @defmac AC_CACHE_VAL (@var{cache-id}, @var{commands-to-set-it})
8769 @acindex{CACHE_VAL}
8770 Ensure that the results of the check identified by @var{cache-id} are
8771 available.  If the results of the check were in the cache file that was
8772 read, and @command{configure} was not given the @option{--quiet} or
8773 @option{--silent} option, print a message saying that the result was
8774 cached; otherwise, run the shell commands @var{commands-to-set-it}.  If
8775 the shell commands are run to determine the value, the value is
8776 saved in the cache file just before @command{configure} creates its output
8777 files.  @xref{Cache Variable Names}, for how to choose the name of the
8778 @var{cache-id} variable.
8780 The @var{commands-to-set-it} @emph{must have no side effects} except for
8781 setting the variable @var{cache-id}, see below.
8782 @end defmac
8784 @defmac AC_CACHE_CHECK (@var{message}, @var{cache-id}, @
8785   @var{commands-to-set-it})
8786 @acindex{CACHE_CHECK}
8787 A wrapper for @code{AC_CACHE_VAL} that takes care of printing the
8788 messages.  This macro provides a convenient shorthand for the most
8789 common way to use these macros.  It calls @code{AC_MSG_CHECKING} for
8790 @var{message}, then @code{AC_CACHE_VAL} with the @var{cache-id} and
8791 @var{commands} arguments, and @code{AC_MSG_RESULT} with @var{cache-id}.
8793 The @var{commands-to-set-it} @emph{must have no side effects} except for
8794 setting the variable @var{cache-id}, see below.
8795 @end defmac
8797 It is common to find buggy macros using @code{AC_CACHE_VAL} or
8798 @code{AC_CACHE_CHECK}, because people are tempted to call
8799 @code{AC_DEFINE} in the @var{commands-to-set-it}.  Instead, the code that
8800 @emph{follows} the call to @code{AC_CACHE_VAL} should call
8801 @code{AC_DEFINE}, by examining the value of the cache variable.  For
8802 instance, the following macro is broken:
8804 @example
8805 @group
8806 AC_DEFUN([AC_SHELL_TRUE],
8807 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
8808                 [my_cv_shell_true_works=no
8809                  (true) 2>/dev/null && my_cv_shell_true_works=yes
8810                  if test "$my_cv_shell_true_works" = yes; then
8811                    AC_DEFINE([TRUE_WORKS], [1],
8812                              [Define if `true(1)' works properly.])
8813                  fi])
8815 @end group
8816 @end example
8818 @noindent
8819 This fails if the cache is enabled: the second time this macro is run,
8820 @code{TRUE_WORKS} @emph{will not be defined}.  The proper implementation
8823 @example
8824 @group
8825 AC_DEFUN([AC_SHELL_TRUE],
8826 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
8827                 [my_cv_shell_true_works=no
8828                  (true) 2>/dev/null && my_cv_shell_true_works=yes])
8829  if test "$my_cv_shell_true_works" = yes; then
8830    AC_DEFINE([TRUE_WORKS], [1],
8831              [Define if `true(1)' works properly.])
8832  fi
8834 @end group
8835 @end example
8837 Also, @var{commands-to-set-it} should not print any messages, for
8838 example with @code{AC_MSG_CHECKING}; do that before calling
8839 @code{AC_CACHE_VAL}, so the messages are printed regardless of whether
8840 the results of the check are retrieved from the cache or determined by
8841 running the shell commands.
8843 @menu
8844 * Cache Variable Names::        Shell variables used in caches
8845 * Cache Files::                 Files @command{configure} uses for caching
8846 * Cache Checkpointing::         Loading and saving the cache file
8847 @end menu
8849 @node Cache Variable Names
8850 @subsection Cache Variable Names
8851 @cindex Cache variable
8853 The names of cache variables should have the following format:
8855 @example
8856 @var{package-prefix}_cv_@var{value-type}_@var{specific-value}_@ovar{additional-options}
8857 @end example
8859 @noindent
8860 for example, @samp{ac_cv_header_stat_broken} or
8861 @samp{ac_cv_prog_gcc_traditional}.  The parts of the variable name are:
8863 @table @asis
8864 @item @var{package-prefix}
8865 An abbreviation for your package or organization; the same prefix you
8866 begin local Autoconf macros with, except lowercase by convention.
8867 For cache values used by the distributed Autoconf macros, this value is
8868 @samp{ac}.
8870 @item @code{_cv_}
8871 Indicates that this shell variable is a cache value.  This string
8872 @emph{must} be present in the variable name, including the leading
8873 underscore.
8875 @item @var{value-type}
8876 A convention for classifying cache values, to produce a rational naming
8877 system.  The values used in Autoconf are listed in @ref{Macro Names}.
8879 @item @var{specific-value}
8880 Which member of the class of cache values this test applies to.
8881 For example, which function (@samp{alloca}), program (@samp{gcc}), or
8882 output variable (@samp{INSTALL}).
8884 @item @var{additional-options}
8885 Any particular behavior of the specific member that this test applies to.
8886 For example, @samp{broken} or @samp{set}.  This part of the name may
8887 be omitted if it does not apply.
8888 @end table
8890 The values assigned to cache variables may not contain newlines.
8891 Usually, their values are Boolean (@samp{yes} or @samp{no}) or the
8892 names of files or functions; so this is not an important restriction.
8894 @node Cache Files
8895 @subsection Cache Files
8897 A cache file is a shell script that caches the results of configure
8898 tests run on one system so they can be shared between configure scripts
8899 and configure runs.  It is not useful on other systems.  If its contents
8900 are invalid for some reason, the user may delete or edit it.
8902 By default, @command{configure} uses no cache file,
8903 to avoid problems caused by accidental
8904 use of stale cache files.
8906 To enable caching, @command{configure} accepts @option{--config-cache} (or
8907 @option{-C}) to cache results in the file @file{config.cache}.
8908 Alternatively, @option{--cache-file=@var{file}} specifies that
8909 @var{file} be the cache file.  The cache file is created if it does not
8910 exist already.  When @command{configure} calls @command{configure} scripts in
8911 subdirectories, it uses the @option{--cache-file} argument so that they
8912 share the same cache.  @xref{Subdirectories}, for information on
8913 configuring subdirectories with the @code{AC_CONFIG_SUBDIRS} macro.
8915 @file{config.status} only pays attention to the cache file if it is
8916 given the @option{--recheck} option, which makes it rerun
8917 @command{configure}.
8919 It is wrong to try to distribute cache files for particular system types.
8920 There is too much room for error in doing that, and too much
8921 administrative overhead in maintaining them.  For any features that
8922 can't be guessed automatically, use the standard method of the canonical
8923 system type and linking files (@pxref{Manual Configuration}).
8925 The site initialization script can specify a site-wide cache file to
8926 use, instead of the usual per-program cache.  In this case, the cache
8927 file gradually accumulates information whenever someone runs a new
8928 @command{configure} script.  (Running @command{configure} merges the new cache
8929 results with the existing cache file.)  This may cause problems,
8930 however, if the system configuration (e.g., the installed libraries or
8931 compilers) changes and the stale cache file is not deleted.
8933 @node Cache Checkpointing
8934 @subsection Cache Checkpointing
8936 If your configure script, or a macro called from @file{configure.ac}, happens
8937 to abort the configure process, it may be useful to checkpoint the cache
8938 a few times at key points using @code{AC_CACHE_SAVE}.  Doing so
8939 reduces the amount of time it takes to rerun the configure script with
8940 (hopefully) the error that caused the previous abort corrected.
8942 @c FIXME: Do we really want to document this guy?
8943 @defmac AC_CACHE_LOAD
8944 @acindex{CACHE_LOAD}
8945 Loads values from existing cache file, or creates a new cache file if a
8946 cache file is not found.  Called automatically from @code{AC_INIT}.
8947 @end defmac
8949 @defmac AC_CACHE_SAVE
8950 @acindex{CACHE_SAVE}
8951 Flushes all cached values to the cache file.  Called automatically from
8952 @code{AC_OUTPUT}, but it can be quite useful to call
8953 @code{AC_CACHE_SAVE} at key points in @file{configure.ac}.
8954 @end defmac
8956 For instance:
8958 @example
8959 @r{ @dots{} AC_INIT, etc. @dots{}}
8960 @group
8961 # Checks for programs.
8962 AC_PROG_CC
8963 AC_PROG_AWK
8964 @r{ @dots{} more program checks @dots{}}
8965 AC_CACHE_SAVE
8966 @end group
8968 @group
8969 # Checks for libraries.
8970 AC_CHECK_LIB([nsl], [gethostbyname])
8971 AC_CHECK_LIB([socket], [connect])
8972 @r{ @dots{} more lib checks @dots{}}
8973 AC_CACHE_SAVE
8974 @end group
8976 @group
8977 # Might abort@dots{}
8978 AM_PATH_GTK([1.0.2], [], [AC_MSG_ERROR([GTK not in path])])
8979 AM_PATH_GTKMM([0.9.5], [], [AC_MSG_ERROR([GTK not in path])])
8980 @end group
8981 @r{ @dots{} AC_OUTPUT, etc. @dots{}}
8982 @end example
8984 @node Printing Messages
8985 @section Printing Messages
8986 @cindex Messages, from @command{configure}
8988 @command{configure} scripts need to give users running them several kinds
8989 of information.  The following macros print messages in ways appropriate
8990 for each kind.  The arguments to all of them get enclosed in shell
8991 double quotes, so the shell performs variable and back-quote
8992 substitution on them.
8994 These macros are all wrappers around the @command{echo} shell command.
8995 They direct output to the appropriate file descriptor (@pxref{File
8996 Descriptor Macros}).
8997 @command{configure} scripts should rarely need to run @command{echo} directly
8998 to print messages for the user.  Using these macros makes it easy to
8999 change how and when each kind of message is printed; such changes need
9000 only be made to the macro definitions and all the callers change
9001 automatically.
9003 To diagnose static issues, i.e., when @command{autoconf} is run, see
9004 @ref{Diagnostic Macros}.
9006 @defmac AC_MSG_CHECKING (@var{feature-description})
9007 @acindex{MSG_CHECKING}
9008 Notify the user that @command{configure} is checking for a particular
9009 feature.  This macro prints a message that starts with @samp{checking }
9010 and ends with @samp{...} and no newline.  It must be followed by a call
9011 to @code{AC_MSG_RESULT} to print the result of the check and the
9012 newline.  The @var{feature-description} should be something like
9013 @samp{whether the Fortran compiler accepts C++ comments} or @samp{for
9014 c89}.
9016 This macro prints nothing if @command{configure} is run with the
9017 @option{--quiet} or @option{--silent} option.
9018 @end defmac
9020 @anchor{AC_MSG_RESULT}
9021 @defmac AC_MSG_RESULT (@var{result-description})
9022 @acindex{MSG_RESULT}
9023 Notify the user of the results of a check.  @var{result-description} is
9024 almost always the value of the cache variable for the check, typically
9025 @samp{yes}, @samp{no}, or a file name.  This macro should follow a call
9026 to @code{AC_MSG_CHECKING}, and the @var{result-description} should be
9027 the completion of the message printed by the call to
9028 @code{AC_MSG_CHECKING}.
9030 This macro prints nothing if @command{configure} is run with the
9031 @option{--quiet} or @option{--silent} option.
9032 @end defmac
9034 @anchor{AC_MSG_NOTICE}
9035 @defmac AC_MSG_NOTICE (@var{message})
9036 @acindex{MSG_NOTICE}
9037 Deliver the @var{message} to the user.  It is useful mainly to print a
9038 general description of the overall purpose of a group of feature checks,
9039 e.g.,
9041 @example
9042 AC_MSG_NOTICE([checking if stack overflow is detectable])
9043 @end example
9045 This macro prints nothing if @command{configure} is run with the
9046 @option{--quiet} or @option{--silent} option.
9047 @end defmac
9049 @anchor{AC_MSG_ERROR}
9050 @defmac AC_MSG_ERROR (@var{error-description}, @ovar{exit-status})
9051 @acindex{MSG_ERROR}
9052 Notify the user of an error that prevents @command{configure} from
9053 completing.  This macro prints an error message to the standard error
9054 output and exits @command{configure} with @var{exit-status} (1 by default).
9055 @var{error-description} should be something like @samp{invalid value
9056 $HOME for \$HOME}.
9058 The @var{error-description} should start with a lower-case letter, and
9059 ``cannot'' is preferred to ``can't''.
9060 @end defmac
9062 @defmac AC_MSG_FAILURE (@var{error-description}, @ovar{exit-status})
9063 @acindex{MSG_FAILURE}
9064 This @code{AC_MSG_ERROR} wrapper notifies the user of an error that
9065 prevents @command{configure} from completing @emph{and} that additional
9066 details are provided in @file{config.log}.  This is typically used when
9067 abnormal results are found during a compilation.
9068 @end defmac
9070 @anchor{AC_MSG_WARN}
9071 @defmac AC_MSG_WARN (@var{problem-description})
9072 @acindex{MSG_WARN}
9073 Notify the @command{configure} user of a possible problem.  This macro
9074 prints the message to the standard error output; @command{configure}
9075 continues running afterward, so macros that call @code{AC_MSG_WARN} should
9076 provide a default (back-up) behavior for the situations they warn about.
9077 @var{problem-description} should be something like @samp{ln -s seems to
9078 make hard links}.
9079 @end defmac
9083 @c ====================================================== Programming in M4.
9085 @node Programming in M4
9086 @chapter Programming in M4
9087 @cindex M4
9089 Autoconf is written on top of two layers: @dfn{M4sugar}, which provides
9090 convenient macros for pure M4 programming, and @dfn{M4sh}, which
9091 provides macros dedicated to shell script generation.
9093 As of this version of Autoconf, these two layers still contain
9094 experimental macros, whose interface might change in the future.  As a
9095 matter of fact, @emph{anything that is not documented must not be used}.
9097 @menu
9098 * M4 Quotation::                Protecting macros from unwanted expansion
9099 * Using autom4te::              The Autoconf executables backbone
9100 * Programming in M4sugar::      Convenient pure M4 macros
9101 * Programming in M4sh::         Common shell Constructs
9102 * File Descriptor Macros::      File descriptor macros for input and output
9103 @end menu
9105 @node M4 Quotation
9106 @section M4 Quotation
9107 @cindex M4 quotation
9108 @cindex quotation
9110 The most common problem with existing macros is an improper quotation.
9111 This section, which users of Autoconf can skip, but which macro writers
9112 @emph{must} read, first justifies the quotation scheme that was chosen
9113 for Autoconf and then ends with a rule of thumb.  Understanding the
9114 former helps one to follow the latter.
9116 @menu
9117 * Active Characters::           Characters that change the behavior of M4
9118 * One Macro Call::              Quotation and one macro call
9119 * Quoting and Parameters::      M4 vs. shell parameters
9120 * Quotation and Nested Macros::  Macros calling macros
9121 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
9122 * Quadrigraphs::                Another way to escape special characters
9123 * Quotation Rule Of Thumb::     One parenthesis, one quote
9124 @end menu
9126 @node Active Characters
9127 @subsection Active Characters
9129 To fully understand where proper quotation is important, you first need
9130 to know what the special characters are in Autoconf: @samp{#} introduces
9131 a comment inside which no macro expansion is performed, @samp{,}
9132 separates arguments, @samp{[} and @samp{]} are the quotes themselves,
9133 @samp{(} and @samp{)} (which M4 tries to match by pairs), and finally
9134 @samp{$} inside a macro definition.
9136 In order to understand the delicate case of macro calls, we first have
9137 to present some obvious failures.  Below they are ``obvious-ified'',
9138 but when you find them in real life, they are usually in disguise.
9140 Comments, introduced by a hash and running up to the newline, are opaque
9141 tokens to the top level: active characters are turned off, and there is
9142 no macro expansion:
9144 @example
9145 # define([def], ine)
9146 @result{}# define([def], ine)
9147 @end example
9149 Each time there can be a macro expansion, there is a quotation
9150 expansion, i.e., one level of quotes is stripped:
9152 @example
9153 int tab[10];
9154 @result{}int tab10;
9155 [int tab[10];]
9156 @result{}int tab[10];
9157 @end example
9159 Without this in mind, the reader might try hopelessly to use her macro
9160 @code{array}:
9162 @example
9163 define([array], [int tab[10];])
9164 array
9165 @result{}int tab10;
9166 [array]
9167 @result{}array
9168 @end example
9170 @noindent
9171 How can you correctly output the intended results@footnote{Using
9172 @code{defn}.}?
9175 @node One Macro Call
9176 @subsection One Macro Call
9178 Let's proceed on the interaction between active characters and macros
9179 with this small macro, which just returns its first argument:
9181 @example
9182 define([car], [$1])
9183 @end example
9185 @noindent
9186 The two pairs of quotes above are not part of the arguments of
9187 @code{define}; rather, they are understood by the top level when it
9188 tries to find the arguments of @code{define}.  Therefore, assuming
9189 @code{car} is not already defined, it is equivalent to write:
9191 @example
9192 define(car, $1)
9193 @end example
9195 @noindent
9196 But, while it is acceptable for a @file{configure.ac} to avoid unnecessary
9197 quotes, it is bad practice for Autoconf macros which must both be more
9198 robust and also advocate perfect style.
9200 At the top level, there are only two possibilities: either you
9201 quote or you don't:
9203 @example
9204 car(foo, bar, baz)
9205 @result{}foo
9206 [car(foo, bar, baz)]
9207 @result{}car(foo, bar, baz)
9208 @end example
9210 Let's pay attention to the special characters:
9212 @example
9213 car(#)
9214 @error{}EOF in argument list
9215 @end example
9217 The closing parenthesis is hidden in the comment; with a hypothetical
9218 quoting, the top level understood it this way:
9220 @example
9221 car([#)]
9222 @end example
9224 @noindent
9225 Proper quotation, of course, fixes the problem:
9227 @example
9228 car([#])
9229 @result{}#
9230 @end example
9232 Here are more examples:
9234 @example
9235 car(foo, bar)
9236 @result{}foo
9237 car([foo, bar])
9238 @result{}foo, bar
9239 car((foo, bar))
9240 @result{}(foo, bar)
9241 car([(foo], [bar)])
9242 @result{}(foo
9243 define([a], [b])
9244 @result{}
9245 car(a)
9246 @result{}b
9247 car([a])
9248 @result{}b
9249 car([[a]])
9250 @result{}a
9251 car([[[a]]])
9252 @result{}[a]
9253 @end example
9255 @node Quoting and Parameters
9256 @subsection
9258 When M4 encounters @samp{$} within a macro definition, followed
9259 immediately by a character it recognizes (@samp{0}@dots{}@samp{9},
9260 @samp{#}, @samp{@@}, or @samp{*}), it will perform M4 parameter
9261 expansion.  This happens regardless of how many layers of quotes the
9262 parameter expansion is nested within, or even if it occurs in text that
9263 will be rescanned as a comment.
9265 @example
9266 define([none], [$1])
9267 @result{}
9268 define([one], [[$1]])
9269 @result{}
9270 define([two], [[[$1]]])
9271 @result{}
9272 define([comment], [# $1])
9273 @result{}
9274 define([active], [ACTIVE])
9275 @result{}
9276 none([active])
9277 @result{}ACTIVE
9278 one([active])
9279 @result{}active
9280 two([active])
9281 @result{}[active]
9282 comment([active])
9283 @result{}# active
9284 @end example
9286 On the other hand, since autoconf generates shell code, you often want
9287 to output shell variable expansion, rather than performing M4 parameter
9288 expansion.  To do this, you must use M4 quoting to separate the @samp{$}
9289 from the next character in the definition of your macro.  If the macro
9290 definition occurs in single-quoted text, then insert another level of
9291 quoting; if the usage is already inside a double-quoted string, then
9292 split it into concatenated strings.
9294 @example
9295 define([single], [a single-quoted $[]1 definition])
9296 @result{}
9297 define([double], [[a double-quoted $][1 definition]])
9298 @result{}
9299 single
9300 @result{}a single-quoted $1 definition
9301 double
9302 @result{}a double-quoted $1 definition
9303 @end example
9305 Posix states that M4 implementations are free to provide implementation
9306 extensions when @samp{$@{} is encountered in a macro definition.
9307 Autoconf reserves the longer sequence @samp{$@{@{} for use with planned
9308 extensions that will be available in the future @acronym{GNU} M4 2.0,
9309 but guarantees that all other instances of @samp{$@{} will be output
9310 literally.  Therefore, this idiom can also be used to output shell code
9311 parameter references:
9313 @example
9314 define([first], [$@{1@}])first
9315 @result{}$@{1@}
9316 @end example
9318 Posix also states that @samp{$11} should expand to the first parameter
9319 concatenated with a literal @samp{1}, although some versions of
9320 @acronym{GNU} M4 expand the eleventh parameter instead.  For
9321 portability, you should only use single-digit M4 parameter expansion.
9323 With this in mind, we can explore the cases where macros invoke
9324 macros@enddots{}
9326 @node Quotation and Nested Macros
9327 @subsection Quotation and Nested Macros
9329 The examples below use the following macros:
9331 @example
9332 define([car], [$1])
9333 define([active], [ACT, IVE])
9334 define([array], [int tab[10]])
9335 @end example
9337 Each additional embedded macro call introduces other possible
9338 interesting quotations:
9340 @example
9341 car(active)
9342 @result{}ACT
9343 car([active])
9344 @result{}ACT, IVE
9345 car([[active]])
9346 @result{}active
9347 @end example
9349 In the first case, the top level looks for the arguments of @code{car},
9350 and finds @samp{active}.  Because M4 evaluates its arguments
9351 before applying the macro, @samp{active} is expanded, which results in:
9353 @example
9354 car(ACT, IVE)
9355 @result{}ACT
9356 @end example
9358 @noindent
9359 In the second case, the top level gives @samp{active} as first and only
9360 argument of @code{car}, which results in:
9362 @example
9363 active
9364 @result{}ACT, IVE
9365 @end example
9367 @noindent
9368 i.e., the argument is evaluated @emph{after} the macro that invokes it.
9369 In the third case, @code{car} receives @samp{[active]}, which results in:
9371 @example
9372 [active]
9373 @result{}active
9374 @end example
9376 @noindent
9377 exactly as we already saw above.
9379 The example above, applied to a more realistic example, gives:
9381 @example
9382 car(int tab[10];)
9383 @result{}int tab10;
9384 car([int tab[10];])
9385 @result{}int tab10;
9386 car([[int tab[10];]])
9387 @result{}int tab[10];
9388 @end example
9390 @noindent
9391 Huh?  The first case is easily understood, but why is the second wrong,
9392 and the third right?  To understand that, you must know that after
9393 M4 expands a macro, the resulting text is immediately subjected
9394 to macro expansion and quote removal.  This means that the quote removal
9395 occurs twice---first before the argument is passed to the @code{car}
9396 macro, and second after the @code{car} macro expands to the first
9397 argument.
9399 As the author of the Autoconf macro @code{car}, you then consider it to
9400 be incorrect that your users have to double-quote the arguments of
9401 @code{car}, so you ``fix'' your macro.  Let's call it @code{qar} for
9402 quoted car:
9404 @example
9405 define([qar], [[$1]])
9406 @end example
9408 @noindent
9409 and check that @code{qar} is properly fixed:
9411 @example
9412 qar([int tab[10];])
9413 @result{}int tab[10];
9414 @end example
9416 @noindent
9417 Ahhh!  That's much better.
9419 But note what you've done: now that the result of @code{qar} is always
9420 a literal string, the only time a user can use nested macros is if she
9421 relies on an @emph{unquoted} macro call:
9423 @example
9424 qar(active)
9425 @result{}ACT
9426 qar([active])
9427 @result{}active
9428 @end example
9430 @noindent
9431 leaving no way for her to reproduce what she used to do with @code{car}:
9433 @example
9434 car([active])
9435 @result{}ACT, IVE
9436 @end example
9438 @noindent
9439 Worse yet: she wants to use a macro that produces a set of @code{cpp}
9440 macros:
9442 @example
9443 define([my_includes], [#include <stdio.h>])
9444 car([my_includes])
9445 @result{}#include <stdio.h>
9446 qar(my_includes)
9447 @error{}EOF in argument list
9448 @end example
9450 This macro, @code{qar}, because it double quotes its arguments, forces
9451 its users to leave their macro calls unquoted, which is dangerous.
9452 Commas and other active symbols are interpreted by M4 before
9453 they are given to the macro, often not in the way the users expect.
9454 Also, because @code{qar} behaves differently from the other macros,
9455 it's an exception that should be avoided in Autoconf.
9457 @node Changequote is Evil
9458 @subsection @code{changequote} is Evil
9459 @cindex @code{changequote}
9461 The temptation is often high to bypass proper quotation, in particular
9462 when it's late at night.  Then, many experienced Autoconf hackers
9463 finally surrender to the dark side of the force and use the ultimate
9464 weapon: @code{changequote}.
9466 The M4 builtin @code{changequote} belongs to a set of primitives that
9467 allow one to adjust the syntax of the language to adjust it to one's
9468 needs.  For instance, by default M4 uses @samp{`} and @samp{'} as
9469 quotes, but in the context of shell programming (and actually of most
9470 programming languages), that's about the worst choice one can make:
9471 because of strings and back-quoted expressions in shell code (such as
9472 @samp{'this'} and @samp{`that`}), and because of literal characters in usual
9473 programming languages (as in @samp{'0'}), there are many unbalanced
9474 @samp{`} and @samp{'}.  Proper M4 quotation then becomes a nightmare, if
9475 not impossible.  In order to make M4 useful in such a context, its
9476 designers have equipped it with @code{changequote}, which makes it
9477 possible to choose another pair of quotes.  M4sugar, M4sh, Autoconf, and
9478 Autotest all have chosen to use @samp{[} and @samp{]}.  Not especially
9479 because they are unlikely characters, but @emph{because they are
9480 characters unlikely to be unbalanced}.
9482 There are other magic primitives, such as @code{changecom} to specify
9483 what syntactic forms are comments (it is common to see
9484 @samp{changecom(<!--, -->)} when M4 is used to produce HTML pages),
9485 @code{changeword} and @code{changesyntax} to change other syntactic
9486 details (such as the character to denote the @var{n}th argument, @samp{$} by
9487 default, the parenthesis around arguments, etc.).
9489 These primitives are really meant to make M4 more useful for specific
9490 domains: they should be considered like command line options:
9491 @option{--quotes}, @option{--comments}, @option{--words}, and
9492 @option{--syntax}.  Nevertheless, they are implemented as M4 builtins, as
9493 it makes M4 libraries self contained (no need for additional options).
9495 There lies the problem@enddots{}
9497 @sp 1
9499 The problem is that it is then tempting to use them in the middle of an
9500 M4 script, as opposed to its initialization.  This, if not carefully
9501 thought out, can lead to disastrous effects: @emph{you are changing the
9502 language in the middle of the execution}.  Changing and restoring the
9503 syntax is often not enough: if you happened to invoke macros in between,
9504 these macros are lost, as the current syntax is probably not
9505 the one they were implemented with.
9507 @c FIXME: I've been looking for a short, real case example, but I
9508 @c lost them all :(
9511 @node Quadrigraphs
9512 @subsection Quadrigraphs
9513 @cindex quadrigraphs
9514 @cindex @samp{@@S|@@}
9515 @cindex @samp{@@&t@@}
9516 @c Info cannot handle `:' in index entries.
9517 @c @cindex @samp{@@<:@@}
9518 @c @cindex @samp{@@:>@@}
9519 @c @cindex @samp{@@%:@@}
9521 When writing an Autoconf macro you may occasionally need to generate
9522 special characters that are difficult to express with the standard
9523 Autoconf quoting rules.  For example, you may need to output the regular
9524 expression @samp{[^[]}, which matches any character other than @samp{[}.
9525 This expression contains unbalanced brackets so it cannot be put easily
9526 into an M4 macro.
9528 You can work around this problem by using one of the following
9529 @dfn{quadrigraphs}:
9531 @table @samp
9532 @item @@<:@@
9533 @samp{[}
9534 @item @@:>@@
9535 @samp{]}
9536 @item @@S|@@
9537 @samp{$}
9538 @item @@%:@@
9539 @samp{#}
9540 @item @@&t@@
9541 Expands to nothing.
9542 @end table
9544 Quadrigraphs are replaced at a late stage of the translation process,
9545 after @command{m4} is run, so they do not get in the way of M4 quoting.
9546 For example, the string @samp{^@@<:@@}, independently of its quotation,
9547 appears as @samp{^[} in the output.
9549 The empty quadrigraph can be used:
9551 @itemize @minus
9552 @item to mark trailing spaces explicitly
9554 Trailing spaces are smashed by @command{autom4te}.  This is a feature.
9556 @item to produce other quadrigraphs
9558 For instance @samp{@@<@@&t@@:@@} produces @samp{@@<:@@}.
9560 @item to escape @emph{occurrences} of forbidden patterns
9562 For instance you might want to mention @code{AC_FOO} in a comment, while
9563 still being sure that @command{autom4te} still catches unexpanded
9564 @samp{AC_*}.  Then write @samp{AC@@&t@@_FOO}.
9565 @end itemize
9567 The name @samp{@@&t@@} was suggested by Paul Eggert:
9569 @quotation
9570 I should give some credit to the @samp{@@&t@@} pun.  The @samp{&} is my
9571 own invention, but the @samp{t} came from the source code of the
9572 @sc{algol68c} compiler, written by Steve Bourne (of Bourne shell fame),
9573 and which used @samp{mt} to denote the empty string.  In C, it would
9574 have looked like something like:
9576 @example
9577 char const mt[] = "";
9578 @end example
9580 @noindent
9581 but of course the source code was written in Algol 68.
9583 I don't know where he got @samp{mt} from: it could have been his own
9584 invention, and I suppose it could have been a common pun around the
9585 Cambridge University computer lab at the time.
9586 @end quotation
9588 @node Quotation Rule Of Thumb
9589 @subsection Quotation Rule Of Thumb
9591 To conclude, the quotation rule of thumb is:
9593 @center @emph{One pair of quotes per pair of parentheses.}
9595 Never over-quote, never under-quote, in particular in the definition of
9596 macros.  In the few places where the macros need to use brackets
9597 (usually in C program text or regular expressions), properly quote
9598 @emph{the arguments}!
9600 It is common to read Autoconf programs with snippets like:
9602 @example
9603 AC_TRY_LINK(
9604 changequote(<<, >>)dnl
9605 <<#include <time.h>
9606 #ifndef tzname /* For SGI.  */
9607 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9608 #endif>>,
9609 changequote([, ])dnl
9610 [atoi (*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
9611 @end example
9613 @noindent
9614 which is incredibly useless since @code{AC_TRY_LINK} is @emph{already}
9615 double quoting, so you just need:
9617 @example
9618 AC_TRY_LINK(
9619 [#include <time.h>
9620 #ifndef tzname /* For SGI.  */
9621 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9622 #endif],
9623             [atoi (*tzname);],
9624             [ac_cv_var_tzname=yes],
9625             [ac_cv_var_tzname=no])
9626 @end example
9628 @noindent
9629 The M4-fluent reader might note that these two examples are rigorously
9630 equivalent, since M4 swallows both the @samp{changequote(<<, >>)}
9631 and @samp{<<} @samp{>>} when it @dfn{collects} the arguments: these
9632 quotes are not part of the arguments!
9634 Simplified, the example above is just doing this:
9636 @example
9637 changequote(<<, >>)dnl
9638 <<[]>>
9639 changequote([, ])dnl
9640 @end example
9642 @noindent
9643 instead of simply:
9645 @example
9646 [[]]
9647 @end example
9649 With macros that do not double quote their arguments (which is the
9650 rule), double-quote the (risky) literals:
9652 @example
9653 AC_LINK_IFELSE([AC_LANG_PROGRAM(
9654 [[#include <time.h>
9655 #ifndef tzname /* For SGI.  */
9656 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9657 #endif]],
9658                                 [atoi (*tzname);])],
9659                [ac_cv_var_tzname=yes],
9660                [ac_cv_var_tzname=no])
9661 @end example
9663 Please note that the macro @code{AC_TRY_LINK} is obsolete, so you really
9664 should be using @code{AC_LINK_IFELSE} instead.
9666 @xref{Quadrigraphs}, for what to do if you run into a hopeless case
9667 where quoting does not suffice.
9669 When you create a @command{configure} script using newly written macros,
9670 examine it carefully to check whether you need to add more quotes in
9671 your macros.  If one or more words have disappeared in the M4
9672 output, you need more quotes.  When in doubt, quote.
9674 However, it's also possible to put on too many layers of quotes.  If
9675 this happens, the resulting @command{configure} script may contain
9676 unexpanded macros.  The @command{autoconf} program checks for this problem
9677 by looking for the string @samp{AC_} in @file{configure}.  However, this
9678 heuristic does not work in general: for example, it does not catch
9679 overquoting in @code{AC_DEFINE} descriptions.
9682 @c ---------------------------------------- Using autom4te
9684 @node Using autom4te
9685 @section Using @command{autom4te}
9687 The Autoconf suite, including M4sugar, M4sh, and Autotest, in addition
9688 to Autoconf per se, heavily rely on M4.  All these different uses
9689 revealed common needs factored into a layer over M4:
9690 @command{autom4te}@footnote{
9692 Yet another great name from Lars J. Aas.
9696 @command{autom4te} is a preprocessor that is like @command{m4}.
9697 It supports M4 extensions designed for use in tools like Autoconf.
9699 @menu
9700 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
9701 * Customizing autom4te::        Customizing the Autoconf package
9702 @end menu
9704 @node autom4te Invocation
9705 @subsection Invoking @command{autom4te}
9707 The command line arguments are modeled after M4's:
9709 @example
9710 autom4te @var{options} @var{files}
9711 @end example
9713 @noindent
9714 @evindex M4
9715 where the @var{files} are directly passed to @command{m4}.  By default,
9716 @acronym{GNU} M4 is found during configuration, but the environment
9717 variable
9718 @env{M4} can be set to tell @command{autom4te} where to look.  In addition
9719 to the regular expansion, it handles the replacement of the quadrigraphs
9720 (@pxref{Quadrigraphs}), and of @samp{__oline__}, the current line in the
9721 output.  It supports an extended syntax for the @var{files}:
9723 @table @file
9724 @item @var{file}.m4f
9725 This file is an M4 frozen file.  Note that @emph{all the previous files
9726 are ignored}.  See the option @option{--melt} for the rationale.
9728 @item @var{file}?
9729 If found in the library path, the @var{file} is included for expansion,
9730 otherwise it is ignored instead of triggering a failure.
9731 @end table
9733 @sp 1
9735 Of course, it supports the Autoconf common subset of options:
9737 @table @option
9738 @item --help
9739 @itemx -h
9740 Print a summary of the command line options and exit.
9742 @item --version
9743 @itemx -V
9744 Print the version number of Autoconf and exit.
9746 @item --verbose
9747 @itemx -v
9748 Report processing steps.
9750 @item --debug
9751 @itemx -d
9752 Don't remove the temporary files and be even more verbose.
9754 @item --include=@var{dir}
9755 @itemx -I @var{dir}
9756 Also look for input files in @var{dir}.  Multiple invocations
9757 accumulate.
9759 @item --output=@var{file}
9760 @itemx -o @var{file}
9761 Save output (script or trace) to @var{file}.  The file @option{-} stands
9762 for the standard output.
9763 @end table
9765 @sp 1
9767 As an extension of @command{m4}, it includes the following options:
9769 @table @option
9770 @item --warnings=@var{category}
9771 @itemx -W @var{category}
9772 @evindex WARNINGS
9773 @c FIXME: Point to the M4sugar macros, not Autoconf's.
9774 Report the warnings related to @var{category} (which can actually be a
9775 comma separated list).  @xref{Reporting Messages}, macro
9776 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
9777 values include:
9779 @table @samp
9780 @item all
9781 report all the warnings
9783 @item none
9784 report none
9786 @item error
9787 treats warnings as errors
9789 @item no-@var{category}
9790 disable warnings falling into @var{category}
9791 @end table
9793 Warnings about @samp{syntax} are enabled by default, and the environment
9794 variable @env{WARNINGS}, a comma separated list of categories, is
9795 honored.  @samp{autom4te -W @var{category}} actually
9796 behaves as if you had run:
9798 @example
9799 autom4te --warnings=syntax,$WARNINGS,@var{category}
9800 @end example
9802 @noindent
9803 For example, if you want to disable defaults and @env{WARNINGS}
9804 of @command{autom4te}, but enable the warnings about obsolete
9805 constructs, you would use @option{-W none,obsolete}.
9807 @cindex Back trace
9808 @cindex Macro invocation stack
9809 @command{autom4te} displays a back trace for errors, but not for
9810 warnings; if you want them, just pass @option{-W error}.
9812 @item --melt
9813 @itemx -M
9814 Do not use frozen files.  Any argument @code{@var{file}.m4f} is
9815 replaced by @code{@var{file}.m4}.  This helps tracing the macros which
9816 are executed only when the files are frozen, typically
9817 @code{m4_define}.  For instance, running:
9819 @example
9820 autom4te --melt 1.m4 2.m4f 3.m4 4.m4f input.m4
9821 @end example
9823 @noindent
9824 is roughly equivalent to running:
9826 @example
9827 m4 1.m4 2.m4 3.m4 4.m4 input.m4
9828 @end example
9830 @noindent
9831 while
9833 @example
9834 autom4te 1.m4 2.m4f 3.m4 4.m4f input.m4
9835 @end example
9837 @noindent
9838 is equivalent to:
9840 @example
9841 m4 --reload-state=4.m4f input.m4
9842 @end example
9844 @item --freeze
9845 @itemx -f
9846 Produce a frozen state file.  @command{autom4te} freezing is stricter
9847 than M4's: it must produce no warnings, and no output other than empty
9848 lines (a line with white space is @emph{not} empty) and comments
9849 (starting with @samp{#}).  Unlike @command{m4}'s similarly-named option,
9850 this option takes no argument:
9852 @example
9853 autom4te 1.m4 2.m4 3.m4 --freeze --output=3.m4f
9854 @end example
9856 @noindent
9857 corresponds to
9859 @example
9860 m4 1.m4 2.m4 3.m4 --freeze-state=3.m4f
9861 @end example
9863 @item --mode=@var{octal-mode}
9864 @itemx -m @var{octal-mode}
9865 Set the mode of the non-traces output to @var{octal-mode}; by default
9866 @samp{0666}.
9867 @end table
9869 @sp 1
9871 @cindex @file{autom4te.cache}
9872 As another additional feature over @command{m4}, @command{autom4te}
9873 caches its results.  @acronym{GNU} M4 is able to produce a regular
9874 output and traces at the same time.  Traces are heavily used in the
9875 @acronym{GNU} Build System: @command{autoheader} uses them to build
9876 @file{config.h.in}, @command{autoreconf} to determine what
9877 @acronym{GNU} Build System components are used, @command{automake} to
9878 ``parse'' @file{configure.ac} etc.  To avoid recomputation,
9879 traces are cached while performing regular expansion,
9880 and conversely.  This cache is (actually, the caches are) stored in
9881 the directory @file{autom4te.cache}.  @emph{It can safely be removed}
9882 at any moment (especially if for some reason @command{autom4te}
9883 considers it is trashed).
9885 @table @option
9886 @item --cache=@var{directory}
9887 @itemx -C @var{directory}
9888 Specify the name of the directory where the result should be cached.
9889 Passing an empty value disables caching.  Be sure to pass a relative
9890 file name, as for the time being, global caches are not supported.
9892 @item --no-cache
9893 Don't cache the results.
9895 @item --force
9896 @itemx -f
9897 If a cache is used, consider it obsolete (but update it anyway).
9898 @end table
9900 @sp 1
9902 Because traces are so important to the @acronym{GNU} Build System,
9903 @command{autom4te} provides high level tracing features as compared to
9904 M4, and helps exploiting the cache:
9906 @table @option
9907 @item --trace=@var{macro}[:@var{format}]
9908 @itemx -t @var{macro}[:@var{format}]
9909 Trace the invocations of @var{macro} according to the @var{format}.
9910 Multiple @option{--trace} arguments can be used to list several macros.
9911 Multiple @option{--trace} arguments for a single macro are not
9912 cumulative; instead, you should just make @var{format} as long as
9913 needed.
9915 The @var{format} is a regular string, with newlines if desired, and
9916 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}.  It can
9917 use the following special escapes:
9919 @table @samp
9920 @item $$
9921 The character @samp{$}.
9923 @item $f
9924 The file name from which @var{macro} is called.
9926 @item $l
9927 The line number from which @var{macro} is called.
9929 @item $d
9930 The depth of the @var{macro} call.  This is an M4 technical detail that
9931 you probably don't want to know about.
9933 @item $n
9934 The name of the @var{macro}.
9936 @item $@var{num}
9937 The @var{num}th argument of the call to @var{macro}.
9939 @item $@@
9940 @itemx $@var{sep}@@
9941 @itemx $@{@var{separator}@}@@
9942 All the arguments passed to @var{macro}, separated by the character
9943 @var{sep} or the string @var{separator} (@samp{,} by default).  Each
9944 argument is quoted, i.e., enclosed in a pair of square brackets.
9946 @item $*
9947 @itemx $@var{sep}*
9948 @itemx $@{@var{separator}@}*
9949 As above, but the arguments are not quoted.
9951 @item $%
9952 @itemx $@var{sep}%
9953 @itemx $@{@var{separator}@}%
9954 As above, but the arguments are not quoted, all new line characters in
9955 the arguments are smashed, and the default separator is @samp{:}.
9957 The escape @samp{$%} produces single-line trace outputs (unless you put
9958 newlines in the @samp{separator}), while @samp{$@@} and @samp{$*} do
9959 not.
9960 @end table
9962 @xref{autoconf Invocation}, for examples of trace uses.
9964 @item --preselect=@var{macro}
9965 @itemx -p @var{macro}
9966 Cache the traces of @var{macro}, but do not enable traces.  This is
9967 especially important to save CPU cycles in the future.  For instance,
9968 when invoked, @command{autoconf} preselects all the macros that
9969 @command{autoheader}, @command{automake}, @command{autoreconf}, etc.,
9970 trace, so that running @command{m4} is not needed to trace them: the
9971 cache suffices.  This results in a huge speed-up.
9972 @end table
9974 @sp 1
9976 @cindex Autom4te Library
9977 Finally, @command{autom4te} introduces the concept of @dfn{Autom4te
9978 libraries}.  They consists in a powerful yet extremely simple feature:
9979 sets of combined command line arguments:
9981 @table @option
9982 @item --language=@var{language}
9983 @itemx -l @var{language}
9984 Use the @var{language} Autom4te library.  Current languages include:
9986 @table @code
9987 @item M4sugar
9988 create M4sugar output.
9990 @item M4sh
9991 create M4sh executable shell scripts.
9993 @item Autotest
9994 create Autotest executable test suites.
9996 @item Autoconf-without-aclocal-m4
9997 create Autoconf executable configure scripts without
9998 reading @file{aclocal.m4}.
10000 @item Autoconf
10001 create Autoconf executable configure scripts.  This language inherits
10002 all the characteristics of @code{Autoconf-without-aclocal-m4} and
10003 additionally reads @file{aclocal.m4}.
10004 @end table
10006 @item --prepend-include=@var{dir}
10007 @item -B @var{dir}
10008 Prepend directory @var{dir} to the search path.  This is used to include
10009 the language-specific files before any third-party macros.
10011 @end table
10013 @cindex @file{autom4te.cfg}
10014 As an example, if Autoconf is installed in its default location,
10015 @file{/usr/local}, the command @samp{autom4te -l m4sugar foo.m4} is
10016 strictly equivalent to the command:
10018 @example
10019 autom4te --prepend-include /usr/local/share/autoconf \
10020   m4sugar/m4sugar.m4f --warnings syntax foo.m4
10021 @end example
10023 @noindent
10024 Recursive expansion applies here: the command @samp{autom4te -l m4sh foo.m4}
10025 is the same as @samp{autom4te --language M4sugar m4sugar/m4sh.m4f
10026 foo.m4}, i.e.:
10028 @example
10029 autom4te --prepend-include /usr/local/share/autoconf \
10030   m4sugar/m4sugar.m4f m4sugar/m4sh.m4f --mode 777 foo.m4
10031 @end example
10033 @noindent
10034 The definition of the languages is stored in @file{autom4te.cfg}.
10036 @node Customizing autom4te
10037 @subsection Customizing @command{autom4te}
10039 One can customize @command{autom4te} via @file{~/.autom4te.cfg} (i.e.,
10040 as found in the user home directory), and @file{./.autom4te.cfg} (i.e.,
10041 as found in the directory from which @command{autom4te} is run).  The
10042 order is first reading @file{autom4te.cfg}, then @file{~/.autom4te.cfg},
10043 then @file{./.autom4te.cfg}, and finally the command line arguments.
10045 In these text files, comments are introduced with @code{#}, and empty
10046 lines are ignored.  Customization is performed on a per-language basis,
10047 wrapped in between a @samp{begin-language: "@var{language}"},
10048 @samp{end-language: "@var{language}"} pair.
10050 Customizing a language stands for appending options (@pxref{autom4te
10051 Invocation}) to the current definition of the language.  Options, and
10052 more generally arguments, are introduced by @samp{args:
10053 @var{arguments}}.  You may use the traditional shell syntax to quote the
10054 @var{arguments}.
10056 As an example, to disable Autoconf caches (@file{autom4te.cache})
10057 globally, include the following lines in @file{~/.autom4te.cfg}:
10059 @verbatim
10060 ## ------------------ ##
10061 ## User Preferences.  ##
10062 ## ------------------ ##
10064 begin-language: "Autoconf-without-aclocal-m4"
10065 args: --no-cache
10066 end-language: "Autoconf-without-aclocal-m4"
10067 @end verbatim
10070 @node Programming in M4sugar
10071 @section Programming in M4sugar
10073 @cindex M4sugar
10074 M4 by itself provides only a small, but sufficient, set of all-purpose
10075 macros.  M4sugar introduces additional generic macros.  Its name was
10076 coined by Lars J. Aas: ``Readability And Greater Understanding Stands 4
10077 M4sugar''.
10079 M4sugar reserves the macro namespace @samp{^_m4_} for internal use, and
10080 the macro namespace @samp{^m4_} for M4sugar macros.  You should not
10081 define your own macros into these namespaces.
10083 @menu
10084 * Redefined M4 Macros::         M4 builtins changed in M4sugar
10085 * Diagnostic Macros::           Diagnostic messages from M4sugar
10086 * Diversion support::           Diversions in M4sugar
10087 * Conditional constructs::      Conditions in M4
10088 * Looping constructs::          Iteration in M4
10089 * Evaluation Macros::           More quotation and evaluation control
10090 * Text processing Macros::      String manipulation in M4
10091 * Number processing Macros::    Arithmetic computation in M4
10092 * Forbidden Patterns::          Catching unexpanded macros
10093 @end menu
10095 @node Redefined M4 Macros
10096 @subsection Redefined M4 Macros
10098 @msindex{builtin}
10099 @msindex{changecom}
10100 @msindex{changequote}
10101 @msindex{debugfile}
10102 @msindex{debugmode}
10103 @msindex{decr}
10104 @msindex{define}
10105 @msindex{divnum}
10106 @msindex{dumpdef}
10107 @msindex{errprint}
10108 @msindex{esyscmd}
10109 @msindex{eval}
10110 @msindex{format}
10111 @msindex{ifdef}
10112 @msindex{incr}
10113 @msindex{index}
10114 @msindex{indir}
10115 @msindex{len}
10116 @msindex{pushdef}
10117 @msindex{shift}
10118 @msindex{substr}
10119 @msindex{syscmd}
10120 @msindex{sysval}
10121 @msindex{traceoff}
10122 @msindex{traceon}
10123 @msindex{translit}
10124 With a few exceptions, all the M4 native macros are moved in the
10125 @samp{m4_} pseudo-namespace, e.g., M4sugar renames @code{define} as
10126 @code{m4_define} etc.
10128 The list of macros unchanged from M4, except for their name, is:
10129 @itemize @minus
10130 @item m4_builtin
10131 @item m4_changecom
10132 @item m4_changequote
10133 @item m4_debugfile
10134 @item m4_debugmode
10135 @item m4_decr
10136 @item m4_define
10137 @item m4_divnum
10138 @item m4_dumpdef
10139 @item m4_errprint
10140 @item m4_esyscmd
10141 @item m4_eval
10142 @item m4_format
10143 @item m4_ifdef
10144 @item m4_incr
10145 @item m4_index
10146 @item m4_indir
10147 @item m4_len
10148 @item m4_pushdef
10149 @item m4_shift
10150 @item m4_substr
10151 @item m4_syscmd
10152 @item m4_sysval
10153 @item m4_traceoff
10154 @item m4_traceon
10155 @item m4_translit
10156 @end itemize
10158 Some M4 macros are redefined, and are slightly incompatible with their
10159 native equivalent.
10161 @defmac __file__
10162 @defmacx __line__
10163 @MSindex __file__
10164 @MSindex __line__
10165 All M4 macros starting with @samp{__} retain their original name: for
10166 example, no @code{m4__file__} is defined.
10167 @end defmac
10169 @defmac __oline__
10170 @MSindex __oline__
10171 This is not technically a macro, but a feature of Autom4te.  The
10172 sequence @code{__oline__} can be used similarly to the other m4sugar
10173 location macros, but rather than expanding to the location of the input
10174 file, it is translated to the line number where it appears in the output
10175 file after all other M4 expansions.
10176 @end defmac
10178 @defmac dnl
10179 @MSindex dnl
10180 This macro kept its original name: no @code{m4_dnl} is defined.
10181 @end defmac
10183 @defmac m4_bpatsubst (@var{string}, @var{regexp}, @ovar{replacement})
10184 @msindex{bpatsubst}
10185 This macro corresponds to @code{patsubst}.  The name @code{m4_patsubst}
10186 is kept for future versions of M4sugar, once @acronym{GNU} M4 2.0 is
10187 released and supports extended regular expression syntax.
10188 @end defmac
10190 @defmac m4_bregexp (@var{string}, @var{regexp}, @ovar{replacement})
10191 @msindex{bregexp}
10192 This macro corresponds to @code{regexp}.  The name @code{m4_regexp}
10193 is kept for future versions of M4sugar, once @acronym{GNU} M4 2.0 is
10194 released and supports extended regular expression syntax.
10195 @end defmac
10197 @defmac m4_defn (@var{macro})
10198 @msindex{defn}
10199 Unlike the M4 builtin, this macro fails if @var{macro} is not
10200 defined.  Also, while newer M4 can concatenate multiple definitions,
10201 this version currently only supports a single @var{macro}.  See
10202 @code{m4_undefine}.
10203 @end defmac
10205 @defmac m4_divert (@var{diversion})
10206 @msindex{divert}
10207 M4sugar relies heavily on diversions, so rather than behaving as a
10208 primitive, @code{m4_divert} behaves like:
10209 @example
10210 m4_divert_pop()m4_divert_push([@var{diversion}])
10211 @end example
10212 @noindent
10213 @xref{Diversion support}, for more details about the use of the
10214 diversion stack.
10215 @end defmac
10217 @defmac m4_exit (@var{exit-status})
10218 @msindex{exit}
10219 This macro corresponds to @code{m4exit}.
10220 @end defmac
10222 @defmac m4_if (@var{comment})
10223 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @ovar{not-equal})
10224 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @dots{})
10225 @msindex{if}
10226 This macro corresponds to @code{ifelse}.  @var{string-1} and
10227 @var{string-2} are compared literally, so usually one of the two
10228 arguments is passed unquoted.  @xref{Conditional constructs}, for more
10229 conditional idioms.
10230 @end defmac
10232 @defmac m4_include (@var{file})
10233 @defmacx m4_sinclude (@var{file})
10234 @msindex{include}
10235 @msindex{sinclude}
10236 Like the M4 builtins, but warn against multiple inclusions of @var{file}.
10237 @end defmac
10239 @defmac m4_mkstemp (@var{template})
10240 @defmacx m4_maketemp (@var{template})
10241 @msindex{maketemp}
10242 @msindex{mkstemp}
10243 Posix requires @code{maketemp} to replace the trailing @samp{X}
10244 characters in @var{template} with the process id, without regards to the
10245 existence of a file by that name, but this a security hole.  When this
10246 was pointed out to the Posix folks, they agreed to invent a new macro
10247 @code{mkstemp} that always creates a uniquely named file, but not all
10248 versions of @acronym{GNU} M4 support the new macro.  In M4sugar,
10249 @code{m4_maketemp} and @code{m4_mkstemp} are synonyms for each other,
10250 and both have the secure semantics regardless of which macro the
10251 underlying M4 provides.
10252 @end defmac
10254 @defmac m4_popdef (@var{macro})
10255 @msindex{popdef}
10256 Unlike the M4 builtin, this macro fails if @var{macro} is not
10257 defined.  Also, while newer M4 can pop multiple definitions at once,
10258 this version currently only supports a single @var{macro}.  See
10259 @code{m4_undefine}.
10260 @end defmac
10262 @defmac m4_undefine (@var{macro})
10263 @msindex{undefine}
10264 Unlike the M4 builtin, this macro fails if @var{macro} is not
10265 defined.  Also, while newer M4 can undefine multiple definitions at
10266 once, this version currently only supports a single @var{macro}.  Use
10268 @example
10269 m4_ifdef([@var{macro}], [m4_undefine([@var{macro}])])
10270 @end example
10272 @noindent
10273 to recover the behavior of the builtin.
10274 @end defmac
10276 @defmac m4_undivert (@var{diversion})
10277 @msindex{undivert}
10278 Unlike the M4 builtin, only one diversion can be undiverted per
10279 invocation.  Also, since the M4sugar diversion stack prefers named
10280 diversions, the use of @code{m4_undivert} to include files is risky.
10281 @xref{Diversion support}, for more details about the use of the
10282 diversion stack.
10283 @end defmac
10285 @defmac m4_wrap (@var{text})
10286 @msindex{wrap}
10287 This macro corresponds to @code{m4wrap}.
10289 Posix requires arguments of multiple @code{m4wrap} calls to be
10290 reprocessed at @acronym{EOF} in the same order as the original calls.
10291 @acronym{GNU} M4 versions through 1.4.x, however, reprocess them in
10292 reverse order.  Your code should not depend on the order.
10294 Also, Posix requires @code{m4wrap} to ignore its second and succeeding
10295 arguments, but @acronym{GNU} M4 versions through 1.4.x concatenate the
10296 arguments with intervening spaces.  Your code should not pass more than
10297 one argument.
10299 You are encouraged to end @var{text} with @samp{[]}, to avoid unexpected
10300 token pasting between consecutive invocations of @code{m4_wrap}, as in:
10302 @example
10303 m4_define([foo], [bar])
10304 m4_define([foofoo], [OUCH])
10305 m4_wrap([foo])
10306 m4_wrap([foo])
10307 @result{}OUCH
10308 @end example
10309 @end defmac
10312 @node Diagnostic Macros
10313 @subsection Diagnostic messages from M4sugar
10314 @cindex Messages, from @command{M4sugar}
10316 When macros statically diagnose abnormal situations, benign or fatal,
10317 they should report them using these macros.  For issuing dynamic issues,
10318 i.e., when @command{configure} is run, see @ref{Printing Messages}.
10320 @defmac m4_assert (@var{expression}, @dvar{exit-status, 1})
10321 @msindex{assert}
10322 Assert that the arithmetic @var{expression} evaluates to non-zero.
10323 Otherwise, issue a fatal error, and exit @command{autom4te} with
10324 @var{exit-status}.
10325 @end defmac
10327 @defmac m4_errprintn (@var{message})
10328 @msindex{errprintn}
10329 Similar to the builtin @code{m4_errprint}, except that a newline is
10330 guaranteed after @var{message}.
10331 @end defmac
10333 @anchor{m4_fatal}
10334 @defmac m4_fatal
10335 @msindex{fatal}
10336 Report a severe error @var{message} prefixed with the current location,
10337 and have @command{autom4te} die.
10338 @end defmac
10340 @defmac m4_location
10341 @msindex{location}
10342 Useful as a prefix in a message line.  Short for:
10343 @example
10344 __file__:__line__
10345 @end example
10346 @end defmac
10348 @anchor{m4_warn}
10349 @defmac m4_warn (@var{category}, @var{message})
10350 @msindex{warn}
10351 Report @var{message} as a warning (or as an error if requested by the
10352 user) if warnings of the @var{category} are turned on.  If the message
10353 is emitted, it is prefixed with the current location, and followed by a
10354 call trace of all macros defined via @code{AC_DEFUN} used to get to the
10355 current expansion.  You are encouraged to use standard categories, which
10356 currently include:
10358 @table @samp
10359 @item all
10360 messages that don't fall into one of the following categories.  Use of an
10361 empty @var{category} is equivalent.
10363 @item cross
10364 related to cross compilation issues.
10366 @item obsolete
10367 use of an obsolete construct.
10369 @item syntax
10370 dubious syntactic constructs, incorrectly ordered macro calls.
10371 @end table
10372 @end defmac
10375 @node Diversion support
10376 @subsection Diversion support
10378 M4sugar makes heavy use of diversions, because it is often the case that
10379 text that must appear early in the output is not discovered until late
10380 in the input.  Additionally, some of the topological sorting algorithms
10381 used in resolving macro dependencies use diversions.  Therefore, most
10382 macros should not need to change diversions directly, but rather rely on
10383 higher-level M4sugar macros to manage diversions transparently.
10385 To make diversion management easier, M4sugar uses the concept of named
10386 diversions.  Rather than using diversion numbers directly, it is nicer
10387 to associate a name with each diversion; the diversion number associated
10388 with a particular diversion name is an implementation detail, so you
10389 should only use diversion names.
10391 M4sugar defines two named diversions.
10392 @table @code
10393 @item KILL
10394 Text written to this diversion is discarded.  This is the default
10395 diversion once M4sugar is initialized.
10396 @item GROW
10397 This diversion is used behind the scenes by topological sorting macros,
10398 such as @code{AC_REQUIRE}.
10399 @end table
10401 M4sh adds several more named diversions.
10402 @table @code
10403 @item BINSH
10404 This diversion is reserved for the @samp{#!} interpreter line.
10405 @item HEADER-REVISION
10406 This diversion holds text from @code{AC_REVISION}.
10407 @item HEADER-COMMENT
10408 This diversion holds comments about the purpose of a file.
10409 @item HEADER-COPYRIGHT
10410 This diversion is managed by @code{AC_COPYRIGHT}.
10411 @item M4SH-SANITIZE
10412 This diversion contains M4sh sanitization code, used to ensure M4sh is
10413 executing in a reasonable shell environment.
10414 @item M4SH-INIT
10415 This diversion contains M4sh initialization code, initializing variables
10416 that are required by other M4sh macros.
10417 @item BODY
10418 This diversion contains the body of the shell code, and is the default
10419 diversion once M4sh is initialized.
10420 @end table
10422 For now, the named diversions of Autoconf, Autoheader, and Autotest are
10423 not documented.
10425 @defmac m4_divert_once (@var{diversion}, @ovar{content})
10426 @msindex{divert_once}
10427 Similar to @code{m4_divert_text}, except that @var{content} is only
10428 output to @var{diversion} if this is the first time that
10429 @code{m4_divert_once} has been called with its particular arguments.
10430 @end defmac
10432 @defmac m4_divert_pop (@ovar{diversion})
10433 @msindex{divert_pop}
10434 If provided, check that the current diversion is indeed @var{diversion}.
10435 Then change to the diversion located earlier on the stack, giving an
10436 error if an attempt is made to pop beyond the initial m4sugar diversion
10437 of @code{KILL}.
10438 @end defmac
10440 @defmac m4_divert_push (@var{diversion})
10441 @msindex{divert_push}
10442 Remember the former diversion on the diversion stack, and output
10443 subsequent text into @var{diversion}.  M4sugar maintains a diversion
10444 stack, and issues an error if there is not a matching pop for every
10445 push.
10446 @end defmac
10448 @defmac m4_divert_text (@var{diversion}, @ovar{content})
10449 @msindex{divert_text}
10450 Output @var{content} and a newline into @var{diversion}, without
10451 affecting the current diversion.  Shorthand for:
10452 @example
10453 m4_divert_push([@var{diversion}])@var{content}
10454 m4_divert_pop([@var{diversion}])dnl
10455 @end example
10456 @end defmac
10458 @node Conditional constructs
10459 @subsection Conditional constructs
10461 The following macros provide additional conditional contructs, as
10462 convenience wrappers around @code{m4_if}.
10464 @defmac m4_bmatch (@var{string}, @var{regex-1}, @var{value-1}, @
10465   @ovar{regex-2}, @ovar{value-2}, @dots{}, @ovar{default})
10466 @msindex{bmatch}
10467 The string @var{string} is repeatedly compared against a series of
10468 @var{regex} arguments; if a match is found, the expansion is the
10469 corresponding @var{value}, otherwise, the macro moves on to the next
10470 @var{regex}.  If no @var{regex} match, then the result is the optional
10471 @var{default}, or nothing.
10472 @end defmac
10474 @defmac m4_bpatsubsts (@var{string}, @var{regex-1}, @var{subst-1}, @
10475   @ovar{regex-2}, @ovar{subst-2}, @dots{})
10476 @msindex{bpatsubsts}
10477 The string @var{string} is altered by @var{regex-1} and @var{subst-1},
10478 as if by:
10479 @example
10480 m4_bpatsubst([[@var{string}]], [@var{regex}], [@var{subst}])
10481 @end example
10483 @noindent
10484 The result of the substitution is then passed through the next set of
10485 @var{regex} and @var{subst}, and so forth.  An empty @var{subst} implies
10486 deletion of any matched portions in the current string.  Note that this
10487 macro over-quotes @var{string}; this behavior is intentional, so that
10488 the result of each step of the recursion remains as a quoted string.
10489 However, it means that anchors (@samp{^} and @samp{$} in the @var{regex}
10490 will line up with the extra quotations, and not the characters of the
10491 original string.
10492 @end defmac
10494 @defmac m4_case (@var{string}, @var{value-1}, @var{if-value-1}, @
10495   @ovar{value-2}, @ovar{if-value-2}, @dots{}, @ovar{default})
10496 @msindex{case}
10497 Test @var{string} against multiple @var{value} possibilities, resulting
10498 in the first @var{if-value} for a match, or in the optional
10499 @var{default}.  This is shorthand for:
10500 @example
10501 m4_if([@var{string}], [@var{value-1}], [@var{if-value-1}],
10502       [@var{string}], [@var{value-2}], [@var{if-value-2}], @dots{},
10503       [@var{default}])
10504 @end example
10505 @end defmac
10507 @defmac m4_cond (@var{test-1}, @var{value-1}, @var{if-value-1}, @
10508   @ovar{test-2}, @ovar{value-2}, @ovar{if-value-2}, @dots{}, @ovar{default})
10509 @msindex{cond}
10510 This macro was introduced in Autoconf 2.62.  Similar to @code{m4_if},
10511 except that each @var{test} is expanded only when it is encountered.
10512 This is useful for short-circuiting expensive tests; while @code{m4_if}
10513 requires all its strings to be expanded up front before doing
10514 comparisons, @code{m4_cond} only expands a @var{test} when all earlier
10515 tests have failed.
10517 For an example, these two sequences give the same result, but in the
10518 case where @samp{$1} does not contain a backslash, the @code{m4_cond}
10519 version only expands @code{m4_index} once, instead of five times, for
10520 faster computation if this is a common case for @samp{$1}.  Notice that
10521 every third argument is unquoted for @code{m4_if}, and quoted for
10522 @code{m4_cond}:
10524 @example
10525 m4_if(m4_index([$1], [\]), [-1], [$2],
10526       m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
10527       m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
10528       m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
10529       m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
10530       [$2])
10531 m4_cond([m4_index([$1], [\])], [-1], [$2],
10532         [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
10533         [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
10534         [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
10535         [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
10536         [$2])
10537 @end example
10538 @end defmac
10540 @defmac m4_default (@var{expr-1}, @var{expr-2})
10541 @msindex{default}
10542 If @var{expr-1} is not empty, use it.  Otherwise, expand to
10543 @var{expr-2}.  Useful for providing a fixed default if the expression
10544 that results in @var{expr-1} would otherwise be empty.
10545 @end defmac
10547 @defmac m4_ifndef (@var{macro}, @var{if-not-defined}, @ovar{if-defined})
10548 @msindex{ifndef}
10549 This is shorthand for:
10550 @example
10551 m4_ifdef([@var{macro}], [@var{if-defined}], [@var{if-not-defined}])
10552 @end example
10553 @end defmac
10555 @defmac m4_ifset (@var{macro}, @ovar{if-true}, @ovar{if-false})
10556 @msindex{ifset}
10557 If @var{macro} is undefined, or is defined as the empty string, expand
10558 to @var{if-false}.  Otherwise, expands to @var{if-true}.  Similar to:
10559 @example
10560 m4_ifval(m4_defn([@var{macro}]), [@var{if-true}], [@var{if-false}])
10561 @end example
10562 @noindent
10563 except that it is not an error if @var{macro} is undefined.
10564 @end defmac
10566 @defmac m4_ifval (@var{cond}, @ovar{if-true}, @ovar{if-false})
10567 @msindex{ifval}
10568 Expands to @var{if-true} if @var{cond} is not empty, otherwise to
10569 @var{if-false}.  This is shorthand for:
10570 @example
10571 m4_if([@var{cond}], [], [@var{if-true}], [@var{if-false}])
10572 @end example
10573 @end defmac
10575 @defmac m4_ifvaln (@var{cond}, @ovar{if-true}, @ovar{if-false})
10576 @msindex{ifvaln}
10577 Similar to @code{m4_ifval}, except guarantee that a newline is present
10578 after any non-empty expansion.
10579 @end defmac
10581 @defmac m4_n (@var{text})
10582 @msindex{n}
10583 Expand to @var{text}, and add a newline if @var{text} is not empty.
10584 @end defmac
10587 @node Looping constructs
10588 @subsection Looping constructs
10590 The following macros are useful in implementing recursive algorithms in
10591 M4, including loop operations.  An M4 list is formed by quoting a list
10592 of quoted elements; generally the lists are comma-separated, although
10593 @code{m4_foreach_w} is whitespace-separated.  For example, the list
10594 @samp{[[a], [b,c]]} contains two elements: @samp{[a]} and @samp{[b,c]}.
10595 It is common to see lists with unquoted elements when those elements are
10596 not likely to be macro names, as in @samp{[fputc_unlocked,
10597 fgetc_unlocked]}.
10599 @defmac m4_car (@var{list})
10600 @msindex{car}
10601 Expands to the quoted first element of the comma-separated quoted
10602 @var{list}.  Often used with @code{m4_cdr} to recursively iterate
10603 through a list.  Generally, when using quoted lists of quoted elements,
10604 @code{m4_car} should be called without any extra quotes.
10605 @end defmac
10607 @defmac m4_cdr (@var{list})
10608 @msindex{cdr}
10609 Expands to a quoted list of all but the first element of the
10610 comma-separated quoted @var{list}, or the empty string if @var{list} had
10611 only one element.  Generally, when using quoted lists of quoted
10612 elements, @code{m4_cdr} should be called without any extra quotes.
10614 For example, this is a simple implementation of @code{m4_map}; note how
10615 each iteration checks for the end of recursion, then merely applies the
10616 first argument to the first element of the list, then recurses with the
10617 rest of the list.  (The actual implementation in M4sugar is a bit more
10618 involved, to gain some speed and share code with @code{m4_map_sep}).
10619 @example
10620 m4_define([m4_map], [m4_ifval([$2],
10621   [m4_apply([$1], m4_car($2))[]$0([$1], m4_cdr($2))])])dnl
10622 m4_map([ m4_eval], [[[1]], [[1+1]], [[10],[16]]])
10623 @result{} 1 2 a
10624 @end example
10625 @end defmac
10627 @defmac m4_for (@var{var}, @var{first}, @var{last}, @ovar{step}, @
10628   @var{expression})
10629 @msindex{for}
10630 Loop over the numeric values between @var{first} and @var{last}
10631 including bounds by increments of @var{step}.  For each iteration,
10632 expand @var{expression} with the numeric value assigned to @var{var}.
10633 If @var{step} is omitted, it defaults to @samp{1} or @samp{-1} depending
10634 on the order of the limits.  If given, @var{step} has to match this
10635 order.
10636 @end defmac
10638 @defmac m4_foreach (@var{var}, @var{list}, @var{expression})
10639 @msindex{foreach}
10640 Loop over the comma-separated M4 list @var{list}, assigning each value
10641 to @var{var}, and expand @var{expression}.  The following example
10642 outputs two lines:
10644 @example
10645 m4_foreach([myvar], [[foo], [bar, baz]],
10646            [echo myvar
10647 ])dnl
10648 @result{}echo foo
10649 @result{}echo bar, baz
10650 @end example
10651 @end defmac
10653 @anchor{m4_foreach_w}
10654 @defmac m4_foreach_w (@var{var}, @var{list}, @var{expression})
10655 @msindex{foreach_w}
10656 Loop over the white-space-separated list @var{list}, assigning each value
10657 to @var{var}, and expand @var{expression}.
10659 The deprecated macro @code{AC_FOREACH} is an alias of
10660 @code{m4_foreach_w}.
10661 @end defmac
10663 @defmac m4_map (@var{macro}, @var{list})
10664 @defmacx m4_map_sep (@var{macro}, @var{separator}, @var{list})
10665 Loop over the comma separated quoted list of argument descriptions in
10666 @var{list}, and invoke @var{macro} with the arguments.  An argument
10667 description is in turn a comma-separated quoted list of quoted elements,
10668 suitable for @code{m4_apply}, making it possible to invoke @var{macro}
10669 without arguments if an argument description is empty.
10670 @code{m4_map_sep} additionally outputs @var{separator} between macro
10671 invocations, with no additional expansion of the separator.
10672 @example
10673 m4_map([m4_count], [])
10674 @result{}
10675 m4_map([ m4_count], [[],
10676                      [[1]],
10677                      [[1], [2]]])
10678 @result{} 0 1 2
10679 m4_map_sep([m4_eval], [,], [[[1+2]],
10680                             [[10], [16]]])
10681 @result{}3,a
10682 @end example
10683 @end defmac
10685 @defmac m4_shiftn (@var{count}, @dots{})
10686 @defmacx m4_shift2 (@dots{})
10687 @defmacx m4_shift3 (@dots{})
10688 @msindex{shift2}
10689 @msindex{shift3}
10690 @msindex{shiftn}
10691 @code{m4_shiftn} performs @var{count} iterations of @code{m4_shift},
10692 along with validation that enough arguments were passed in to match the
10693 shift count, and that the count is positive.  @code{m4_shift2} and
10694 @code{m4_shift3} are specializations
10695 of @code{m4_shiftn}, introduced in Autoconf 2.62, and are more efficient
10696 for two and three shifts, respectively.
10697 @end defmac
10700 @node Evaluation Macros
10701 @subsection Evaluation Macros
10703 The following macros give some control over the order of the evaluation
10704 by adding or removing levels of quotes.
10706 @defmac m4_apply (@var{macro}, @var{list})
10707 @msindex apply
10708 Apply the elements of the quoted, comma-separated @var{list} as the
10709 arguments to @var{macro}.  If @var{list} is empty, invoke @var{macro}
10710 without arguments.
10711 @example
10712 m4_apply([m4_count], [])
10713 @result{}0
10714 m4_apply([m4_count], [[]])
10715 @result{}1
10716 m4_apply([m4_count], [[1], [2]])
10717 @result{}2
10718 m4_apply([m4_join], [[|], [1], [2]])
10719 @result{}1|2
10720 @end example
10721 @end defmac
10723 @defmac m4_count (@var{arg1}, @dots{})
10724 @msindex{count}
10725 This macro returns the decimal count of the number of arguments it was
10726 passed.
10727 @end defmac
10729 @defmac m4_do (@var{arg1}, @dots{})
10730 @msindex{do}
10731 This macro loops over its arguments and expands each @var{arg} in
10732 sequence.  Its main use is for readability; it allows the use of
10733 indentation and fewer @code{dnl} to result in the same expansion.
10734 @end defmac
10736 @defmac m4_dquote (@var{arg1}, @dots{})
10737 @msindex{dquote}
10738 Return the arguments as a quoted list of quoted arguments.
10739 Conveniently, if there is just one @var{arg}, this effectively adds a
10740 level of quoting.
10741 @end defmac
10743 @defmac m4_dquote_elt (@var{arg1}, @dots{})
10744 @msindex{dquote_elt}
10745 Return the arguments as a series of double-quoted arguments.  Whereas
10746 @code{m4_dquote} returns a single argument, @code{m4_dquote_elt} returns
10747 as many arguments as it was passed.
10748 @end defmac
10750 @defmac m4_echo (@var{arg1}, @dots{})
10751 @msindex{echo}
10752 Return the arguments, with the same level of quoting.  Other than
10753 discarding whitespace after unquoted commas, this macro is a no-op.
10754 @end defmac
10756 @defmac m4_expand (@var{arg})
10757 @msindex{expand}
10758 Return the expansion of @var{arg} as a quoted string.  Whereas
10759 @code{m4_quote} is designed to collect expanded text into a single
10760 argument, @code{m4_expand} is designed to perform one level of expansion
10761 on quoted text.  The distinction is in the treatment of whitespace
10762 following a comma in the original @var{arg}.  Any time multiple
10763 arguments are collected into one with @code{m4_quote}, the M4 argument
10764 collection rules discard the whitespace.  However, with @code{m4_expand},
10765 whitespace is discarded only if it results from unquoted commas in the
10766 expansion of macros contained in @var{arg}.
10768 @example
10769 m4_define([active], [ACT, IVE])dnl
10770 m4_define([active2], [[ACT, IVE]])dnl
10771 m4_quote(active, active)
10772 @result{}ACT,IVE,ACT,IVE
10773 m4_expand([active, active])
10774 @result{}ACT,IVE, ACT,IVE
10775 m4_quote(active2, active2)
10776 @result{}ACT, IVE,ACT, IVE
10777 m4_expand([active2, active2])
10778 @result{}ACT, IVE, ACT, IVE
10779 @end example
10780 @end defmac
10782 @defmac m4_ignore (@dots{})
10783 @msindex{ignore}
10784 This macro was introduced in Autoconf 2.62.  Expands to nothing,
10785 ignoring all of its arguments.  By itself, this isn't very useful.
10786 However, it can be used to conditionally ignore an arbitrary number of
10787 arguments, by making a decision about which macro name to apply to a
10788 list of arguments.
10789 @example
10790 dnl foo outputs a message only if [debug] is defined.
10791 m4_define([foo],
10792 [m4_ifdef([debug],[AC_MSG_NOTICE],[m4_ignore])([debug message])])
10793 @end example
10795 Note that for earlier versions of Autoconf, the macro @code{__gnu__} can
10796 serve the same purpose, although it is less readable.
10797 @end defmac
10799 @defmac m4_make_list (@var{arg1}, @dots{})
10800 @msindex{make_list}
10801 This macro exists to aid debugging of M4sugar algorithms.  Its net
10802 effect is similar to @code{m4_dquote}---it produces a quoted list of
10803 quoted arguments, for each @var{arg}.  The difference is that this
10804 version uses a comma-newline separator instead of just comma, to improve
10805 readability of the list; with the result that it is less efficient than
10806 @code{m4_dquote}.
10807 @example
10808 m4_define([zero],[0])m4_define([one],[1])m4_define([two],[2])dnl
10809 m4_dquote(zero, [one], [[two]])
10810 @result{}[0],[one],[[two]]
10811 m4_make_list(zero, [one], [[two]])
10812 @result{}[0],
10813 @result{}[one],
10814 @result{}[[two]]
10815 m4_foreach([number], m4_dquote(zero, [one], [[two]]), [ number])
10816 @result{} 0 1 two
10817 m4_foreach([number], m4_make_list(zero, [one], [[two]]), [ number])
10818 @result{} 0 1 two
10819 @end example
10820 @end defmac
10822 @c m4_noquote is too dangerous to document - it invokes macros that
10823 @c probably rely on @samp{[]} nested quoting for proper operation.  The
10824 @c user should generally prefer m4_unquote instead.
10826 @defmac m4_quote (@var{arg1}, @dots{})
10827 @msindex{quote}
10828 Return the arguments as a single entity, i.e., wrap them into a pair of
10829 quotes.  This effectively collapses multiple arguments into one,
10830 although it loses whitespace after unquoted commas in the process.
10831 @end defmac
10833 @defmac m4_unquote (@var{arg1}, @dots{})
10834 @msindex{unquote}
10835 This macro was introduced in Autoconf 2.62.  Expand each argument,
10836 separated by commas.  For a single @var{arg}, this effectively removes a
10837 layer of quoting, and @code{m4_unquote([@var{arg}])} is more efficient
10838 than the equivalent @code{m4_do([@var{arg}])}.  For multiple arguments,
10839 this results in an unquoted list of expansions.  This is commonly used
10840 with @code{m4_split}, in order to convert a single quoted list into a
10841 series of quoted elements.
10842 @end defmac
10844 The following example aims at emphasizing the difference between several
10845 scenarios: not using these macros, using @code{m4_defn}, using
10846 @code{m4_quote}, using @code{m4_dquote}, and using @code{m4_expand}.
10848 @example
10849 $ @kbd{cat example.m4}
10850 # Overquote, so that quotes are visible.
10851 m4_define([show], [$[]1 = [$1], $[]@@ = [$@@]])
10852 m4_define([a], [A])
10853 m4_define([mkargs], [1, 2[,] 3])
10854 m4_define([arg1], [[$1]])
10855 m4_divert([0])dnl
10856 show(a, b)
10857 show([a, b])
10858 show(m4_quote(a, b))
10859 show(m4_dquote(a, b))
10860 show(m4_expand([a, b]))
10862 arg1(mkargs)
10863 arg1([mkargs])
10864 arg1(m4_defn([mkargs]))
10865 arg1(m4_quote(mkargs))
10866 arg1(m4_dquote(mkargs))
10867 arg1(m4_expand([mkargs]))
10868 $ @kbd{autom4te -l m4sugar example.m4}
10869 $1 = A, $@@ = [A],[b]
10870 $1 = a, b, $@@ = [a, b]
10871 $1 = A,b, $@@ = [A,b]
10872 $1 = [A],[b], $@@ = [[A],[b]]
10873 $1 = A, b, $@@ = [A, b]
10876 mkargs
10877 1, 2[,] 3
10878 1,2, 3
10879 [1],[2, 3]
10880 1,2, 3
10881 @end example
10884 @node Text processing Macros
10885 @subsection String manipulation in M4
10887 The following macros may be used to manipulate strings in M4.
10888 They are not intended for casual use.
10890 @defmac m4_append (@var{macro-name}, @var{string}, @ovar{separator})
10891 @defmacx m4_append_uniq (@var{macro-name}, @var{string}, @ovar{separator} @
10892   @ovar{if-uniq}, @ovar{if-duplicate})
10893 @msindex{append}
10894 @msindex{append_uniq}
10895 Redefine @var{macro-name} to its former contents with @var{separator}
10896 and @var{string} added at the end.  If @var{macro-name} was undefined
10897 before (but not if it was defined but empty), then no @var{separator} is
10898 added.  As of Autoconf 2.62, neither @var{string} nor @var{separator}
10899 are expanded during this macro; instead, they are expanded when
10900 @var{macro-name} is invoked.
10902 @code{m4_append} can be used to grow strings, and @code{m4_append_uniq}
10903 to grow strings without duplicating substrings.  Additionally,
10904 @code{m4_append_uniq} takes two optional parameters as of Autoconf 2.62;
10905 @var{if-uniq} is expanded if @var{string} was appended, and
10906 @var{if-duplicate} is expanded if @var{string} was already present.
10908 @example
10909 m4_define([active], [ACTIVE])dnl
10910 m4_append([sentence], [This is an])dnl
10911 m4_append([sentence], [ active ])dnl
10912 m4_append([sentence], [symbol.])dnl
10913 sentence
10914 @result{}This is an ACTIVE symbol.
10915 m4_undefine([active])dnl
10916 @result{}This is an active symbol.
10917 m4_append_uniq([list], [one], [, ], [new], [existing])
10918 @result{}new
10919 m4_append_uniq([list], [one], [, ], [new], [existing])
10920 @result{}existing
10921 m4_append_uniq([list], [two], [, ], [new], [existing])
10922 @result{}new
10923 m4_append_uniq([list], [three], [, ], [new], [existing])
10924 @result{}new
10925 m4_append_uniq([list], [two], [, ], [new], [existing])
10926 @result{}existing
10927 list
10928 @result{}one, two, three
10929 m4_dquote(list)
10930 @result{}[one],[two],[three]
10931 m4_append([list2], [one], [[, ]])dnl
10932 m4_append_uniq([list2], [two], [[, ]])dnl
10933 m4_append([list2], [three], [[, ]])dnl
10934 list2
10935 @result{}one, two, three
10936 m4_dquote(list2)
10937 @result{}[one, two, three]
10938 @end example
10939 @end defmac
10941 @defmac m4_flatten (@var{string})
10942 @msindex{flatten}
10943 Flatten @var{string} into a single line.  Delete all backslash-newline
10944 pairs, and replace all remaining newlines with a space.  The result is
10945 still a quoted string.
10946 @end defmac
10948 @defmac m4_join (@ovar{separator}, @var{args}@dots{})
10949 @msindex{join}
10950 Concatenate each @var{arg}, separated by @var{separator}, with the
10951 exception that no back-to-back separators are issued for empty
10952 arguments.  The result is a quoted string.
10953 @example
10954 m4_define([active], [ACTIVE])dnl
10955 m4_join([|], [one], [], [active], [two])
10956 @result{}one|active|two
10957 @end example
10959 Note that if all you intend to do is join @var{args} with commas between
10960 them, to form a quoted list suitable for @code{m4_foreach}, it is more
10961 efficient to use @code{m4_dquote}.
10962 @end defmac
10964 @defmac m4_newline
10965 @msindex{newline}
10966 This macro was introduced in Autoconf 2.62, and expands to a newline.
10967 It is primarily useful for maintaining macro formatting, and ensuring
10968 that M4 does not discard leading whitespace during argument collection.
10969 @end defmac
10971 @defmac m4_normalize (@var{string})
10972 @msindex{normalize}
10973 Remove leading and trailing spaces and tabs, sequences of
10974 backslash-then-newline, and replace multiple spaces, tabs, and newlines
10975 with a single space.  This is a combination of @code{m4_flatten} and
10976 @code{m4_strip}.
10977 @end defmac
10979 @defmac m4_re_escape (@var{string})
10980 @msindex{re_escape}
10981 Backslash-escape all characters in @var{string} that are active in
10982 regexps.
10983 @end defmac
10985 @defmac m4_split (@var{string}, @dvar{regexp, [\t ]+})
10986 @msindex{split}
10987 Split @var{string} into an M4 list of elements quoted by @samp{[} and
10988 @samp{]}, while keeping white space at the beginning and at the end.
10989 If @var{regexp} is given, use it instead of @samp{[\t ]+} for splitting.
10990 If @var{string} is empty, the result is an empty list.
10991 @end defmac
10993 @defmac m4_strip (@var{string})
10994 @msindex{strip}
10995 Strip whitespace from @var{string}.  Sequences of space and tab are
10996 reduced to a single space, then leading and trailing spaces are
10997 removed.  The result is still a quoted string.
10998 @end defmac
11000 @defmac m4_text_box (@var{message}, @dvar{frame, -})
11001 @msindex{text_box}
11002 Add a text box around @var{message}, using @var{frame} as the border
11003 character above and below the message.  The frame correctly accounts for
11004 the subsequent expansion of @var{message}.  For example:
11005 @example
11006 m4_define([macro], [abc])dnl
11007 m4_text_box([macro])
11008 @result{}## --- ##
11009 @result{}## abc ##
11010 @result{}## --- ##
11011 @end example
11012 @end defmac
11014 @defmac m4_text_wrap (@var{string}, @ovar{prefix}, @
11015   @dvar{prefix1, @var{prefix}}, @dvar{width, 79})
11016 @msindex{text_wrap}
11017 Break @var{string} into a series of whitespace-separated words, then
11018 format those words to wrap within @var{width} columns, and without
11019 trailing whitespace.  If given, @var{prefix1} is prepended to the first
11020 line, and @var{prefix} is prepended to each continuation line.  As a
11021 special case, if @var{prefix1} is longer than @var{prefix}, the first
11022 line will consist solely of @var{prefix1}.  No expansions occur on
11023 @var{prefix}, @var{prefix1}, or the words of @var{string}, although
11024 quadrigraphs are recognized.
11026 For some examples:
11027 @example
11028 m4_text_wrap([Short string */], [   ], [/* ], [20])
11029 @result{}/* Short string */
11030 m4_text_wrap([Much longer string */], [   ], [/* ], [20])
11031 @result{}/* Much longer
11032 @result{}   string */
11033 m4_text_wrap([Short doc.], [          ], [  --short ], [30])
11034 @result{}  --short Short doc.
11035 m4_text_wrap([Short doc.], [          ], [  --too-wide ], [30])
11036 @result{}  --too-wide
11037 @result{}          Short doc.
11038 m4_text_wrap([Super long documentation.], [     ],
11039              [  --too-wide ], 30)
11040 @result{}  --too-wide
11041 @result{}     Super long
11042 @result{}     documentation.
11043 @end example
11044 @end defmac
11046 @defmac m4_tolower (@var{string})
11047 @defmacx m4_toupper (@var{string})
11048 @msindex{tolower}
11049 @msindex{toupper}
11050 Return @var{string} with letters converted to upper or lower case,
11051 respectively.
11052 @end defmac
11054 @node Number processing Macros
11055 @subsection Arithmetic computation in M4
11057 The following macros make it easier to deal with arithmetic operations.
11058 Where a parameter is documented as taking an arithmetic expression, you
11059 can use anything that can be parsed by @code{m4_eval}.
11061 @defmac m4_cmp (@var{expr-1}, @var{expr-2})
11062 @msindex{cmp}
11063 Compare the arithmetic expressions @var{expr-1} and @var{expr-2}, and
11064 expand to @samp{-1} if @var{expr-1} is smaller, @samp{0} if they are
11065 equal, and @samp{1} if @var{expr-1} is larger.
11066 @end defmac
11068 @defmac m4_list_cmp (@var{list-1}, @var{list-2})
11069 @msindex{list_cmp}
11070 Compare the two M4 lists consisting of comma-separated arithmetic
11071 expressions, left to right.  Expand to @samp{-1} for the first element
11072 pairing where the value from @var{list-1} is smaller, @samp{1} where the
11073 value from @var{list-2} is smaller, or @samp{0} if both lists have the
11074 same values.  If one list is shorter than the other, the remaining
11075 elements of the longer list are compared against 0.
11076 @example
11077 m4_list_cmp([1, 0],       [1])
11078 @result{}0
11079 m4_list_cmp([1, [1 * 0]], [1, 0])
11080 @result{}0
11081 m4_list_cmp([1, 2],       [1, 0])
11082 @result{}1
11083 m4_list_cmp([1, [1+1], 3],[1, 2])
11084 @result{}1
11085 m4_list_cmp([1, 2, -3],   [1, 2])
11086 @result{}-1
11087 m4_list_cmp([1, 0],       [1, 2])
11088 @result{}-1
11089 m4_list_cmp([1],          [1, 2])
11090 @result{}-1
11091 @end example
11092 @end defmac
11094 @defmac m4_max (@var{arg1}, @dots{})
11095 @msindex{max}
11096 This macro was introduced in Autoconf 2.62.  Expand to the decimal value
11097 of the maximum arithmetic expression among all the arguments.
11098 @end defmac
11100 @defmac m4_min (@var{arg1}, @dots{})
11101 @msindex{min}
11102 This macro was introduced in Autoconf 2.62.  Expand to the decimal value
11103 of the minimum arithmetic expression among all the arguments.
11104 @end defmac
11106 @defmac m4_sign (@var{expr})
11107 @msindex{sign}
11108 Expand to @samp{-1} if the arithmetic expression @var{expr} is negative,
11109 @samp{1} if it is positive, and @samp{0} if it is zero.
11110 @end defmac
11112 @anchor{m4_version_compare}
11113 @defmac m4_version_compare (@var{version-1}, @var{version-2})
11114 @msindex{version_compare}
11115 This macro was introduced in Autoconf 2.53, but had a number of
11116 usability limitations that were not lifted until Autoconf 2.62.  Compare
11117 the version strings @var{version-1} and @var{version-2}, and expand to
11118 @samp{-1} if @var{version-1} is smaller, @samp{0} if they are the same,
11119 or @samp{1} @var{version-2} is smaller.  Version strings must be a list
11120 of elements separated by @samp{.}, where each element is a number along
11121 with optional case-insensitive letters designating beta releases.  The
11122 comparison stops at the leftmost element that contains a difference,
11123 although a 0 element compares equal to a missing element.
11125 @example
11126 m4_version_compare([1.1], [2.0])
11127 @result{}-1
11128 m4_version_compare([2.0b], [2.0a])
11129 @result{}1
11130 m4_version_compare([1.1.1], [1.1.1a])
11131 @result{}-1
11132 m4_version_compare([1.2], [1.1.1a])
11133 @result{}1
11134 m4_version_compare([1.0], [1])
11135 @result{}0
11136 m4_version_compare([1.1pre], [1.1PRE])
11137 @result{}0
11138 m4_version_compare([1.1a], [1,10])
11139 @result{}-1
11140 @end example
11141 @end defmac
11144 @node Forbidden Patterns
11145 @subsection Forbidden Patterns
11146 @cindex Forbidden patterns
11147 @cindex Patterns, forbidden
11149 M4sugar provides a means to define suspicious patterns, patterns
11150 describing tokens which should not be found in the output.  For
11151 instance, if an Autoconf @file{configure} script includes tokens such as
11152 @samp{AC_DEFINE}, or @samp{dnl}, then most probably something went
11153 wrong (typically a macro was not evaluated because of overquotation).
11155 M4sugar forbids all the tokens matching @samp{^_?m4_} and @samp{^dnl$}.
11156 Additional layers, such as M4sh and Autoconf, add additional forbidden
11157 patterns to the list.
11159 @defmac m4_pattern_forbid (@var{pattern})
11160 @msindex{pattern_forbid}
11161 Declare that no token matching @var{pattern} must be found in the output.
11162 Comments are not checked; this can be a problem if, for instance, you
11163 have some macro left unexpanded after an @samp{#include}.  No consensus
11164 is currently found in the Autoconf community, as some people consider it
11165 should be valid to name macros in comments (which doesn't make sense to
11166 the authors of this documentation: input, such as macros, should be
11167 documented by @samp{dnl} comments; reserving @samp{#}-comments to
11168 document the output).
11169 @end defmac
11171 Of course, you might encounter exceptions to these generic rules, for
11172 instance you might have to refer to @samp{$m4_flags}.
11174 @defmac m4_pattern_allow (@var{pattern})
11175 @msindex{pattern_allow}
11176 Any token matching @var{pattern} is allowed, including if it matches an
11177 @code{m4_pattern_forbid} pattern.
11178 @end defmac
11180 @node Programming in M4sh
11181 @section Programming in M4sh
11183 @c FIXME: Eventually will become a chapter, as it is not related to
11184 @c programming in M4 per se.
11186 M4sh, pronounced ``mash'', is aiming at producing portable Bourne shell
11187 scripts.  This name was coined by Lars J. Aas, who notes that,
11188 according to the Webster's Revised Unabridged Dictionary (1913):
11190 @quotation
11191 Mash \Mash\, n.  [Akin to G. meisch, maisch, meische, maische, mash,
11192 wash, and prob.@: to AS. miscian to mix.  See ``Mix''.]
11194 @enumerate 1
11195 @item
11196 A mass of mixed ingredients reduced to a soft pulpy state by beating or
11197 pressure@enddots{}
11199 @item
11200 A mixture of meal or bran and water fed to animals.
11202 @item
11203 A mess; trouble.  [Obs.] --Beau.@: & Fl.
11204 @end enumerate
11205 @end quotation
11208 For the time being, it is not mature enough to be widely used.
11210 M4sh reserves the M4 macro namespace @samp{^_AS_} for internal use, and
11211 the namespace @samp{^AS_} for M4sh macros.  It also reserves the shell
11212 and environment variable namespace @samp{^as_}, and the here-doc
11213 delimiter namespace @samp{^_AS[A-Z]} in the output file.  You should not
11214 define your own macros or output shell code that conflicts with these
11215 namespaces.
11217 M4sh provides portable alternatives for some common shell constructs
11218 that unfortunately are not portable in practice.
11220 @c Deprecated, to be replaced by a better API
11221 @ignore
11222 @defmac AS_BASENAME (@var{file-name})
11223 @asindex{BASENAME}
11224 Output the non-directory portion of @var{file-name}.  For example,
11225 if @code{$file} is @samp{/one/two/three}, the command
11226 @code{base=`AS_BASENAME(["$file"])`} sets @code{base} to @samp{three}.
11227 @end defmac
11228 @end ignore
11230 @defmac AS_BOURNE_COMPATIBLE
11231 @asindex{BOURNE_COMPATIBLE}
11232 Set up the shell to be more compatible with the Bourne shell as
11233 standardized by Posix, if possible.  This may involve setting
11234 environment variables, or setting options, or similar
11235 implementation-specific actions.
11236 @end defmac
11238 @defmac AS_CASE (@var{word}, @ovar{pattern1}, @ovar{if-matched1}, @
11239   @dots{}, @ovar{default})
11240 @asindex{CASE}
11241 Expand into a shell @samp{case} statement, where @var{word} is matched
11242 against one or more patterns.  @var{if-matched} is run if the
11243 corresponding pattern matched @var{word}, else @var{default} is run.
11244 @end defmac
11246 @defmac AS_DIRNAME (@var{file-name})
11247 @asindex{DIRNAME}
11248 Output the directory portion of @var{file-name}.  For example,
11249 if @code{$file} is @samp{/one/two/three}, the command
11250 @code{dir=`AS_DIRNAME(["$file"])`} sets @code{dir} to @samp{/one/two}.
11251 @end defmac
11253 @defmac AS_IF (@var{test1}, @ovar{run-if-true1}, @dots{}, @ovar{run-if-false})
11254 @asindex{IF}
11255 Run shell code @var{test1}.  If @var{test1} exits with a zero status then
11256 run shell code @var{run-if-true1}, else examine further tests.  If no test
11257 exits with a zero status, run shell code @var{run-if-false}, with
11258 simplifications if either @var{run-if-true1} or @var{run-if-false1}
11259 is empty.  For example,
11261 @example
11262 AS_IF([test "$foo" = yes], [HANDLE_FOO([yes])],
11263       [test "$foo" != no], [HANDLE_FOO([maybe])],
11264       [echo foo not specified])
11265 @end example
11267 @noindent
11268 ensures any required macros of @code{HANDLE_FOO}
11269 are expanded before the first test.
11270 @end defmac
11272 @defmac AS_MKDIR_P (@var{file-name})
11273 @asindex{MKDIR_P}
11274 Make the directory @var{file-name}, including intervening directories
11275 as necessary.  This is equivalent to @samp{mkdir -p @var{file-name}},
11276 except that it is portable to older versions of @command{mkdir} that
11277 lack support for the @option{-p} option.  Also, @code{AS_MKDIR_P}
11278 succeeds if @var{file-name} is a symbolic link to an existing directory,
11279 even though Posix is unclear whether @samp{mkdir -p} should
11280 succeed in that case.  If creation of @var{file-name} fails, exit the
11281 script.
11283 Also see the @code{AC_PROG_MKDIR_P} macro (@pxref{Particular Programs}).
11284 @end defmac
11286 @defmac AS_SHELL_SANITIZE
11287 @asindex{SHELL_SANITIZE}
11288 Initialize the shell suitably for @code{configure} scripts.  This has
11289 the effect of @code{AS_BOURNE_COMPATIBLE}, and sets some other
11290 environment variables for predictable results from configuration tests.
11291 For example, it sets @env{LC_ALL} to change to the default C locale.
11292 @xref{Special Shell Variables}.
11293 @end defmac
11295 @defmac AS_TR_CPP (@var{expression})
11296 @asindex{TR_CPP}
11297 Transform @var{expression} into a valid right-hand side for a C @code{#define}.
11298 For example:
11300 @example
11301 # This outputs "#define HAVE_CHAR_P 1".
11302 type="char *"
11303 echo "#define AS_TR_CPP([HAVE_$type]) 1"
11304 @end example
11305 @end defmac
11307 @defmac AS_TR_SH (@var{expression})
11308 @asindex{TR_SH}
11309 Transform @var{expression} into a valid shell variable name.  For example:
11311 @example
11312 # This outputs "Have it!".
11313 header="sys/some file.h"
11314 AS_TR_SH([HAVE_$header])=yes
11315 if test "$HAVE_sys_some_file_h" = yes; then echo "Have it!"; fi
11316 @end example
11317 @end defmac
11319 @defmac AS_SET_CATFILE (@var{var}, @var{dir}, @var{file})
11320 @asindex{SET_CATFILE}
11321 Set the shell variable @var{var} to @var{dir}/@var{file}, but
11322 optimizing the common cases (@var{dir} or @var{file} is @samp{.},
11323 @var{file} is absolute, etc.).
11324 @end defmac
11327 @node File Descriptor Macros
11328 @section File Descriptor Macros
11329 @cindex input
11330 @cindex standard input
11331 @cindex file descriptors
11332 @cindex descriptors
11333 @cindex low-level output
11334 @cindex output, low-level
11336 The following macros define file descriptors used to output messages
11337 (or input values) from @file{configure} scripts.
11338 For example:
11340 @example
11341 echo "$wombats found" >&AS_MESSAGE_LOG_FD
11342 echo 'Enter desired kangaroo count:' >&AS_MESSAGE_FD
11343 read kangaroos <&AS_ORIGINAL_STDIN_FD`
11344 @end example
11346 @noindent
11347 However doing so is seldom needed, because Autoconf provides higher
11348 level macros as described below.
11350 @defmac AS_MESSAGE_FD
11351 @asindex{MESSAGE_FD}
11352 The file descriptor for @samp{checking for...}  messages and results.
11353 Normally this directs messages to the standard output, however when
11354 @command{configure} is run with the @option{-q} option, messages sent to
11355 @code{AS_MESSAGE_FD} are discarded.
11357 If you want to display some messages, consider using one of the printing
11358 macros (@pxref{Printing Messages}) instead.  Copies of messages output
11359 via these macros are also recorded in @file{config.log}.
11360 @end defmac
11362 @defmac AS_MESSAGE_LOG_FD
11363 @asindex{MESSAGE_LOG_FD}
11365 The file descriptor for messages logged to @file{config.log}.  Macros
11366 that run tools, like @code{AC_COMPILE_IFELSE} (@pxref{Running the
11367 Compiler}), redirect all output to this descriptor.  You may want to do
11368 so if you develop such a low-level macro.
11369 @end defmac
11371 @defmac AS_ORIGINAL_STDIN_FD
11372 @asindex{ORIGINAL_STDIN_FD}
11373 The file descriptor for the original standard input.
11375 When @command{configure} runs, it may accidentally execute an
11376 interactive command that has the same name as the non-interactive meant
11377 to be used or checked.  If the standard input was the terminal, such
11378 interactive programs would cause @command{configure} to stop, pending
11379 some user input.  Therefore @command{configure} redirects its standard
11380 input from @file{/dev/null} during its initialization.  This is not
11381 normally a problem, since @command{configure} normally does not need
11382 user input.
11384 In the extreme case where your @file{configure} script really needs to
11385 obtain some values from the original standard input, you can read them
11386 explicitly from @code{AS_ORIGINAL_STDIN_FD}.
11387 @end defmac
11390 @c =================================================== Writing Autoconf Macros.
11392 @node Writing Autoconf Macros
11393 @chapter Writing Autoconf Macros
11395 When you write a feature test that could be applicable to more than one
11396 software package, the best thing to do is encapsulate it in a new macro.
11397 Here are some instructions and guidelines for writing Autoconf macros.
11399 @menu
11400 * Macro Definitions::           Basic format of an Autoconf macro
11401 * Macro Names::                 What to call your new macros
11402 * Reporting Messages::          Notifying @command{autoconf} users
11403 * Dependencies Between Macros::  What to do when macros depend on other macros
11404 * Obsoleting Macros::           Warning about old ways of doing things
11405 * Coding Style::                Writing Autoconf macros @`a la Autoconf
11406 @end menu
11408 @node Macro Definitions
11409 @section Macro Definitions
11411 @acindex{DEFUN}
11412 Autoconf macros are defined using the @code{AC_DEFUN} macro, which is
11413 similar to the M4 builtin @code{m4_define} macro.  In addition to
11414 defining a macro, @code{AC_DEFUN} adds to it some code that is used to
11415 constrain the order in which macros are called (@pxref{Prerequisite
11416 Macros}).
11418 An Autoconf macro definition looks like this:
11420 @example
11421 AC_DEFUN(@var{macro-name}, @var{macro-body})
11422 @end example
11424 You can refer to any arguments passed to the macro as @samp{$1},
11425 @samp{$2}, etc.  @xref{Definitions, , How to define new macros, m4.info,
11426 @acronym{GNU} M4}, for more complete information on writing M4 macros.
11428 Be sure to properly quote both the @var{macro-body} @emph{and} the
11429 @var{macro-name} to avoid any problems if the macro happens to have
11430 been previously defined.
11432 Each macro should have a header comment that gives its prototype, and a
11433 brief description.  When arguments have default values, display them in
11434 the prototype.  For example:
11436 @example
11437 # AC_MSG_ERROR(ERROR, [EXIT-STATUS = 1])
11438 # --------------------------------------
11439 m4_define([AC_MSG_ERROR],
11440   [@{ AS_MESSAGE([error: $1], [2])
11441      exit m4_default([$2], [1]); @}])
11442 @end example
11444 Comments about the macro should be left in the header comment.  Most
11445 other comments make their way into @file{configure}, so just keep
11446 using @samp{#} to introduce comments.
11448 @cindex @code{dnl}
11449 If you have some special comments about pure M4 code, comments
11450 that make no sense in @file{configure} and in the header comment, then
11451 use the builtin @code{dnl}: it causes M4 to discard the text
11452 through the next newline.
11454 Keep in mind that @code{dnl} is rarely needed to introduce comments;
11455 @code{dnl} is more useful to get rid of the newlines following macros
11456 that produce no output, such as @code{AC_REQUIRE}.
11459 @node Macro Names
11460 @section Macro Names
11462 All of the public Autoconf macros have all-uppercase names in the
11463 namespace @samp{^AC_} to prevent them from accidentally conflicting with
11464 other text; Autoconf also reserves the namespace @samp{^_AC_} for
11465 internal macros.  All shell variables that they use for internal
11466 purposes have mostly-lowercase names starting with @samp{ac_}.  Autoconf
11467 also uses here-doc delimiters in the namespace @samp{^_AC[A-Z]}.  During
11468 @command{configure}, files produced by Autoconf make heavy use of the
11469 file system namespace @samp{^conf}.
11471 Since Autoconf is built on top of M4sugar (@pxref{Programming in
11472 M4sugar}) and M4sh (@pxref{Programming in M4sh}), you must also be aware
11473 of those namespaces (@samp{^_?\(m4\|AS\)_}).  And since
11474 @file{configure.ac} is also designed to be scanned by Autoheader,
11475 Autoscan, Autoupdate, and Automake, you should be aware of the
11476 @samp{^_?A[HNUM]_} namespaces.  In general, you @emph{should not use}
11477 the namespace of a package that does not own the macro or shell code you
11478 are writing.
11480 To ensure that your macros don't conflict with present or future
11481 Autoconf macros, you should prefix your own macro names and any shell
11482 variables they use with some other sequence.  Possibilities include your
11483 initials, or an abbreviation for the name of your organization or
11484 software package.  Historically, people have not always followed the
11485 rule of using a namespace appropriate for their package, and this has
11486 made it difficult for determining the origin of a macro (and where to
11487 report bugs about that macro), as well as difficult for the true
11488 namespace owner to add new macros without interference from pre-existing
11489 uses of third-party macros.  Perhaps the best example of this confusion
11490 is the @code{AM_GNU_GETTEXT} macro, which belongs, not to Automake, but
11491 to Gettext.
11493 Most of the Autoconf macros' names follow a structured naming convention
11494 that indicates the kind of feature check by the name.  The macro names
11495 consist of several words, separated by underscores, going from most
11496 general to most specific.  The names of their cache variables use the
11497 same convention (@pxref{Cache Variable Names}, for more information on
11498 them).
11500 The first word of the name after the namepace initials (such as
11501 @samp{AC_}) usually tells the category
11502 of the feature being tested.  Here are the categories used in Autoconf for
11503 specific test macros, the kind of macro that you are more likely to
11504 write.  They are also used for cache variables, in all-lowercase.  Use
11505 them where applicable; where they're not, invent your own categories.
11507 @table @code
11508 @item C
11509 C language builtin features.
11510 @item DECL
11511 Declarations of C variables in header files.
11512 @item FUNC
11513 Functions in libraries.
11514 @item GROUP
11515 Posix group owners of files.
11516 @item HEADER
11517 Header files.
11518 @item LIB
11519 C libraries.
11520 @item PROG
11521 The base names of programs.
11522 @item MEMBER
11523 Members of aggregates.
11524 @item SYS
11525 Operating system features.
11526 @item TYPE
11527 C builtin or declared types.
11528 @item VAR
11529 C variables in libraries.
11530 @end table
11532 After the category comes the name of the particular feature being
11533 tested.  Any further words in the macro name indicate particular aspects
11534 of the feature.  For example, @code{AC_PROG_CC_STDC} checks whether the
11535 C compiler supports @acronym{ISO} Standard C.
11537 An internal macro should have a name that starts with an underscore;
11538 Autoconf internals should therefore start with @samp{_AC_}.
11539 Additionally, a macro that is an internal subroutine of another macro
11540 should have a name that starts with an underscore and the name of that
11541 other macro, followed by one or more words saying what the internal
11542 macro does.  For example, @code{AC_PATH_X} has internal macros
11543 @code{_AC_PATH_X_XMKMF} and @code{_AC_PATH_X_DIRECT}.
11545 @node Reporting Messages
11546 @section Reporting Messages
11547 @cindex Messages, from @command{autoconf}
11549 When macros statically diagnose abnormal situations, benign or fatal, it
11550 is possible to make @command{autoconf} detect the problem, and refuse to
11551 create @file{configure} in the case of an error.  The macros in this
11552 section are considered obsolescent, and new code should use M4sugar
11553 macros for this purpose, see @ref{Diagnostic Macros}.
11555 On the other hand, it is possible to want to detect errors when
11556 @command{configure} is run, which are dependent on the environment of
11557 the user rather than the maintainer.  For dynamic diagnostics, see
11558 @ref{Printing Messages}.
11560 @defmac AC_DIAGNOSE (@var{category}, @var{message})
11561 @acindex{DIAGNOSE}
11562 Report @var{message} as a warning (or as an error if requested by the
11563 user) if warnings of the @var{category} are turned on.  This macro is
11564 obsolescent; you are encouraged to use:
11565 @example
11566 m4_warn([@var{category}], [@var{message}])
11567 @end example
11568 @noindent
11569 instead.  @xref{m4_warn}, for more details, including valid
11570 @var{category} names.
11571 @end defmac
11573 @defmac AC_WARNING (@var{message})
11574 @acindex{WARNING}
11575 Report @var{message} as a syntax warning.  This macro is obsolescent;
11576 you are encouraged to use:
11577 @example
11578 m4_warn([syntax], [@var{message}])
11579 @end example
11580 @noindent
11581 instead.  @xref{m4_warn}, for more details, as well as better
11582 finer-grained categories of warnings (not all problems have to do with
11583 syntax).
11584 @end defmac
11586 @defmac AC_FATAL (@var{message})
11587 @acindex{FATAL}
11588 Report a severe error @var{message}, and have @command{autoconf} die.
11589 This macro is obsolescent; you are encouraged to use:
11590 @example
11591 m4_fatal([@var{message}])
11592 @end example
11593 @noindent
11594 instead.  @xref{m4_fatal}, for more details.
11595 @end defmac
11597 When the user runs @samp{autoconf -W error}, warnings from
11598 @code{m4_warn} (including those issued through @code{AC_DIAGNOSE} and
11599 @code{AC_WARNING}) are reported as errors, see @ref{autoconf Invocation}.
11601 @node Dependencies Between Macros
11602 @section Dependencies Between Macros
11603 @cindex Dependencies between macros
11605 Some Autoconf macros depend on other macros having been called first in
11606 order to work correctly.  Autoconf provides a way to ensure that certain
11607 macros are called if needed and a way to warn the user if macros are
11608 called in an order that might cause incorrect operation.
11610 @menu
11611 * Prerequisite Macros::         Ensuring required information
11612 * Suggested Ordering::          Warning about possible ordering problems
11613 * One-Shot Macros::             Ensuring a macro is called only once
11614 @end menu
11616 @node Prerequisite Macros
11617 @subsection Prerequisite Macros
11618 @cindex Prerequisite macros
11619 @cindex Macros, prerequisites
11621 A macro that you write might need to use values that have previously
11622 been computed by other macros.  For example, @code{AC_DECL_YYTEXT}
11623 examines the output of @code{flex} or @code{lex}, so it depends on
11624 @code{AC_PROG_LEX} having been called first to set the shell variable
11625 @code{LEX}.
11627 Rather than forcing the user of the macros to keep track of the
11628 dependencies between them, you can use the @code{AC_REQUIRE} macro to do
11629 it automatically.  @code{AC_REQUIRE} can ensure that a macro is only
11630 called if it is needed, and only called once.
11632 @defmac AC_REQUIRE (@var{macro-name})
11633 @acindex{REQUIRE}
11634 If the M4 macro @var{macro-name} has not already been called, call it
11635 (without any arguments).  Make sure to quote @var{macro-name} with
11636 square brackets.  @var{macro-name} must have been defined using
11637 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
11638 that it has been called.
11640 @code{AC_REQUIRE} must be used inside a macro defined by @code{AC_DEFUN}; it
11641 must not be called from the top level.
11642 @end defmac
11644 @code{AC_REQUIRE} is often misunderstood.  It really implements
11645 dependencies between macros in the sense that if one macro depends upon
11646 another, the latter is expanded @emph{before} the body of the
11647 former.  To be more precise, the required macro is expanded before
11648 the outermost defined macro in the current expansion stack.
11649 In particular, @samp{AC_REQUIRE([FOO])} is not replaced with the body of
11650 @code{FOO}.  For instance, this definition of macros:
11652 @example
11653 @group
11654 AC_DEFUN([TRAVOLTA],
11655 [test "$body_temperature_in_celsius" -gt "38" &&
11656   dance_floor=occupied])
11657 AC_DEFUN([NEWTON_JOHN],
11658 [test "$hair_style" = "curly" &&
11659   dance_floor=occupied])
11660 @end group
11662 @group
11663 AC_DEFUN([RESERVE_DANCE_FLOOR],
11664 [if date | grep '^Sat.*pm' >/dev/null 2>&1; then
11665   AC_REQUIRE([TRAVOLTA])
11666   AC_REQUIRE([NEWTON_JOHN])
11667 fi])
11668 @end group
11669 @end example
11671 @noindent
11672 with this @file{configure.ac}
11674 @example
11675 AC_INIT([Dance Manager], [1.0], [bug-dance@@example.org])
11676 RESERVE_DANCE_FLOOR
11677 if test "$dance_floor" = occupied; then
11678   AC_MSG_ERROR([cannot pick up here, let's move])
11680 @end example
11682 @noindent
11683 does not leave you with a better chance to meet a kindred soul at
11684 other times than Saturday night since it expands into:
11686 @example
11687 @group
11688 test "$body_temperature_in_Celsius" -gt "38" &&
11689   dance_floor=occupied
11690 test "$hair_style" = "curly" &&
11691   dance_floor=occupied
11693 if date | grep '^Sat.*pm' >/dev/null 2>&1; then
11697 @end group
11698 @end example
11700 This behavior was chosen on purpose: (i) it prevents messages in
11701 required macros from interrupting the messages in the requiring macros;
11702 (ii) it avoids bad surprises when shell conditionals are used, as in:
11704 @example
11705 @group
11706 if @dots{}; then
11707   AC_REQUIRE([SOME_CHECK])
11709 @dots{}
11710 SOME_CHECK
11711 @end group
11712 @end example
11714 The helper macros @code{AS_IF} and @code{AS_CASE} may be used to
11715 enforce expansion of required macros outside of shell conditional
11716 constructs.  You are furthermore encouraged to put all @code{AC_REQUIRE} calls
11717 at the beginning of a macro.  You can use @code{dnl} to avoid the empty
11718 lines they leave.
11720 @node Suggested Ordering
11721 @subsection Suggested Ordering
11722 @cindex Macros, ordering
11723 @cindex Ordering macros
11725 Some macros should be run before another macro if both are called, but
11726 neither @emph{requires} that the other be called.  For example, a macro
11727 that changes the behavior of the C compiler should be called before any
11728 macros that run the C compiler.  Many of these dependencies are noted in
11729 the documentation.
11731 Autoconf provides the @code{AC_BEFORE} macro to warn users when macros
11732 with this kind of dependency appear out of order in a
11733 @file{configure.ac} file.  The warning occurs when creating
11734 @command{configure} from @file{configure.ac}, not when running
11735 @command{configure}.
11737 For example, @code{AC_PROG_CPP} checks whether the C compiler
11738 can run the C preprocessor when given the @option{-E} option.  It should
11739 therefore be called after any macros that change which C compiler is
11740 being used, such as @code{AC_PROG_CC}.  So @code{AC_PROG_CC} contains:
11742 @example
11743 AC_BEFORE([$0], [AC_PROG_CPP])dnl
11744 @end example
11746 @noindent
11747 This warns the user if a call to @code{AC_PROG_CPP} has already occurred
11748 when @code{AC_PROG_CC} is called.
11750 @defmac AC_BEFORE (@var{this-macro-name}, @var{called-macro-name})
11751 @acindex{BEFORE}
11752 Make M4 print a warning message to the standard error output if
11753 @var{called-macro-name} has already been called.  @var{this-macro-name}
11754 should be the name of the macro that is calling @code{AC_BEFORE}.  The
11755 macro @var{called-macro-name} must have been defined using
11756 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
11757 that it has been called.
11758 @end defmac
11760 @node One-Shot Macros
11761 @subsection One-Shot Macros
11762 @cindex One-shot macros
11763 @cindex Macros, called once
11765 Some macros should be called only once, either because calling them
11766 multiple time is unsafe, or because it is bad style.  For instance
11767 Autoconf ensures that @code{AC_CANONICAL_BUILD} and cousins
11768 (@pxref{Canonicalizing}) are evaluated only once, because it makes no
11769 sense to run these expensive checks more than once.  Such one-shot
11770 macros can be defined using @code{AC_DEFUN_ONCE}.
11772 @defmac AC_DEFUN_ONCE (@var{macro-name}, @var{macro-body})
11773 @acindex{DEFUN_ONCE}
11775 Declare macro @var{macro-name} like @code{AC_DEFUN} would (@pxref{Macro
11776 Definitions}), and emit a warning any time the macro is called more than
11777 once.
11778 @end defmac
11780 Obviously it is not sensible to evaluate a macro defined by
11781 @code{AC_DEFUN_ONCE} in a macro defined by @code{AC_DEFUN}.
11782 Most of the time you want to use @code{AC_REQUIRE} (@pxref{Prerequisite
11783 Macros}).
11785 @node Obsoleting Macros
11786 @section Obsoleting Macros
11787 @cindex Obsoleting macros
11788 @cindex Macros, obsoleting
11790 Configuration and portability technology has evolved over the years.
11791 Often better ways of solving a particular problem are developed, or
11792 ad-hoc approaches are systematized.  This process has occurred in many
11793 parts of Autoconf.  One result is that some of the macros are now
11794 considered @dfn{obsolete}; they still work, but are no longer considered
11795 the best thing to do, hence they should be replaced with more modern
11796 macros.  Ideally, @command{autoupdate} should replace the old macro calls
11797 with their modern implementation.
11799 Autoconf provides a simple means to obsolete a macro.
11801 @anchor{AU_DEFUN}
11802 @defmac AU_DEFUN (@var{old-macro}, @var{implementation}, @ovar{message})
11803 @auindex{DEFUN}
11804 Define @var{old-macro} as @var{implementation}.  The only difference
11805 with @code{AC_DEFUN} is that the user is warned that
11806 @var{old-macro} is now obsolete.
11808 If she then uses @command{autoupdate}, the call to @var{old-macro} is
11809 replaced by the modern @var{implementation}.  @var{message} should
11810 include information on what to do after running @command{autoupdate};
11811 @command{autoupdate} prints it as a warning, and includes it
11812 in the updated @file{configure.ac} file.
11814 The details of this macro are hairy: if @command{autoconf} encounters an
11815 @code{AU_DEFUN}ed macro, all macros inside its second argument are expanded
11816 as usual.  However, when @command{autoupdate} is run, only M4 and M4sugar
11817 macros are expanded here, while all other macros are disabled and
11818 appear literally in the updated @file{configure.ac}.
11819 @end defmac
11821 @defmac AU_ALIAS (@var{old-name}, @var{new-name})
11822 @auindex{ALIAS}
11823 Used if the @var{old-name} is to be replaced by a call to @var{new-macro}
11824 with the same parameters.  This happens for example if the macro was renamed.
11825 @end defmac
11827 @node Coding Style
11828 @section Coding Style
11829 @cindex Coding style
11831 The Autoconf macros follow a strict coding style.  You are encouraged to
11832 follow this style, especially if you intend to distribute your macro,
11833 either by contributing it to Autoconf itself, or via other means.
11835 The first requirement is to pay great attention to the quotation.  For
11836 more details, see @ref{Autoconf Language}, and @ref{M4 Quotation}.
11838 Do not try to invent new interfaces.  It is likely that there is a macro
11839 in Autoconf that resembles the macro you are defining: try to stick to
11840 this existing interface (order of arguments, default values, etc.).  We
11841 @emph{are} conscious that some of these interfaces are not perfect;
11842 nevertheless, when harmless, homogeneity should be preferred over
11843 creativity.
11845 Be careful about clashes both between M4 symbols and between shell
11846 variables.
11848 If you stick to the suggested M4 naming scheme (@pxref{Macro Names}),
11849 you are unlikely to generate conflicts.  Nevertheless, when you need to
11850 set a special value, @emph{avoid using a regular macro name}; rather,
11851 use an ``impossible'' name.  For instance, up to version 2.13, the macro
11852 @code{AC_SUBST} used to remember what @var{symbol} macros were already defined
11853 by setting @code{AC_SUBST_@var{symbol}}, which is a regular macro name.
11854 But since there is a macro named @code{AC_SUBST_FILE}, it was just
11855 impossible to @samp{AC_SUBST(FILE)}!  In this case,
11856 @code{AC_SUBST(@var{symbol})} or @code{_AC_SUBST(@var{symbol})} should
11857 have been used (yes, with the parentheses).
11858 @c or better yet, high-level macros such as @code{m4_expand_once}
11860 No Autoconf macro should ever enter the user-variable name space; i.e.,
11861 except for the variables that are the actual result of running the
11862 macro, all shell variables should start with @code{ac_}.  In
11863 addition, small macros or any macro that is likely to be embedded in
11864 other macros should be careful not to use obvious names.
11866 @cindex @code{dnl}
11867 Do not use @code{dnl} to introduce comments: most of the comments you
11868 are likely to write are either header comments which are not output
11869 anyway, or comments that should make their way into @file{configure}.
11870 There are exceptional cases where you do want to comment special M4
11871 constructs, in which case @code{dnl} is right, but keep in mind that it
11872 is unlikely.
11874 M4 ignores the leading blanks and newlines before each argument.
11875 Use this feature to
11876 indent in such a way that arguments are (more or less) aligned with the
11877 opening parenthesis of the macro being called.  For instance, instead of
11879 @example
11880 AC_CACHE_CHECK(for EMX OS/2 environment,
11881 ac_cv_emxos2,
11882 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, [return __EMX__;])],
11883 [ac_cv_emxos2=yes], [ac_cv_emxos2=no])])
11884 @end example
11886 @noindent
11887 write
11889 @example
11890 AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
11891 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
11892                    [ac_cv_emxos2=yes],
11893                    [ac_cv_emxos2=no])])
11894 @end example
11896 @noindent
11897 or even
11899 @example
11900 AC_CACHE_CHECK([for EMX OS/2 environment],
11901                [ac_cv_emxos2],
11902                [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
11903                                                    [return __EMX__;])],
11904                                   [ac_cv_emxos2=yes],
11905                                   [ac_cv_emxos2=no])])
11906 @end example
11908 When using @code{AC_RUN_IFELSE} or any macro that cannot work when
11909 cross-compiling, provide a pessimistic value (typically @samp{no}).
11911 Feel free to use various tricks to prevent auxiliary tools, such as
11912 syntax-highlighting editors, from behaving improperly.  For instance,
11913 instead of:
11915 @example
11916 m4_bpatsubst([$1], [$"])
11917 @end example
11919 @noindent
11922 @example
11923 m4_bpatsubst([$1], [$""])
11924 @end example
11926 @noindent
11927 so that Emacsen do not open an endless ``string'' at the first quote.
11928 For the same reasons, avoid:
11930 @example
11931 test $[#] != 0
11932 @end example
11934 @noindent
11935 and use:
11937 @example
11938 test $[@@%:@@] != 0
11939 @end example
11941 @noindent
11942 Otherwise, the closing bracket would be hidden inside a @samp{#}-comment,
11943 breaking the bracket-matching highlighting from Emacsen.  Note the
11944 preferred style to escape from M4: @samp{$[1]}, @samp{$[@@]}, etc.  Do
11945 not escape when it is unnecessary.  Common examples of useless quotation
11946 are @samp{[$]$1} (write @samp{$$1}), @samp{[$]var} (use @samp{$var}),
11947 etc.  If you add portability issues to the picture, you'll prefer
11948 @samp{$@{1+"$[@@]"@}} to @samp{"[$]@@"}, and you'll prefer do something
11949 better than hacking Autoconf @code{:-)}.
11951 When using @command{sed}, don't use @option{-e} except for indenting
11952 purposes.  With the @code{s} and @code{y} commands, the preferred
11953 separator is @samp{/} unless @samp{/} itself might appear in the pattern
11954 or replacement, in which case you should use @samp{|}, or optionally
11955 @samp{,} if you know the pattern and replacement cannot contain a file
11956 name.  If none of these characters will do, choose a printable character
11957 that cannot appear in the pattern or replacement.  Characters from the
11958 set @samp{"#$&'()*;<=>?`|~} are good choices if the pattern or
11959 replacement might contain a file name, since they have special meaning
11960 to the shell and are less likely to occur in file names.
11962 @xref{Macro Definitions}, for details on how to define a macro.  If a
11963 macro doesn't use @code{AC_REQUIRE}, is expected to never be the object
11964 of an @code{AC_REQUIRE} directive, and macros required by other macros
11965 inside arguments do not need to be expanded before this macro, then
11966 use @code{m4_define}.  In case of doubt, use @code{AC_DEFUN}.
11967 All the @code{AC_REQUIRE} statements should be at the beginning of the
11968 macro, and each statement should be followed by @code{dnl}.
11970 You should not rely on the number of arguments: instead of checking
11971 whether an argument is missing, test that it is not empty.  It provides
11972 both a simpler and a more predictable interface to the user, and saves
11973 room for further arguments.
11975 Unless the macro is short, try to leave the closing @samp{])} at the
11976 beginning of a line, followed by a comment that repeats the name of the
11977 macro being defined.  This introduces an additional newline in
11978 @command{configure}; normally, that is not a problem, but if you want to
11979 remove it you can use @samp{[]dnl} on the last line.  You can similarly
11980 use @samp{[]dnl} after a macro call to remove its newline.  @samp{[]dnl}
11981 is recommended instead of @samp{dnl} to ensure that M4 does not
11982 interpret the @samp{dnl} as being attached to the preceding text or
11983 macro output.  For example, instead of:
11985 @example
11986 AC_DEFUN([AC_PATH_X],
11987 [AC_MSG_CHECKING([for X])
11988 AC_REQUIRE_CPP()
11989 @r{# @dots{}omitted@dots{}}
11990   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
11991 fi])
11992 @end example
11994 @noindent
11995 you would write:
11997 @example
11998 AC_DEFUN([AC_PATH_X],
11999 [AC_REQUIRE_CPP()[]dnl
12000 AC_MSG_CHECKING([for X])
12001 @r{# @dots{}omitted@dots{}}
12002   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
12003 fi[]dnl
12004 ])# AC_PATH_X
12005 @end example
12007 If the macro is long, try to split it into logical chunks.  Typically,
12008 macros that check for a bug in a function and prepare its
12009 @code{AC_LIBOBJ} replacement should have an auxiliary macro to perform
12010 this setup.  Do not hesitate to introduce auxiliary macros to factor
12011 your code.
12013 In order to highlight the recommended coding style, here is a macro
12014 written the old way:
12016 @example
12017 dnl Check for EMX on OS/2.
12018 dnl _AC_EMXOS2
12019 AC_DEFUN(_AC_EMXOS2,
12020 [AC_CACHE_CHECK(for EMX OS/2 environment, ac_cv_emxos2,
12021 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, return __EMX__;)],
12022 ac_cv_emxos2=yes, ac_cv_emxos2=no)])
12023 test "$ac_cv_emxos2" = yes && EMXOS2=yes])
12024 @end example
12026 @noindent
12027 and the new way:
12029 @example
12030 # _AC_EMXOS2
12031 # ----------
12032 # Check for EMX on OS/2.
12033 m4_define([_AC_EMXOS2],
12034 [AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
12035 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
12036                    [ac_cv_emxos2=yes],
12037                    [ac_cv_emxos2=no])])
12038 test "$ac_cv_emxos2" = yes && EMXOS2=yes[]dnl
12039 ])# _AC_EMXOS2
12040 @end example
12045 @c ============================================= Portable Shell Programming
12047 @node Portable Shell
12048 @chapter Portable Shell Programming
12049 @cindex Portable shell programming
12051 When writing your own checks, there are some shell-script programming
12052 techniques you should avoid in order to make your code portable.  The
12053 Bourne shell and upward-compatible shells like the Korn shell and Bash
12054 have evolved over the years, but to prevent trouble, do not take
12055 advantage of features that were added after Unix version 7, circa
12056 1977 (@pxref{Systemology}).
12058 You should not use aliases, negated character classes, or other features
12059 that are not found in all Bourne-compatible shells; restrict yourself
12060 to the lowest common denominator.  Even @code{unset} is not supported
12061 by all shells!
12063 Shell functions are considered portable nowadays, though Autoconf still
12064 does not use them (Autotest does).  However, inside a shell function,
12065 you should not rely on the error status of a subshell if the last
12066 command of that subshell was @code{exit} or @code{trap}, as this
12067 triggers bugs in zsh 4.x; while Autoconf tries to find a shell that
12068 does not exhibit the bug, zsh might be the only shell present on the
12069 user's machine.
12071 Some ancient systems have quite
12072 small limits on the length of the @samp{#!} line; for instance, 32
12073 bytes (not including the newline) on SunOS 4.
12074 A few ancient 4.2@acronym{BSD} based systems (such as Dynix circa 1984)
12075 required a single space between the @samp{#!} and the @samp{/}.
12076 However, these ancient systems are no longer of practical concern.
12078 The set of external programs you should run in a @command{configure} script
12079 is fairly small.  @xref{Utilities in Makefiles, , Utilities in
12080 Makefiles, standards, @acronym{GNU} Coding Standards}, for the list.  This
12081 restriction allows users to start out with a fairly small set of
12082 programs and build the rest, avoiding too many interdependencies between
12083 packages.
12085 Some of these external utilities have a portable subset of features; see
12086 @ref{Limitations of Usual Tools}.
12088 There are other sources of documentation about shells.  The
12089 specification for the Posix
12090 @uref{http://www.opengroup.org/@/susv3/@/utilities/@/xcu_chap02.html, Shell
12091 Command Language}, though more generous than the restrictive shell
12092 subset described above, is fairly portable nowadays.  Also please see
12093 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/, the Shell FAQs}.
12095 @menu
12096 * Shellology::                  A zoology of shells
12097 * Here-Documents::              Quirks and tricks
12098 * File Descriptors::            FDs and redirections
12099 * File System Conventions::     File names
12100 * Shell Pattern Matching::      Pattern matching
12101 * Shell Substitutions::         Variable and command expansions
12102 * Assignments::                 Varying side effects of assignments
12103 * Parentheses::                 Parentheses in shell scripts
12104 * Slashes::                     Slashes in shell scripts
12105 * Special Shell Variables::     Variables you should not change
12106 * Limitations of Builtins::     Portable use of not so portable /bin/sh
12107 * Limitations of Usual Tools::  Portable use of portable tools
12108 @end menu
12110 @node Shellology
12111 @section Shellology
12112 @cindex Shellology
12114 There are several families of shells, most prominently the Bourne family
12115 and the C shell family which are deeply incompatible.  If you want to
12116 write portable shell scripts, avoid members of the C shell family.  The
12117 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/@/shell-differences/, the
12118 Shell difference FAQ} includes a small history of Posix shells, and a
12119 comparison between several of them.
12121 Below we describe some of the members of the Bourne shell family.
12123 @table @asis
12124 @item Ash
12125 @cindex Ash
12126 Ash is often used on @acronym{GNU}/Linux and @acronym{BSD}
12127 systems as a light-weight Bourne-compatible shell.  Ash 0.2 has some
12128 bugs that are fixed in the 0.3.x series, but portable shell scripts
12129 should work around them, since version 0.2 is still shipped with many
12130 @acronym{GNU}/Linux distributions.
12132 To be compatible with Ash 0.2:
12134 @itemize @minus
12135 @item
12136 don't use @samp{$?} after expanding empty or unset variables,
12137 or at the start of an @command{eval}:
12139 @example
12140 foo=
12141 false
12142 $foo
12143 echo "Do not use it: $?"
12144 false
12145 eval 'echo "Do not use it: $?"'
12146 @end example
12148 @item
12149 don't use command substitution within variable expansion:
12151 @example
12152 cat $@{FOO=`bar`@}
12153 @end example
12155 @item
12156 beware that single builtin substitutions are not performed by a
12157 subshell, hence their effect applies to the current shell!  @xref{Shell
12158 Substitutions}, item ``Command Substitution''.
12159 @end itemize
12161 @item Bash
12162 @cindex Bash
12163 To detect whether you are running Bash, test whether
12164 @code{BASH_VERSION} is set.  To require
12165 Posix compatibility, run @samp{set -o posix}.  @xref{Bash POSIX
12166 Mode, , Bash Posix Mode, bash, The @acronym{GNU} Bash Reference
12167 Manual}, for details.
12169 @item Bash 2.05 and later
12170 @cindex Bash 2.05 and later
12171 Versions 2.05 and later of Bash use a different format for the
12172 output of the @command{set} builtin, designed to make evaluating its
12173 output easier.  However, this output is not compatible with earlier
12174 versions of Bash (or with many other shells, probably).  So if
12175 you use Bash 2.05 or higher to execute @command{configure},
12176 you'll need to use Bash 2.05 for all other build tasks as well.
12178 @item Ksh
12179 @cindex Ksh
12180 @cindex Korn shell
12181 @prindex @samp{ksh}
12182 @prindex @samp{ksh88}
12183 @prindex @samp{ksh93}
12184 The Korn shell is compatible with the Bourne family and it mostly
12185 conforms to Posix.  It has two major variants commonly
12186 called @samp{ksh88} and @samp{ksh93}, named after the years of initial
12187 release.  It is usually called @command{ksh}, but is called @command{sh}
12188 on some hosts if you set your path appropriately.
12190 Solaris systems have three variants:
12191 @prindex @command{/usr/bin/ksh} on Solaris
12192 @command{/usr/bin/ksh} is @samp{ksh88}; it is
12193 standard on Solaris 2.0 and later.
12194 @prindex @command{/usr/xpg4/bin/sh} on Solaris
12195 @command{/usr/xpg4/bin/sh} is a Posix-compliant variant of
12196 @samp{ksh88}; it is standard on Solaris 9 and later.
12197 @prindex @command{/usr/dt/bin/dtksh} on Solaris
12198 @command{/usr/dt/bin/dtksh} is @samp{ksh93}.
12199 Variants that are not standard may be parts of optional
12200 packages.  There is no extra charge for these packages, but they are
12201 not part of a minimal OS install and therefore some installations may
12202 not have it.
12204 Starting with Tru64 Version 4.0, the Korn shell @command{/usr/bin/ksh}
12205 is also available as @command{/usr/bin/posix/sh}.  If the environment
12206 variable @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
12207 the standard shell conform to Posix.
12209 @item Pdksh
12210 @prindex @samp{pdksh}
12211 A public-domain clone of the Korn shell called @command{pdksh} is widely
12212 available: it has most of the @samp{ksh88} features along with a few of
12213 its own.  It usually sets @code{KSH_VERSION}, except if invoked as
12214 @command{/bin/sh} on Open@acronym{BSD}, and similarly to Bash you can require
12215 Posix compatibility by running @samp{set -o posix}.  Unfortunately, with
12216 @command{pdksh} 5.2.14 (the latest stable version as of January 2007)
12217 Posix mode is buggy and causes @command{pdksh} to depart from Posix in
12218 at least one respect:
12220 @example
12221 $ @kbd{echo "`echo \"hello\"`"}
12222 hello
12223 $ @kbd{set -o posix}
12224 $ @kbd{echo "`echo \"hello\"`"}
12225 "hello"
12226 @end example
12228 The last line of output contains spurious quotes.  This is yet another
12229 reason why portable shell code should not contain
12230 @code{"`@dots{}\"@dots{}\"@dots{}`"} constructs (@pxref{Shell
12231 Substitutions}).
12233 @item Zsh
12234 @cindex Zsh
12235 To detect whether you are running @command{zsh}, test whether
12236 @code{ZSH_VERSION} is set.  By default @command{zsh} is @emph{not}
12237 compatible with the Bourne shell: you must execute @samp{emulate sh},
12238 and for @command{zsh} versions before 3.1.6-dev-18 you must also
12239 set @code{NULLCMD} to @samp{:}.  @xref{Compatibility, , Compatibility,
12240 zsh, The Z Shell Manual}, for details.
12242 The default Mac OS X @command{sh} was originally Zsh; it was changed to
12243 Bash in Mac OS X 10.2.
12244 @end table
12246 The following discussion between Russ Allbery and Robert Lipe is worth
12247 reading:
12249 @noindent
12250 Russ Allbery:
12252 @quotation
12253 The @acronym{GNU} assumption that @command{/bin/sh} is the one and only shell
12254 leads to a permanent deadlock.  Vendors don't want to break users'
12255 existing shell scripts, and there are some corner cases in the Bourne
12256 shell that are not completely compatible with a Posix shell.  Thus,
12257 vendors who have taken this route will @emph{never} (OK@dots{}``never say
12258 never'') replace the Bourne shell (as @command{/bin/sh}) with a
12259 Posix shell.
12260 @end quotation
12262 @noindent
12263 Robert Lipe:
12265 @quotation
12266 This is exactly the problem.  While most (at least most System V's) do
12267 have a Bourne shell that accepts shell functions most vendor
12268 @command{/bin/sh} programs are not the Posix shell.
12270 So while most modern systems do have a shell @emph{somewhere} that meets the
12271 Posix standard, the challenge is to find it.
12272 @end quotation
12274 @node Here-Documents
12275 @section Here-Documents
12276 @cindex Here-documents
12277 @cindex Shell here-documents
12279 Don't rely on @samp{\} being preserved just because it has no special
12280 meaning together with the next symbol.  In the native @command{sh}
12281 on Open@acronym{BSD} 2.7 @samp{\"} expands to @samp{"} in here-documents with
12282 unquoted delimiter.  As a general rule, if @samp{\\} expands to @samp{\}
12283 use @samp{\\} to get @samp{\}.
12285 With Open@acronym{BSD} 2.7's @command{sh}
12287 @example
12288 @group
12289 $ @kbd{cat <<EOF
12290 > \" \\
12291 > EOF}
12292 " \
12293 @end group
12294 @end example
12296 @noindent
12297 and with Bash:
12299 @example
12300 @group
12301 bash-2.04$ @kbd{cat <<EOF
12302 > \" \\
12303 > EOF}
12304 \" \
12305 @end group
12306 @end example
12308 Some shells mishandle large here-documents: for example,
12309 Solaris 10 @command{dtksh} and the UnixWare 7.1.1 Posix shell, which are
12310 derived from Korn shell version M-12/28/93d, mishandle braced variable
12311 expansion that crosses a 1024- or 4096-byte buffer boundary
12312 within a here-document.  Only the part of the variable name after the boundary
12313 is used.  For example, @code{$@{variable@}} could be replaced by the expansion
12314 of @code{$@{ble@}}.  If the end of the variable name is aligned with the block
12315 boundary, the shell reports an error, as if you used @code{$@{@}}.
12316 Instead of @code{$@{variable-default@}}, the shell may expand
12317 @code{$@{riable-default@}}, or even @code{$@{fault@}}.  This bug can often
12318 be worked around by omitting the braces: @code{$variable}.  The bug was
12319 fixed in
12320 @samp{ksh93g} (1998-04-30) but as of 2006 many operating systems were
12321 still shipping older versions with the bug.
12323 Many shells (including the Bourne shell) implement here-documents
12324 inefficiently.  In particular, some shells can be extremely inefficient when
12325 a single statement contains many here-documents.  For instance if your
12326 @file{configure.ac} includes something like:
12328 @example
12329 @group
12330 if <cross_compiling>; then
12331   assume this and that
12332 else
12333   check this
12334   check that
12335   check something else
12336   @dots{}
12337   on and on forever
12338   @dots{}
12340 @end group
12341 @end example
12343 A shell parses the whole @code{if}/@code{fi} construct, creating
12344 temporary files for each here-document in it.  Some shells create links
12345 for such here-documents on every @code{fork}, so that the clean-up code
12346 they had installed correctly removes them.  It is creating the links
12347 that can take the shell forever.
12349 Moving the tests out of the @code{if}/@code{fi}, or creating multiple
12350 @code{if}/@code{fi} constructs, would improve the performance
12351 significantly.  Anyway, this kind of construct is not exactly the
12352 typical use of Autoconf.  In fact, it's even not recommended, because M4
12353 macros can't look into shell conditionals, so we may fail to expand a
12354 macro when it was expanded before in a conditional path, and the
12355 condition turned out to be false at runtime, and we end up not
12356 executing the macro at all.
12358 @node File Descriptors
12359 @section File Descriptors
12360 @cindex Descriptors
12361 @cindex File descriptors
12362 @cindex Shell file descriptors
12364 Most shells, if not all (including Bash, Zsh, Ash), output traces on
12365 stderr, even for subshells.  This might result in undesirable content
12366 if you meant to capture the standard-error output of the inner command:
12368 @example
12369 $ @kbd{ash -x -c '(eval "echo foo >&2") 2>stderr'}
12370 $ @kbd{cat stderr}
12371 + eval echo foo >&2
12372 + echo foo
12374 $ @kbd{bash -x -c '(eval "echo foo >&2") 2>stderr'}
12375 $ @kbd{cat stderr}
12376 + eval 'echo foo >&2'
12377 ++ echo foo
12379 $ @kbd{zsh -x -c '(eval "echo foo >&2") 2>stderr'}
12380 @i{# Traces on startup files deleted here.}
12381 $ @kbd{cat stderr}
12382 +zsh:1> eval echo foo >&2
12383 +zsh:1> echo foo
12385 @end example
12387 @noindent
12388 One workaround is to grep out uninteresting lines, hoping not to remove
12389 good ones.
12391 If you intend to redirect both standard error and standard output,
12392 redirect standard output first.  This works better with @acronym{HP-UX},
12393 since its shell mishandles tracing if standard error is redirected
12394 first:
12396 @example
12397 $ @kbd{sh -x -c ': 2>err >out'}
12398 + :
12399 + 2> err $ @kbd{cat err}
12400 1> out
12401 @end example
12403 Don't try to redirect the standard error of a command substitution.  It
12404 must be done @emph{inside} the command substitution.  When running
12405 @samp{: `cd /zorglub` 2>/dev/null} expect the error message to
12406 escape, while @samp{: `cd /zorglub 2>/dev/null`} works properly.
12408 It is worth noting that Zsh (but not Ash nor Bash) makes it possible
12409 in assignments though: @samp{foo=`cd /zorglub` 2>/dev/null}.
12411 When catering to old systems, don't redirect the same file descriptor
12412 several times, as you are doomed to failure under Ultrix.
12414 @example
12415 ULTRIX V4.4 (Rev. 69) System #31: Thu Aug 10 19:42:23 GMT 1995
12416 UWS V4.4 (Rev. 11)
12417 $ @kbd{eval 'echo matter >fullness' >void}
12418 illegal io
12419 $ @kbd{eval '(echo matter >fullness)' >void}
12420 illegal io
12421 $ @kbd{(eval '(echo matter >fullness)') >void}
12422 Ambiguous output redirect.
12423 @end example
12425 @noindent
12426 In each case the expected result is of course @file{fullness} containing
12427 @samp{matter} and @file{void} being empty.  However, this bug is
12428 probably not of practical concern to modern platforms.
12430 Don't rely on file descriptors 0, 1, and 2 remaining closed in a
12431 subsidiary program.  If any of these descriptors is closed, the
12432 operating system may open an unspecified file for the descriptor in the
12433 new process image.  Posix says this may be done only if the subsidiary
12434 program is set-user-ID or set-group-ID, but @acronym{HP-UX} 11.23 does
12435 it even for ordinary programs.
12437 Don't rely on open file descriptors being open in child processes.  In
12438 @command{ksh}, file descriptors above 2 which are opened using
12439 @samp{exec @var{n}>file} are closed by a subsequent @samp{exec} (such as
12440 that involved in the fork-and-exec which runs a program or script).
12441 Thus, using @command{sh}, we have:
12443 @example
12444 $ @kbd{cat ./descrips}
12445 #!/bin/sh -
12446 echo hello >&5
12447 $ @kbd{exec 5>t}
12448 $ @kbd{./descrips}
12449 $ @kbd{cat t}
12450 hello
12452 @end example
12454 @noindent
12455 But using ksh:
12457 @example
12458 $ @kbd{exec 5>t}
12459 $ @kbd{./descrips}
12460 hello
12461 $ @kbd{cat t}
12463 @end example
12465 @noindent
12466 Within the process which runs the @samp{descrips} script, file
12467 descriptor 5 is closed.
12469 @acronym{DOS} variants cannot rename or remove open files, such as in
12470 @samp{mv foo bar >foo} or @samp{rm foo >foo}, even though this is
12471 perfectly portable among Posix hosts.
12473 A few ancient systems reserved some file descriptors.  By convention,
12474 file descriptor 3 was opened to @file{/dev/tty} when you logged into
12475 Eighth Edition (1985) through Tenth Edition Unix (1989).  File
12476 descriptor 4 had a special use on the Stardent/Kubota Titan (circa
12477 1990), though we don't now remember what it was.  Both these systems are
12478 obsolete, so it's now safe to treat file descriptors 3 and 4 like any
12479 other file descriptors.
12481 @node File System Conventions
12482 @section File System Conventions
12483 @cindex File system conventions
12485 Autoconf uses shell-script processing extensively, so the file names
12486 that it processes should not contain characters that are special to the
12487 shell.  Special characters include space, tab, newline, @sc{nul}, and
12488 the following:
12490 @example
12491 " # $ & ' ( ) * ; < = > ? [ \ ` |
12492 @end example
12494 Also, file names should not begin with @samp{~} or @samp{-}, and should
12495 contain neither @samp{-} immediately after @samp{/} nor @samp{~}
12496 immediately after @samp{:}.  On Posix-like platforms, directory names
12497 should not contain @samp{:}, as this runs afoul of @samp{:} used as the
12498 path separator.
12500 These restrictions apply not only to the files that you distribute, but
12501 also to the absolute file names of your source, build, and destination
12502 directories.
12504 On some Posix-like platforms, @samp{!} and @samp{^} are special too, so
12505 they should be avoided.
12507 Posix lets implementations treat leading @file{//} specially, but
12508 requires leading @file{///} and beyond to be equivalent to @file{/}.
12509 Most Unix variants treat @file{//} like @file{/}.  However, some treat
12510 @file{//} as a ``super-root'' that can provide access to files that are
12511 not otherwise reachable from @file{/}.  The super-root tradition began
12512 with Apollo Domain/OS, which died out long ago, but unfortunately Cygwin
12513 has revived it.
12515 While @command{autoconf} and friends are usually run on some Posix
12516 variety, they can be used on other systems, most notably @acronym{DOS}
12517 variants.  This impacts several assumptions regarding file names.
12519 @noindent
12520 For example, the following code:
12522 @example
12523 case $foo_dir in
12524   /*) # Absolute
12525      ;;
12526   *)
12527      foo_dir=$dots$foo_dir ;;
12528 esac
12529 @end example
12531 @noindent
12532 fails to properly detect absolute file names on those systems, because
12533 they can use a drivespec, and usually use a backslash as directory
12534 separator.  If you want to be portable to @acronym{DOS} variants (at the
12535 price of rejecting valid but oddball Posix file names like @file{a:\b}),
12536 you can check for absolute file names like this:
12538 @cindex absolute file names, detect
12539 @example
12540 case $foo_dir in
12541   [\\/]* | ?:[\\/]* ) # Absolute
12542      ;;
12543   *)
12544      foo_dir=$dots$foo_dir ;;
12545 esac
12546 @end example
12548 @noindent
12549 Make sure you quote the brackets if appropriate and keep the backslash as
12550 first character (@pxref{Limitations of Builtins}).
12552 Also, because the colon is used as part of a drivespec, these systems don't
12553 use it as path separator.  When creating or accessing paths, you can use the
12554 @code{PATH_SEPARATOR} output variable instead.  @command{configure} sets this
12555 to the appropriate value for the build system (@samp{:} or @samp{;}) when it
12556 starts up.
12558 File names need extra care as well.  While @acronym{DOS} variants
12559 that are Posixy enough to run @command{autoconf} (such as @acronym{DJGPP})
12560 are usually able to handle long file names properly, there are still
12561 limitations that can seriously break packages.  Several of these issues
12562 can be easily detected by the
12563 @uref{ftp://ftp.gnu.org/gnu/non-gnu/doschk/doschk-1.1.tar.gz, doschk}
12564 package.
12566 A short overview follows; problems are marked with @sc{sfn}/@sc{lfn} to
12567 indicate where they apply: @sc{sfn} means the issues are only relevant to
12568 plain @acronym{DOS}, not to @acronym{DOS} under Microsoft Windows
12569 variants, while @sc{lfn} identifies problems that exist even under
12570 Microsoft Windows variants.
12572 @table @asis
12573 @item No multiple dots (@sc{sfn})
12574 @acronym{DOS} cannot handle multiple dots in file names.  This is an especially
12575 important thing to remember when building a portable configure script,
12576 as @command{autoconf} uses a .in suffix for template files.
12578 This is perfectly OK on Posix variants:
12580 @example
12581 AC_CONFIG_HEADERS([config.h])
12582 AC_CONFIG_FILES([source.c foo.bar])
12583 AC_OUTPUT
12584 @end example
12586 @noindent
12587 but it causes problems on @acronym{DOS}, as it requires @samp{config.h.in},
12588 @samp{source.c.in} and @samp{foo.bar.in}.  To make your package more portable
12589 to @acronym{DOS}-based environments, you should use this instead:
12591 @example
12592 AC_CONFIG_HEADERS([config.h:config.hin])
12593 AC_CONFIG_FILES([source.c:source.cin foo.bar:foobar.in])
12594 AC_OUTPUT
12595 @end example
12597 @item No leading dot (@sc{sfn})
12598 @acronym{DOS} cannot handle file names that start with a dot.  This is usually
12599 not important for @command{autoconf}.
12601 @item Case insensitivity (@sc{lfn})
12602 @acronym{DOS} is case insensitive, so you cannot, for example, have both a
12603 file called @samp{INSTALL} and a directory called @samp{install}.  This
12604 also affects @command{make}; if there's a file called @samp{INSTALL} in
12605 the directory, @samp{make install} does nothing (unless the
12606 @samp{install} target is marked as PHONY).
12608 @item The 8+3 limit (@sc{sfn})
12609 Because the @acronym{DOS} file system only stores the first 8 characters of
12610 the file name and the first 3 of the extension, those must be unique.
12611 That means that @file{foobar-part1.c}, @file{foobar-part2.c} and
12612 @file{foobar-prettybird.c} all resolve to the same file name
12613 (@file{FOOBAR-P.C}).  The same goes for @file{foo.bar} and
12614 @file{foo.bartender}.
12616 The 8+3 limit is not usually a problem under Microsoft Windows, as it
12617 uses numeric
12618 tails in the short version of file names to make them unique.  However, a
12619 registry setting can turn this behavior off.  While this makes it
12620 possible to share file trees containing long file names between @sc{sfn}
12621 and @sc{lfn} environments, it also means the above problem applies there
12622 as well.
12624 @item Invalid characters (@sc{lfn})
12625 Some characters are invalid in @acronym{DOS} file names, and should therefore
12626 be avoided.  In a @sc{lfn} environment, these are @samp{/}, @samp{\},
12627 @samp{?}, @samp{*}, @samp{:}, @samp{<}, @samp{>}, @samp{|} and @samp{"}.
12628 In a @sc{sfn} environment, other characters are also invalid.  These
12629 include @samp{+}, @samp{,}, @samp{[} and @samp{]}.
12631 @item Invalid names (@sc{lfn})
12632 Some @acronym{DOS} file names are reserved, and cause problems if you
12633 try to use files with those names.  These names include @file{CON},
12634 @file{AUX}, @file{COM1}, @file{COM2}, @file{COM3}, @file{COM4},
12635 @file{LPT1}, @file{LPT2}, @file{LPT3}, @file{NUL}, and @file{PRN}.
12636 File names are case insensitive, so even names like
12637 @file{aux/config.guess} are disallowed.
12639 @end table
12641 @node Shell Pattern Matching
12642 @section Shell Pattern Matching
12643 @cindex Shell pattern matching
12645 Nowadays portable patterns can use negated character classes like
12646 @samp{[!-aeiou]}.  The older syntax @samp{[^-aeiou]} is supported by
12647 some shells but not others; hence portable scripts should never use
12648 @samp{^} as the first character of a bracket pattern.
12650 Outside the C locale, patterns like @samp{[a-z]} are problematic since
12651 they may match characters that are not lower-case letters.
12653 @node Shell Substitutions
12654 @section Shell Substitutions
12655 @cindex Shell substitutions
12657 Contrary to a persistent urban legend, the Bourne shell does not
12658 systematically split variables and back-quoted expressions, in particular
12659 on the right-hand side of assignments and in the argument of @code{case}.
12660 For instance, the following code:
12662 @example
12663 case "$given_srcdir" in
12664 .)  top_srcdir="`echo "$dots" | sed 's|/$||'`" ;;
12665 *)  top_srcdir="$dots$given_srcdir" ;;
12666 esac
12667 @end example
12669 @noindent
12670 is more readable when written as:
12672 @example
12673 case $given_srcdir in
12674 .)  top_srcdir=`echo "$dots" | sed 's|/$||'` ;;
12675 *)  top_srcdir=$dots$given_srcdir ;;
12676 esac
12677 @end example
12679 @noindent
12680 and in fact it is even @emph{more} portable: in the first case of the
12681 first attempt, the computation of @code{top_srcdir} is not portable,
12682 since not all shells properly understand @code{"`@dots{}"@dots{}"@dots{}`"}.
12683 Worse yet, not all shells understand @code{"`@dots{}\"@dots{}\"@dots{}`"}
12684 the same way.  There is just no portable way to use double-quoted
12685 strings inside double-quoted back-quoted expressions (pfew!).
12687 @table @code
12688 @item $@@
12689 @cindex @samp{"$@@"}
12690 One of the most famous shell-portability issues is related to
12691 @samp{"$@@"}.  When there are no positional arguments, Posix says
12692 that @samp{"$@@"} is supposed to be equivalent to nothing, but the
12693 original Unix version 7 Bourne shell treated it as equivalent to
12694 @samp{""} instead, and this behavior survives in later implementations
12695 like Digital Unix 5.0.
12697 The traditional way to work around this portability problem is to use
12698 @samp{$@{1+"$@@"@}}.  Unfortunately this method does not work with
12699 Zsh (3.x and 4.x), which is used on Mac OS X@.  When emulating
12700 the Bourne shell, Zsh performs word splitting on @samp{$@{1+"$@@"@}}:
12702 @example
12703 zsh $ @kbd{emulate sh}
12704 zsh $ @kbd{for i in "$@@"; do echo $i; done}
12705 Hello World
12707 zsh $ @kbd{for i in $@{1+"$@@"@}; do echo $i; done}
12708 Hello
12709 World
12711 @end example
12713 @noindent
12714 Zsh handles plain @samp{"$@@"} properly, but we can't use plain
12715 @samp{"$@@"} because of the portability problems mentioned above.
12716 One workaround relies on Zsh's ``global aliases'' to convert
12717 @samp{$@{1+"$@@"@}} into @samp{"$@@"} by itself:
12719 @example
12720 test "$@{ZSH_VERSION+set@}" = set && alias -g '$@{1+"$@@"@}'='"$@@"'
12721 @end example
12723 Zsh only recognizes this alias when a shell word matches it exactly;
12724 @samp{"foo"$@{1+"$@@"@}} remains subject to word splitting.  Since this
12725 case always yields at least one shell word, use plain @samp{"$@@"}.
12727 A more conservative workaround is to avoid @samp{"$@@"} if it is
12728 possible that there may be no positional arguments.  For example,
12729 instead of:
12731 @example
12732 cat conftest.c "$@@"
12733 @end example
12735 you can use this instead:
12737 @example
12738 case $# in
12739 0) cat conftest.c;;
12740 *) cat conftest.c "$@@";;
12741 esac
12742 @end example
12744 Autoconf macros often use the @command{set} command to update
12745 @samp{$@@}, so if you are writing shell code intended for
12746 @command{configure} you should not assume that the value of @samp{$@@}
12747 persists for any length of time.
12750 @item $@{10@}
12751 @cindex positional parameters
12752 The 10th, 11th, @dots{} positional parameters can be accessed only after
12753 a @code{shift}.  The 7th Edition shell reported an error if given
12754 @code{$@{10@}}, and
12755 Solaris 10 @command{/bin/sh} still acts that way:
12757 @example
12758 $ @kbd{set 1 2 3 4 5 6 7 8 9 10}
12759 $ @kbd{echo $@{10@}}
12760 bad substitution
12761 @end example
12763 @item $@{@var{var}:-@var{value}@}
12764 @c Info cannot handle `:' in index entries.
12765 @c @cindex $@{@var{var}:-@var{value}@}
12766 Old @acronym{BSD} shells, including the Ultrix @code{sh}, don't accept the
12767 colon for any shell substitution, and complain and die.
12768 Similarly for $@{@var{var}:=@var{value}@}, $@{@var{var}:?@var{value}@}, etc.
12770 @item $@{@var{var}=@var{literal}@}
12771 @cindex $@{@var{var}=@var{literal}@}
12772 Be sure to quote:
12774 @example
12775 : $@{var='Some words'@}
12776 @end example
12778 @noindent
12779 otherwise some shells, such as on Digital Unix V 5.0, die because
12780 of a ``bad substitution''.
12782 @sp 1
12784 Solaris @command{/bin/sh} has a frightening bug in its interpretation
12785 of this.  Imagine you need set a variable to a string containing
12786 @samp{@}}.  This @samp{@}} character confuses Solaris @command{/bin/sh}
12787 when the affected variable was already set.  This bug can be exercised
12788 by running:
12790 @example
12791 $ @kbd{unset foo}
12792 $ @kbd{foo=$@{foo='@}'@}}
12793 $ @kbd{echo $foo}
12795 $ @kbd{foo=$@{foo='@}'   # no error; this hints to what the bug is}
12796 $ @kbd{echo $foo}
12798 $ @kbd{foo=$@{foo='@}'@}}
12799 $ @kbd{echo $foo}
12800 @}@}
12801  ^ ugh!
12802 @end example
12804 It seems that @samp{@}} is interpreted as matching @samp{$@{}, even
12805 though it is enclosed in single quotes.  The problem doesn't happen
12806 using double quotes.
12808 @item $@{@var{var}=@var{expanded-value}@}
12809 @cindex $@{@var{var}=@var{expanded-value}@}
12810 On Ultrix,
12811 running
12813 @example
12814 default="yu,yaa"
12815 : $@{var="$default"@}
12816 @end example
12818 @noindent
12819 sets @var{var} to @samp{M-yM-uM-,M-yM-aM-a}, i.e., the 8th bit of
12820 each char is set.  You don't observe the phenomenon using a simple
12821 @samp{echo $var} since apparently the shell resets the 8th bit when it
12822 expands $var.  Here are two means to make this shell confess its sins:
12824 @example
12825 $ @kbd{cat -v <<EOF
12826 $var
12827 EOF}
12828 @end example
12830 @noindent
12833 @example
12834 $ @kbd{set | grep '^var=' | cat -v}
12835 @end example
12837 One classic incarnation of this bug is:
12839 @example
12840 default="a b c"
12841 : $@{list="$default"@}
12842 for c in $list; do
12843   echo $c
12844 done
12845 @end example
12847 @noindent
12848 You'll get @samp{a b c} on a single line.  Why?  Because there are no
12849 spaces in @samp{$list}: there are @samp{M- }, i.e., spaces with the 8th
12850 bit set, hence no IFS splitting is performed!!!
12852 One piece of good news is that Ultrix works fine with @samp{:
12853 $@{list=$default@}}; i.e., if you @emph{don't} quote.  The bad news is
12854 then that @acronym{QNX} 4.25 then sets @var{list} to the @emph{last} item of
12855 @var{default}!
12857 The portable way out consists in using a double assignment, to switch
12858 the 8th bit twice on Ultrix:
12860 @example
12861 list=$@{list="$default"@}
12862 @end example
12864 @noindent
12865 @dots{}but beware of the @samp{@}} bug from Solaris (see above).  For safety,
12866 use:
12868 @example
12869 test "$@{var+set@}" = set || var=@var{@{value@}}
12870 @end example
12872 @item $@{#@var{var}@}
12873 @itemx $@{@var{var}%@var{word}@}
12874 @itemx $@{@var{var}%%@var{word}@}
12875 @itemx $@{@var{var}#@var{word}@}
12876 @itemx $@{@var{var}##@var{word}@}
12877 @cindex $@{#@var{var}@}
12878 @cindex $@{@var{var}%@var{word}@}
12879 @cindex $@{@var{var}%%@var{word}@}
12880 @cindex $@{@var{var}#@var{word}@}
12881 @cindex $@{@var{var}##@var{word}@}
12882 Posix requires support for these usages, but they do not work with many
12883 traditional shells, e.g., Solaris 10 @command{/bin/sh}.
12885 Also, @command{pdksh} 5.2.14 mishandles some @var{word} forms.  For
12886 example if @samp{$1} is @samp{a/b} and @samp{$2} is @samp{a}, then
12887 @samp{$@{1#$2@}} should yield @samp{/b}, but with @command{pdksh} it
12888 yields the empty string.
12891 @item `@var{commands}`
12892 @cindex `@var{commands}`
12893 @cindex Command Substitution
12894 Posix requires shells to trim all trailing newlines from command
12895 output before substituting it, so assignments like
12896 @samp{dir=`echo "$file" | tr a A`} do not work as expected if
12897 @samp{$file} ends in a newline.
12899 While in general it makes no sense, do not substitute a single builtin
12900 with side effects, because Ash 0.2, trying to optimize, does not fork a
12901 subshell to perform the command.
12903 For instance, if you wanted to check that @command{cd} is silent, do not
12904 use @samp{test -z "`cd /`"} because the following can happen:
12906 @example
12907 $ @kbd{pwd}
12908 /tmp
12909 $ @kbd{test -z "`cd /`" && pwd}
12911 @end example
12913 @noindent
12914 The result of @samp{foo=`exit 1`} is left as an exercise to the reader.
12916 The MSYS shell leaves a stray byte in the expansion of a double-quoted
12917 command substitution of a native program, if the end of the substitution
12918 is not aligned with the end of the double quote.  This may be worked
12919 around by inserting another pair of quotes:
12921 @example
12922 $ @kbd{echo "`printf 'foo\r\n'` bar" > broken}
12923 $ @kbd{echo "`printf 'foo\r\n'`"" bar" | cmp - broken}
12924 - broken differ: char 4, line 1
12925 @end example
12928 @item $(@var{commands})
12929 @cindex $(@var{commands})
12930 This construct is meant to replace @samp{`@var{commands}`},
12931 and it has most of the problems listed under @code{`@var{commands}`}.
12933 This construct can be
12934 nested while this is impossible to do portably with back quotes.
12935 Unfortunately it is not yet universally supported.  Most notably, even recent
12936 releases of Solaris don't support it:
12938 @example
12939 $ @kbd{showrev -c /bin/sh | grep version}
12940 Command version: SunOS 5.10 Generic 121005-03 Oct 2006
12941 $ @kbd{echo $(echo blah)}
12942 syntax error: `(' unexpected
12943 @end example
12945 @noindent
12946 nor does @sc{irix} 6.5's Bourne shell:
12947 @example
12948 $ @kbd{uname -a}
12949 IRIX firebird-image 6.5 07151432 IP22
12950 $ @kbd{echo $(echo blah)}
12951 $(echo blah)
12952 @end example
12954 If you do use @samp{$(@var{commands})}, make sure that the commands
12955 do not start with a parenthesis, as that would cause confusion with
12956 a different notation @samp{$((@var{expression}))} that in modern
12957 shells is an arithmetic expression not a command.  To avoid the
12958 confusion, insert a space between the two opening parentheses.
12960 Avoid @var{commands} that contain unbalanced parentheses in
12961 here-documents, comments, or case statement patterns, as many shells
12962 mishandle them.  For example, Bash 3.1, @samp{ksh88}, @command{pdksh}
12963 5.2.14, and Zsh 4.2.6 all mishandle the following valid command:
12965 @example
12966 echo $(case x in x) echo hello;; esac)
12967 @end example
12969 @item ^
12970 @cindex ^ quoting
12971 Always quote @samp{^}, otherwise traditional shells such as
12972 @command{/bin/sh} on Solaris 10 treat this like @samp{|}.
12974 @end table
12977 @node Assignments
12978 @section Assignments
12979 @cindex Shell assignments
12981 When setting several variables in a row, be aware that the order of the
12982 evaluation is undefined.  For instance @samp{foo=1 foo=2; echo $foo}
12983 gives @samp{1} with Solaris @command{/bin/sh}, but @samp{2} with Bash.
12984 You must use
12985 @samp{;} to enforce the order: @samp{foo=1; foo=2; echo $foo}.
12987 Don't rely on the following to find @file{subdir/program}:
12989 @example
12990 PATH=subdir$PATH_SEPARATOR$PATH program
12991 @end example
12993 @noindent
12994 as this does not work with Zsh 3.0.6.  Use something like this
12995 instead:
12997 @example
12998 (PATH=subdir$PATH_SEPARATOR$PATH; export PATH; exec program)
12999 @end example
13001 Don't rely on the exit status of an assignment: Ash 0.2 does not change
13002 the status and propagates that of the last statement:
13004 @example
13005 $ @kbd{false || foo=bar; echo $?}
13007 $ @kbd{false || foo=`:`; echo $?}
13009 @end example
13011 @noindent
13012 and to make things even worse, @acronym{QNX} 4.25 just sets the exit status
13013 to 0 in any case:
13015 @example
13016 $ @kbd{foo=`exit 1`; echo $?}
13018 @end example
13020 To assign default values, follow this algorithm:
13022 @enumerate
13023 @item
13024 If the default value is a literal and does not contain any closing
13025 brace, use:
13027 @example
13028 : $@{var='my literal'@}
13029 @end example
13031 @item
13032 If the default value contains no closing brace, has to be expanded, and
13033 the variable being initialized is not intended to be IFS-split
13034 (i.e., it's not a list), then use:
13036 @example
13037 : $@{var="$default"@}
13038 @end example
13040 @item
13041 If the default value contains no closing brace, has to be expanded, and
13042 the variable being initialized is intended to be IFS-split (i.e., it's a list),
13043 then use:
13045 @example
13046 var=$@{var="$default"@}
13047 @end example
13049 @item
13050 If the default value contains a closing brace, then use:
13052 @example
13053 test "$@{var+set@}" = set || var="has a '@}'"
13054 @end example
13055 @end enumerate
13057 In most cases @samp{var=$@{var="$default"@}} is fine, but in case of
13058 doubt, just use the last form.  @xref{Shell Substitutions}, items
13059 @samp{$@{@var{var}:-@var{value}@}} and @samp{$@{@var{var}=@var{value}@}}
13060 for the rationale.
13062 @node Parentheses
13063 @section Parentheses in Shell Scripts
13064 @cindex Shell parentheses
13066 Beware of two opening parentheses in a row, as many shell
13067 implementations treat them specially.  Posix requires that the command
13068 @samp{((cat))} must behave like @samp{(cat)}, but many shells, including
13069 Bash and the Korn shell, treat @samp{((cat))} as an arithmetic
13070 expression equivalent to @samp{let "cat"}, and may or may not report an
13071 error when they detect that @samp{cat} is not a number.  As another
13072 example, @samp{pdksh} 5.2.14 misparses the following code:
13074 @example
13075 if ((true) || false); then
13076   echo ok
13078 @end example
13080 @noindent
13081 To work around this problem, insert a space between the two opening
13082 parentheses.  There is a similar problem and workaround with
13083 @samp{$((}; see @ref{Shell Substitutions}.
13085 @node Slashes
13086 @section Slashes in Shell Scripts
13087 @cindex Shell slashes
13089 Unpatched Tru64 5.1 @command{sh} omits the last slash of command-line
13090 arguments that contain two trailing slashes:
13092 @example
13093 $ @kbd{echo / // /// //// .// //.}
13094 / / // /// ./ //.
13095 $ @kbd{x=//}
13096 $ @kbd{eval "echo \$x"}
13098 $ @kbd{set -x}
13099 $ @kbd{echo abc | tr -t ab //}
13100 + echo abc
13101 + tr -t ab /
13103 @end example
13105 Unpatched Tru64 4.0 @command{sh} adds a slash after @samp{"$var"} if the
13106 variable is empty and the second double-quote is followed by a word that
13107 begins and ends with slash:
13109 @example
13110 $ @kbd{sh -xc 'p=; echo "$p"/ouch/'}
13112 + echo //ouch/
13113 //ouch/
13114 @end example
13116 However, our understanding is that patches are available, so perhaps
13117 it's not worth worrying about working around these horrendous bugs.
13119 @node Special Shell Variables
13120 @section Special Shell Variables
13121 @cindex Shell variables
13122 @cindex Special shell variables
13124 Some shell variables should not be used, since they can have a deep
13125 influence on the behavior of the shell.  In order to recover a sane
13126 behavior from the shell, some variables should be unset, but
13127 @command{unset} is not portable (@pxref{Limitations of Builtins}) and a
13128 fallback value is needed.
13130 As a general rule, shell variable names containing a lower-case letter
13131 are safe; you can define and use these variables without worrying about
13132 their effect on the underlying system, and without worrying about
13133 whether the shell changes them unexpectedly.  (The exception is the
13134 shell variable @code{status}, as described below.)
13136 Here is a list of names that are known to cause trouble.  This list is
13137 not exhaustive, but you should be safe if you avoid the name
13138 @code{status} and names containing only upper-case letters and
13139 underscores.
13141 @c Alphabetical order, case insensitive, `A' before `a'.
13142 @table @code
13143 @item _
13144 Many shells reserve @samp{$_} for various purposes, e.g., the name of
13145 the last command executed.
13147 @item BIN_SH
13148 @evindex BIN_SH
13149 In Tru64, if @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
13150 the standard shell conform to Posix.
13152 @item CDPATH
13153 @evindex CDPATH
13154 When this variable is set it specifies a list of directories to search
13155 when invoking @code{cd} with a relative file name that did not start
13156 with @samp{./} or @samp{../}.  Posix
13157 1003.1-2001 says that if a nonempty directory name from @env{CDPATH}
13158 is used successfully, @code{cd} prints the resulting absolute
13159 file name.  Unfortunately this output can break idioms like
13160 @samp{abs=`cd src && pwd`} because @code{abs} receives the name twice.
13161 Also, many shells do not conform to this part of Posix; for
13162 example, @command{zsh} prints the result only if a directory name
13163 other than @file{.} was chosen from @env{CDPATH}.
13165 In practice the shells that have this problem also support
13166 @command{unset}, so you can work around the problem as follows:
13168 @example
13169 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
13170 @end example
13172 You can also avoid output by ensuring that your directory name is
13173 absolute or anchored at @samp{./}, as in @samp{abs=`cd ./src && pwd`}.
13175 Autoconf-generated scripts automatically unset @env{CDPATH} if
13176 possible, so you need not worry about this problem in those scripts.
13178 @item DUALCASE
13179 @evindex DUALCASE
13180 In the MKS shell, case statements and file name generation are
13181 case-insensitive unless @env{DUALCASE} is nonzero.
13182 Autoconf-generated scripts export this variable when they start up.
13184 @item ENV
13185 @itemx MAIL
13186 @itemx MAILPATH
13187 @itemx PS1
13188 @itemx PS2
13189 @itemx PS4
13190 @evindex ENV
13191 @evindex MAIL
13192 @evindex MAILPATH
13193 @evindex PS1
13194 @evindex PS2
13195 @evindex PS4
13196 These variables should not matter for shell scripts, since they are
13197 supposed to affect only interactive shells.  However, at least one
13198 shell (the pre-3.0 @sc{uwin} Korn shell) gets confused about
13199 whether it is interactive, which means that (for example) a @env{PS1}
13200 with a side effect can unexpectedly modify @samp{$?}.  To work around
13201 this bug, Autoconf-generated scripts do something like this:
13203 @example
13204 (unset ENV) >/dev/null 2>&1 && unset ENV MAIL MAILPATH
13205 PS1='$ '
13206 PS2='> '
13207 PS4='+ '
13208 @end example
13210 @item FPATH
13211 The Korn shell uses @env{FPATH} to find shell functions, so avoid
13212 @env{FPATH} in portable scripts.  @env{FPATH} is consulted after
13213 @env{PATH}, but you still need to be wary of tests that use @env{PATH}
13214 to find whether a command exists, since they might report the wrong
13215 result if @env{FPATH} is also set.
13217 @item IFS
13218 @evindex IFS
13219 Long ago, shell scripts inherited @env{IFS} from the environment,
13220 but this caused many problems so modern shells ignore any environment
13221 settings for @env{IFS}.
13223 Don't set the first character of @code{IFS} to backslash.  Indeed,
13224 Bourne shells use the first character (backslash) when joining the
13225 components in @samp{"$@@"} and some shells then reinterpret (!)@: the
13226 backslash escapes, so you can end up with backspace and other strange
13227 characters.
13229 The proper value for @code{IFS} (in regular code, not when performing
13230 splits) is @samp{@key{SPC}@key{TAB}@key{RET}}.  The first character is
13231 especially important, as it is used to join the arguments in @samp{$*};
13232 however, note that traditional shells, but also bash-2.04, fail to adhere
13233 to this and join with a space anyway.
13235 @item LANG
13236 @itemx LC_ALL
13237 @itemx LC_COLLATE
13238 @itemx LC_CTYPE
13239 @itemx LC_MESSAGES
13240 @itemx LC_MONETARY
13241 @itemx LC_NUMERIC
13242 @itemx LC_TIME
13243 @evindex LANG
13244 @evindex LC_ALL
13245 @evindex LC_COLLATE
13246 @evindex LC_CTYPE
13247 @evindex LC_MESSAGES
13248 @evindex LC_MONETARY
13249 @evindex LC_NUMERIC
13250 @evindex LC_TIME
13252 Autoconf-generated scripts normally set all these variables to
13253 @samp{C} because so much configuration code assumes the C locale and
13254 Posix requires that locale environment variables be set to
13255 @samp{C} if the C locale is desired.  However, some older, nonstandard
13256 systems (notably @acronym{SCO}) break if locale environment variables
13257 are set to @samp{C}, so when running on these systems
13258 Autoconf-generated scripts unset the variables instead.
13260 @item LANGUAGE
13261 @evindex LANGUAGE
13263 @env{LANGUAGE} is not specified by Posix, but it is a @acronym{GNU}
13264 extension that overrides @env{LC_ALL} in some cases, so
13265 Autoconf-generated scripts set it too.
13267 @item LC_ADDRESS
13268 @itemx LC_IDENTIFICATION
13269 @itemx LC_MEASUREMENT
13270 @itemx LC_NAME
13271 @itemx LC_PAPER
13272 @itemx LC_TELEPHONE
13273 @evindex LC_ADDRESS
13274 @evindex LC_IDENTIFICATION
13275 @evindex LC_MEASUREMENT
13276 @evindex LC_NAME
13277 @evindex LC_PAPER
13278 @evindex LC_TELEPHONE
13280 These locale environment variables are @acronym{GNU} extensions.  They
13281 are treated like their Posix brethren (@env{LC_COLLATE},
13282 etc.)@: as described above.
13284 @item LINENO
13285 Most modern shells provide the current line number in @code{LINENO}.
13286 Its value is the line number of the beginning of the current command.
13287 Autoconf attempts to execute @command{configure} with a shell that
13288 supports @code{LINENO}.
13289 If no such shell is available, it attempts to implement @code{LINENO}
13290 with a Sed prepass that replaces each instance of the string
13291 @code{$LINENO} (not followed by an alphanumeric character) with the
13292 line's number.
13294 You should not rely on @code{LINENO} within @command{eval}, as the
13295 behavior differs in practice.  Also, the possibility of the Sed
13296 prepass means that you should not rely on @code{$LINENO} when quoted,
13297 when in here-documents, or when in long commands that cross line
13298 boundaries.  Subshells should be OK, though.  In the following
13299 example, lines 1, 6, and 9 are portable, but the other instances of
13300 @code{LINENO} are not:
13302 @example
13303 @group
13304 $ @kbd{cat lineno}
13305 echo 1. $LINENO
13306 cat <<EOF
13307 3. $LINENO
13308 4. $LINENO
13310 ( echo 6. $LINENO )
13311 eval 'echo 7. $LINENO'
13312 echo 8. '$LINENO'
13313 echo 9. $LINENO '
13314 10.' $LINENO
13315 @end group
13316 @group
13317 $ @kbd{bash-2.05 lineno}
13318 1. 1
13319 3. 2
13320 4. 2
13321 6. 6
13322 7. 1
13323 8. $LINENO
13324 9. 9
13325 10. 9
13326 @end group
13327 @group
13328 $ @kbd{zsh-3.0.6 lineno}
13329 1. 1
13330 3. 2
13331 4. 2
13332 6. 6
13333 7. 7
13334 8. $LINENO
13335 9. 9
13336 10. 9
13337 @end group
13338 @group
13339 $ @kbd{pdksh-5.2.14 lineno}
13340 1. 1
13341 3. 2
13342 4. 2
13343 6. 6
13344 7. 0
13345 8. $LINENO
13346 9. 9
13347 10. 9
13348 @end group
13349 @group
13350 $ @kbd{sed '=' <lineno |}
13351 > @kbd{  sed '}
13352 > @kbd{    N}
13353 > @kbd{    s,$,-,}
13354 > @kbd{    t loop}
13355 > @kbd{    :loop}
13356 > @kbd{    s,^\([0-9]*\)\(.*\)[$]LINENO\([^a-zA-Z0-9_]\),\1\2\1\3,}
13357 > @kbd{    t loop}
13358 > @kbd{    s,-$,,}
13359 > @kbd{    s,^[0-9]*\n,,}
13360 > @kbd{  ' |}
13361 > @kbd{  sh}
13362 1. 1
13363 3. 3
13364 4. 4
13365 6. 6
13366 7. 7
13367 8. 8
13368 9. 9
13369 10. 10
13370 @end group
13371 @end example
13373 @item NULLCMD
13374 @evindex NULLCMD
13375 When executing the command @samp{>foo}, @command{zsh} executes
13376 @samp{$NULLCMD >foo} unless it is operating in Bourne shell
13377 compatibility mode and the @command{zsh} version is newer
13378 than 3.1.6-dev-18.  If you are using an older @command{zsh}
13379 and forget to set @env{NULLCMD},
13380 your script might be suspended waiting for data on its standard input.
13382 @item PATH_SEPARATOR
13383 @evindex PATH_SEPARATOR
13384 On @acronym{DJGPP} systems, the @env{PATH_SEPARATOR} environment
13385 variable can be set to either @samp{:} or @samp{;} to control the path
13386 separator Bash uses to set up certain environment variables (such as
13387 @env{PATH}).  You can set this variable to @samp{;} if you want
13388 @command{configure} to use @samp{;} as a separator; this might be useful
13389 if you plan to use non-Posix shells to execute files.  @xref{File System
13390 Conventions}, for more information about @code{PATH_SEPARATOR}.
13392 @item PWD
13393 @evindex PWD
13394 Posix 1003.1-2001 requires that @command{cd} and
13395 @command{pwd} must update the @env{PWD} environment variable to point
13396 to the logical name of the current directory, but traditional shells
13397 do not support this.  This can cause confusion if one shell instance
13398 maintains @env{PWD} but a subsidiary and different shell does not know
13399 about @env{PWD} and executes @command{cd}; in this case @env{PWD}
13400 points to the wrong directory.  Use @samp{`pwd`} rather than
13401 @samp{$PWD}.
13403 @item RANDOM
13404 Many shells provide @code{RANDOM}, a variable that returns a different
13405 integer each time it is used.  Most of the time, its value does not
13406 change when it is not used, but on @sc{irix} 6.5 the value changes all
13407 the time.  This can be observed by using @command{set}.  It is common
13408 practice to use @code{$RANDOM} as part of a file name, but code
13409 shouldn't rely on @code{$RANDOM} expanding to a nonempty string.
13411 @item status
13412 This variable is an alias to @samp{$?} for @code{zsh} (at least 3.1.6),
13413 hence read-only.  Do not use it.
13414 @end table
13416 @node Limitations of Builtins
13417 @section Limitations of Shell Builtins
13418 @cindex Shell builtins
13419 @cindex Limitations of shell builtins
13421 No, no, we are serious: some shells do have limitations!  :)
13423 You should always keep in mind that any builtin or command may support
13424 options, and therefore differ in behavior with arguments
13425 starting with a dash.  For instance, the innocent @samp{echo "$word"}
13426 can give unexpected results when @code{word} starts with a dash.  It is
13427 often possible to avoid this problem using @samp{echo "x$word"}, taking
13428 the @samp{x} into account later in the pipe.
13430 @table @asis
13431 @item @command{.}
13432 @prindex @command{.}
13433 Use @command{.} only with regular files (use @samp{test -f}).  Bash
13434 2.03, for instance, chokes on @samp{. /dev/null}.  Also, remember that
13435 @command{.} uses @env{PATH} if its argument contains no slashes, so if
13436 you want to use @command{.} on a file @file{foo} in the current
13437 directory, you must use @samp{. ./foo}.
13439 @item @command{!}
13440 @prindex @command{!}
13441 The Unix version 7 shell did not support
13442 negating the exit status of commands with @command{!}, and this feature
13443 is still absent from some shells (e.g., Solaris @command{/bin/sh}).
13444 Shell code like this:
13446 @example
13447 if ! cmp file1 file2 >/dev/null 2>&1; then
13448   echo files differ or trouble
13450 @end example
13452 is therefore not portable in practice.  Typically it is easy to rewrite
13453 such code, e.g.:
13455 @example
13456 cmp file1 file2 >/dev/null 2>&1 ||
13457   echo files differ or trouble
13458 @end example
13460 More generally, one can always rewrite @samp{! @var{command}} as:
13462 @example
13463 if @var{command}; then (exit 1); else :; fi
13464 @end example
13466 @item @command{break}
13467 @c ------------------
13468 @prindex @command{break}
13469 The use of @samp{break 2} etc.@: is safe.
13472 @item @command{case}
13473 @c -----------------
13474 @prindex @command{case}
13475 You don't need to quote the argument; no splitting is performed.
13477 You don't need the final @samp{;;}, but you should use it.
13479 Posix requires support for @code{case} patterns with opening
13480 parentheses like this:
13482 @example
13483 case $file_name in
13484 (*.c) echo "C source code";;
13485 esac
13486 @end example
13488 @noindent
13489 but the @code{(} in this example is not portable to many Bourne
13490 shell implementations.  It can be omitted safely.
13492 Zsh handles pattern fragments derived from parameter expansions or
13493 command substitutions as though quoted:
13495 @example
13496 $ pat=\?; case aa in ?$pat) echo match;; esac
13497 $ pat=\?; case a? in ?$pat) echo match;; esac
13498 match
13499 @end example
13501 @noindent
13502 Because of a bug in its @code{fnmatch}, Bash fails to properly
13503 handle backslashes in character classes:
13505 @example
13506 bash-2.02$ @kbd{case /tmp in [/\\]*) echo OK;; esac}
13507 bash-2.02$
13508 @end example
13510 @noindent
13511 This is extremely unfortunate, since you are likely to use this code to
13512 handle Posix or @sc{ms-dos} absolute file names.  To work around this
13513 bug, always put the backslash first:
13515 @example
13516 bash-2.02$ @kbd{case '\TMP' in [\\/]*) echo OK;; esac}
13518 bash-2.02$ @kbd{case /tmp in [\\/]*) echo OK;; esac}
13520 @end example
13522 Many Bourne shells cannot handle closing brackets in character classes
13523 correctly.
13525 Some shells also have problems with backslash escaping in case you do not want
13526 to match the backslash: both a backslash and the escaped character match this
13527 pattern.  To work around this, specify the character class in a variable, so
13528 that quote removal does not apply afterwards, and the special characters don't
13529 have to be backslash-escaped:
13531 @example
13532 $ @kbd{case '\' in [\<]) echo OK;; esac}
13534 $ @kbd{scanset='[<]'; case '\' in $scanset) echo OK;; esac}
13536 @end example
13538 Even with this, Solaris @command{ksh} matches a backslash if the set
13539 contains any
13540 of the characters @samp{|}, @samp{&}, @samp{(}, or @samp{)}.
13542 Conversely, Tru64 @command{ksh} (circa 2003) erroneously always matches
13543 a closing parenthesis if not specified in a character class:
13545 @example
13546 $ @kbd{case foo in *\)*) echo fail ;; esac}
13547 fail
13548 $ @kbd{case foo in *')'*) echo fail ;; esac}
13549 fail
13550 @end example
13552 Some shells, such as Ash 0.3.8, are confused by an empty
13553 @code{case}/@code{esac}:
13555 @example
13556 ash-0.3.8 $ @kbd{case foo in esac;}
13557 @error{}Syntax error: ";" unexpected (expecting ")")
13558 @end example
13560 Many shells still do not support parenthesized cases, which is a pity
13561 for those of us using tools that rely on balanced parentheses.  For
13562 instance, Solaris @command{/bin/sh}:
13564 @example
13565 $ @kbd{case foo in (foo) echo foo;; esac}
13566 @error{}syntax error: `(' unexpected
13567 @end example
13570 @item @command{cd}
13571 @c ---------------
13572 @prindex @command{cd}
13573 Posix 1003.1-2001 requires that @command{cd} must support
13574 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
13575 with @option{-L} being the default.  However, traditional shells do
13576 not support these options, and their @command{cd} command has the
13577 @option{-P} behavior.
13579 Portable scripts should assume neither option is supported, and should
13580 assume neither behavior is the default.  This can be a bit tricky,
13581 since the Posix default behavior means that, for example,
13582 @samp{ls ..} and @samp{cd ..} may refer to different directories if
13583 the current logical directory is a symbolic link.  It is safe to use
13584 @command{cd @var{dir}} if @var{dir} contains no @file{..} components.
13585 Also, Autoconf-generated scripts check for this problem when computing
13586 variables like @code{ac_top_srcdir} (@pxref{Configuration Actions}),
13587 so it is safe to @command{cd} to these variables.
13589 See @xref{Special Shell Variables}, for portability problems involving
13590 @command{cd} and the @env{CDPATH} environment variable.
13591 Also please see the discussion of the @command{pwd} command.
13594 @item @command{echo}
13595 @c -----------------
13596 @prindex @command{echo}
13597 The simple @command{echo} is probably the most surprising source of
13598 portability troubles.  It is not possible to use @samp{echo} portably
13599 unless both options and escape sequences are omitted.  New applications
13600 which are not aiming at portability should use @samp{printf} instead of
13601 @samp{echo}.
13603 Don't expect any option.  @xref{Preset Output Variables}, @code{ECHO_N}
13604 etc.@: for a means to simulate @option{-n}.
13606 Do not use backslashes in the arguments, as there is no consensus on
13607 their handling.  For @samp{echo '\n' | wc -l}, the @command{sh} of
13608 Solaris outputs 2, but Bash and Zsh (in @command{sh} emulation mode) output 1.
13609 The problem is truly @command{echo}: all the shells
13610 understand @samp{'\n'} as the string composed of a backslash and an
13611 @samp{n}.
13613 Because of these problems, do not pass a string containing arbitrary
13614 characters to @command{echo}.  For example, @samp{echo "$foo"} is safe
13615 if you know that @var{foo}'s value cannot contain backslashes and cannot
13616 start with @samp{-}, but otherwise you should use a here-document like
13617 this:
13619 @example
13620 cat <<EOF
13621 $foo
13623 @end example
13626 @item @command{eval}
13627 @c -----------------
13628 @prindex @command{eval}
13629 The @command{eval} command is useful in limited circumstances, e.g.,
13630 using commands like @samp{eval table_$key=\$value} and @samp{eval
13631 value=table_$key} to simulate a hash table when the key is known to be
13632 alphanumeric.  However, @command{eval} is tricky to use on arbitrary
13633 arguments, even when it is implemented correctly.
13635 It is obviously unwise to use @samp{eval $cmd} if the string value of
13636 @samp{cmd} was derived from an untrustworthy source.  But even if the
13637 string value is valid, @samp{eval $cmd} might not work as intended,
13638 since it causes field splitting and file name expansion to occur twice,
13639 once for the @command{eval} and once for the command itself.  It is
13640 therefore safer to use @samp{eval "$cmd"}.  For example, if @var{cmd}
13641 has the value @samp{cat test?.c}, @samp{eval $cmd} might expand to the
13642 equivalent of @samp{cat test;.c} if there happens to be a file named
13643 @file{test;.c} in the current directory; and this in turn
13644 mistakenly attempts to invoke @command{cat} on the file @file{test} and
13645 then execute the command @command{.c}.  To avoid this problem, use
13646 @samp{eval "$cmd"} rather than @samp{eval $cmd}.
13648 However, suppose that you want to output the text of the evaluated
13649 command just before executing it.  Assuming the previous example,
13650 @samp{echo "Executing: $cmd"} outputs @samp{Executing: cat test?.c}, but
13651 this output doesn't show the user that @samp{test;.c} is the actual name
13652 of the copied file.  Conversely, @samp{eval "echo Executing: $cmd"}
13653 works on this example, but it fails with @samp{cmd='cat foo >bar'},
13654 since it mistakenly replaces the contents of @file{bar} by the
13655 string @samp{cat foo}.  No simple, general, and portable solution to
13656 this problem is known.
13658 You should also be wary of common bugs in @command{eval} implementations.
13659 In some shell implementations (e.g., older @command{ash}, Open@acronym{BSD} 3.8
13660 @command{sh}, @command{pdksh} v5.2.14 99/07/13.2, and @command{zsh}
13661 4.2.5), the arguments of @samp{eval} are evaluated in a context where
13662 @samp{$?} is 0, so they exhibit behavior like this:
13664 @example
13665 $ @kbd{false; eval 'echo $?'}
13667 @end example
13669 The correct behavior here is to output a nonzero value,
13670 but portable scripts should not rely on this.
13672 You should not rely on @code{LINENO} within @command{eval}.
13673 @xref{Special Shell Variables}.
13675 @item @command{exit}
13676 @c -----------------
13677 @prindex @command{exit}
13678 The default value of @command{exit} is supposed to be @code{$?};
13679 unfortunately, some shells, such as the @acronym{DJGPP} port of Bash 2.04, just
13680 perform @samp{exit 0}.
13682 @example
13683 bash-2.04$ @kbd{foo=`exit 1` || echo fail}
13684 fail
13685 bash-2.04$ @kbd{foo=`(exit 1)` || echo fail}
13686 fail
13687 bash-2.04$ @kbd{foo=`(exit 1); exit` || echo fail}
13688 bash-2.04$
13689 @end example
13691 Using @samp{exit $?} restores the expected behavior.
13693 Some shell scripts, such as those generated by @command{autoconf}, use a
13694 trap to clean up before exiting.  If the last shell command exited with
13695 nonzero status, the trap also exits with nonzero status so that the
13696 invoker can tell that an error occurred.
13698 Unfortunately, in some shells, such as Solaris @command{/bin/sh}, an exit
13699 trap ignores the @code{exit} command's argument.  In these shells, a trap
13700 cannot determine whether it was invoked by plain @code{exit} or by
13701 @code{exit 1}.  Instead of calling @code{exit} directly, use the
13702 @code{AC_MSG_ERROR} macro that has a workaround for this problem.
13705 @item @command{export}
13706 @c -------------------
13707 @prindex @command{export}
13708 The builtin @command{export} dubs a shell variable @dfn{environment
13709 variable}.  Each update of exported variables corresponds to an update
13710 of the environment variables.  Conversely, each environment variable
13711 received by the shell when it is launched should be imported as a shell
13712 variable marked as exported.
13714 Alas, many shells, such as Solaris @command{/bin/sh},
13715 @sc{irix} 6.3, @sc{irix} 5.2,
13716 @acronym{AIX} 4.1.5, and Digital Unix 4.0, forget to
13717 @command{export} the environment variables they receive.  As a result,
13718 two variables coexist: the environment variable and the shell
13719 variable.  The following code demonstrates this failure:
13721 @example
13722 #!/bin/sh
13723 echo $FOO
13724 FOO=bar
13725 echo $FOO
13726 exec /bin/sh $0
13727 @end example
13729 @noindent
13730 when run with @samp{FOO=foo} in the environment, these shells print
13731 alternately @samp{foo} and @samp{bar}, although they should print only
13732 @samp{foo} and then a sequence of @samp{bar}s.
13734 Therefore you should @command{export} again each environment variable
13735 that you update.
13738 @item @command{false}
13739 @c ------------------
13740 @prindex @command{false}
13741 Don't expect @command{false} to exit with status 1: in native
13742 Solaris @file{/bin/false} exits with status 255.
13745 @item @command{for}
13746 @c ----------------
13747 @prindex @command{for}
13748 To loop over positional arguments, use:
13750 @example
13751 for arg
13753   echo "$arg"
13754 done
13755 @end example
13757 @noindent
13758 You may @emph{not} leave the @code{do} on the same line as @code{for},
13759 since some shells improperly grok:
13761 @example
13762 for arg; do
13763   echo "$arg"
13764 done
13765 @end example
13767 If you want to explicitly refer to the positional arguments, given the
13768 @samp{$@@} bug (@pxref{Shell Substitutions}), use:
13770 @example
13771 for arg in $@{1+"$@@"@}; do
13772   echo "$arg"
13773 done
13774 @end example
13776 @noindent
13777 But keep in mind that Zsh, even in Bourne shell emulation mode, performs
13778 word splitting on @samp{$@{1+"$@@"@}}; see @ref{Shell Substitutions},
13779 item @samp{$@@}, for more.
13782 @item @command{if}
13783 @c ---------------
13784 @prindex @command{if}
13785 Using @samp{!} is not portable.  Instead of:
13787 @example
13788 if ! cmp -s file file.new; then
13789   mv file.new file
13791 @end example
13793 @noindent
13794 use:
13796 @example
13797 if cmp -s file file.new; then :; else
13798   mv file.new file
13800 @end example
13802 There are shells that do not reset the exit status from an @command{if}:
13804 @example
13805 $ @kbd{if (exit 42); then true; fi; echo $?}
13807 @end example
13809 @noindent
13810 whereas a proper shell should have printed @samp{0}.  This is especially
13811 bad in makefiles since it produces false failures.  This is why properly
13812 written makefiles, such as Automake's, have such hairy constructs:
13814 @example
13815 if test -f "$file"; then
13816   install "$file" "$dest"
13817 else
13818   :
13820 @end example
13823 @item @command{printf}
13824 @c ------------------
13825 @prindex @command{printf}
13826 A format string starting with a @samp{-} can cause problems.
13827 Bash interprets it as an option and
13828 gives an error.  And @samp{--} to mark the end of options is not good
13829 in the Net@acronym{BSD} Almquist shell (e.g., 0.4.6) which takes that
13830 literally as the format string.  Putting the @samp{-} in a @samp{%c}
13831 or @samp{%s} is probably easiest:
13833 @example
13834 printf %s -foo
13835 @end example
13837 Bash 2.03 mishandles an escape sequence that happens to evaluate to @samp{%}:
13839 @example
13840 $ @kbd{printf '\045'}
13841 bash: printf: `%': missing format character
13842 @end example
13844 Large outputs may cause trouble.  On Solaris 2.5.1 through 10, for
13845 example, @file{/usr/bin/printf} is buggy, so when using
13846 @command{/bin/sh} the command @samp{printf %010000x 123} normally dumps
13847 core.
13850 @item @command{read}
13851 @c ------------------
13852 @prindex @command{read}
13853 Not all shells support @option{-r} (Solaris @command{/bin/sh} for example).
13856 @item @command{pwd}
13857 @c ----------------
13858 @prindex @command{pwd}
13859 With modern shells, plain @command{pwd} outputs a ``logical''
13860 directory name, some of whose components may be symbolic links.  These
13861 directory names are in contrast to ``physical'' directory names, whose
13862 components are all directories.
13864 Posix 1003.1-2001 requires that @command{pwd} must support
13865 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
13866 with @option{-L} being the default.  However, traditional shells do
13867 not support these options, and their @command{pwd} command has the
13868 @option{-P} behavior.
13870 Portable scripts should assume neither option is supported, and should
13871 assume neither behavior is the default.  Also, on many hosts
13872 @samp{/bin/pwd} is equivalent to @samp{pwd -P}, but Posix
13873 does not require this behavior and portable scripts should not rely on
13876 Typically it's best to use plain @command{pwd}.  On modern hosts this
13877 outputs logical directory names, which have the following advantages:
13879 @itemize @bullet
13880 @item
13881 Logical names are what the user specified.
13882 @item
13883 Physical names may not be portable from one installation
13884 host to another due to network file system gymnastics.
13885 @item
13886 On modern hosts @samp{pwd -P} may fail due to lack of permissions to
13887 some parent directory, but plain @command{pwd} cannot fail for this
13888 reason.
13889 @end itemize
13891 Also please see the discussion of the @command{cd} command.
13894 @item @command{set}
13895 @c ----------------
13896 @prindex @command{set}
13897 With the Free@acronym{BSD} 6.0 shell, the @command{set} command (without
13898 any options) does not sort its output.
13900 The @command{set} builtin faces the usual problem with arguments
13901 starting with a
13902 dash.  Modern shells such as Bash or Zsh understand @option{--} to specify
13903 the end of the options (any argument after @option{--} is a parameter,
13904 even @samp{-x} for instance), but many traditional shells (e.g., Solaris
13905 10 @command{/bin/sh}) simply stop option
13906 processing as soon as a non-option argument is found.  Therefore, use
13907 @samp{dummy} or simply @samp{x} to end the option processing, and use
13908 @command{shift} to pop it out:
13910 @example
13911 set x $my_list; shift
13912 @end example
13914 Avoid @samp{set -}, e.g., @samp{set - $my_list}.  Posix no
13915 longer requires support for this command, and in traditional shells
13916 @samp{set - $my_list} resets the @option{-v} and @option{-x} options, which
13917 makes scripts harder to debug.
13919 Some nonstandard shells do not recognize more than one option
13920 (e.g., @samp{set -e -x} assigns @samp{-x} to the command line).  It is
13921 better to combine them:
13923 @example
13924 set -ex
13925 @end example
13927 The @acronym{BSD} shell has had several problems with the @option{-e}
13928 option, partly because @acronym{BSD} @command{make} traditionally used
13929 @option{-e} even though this was incompatible with Posix
13930 (@pxref{Failure in Make Rules}).  Older versions of the @acronym{BSD}
13931 shell (circa 1990) mishandled @samp{&&}, @samp{||}, @samp{if}, and
13932 @samp{case} when @option{-e} was in effect, causing the shell to exit
13933 unexpectedly in some cases.  This was particularly a problem with
13934 makefiles, and led to circumlocutions like @samp{sh -c 'test -f file ||
13935 touch file'}, where the seemingly-unnecessary @samp{sh -c '@dots{}'}
13936 wrapper works around the bug.
13938 Even relatively-recent versions of the @acronym{BSD} shell (e.g.,
13939 Open@acronym{BSD} 3.4) wrongly exit with @option{-e} if a command within
13940 @samp{&&} fails inside a compound statement.  For example:
13942 @example
13943 #! /bin/sh
13944 set -e
13945 foo=''
13946 test -n "$foo" && exit 1
13947 echo one
13948 if :; then
13949   test -n "$foo" && exit 1
13951 echo two
13952 @end example
13954 @noindent
13955 does not print @samp{two}.  One workaround is to use @samp{if test -n
13956 "$foo"; then exit 1; fi} rather than @samp{test -n "$foo" && exit 1}.
13957 Another possibility is to warn @acronym{BSD} users not to use @samp{sh -e}.
13960 @item @command{shift}
13961 @c ------------------
13962 @prindex @command{shift}
13963 Not only is @command{shift}ing a bad idea when there is nothing left to
13964 shift, but in addition it is not portable: the shell of @acronym{MIPS
13965 RISC/OS} 4.52 refuses to do it.
13967 Don't use @samp{shift 2} etc.; it was not in the 7th Edition Bourne shell,
13968 and it is also absent in many pre-Posix shells.
13971 @item @command{source}
13972 @c -------------------
13973 @prindex @command{source}
13974 This command is not portable, as Posix does not require it; use
13975 @command{.} instead.
13978 @item @command{test}
13979 @c -----------------
13980 @prindex @command{test}
13981 The @code{test} program is the way to perform many file and string
13982 tests.  It is often invoked by the alternate name @samp{[}, but using
13983 that name in Autoconf code is asking for trouble since it is an M4 quote
13984 character.
13986 The @option{-a}, @option{-o}, @samp{(}, and @samp{)} operands are not
13987 portable and should be avoided.  Thus, portable uses of @command{test}
13988 should never have more than four arguments, and scripts should use shell
13989 constructs like @samp{&&} and @samp{||} instead.  If you combine
13990 @samp{&&} and @samp{||} in the same statement, keep in mind that they
13991 have equal precedence, so it is often better to parenthesize even when
13992 this is redundant.  For example:
13994 @smallexample
13995 # Not portable:
13996 test "X$a" = "X$b" -a \
13997   '(' "X$c" != "X$d" -o "X$e" = "X$f" ')'
13999 # Portable:
14000 test "X$a" = "X$b" &&
14001   @{ test "X$c" != "X$d" || test "X$e" = "X$f"; @}
14002 @end smallexample
14004 @command{test} does not process options like most other commands do; for
14005 example, it does not recognize the @option{--} argument as marking the
14006 end of options.
14008 It is safe to use @samp{!} as a @command{test} operator.  For example,
14009 @samp{if test ! -d foo; @dots{}} is portable even though @samp{if ! test
14010 -d foo; @dots{}} is not.
14013 @item @command{test} (files)
14014 @c -------------------------
14015 To enable @command{configure} scripts to support cross-compilation, they
14016 shouldn't do anything that tests features of the build system instead of
14017 the host system.  But occasionally you may find it necessary to check
14018 whether some arbitrary file exists.  To do so, use @samp{test -f} or
14019 @samp{test -r}.  Do not use @samp{test -x}, because 4.3@acronym{BSD} does not
14020 have it.  Do not use @samp{test -e} either, because Solaris @command{/bin/sh}
14021 lacks it.  To test for symbolic links on systems that have them, use
14022 @samp{test -h} rather than @samp{test -L}; either form conforms to
14023 Posix 1003.1-2001, but older shells like Solaris 8
14024 @code{/bin/sh} support only @option{-h}.
14026 @item @command{test} (strings)
14027 @c ---------------------------
14028 Posix says that @samp{test "@var{string}"} succeeds if @var{string} is
14029 not null, but this usage is not portable to traditional platforms like
14030 Solaris 10 @command{/bin/sh}, which mishandle strings like @samp{!} and
14031 @samp{-n}.
14033 Posix also says that @samp{test ! "@var{string}"},
14034 @samp{test -n "@var{string}"} and
14035 @samp{test -z "@var{string}"} work with any string, but many
14036 shells (such as Solaris, @acronym{AIX} 3.2, @sc{unicos} 10.0.0.6,
14037 Digital Unix 4, etc.)@: get confused if
14038 @var{string} looks like an operator:
14040 @example
14041 $ @kbd{test -n =}
14042 test: argument expected
14043 $ @kbd{test ! -n}
14044 test: argument expected
14045 @end example
14047 Similarly, Posix says that both @samp{test "@var{string1}" = "@var{string2"}}
14048 and @samp{test "@var{string1}" != "@var{string2"}} work for any pairs of
14049 strings, but in practice this is not true for troublesome strings that
14050 look like operators or parentheses, or that begin with @samp{-}.
14052 It is best to protect such strings with a leading @samp{X}, e.g.,
14053 @samp{test "X@var{string}" != X} rather than @samp{test -n
14054 "@var{string}"} or @samp{test ! "@var{string}"}.
14056 It is common to find variations of the following idiom:
14058 @example
14059 test -n "`echo $ac_feature | sed 's/[-a-zA-Z0-9_]//g'`" &&
14060   @var{action}
14061 @end example
14063 @noindent
14064 to take an action when a token matches a given pattern.  Such constructs
14065 should be avoided by using:
14067 @example
14068 case $ac_feature in
14069   *[!-a-zA-Z0-9_]*) @var{action};;
14070 esac
14071 @end example
14073 If the pattern is a complicated regular expression that cannot be
14074 expressed as a shell pattern, use something like this instead:
14076 @example
14077 expr "X$ac_feature" : 'X.*[^-a-zA-Z0-9_]' >/dev/null &&
14078   @var{action}
14079 @end example
14081 @samp{expr "X@var{foo}" : "X@var{bar}"} is more robust than @samp{echo
14082 "X@var{foo}" | grep "^X@var{bar}"}, because it avoids problems when
14083 @samp{@var{foo}} contains backslashes.
14086 @item @command{trap}
14087 @c -----------------
14088 @prindex @command{trap}
14089 It is safe to trap at least the signals 1, 2, 13, and 15.  You can also
14090 trap 0, i.e., have the @command{trap} run when the script ends (either via an
14091 explicit @command{exit}, or the end of the script).  The trap for 0 should be
14092 installed outside of a shell function, or @acronym{AIX} 5.3 @command{/bin/sh}
14093 will invoke the trap at the end of this function.
14095 Posix says that @samp{trap - 1 2 13 15} resets the traps for the
14096 specified signals to their default values, but many common shells (e.g.,
14097 Solaris @command{/bin/sh}) misinterpret this and attempt to execute a
14098 ``command'' named @command{-} when the specified conditions arise.
14099 There is no portable workaround, except for @samp{trap - 0}, for which
14100 @samp{trap '' 0} is a portable substitute.
14102 Although Posix is not absolutely clear on this point, it is widely
14103 admitted that when entering the trap @samp{$?} should be set to the exit
14104 status of the last command run before the trap.  The ambiguity can be
14105 summarized as: ``when the trap is launched by an @command{exit}, what is
14106 the @emph{last} command run: that before @command{exit}, or
14107 @command{exit} itself?''
14109 Bash considers @command{exit} to be the last command, while Zsh and
14110 Solaris @command{/bin/sh} consider that when the trap is run it is
14111 @emph{still} in the @command{exit}, hence it is the previous exit status
14112 that the trap receives:
14114 @example
14115 $ @kbd{cat trap.sh}
14116 trap 'echo $?' 0
14117 (exit 42); exit 0
14118 $ @kbd{zsh trap.sh}
14120 $ @kbd{bash trap.sh}
14122 @end example
14124 The portable solution is then simple: when you want to @samp{exit 42},
14125 run @samp{(exit 42); exit 42}, the first @command{exit} being used to
14126 set the exit status to 42 for Zsh, and the second to trigger the trap
14127 and pass 42 as exit status for Bash.
14129 The shell in Free@acronym{BSD} 4.0 has the following bug: @samp{$?} is
14130 reset to 0 by empty lines if the code is inside @command{trap}.
14132 @example
14133 $ @kbd{trap 'false}
14135 echo $?' 0
14136 $ @kbd{exit}
14138 @end example
14140 @noindent
14141 Fortunately, this bug only affects @command{trap}.
14143 @item @command{true}
14144 @c -----------------
14145 @prindex @command{true}
14146 @c Info cannot handle `:' in index entries.
14147 @c @prindex @command{:}
14148 Don't worry: as far as we know @command{true} is portable.
14149 Nevertheless, it's not always a builtin (e.g., Bash 1.x), and the
14150 portable shell community tends to prefer using @command{:}.  This has a
14151 funny side effect: when asked whether @command{false} is more portable
14152 than @command{true} Alexandre Oliva answered:
14154 @quotation
14155 In a sense, yes, because if it doesn't exist, the shell will produce an
14156 exit status of failure, which is correct for @command{false}, but not
14157 for @command{true}.
14158 @end quotation
14161 @item @command{unset}
14162 @c ------------------
14163 @prindex @command{unset}
14164 In some nonconforming shells (e.g., Bash 2.05a), @code{unset FOO} fails
14165 when @code{FOO} is not set.  Also, Bash 2.01 mishandles @code{unset
14166 MAIL} in some cases and dumps core.
14168 A few ancient shells lack @command{unset} entirely.  Nevertheless, because
14169 it is extremely useful to disable embarrassing variables such as
14170 @code{PS1}, you can test for its existence and use
14171 it @emph{provided} you give a neutralizing value when @command{unset} is
14172 not supported:
14174 @smallexample
14175 # "|| exit" suppresses any "Segmentation fault" message.
14176 if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then
14177   unset=unset
14178 else
14179   unset=false
14181 $unset PS1 || PS1='$ '
14182 @end smallexample
14184 @noindent
14185 @xref{Special Shell Variables}, for some neutralizing values.  Also, see
14186 @ref{Limitations of Builtins}, documentation of @command{export}, for
14187 the case of environment variables.
14188 @end table
14190 @node Limitations of Usual Tools
14191 @section Limitations of Usual Tools
14192 @cindex Limitations of usual tools
14194 The small set of tools you can expect to find on any machine can still
14195 include some limitations you should be aware of.
14197 @table @asis
14198 @item Awk
14199 @c ------
14200 @prindex Awk
14201 Don't leave white space before the opening parenthesis in a user function call.
14202 Posix does not allow this and @acronym{GNU} Awk rejects it:
14204 @example
14205 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
14206         BEGIN @{ die () @}'}
14207 gawk: cmd. line:2:         BEGIN @{ die () @}
14208 gawk: cmd. line:2:                      ^ parse error
14209 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
14210         BEGIN @{ die() @}'}
14211 Aaaaarg!
14212 @end example
14214 Posix says that if a program contains only @samp{BEGIN} actions, and
14215 contains no instances of @code{getline}, then the program merely
14216 executes the actions without reading input.  However, traditional Awk
14217 implementations (such as Solaris 10 @command{awk}) read and discard
14218 input in this case.  Portable scripts can redirect input from
14219 @file{/dev/null} to work around the problem.  For example:
14221 @example
14222 awk 'BEGIN @{print "hello world"@}' </dev/null
14223 @end example
14225 Posix says that in an @samp{END} action, @samp{$NF} (and presumably,
14226 @samp{$1}) retain their value from the last record read, if no
14227 intervening @samp{getline} occurred.  However, some implementations
14228 (such as Solaris 10 @samp{/usr/bin/awk}, @samp{nawk}, or Darwin
14229 @samp{awk}) reset these variables.  A workaround is to use an
14230 intermediate variable prior to the @samp{END} block.  For example:
14232 @example
14233 $ @kbd{cat end.awk}
14234 @{ tmp = $1 @}
14235 END @{ print "a", $1, $NF, "b", tmp @}
14236 $ @kbd{echo 1 | awk -f end.awk}
14237 a   b 1
14238 $ @kbd{echo 1 | gawk -f end.awk}
14239 a 1 1 b 1
14240 @end example
14242 If you want your program to be deterministic, don't depend on @code{for}
14243 on arrays:
14245 @example
14246 $ @kbd{cat for.awk}
14247 END @{
14248   arr["foo"] = 1
14249   arr["bar"] = 1
14250   for (i in arr)
14251     print i
14253 $ @kbd{gawk -f for.awk </dev/null}
14256 $ @kbd{nawk -f for.awk </dev/null}
14259 @end example
14261 Some Awk implementations, such as @acronym{HP-UX} 11.0's native one,
14262 mishandle anchors:
14264 @example
14265 $ @kbd{echo xfoo | $AWK '/foo|^bar/ @{ print @}'}
14266 $ @kbd{echo bar | $AWK '/foo|^bar/ @{ print @}'}
14268 $ @kbd{echo xfoo | $AWK '/^bar|foo/ @{ print @}'}
14269 xfoo
14270 $ @kbd{echo bar | $AWK '/^bar|foo/ @{ print @}'}
14272 @end example
14274 @noindent
14275 Either do not depend on such patterns (i.e., use @samp{/^(.*foo|bar)/},
14276 or use a simple test to reject such implementations.
14278 @acronym{AIX} version 5.2 has an arbitrary limit of 399 on the
14279 length of regular expressions and literal strings in an Awk program.
14281 Traditional Awk implementations derived from Unix version 7, such as
14282 Solaris @command{/bin/awk}, have many limitations and do not
14283 conform to Posix.  Nowadays @code{AC_PROG_AWK} (@pxref{Particular
14284 Programs}) finds you an Awk that doesn't have these problems, but if
14285 for some reason you prefer not to use @code{AC_PROG_AWK} you may need to
14286 address them.
14288 Traditional Awk does not support multidimensional arrays or user-defined
14289 functions.
14291 Traditional Awk does not support the @option{-v} option.  You can use
14292 assignments after the program instead, e.g., @command{$AWK '@{print v
14293 $1@}' v=x}; however, don't forget that such assignments are not
14294 evaluated until they are encountered (e.g., after any @code{BEGIN}
14295 action).
14297 Traditional Awk does not support the keywords @code{delete} or @code{do}.
14299 Traditional Awk does not support the expressions
14300 @code{@var{a}?@var{b}:@var{c}}, @code{!@var{a}}, @code{@var{a}^@var{b}},
14301 or @code{@var{a}^=@var{b}}.
14303 Traditional Awk does not support the predefined @code{CONVFMT} variable.
14305 Traditional Awk supports only the predefined functions @code{exp}, @code{index},
14306 @code{int}, @code{length}, @code{log}, @code{split}, @code{sprintf},
14307 @code{sqrt}, and @code{substr}.
14309 Traditional Awk @code{getline} is not at all compatible with Posix;
14310 avoid it.
14312 Traditional Awk has @code{for (i in a) @dots{}} but no other uses of the
14313 @code{in} keyword.  For example, it lacks @code{if (i in a) @dots{}}.
14315 In code portable to both traditional and modern Awk, @code{FS} must be a
14316 string containing just one ordinary character, and similarly for the
14317 field-separator argument to @code{split}.
14319 Traditional Awk has a limit of 99
14320 fields in a record.  You may be able to circumvent this problem by using
14321 @code{split}.
14323 Traditional Awk has a limit of at most 99 bytes in a number formatted by
14324 @code{OFMT}; for example, @code{OFMT="%.300e"; print 0.1;} typically
14325 dumps core.
14327 The original version of Awk had a limit of at most 99 bytes per
14328 @code{split} field, 99 bytes per @code{substr} substring, and 99 bytes
14329 per run of non-special characters in a @code{printf} format, but these
14330 bugs have been fixed on all practical hosts that we know of.
14332 @item @command{basename}
14333 @c ---------------------
14334 @prindex @command{basename}
14335 Not all hosts have a working @command{basename}.
14336 You can use @command{expr} instead.
14338 @c AS_BASENAME is to be replaced by a better API.
14339 @ignore
14340 Not all hosts have a working @command{basename}, and you should instead
14341 use @code{AS_BASENAME} (@pxref{Programming in M4sh}), followed by
14342 @command{expr} if you need to strip a suffix.  For example:
14344 @example
14345 a=`basename "$aname"`       # This is not portable.
14346 a=`AS_BASENAME(["$aname"])` # This is more portable.
14348 # This is not portable.
14349 c=`basename "$cname" .c`
14351 # This is more portable.
14352 c=`AS_BASENAME(["$cname"])`
14353 case $c in
14354 ?*.c) c=`expr "X$c" : 'X\(.*\)\.c'`;;
14355 esac
14356 @end example
14357 @end ignore
14360 @item @command{cat}
14361 @c ----------------
14362 @prindex @command{cat}
14363 Don't rely on any option.
14366 @item @command{cc}
14367 @c ---------------
14368 @prindex @command{cc}
14369 The command @samp{cc -c foo.c} traditionally produces an object file
14370 named @file{foo.o}.  Most compilers allow @option{-c} to be combined
14371 with @option{-o} to specify a different object file name, but
14372 Posix does not require this combination and a few compilers
14373 lack support for it.  @xref{C Compiler}, for how @acronym{GNU} Make
14374 tests for this feature with @code{AC_PROG_CC_C_O}.
14376 When a compilation such as @samp{cc -o foo foo.c} fails, some compilers
14377 (such as @sc{cds} on Reliant Unix) leave a @file{foo.o}.
14379 @acronym{HP-UX} @command{cc} doesn't accept @file{.S} files to preprocess and
14380 assemble.  @samp{cc -c foo.S} appears to succeed, but in fact does
14381 nothing.
14383 The default executable, produced by @samp{cc foo.c}, can be
14385 @itemize
14386 @item @file{a.out} --- usual Posix convention.
14387 @item @file{b.out} --- i960 compilers (including @command{gcc}).
14388 @item @file{a.exe} --- @acronym{DJGPP} port of @command{gcc}.
14389 @item @file{a_out.exe} --- GNV @command{cc} wrapper for DEC C on OpenVMS.
14390 @item @file{foo.exe} --- various MS-DOS compilers.
14391 @end itemize
14393 The C compiler's traditional name is @command{cc}, but other names like
14394 @command{gcc} are common.  Posix 1003.1-2001 specifies the
14395 name @command{c99}, but older Posix editions specified
14396 @command{c89} and anyway these standard names are rarely used in
14397 practice.  Typically the C compiler is invoked from makefiles that use
14398 @samp{$(CC)}, so the value of the @samp{CC} make variable selects the
14399 compiler name.
14402 @item @command{chmod}
14403 @c ------------------
14404 @prindex @command{chmod}
14405 Avoid usages like @samp{chmod -w file}; use @samp{chmod a-w file}
14406 instead, for two reasons.  First, plain @option{-w} does not necessarily
14407 make the file unwritable, since it does not affect mode bits that
14408 correspond to bits in the file mode creation mask.  Second,
14409 Posix says that the @option{-w} might be interpreted as an
14410 implementation-specific option, not as a mode; Posix suggests
14411 using @samp{chmod -- -w file} to avoid this confusion, but unfortunately
14412 @samp{--} does not work on some older hosts.
14415 @item @command{cmp}
14416 @c ----------------
14417 @prindex @command{cmp}
14418 @command{cmp} performs a raw data comparison of two files, while
14419 @command{diff} compares two text files.  Therefore, if you might compare
14420 DOS files, even if only checking whether two files are different, use
14421 @command{diff} to avoid spurious differences due to differences of
14422 newline encoding.
14425 @item @command{cp}
14426 @c ---------------
14427 @prindex @command{cp}
14428 Avoid the @option{-r} option, since Posix 1003.1-2004 marks it as
14429 obsolescent and its behavior on special files is implementation-defined.
14430 Use @option{-R} instead.  On @acronym{GNU} hosts the two options
14431 are equivalent, but on Solaris hosts (for example) @command{cp -r}
14432 reads from pipes instead of replicating them.
14434 Some @command{cp} implementations (e.g., @acronym{BSD/OS} 4.2) do not allow
14435 trailing slashes at the end of nonexistent destination directories.  To
14436 avoid this problem, omit the trailing slashes.  For example, use
14437 @samp{cp -R source /tmp/newdir} rather than @samp{cp -R source
14438 /tmp/newdir/} if @file{/tmp/newdir} does not exist.
14440 @c This is thanks to Ian.
14441 The ancient SunOS 4 @command{cp} does not support @option{-f}, although
14442 its @command{mv} does.
14444 @cindex timestamp resolution
14445 Traditionally, file timestamps had 1-second resolution, and @samp{cp
14446 -p} copied the timestamps exactly.  However, many modern file systems
14447 have timestamps with 1-nanosecond resolution.  Unfortunately, @samp{cp
14448 -p} implementations truncate timestamps when copying files, so this
14449 can result in the destination file appearing to be older than the
14450 source.  The exact amount of truncation depends on the resolution of
14451 the system calls that @command{cp} uses; traditionally this was
14452 @code{utime}, which has 1-second resolution, but some newer
14453 @command{cp} implementations use @code{utimes}, which has
14454 1-microsecond resolution.  These newer implementations include @acronym{GNU}
14455 Core Utilities 5.0.91 or later, and Solaris 8 (sparc) patch 109933-02 or
14456 later.  Unfortunately as of January 2006 there is still no system
14457 call to set timestamps to the full nanosecond resolution.
14459 Bob Proulx notes that @samp{cp -p} always @emph{tries} to copy
14460 ownerships.  But whether it actually does copy ownerships or not is a
14461 system dependent policy decision implemented by the kernel.  If the
14462 kernel allows it then it happens.  If the kernel does not allow it then
14463 it does not happen.  It is not something @command{cp} itself has control
14464 over.
14466 In Unix System V any user can chown files to any other user, and System
14467 V also has a non-sticky @file{/tmp}.  That probably derives from the
14468 heritage of System V in a business environment without hostile users.
14469 @acronym{BSD} changed this
14470 to be a more secure model where only root can @command{chown} files and
14471 a sticky @file{/tmp} is used.  That undoubtedly derives from the heritage
14472 of @acronym{BSD} in a campus environment.
14474 @acronym{GNU}/Linux and Solaris by default follow @acronym{BSD}, but
14475 can be configured to allow a System V style @command{chown}.  On the
14476 other hand, @acronym{HP-UX} follows System V, but can
14477 be configured to use the modern security model and disallow
14478 @command{chown}.  Since it is an administrator-configurable parameter
14479 you can't use the name of the kernel as an indicator of the behavior.
14483 @item @command{date}
14484 @c -----------------
14485 @prindex @command{date}
14486 Some versions of @command{date} do not recognize special @samp{%} directives,
14487 and unfortunately, instead of complaining, they just pass them through,
14488 and exit with success:
14490 @example
14491 $ @kbd{uname -a}
14492 OSF1 medusa.sis.pasteur.fr V5.1 732 alpha
14493 $ @kbd{date "+%s"}
14495 @end example
14498 @item @command{diff}
14499 @c -----------------
14500 @prindex @command{diff}
14501 Option @option{-u} is nonportable.
14503 Some implementations, such as Tru64's, fail when comparing to
14504 @file{/dev/null}.  Use an empty file instead.
14507 @item @command{dirname}
14508 @c --------------------
14509 @prindex @command{dirname}
14510 Not all hosts have a working @command{dirname}, and you should instead
14511 use @code{AS_DIRNAME} (@pxref{Programming in M4sh}).  For example:
14513 @example
14514 dir=`dirname "$file"`       # This is not portable.
14515 dir=`AS_DIRNAME(["$file"])` # This is more portable.
14516 @end example
14519 @item @command{egrep}
14520 @c ------------------
14521 @prindex @command{egrep}
14522 Posix 1003.1-2001 no longer requires @command{egrep},
14523 but many hosts do not yet support the Posix
14524 replacement @code{grep -E}.  Also, some traditional implementations do
14525 not work on long input lines.  To work around these problems, invoke
14526 @code{AC_PROG_EGREP} and then use @code{$EGREP}.
14528 Portable extended regular expressions should use @samp{\} only to escape
14529 characters in the string @samp{$()*+.?[\^@{|}.  For example, @samp{\@}}
14530 is not portable, even though it typically matches @samp{@}}.
14532 The empty alternative is not portable.  Use @samp{?} instead.  For
14533 instance with Digital Unix v5.0:
14535 @example
14536 > printf "foo\n|foo\n" | $EGREP '^(|foo|bar)$'
14537 |foo
14538 > printf "bar\nbar|\n" | $EGREP '^(foo|bar|)$'
14539 bar|
14540 > printf "foo\nfoo|\n|bar\nbar\n" | $EGREP '^(foo||bar)$'
14542 |bar
14543 @end example
14545 @command{$EGREP} also suffers the limitations of @command{grep}.
14547 @item @command{expr}
14548 @c -----------------
14549 @prindex @command{expr}
14550 No @command{expr} keyword starts with @samp{X}, so use @samp{expr
14551 X"@var{word}" : 'X@var{regex}'} to keep @command{expr} from
14552 misinterpreting @var{word}.
14554 Don't use @code{length}, @code{substr}, @code{match} and @code{index}.
14556 @item @command{expr} (@samp{|})
14557 @prindex @command{expr} (@samp{|})
14558 You can use @samp{|}.  Although Posix does require that @samp{expr
14559 ''} return the empty string, it does not specify the result when you
14560 @samp{|} together the empty string (or zero) with the empty string.  For
14561 example:
14563 @example
14564 expr '' \| ''
14565 @end example
14567 Posix 1003.2-1992 returns the empty string
14568 for this case, but traditional Unix returns @samp{0} (Solaris is
14569 one such example).  In Posix 1003.1-2001, the specification was
14570 changed to match traditional Unix's behavior (which is
14571 bizarre, but it's too late to fix this).  Please note that the same
14572 problem does arise when the empty string results from a computation,
14573 as in:
14575 @example
14576 expr bar : foo \| foo : bar
14577 @end example
14579 @noindent
14580 Avoid this portability problem by avoiding the empty string.
14583 @item @command{expr} (@samp{:})
14584 @c ----------------------------
14585 @prindex @command{expr}
14586 Portable @command{expr} regular expressions should use @samp{\} to
14587 escape only characters in the string @samp{$()*.0123456789[\^n@{@}}.
14588 For example, alternation, @samp{\|}, is common but Posix does not
14589 require its support, so it should be avoided in portable scripts.
14590 Similarly, @samp{\+} and @samp{\?} should be avoided.
14592 Portable @command{expr} regular expressions should not begin with
14593 @samp{^}.  Patterns are automatically anchored so leading @samp{^} is
14594 not needed anyway.
14596 The Posix standard is ambiguous as to whether
14597 @samp{expr 'a' : '\(b\)'} outputs @samp{0} or the empty string.
14598 In practice, it outputs the empty string on most platforms, but portable
14599 scripts should not assume this.  For instance, the @acronym{QNX} 4.25 native
14600 @command{expr} returns @samp{0}.
14602 One might think that a way to get a uniform behavior would be to use
14603 the empty string as a default value:
14605 @example
14606 expr a : '\(b\)' \| ''
14607 @end example
14609 @noindent
14610 Unfortunately this behaves exactly as the original expression; see the
14611 @command{expr} (@samp{|}) entry for more information.
14613 Some ancient @command{expr} implementations (e.g., SunOS 4 @command{expr} and
14614 Solaris 8 @command{/usr/ucb/expr}) have a silly length limit that causes
14615 @command{expr} to fail if the matched substring is longer than 120
14616 bytes.  In this case, you might want to fall back on @samp{echo|sed} if
14617 @command{expr} fails.  Nowadays this is of practical importance only for
14618 the rare installer who mistakenly puts @file{/usr/ucb} before
14619 @file{/usr/bin} in @env{PATH}.
14621 On Mac OS X 10.4, @command{expr} mishandles the pattern @samp{[^-]} in
14622 some cases.  For example, the command
14623 @example
14624 expr Xpowerpc-apple-darwin8.1.0 : 'X[^-]*-[^-]*-\(.*\)'
14625 @end example
14627 @noindent
14628 outputs @samp{apple-darwin8.1.0} rather than the correct @samp{darwin8.1.0}.
14629 This particular case can be worked around by substituting @samp{[^--]}
14630 for @samp{[^-]}.
14632 Don't leave, there is some more!
14634 The @acronym{QNX} 4.25 @command{expr}, in addition of preferring @samp{0} to
14635 the empty string, has a funny behavior in its exit status: it's always 1
14636 when parentheses are used!
14638 @example
14639 $ @kbd{val=`expr 'a' : 'a'`; echo "$?: $val"}
14640 0: 1
14641 $ @kbd{val=`expr 'a' : 'b'`; echo "$?: $val"}
14642 1: 0
14644 $ @kbd{val=`expr 'a' : '\(a\)'`; echo "?: $val"}
14645 1: a
14646 $ @kbd{val=`expr 'a' : '\(b\)'`; echo "?: $val"}
14647 1: 0
14648 @end example
14650 @noindent
14651 In practice this can be a big problem if you are ready to catch failures
14652 of @command{expr} programs with some other method (such as using
14653 @command{sed}), since you may get twice the result.  For instance
14655 @example
14656 $ @kbd{expr 'a' : '\(a\)' || echo 'a' | sed 's/^\(a\)$/\1/'}
14657 @end example
14659 @noindent
14660 outputs @samp{a} on most hosts, but @samp{aa} on @acronym{QNX} 4.25.  A
14661 simple workaround consists of testing @command{expr} and using a variable
14662 set to @command{expr} or to @command{false} according to the result.
14664 Tru64 @command{expr} incorrectly treats the result as a number, if it
14665 can be interpreted that way:
14667 @example
14668 $ @kbd{expr 00001 : '.*\(...\)'}
14670 @end example
14673 @item @command{fgrep}
14674 @c ------------------
14675 @prindex @command{fgrep}
14676 Posix 1003.1-2001 no longer requires @command{fgrep},
14677 but many hosts do not yet support the Posix
14678 replacement @code{grep -F}.  Also, some traditional implementations do
14679 not work on long input lines.  To work around these problems, invoke
14680 @code{AC_PROG_FGREP} and then use @code{$FGREP}.
14683 @item @command{find}
14684 @c -----------------
14685 @prindex @command{find}
14686 The option @option{-maxdepth} seems to be @acronym{GNU} specific.
14687 Tru64 v5.1, Net@acronym{BSD} 1.5 and Solaris @command{find}
14688 commands do not understand it.
14690 The replacement of @samp{@{@}} is guaranteed only if the argument is
14691 exactly @emph{@{@}}, not if it's only a part of an argument.  For
14692 instance on DU, and @acronym{HP-UX} 10.20 and @acronym{HP-UX} 11:
14694 @example
14695 $ @kbd{touch foo}
14696 $ @kbd{find . -name foo -exec echo "@{@}-@{@}" \;}
14697 @{@}-@{@}
14698 @end example
14700 @noindent
14701 while @acronym{GNU} @command{find} reports @samp{./foo-./foo}.
14704 @item @command{grep}
14705 @c -----------------
14706 @prindex @command{grep}
14707 Portable scripts can rely on the @command{grep} options @option{-c},
14708 @option{-l}, @option{-n}, and @option{-v}, but should avoid other
14709 options.  For example, don't use @option{-w}, as Posix does not require
14710 it and Irix 6.5.16m's @command{grep} does not support it.  Also,
14711 portable scripts should not combine @option{-c} with @option{-l},
14712 as Posix does not allow this.
14714 Some of the options required by Posix are not portable in practice.
14715 Don't use @samp{grep -q} to suppress output, because many @command{grep}
14716 implementations (e.g., Solaris) do not support @option{-q}.
14717 Don't use @samp{grep -s} to suppress output either, because Posix
14718 says @option{-s} does not suppress output, only some error messages;
14719 also, the @option{-s} option of traditional @command{grep} behaved
14720 like @option{-q} does in most modern implementations.  Instead,
14721 redirect the standard output and standard error (in case the file
14722 doesn't exist) of @code{grep} to @file{/dev/null}.  Check the exit
14723 status of @code{grep} to determine whether it found a match.
14725 Some traditional @command{grep} implementations do not work on long
14726 input lines.  On AIX the default @code{grep} silently truncates long
14727 lines on the input before matching.
14729 Also, many implementations do not support multiple regexps
14730 with @option{-e}: they either reject @option{-e} entirely (e.g., Solaris)
14731 or honor only the last pattern (e.g., @acronym{IRIX} 6.5 and NeXT).  To
14732 work around these problems, invoke @code{AC_PROG_GREP} and then use
14733 @code{$GREP}.
14735 Another possible workaround for the multiple @option{-e} problem is to
14736 separate the patterns by newlines, for example:
14738 @example
14739 grep 'foo
14740 bar' in.txt
14741 @end example
14743 @noindent
14744 except that this fails with traditional @command{grep}
14745 implementations and with Open@acronym{BSD} 3.8 @command{grep}.
14747 Traditional @command{grep} implementations (e.g., Solaris) do not
14748 support the @option{-E} or @option{-F} options.  To work around these
14749 problems, invoke @code{AC_PROG_EGREP} and then use @code{$EGREP}, and
14750 similarly for @code{AC_PROG_FGREP} and @code{$FGREP}.  Even if you are
14751 willing to require support for Posix @command{grep}, your script should
14752 not use both @option{-E} and @option{-F}, since Posix does not allow
14753 this combination.
14755 Portable @command{grep} regular expressions should use @samp{\} only to
14756 escape characters in the string @samp{$()*.0123456789[\^@{@}}.  For example,
14757 alternation, @samp{\|}, is common but Posix does not require its
14758 support in basic regular expressions, so it should be avoided in
14759 portable scripts.  Solaris and HP-UX @command{grep} do not support it.
14760 Similarly, the following escape sequences should also be avoided:
14761 @samp{\<}, @samp{\>}, @samp{\+}, @samp{\?}, @samp{\`}, @samp{\'},
14762 @samp{\B}, @samp{\b}, @samp{\S}, @samp{\s}, @samp{\W}, and @samp{\w}.
14765 @item @command{join}
14766 @c -----------------
14767 @prindex @command{join}
14768 Solaris 8 @command{join} has bugs when the second operand is standard
14769 input, and when standard input is a pipe.  For example, the following
14770 shell script causes Solaris 8 @command{join} to loop forever:
14772 @example
14773 cat >file <<'EOF'
14774 1 x
14775 2 y
14777 cat file | join file -
14778 @end example
14780 Use @samp{join - file} instead.
14783 @item @command{ln}
14784 @c ---------------
14785 @prindex @command{ln}
14786 @cindex Symbolic links
14787 Don't rely on @command{ln} having a @option{-f} option.  Symbolic links
14788 are not available on old systems; use @samp{$(LN_S)} as a portable substitute.
14790 For versions of the @acronym{DJGPP} before 2.04,
14791 @command{ln} emulates symbolic links
14792 to executables by generating a stub that in turn calls the real
14793 program.  This feature also works with nonexistent files like in the
14794 Posix spec.  So @samp{ln -s file link} generates @file{link.exe},
14795 which attempts to call @file{file.exe} if run.  But this feature only
14796 works for executables, so @samp{cp -p} is used instead for these
14797 systems.  @acronym{DJGPP} versions 2.04 and later have full support
14798 for symbolic links.
14801 @item @command{ls}
14802 @c ---------------
14803 @prindex @command{ls}
14804 @cindex Listing directories
14805 The portable options are @option{-acdilrtu}.  Current practice is for
14806 @option{-l} to output both owner and group, even though ancient versions
14807 of @command{ls} omitted the group.
14809 On ancient hosts, @samp{ls foo} sent the diagnostic @samp{foo not found}
14810 to standard output if @file{foo} did not exist.  Hence a shell command
14811 like @samp{sources=`ls *.c 2>/dev/null`} did not always work, since it
14812 was equivalent to @samp{sources='*.c not found'} in the absence of
14813 @samp{.c} files.  This is no longer a practical problem, since current
14814 @command{ls} implementations send diagnostics to standard error.
14816 @item @command{mkdir}
14817 @c ------------------
14818 @prindex @command{mkdir}
14819 @cindex Making directories
14820 No @command{mkdir} option is portable to older systems.  Instead of
14821 @samp{mkdir -p @var{file-name}}, you should use
14822 @code{AS_MKDIR_P(@var{file-name})} (@pxref{Programming in M4sh})
14823 or @code{AC_PROG_MKDIR_P} (@pxref{Particular Programs}).
14825 Combining the @option{-m} and @option{-p} options, as in @samp{mkdir -m
14826 go-w -p @var{dir}}, often leads to trouble.  Free@acronym{BSD}
14827 @command{mkdir} incorrectly attempts to change the permissions of
14828 @var{dir} even if it already exists.  @acronym{HP-UX} 11.23 and
14829 @acronym{IRIX} 6.5 @command{mkdir} often assign the wrong permissions to
14830 any newly-created parents of @var{dir}.
14832 Posix does not clearly specify whether @samp{mkdir -p foo}
14833 should succeed when @file{foo} is a symbolic link to an already-existing
14834 directory.  The @acronym{GNU} Core Utilities 5.1.0 @command{mkdir}
14835 succeeds, but Solaris @command{mkdir} fails.
14837 Traditional @code{mkdir -p} implementations suffer from race conditions.
14838 For example, if you invoke @code{mkdir -p a/b} and @code{mkdir -p a/c}
14839 at the same time, both processes might detect that @file{a} is missing,
14840 one might create @file{a}, then the other might try to create @file{a}
14841 and fail with a @code{File exists} diagnostic.  The @acronym{GNU} Core
14842 Utilities (@samp{fileutils} version 4.1), Free@acronym{BSD} 5.0,
14843 Net@acronym{BSD} 2.0.2, and Open@acronym{BSD} 2.4 are known to be
14844 race-free when two processes invoke @code{mkdir -p} simultaneously, but
14845 earlier versions are vulnerable.  Solaris @command{mkdir} is still
14846 vulnerable as of Solaris 10, and other traditional Unix systems are
14847 probably vulnerable too.  This possible race is harmful in parallel
14848 builds when several Make rules call @code{mkdir -p} to
14849 construct directories.  You may use
14850 @code{install-sh -d} as a safe replacement, provided this script is
14851 recent enough; the copy shipped with Autoconf 2.60 and Automake 1.10 is
14852 OK, but copies from older versions are vulnerable.
14855 @item @command{mktemp}
14856 @c -------------------
14857 @prindex @command{mktemp}
14858 @cindex Creating temporary files
14859 Shell scripts can use temporary files safely with @command{mktemp}, but
14860 it does not exist on all systems.  A portable way to create a safe
14861 temporary file name is to create a temporary directory with mode 700 and
14862 use a file inside this directory.  Both methods prevent attackers from
14863 gaining control, though @command{mktemp} is far less likely to fail
14864 gratuitously under attack.
14866 Here is sample code to create a new temporary directory safely:
14868 @example
14869 # Create a temporary directory $tmp in $TMPDIR (default /tmp).
14870 # Use mktemp if possible; otherwise fall back on mkdir,
14871 # with $RANDOM to make collisions less likely.
14872 : $@{TMPDIR=/tmp@}
14874   tmp=`
14875     (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
14876   ` &&
14877   test -n "$tmp" && test -d "$tmp"
14878 @} || @{
14879   tmp=$TMPDIR/foo$$-$RANDOM
14880   (umask 077 && mkdir "$tmp")
14881 @} || exit $?
14882 @end example
14885 @item @command{mv}
14886 @c ---------------
14887 @prindex @command{mv}
14888 @cindex Moving open files
14889 The only portable options are @option{-f} and @option{-i}.
14891 Moving individual files between file systems is portable (it was in Unix
14892 version 6),
14893 but it is not always atomic: when doing @samp{mv new existing}, there's
14894 a critical section where neither the old nor the new version of
14895 @file{existing} actually exists.
14897 On some systems moving files from @file{/tmp} can sometimes cause
14898 undesirable (but perfectly valid) warnings, even if you created these
14899 files.  This is because @file{/tmp} belongs to a group that ordinary
14900 users are not members of, and files created in @file{/tmp} inherit
14901 the group of @file{/tmp}.  When the file is copied, @command{mv} issues
14902 a diagnostic without failing:
14904 @smallexample
14905 $ @kbd{touch /tmp/foo}
14906 $ @kbd{mv /tmp/foo .}
14907 @error{}mv: ./foo: set owner/group (was: 100/0): Operation not permitted
14908 $ @kbd{echo $?}
14910 $ @kbd{ls foo}
14912 @end smallexample
14914 @noindent
14915 This annoying behavior conforms to Posix, unfortunately.
14917 Moving directories across mount points is not portable, use @command{cp}
14918 and @command{rm}.
14920 @acronym{DOS} variants cannot rename or remove open files, and do not
14921 support commands like @samp{mv foo bar >foo}, even though this is
14922 perfectly portable among Posix hosts.
14925 @item @command{od}
14926 @c ---------------
14927 @prindex @command{od}
14929 In Mac OS X 10.3, @command{od} does not support the
14930 standard Posix options @option{-A}, @option{-j}, @option{-N}, or
14931 @option{-t}, or the @acronym{XSI} option @option{-s}.  The only
14932 supported Posix option is @option{-v}, and the only supported
14933 @acronym{XSI} options are those in @option{-bcdox}.  The @acronym{BSD}
14934 @command{hexdump} program can be used instead.
14936 This problem no longer exists in Mac OS X 10.4.3.
14939 @item @command{rm}
14940 @c ---------------
14941 @prindex @command{rm}
14942 The @option{-f} and @option{-r} options are portable.
14944 It is not portable to invoke @command{rm} without operands.  For
14945 example, on many systems @samp{rm -f -r} (with no other arguments)
14946 silently succeeds without doing anything, but it fails with a diagnostic
14947 on Net@acronym{BSD} 2.0.2.
14949 A file might not be removed even if its parent directory is writable
14950 and searchable.  Many Posix hosts cannot remove a mount point, a named
14951 stream, a working directory, or a last link to a file that is being
14952 executed.
14954 @acronym{DOS} variants cannot rename or remove open files, and do not
14955 support commands like @samp{rm foo >foo}, even though this is
14956 perfectly portable among Posix hosts.
14959 @item @command{sed}
14960 @c ----------------
14961 @prindex @command{sed}
14962 Patterns should not include the separator (unless escaped), even as part
14963 of a character class.  In conformance with Posix, the Cray
14964 @command{sed} rejects @samp{s/[^/]*$//}: use @samp{s,[^/]*$,,}.
14966 Avoid empty patterns within parentheses (i.e., @samp{\(\)}).  Posix does
14967 not require support for empty patterns, and Unicos 9 @command{sed} rejects
14968 them.
14970 Unicos 9 @command{sed} loops endlessly on patterns like @samp{.*\n.*}.
14972 Sed scripts should not use branch labels longer than 7 characters and
14973 should not contain comments.  @acronym{HP-UX} sed has a limit of 99 commands
14974 (not counting @samp{:} commands) and
14975 48 labels, which can not be circumvented by using more than one script
14976 file.  It can execute up to 19 reads with the @samp{r} command per cycle.
14977 Solaris @command{/usr/ucb/sed} rejects usages that exceed an limit of
14978 about 6000 bytes for the internal representation of commands.
14980 Avoid redundant @samp{;}, as some @command{sed} implementations, such as
14981 Net@acronym{BSD} 1.4.2's, incorrectly try to interpret the second
14982 @samp{;} as a command:
14984 @example
14985 $ @kbd{echo a | sed 's/x/x/;;s/x/x/'}
14986 sed: 1: "s/x/x/;;s/x/x/": invalid command code ;
14987 @end example
14989 Input should not have unreasonably long lines, since some @command{sed}
14990 implementations have an input buffer limited to 4000 bytes.
14992 Portable @command{sed} regular expressions should use @samp{\} only to escape
14993 characters in the string @samp{$()*.0123456789[\^n@{@}}.  For example,
14994 alternation, @samp{\|}, is common but Posix does not require its
14995 support, so it should be avoided in portable scripts.  Solaris
14996 @command{sed} does not support alternation; e.g., @samp{sed '/a\|b/d'}
14997 deletes only lines that contain the literal string @samp{a|b}.
14998 Similarly, @samp{\+} and @samp{\?} should be avoided.
15000 Anchors (@samp{^} and @samp{$}) inside groups are not portable.
15002 Nested parentheses in patterns (e.g., @samp{\(\(a*\)b*)\)}) are
15003 quite portable to current hosts, but was not supported by some ancient
15004 @command{sed} implementations like SVR3.
15006 Some @command{sed} implementations, e.g., Solaris,
15007 restrict the special role of the asterisk to one-character regular expressions.
15008 This may lead to unexpected behavior:
15010 @example
15011 $ @kbd{echo '1*23*4' | /usr/bin/sed 's/\(.\)*/x/g'}
15012 x2x4
15013 $ @kbd{echo '1*23*4' | /usr/xpg4/bin/sed 's/\(.\)*/x/g'}
15015 @end example
15017 The @option{-e} option is mostly portable.
15018 However, its argument
15019 cannot start with @samp{a}, @samp{c}, or @samp{i},
15020 as this runs afoul of a Tru64 5.1 bug.
15021 Also, its argument cannot be empty, as this fails on @acronym{AIX} 5.3.
15022 Some people prefer to use @samp{-e}:
15024 @example
15025 sed -e '@var{command-1}' \
15026     -e '@var{command-2}'
15027 @end example
15029 @noindent
15030 as opposed to the equivalent:
15032 @example
15033 sed '
15034   @var{command-1}
15035   @var{command-2}
15037 @end example
15039 @noindent
15040 The following usage is sometimes equivalent:
15042 @example
15043 sed '@var{command-1};@var{command-2}'
15044 @end example
15046 but Posix says that this use of a semicolon has undefined effect if
15047 @var{command-1}'s verb is @samp{@{}, @samp{a}, @samp{b}, @samp{c},
15048 @samp{i}, @samp{r}, @samp{t}, @samp{w}, @samp{:}, or @samp{#}, so you
15049 should use semicolon only with simple scripts that do not use these
15050 verbs.
15052 Commands inside @{ @} brackets are further restricted.  Posix says that
15053 they cannot be preceded by addresses, @samp{!}, or @samp{;}, and that
15054 each command must be followed immediately by a newline, without any
15055 intervening blanks or semicolons.  The closing bracket must be alone on
15056 a line, other than white space preceding or following it.
15058 Contrary to yet another urban legend, you may portably use @samp{&} in
15059 the replacement part of the @code{s} command to mean ``what was
15060 matched''.  All descendants of Unix version 7 @command{sed}
15061 (at least; we
15062 don't have first hand experience with older @command{sed} implementations) have
15063 supported it.
15065 Posix requires that you must not have any white space between
15066 @samp{!} and the following command.  It is OK to have blanks between
15067 the address and the @samp{!}.  For instance, on Solaris:
15069 @example
15070 $ @kbd{echo "foo" | sed -n '/bar/ ! p'}
15071 @error{}Unrecognized command: /bar/ ! p
15072 $ @kbd{echo "foo" | sed -n '/bar/! p'}
15073 @error{}Unrecognized command: /bar/! p
15074 $ @kbd{echo "foo" | sed -n '/bar/ !p'}
15076 @end example
15078 Posix also says that you should not combine @samp{!} and @samp{;}.  If
15079 you use @samp{!}, it is best to put it on a command that is delimited by
15080 newlines rather than @samp{;}.
15082 Also note that Posix requires that the @samp{b}, @samp{t}, @samp{r}, and
15083 @samp{w} commands be followed by exactly one space before their argument.
15084 On the other hand, no white space is allowed between @samp{:} and the
15085 subsequent label name.
15087 If a sed script is specified on the command line and ends in an
15088 @samp{a}, @samp{c}, or @samp{i} command, the last line of inserted text
15089 should be followed by a newline.  Otherwise some @command{sed}
15090 implementations (e.g., Open@acronym{BSD} 3.9) do not append a newline to the
15091 inserted text.
15093 Many @command{sed} implementations (e.g., MacOS X 10.4,
15094 Open@acronym{BSD} 3.9, Solaris 10
15095 @command{/usr/ucb/sed}) strip leading white space from the text of
15096 @samp{a}, @samp{c}, and @samp{i} commands.  Prepend a backslash to
15097 work around this incompatibility with Posix:
15099 @example
15100 $ @kbd{echo flushleft | sed 'a\}
15101 > @kbd{   indented}
15102 > @kbd{'}
15103 flushleft
15104 indented
15105 $ @kbd{echo foo | sed 'a\}
15106 > @kbd{\   indented}
15107 > @kbd{'}
15108 flushleft
15109    indented
15110 @end example
15113 @item @command{sed} (@samp{t})
15114 @c ---------------------------
15115 @prindex @command{sed} (@samp{t})
15116 Some old systems have @command{sed} that ``forget'' to reset their
15117 @samp{t} flag when starting a new cycle.  For instance on @acronym{MIPS
15118 RISC/OS}, and on @sc{irix} 5.3, if you run the following @command{sed}
15119 script (the line numbers are not actual part of the texts):
15121 @example
15122 s/keep me/kept/g  # a
15123 t end             # b
15124 s/.*/deleted/g    # c
15125 :end              # d
15126 @end example
15128 @noindent
15131 @example
15132 delete me         # 1
15133 delete me         # 2
15134 keep me           # 3
15135 delete me         # 4
15136 @end example
15138 @noindent
15139 you get
15141 @example
15142 deleted
15143 delete me
15144 kept
15145 deleted
15146 @end example
15148 @noindent
15149 instead of
15151 @example
15152 deleted
15153 deleted
15154 kept
15155 deleted
15156 @end example
15158 Why?  When processing line 1, (c) matches, therefore sets the @samp{t}
15159 flag, and the output is produced.  When processing
15160 line 2, the @samp{t} flag is still set (this is the bug).  Command (a)
15161 fails to match, but @command{sed} is not supposed to clear the @samp{t}
15162 flag when a substitution fails.  Command (b) sees that the flag is set,
15163 therefore it clears it, and jumps to (d), hence you get @samp{delete me}
15164 instead of @samp{deleted}.  When processing line (3), @samp{t} is clear,
15165 (a) matches, so the flag is set, hence (b) clears the flags and jumps.
15166 Finally, since the flag is clear, line 4 is processed properly.
15168 There are two things one should remember about @samp{t} in @command{sed}.
15169 Firstly, always remember that @samp{t} jumps if @emph{some} substitution
15170 succeeded, not only the immediately preceding substitution.  Therefore,
15171 always use a fake @samp{t clear} followed by a @samp{:clear} on the next
15172 line, to reset the @samp{t} flag where needed.
15174 Secondly, you cannot rely on @command{sed} to clear the flag at each new
15175 cycle.
15177 One portable implementation of the script above is:
15179 @example
15180 t clear
15181 :clear
15182 s/keep me/kept/g
15183 t end
15184 s/.*/deleted/g
15185 :end
15186 @end example
15188 @item @command{touch}
15189 @c ------------------
15190 @prindex @command{touch}
15191 @cindex timestamp resolution
15192 If you specify the desired timestamp (e.g., with the @option{-r}
15193 option), @command{touch} typically uses the @code{utime} or
15194 @code{utimes} system call, which can result in the same kind of
15195 timestamp truncation problems that @samp{cp -p} has.
15197 On ancient @acronym{BSD} systems, @command{touch} or any command that
15198 results in an empty file does not update the timestamps, so use a
15199 command like @command{echo} as a workaround.
15200 Also,
15201 @acronym{GNU} @command{touch} 3.16r (and presumably all before that)
15202 fails to work on SunOS 4.1.3 when the empty file is on an
15203 @acronym{NFS}-mounted 4.2 volume.
15204 However, these problems are no longer of practical concern.
15206 @end table
15209 @node Portable Make
15210 @chapter Portable Make Programming
15211 @prindex @command{make}
15212 @cindex Limitations of @command{make}
15214 Writing portable makefiles is an art.  Since a makefile's commands are
15215 executed by the shell, you must consider the shell portability issues
15216 already mentioned.  However, other issues are specific to @command{make}
15217 itself.
15219 @menu
15220 * $< in Ordinary Make Rules::   $< in ordinary rules
15221 * Failure in Make Rules::       Failing portably in rules
15222 * Special Chars in Names::      Special Characters in Macro Names
15223 * Backslash-Newline-Newline::   Empty last lines in macro definitions
15224 * Backslash-Newline Comments::  Spanning comments across line boundaries
15225 * Long Lines in Makefiles::     Line length limitations
15226 * Macros and Submakes::         @code{make macro=value} and submakes
15227 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
15228 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
15229 * Comments in Make Rules::      Other problems with Make comments
15230 * obj/ and Make::               Don't name a subdirectory @file{obj}
15231 * make -k Status::              Exit status of @samp{make -k}
15232 * VPATH and Make::              @code{VPATH} woes
15233 * Single Suffix Rules::         Single suffix rules and separated dependencies
15234 * Timestamps and Make::         Subsecond timestamp resolution
15235 @end menu
15237 @node $< in Ordinary Make Rules
15238 @section @code{$<} in Ordinary Make Rules
15240 Posix says that the @samp{$<} construct in makefiles can be
15241 used only in inference rules and in the @samp{.DEFAULT} rule; its
15242 meaning in ordinary rules is unspecified.  Solaris @command{make}
15243 for instance replaces it with the empty string.  Open@acronym{BSD} (3.0 and
15244 later) @command{make} diagnoses these uses and errors out.
15246 @node Failure in Make Rules
15247 @section Failure in Make Rules
15249 Since 1992 Posix has required that @command{make} must invoke
15250 each command with the equivalent of a @samp{sh -c} subshell.  However,
15251 many @command{make} implementations, including @acronym{BSD} make through 2004,
15252 use @samp{sh -e -c} instead, and the @option{-e} option causes the
15253 subshell to exit immediately if a subsidiary simple-command fails.  For
15254 example, the command @samp{touch T; rm -f U} always attempts to
15255 remove @file{U} with Posix make, but incompatible
15256 @command{make} implementations skip the @command{rm} if the
15257 @command{touch} fails.  One way to work around this is to reword the
15258 affected simple-commands so that they always succeed, e.g., @samp{touch
15259 T || :; rm -f U}.
15260 However, even this approach can run into common bugs in @acronym{BSD}
15261 implementations of the @option{-e} option of @command{sh} and
15262 @command{set} (@pxref{Limitations of Builtins}), so if you are worried
15263 about porting to buggy @acronym{BSD} shells it may be simpler to migrate
15264 complicated @command{make} actions into separate scripts.
15266 @node Special Chars in Names
15267 @section Special Characters in Make Macro Names
15269 Posix limits macro names to nonempty strings containing only
15270 @acronym{ASCII} letters and digits, @samp{.}, and @samp{_}.  Many
15271 @command{make} implementations allow a wider variety of characters, but
15272 portable makefiles should avoid them.  It is portable to start a name
15273 with a special character, e.g., @samp{$(.FOO)}.
15275 Some ancient @command{make} implementations don't support leading
15276 underscores in macro names.  An example is @acronym{NEWS-OS} 4.2R.
15278 @example
15279 $ @kbd{cat Makefile}
15280 _am_include = #
15281 _am_quote =
15282 all:; @@echo this is test
15283 $ @kbd{make}
15284 Make: Must be a separator on rules line 2.  Stop.
15285 $ @kbd{cat Makefile2}
15286 am_include = #
15287 am_quote =
15288 all:; @@echo this is test
15289 $ @kbd{make -f Makefile2}
15290 this is test
15291 @end example
15293 @noindent
15294 However, this problem is no longer of practical concern.
15296 @node Backslash-Newline-Newline
15297 @section Backslash-Newline-Newline in Make Macro Values
15299 @c  This has been seen on ia64 hpux 11.20, and on one hppa hpux 10.20,
15300 @c  but another hppa hpux 10.20 didn't have it.  Bob Proulx
15301 @c  <bob@proulx.com> thinks it was in hpux 8.0 too.
15302 On some versions of @acronym{HP-UX}, @command{make} reads multiple newlines
15303 following a backslash, continuing to the next non-empty line.  For
15304 example,
15306 @example
15307 FOO = one \
15309 BAR = two
15311 test:
15312         : FOO is "$(FOO)"
15313         : BAR is "$(BAR)"
15314 @end example
15316 @noindent
15317 shows @code{FOO} equal to @code{one BAR = two}.  Other implementations
15318 sensibly let a backslash continue only to the immediately following
15319 line.
15321 @node Backslash-Newline Comments
15322 @section Backslash-Newline in Make Comments
15324 According to Posix, Make comments start with @code{#}
15325 and continue until an unescaped newline is reached.
15327 @example
15328 $ @kbd{cat Makefile}
15329 # A = foo \
15330       bar \
15331       baz
15333 all:
15334         @@echo ok
15335 $ @kbd{make}   # GNU make
15337 @end example
15339 @noindent
15340 However this is not always the case.  Some implementations
15341 discard everything from @code{#} through the end of the line, ignoring any
15342 trailing backslash.
15344 @example
15345 $ @kbd{pmake}  # BSD make
15346 "Makefile", line 3: Need an operator
15347 Fatal errors encountered -- cannot continue
15348 @end example
15350 @noindent
15351 Therefore, if you want to comment out a multi-line definition, prefix each
15352 line with @code{#}, not only the first.
15354 @example
15355 # A = foo \
15356 #     bar \
15357 #     baz
15358 @end example
15360 @node Long Lines in Makefiles
15361 @section Long Lines in Makefiles
15363 Tru64 5.1's @command{make} has been reported to crash when given a
15364 makefile with lines longer than around 20 kB.  Earlier versions are
15365 reported to exit with @code{Line too long} diagnostics.
15367 @node Macros and Submakes
15368 @section @code{make macro=value} and Submakes
15370 A command-line variable definition such as @code{foo=bar} overrides any
15371 definition of @code{foo} in a makefile.  Some @command{make}
15372 implementations (such as @acronym{GNU} @command{make}) propagate this
15373 override to subsidiary invocations of @command{make}.  Some other
15374 implementations do not pass the substitution along to submakes.
15376 @example
15377 $ @kbd{cat Makefile}
15378 foo = foo
15379 one:
15380         @@echo $(foo)
15381         $(MAKE) two
15382 two:
15383         @@echo $(foo)
15384 $ @kbd{make foo=bar}            # GNU make 3.79.1
15386 make two
15387 make[1]: Entering directory `/home/adl'
15389 make[1]: Leaving directory `/home/adl'
15390 $ @kbd{pmake foo=bar}           # BSD make
15392 pmake two
15394 @end example
15396 You have a few possibilities if you do want the @code{foo=bar} override
15397 to propagate to submakes.  One is to use the @option{-e}
15398 option, which causes all environment variables to have precedence over
15399 the makefile macro definitions, and declare foo as an environment
15400 variable:
15402 @example
15403 $ @kbd{env foo=bar make -e}
15404 @end example
15406 The @option{-e} option is propagated to submakes automatically,
15407 and since the environment is inherited between @command{make}
15408 invocations, the @code{foo} macro is overridden in
15409 submakes as expected.
15411 This syntax (@code{foo=bar make -e}) is portable only when used
15412 outside of a makefile, for instance from a script or from the
15413 command line.  When run inside a @command{make} rule, @acronym{GNU}
15414 @command{make} 3.80 and prior versions forget to propagate the
15415 @option{-e} option to submakes.
15417 Moreover, using @option{-e} could have unexpected side effects if your
15418 environment contains some other macros usually defined by the
15419 makefile.  (See also the note about @code{make -e} and @code{SHELL}
15420 below.)
15422 Another way to propagate overrides to submakes is to do it
15423 manually, from your makefile:
15425 @example
15426 foo = foo
15427 one:
15428         @@echo $(foo)
15429         $(MAKE) foo=$(foo) two
15430 two:
15431         @@echo $(foo)
15432 @end example
15434 You need to foresee all macros that a user might want to override if
15435 you do that.
15437 @node The Make Macro MAKEFLAGS
15438 @section The Make Macro MAKEFLAGS
15439 @cindex @code{MAKEFLAGS} and @command{make}
15440 @cindex @command{make} and @code{MAKEFLAGS}
15442 Posix requires @command{make} to use @code{MAKEFLAGS} to affect the
15443 current and recursive invocations of make, but allows implementations
15444 several formats for the variable.  It is tricky to parse
15445 @code{$MAKEFLAGS} to determine whether @option{-s} for silent execution
15446 or @option{-k} for continued execution are in effect.  For example, you
15447 cannot assume that the first space-separated word in @code{$MAKEFLAGS}
15448 contains single-letter options, since in the Cygwin version of
15449 @acronym{GNU} @command{make} it is either @option{--unix} or
15450 @option{--win32} with the second word containing single-letter options.
15452 @example
15453 $ @kbd{cat Makefile}
15454 all:
15455         @@echo MAKEFLAGS = $(MAKEFLAGS)
15456 $ @kbd{make}
15457 MAKEFLAGS = --unix
15458 $ @kbd{make -k}
15459 MAKEFLAGS = --unix -k
15460 @end example
15462 @node The Make Macro SHELL
15463 @section The Make Macro @code{SHELL}
15464 @cindex @code{SHELL} and @command{make}
15465 @cindex @command{make} and @code{SHELL}
15467 Posix-compliant @command{make} internally uses the @code{$(SHELL)}
15468 macro to spawn shell processes and execute Make rules.  This
15469 is a builtin macro supplied by @command{make}, but it can be modified
15470 by a makefile or by a command-line argument.
15472 Not all @command{make} implementations define this @code{SHELL} macro.
15473 Tru64
15474 @command{make} is an example; this implementation always uses
15475 @code{/bin/sh}.  So it's a good idea to always define @code{SHELL} in
15476 your makefiles.  If you use Autoconf, do
15478 @example
15479 SHELL = @@SHELL@@
15480 @end example
15482 Do not force @code{SHELL = /bin/sh} because that is not correct
15483 everywhere.  For instance @acronym{DJGPP} lacks @code{/bin/sh}, and when
15484 its @acronym{GNU} @code{make} port sees such a setting it enters a special
15485 emulation mode where features like pipes and redirections are emulated
15486 on top of DOS's @command{command.com}.  Unfortunately this emulation is
15487 incomplete; for instance it does not handle command substitutions.
15488 On @acronym{DJGPP} @code{SHELL} should point to Bash.
15490 Posix-compliant @command{make} should never acquire the value of
15491 $(SHELL) from the environment, even when @code{make -e} is used
15492 (otherwise, think about what would happen to your rules if
15493 @code{SHELL=/bin/tcsh}).
15495 However not all @command{make} implementations have this exception.
15496 For instance it's not surprising that Tru64 @command{make} doesn't
15497 protect @code{SHELL}, since it doesn't use it.
15499 @example
15500 $ @kbd{cat Makefile}
15501 SHELL = /bin/sh
15502 FOO = foo
15503 all:
15504         @@echo $(SHELL)
15505         @@echo $(FOO)
15506 $ @kbd{env SHELL=/bin/tcsh FOO=bar make -e}   # Tru64 Make
15507 /bin/tcsh
15509 $ @kbd{env SHELL=/bin/tcsh FOO=bar gmake -e}  # GNU make
15510 /bin/sh
15512 @end example
15514 @node Comments in Make Rules
15515 @section Comments in Make Rules
15516 @cindex Comments in @file{Makefile} rules
15517 @cindex @file{Makefile} rules and comments
15519 Never put comments in a rule.
15521 Some @command{make} treat anything starting with a tab as a command for
15522 the current rule, even if the tab is immediately followed by a @code{#}.
15523 The @command{make} from Tru64 Unix V5.1 is one of them.  The following
15524 makefile runs @code{# foo} through the shell.
15526 @example
15527 all:
15528         # foo
15529 @end example
15531 @node obj/ and Make
15532 @section The @file{obj/} Subdirectory and Make
15533 @cindex @file{obj/}, subdirectory
15534 @cindex @acronym{BSD} @command{make} and @file{obj/}
15536 Never name one of your subdirectories @file{obj/} if you don't like
15537 surprises.
15539 If an @file{obj/} directory exists, @acronym{BSD} @command{make} enters it
15540 before reading the makefile.  Hence the makefile in the
15541 current directory is not read.
15543 @example
15544 $ @kbd{cat Makefile}
15545 all:
15546         echo Hello
15547 $ @kbd{cat obj/Makefile}
15548 all:
15549         echo World
15550 $ @kbd{make}      # GNU make
15551 echo Hello
15552 Hello
15553 $ @kbd{pmake}     # BSD make
15554 echo World
15555 World
15556 @end example
15558 @node make -k Status
15559 @section Exit Status of @code{make -k}
15560 @cindex @code{make -k}
15562 Do not rely on the exit status of @code{make -k}.  Some implementations
15563 reflect whether they encountered an error in their exit status; other
15564 implementations always succeed.
15566 @example
15567 $ @kbd{cat Makefile}
15568 all:
15569         false
15570 $ @kbd{make -k; echo exit status: $?}    # GNU make
15571 false
15572 make: *** [all] Error 1
15573 exit status: 2
15574 $ @kbd{pmake -k; echo exit status: $?}   # BSD make
15575 false
15576 *** Error code 1 (continuing)
15577 exit status: 0
15578 @end example
15580 @node VPATH and Make
15581 @section @code{VPATH} and Make
15582 @cindex @code{VPATH}
15584 Posix does not specify the semantics of @code{VPATH}.  Typically,
15585 @command{make} supports @code{VPATH}, but its implementation is not
15586 consistent.
15588 Autoconf and Automake support makefiles whose usages of @code{VPATH} are
15589 portable to recent-enough popular implementations of @command{make}, but
15590 to keep the resulting makefiles portable, a package's makefile
15591 prototypes must take the following issues into account.  These issues
15592 are complicated and are often poorly understood, and installers who use
15593 @code{VPATH} should expect to find many bugs in this area.  If you use
15594 @code{VPATH}, the simplest way to avoid these portability bugs is to
15595 stick with @acronym{GNU} @command{make}, since it is the most
15596 commonly-used @command{make} among Autoconf users.
15598 Here are some known issues with some @code{VPATH}
15599 implementations.
15601 @menu
15602 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
15603 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
15604 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
15605 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
15606 * Make Target Lookup::          More details about @code{VPATH} lookup
15607 @end menu
15609 @node VPATH and Double-colon
15610 @subsection @code{VPATH} and Double-colon Rules
15611 @cindex @code{VPATH} and double-colon rules
15612 @cindex double-colon rules and @code{VPATH}
15614 With ancient versions of Sun @command{make},
15615 any assignment to @code{VPATH} causes @command{make} to execute only
15616 the first set of double-colon rules.
15617 However, this problem is no longer of practical concern.
15619 @node $< in Explicit Rules
15620 @subsection @code{$<} Not Supported in Explicit Rules
15621 @cindex explicit rules, @code{$<}, and @code{VPATH}
15622 @cindex @code{$<}, explicit rules, and @code{VPATH}
15623 @cindex @code{VPATH}, explicit rules, and @code{$<}
15625 Using @code{$<} in explicit rules is not portable.
15626 The prerequisite file must be named explicitly in the rule.  If you want
15627 to find the prerequisite via a @code{VPATH} search, you have to code the
15628 whole thing manually.  @xref{Build Directories}.
15630 @node Automatic Rule Rewriting
15631 @subsection Automatic Rule Rewriting
15632 @cindex @code{VPATH} and automatic rule rewriting
15633 @cindex automatic rule rewriting and @code{VPATH}
15635 Some @command{make} implementations, such as Solaris and Tru64,
15636 search for prerequisites in @code{VPATH} and
15637 then rewrite each occurrence as a plain word in the rule.
15638 For instance:
15640 @example
15641 # This isn't portable to GNU make.
15642 VPATH = ../pkg/src
15643 f.c: if.c
15644         cp if.c f.c
15645 @end example
15647 @noindent
15648 executes @code{cp ../pkg/src/if.c f.c} if @file{if.c} is
15649 found in @file{../pkg/src}.
15651 However, this rule leads to real problems in practice.  For example, if
15652 the source directory contains an ordinary file named @file{test} that is
15653 used in a dependency, Solaris @command{make} rewrites commands like
15654 @samp{if test -r foo; @dots{}} to @samp{if ../pkg/src/test -r foo;
15655 @dots{}}, which is typically undesirable.  To avoid this problem,
15656 portable makefiles should never mention a source file whose name is that
15657 of a shell keyword like @file{until} or a shell command like
15658 @command{cat} or @command{gcc} or @command{test}.
15660 Because of these problems @acronym{GNU} @command{make} and many other
15661 @command{make} implementations do not rewrite commands, so portable
15662 makefiles should
15663 search @code{VPATH} manually.  It is tempting to write this:
15665 @smallexample
15666 # This isn't portable to Solaris make.
15667 VPATH = ../pkg/src
15668 f.c: if.c
15669         cp `test -f if.c || echo $(VPATH)/`if.c f.c
15670 @end smallexample
15672 @noindent
15673 However, the ``prerequisite rewriting'' still applies here.  So if
15674 @file{if.c} is in @file{../pkg/src}, Solaris and Tru64 @command{make}
15675 execute
15677 @smallexample
15678 cp `test -f ../pkg/src/if.c || echo ../pkg/src/`if.c f.c
15679 @end smallexample
15681 @noindent
15682 which reduces to
15684 @example
15685 cp if.c f.c
15686 @end example
15688 @noindent
15689 and thus fails.  Oops.
15691 A simple workaround, and good practice anyway, is to use @samp{$?} and
15692 @samp{$@@} when possible:
15694 @smallexample
15695 VPATH = ../pkg/src
15696 f.c: if.c
15697         cp $? $@@
15698 @end smallexample
15700 @noindent
15701 but this does not generalize well to commands with multiple
15702 prerequisites.  A more general workaround is to rewrite the rule so that
15703 the prerequisite @file{if.c} never appears as a plain word.  For
15704 example, these three rules would be safe, assuming @file{if.c} is in
15705 @file{../pkg/src} and the other files are in the working directory:
15707 @smallexample
15708 VPATH = ../pkg/src
15709 f.c: if.c f1.c
15710         cat `test -f ./if.c || echo $(VPATH)/`if.c f1.c >$@@
15711 g.c: if.c g1.c
15712         cat `test -f 'if.c' || echo $(VPATH)/`if.c g1.c >$@@
15713 h.c: if.c h1.c
15714         cat `test -f "if.c" || echo $(VPATH)/`if.c h1.c >$@@
15715 @end smallexample
15717 Things get worse when your prerequisites are in a macro.
15719 @example
15720 VPATH = ../pkg/src
15721 HEADERS = f.h g.h h.h
15722 install-HEADERS: $(HEADERS)
15723         for i in $(HEADERS); do \
15724           $(INSTALL) -m 644 \
15725             `test -f $$i || echo $(VPATH)/`$$i \
15726             $(DESTDIR)$(includedir)/$$i; \
15727         done
15728 @end example
15730 The above @code{install-HEADERS} rule is not Solaris-proof because @code{for
15731 i in $(HEADERS);} is expanded to @code{for i in f.h g.h h.h;}
15732 where @code{f.h} and @code{g.h} are plain words and are hence
15733 subject to @code{VPATH} adjustments.
15735 If the three files are in @file{../pkg/src}, the rule is run as:
15737 @example
15738 for i in ../pkg/src/f.h ../pkg/src/g.h h.h; do \
15739   install -m 644 \
15740      `test -f $i || echo ../pkg/src/`$i \
15741      /usr/local/include/$i; \
15742 done
15743 @end example
15745 where the two first @command{install} calls fail.  For instance,
15746 consider the @code{f.h} installation:
15748 @example
15749 install -m 644 \
15750   `test -f ../pkg/src/f.h || \
15751     echo ../pkg/src/ \
15752   `../pkg/src/f.h \
15753   /usr/local/include/../pkg/src/f.h;
15754 @end example
15756 @noindent
15757 It reduces to:
15759 @example
15760 install -m 644 \
15761   ../pkg/src/f.h \
15762   /usr/local/include/../pkg/src/f.h;
15763 @end example
15765 Note that the manual @code{VPATH} search did not cause any problems here;
15766 however this command installs @file{f.h} in an incorrect directory.
15768 Trying to quote @code{$(HEADERS)} in some way, as we did for
15769 @code{foo.c} a few makefiles ago, does not help:
15771 @example
15772 install-HEADERS: $(HEADERS)
15773         headers='$(HEADERS)'; \
15774         for i in $$headers; do \
15775           $(INSTALL) -m 644 \
15776             `test -f $$i || echo $(VPATH)/`$$i \
15777             $(DESTDIR)$(includedir)/$$i; \
15778         done
15779 @end example
15781 Now, @code{headers='$(HEADERS)'} macro-expands to:
15783 @example
15784 headers='f.h g.h h.h'
15785 @end example
15787 @noindent
15788 but @code{g.h} is still a plain word.  (As an aside, the idiom
15789 @code{headers='$(HEADERS)'; for i in $$headers;} is a good
15790 idea if @code{$(HEADERS)} can be empty, because some shells diagnose a
15791 syntax error on @code{for i in;}.)
15793 One workaround is to strip this unwanted @file{../pkg/src/} prefix manually:
15795 @example
15796 VPATH = ../pkg/src
15797 HEADERS = f.h g.h h.h
15798 install-HEADERS: $(HEADERS)
15799         headers='$(HEADERS)'; \
15800         for i in $$headers; do \
15801           i=`expr "$$i" : '$(VPATH)/\(.*\)'`;
15802           $(INSTALL) -m 644 \
15803             `test -f $$i || echo $(VPATH)/`$$i \
15804             $(DESTDIR)$(includedir)/$$i; \
15805         done
15806 @end example
15808 Automake does something similar.  However the above hack works only if
15809 the files listed in @code{HEADERS} are in the current directory or a
15810 subdirectory; they should not be in an enclosing directory.  If we had
15811 @code{HEADERS = ../f.h}, the above fragment would fail in a VPATH
15812 build with Tru64 @command{make}.  The reason is that not only does
15813 Tru64 @command{make} rewrite dependencies, but it also simplifies
15814 them.  Hence @code{../f.h} becomes @code{../pkg/f.h} instead of
15815 @code{../pkg/src/../f.h}.  This obviously defeats any attempt to strip
15816 a leading @file{../pkg/src/} component.
15818 The following example makes the behavior of Tru64 @command{make}
15819 more apparent.
15821 @example
15822 $ @kbd{cat Makefile}
15823 VPATH = sub
15824 all: ../foo
15825         echo ../foo
15826 $ @kbd{ls}
15827 Makefile foo
15828 $ @kbd{make}
15829 echo foo
15831 @end example
15833 @noindent
15834 Dependency @file{../foo} was found in @file{sub/../foo}, but Tru64
15835 @command{make} simplified it as @file{foo}.  (Note that the @file{sub/}
15836 directory does not even exist, this just means that the simplification
15837 occurred before the file was checked for.)
15839 For the record here is how SunOS 4 @command{make} behaves on this
15840 example.
15842 @smallexample
15843 $ @kbd{make}
15844 make: Fatal error: Don't know how to make target `../foo'
15845 $ @kbd{mkdir sub}
15846 $ @kbd{make}
15847 echo sub/../foo
15848 sub/../foo
15849 @end smallexample
15852 @node Tru64 Directory Magic
15853 @subsection Tru64 @command{make} Creates Prerequisite Directories Magically
15854 @cindex @code{VPATH} and prerequisite directories
15855 @cindex prerequisite directories and @code{VPATH}
15857 When a prerequisite is a subdirectory of @code{VPATH}, Tru64
15858 @command{make} creates it in the current directory.
15860 @example
15861 $ @kbd{mkdir -p foo/bar build}
15862 $ @kbd{cd build}
15863 $ @kbd{cat >Makefile <<END
15864 VPATH = ..
15865 all: foo/bar
15866 END}
15867 $ @kbd{make}
15868 mkdir foo
15869 mkdir foo/bar
15870 @end example
15872 This can yield unexpected results if a rule uses a manual @code{VPATH}
15873 search as presented before.
15875 @example
15876 VPATH = ..
15877 all : foo/bar
15878         command `test -d foo/bar || echo ../`foo/bar
15879 @end example
15881 The above @command{command} is run on the empty @file{foo/bar}
15882 directory that was created in the current directory.
15884 @node Make Target Lookup
15885 @subsection Make Target Lookup
15886 @cindex @code{VPATH}, resolving target pathnames
15888 @acronym{GNU} @command{make} uses a complex algorithm to decide when it
15889 should use files found via a @code{VPATH} search.  @xref{Search
15890 Algorithm, , How Directory Searches are Performed, make, The @acronym{GNU} Make
15891 Manual}.
15893 If a target needs to be rebuilt, @acronym{GNU} @command{make} discards the
15894 file name found during the @code{VPATH} search for this target, and
15895 builds the file locally using the file name given in the makefile.
15896 If a target does not need to be rebuilt, @acronym{GNU} @command{make} uses the
15897 file name found during the @code{VPATH} search.
15899 Other @command{make} implementations, like Net@acronym{BSD} @command{make}, are
15900 easier to describe: the file name found during the @code{VPATH} search
15901 is used whether the target needs to be rebuilt or not.  Therefore
15902 new files are created locally, but existing files are updated at their
15903 @code{VPATH} location.
15905 Open@acronym{BSD} and Free@acronym{BSD} @command{make}, however,
15906 never perform a
15907 @code{VPATH} search for a dependency that has an explicit rule.
15908 This is extremely annoying.
15910 When attempting a @code{VPATH} build for an autoconfiscated package
15911 (e.g., @code{mkdir build && cd build && ../configure}), this means
15912 @acronym{GNU}
15913 @command{make} builds everything locally in the @file{build}
15914 directory, while @acronym{BSD} @command{make} builds new files locally and
15915 updates existing files in the source directory.
15917 @example
15918 $ @kbd{cat Makefile}
15919 VPATH = ..
15920 all: foo.x bar.x
15921 foo.x bar.x: newer.x
15922         @@echo Building $@@
15923 $ @kbd{touch ../bar.x}
15924 $ @kbd{touch ../newer.x}
15925 $ @kbd{make}        # GNU make
15926 Building foo.x
15927 Building bar.x
15928 $ @kbd{pmake}       # NetBSD make
15929 Building foo.x
15930 Building ../bar.x
15931 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
15932 Building foo.x
15933 Building bar.x
15934 $ @kbd{tmake}       # Tru64 make
15935 Building foo.x
15936 Building bar.x
15937 $ @kbd{touch ../bar.x}
15938 $ @kbd{make}        # GNU make
15939 Building foo.x
15940 $ @kbd{pmake}       # NetBSD make
15941 Building foo.x
15942 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
15943 Building foo.x
15944 Building bar.x
15945 $ @kbd{tmake}       # Tru64 make
15946 Building foo.x
15947 Building bar.x
15948 @end example
15950 Note how Net@acronym{BSD} @command{make} updates @file{../bar.x} in its
15951 VPATH location, and how Free@acronym{BSD}, Open@acronym{BSD}, and Tru64
15952 @command{make} always
15953 update @file{bar.x}, even when @file{../bar.x} is up to date.
15955 Another point worth mentioning is that once @acronym{GNU} @command{make} has
15956 decided to ignore a @code{VPATH} file name (e.g., it ignored
15957 @file{../bar.x} in the above example) it continues to ignore it when
15958 the target occurs as a prerequisite of another rule.
15960 The following example shows that @acronym{GNU} @command{make} does not look up
15961 @file{bar.x} in @code{VPATH} before performing the @code{.x.y} rule,
15962 because it ignored the @code{VPATH} result of @file{bar.x} while running
15963 the @code{bar.x: newer.x} rule.
15965 @example
15966 $ @kbd{cat Makefile}
15967 VPATH = ..
15968 all: bar.y
15969 bar.x: newer.x
15970         @@echo Building $@@
15971 .SUFFIXES: .x .y
15972 .x.y:
15973         cp $< $@@
15974 $ @kbd{touch ../bar.x}
15975 $ @kbd{touch ../newer.x}
15976 $ @kbd{make}        # GNU make
15977 Building bar.x
15978 cp bar.x bar.y
15979 cp: cannot stat `bar.x': No such file or directory
15980 make: *** [bar.y] Error 1
15981 $ @kbd{pmake}       # NetBSD make
15982 Building ../bar.x
15983 cp ../bar.x bar.y
15984 $ @kbd{rm bar.y}
15985 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
15986 echo Building bar.x
15987 cp bar.x bar.y
15988 cp: cannot stat `bar.x': No such file or directory
15989 *** Error code 1
15990 $ @kbd{tmake}       # Tru64 make
15991 Building bar.x
15992 cp: bar.x: No such file or directory
15993 *** Exit 1
15994 @end example
15996 Note that if you drop away the command from the @code{bar.x: newer.x}
15997 rule, @acronym{GNU} @command{make} magically starts to work: it
15998 knows that @code{bar.x} hasn't been updated, therefore it doesn't
15999 discard the result from @code{VPATH} (@file{../bar.x}) in succeeding
16000 uses.  Tru64 also works, but Free@acronym{BSD} and Open@acronym{BSD}
16001 still don't.
16003 @example
16004 $ @kbd{cat Makefile}
16005 VPATH = ..
16006 all: bar.y
16007 bar.x: newer.x
16008 .SUFFIXES: .x .y
16009 .x.y:
16010         cp $< $@@
16011 $ @kbd{touch ../bar.x}
16012 $ @kbd{touch ../newer.x}
16013 $ @kbd{make}        # GNU make
16014 cp ../bar.x bar.y
16015 $ @kbd{rm bar.y}
16016 $ @kbd{pmake}       # NetBSD make
16017 cp ../bar.x bar.y
16018 $ @kbd{rm bar.y}
16019 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
16020 cp bar.x bar.y
16021 cp: cannot stat `bar.x': No such file or directory
16022 *** Error code 1
16023 $ @kbd{tmake}       # Tru64 make
16024 cp ../bar.x bar.y
16025 @end example
16027 It seems the sole solution that would please every @command{make}
16028 implementation is to never rely on @code{VPATH} searches for targets.
16029 In other words, @code{VPATH} should be reserved to unbuilt sources.
16032 @node Single Suffix Rules
16033 @section Single Suffix Rules and Separated Dependencies
16034 @cindex Single Suffix Inference Rule
16035 @cindex Rule, Single Suffix Inference
16036 A @dfn{Single Suffix Rule} is basically a usual suffix (inference) rule
16037 (@samp{.from.to:}), but which @emph{destination} suffix is empty
16038 (@samp{.from:}).
16040 @cindex Separated Dependencies
16041 @dfn{Separated dependencies} simply refers to listing the prerequisite
16042 of a target, without defining a rule.  Usually one can list on the one
16043 hand side, the rules, and on the other hand side, the dependencies.
16045 Solaris @command{make} does not support separated dependencies for
16046 targets defined by single suffix rules:
16048 @example
16049 $ @kbd{cat Makefile}
16050 .SUFFIXES: .in
16051 foo: foo.in
16052 .in:
16053         cp $< $@@
16054 $ @kbd{touch foo.in}
16055 $ @kbd{make}
16056 $ @kbd{ls}
16057 Makefile  foo.in
16058 @end example
16060 @noindent
16061 while @acronym{GNU} Make does:
16063 @example
16064 $ @kbd{gmake}
16065 cp foo.in foo
16066 $ @kbd{ls}
16067 Makefile  foo       foo.in
16068 @end example
16070 Note it works without the @samp{foo: foo.in} dependency.
16072 @example
16073 $ @kbd{cat Makefile}
16074 .SUFFIXES: .in
16075 .in:
16076         cp $< $@@
16077 $ @kbd{make foo}
16078 cp foo.in foo
16079 @end example
16081 @noindent
16082 and it works with double suffix inference rules:
16084 @example
16085 $ @kbd{cat Makefile}
16086 foo.out: foo.in
16087 .SUFFIXES: .in .out
16088 .in.out:
16089         cp $< $@@
16090 $ @kbd{make}
16091 cp foo.in foo.out
16092 @end example
16094 As a result, in such a case, you have to write target rules.
16096 @node Timestamps and Make
16097 @section Timestamp Resolution and Make
16098 @cindex timestamp resolution
16099 Traditionally, file timestamps had 1-second resolution, and
16100 @command{make} used those timestamps to determine whether one file was
16101 newer than the other.  However, many modern file systems have
16102 timestamps with 1-nanosecond resolution.  Some @command{make}
16103 implementations look at the entire timestamp; others ignore the
16104 fractional part, which can lead to incorrect results.  Normally this
16105 is not a problem, but in some extreme cases you may need to use tricks
16106 like @samp{sleep 1} to work around timestamp truncation bugs.
16108 Commands like @samp{cp -p} and @samp{touch -r} typically do not copy
16109 file timestamps to their full resolutions (@pxref{Limitations of Usual
16110 Tools}).  Hence you should be wary of rules like this:
16112 @example
16113 dest: src
16114         cp -p src dest
16115 @end example
16117 as @file{dest} often appears to be older than @file{src} after the
16118 timestamp is truncated, and this can cause @command{make} to do
16119 needless rework the next time it is invoked.  To work around this
16120 problem, you can use a timestamp file, e.g.:
16122 @example
16123 dest-stamp: src
16124         cp -p src dest
16125         date >dest-stamp
16126 @end example
16131 @c ======================================== Portable C and C++ Programming
16133 @node Portable C and C++
16134 @chapter Portable C and C++ Programming
16135 @cindex Portable C and C++ programming
16137 C and C++ programs often use low-level features of the underlying
16138 system, and therefore are often more difficult to make portable to other
16139 platforms.
16141 Several standards have been developed to help make your programs more
16142 portable.  If you write programs with these standards in mind, you can
16143 have greater confidence that your programs work on a wide variety
16144 of systems.  @xref{Standards, , Language Standards Supported by
16145 @acronym{GCC}, gcc, Using the @acronym{GNU} Compiler Collection
16146 (@acronym{GCC})}, for a list of C-related
16147 standards.  Many programs also assume the
16148 @uref{http://www.opengroup.org/susv3, Posix standard}.
16150 Some old code is written to be portable to K&R C, which predates any C
16151 standard.  K&R C compilers are no longer of practical interest, though,
16152 and the rest of section assumes at least C89, the first C standard.
16154 Program portability is a huge topic, and this section can only briefly
16155 introduce common pitfalls.  @xref{System Portability, , Portability
16156 between System Types, standards, @acronym{GNU} Coding Standards}, for
16157 more information.
16159 @menu
16160 * Varieties of Unportability::  How to make your programs unportable
16161 * Integer Overflow::            When integers get too large
16162 * Null Pointers::               Properties of null pointers
16163 * Buffer Overruns::             Subscript errors and the like
16164 * Volatile Objects::            @code{volatile} and signals
16165 * Floating Point Portability::  Portable floating-point arithmetic
16166 * Exiting Portably::            Exiting and the exit status
16167 @end menu
16169 @node Varieties of Unportability
16170 @section Varieties of Unportability
16171 @cindex portability
16173 Autoconf tests and ordinary programs often need to test what is allowed
16174 on a system, and therefore they may need to deliberately exceed the
16175 boundaries of what the standards allow, if only to see whether an
16176 optional feature is present.  When you write such a program, you should
16177 keep in mind the difference between constraints, unspecified behavior,
16178 and undefined behavior.
16180 In C, a @dfn{constraint} is a rule that the compiler must enforce.  An
16181 example constraint is that C programs must not declare a bit-field with
16182 negative width.  Tests can therefore reliably assume that programs with
16183 negative-width bit-fields are rejected by a compiler that conforms
16184 to the standard.
16186 @dfn{Unspecified behavior} is valid behavior, where the standard allows
16187 multiple possibilities.  For example, the order of evaluation of
16188 function arguments is unspecified.  Some unspecified behavior is
16189 @dfn{implementation-defined}, i.e., documented by the implementation,
16190 but since Autoconf tests cannot read the documentation they cannot
16191 distinguish between implementation-defined and other unspecified
16192 behavior.  It is common for Autoconf tests to probe implementations to
16193 determine otherwise-unspecified behavior.
16195 @dfn{Undefined behavior} is invalid behavior, where the standard allows
16196 the implementation to do anything it pleases.  For example,
16197 dereferencing a null pointer leads to undefined behavior.  If possible,
16198 test programs should avoid undefined behavior, since a program with
16199 undefined behavior might succeed on a test that should fail.
16201 The above rules apply to programs that are intended to conform to the
16202 standard.  However, strictly-conforming programs are quite rare, since
16203 the standards are so limiting.  A major goal of Autoconf is to support
16204 programs that use implementation features not described by the standard,
16205 and it is fairly common for test programs to violate the above rules, if
16206 the programs work well enough in practice.
16208 @node Integer Overflow
16209 @section Integer Overflow
16210 @cindex integer overflow
16211 @cindex overflow, signed integer
16212 @cindex signed integer overflow
16213 @cindex wraparound arithmetic
16215 In practice many portable C programs assume that signed integer overflow wraps
16216 around reliably using two's complement arithmetic.  Yet the C standard
16217 says that program behavior is undefined on overflow, and in a few cases
16218 C programs do not work on some modern implementations because their
16219 overflows do not wrap around as their authors expected.  Conversely, in
16220 signed integer remainder, the C standard requires overflow
16221 behavior that is commonly not implemented.
16223 @menu
16224 * Integer Overflow Basics::      Why integer overflow is a problem
16225 * Signed Overflow Examples::     Examples of code assuming wraparound
16226 * Optimization and Wraparound::  Optimizations that break uses of wraparound
16227 * Signed Overflow Advice::       Practical advice for signed overflow issues
16228 * Signed Integer Division::      @code{INT_MIN / -1} and @code{INT_MIN % -1}
16229 @end menu
16231 @node Integer Overflow Basics
16232 @subsection Basics of Integer Overflow
16233 @cindex integer overflow
16234 @cindex overflow, signed integer
16235 @cindex signed integer overflow
16236 @cindex wraparound arithmetic
16238 In languages like C, unsigned integer overflow reliably wraps around;
16239 e.g., @code{UINT_MAX + 1} yields zero.
16240 This is guaranteed by the C standard and is
16241 portable in practice, unless you specify aggressive,
16242 nonstandard optimization options
16243 suitable only for special applications.
16245 In contrast, the C standard says that signed integer overflow leads to
16246 undefined behavior where a program can do anything, including dumping
16247 core or overrunning a buffer.  The misbehavior can even precede the
16248 overflow.  Such an overflow can occur during addition, subtraction,
16249 multiplication, division, and left shift.
16251 Despite this requirement of the standard, many C programs and Autoconf
16252 tests assume that signed integer overflow silently wraps around modulo a
16253 power of two, using two's complement arithmetic, so long as you cast the
16254 resulting value to a signed integer type or store it into a signed
16255 integer variable.  If you use conservative optimization flags, such
16256 programs are generally portable to the vast majority of modern
16257 platforms, with a few exceptions discussed later.
16259 For historical reasons the C standard also allows implementations with
16260 ones' complement or signed magnitude arithmetic, but it is safe to
16261 assume two's complement nowadays.
16263 Also, overflow can occur when converting an out-of-range value to a
16264 signed integer type.  Here a standard implementation must define what
16265 happens, but this might include raising an exception.  In practice all
16266 known implementations support silent wraparound in this case, so you need
16267 not worry about other possibilities.
16269 @node Signed Overflow Examples
16270 @subsection Examples of Code Assuming Wraparound Overflow
16271 @cindex integer overflow
16272 @cindex overflow, signed integer
16273 @cindex signed integer overflow
16274 @cindex wraparound arithmetic
16276 There has long been a tension between what the C standard requires for
16277 signed integer overflow, and what C programs commonly assume.  The
16278 standard allows aggressive optimizations based on assumptions that
16279 overflow never occurs, but many practical C programs rely on overflow
16280 wrapping around.  These programs do not conform to the standard, but
16281 they commonly work in practice because compiler writers are
16282 understandably reluctant to implement optimizations that would break
16283 many programs, unless perhaps a user specifies aggressive optimization.
16285 The C Standard says that if a program has signed integer overflow its
16286 behavior is undefined, and the undefined behavior can even precede the
16287 overflow.  To take an extreme example:
16289 @c Inspired by Robert Dewar's example in
16290 @c <http://gcc.gnu.org/ml/gcc/2007-01/msg00038.html> (2007-01-01).
16291 @example
16292 if (password == expected_password)
16293   allow_superuser_privileges ();
16294 else if (counter++ == INT_MAX)
16295   abort ();
16296 else
16297   printf ("%d password mismatches\n", counter);
16298 @end example
16300 @noindent
16301 If the @code{int} variable @code{counter} equals @code{INT_MAX},
16302 @code{counter++} must overflow and the behavior is undefined, so the C
16303 standard allows the compiler to optimize away the test against
16304 @code{INT_MAX} and the @code{abort} call.
16305 Worse, if an earlier bug in the program lets the compiler deduce that
16306 @code{counter == INT_MAX} or that @code{counter} previously overflowed,
16307 the C standard allows the compiler to optimize away the password test
16308 and generate code that allows superuser privileges unconditionally.
16310 Despite this requirement by the standard, it has long been common for C
16311 code to assume wraparound arithmetic after signed overflow, and all
16312 known practical C implementations support some C idioms that assume
16313 wraparound signed arithmetic, even if the idioms do not conform
16314 strictly to the standard.  If your code looks like the following
16315 examples it will almost surely work with real-world compilers.
16317 Here is an example derived from the 7th Edition Unix implementation of
16318 @code{atoi} (1979-01-10):
16320 @example
16321 char *p;
16322 int f, n;
16323 @dots{}
16324 while (*p >= '0' && *p <= '9')
16325   n = n * 10 + *p++ - '0';
16326 return (f ? -n : n);
16327 @end example
16329 @noindent
16330 Even if the input string is in range, on most modern machines this has
16331 signed overflow when computing the most negative integer (the @code{-n}
16332 overflows) or a value near an extreme integer (the first @code{+}
16333 overflows).
16335 Here is another example, derived from the 7th Edition implementation of
16336 @code{rand} (1979-01-10).  Here the programmer expects both
16337 multiplication and addition to wrap on overflow:
16339 @example
16340 static long int randx = 1;
16341 @dots{}
16342 randx = randx * 1103515245 + 12345;
16343 return (randx >> 16) & 077777;
16344 @end example
16346 In the following example, derived from the @acronym{GNU} C Library 2.5
16347 implementation of @code{mktime} (2006-09-09), the code assumes
16348 wraparound arithmetic in @code{+} to detect signed overflow:
16350 @example
16351 time_t t, t1, t2;
16352 int sec_requested, sec_adjustment;
16353 @dots{}
16354 t1 = t + sec_requested;
16355 t2 = t1 + sec_adjustment;
16356 if (((t1 < t) != (sec_requested < 0))
16357     | ((t2 < t1) != (sec_adjustment < 0)))
16358   return -1;
16359 @end example
16361 If your code looks like these examples, it is probably safe even though
16362 it does not strictly conform to the C standard.  This might lead one to
16363 believe that one can generally assume wraparound on overflow, but that
16364 is not always true, as can be seen in the next section.
16366 @node Optimization and Wraparound
16367 @subsection Optimizations That Break Wraparound Arithmetic
16368 @cindex loop induction
16370 Compilers sometimes generate code that is incompatible with wraparound
16371 integer arithmetic.  A simple example is an algebraic simplification: a
16372 compiler might translate @code{(i * 2000) / 1000} to @code{i * 2}
16373 because it assumes that @code{i * 2000} does not overflow.  The
16374 translation is not equivalent to the original when overflow occurs:
16375 e.g., in the typical case of 32-bit signed two's complement wraparound
16376 @code{int}, if @code{i} has type @code{int} and value @code{1073742},
16377 the original expression returns @minus{}2147483 but the optimized
16378 version returns the mathematically correct value 2147484.
16380 More subtly, loop induction optimizations often exploit the undefined
16381 behavior of signed overflow.  Consider the following contrived function
16382 @code{sumc}:
16384 @example
16386 sumc (int lo, int hi)
16388   int sum = 0;
16389   int i;
16390   for (i = lo; i <= hi; i++)
16391     sum ^= i * 53;
16392   return sum;
16394 @end example
16396 @noindent
16397 To avoid multiplying by 53 each time through the loop, an optimizing
16398 compiler might internally transform @code{sumc} to the equivalent of the
16399 following:
16401 @example
16403 transformed_sumc (int lo, int hi)
16405   int sum = 0;
16406   int hic = hi * 53;
16407   int ic;
16408   for (ic = lo * 53; ic <= hic; ic += 53)
16409     sum ^= ic;
16410   return sum;
16412 @end example
16414 @noindent
16415 This transformation is allowed by the C standard, but it is invalid for
16416 wraparound arithmetic when @code{INT_MAX / 53 < hi}, because then the
16417 overflow in computing expressions like @code{hi * 53} can cause the
16418 expression @code{i <= hi} to yield a different value from the
16419 transformed expression @code{ic <= hic}.
16421 For this reason, compilers that use loop induction and similar
16422 techniques often do not support reliable wraparound arithmetic when a
16423 loop induction variable like @code{ic} is involved.  Since loop
16424 induction variables are generated by the compiler, and are not visible
16425 in the source code, it is not always trivial to say whether the problem
16426 affects your code.
16428 Hardly any code actually depends on wraparound arithmetic in cases like
16429 these, so in practice these loop induction optimizations are almost
16430 always useful.  However, edge cases in this area can cause problems.
16431 For example:
16433 @example
16434 int j;
16435 for (j = 1; 0 < j; j *= 2)
16436   test (j);
16437 @end example
16439 @noindent
16440 Here, the loop attempts to iterate through all powers of 2 that
16441 @code{int} can represent, but the C standard allows a compiler to
16442 optimize away the comparison and generate an infinite loop,
16443 under the argument that behavior is undefined on overflow.  As of this
16444 writing this optimization is not done by any production version of
16445 @acronym{GCC} with @option{-O2}, but it might be performed by other
16446 compilers, or by more aggressive @acronym{GCC} optimization options,
16447 and the @acronym{GCC} developers have not decided whether it will
16448 continue to work with @acronym{GCC} and @option{-O2}.
16450 @node Signed Overflow Advice
16451 @subsection Practical Advice for Signed Overflow Issues
16452 @cindex integer overflow
16453 @cindex overflow, signed integer
16454 @cindex signed integer overflow
16455 @cindex wraparound arithmetic
16457 Ideally the safest approach is to avoid signed integer overflow
16458 entirely.  For example, instead of multiplying two signed integers, you
16459 can convert them to unsigned integers, multiply the unsigned values,
16460 then test whether the result is in signed range.
16462 Rewriting code in this way will be inconvenient, though, particularly if
16463 the signed values might be negative.  Also, it may hurt
16464 performance.  Using unsigned arithmetic to check for overflow is
16465 particularly painful to do portably and efficiently when dealing with an
16466 integer type like @code{uid_t} whose width and signedness vary from
16467 platform to platform.
16469 Furthermore, many C applications pervasively assume wraparound behavior
16470 and typically it is not easy to find and remove all these assumptions.
16471 Hence it is often useful to maintain nonstandard code that assumes
16472 wraparound on overflow, instead of rewriting the code.  The rest of this
16473 section attempts to give practical advice for this situation.
16475 If your code wants to detect signed integer overflow in @code{sum = a +
16476 b}, it is generally safe to use an expression like @code{(sum < a) != (b
16477 < 0)}.
16479 If your code uses a signed loop index, make sure that the index cannot
16480 overflow, along with all signed expressions derived from the index.
16481 Here is a contrived example of problematic code with two instances of
16482 overflow.
16484 @example
16485 for (i = INT_MAX - 10; i <= INT_MAX; i++)
16486   if (i + 1 < 0)
16487     @{
16488       report_overflow ();
16489       break;
16490     @}
16491 @end example
16493 @noindent
16494 Because of the two overflows, a compiler might optimize away or
16495 transform the two comparisons in a way that is incompatible with the
16496 wraparound assumption.
16498 If your code uses an expression like @code{(i * 2000) / 1000} and you
16499 actually want the multiplication to wrap around on overflow, use
16500 unsigned arithmetic
16501 to do it, e.g., @code{((int) (i * 2000u)) / 1000}.
16503 If your code assumes wraparound behavior and you want to insulate it
16504 against any @acronym{GCC} optimizations that would fail to support that
16505 behavior, you should use @acronym{GCC}'s @option{-fwrapv} option, which
16506 causes signed overflow to wrap around reliably (except for division and
16507 remainder, as discussed in the next section).
16509 If you need to port to platforms where signed integer overflow does not
16510 reliably wrap around (e.g., due to hardware overflow checking, or to
16511 highly aggressive optimizations), you should consider debugging with
16512 @acronym{GCC}'s @option{-ftrapv} option, which causes signed overflow to
16513 raise an exception.
16515 @node Signed Integer Division
16516 @subsection Signed Integer Division and Integer Overflow
16517 @cindex division, integer
16519 Overflow in signed
16520 integer division is not always harmless: for example, on CPUs of the
16521 i386 family, dividing @code{INT_MIN} by @code{-1} yields a SIGFPE signal
16522 which by default terminates the program.  Worse, taking the remainder
16523 of these two values typically yields the same signal on these CPUs,
16524 even though the C standard requires @code{INT_MIN % -1} to yield zero
16525 because the expression does not overflow.
16527 @node Null Pointers
16528 @section Properties of Null Pointers
16529 @cindex null pointers
16531 Most modern hosts reliably fail when you attempt to dereference a null
16532 pointer.
16534 On almost all modern hosts, null pointers use an all-bits-zero internal
16535 representation, so you can reliably use @code{memset} with 0 to set all
16536 the pointers in an array to null values.
16538 If @code{p} is a null pointer to an object type, the C expression
16539 @code{p + 0} always evaluates to @code{p} on modern hosts, even though
16540 the standard says that it has undefined behavior.
16542 @node Buffer Overruns
16543 @section Buffer Overruns and Subscript Errors
16544 @cindex buffer overruns
16546 Buffer overruns and subscript errors are the most common dangerous
16547 errors in C programs.  They result in undefined behavior because storing
16548 outside an array typically modifies storage that is used by some other
16549 object, and most modern systems lack runtime checks to catch these
16550 errors.  Programs should not rely on buffer overruns being caught.
16552 There is one exception to the usual rule that a portable program cannot
16553 address outside an array.  In C, it is valid to compute the address just
16554 past an object, e.g., @code{&a[N]} where @code{a} has @code{N} elements,
16555 so long as you do not dereference the resulting pointer.  But it is not
16556 valid to compute the address just before an object, e.g., @code{&a[-1]};
16557 nor is it valid to compute two past the end, e.g., @code{&a[N+1]}.  On
16558 most platforms @code{&a[-1] < &a[0] && &a[N] < &a[N+1]}, but this is not
16559 reliable in general, and it is usually easy enough to avoid the
16560 potential portability problem, e.g., by allocating an extra unused array
16561 element at the start or end.
16563 @uref{http://valgrind.org/, Valgrind} can catch many overruns.
16564 @acronym{GCC}
16565 users might also consider using the @option{-fmudflap} option to catch
16566 overruns.
16568 Buffer overruns are usually caused by off-by-one errors, but there are
16569 more subtle ways to get them.
16571 Using @code{int} values to index into an array or compute array sizes
16572 causes problems on typical 64-bit hosts where an array index might
16573 be @math{2^31} or larger.  Index values of type @code{size_t} avoid this
16574 problem, but cannot be negative.  Index values of type @code{ptrdiff_t}
16575 are signed, and are wide enough in practice.
16577 If you add or multiply two numbers to calculate an array size, e.g.,
16578 @code{malloc (x * sizeof y + z)}, havoc ensues if the addition or
16579 multiplication overflows.
16581 Many implementations of the @code{alloca} function silently misbehave
16582 and can generate buffer overflows if given sizes that are too large.
16583 The size limits are implementation dependent, but are at least 4000
16584 bytes on all platforms that we know about.
16586 The standard functions @code{asctime}, @code{asctime_r}, @code{ctime},
16587 @code{ctime_r}, and @code{gets} are prone to buffer overflows, and
16588 portable code should not use them unless the inputs are known to be
16589 within certain limits.  The time-related functions can overflow their
16590 buffers if given timestamps out of range (e.g., a year less than -999
16591 or greater than 9999).  Time-related buffer overflows cannot happen with
16592 recent-enough versions of the @acronym{GNU} C library, but are possible
16593 with other
16594 implementations.  The @code{gets} function is the worst, since it almost
16595 invariably overflows its buffer when presented with an input line larger
16596 than the buffer.
16598 @node Volatile Objects
16599 @section Volatile Objects
16600 @cindex volatile objects
16602 The keyword @code{volatile} is often misunderstood in portable code.
16603 Its use inhibits some memory-access optimizations, but programmers often
16604 wish that it had a different meaning than it actually does.
16606 @code{volatile} was designed for code that accesses special objects like
16607 memory-mapped device registers whose contents spontaneously change.
16608 Such code is inherently low-level, and it is difficult to specify
16609 portably what @code{volatile} means in these cases.  The C standard
16610 says, ``What constitutes an access to an object that has
16611 volatile-qualified type is implementation-defined,'' so in theory each
16612 implementation is supposed to fill in the gap by documenting what
16613 @code{volatile} means for that implementation.  In practice, though,
16614 this documentation is usually absent or incomplete.
16616 One area of confusion is the distinction between objects defined with
16617 volatile types, and volatile lvalues.  From the C standard's point of
16618 view, an object defined with a volatile type has externally visible
16619 behavior.  You can think of such objects as having little oscilloscope
16620 probes attached to them, so that the user can observe some properties of
16621 accesses to them, just as the user can observe data written to output
16622 files.  However, the standard does not make it clear whether users can
16623 observe accesses by volatile lvalues to ordinary objects.  For example:
16625 @example
16626 /* Declare and access a volatile object.
16627    Accesses to X are "visible" to users.  */
16628 static int volatile x;
16629 x = 1;
16631 /* Access two ordinary objects via a volatile lvalue.
16632    It's not clear whether accesses to *P are "visible".  */
16633 int y;
16634 int *z = malloc (sizeof (int));
16635 int volatile *p;
16636 p = &y;
16637 *p = 1;
16638 p = z;
16639 *p = 1;
16640 @end example
16642 Programmers often wish that @code{volatile} meant ``Perform the memory
16643 access here and now, without merging several memory accesses, without
16644 changing the memory word size, and without reordering.''  But the C
16645 standard does not require this.  For objects defined with a volatile
16646 type, accesses must be done before the next sequence point; but
16647 otherwise merging, reordering, and word-size change is allowed.  Worse,
16648 it is not clear from the standard whether volatile lvalues provide more
16649 guarantees in general than nonvolatile lvalues, if the underlying
16650 objects are ordinary.
16652 Even when accessing objects defined with a volatile type,
16653 the C standard allows only
16654 extremely limited signal handlers: the behavior is undefined if a signal
16655 handler reads any nonlocal object, or writes to any nonlocal object
16656 whose type is not @code{sig_atomic_t volatile}, or calls any standard
16657 library function other than @code{abort}, @code{signal}, and (if C99)
16658 @code{_Exit}.  Hence C compilers need not worry about a signal handler
16659 disturbing ordinary computation, unless the computation accesses a
16660 @code{sig_atomic_t volatile} lvalue that is not a local variable.
16661 (There is an obscure exception for accesses via a pointer to a volatile
16662 character, since it may point into part of a @code{sig_atomic_t
16663 volatile} object.)  Posix
16664 adds to the list of library functions callable from a portable signal
16665 handler, but otherwise is like the C standard in this area.
16667 Some C implementations allow memory-access optimizations within each
16668 translation unit, such that actual behavior agrees with the behavior
16669 required by the standard only when calling a function in some other
16670 translation unit, and a signal handler acts like it was called from a
16671 different translation unit.  The C standard hints that in these
16672 implementations, objects referred to by signal handlers ``would require
16673 explicit specification of @code{volatile} storage, as well as other
16674 implementation-defined restrictions.''  But unfortunately even for this
16675 special case these other restrictions are often not documented well.
16676 @xref{Volatiles, , When is a Volatile Object Accessed?, gcc, Using the
16677 @acronym{GNU} Compiler Collection (@acronym{GCC})}, for some
16678 restrictions imposed by @acronym{GCC}.  @xref{Defining Handlers, ,
16679 Defining Signal Handlers, libc, The @acronym{GNU} C Library}, for some
16680 restrictions imposed by the @acronym{GNU} C library.  Restrictions
16681 differ on other platforms.
16683 If possible, it is best to use a signal handler that fits within the
16684 limits imposed by the C and Posix standards.
16686 If this is not practical, you can try the following rules of thumb.  A
16687 signal handler should access only volatile lvalues, preferably lvalues
16688 that refer to objects defined with a volatile type, and should not
16689 assume that the accessed objects have an internally consistent state
16690 if they are larger than a machine word.  Furthermore, installers
16691 should employ compilers and compiler options that are commonly used
16692 for building operating system kernels, because kernels often need more
16693 from @code{volatile} than the C Standard requires, and installers who
16694 compile an application in a similar environment can sometimes benefit
16695 from the extra constraints imposed by kernels on compilers.
16696 Admittedly we are handwaving somewhat here, as there are few
16697 guarantees in this area; the rules of thumb may help to fix some bugs
16698 but there is a good chance that they will not fix them all.
16700 For @code{volatile}, C++ has the same problems that C does.
16701 Multithreaded applications have even more problems with @code{volatile},
16702 but they are beyond the scope of this section.
16704 The bottom line is that using @code{volatile} typically hurts
16705 performance but should not hurt correctness.  In some cases its use
16706 does help correctness, but these cases are often so poorly understood
16707 that all too often adding @code{volatile} to a data structure merely
16708 alleviates some symptoms of a bug while not fixing the bug in general.
16710 @node Floating Point Portability
16711 @section Floating Point Portability
16712 @cindex floating point
16714 Almost all modern systems use IEEE-754 floating point, and it is safe to
16715 assume IEEE-754 in most portable code these days.  For more information,
16716 please see David Goldberg's classic paper
16717 @uref{http://www.validlab.com/goldberg/paper.pdf, What Every Computer
16718 Scientist Should Know About Floating-Point Arithmetic}.
16720 @node Exiting Portably
16721 @section Exiting Portably
16722 @cindex exiting portably
16724 A C or C++ program can exit with status @var{N} by returning
16725 @var{N} from the @code{main} function.  Portable programs are supposed
16726 to exit either with status 0 or @code{EXIT_SUCCESS} to succeed, or with
16727 status @code{EXIT_FAILURE} to fail, but in practice it is portable to
16728 fail by exiting with status 1, and test programs that assume Posix can
16729 fail by exiting with status values from 1 through 255.  Programs on
16730 SunOS 2.0 (1985) through 3.5.2 (1988) incorrectly exited with zero
16731 status when @code{main} returned nonzero, but ancient systems like these
16732 are no longer of practical concern.
16734 A program can also exit with status @var{N} by passing @var{N} to the
16735 @code{exit} function, and a program can fail by calling the @code{abort}
16736 function.  If a program is specialized to just some platforms, it can fail
16737 by calling functions specific to those platforms, e.g., @code{_exit}
16738 (Posix) and @code{_Exit} (C99).  However, like other functions, an exit
16739 function should be declared, typically by including a header.  For
16740 example, if a C program calls @code{exit}, it should include @file{stdlib.h}
16741 either directly or via the default includes (@pxref{Default Includes}).
16743 A program can fail due to undefined behavior such as dereferencing a null
16744 pointer, but this is not recommended as undefined behavior allows an
16745 implementation to do whatever it pleases and this includes exiting
16746 successfully.
16749 @c ================================================== Manual Configuration
16751 @node Manual Configuration
16752 @chapter Manual Configuration
16754 A few kinds of features can't be guessed automatically by running test
16755 programs.  For example, the details of the object-file format, or
16756 special options that need to be passed to the compiler or linker.  You
16757 can check for such features using ad-hoc means, such as having
16758 @command{configure} check the output of the @code{uname} program, or
16759 looking for libraries that are unique to particular systems.  However,
16760 Autoconf provides a uniform method for handling unguessable features.
16762 @menu
16763 * Specifying Names::            Specifying the system type
16764 * Canonicalizing::              Getting the canonical system type
16765 * Using System Type::           What to do with the system type
16766 @end menu
16768 @node Specifying Names
16769 @section Specifying the System Type
16770 @cindex System type
16772 Autoconf-generated
16773 @command{configure} scripts can make decisions based on a canonical name
16774 for the system type, which has the form:
16775 @samp{@var{cpu}-@var{vendor}-@var{os}}, where @var{os} can be
16776 @samp{@var{system}} or @samp{@var{kernel}-@var{system}}
16778 @command{configure} can usually guess the canonical name for the type of
16779 system it's running on.  To do so it runs a script called
16780 @command{config.guess}, which infers the name using the @code{uname}
16781 command or symbols predefined by the C preprocessor.
16783 Alternately, the user can specify the system type with command line
16784 arguments to @command{configure}.  Doing so is necessary when
16785 cross-compiling.  In the most complex case of cross-compiling, three
16786 system types are involved.  The options to specify them are:
16788 @table @option
16789 @item --build=@var{build-type}
16790 the type of system on which the package is being configured and
16791 compiled.  It defaults to the result of running @command{config.guess}.
16793 @item --host=@var{host-type}
16794 the type of system on which the package runs.  By default it is the
16795 same as the build machine.  Specifying it enables the cross-compilation
16796 mode.
16798 @item --target=@var{target-type}
16799 the type of system for which any compiler tools in the package
16800 produce code (rarely needed).  By default, it is the same as host.
16801 @end table
16803 If you mean to override the result of @command{config.guess}, use
16804 @option{--build}, not @option{--host}, since the latter enables
16805 cross-compilation.  For historical reasons,
16806 whenever you specify @option{--host},
16807 be sure to specify @option{--build} too; this will be fixed in the
16808 future.  So, to enter cross-compilation mode, use a command like this
16810 @example
16811 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
16812 @end example
16814 @noindent
16815 Note that if you do not specify @option{--host}, @command{configure}
16816 fails if it can't run the code generated by the specified compiler.  For
16817 example, configuring as follows fails:
16819 @example
16820 ./configure CC=m68k-coff-gcc
16821 @end example
16823 In the future, when cross-compiling Autoconf will @emph{not}
16824 accept tools (compilers, linkers, assemblers) whose name is not
16825 prefixed with the host type.  The only case when this may be
16826 useful is when you really are not cross-compiling, but only
16827 building for a least-common-denominator architecture: an example
16828 is building for @code{i386-pc-linux-gnu} while running on an
16829 @code{i686-pc-linux-gnu} architecture.  In this case, some particular
16830 pairs might be similar enough to let you get away with the system
16831 compilers, but in general the compiler might make bogus assumptions
16832 on the host: if you know what you are doing, please create symbolic
16833 links from the host compiler to the build compiler.
16835 @cindex @command{config.sub}
16836 @command{configure} recognizes short aliases for many system types; for
16837 example, @samp{decstation} can be used instead of
16838 @samp{mips-dec-ultrix4.2}.  @command{configure} runs a script called
16839 @command{config.sub} to canonicalize system type aliases.
16841 This section deliberately omits the description of the obsolete
16842 interface; see @ref{Hosts and Cross-Compilation}.
16845 @node Canonicalizing
16846 @section Getting the Canonical System Type
16847 @cindex System type
16848 @cindex Canonical system type
16850 The following macros make the system type available to @command{configure}
16851 scripts.
16853 @ovindex build_alias
16854 @ovindex host_alias
16855 @ovindex target_alias
16857 The variables @samp{build_alias}, @samp{host_alias}, and
16858 @samp{target_alias} are always exactly the arguments of @option{--build},
16859 @option{--host}, and @option{--target}; in particular, they are left empty
16860 if the user did not use them, even if the corresponding
16861 @code{AC_CANONICAL} macro was run.  Any configure script may use these
16862 variables anywhere.  These are the variables that should be used when in
16863 interaction with the user.
16865 If you need to recognize some special environments based on their system
16866 type, run the following macros to get canonical system names.  These
16867 variables are not set before the macro call.
16869 If you use these macros, you must distribute @command{config.guess} and
16870 @command{config.sub} along with your source code.  @xref{Output}, for
16871 information about the @code{AC_CONFIG_AUX_DIR} macro which you can use
16872 to control in which directory @command{configure} looks for those scripts.
16875 @defmac AC_CANONICAL_BUILD
16876 @acindex{CANONICAL_BUILD}
16877 @ovindex build
16878 @ovindex build_cpu
16879 @ovindex build_vendor
16880 @ovindex build_os
16881 Compute the canonical build-system type variable, @code{build}, and its
16882 three individual parts @code{build_cpu}, @code{build_vendor}, and
16883 @code{build_os}.
16885 If @option{--build} was specified, then @code{build} is the
16886 canonicalization of @code{build_alias} by @command{config.sub},
16887 otherwise it is determined by the shell script @command{config.guess}.
16888 @end defmac
16890 @defmac AC_CANONICAL_HOST
16891 @acindex{CANONICAL_HOST}
16892 @ovindex host
16893 @ovindex host_cpu
16894 @ovindex host_vendor
16895 @ovindex host_os
16896 Compute the canonical host-system type variable, @code{host}, and its
16897 three individual parts @code{host_cpu}, @code{host_vendor}, and
16898 @code{host_os}.
16900 If @option{--host} was specified, then @code{host} is the
16901 canonicalization of @code{host_alias} by @command{config.sub},
16902 otherwise it defaults to @code{build}.
16903 @end defmac
16905 @defmac AC_CANONICAL_TARGET
16906 @acindex{CANONICAL_TARGET}
16907 @ovindex target
16908 @ovindex target_cpu
16909 @ovindex target_vendor
16910 @ovindex target_os
16911 Compute the canonical target-system type variable, @code{target}, and its
16912 three individual parts @code{target_cpu}, @code{target_vendor}, and
16913 @code{target_os}.
16915 If @option{--target} was specified, then @code{target} is the
16916 canonicalization of @code{target_alias} by @command{config.sub},
16917 otherwise it defaults to @code{host}.
16918 @end defmac
16920 Note that there can be artifacts due to the backward compatibility
16921 code.  See @xref{Hosts and Cross-Compilation}, for more.
16923 @node Using System Type
16924 @section Using the System Type
16926 In @file{configure.ac} the system type is generally used by one or more
16927 @code{case} statements to select system-specifics.  Shell wildcards can
16928 be used to match a group of system types.
16930 For example, an extra assembler code object file could be chosen, giving
16931 access to a CPU cycle counter register.  @code{$(CYCLE_OBJ)} in the
16932 following would be used in a makefile to add the object to a
16933 program or library.
16935 @example
16936 case $host in
16937   alpha*-*-*) CYCLE_OBJ=rpcc.o ;;
16938   i?86-*-*)   CYCLE_OBJ=rdtsc.o ;;
16939   *)          CYCLE_OBJ= ;;
16940 esac
16941 AC_SUBST([CYCLE_OBJ])
16942 @end example
16944 @code{AC_CONFIG_LINKS} (@pxref{Configuration Links}) is another good way
16945 to select variant source files, for example optimized code for some
16946 CPUs.  The configured CPU type doesn't always indicate exact CPU types,
16947 so some runtime capability checks may be necessary too.
16949 @example
16950 case $host in
16951   alpha*-*-*)   AC_CONFIG_LINKS([dither.c:alpha/dither.c]) ;;
16952   powerpc*-*-*) AC_CONFIG_LINKS([dither.c:powerpc/dither.c]) ;;
16953   *-*-*)        AC_CONFIG_LINKS([dither.c:generic/dither.c]) ;;
16954 esac
16955 @end example
16957 The host system type can also be used to find cross-compilation tools
16958 with @code{AC_CHECK_TOOL} (@pxref{Generic Programs}).
16960 The above examples all show @samp{$host}, since this is where the code
16961 is going to run.  Only rarely is it necessary to test @samp{$build}
16962 (which is where the build is being done).
16964 Whenever you're tempted to use @samp{$host} it's worth considering
16965 whether some sort of probe would be better.  New system types come along
16966 periodically or previously missing features are added.  Well-written
16967 probes can adapt themselves to such things, but hard-coded lists of
16968 names can't.  Here are some guidelines,
16970 @itemize @bullet
16971 @item
16972 Availability of libraries and library functions should always be checked
16973 by probing.
16974 @item
16975 Variant behavior of system calls is best identified with runtime tests
16976 if possible, but bug workarounds or obscure difficulties might have to
16977 be driven from @samp{$host}.
16978 @item
16979 Assembler code is inevitably highly CPU-specific and is best selected
16980 according to @samp{$host_cpu}.
16981 @item
16982 Assembler variations like underscore prefix on globals or ELF versus
16983 COFF type directives are however best determined by probing, perhaps
16984 even examining the compiler output.
16985 @end itemize
16987 @samp{$target} is for use by a package creating a compiler or similar.
16988 For ordinary packages it's meaningless and should not be used.  It
16989 indicates what the created compiler should generate code for, if it can
16990 cross-compile.  @samp{$target} generally selects various hard-coded CPU
16991 and system conventions, since usually the compiler or tools under
16992 construction themselves determine how the target works.
16995 @c ===================================================== Site Configuration.
16997 @node Site Configuration
16998 @chapter Site Configuration
17000 @command{configure} scripts support several kinds of local configuration
17001 decisions.  There are ways for users to specify where external software
17002 packages are, include or exclude optional features, install programs
17003 under modified names, and set default values for @command{configure}
17004 options.
17006 @menu
17007 * Help Formatting::             Customizing @samp{configure --help}
17008 * External Software::           Working with other optional software
17009 * Package Options::             Selecting optional features
17010 * Pretty Help Strings::         Formatting help string
17011 * Option Checking::             Controlling checking of @command{configure} options
17012 * Site Details::                Configuring site details
17013 * Transforming Names::          Changing program names when installing
17014 * Site Defaults::               Giving @command{configure} local defaults
17015 @end menu
17017 @node Help Formatting
17018 @section Controlling Help Output
17020 Users consult @samp{configure --help} to learn of configuration
17021 decisions specific to your package.  By default, @command{configure}
17022 breaks this output into sections for each type of option; within each
17023 section, help strings appear in the order @file{configure.ac} defines
17024 them:
17026 @example
17027 Optional Features:
17028   @dots{}
17029   --enable-bar            include bar
17031 Optional Packages:
17032   @dots{}
17033   --with-foo              use foo
17034 @end example
17036 @defmac AC_PRESERVE_HELP_ORDER
17037 @acindex{PRESERVE_HELP_ORDER}
17039 Request an alternate @option{--help} format, in which options of all
17040 types appear together, in the order defined.  Call this macro before any
17041 @code{AC_ARG_ENABLE} or @code{AC_ARG_WITH}.
17043 @example
17044 Optional Features and Packages:
17045   @dots{}
17046   --enable-bar            include bar
17047   --with-foo              use foo
17048 @end example
17050 @end defmac
17052 @node External Software
17053 @section Working With External Software
17054 @cindex External software
17056 Some packages require, or can optionally use, other software packages
17057 that are already installed.  The user can give @command{configure}
17058 command line options to specify which such external software to use.
17059 The options have one of these forms:
17061 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
17062 @c awful.
17063 @example
17064 --with-@var{package}[=@var{arg}]
17065 --without-@var{package}
17066 @end example
17068 For example, @option{--with-gnu-ld} means work with the @acronym{GNU} linker
17069 instead of some other linker.  @option{--with-x} means work with The X
17070 Window System.
17072 The user can give an argument by following the package name with
17073 @samp{=} and the argument.  Giving an argument of @samp{no} is for
17074 packages that are used by default; it says to @emph{not} use the
17075 package.  An argument that is neither @samp{yes} nor @samp{no} could
17076 include a name or number of a version of the other package, to specify
17077 more precisely which other package this program is supposed to work
17078 with.  If no argument is given, it defaults to @samp{yes}.
17079 @option{--without-@var{package}} is equivalent to
17080 @option{--with-@var{package}=no}.
17082 Normally @command{configure} scripts complain about
17083 @option{--with-@var{package}} options that they do not support.
17084 @xref{Option Checking}, for details, and for how to override the
17085 defaults.
17087 For each external software package that may be used, @file{configure.ac}
17088 should call @code{AC_ARG_WITH} to detect whether the @command{configure}
17089 user asked to use it.  Whether each package is used or not by default,
17090 and which arguments are valid, is up to you.
17092 @anchor{AC_ARG_WITH}
17093 @defmac AC_ARG_WITH (@var{package}, @var{help-string}, @
17094   @ovar{action-if-given}, @ovar{action-if-not-given})
17095 @acindex{ARG_WITH}
17096 If the user gave @command{configure} the option @option{--with-@var{package}}
17097 or @option{--without-@var{package}}, run shell commands
17098 @var{action-if-given}.  If neither option was given, run shell commands
17099 @var{action-if-not-given}.  The name @var{package} indicates another
17100 software package that this program should work with.  It should consist
17101 only of alphanumeric characters, dashes, and dots.
17103 The option's argument is available to the shell commands
17104 @var{action-if-given} in the shell variable @code{withval}, which is
17105 actually just the value of the shell variable named
17106 @code{with_@var{package}}, with any non-alphanumeric characters in
17107 @var{package} changed into @samp{_}.  You may use that variable instead,
17108 if you wish.
17110 The argument @var{help-string} is a description of the option that
17111 looks like this:
17112 @example
17113   --with-readline         support fancy command line editing
17114 @end example
17116 @noindent
17117 @var{help-string} may be more than one line long, if more detail is
17118 needed.  Just make sure the columns line up in @samp{configure
17119 --help}.  Avoid tabs in the help string.  You'll need to enclose the
17120 help string in @samp{[} and @samp{]} in order to produce the leading
17121 blanks.
17123 You should format your @var{help-string} with the macro
17124 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
17126 The following example shows how to use the @code{AC_ARG_WITH} macro in
17127 a common situation.  You want to let the user decide whether to enable
17128 support for an external library (e.g., the readline library); if the user
17129 specified neither @option{--with-readline} nor @option{--without-readline},
17130 you want to enable support for readline only if the library is available
17131 on the system.
17133 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
17134 @example
17135 AC_ARG_WITH([readline],
17136   [AS_HELP_STRING([--with-readline],
17137     [support fancy command line editing @@<:@@default=check@@:>@@])],
17138   [],
17139   [with_readline=check])
17141 LIBREADLINE=
17142 AS_IF([test "x$with_readline" != xno],
17143   [AC_CHECK_LIB([readline], [main],
17144     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
17145      AC_DEFINE([HAVE_LIBREADLINE], [1],
17146                [Define if you have libreadline])
17147     ],
17148     [if test "x$with_readline" != xcheck; then
17149        AC_MSG_FAILURE(
17150          [--with-readline was given, but test for readline failed])
17151      fi
17152     ], -lncurses)])
17153 @end example
17155 The next example shows how to use @code{AC_ARG_WITH} to give the user the
17156 possibility to enable support for the readline library, in case it is still
17157 experimental and not well tested, and is therefore disabled by default.
17159 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
17160 @example
17161 AC_ARG_WITH([readline],
17162   [AS_HELP_STRING([--with-readline],
17163     [enable experimental support for readline])],
17164   [],
17165   [with_readline=no])
17167 LIBREADLINE=
17168 AS_IF([test "x$with_readline" != xno],
17169   [AC_CHECK_LIB([readline], [main],
17170     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
17171      AC_DEFINE([HAVE_LIBREADLINE], [1],
17172                [Define if you have libreadline])
17173     ],
17174     [AC_MSG_FAILURE(
17175        [--with-readline was given, but test for readline failed])],
17176     [-lncurses])])
17177 @end example
17179 The last example shows how to use @code{AC_ARG_WITH} to give the user the
17180 possibility to disable support for the readline library, given that it is
17181 an important feature and that it should be enabled by default.
17183 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
17184 @example
17185 AC_ARG_WITH([readline],
17186   [AS_HELP_STRING([--without-readline],
17187     [disable support for readline])],
17188   [],
17189   [with_readline=yes])
17191 LIBREADLINE=
17192 AS_IF([test "x$with_readline" != xno],
17193   [AC_CHECK_LIB([readline], [main],
17194     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
17195      AC_DEFINE([HAVE_LIBREADLINE], [1],
17196                [Define if you have libreadline])
17197     ],
17198     [AC_MSG_FAILURE(
17199        [readline test failed (--without-readline to disable)])],
17200     [-lncurses])])
17201 @end example
17203 These three examples can be easily adapted to the case where
17204 @code{AC_ARG_ENABLE} should be preferred to @code{AC_ARG_WITH} (see
17205 @ref{Package Options}).
17206 @end defmac
17208 @node Package Options
17209 @section Choosing Package Options
17210 @cindex Package options
17211 @cindex Options, package
17213 If a software package has optional compile-time features, the user can
17214 give @command{configure} command line options to specify whether to
17215 compile them.  The options have one of these forms:
17217 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
17218 @c awful.
17219 @example
17220 --enable-@var{feature}[=@var{arg}]
17221 --disable-@var{feature}
17222 @end example
17224 These options allow users to choose which optional features to build and
17225 install.  @option{--enable-@var{feature}} options should never make a
17226 feature behave differently or cause one feature to replace another.
17227 They should only cause parts of the program to be built rather than left
17228 out.
17230 The user can give an argument by following the feature name with
17231 @samp{=} and the argument.  Giving an argument of @samp{no} requests
17232 that the feature @emph{not} be made available.  A feature with an
17233 argument looks like @option{--enable-debug=stabs}.  If no argument is
17234 given, it defaults to @samp{yes}.  @option{--disable-@var{feature}} is
17235 equivalent to @option{--enable-@var{feature}=no}.
17237 Normally @command{configure} scripts complain about
17238 @option{--enable-@var{package}} options that they do not support.
17239 @xref{Option Checking}, for details, and for how to override the
17240 defaults.
17242 For each optional feature, @file{configure.ac} should call
17243 @code{AC_ARG_ENABLE} to detect whether the @command{configure} user asked
17244 to include it.  Whether each feature is included or not by default, and
17245 which arguments are valid, is up to you.
17247 @anchor{AC_ARG_ENABLE}
17248 @defmac AC_ARG_ENABLE (@var{feature}, @var{help-string}, @
17249   @ovar{action-if-given}, @ovar{action-if-not-given})
17250 @acindex{ARG_ENABLE}
17251 If the user gave @command{configure} the option
17252 @option{--enable-@var{feature}} or @option{--disable-@var{feature}}, run
17253 shell commands @var{action-if-given}.  If neither option was given, run
17254 shell commands @var{action-if-not-given}.  The name @var{feature}
17255 indicates an optional user-level facility.  It should consist only of
17256 alphanumeric characters, dashes, and dots.
17258 The option's argument is available to the shell commands
17259 @var{action-if-given} in the shell variable @code{enableval}, which is
17260 actually just the value of the shell variable named
17261 @code{enable_@var{feature}}, with any non-alphanumeric characters in
17262 @var{feature} changed into @samp{_}.  You may use that variable instead,
17263 if you wish.  The @var{help-string} argument is like that of
17264 @code{AC_ARG_WITH} (@pxref{External Software}).
17266 You should format your @var{help-string} with the macro
17267 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
17269 See the examples suggested with the definition of @code{AC_ARG_WITH}
17270 (@pxref{External Software}) to get an idea of possible applications of
17271 @code{AC_ARG_ENABLE}.
17272 @end defmac
17274 @node Pretty Help Strings
17275 @section Making Your Help Strings Look Pretty
17276 @cindex Help strings
17278 Properly formatting the @samp{help strings} which are used in
17279 @code{AC_ARG_WITH} (@pxref{External Software}) and @code{AC_ARG_ENABLE}
17280 (@pxref{Package Options}) can be challenging.  Specifically, you want
17281 your own @samp{help strings} to line up in the appropriate columns of
17282 @samp{configure --help} just like the standard Autoconf @samp{help
17283 strings} do.  This is the purpose of the @code{AS_HELP_STRING} macro.
17285 @anchor{AS_HELP_STRING}
17286 @defmac AS_HELP_STRING (@var{left-hand-side}, @var{right-hand-side} @
17287   @dvar{indent-column, 26}, @dvar{wrap-column, 79})
17288 @asindex{HELP_STRING}
17290 Expands into an help string that looks pretty when the user executes
17291 @samp{configure --help}.  It is typically used in @code{AC_ARG_WITH}
17292 (@pxref{External Software}) or @code{AC_ARG_ENABLE} (@pxref{Package
17293 Options}).  The following example makes this clearer.
17295 @example
17296 AC_ARG_WITH([foo],
17297   [AS_HELP_STRING([--with-foo],
17298      [use foo (default is no)])],
17299   [use_foo=$withval],
17300   [use_foo=no])
17301 @end example
17303 Then the last few lines of @samp{configure --help} appear like
17304 this:
17306 @example
17307 --enable and --with options recognized:
17308   --with-foo              use foo (default is no)
17309 @end example
17311 Macro expansion is performed on the first argument.  However, the second
17312 argument of @code{AS_HELP_STRING} is treated as a whitespace separated
17313 list of text to be reformatted, and is not subject to macro expansion.
17314 Since it is not expanded, it should not be double quoted.
17315 @xref{Autoconf Language}, for a more detailed explanation.
17317 The @code{AS_HELP_STRING} macro is particularly helpful when the
17318 @var{left-hand-side} and/or @var{right-hand-side} are composed of macro
17319 arguments, as shown in the following example.
17321 @example
17322 AC_DEFUN([MY_ARG_WITH],
17323   [AC_ARG_WITH(m4_translit([[$1]], [_], [-]),
17324      [AS_HELP_STRING([--with-m4_translit([$1], [_], [-])],
17325                      [use $1 (default is $2)])],
17326      [use_[]$1=$withval],
17327      [use_[]$1=$2])])
17328 MY_ARG_WITH([a_b], [no])
17329 @end example
17330 @noindent
17331 Here, the last few lines of @samp{configure --help} will include:
17333 @example
17334 --enable and --with options recognized:
17335   --with-a-b              use a_b (default is no)
17336 @end example
17338 The parameters @var{indent-column} and @var{wrap-column} were introduced
17339 in Autoconf 2.62.  Generally, they should not be specified; they exist
17340 for fine-tuning of the wrapping.
17341 @example
17342 AS_HELP_STRING([--option], [description of option])
17343 @result{}  --option                description of option
17344 AS_HELP_STRING([--option], [description of option], [15], [30])
17345 @result{}  --option     description of
17346 @result{}               option
17347 @end example
17348 @end defmac
17351 @node Option Checking
17352 @section Controlling Checking of @command{configure} Options
17353 @cindex Options, Package
17355 The @command{configure} script checks its command-line options against a
17356 list of known options, like @option{--help} or @option{--config-cache}.
17357 An unknown option ordinarily indicates a mistake by the user and
17358 @command{configure} halts with an error.  However, by default unknown
17359 @option{--with-@var{package}} and @option{--enable-@var{feature}}
17360 options elicit only a warning, to support configuring entire source
17361 trees.
17363 Source trees often contain multiple packages with a top-level
17364 @command{configure} script that uses the @code{AC_CONFIG_SUBDIRS} macro
17365 (@pxref{Subdirectories}).  Because the packages generally support
17366 different @option{--with-@var{package}} and
17367 @option{--enable-@var{feature}} options, the @acronym{GNU} Coding
17368 Standards say they must accept unrecognized options without halting.
17369 Even a warning message is undesirable here, so @code{AC_CONFIG_SUBDIRS}
17370 automatically disables the warnings.
17372 This default behavior may be modified in two ways.  First, the installer
17373 can invoke @command{configure --disable-option-checking} to disable
17374 these warnings, or invoke @command{configure --enable-option-checking=fatal}
17375 options to turn them into fatal errors, respectively.  Second, the
17376 maintainer can use @code{AC_DISABLE_OPTION_CHECKING}.
17378 @defmac AC_DISABLE_OPTION_CHECKING
17379 @acindex{DISABLE_OPTION_CHECKING}
17381 By default, disable warnings related to any unrecognized
17382 @option{--with-@var{package}} or @option{--enable-@var{feature}}
17383 options.  This is implied by @code{AC_CONFIG_SUBDIRS}.
17385 The installer can override this behavior by passing
17386 @option{--enable-option-checking} (enable warnings) or
17387 @option{--enable-option-checking=fatal} (enable errors) to
17388 @command{configure}.
17389 @end defmac
17392 @node Site Details
17393 @section Configuring Site Details
17394 @cindex Site details
17396 Some software packages require complex site-specific information.  Some
17397 examples are host names to use for certain services, company names, and
17398 email addresses to contact.  Since some configuration scripts generated
17399 by Metaconfig ask for such information interactively, people sometimes
17400 wonder how to get that information in Autoconf-generated configuration
17401 scripts, which aren't interactive.
17403 Such site configuration information should be put in a file that is
17404 edited @emph{only by users}, not by programs.  The location of the file
17405 can either be based on the @code{prefix} variable, or be a standard
17406 location such as the user's home directory.  It could even be specified
17407 by an environment variable.  The programs should examine that file at
17408 runtime, rather than at compile time.  Runtime configuration is more
17409 convenient for users and makes the configuration process simpler than
17410 getting the information while configuring.  @xref{Directory Variables, ,
17411 Variables for Installation Directories, standards, @acronym{GNU} Coding
17412 Standards}, for more information on where to put data files.
17414 @node Transforming Names
17415 @section Transforming Program Names When Installing
17416 @cindex Transforming program names
17417 @cindex Program names, transforming
17419 Autoconf supports changing the names of programs when installing them.
17420 In order to use these transformations, @file{configure.ac} must call the
17421 macro @code{AC_ARG_PROGRAM}.
17423 @defmac AC_ARG_PROGRAM
17424 @acindex{ARG_PROGRAM}
17425 @ovindex program_transform_name
17426 Place in output variable @code{program_transform_name} a sequence of
17427 @code{sed} commands for changing the names of installed programs.
17429 If any of the options described below are given to @command{configure},
17430 program names are transformed accordingly.  Otherwise, if
17431 @code{AC_CANONICAL_TARGET} has been called and a @option{--target} value
17432 is given, the target type followed by a dash is used as a prefix.
17433 Otherwise, no program name transformation is done.
17434 @end defmac
17436 @menu
17437 * Transformation Options::      @command{configure} options to transform names
17438 * Transformation Examples::     Sample uses of transforming names
17439 * Transformation Rules::        Makefile uses of transforming names
17440 @end menu
17442 @node Transformation Options
17443 @subsection Transformation Options
17445 You can specify name transformations by giving @command{configure} these
17446 command line options:
17448 @table @option
17449 @item --program-prefix=@var{prefix}
17450 prepend @var{prefix} to the names;
17452 @item --program-suffix=@var{suffix}
17453 append @var{suffix} to the names;
17455 @item --program-transform-name=@var{expression}
17456 perform @code{sed} substitution @var{expression} on the names.
17457 @end table
17459 @node Transformation Examples
17460 @subsection Transformation Examples
17462 These transformations are useful with programs that can be part of a
17463 cross-compilation development environment.  For example, a
17464 cross-assembler running on a Sun 4 configured with
17465 @option{--target=i960-vxworks} is normally installed as
17466 @file{i960-vxworks-as}, rather than @file{as}, which could be confused
17467 with a native Sun 4 assembler.
17469 You can force a program name to begin with @file{g}, if you don't want
17470 @acronym{GNU} programs installed on your system to shadow other programs with
17471 the same name.  For example, if you configure @acronym{GNU} @code{diff} with
17472 @option{--program-prefix=g}, then when you run @samp{make install} it is
17473 installed as @file{/usr/local/bin/gdiff}.
17475 As a more sophisticated example, you could use
17477 @example
17478 --program-transform-name='s/^/g/; s/^gg/g/; s/^gless/less/'
17479 @end example
17480 @noindent
17482 to prepend @samp{g} to most of the program names in a source tree,
17483 excepting those like @code{gdb} that already have one and those like
17484 @code{less} and @code{lesskey} that aren't @acronym{GNU} programs.  (That is
17485 assuming that you have a source tree containing those programs that is
17486 set up to use this feature.)
17488 One way to install multiple versions of some programs simultaneously is
17489 to append a version number to the name of one or both.  For example, if
17490 you want to keep Autoconf version 1 around for awhile, you can configure
17491 Autoconf version 2 using @option{--program-suffix=2} to install the
17492 programs as @file{/usr/local/bin/autoconf2},
17493 @file{/usr/local/bin/autoheader2}, etc.  Nevertheless, pay attention
17494 that only the binaries are renamed, therefore you'd have problems with
17495 the library files which might overlap.
17497 @node Transformation Rules
17498 @subsection Transformation Rules
17500 Here is how to use the variable @code{program_transform_name} in a
17501 @file{Makefile.in}:
17503 @example
17504 PROGRAMS = cp ls rm
17505 transform = @@program_transform_name@@
17506 install:
17507         for p in $(PROGRAMS); do \
17508           $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p | \
17509                                               sed '$(transform)'`; \
17510         done
17512 uninstall:
17513         for p in $(PROGRAMS); do \
17514           rm -f $(DESTDIR)$(bindir)/`echo $$p | sed '$(transform)'`; \
17515         done
17516 @end example
17518 It is guaranteed that @code{program_transform_name} is never empty, and
17519 that there are no useless separators.  Therefore you may safely embed
17520 @code{program_transform_name} within a sed program using @samp{;}:
17522 @example
17523 transform = @@program_transform_name@@
17524 transform_exe = s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/
17525 @end example
17527 Whether to do the transformations on documentation files (Texinfo or
17528 @code{man}) is a tricky question; there seems to be no perfect answer,
17529 due to the several reasons for name transforming.  Documentation is not
17530 usually particular to a specific architecture, and Texinfo files do not
17531 conflict with system documentation.  But they might conflict with
17532 earlier versions of the same files, and @code{man} pages sometimes do
17533 conflict with system documentation.  As a compromise, it is probably
17534 best to do name transformations on @code{man} pages but not on Texinfo
17535 manuals.
17537 @node Site Defaults
17538 @section Setting Site Defaults
17539 @cindex Site defaults
17541 Autoconf-generated @command{configure} scripts allow your site to provide
17542 default values for some configuration values.  You do this by creating
17543 site- and system-wide initialization files.
17545 @evindex CONFIG_SITE
17546 If the environment variable @code{CONFIG_SITE} is set, @command{configure}
17547 uses its value as the name of a shell script to read.  Otherwise, it
17548 reads the shell script @file{@var{prefix}/share/config.site} if it exists,
17549 then @file{@var{prefix}/etc/config.site} if it exists.  Thus,
17550 settings in machine-specific files override those in machine-independent
17551 ones in case of conflict.
17553 Site files can be arbitrary shell scripts, but only certain kinds of
17554 code are really appropriate to be in them.  Because @command{configure}
17555 reads any cache file after it has read any site files, a site file can
17556 define a default cache file to be shared between all Autoconf-generated
17557 @command{configure} scripts run on that system (@pxref{Cache Files}).  If
17558 you set a default cache file in a site file, it is a good idea to also
17559 set the output variable @code{CC} in that site file, because the cache
17560 file is only valid for a particular compiler, but many systems have
17561 several available.
17563 You can examine or override the value set by a command line option to
17564 @command{configure} in a site file; options set shell variables that have
17565 the same names as the options, with any dashes turned into underscores.
17566 The exceptions are that @option{--without-} and @option{--disable-} options
17567 are like giving the corresponding @option{--with-} or @option{--enable-}
17568 option and the value @samp{no}.  Thus, @option{--cache-file=localcache}
17569 sets the variable @code{cache_file} to the value @samp{localcache};
17570 @option{--enable-warnings=no} or @option{--disable-warnings} sets the variable
17571 @code{enable_warnings} to the value @samp{no}; @option{--prefix=/usr} sets the
17572 variable @code{prefix} to the value @samp{/usr}; etc.
17574 Site files are also good places to set default values for other output
17575 variables, such as @code{CFLAGS}, if you need to give them non-default
17576 values: anything you would normally do, repetitively, on the command
17577 line.  If you use non-default values for @var{prefix} or
17578 @var{exec_prefix} (wherever you locate the site file), you can set them
17579 in the site file if you specify it with the @code{CONFIG_SITE}
17580 environment variable.
17582 You can set some cache values in the site file itself.  Doing this is
17583 useful if you are cross-compiling, where it is impossible to check features
17584 that require running a test program.  You could ``prime the cache'' by
17585 setting those values correctly for that system in
17586 @file{@var{prefix}/etc/config.site}.  To find out the names of the cache
17587 variables you need to set, look for shell variables with @samp{_cv_} in
17588 their names in the affected @command{configure} scripts, or in the Autoconf
17589 M4 source code for those macros.
17591 The cache file is careful to not override any variables set in the site
17592 files.  Similarly, you should not override command-line options in the
17593 site files.  Your code should check that variables such as @code{prefix}
17594 and @code{cache_file} have their default values (as set near the top of
17595 @command{configure}) before changing them.
17597 Here is a sample file @file{/usr/share/local/gnu/share/config.site}.  The
17598 command @samp{configure --prefix=/usr/share/local/gnu} would read this
17599 file (if @code{CONFIG_SITE} is not set to a different file).
17601 @example
17602 # config.site for configure
17604 # Change some defaults.
17605 test "$prefix" = NONE && prefix=/usr/share/local/gnu
17606 test "$exec_prefix" = NONE && exec_prefix=/usr/local/gnu
17607 test "$sharedstatedir" = '$prefix/com' && sharedstatedir=/var
17608 test "$localstatedir" = '$prefix/var' && localstatedir=/var
17610 # Give Autoconf 2.x generated configure scripts a shared default
17611 # cache file for feature test results, architecture-specific.
17612 if test "$cache_file" = /dev/null; then
17613   cache_file="$prefix/var/config.cache"
17614   # A cache file is only valid for one C compiler.
17615   CC=gcc
17617 @end example
17620 @c ============================================== Running configure Scripts.
17622 @node Running configure Scripts
17623 @chapter Running @command{configure} Scripts
17624 @cindex @command{configure}
17626 Below are instructions on how to configure a package that uses a
17627 @command{configure} script, suitable for inclusion as an @file{INSTALL}
17628 file in the package.  A plain-text version of @file{INSTALL} which you
17629 may use comes with Autoconf.
17631 @menu
17632 * Basic Installation::          Instructions for typical cases
17633 * Compilers and Options::       Selecting compilers and optimization
17634 * Multiple Architectures::      Compiling for multiple architectures at once
17635 * Installation Names::          Installing in different directories
17636 * Optional Features::           Selecting optional features
17637 * System Type::                 Specifying the system type
17638 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
17639 * Defining Variables::          Specifying the compiler etc.
17640 * configure Invocation::        Changing how @command{configure} runs
17641 @end menu
17643 @set autoconf
17644 @include install.texi
17647 @c ============================================== config.status Invocation
17649 @node config.status Invocation
17650 @chapter config.status Invocation
17651 @cindex @command{config.status}
17653 The @command{configure} script creates a file named @file{config.status},
17654 which actually configures, @dfn{instantiates}, the template files.  It
17655 also records the configuration options that were specified when the
17656 package was last configured in case reconfiguring is needed.
17658 Synopsis:
17659 @example
17660 ./config.status @var{option}@dots{} [@var{file}@dots{}]
17661 @end example
17663 It configures the @var{files}; if none are specified, all the templates
17664 are instantiated.  The files must be specified without their
17665 dependencies, as in
17667 @example
17668 ./config.status foobar
17669 @end example
17671 @noindent
17674 @example
17675 ./config.status foobar:foo.in:bar.in
17676 @end example
17678 The supported options are:
17680 @table @option
17681 @item --help
17682 @itemx -h
17683 Print a summary of the command line options, the list of the template
17684 files, and exit.
17686 @item --version
17687 @itemx -V
17688 Print the version number of Autoconf and the configuration settings,
17689 and exit.
17691 @item --silent
17692 @itemx --quiet
17693 @itemx -q
17694 Do not print progress messages.
17696 @item --debug
17697 @itemx -d
17698 Don't remove the temporary files.
17700 @item --file=@var{file}[:@var{template}]
17701 Require that @var{file} be instantiated as if
17702 @samp{AC_CONFIG_FILES(@var{file}:@var{template})} was used.  Both
17703 @var{file} and @var{template} may be @samp{-} in which case the standard
17704 output and/or standard input, respectively, is used.  If a
17705 @var{template} file name is relative, it is first looked for in the build
17706 tree, and then in the source tree.  @xref{Configuration Actions}, for
17707 more details.
17709 This option and the following ones provide one way for separately
17710 distributed packages to share the values computed by @command{configure}.
17711 Doing so can be useful if some of the packages need a superset of the
17712 features that one of them, perhaps a common library, does.  These
17713 options allow a @file{config.status} file to create files other than the
17714 ones that its @file{configure.ac} specifies, so it can be used for a
17715 different package.
17717 @item --header=@var{file}[:@var{template}]
17718 Same as @option{--file} above, but with @samp{AC_CONFIG_HEADERS}.
17720 @item --recheck
17721 Ask @file{config.status} to update itself and exit (no instantiation).
17722 This option is useful if you change @command{configure}, so that the
17723 results of some tests might be different from the previous run.  The
17724 @option{--recheck} option reruns @command{configure} with the same arguments
17725 you used before, plus the @option{--no-create} option, which prevents
17726 @command{configure} from running @file{config.status} and creating
17727 @file{Makefile} and other files, and the @option{--no-recursion} option,
17728 which prevents @command{configure} from running other @command{configure}
17729 scripts in subdirectories.  (This is so other Make rules can
17730 run @file{config.status} when it changes; @pxref{Automatic Remaking},
17731 for an example).
17732 @end table
17734 @file{config.status} checks several optional environment variables that
17735 can alter its behavior:
17737 @defvar CONFIG_SHELL
17738 @evindex CONFIG_SHELL
17739 The shell with which to run @command{configure} for the @option{--recheck}
17740 option.  It must be Bourne-compatible.  The default is a shell that
17741 supports @code{LINENO} if available, and @file{/bin/sh} otherwise.
17742 Invoking @command{configure} by hand bypasses this setting, so you may
17743 need to use a command like @samp{CONFIG_SHELL=/bin/bash /bin/bash ./configure}
17744 to insure that the same shell is used everywhere.  The absolute name of the
17745 shell should be passed.
17746 @end defvar
17748 @defvar CONFIG_STATUS
17749 @evindex CONFIG_STATUS
17750 The file name to use for the shell script that records the
17751 configuration.  The default is @file{./config.status}.  This variable is
17752 useful when one package uses parts of another and the @command{configure}
17753 scripts shouldn't be merged because they are maintained separately.
17754 @end defvar
17756 You can use @file{./config.status} in your makefiles.  For example, in
17757 the dependencies given above (@pxref{Automatic Remaking}),
17758 @file{config.status} is run twice when @file{configure.ac} has changed.
17759 If that bothers you, you can make each run only regenerate the files for
17760 that rule:
17761 @example
17762 @group
17763 config.h: stamp-h
17764 stamp-h: config.h.in config.status
17765         ./config.status config.h
17766         echo > stamp-h
17768 Makefile: Makefile.in config.status
17769         ./config.status Makefile
17770 @end group
17771 @end example
17773 The calling convention of @file{config.status} has changed; see
17774 @ref{Obsolete config.status Use}, for details.
17777 @c =================================================== Obsolete Constructs
17779 @node Obsolete Constructs
17780 @chapter Obsolete Constructs
17781 @cindex Obsolete constructs
17783 Autoconf changes, and throughout the years some constructs have been
17784 obsoleted.  Most of the changes involve the macros, but in some cases
17785 the tools themselves, or even some concepts, are now considered
17786 obsolete.
17788 You may completely skip this chapter if you are new to Autoconf.  Its
17789 intention is mainly to help maintainers updating their packages by
17790 understanding how to move to more modern constructs.
17792 @menu
17793 * Obsolete config.status Use::  Obsolete convention for @command{config.status}
17794 * acconfig Header::             Additional entries in @file{config.h.in}
17795 * autoupdate Invocation::       Automatic update of @file{configure.ac}
17796 * Obsolete Macros::             Backward compatibility macros
17797 * Autoconf 1::                  Tips for upgrading your files
17798 * Autoconf 2.13::               Some fresher tips
17799 @end menu
17801 @node Obsolete config.status Use
17802 @section Obsolete @file{config.status} Invocation
17804 @file{config.status} now supports arguments to specify the files to
17805 instantiate; see @ref{config.status Invocation}, for more details.
17806 Before, environment variables had to be used.
17808 @defvar CONFIG_COMMANDS
17809 @evindex CONFIG_COMMANDS
17810 The tags of the commands to execute.  The default is the arguments given
17811 to @code{AC_OUTPUT} and @code{AC_CONFIG_COMMANDS} in
17812 @file{configure.ac}.
17813 @end defvar
17815 @defvar CONFIG_FILES
17816 @evindex CONFIG_FILES
17817 The files in which to perform @samp{@@@var{variable}@@} substitutions.
17818 The default is the arguments given to @code{AC_OUTPUT} and
17819 @code{AC_CONFIG_FILES} in @file{configure.ac}.
17820 @end defvar
17822 @defvar CONFIG_HEADERS
17823 @evindex CONFIG_HEADERS
17824 The files in which to substitute C @code{#define} statements.  The
17825 default is the arguments given to @code{AC_CONFIG_HEADERS}; if that
17826 macro was not called, @file{config.status} ignores this variable.
17827 @end defvar
17829 @defvar CONFIG_LINKS
17830 @evindex CONFIG_LINKS
17831 The symbolic links to establish.  The default is the arguments given to
17832 @code{AC_CONFIG_LINKS}; if that macro was not called,
17833 @file{config.status} ignores this variable.
17834 @end defvar
17836 In @ref{config.status Invocation}, using this old interface, the example
17837 would be:
17839 @example
17840 @group
17841 config.h: stamp-h
17842 stamp-h: config.h.in config.status
17843         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_FILES= \
17844           CONFIG_HEADERS=config.h ./config.status
17845         echo > stamp-h
17847 Makefile: Makefile.in config.status
17848         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_HEADERS= \
17849           CONFIG_FILES=Makefile ./config.status
17850 @end group
17851 @end example
17853 @noindent
17854 (If @file{configure.ac} does not call @code{AC_CONFIG_HEADERS}, there is
17855 no need to set @code{CONFIG_HEADERS} in the @code{make} rules.  Equally
17856 for @code{CONFIG_COMMANDS}, etc.)
17859 @node acconfig Header
17860 @section @file{acconfig.h}
17862 @cindex @file{acconfig.h}
17863 @cindex @file{config.h.top}
17864 @cindex @file{config.h.bot}
17866 In order to produce @file{config.h.in}, @command{autoheader} needs to
17867 build or to find templates for each symbol.  Modern releases of Autoconf
17868 use @code{AH_VERBATIM} and @code{AH_TEMPLATE} (@pxref{Autoheader
17869 Macros}), but in older releases a file, @file{acconfig.h}, contained the
17870 list of needed templates.  @command{autoheader} copied comments and
17871 @code{#define} and @code{#undef} statements from @file{acconfig.h} in
17872 the current directory, if present.  This file used to be mandatory if
17873 you @code{AC_DEFINE} any additional symbols.
17875 Modern releases of Autoconf also provide @code{AH_TOP} and
17876 @code{AH_BOTTOM} if you need to prepend/append some information to
17877 @file{config.h.in}.  Ancient versions of Autoconf had a similar feature:
17878 if @file{./acconfig.h} contains the string @samp{@@TOP@@},
17879 @command{autoheader} copies the lines before the line containing
17880 @samp{@@TOP@@} into the top of the file that it generates.  Similarly,
17881 if @file{./acconfig.h} contains the string @samp{@@BOTTOM@@},
17882 @command{autoheader} copies the lines after that line to the end of the
17883 file it generates.  Either or both of those strings may be omitted.  An
17884 even older alternate way to produce the same effect in ancient versions
17885 of Autoconf is to create the files @file{@var{file}.top} (typically
17886 @file{config.h.top}) and/or @file{@var{file}.bot} in the current
17887 directory.  If they exist, @command{autoheader} copies them to the
17888 beginning and end, respectively, of its output.
17890 In former versions of Autoconf, the files used in preparing a software
17891 package for distribution were:
17892 @example
17893 @group
17894 configure.ac --.   .------> autoconf* -----> configure
17895                +---+
17896 [aclocal.m4] --+   `---.
17897 [acsite.m4] ---'       |
17898                        +--> [autoheader*] -> [config.h.in]
17899 [acconfig.h] ----.     |
17900                  +-----'
17901 [config.h.top] --+
17902 [config.h.bot] --'
17903 @end group
17904 @end example
17906 Using only the @code{AH_} macros, @file{configure.ac} should be
17907 self-contained, and should not depend upon @file{acconfig.h} etc.
17910 @node autoupdate Invocation
17911 @section Using @command{autoupdate} to Modernize @file{configure.ac}
17912 @cindex @command{autoupdate}
17914 The @command{autoupdate} program updates a @file{configure.ac} file that
17915 calls Autoconf macros by their old names to use the current macro names.
17916 In version 2 of Autoconf, most of the macros were renamed to use a more
17917 uniform and descriptive naming scheme.  @xref{Macro Names}, for a
17918 description of the new scheme.  Although the old names still work
17919 (@pxref{Obsolete Macros}, for a list of the old macros and the corresponding
17920 new names), you can make your @file{configure.ac} files more readable
17921 and make it easier to use the current Autoconf documentation if you
17922 update them to use the new macro names.
17924 @evindex SIMPLE_BACKUP_SUFFIX
17925 If given no arguments, @command{autoupdate} updates @file{configure.ac},
17926 backing up the original version with the suffix @file{~} (or the value
17927 of the environment variable @code{SIMPLE_BACKUP_SUFFIX}, if that is
17928 set).  If you give @command{autoupdate} an argument, it reads that file
17929 instead of @file{configure.ac} and writes the updated file to the
17930 standard output.
17932 @noindent
17933 @command{autoupdate} accepts the following options:
17935 @table @option
17936 @item --help
17937 @itemx -h
17938 Print a summary of the command line options and exit.
17940 @item --version
17941 @itemx -V
17942 Print the version number of Autoconf and exit.
17944 @item --verbose
17945 @itemx -v
17946 Report processing steps.
17948 @item --debug
17949 @itemx -d
17950 Don't remove the temporary files.
17952 @item --force
17953 @itemx -f
17954 Force the update even if the file has not changed.  Disregard the cache.
17956 @item --include=@var{dir}
17957 @itemx -I @var{dir}
17958 Also look for input files in @var{dir}.  Multiple invocations accumulate.
17959 Directories are browsed from last to first.
17960 @end table
17962 @node Obsolete Macros
17963 @section Obsolete Macros
17965 Several macros are obsoleted in Autoconf, for various reasons (typically
17966 they failed to quote properly, couldn't be extended for more recent
17967 issues, etc.).  They are still supported, but deprecated: their use
17968 should be avoided.
17970 During the jump from Autoconf version 1 to version 2, most of the
17971 macros were renamed to use a more uniform and descriptive naming scheme,
17972 but their signature did not change.  @xref{Macro Names}, for a
17973 description of the new naming scheme.  Below, if there is just the mapping
17974 from old names to new names for these macros, the reader is invited to
17975 refer to the definition of the new macro for the signature and the
17976 description.
17978 @defmac AC_AIX
17979 @acindex{AIX}
17980 @cvindex _ALL_SOURCE
17981 This macro is a platform-specific subset of
17982 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
17983 @end defmac
17985 @defmac AC_ALLOCA
17986 @acindex{ALLOCA}
17987 Replaced by @code{AC_FUNC_ALLOCA} (@pxref{AC_FUNC_ALLOCA}).
17988 @end defmac
17990 @defmac AC_ARG_ARRAY
17991 @acindex{ARG_ARRAY}
17992 Removed because of limited usefulness.
17993 @end defmac
17995 @defmac AC_C_CROSS
17996 @acindex{C_CROSS}
17997 This macro is obsolete; it does nothing.
17998 @end defmac
18000 @defmac AC_C_LONG_DOUBLE
18001 @acindex{C_LONG_DOUBLE}
18002 @cvindex HAVE_LONG_DOUBLE
18003 If the C compiler supports a working @code{long double} type with more
18004 range or precision than the @code{double} type, define
18005 @code{HAVE_LONG_DOUBLE}.
18007 You should use @code{AC_TYPE_LONG_DOUBLE} or
18008 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
18009 @end defmac
18011 @defmac AC_CANONICAL_SYSTEM
18012 @acindex{CANONICAL_SYSTEM}
18013 Determine the system type and set output variables to the names of the
18014 canonical system types.  @xref{Canonicalizing}, for details about the
18015 variables this macro sets.
18017 The user is encouraged to use either @code{AC_CANONICAL_BUILD}, or
18018 @code{AC_CANONICAL_HOST}, or @code{AC_CANONICAL_TARGET}, depending on
18019 the needs.  Using @code{AC_CANONICAL_TARGET} is enough to run the two
18020 other macros (@pxref{Canonicalizing}).
18021 @end defmac
18023 @defmac AC_CHAR_UNSIGNED
18024 @acindex{CHAR_UNSIGNED}
18025 Replaced by @code{AC_C_CHAR_UNSIGNED} (@pxref{AC_C_CHAR_UNSIGNED}).
18026 @end defmac
18028 @defmac AC_CHECK_TYPE (@var{type}, @var{default})
18029 @acindex{CHECK_TYPE}
18030 Autoconf, up to 2.13, used to provide this version of
18031 @code{AC_CHECK_TYPE}, deprecated because of its flaws.  First, although
18032 it is a member of the @code{CHECK} clan, it does
18033 more than just checking.  Secondly, missing types are defined
18034 using @code{#define}, not @code{typedef}, and this can lead to
18035 problems in the case of pointer types.
18037 This use of @code{AC_CHECK_TYPE} is obsolete and discouraged; see
18038 @ref{Generic Types}, for the description of the current macro.
18040 If the type @var{type} is not defined, define it to be the C (or C++)
18041 builtin type @var{default}, e.g., @samp{short int} or @samp{unsigned int}.
18043 This macro is equivalent to:
18045 @example
18046 AC_CHECK_TYPE([@var{type}], [],
18047   [AC_DEFINE_UNQUOTED([@var{type}], [@var{default}],
18048      [Define to `@var{default}'
18049       if <sys/types.h> does not define.])])
18050 @end example
18052 In order to keep backward compatibility, the two versions of
18053 @code{AC_CHECK_TYPE} are implemented, selected using these heuristics:
18055 @enumerate
18056 @item
18057 If there are three or four arguments, the modern version is used.
18059 @item
18060 If the second argument appears to be a C or C++ type, then the
18061 obsolete version is used.  This happens if the argument is a C or C++
18062 @emph{builtin} type or a C identifier ending in @samp{_t}, optionally
18063 followed by one of @samp{[(* } and then by a string of zero or more
18064 characters taken from the set @samp{[]()* _a-zA-Z0-9}.
18066 @item
18067 If the second argument is spelled with the alphabet of valid C and C++
18068 types, the user is warned and the modern version is used.
18070 @item
18071 Otherwise, the modern version is used.
18072 @end enumerate
18074 @noindent
18075 You are encouraged either to use a valid builtin type, or to use the
18076 equivalent modern code (see above), or better yet, to use
18077 @code{AC_CHECK_TYPES} together with
18079 @example
18080 #ifndef HAVE_LOFF_T
18081 typedef loff_t off_t;
18082 #endif
18083 @end example
18084 @end defmac
18085 @c end of AC_CHECK_TYPE
18087 @defmac AC_CHECKING (@var{feature-description})
18088 @acindex{CHECKING}
18089 Same as
18091 @example
18092 AC_MSG_NOTICE([checking @var{feature-description}@dots{}]
18093 @end example
18095 @noindent
18096 @xref{AC_MSG_NOTICE}.
18097 @end defmac
18099 @defmac AC_COMPILE_CHECK (@var{echo-text}, @var{includes}, @
18100   @var{function-body}, @var{action-if-true}, @ovar{action-if-false})
18101 @acindex{COMPILE_CHECK}
18102 This is an obsolete version of @code{AC_TRY_COMPILE} itself replaced by
18103 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}), with the
18104 addition that it prints @samp{checking for @var{echo-text}} to the
18105 standard output first, if @var{echo-text} is non-empty.  Use
18106 @code{AC_MSG_CHECKING} and @code{AC_MSG_RESULT} instead to print
18107 messages (@pxref{Printing Messages}).
18108 @end defmac
18110 @defmac AC_CONST
18111 @acindex{CONST}
18112 Replaced by @code{AC_C_CONST} (@pxref{AC_C_CONST}).
18113 @end defmac
18115 @defmac AC_CROSS_CHECK
18116 @acindex{CROSS_CHECK}
18117 Same as @code{AC_C_CROSS}, which is obsolete too, and does nothing
18118 @code{:-)}.
18119 @end defmac
18121 @defmac AC_CYGWIN
18122 @acindex{CYGWIN}
18123 @evindex CYGWIN
18124 Check for the Cygwin environment in which case the shell variable
18125 @code{CYGWIN} is set to @samp{yes}.  Don't use this macro, the dignified
18126 means to check the nature of the host is using @code{AC_CANONICAL_HOST}
18127 (@pxref{Canonicalizing}).  As a matter of fact this macro is defined as:
18129 @example
18130 AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
18131 case $host_os in
18132   *cygwin* ) CYGWIN=yes;;
18133          * ) CYGWIN=no;;
18134 esac
18135 @end example
18137 Beware that the variable @env{CYGWIN} has a special meaning when
18138 running Cygwin, and should not be changed.  That's yet another reason
18139 not to use this macro.
18140 @end defmac
18142 @defmac AC_DECL_SYS_SIGLIST
18143 @acindex{DECL_SYS_SIGLIST}
18144 @cvindex SYS_SIGLIST_DECLARED
18145 Same as:
18147 @example
18148 AC_CHECK_DECLS([sys_siglist], [], [],
18149 [#include <signal.h>
18150 /* NetBSD declares sys_siglist in unistd.h.  */
18151 #ifdef HAVE_UNISTD_H
18152 # include <unistd.h>
18153 #endif
18155 @end example
18157 @noindent
18158 @xref{AC_CHECK_DECLS}.
18159 @end defmac
18161 @defmac AC_DECL_YYTEXT
18162 @acindex{DECL_YYTEXT}
18163 Does nothing, now integrated in @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
18164 @end defmac
18166 @defmac AC_DIR_HEADER
18167 @acindex{DIR_HEADER}
18168 @cvindex DIRENT
18169 @cvindex SYSNDIR
18170 @cvindex SYSDIR
18171 @cvindex NDIR
18172 Like calling @code{AC_FUNC_CLOSEDIR_VOID}
18173 (@pxref{AC_FUNC_CLOSEDIR_VOID}) and @code{AC_HEADER_DIRENT}
18174 (@pxref{AC_HEADER_DIRENT}),
18175 but defines a different set of C preprocessor macros to indicate which
18176 header file is found:
18178 @multitable {@file{sys/ndir.h}} {Old Symbol} {@code{HAVE_SYS_NDIR_H}}
18179 @item Header            @tab Old Symbol     @tab New Symbol
18180 @item @file{dirent.h}   @tab @code{DIRENT}  @tab @code{HAVE_DIRENT_H}
18181 @item @file{sys/ndir.h} @tab @code{SYSNDIR} @tab @code{HAVE_SYS_NDIR_H}
18182 @item @file{sys/dir.h}  @tab @code{SYSDIR}  @tab @code{HAVE_SYS_DIR_H}
18183 @item @file{ndir.h}     @tab @code{NDIR}    @tab @code{HAVE_NDIR_H}
18184 @end multitable
18185 @end defmac
18187 @defmac AC_DYNIX_SEQ
18188 @acindex{DYNIX_SEQ}
18189 If on DYNIX/ptx, add @option{-lseq} to output variable
18190 @code{LIBS}.  This macro used to be defined as
18192 @example
18193 AC_CHECK_LIB([seq], [getmntent], [LIBS="-lseq $LIBS"])
18194 @end example
18196 @noindent
18197 now it is just @code{AC_FUNC_GETMNTENT} (@pxref{AC_FUNC_GETMNTENT}).
18198 @end defmac
18200 @defmac AC_EXEEXT
18201 @acindex{EXEEXT}
18202 @ovindex EXEEXT
18203 Defined the output variable @code{EXEEXT} based on the output of the
18204 compiler, which is now done automatically.  Typically set to empty
18205 string if Posix and @samp{.exe} if a @acronym{DOS} variant.
18206 @end defmac
18208 @defmac AC_EMXOS2
18209 @acindex{EMXOS2}
18210 Similar to @code{AC_CYGWIN} but checks for the EMX environment on OS/2
18211 and sets @code{EMXOS2}.  Don't use this macro, the dignified means to
18212 check the nature of the host is using @code{AC_CANONICAL_HOST}
18213 (@pxref{Canonicalizing}).
18214 @end defmac
18216 @defmac AC_ENABLE (@var{feature}, @var{action-if-given}, @
18217   @ovar{action-if-not-given})
18218 @acindex{ENABLE}
18219 This is an obsolete version of @code{AC_ARG_ENABLE} that does not
18220 support providing a help string (@pxref{AC_ARG_ENABLE}).
18221 @end defmac
18223 @defmac AC_ERROR
18224 @acindex{ERROR}
18225 Replaced by @code{AC_MSG_ERROR} (@pxref{AC_MSG_ERROR}).
18226 @end defmac
18228 @defmac AC_FIND_X
18229 @acindex{FIND_X}
18230 Replaced by @code{AC_PATH_X} (@pxref{AC_PATH_X}).
18231 @end defmac
18233 @defmac AC_FIND_XTRA
18234 @acindex{FIND_XTRA}
18235 Replaced by @code{AC_PATH_XTRA} (@pxref{AC_PATH_XTRA}).
18236 @end defmac
18238 @defmac AC_FOREACH
18239 @acindex{FOREACH}
18240 Replaced by @code{m4_foreach_w} (@pxref{m4_foreach_w}).
18241 @end defmac
18243 @defmac AC_FUNC_CHECK
18244 @acindex{FUNC_CHECK}
18245 Replaced by @code{AC_CHECK_FUNC} (@pxref{AC_CHECK_FUNC}).
18246 @end defmac
18248 @anchor{AC_FUNC_SETVBUF_REVERSED}
18249 @defmac AC_FUNC_SETVBUF_REVERSED
18250 @acindex{FUNC_SETVBUF_REVERSED}
18251 @cvindex SETVBUF_REVERSED
18252 @c @fuindex setvbuf
18253 @prindex @code{setvbuf}
18254 Do nothing.  Formerly, this macro checked whether @code{setvbuf} takes
18255 the buffering type as its second argument and the buffer pointer as the
18256 third, instead of the other way around, and defined
18257 @code{SETVBUF_REVERSED}.  However, the last systems to have the problem
18258 were those based on SVR2, which became obsolete in 1987, and the macro
18259 is no longer needed.
18260 @end defmac
18262 @defmac AC_FUNC_WAIT3
18263 @acindex{FUNC_WAIT3}
18264 @cvindex HAVE_WAIT3
18265 If @code{wait3} is found and fills in the contents of its third argument
18266 (a @samp{struct rusage *}), which @acronym{HP-UX} does not do, define
18267 @code{HAVE_WAIT3}.
18269 These days portable programs should use @code{waitpid}, not
18270 @code{wait3}, as @code{wait3} has been removed from Posix.
18271 @end defmac
18273 @defmac AC_GCC_TRADITIONAL
18274 @acindex{GCC_TRADITIONAL}
18275 Replaced by @code{AC_PROG_GCC_TRADITIONAL} (@pxref{AC_PROG_GCC_TRADITIONAL}).
18276 @end defmac
18278 @defmac AC_GETGROUPS_T
18279 @acindex{GETGROUPS_T}
18280 Replaced by @code{AC_TYPE_GETGROUPS} (@pxref{AC_TYPE_GETGROUPS}).
18281 @end defmac
18283 @defmac AC_GETLOADAVG
18284 @acindex{GETLOADAVG}
18285 Replaced by @code{AC_FUNC_GETLOADAVG} (@pxref{AC_FUNC_GETLOADAVG}).
18286 @end defmac
18288 @defmac AC_GNU_SOURCE
18289 @acindex{GNU_SOURCE}
18290 @cvindex _GNU_SOURCE
18291 This macro is a platform-specific subset of
18292 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
18293 @end defmac
18295 @defmac AC_HAVE_FUNCS
18296 @acindex{HAVE_FUNCS}
18297 Replaced by @code{AC_CHECK_FUNCS} (@pxref{AC_CHECK_FUNCS}).
18298 @end defmac
18300 @defmac AC_HAVE_HEADERS
18301 @acindex{HAVE_HEADERS}
18302 Replaced by @code{AC_CHECK_HEADERS} (@pxref{AC_CHECK_HEADERS}).
18303 @end defmac
18305 @defmac AC_HAVE_LIBRARY (@var{library}, @ovar{action-if-found}, @
18306   @ovar{action-if-not-found}, @ovar{other-libraries})
18307 @acindex{HAVE_LIBRARY}
18308 This macro is equivalent to calling @code{AC_CHECK_LIB} with a
18309 @var{function} argument of @code{main}.  In addition, @var{library} can
18310 be written as any of @samp{foo}, @option{-lfoo}, or @samp{libfoo.a}.  In
18311 all of those cases, the compiler is passed @option{-lfoo}.  However,
18312 @var{library} cannot be a shell variable; it must be a literal name.
18313 @xref{AC_CHECK_LIB}.
18314 @end defmac
18316 @defmac AC_HAVE_POUNDBANG
18317 @acindex{HAVE_POUNDBANG}
18318 Replaced by @code{AC_SYS_INTERPRETER} (@pxref{AC_SYS_INTERPRETER}).
18319 @end defmac
18321 @defmac AC_HEADER_CHECK
18322 @acindex{HEADER_CHECK}
18323 Replaced by @code{AC_CHECK_HEADER} (@pxref{AC_CHECK_HEADER}).
18324 @end defmac
18326 @defmac AC_HEADER_EGREP
18327 @acindex{HEADER_EGREP}
18328 Replaced by @code{AC_EGREP_HEADER} (@pxref{AC_EGREP_HEADER}).
18329 @end defmac
18331 @defmac AC_HELP_STRING
18332 @acindex{HELP_STRING}
18333 Replaced by @code{AS_HELP_STRING} (@pxref{AS_HELP_STRING}).
18334 @end defmac
18336 @defmac AC_INIT (@var{unique-file-in-source-dir})
18337 @acindex{INIT}
18338 Formerly @code{AC_INIT} used to have a single argument, and was
18339 equivalent to:
18341 @example
18342 AC_INIT
18343 AC_CONFIG_SRCDIR(@var{unique-file-in-source-dir})
18344 @end example
18345 See @ref{AC_INIT} and @ref{AC_CONFIG_SRCDIR}.
18346 @end defmac
18348 @defmac AC_INLINE
18349 @acindex{INLINE}
18350 Replaced by @code{AC_C_INLINE} (@pxref{AC_C_INLINE}).
18351 @end defmac
18353 @defmac AC_INT_16_BITS
18354 @acindex{INT_16_BITS}
18355 @cvindex INT_16_BITS
18356 If the C type @code{int} is 16 bits wide, define @code{INT_16_BITS}.
18357 Use @samp{AC_CHECK_SIZEOF(int)} instead (@pxref{AC_CHECK_SIZEOF}).
18358 @end defmac
18360 @defmac AC_IRIX_SUN
18361 @acindex{IRIX_SUN}
18362 If on @sc{irix} (Silicon Graphics Unix), add @option{-lsun} to output
18363 @code{LIBS}.  If you were using it to get @code{getmntent}, use
18364 @code{AC_FUNC_GETMNTENT} instead.  If you used it for the NIS versions
18365 of the password and group functions, use @samp{AC_CHECK_LIB(sun,
18366 getpwnam)}.  Up to Autoconf 2.13, it used to be
18368 @example
18369 AC_CHECK_LIB([sun], [getmntent], [LIBS="-lsun $LIBS"])
18370 @end example
18372 @noindent
18373 now it is defined as
18375 @example
18376 AC_FUNC_GETMNTENT
18377 AC_CHECK_LIB([sun], [getpwnam])
18378 @end example
18380 @noindent
18381 See @ref{AC_FUNC_GETMNTENT} and @ref{AC_CHECK_LIB}.
18382 @end defmac
18384 @defmac AC_ISC_POSIX
18385 @acindex{ISC_POSIX}
18386 @ovindex LIBS
18387 This macro adds @option{-lcposix} to output variable @code{LIBS} if
18388 necessary for Posix facilities.  Sun dropped support for the obsolete
18389 @sc{interactive} Systems Corporation Unix on 2006-07-23.  New programs
18390 need not use this macro.  It is implemented as
18391 @code{AC_SEARCH_LIBS([strerror], [cposix])} (@pxref{AC_SEARCH_LIBS}).
18392 @end defmac
18394 @defmac AC_LANG_C
18395 @acindex{LANG_C}
18396 Same as @samp{AC_LANG([C])} (@pxref{AC_LANG}).
18397 @end defmac
18399 @defmac AC_LANG_CPLUSPLUS
18400 @acindex{LANG_CPLUSPLUS}
18401 Same as @samp{AC_LANG([C++])} (@pxref{AC_LANG}).
18402 @end defmac
18404 @defmac AC_LANG_FORTRAN77
18405 @acindex{LANG_FORTRAN77}
18406 Same as @samp{AC_LANG([Fortran 77])} (@pxref{AC_LANG}).
18407 @end defmac
18409 @defmac AC_LANG_RESTORE
18410 @acindex{LANG_RESTORE}
18411 Select the @var{language} that is saved on the top of the stack, as set
18412 by @code{AC_LANG_SAVE}, remove it from the stack, and call
18413 @code{AC_LANG(@var{language})}.  @xref{Language Choice}, for the
18414 preferred way to change languages.
18415 @end defmac
18417 @defmac AC_LANG_SAVE
18418 @acindex{LANG_SAVE}
18419 Remember the current language (as set by @code{AC_LANG}) on a stack.
18420 The current language does not change.  @code{AC_LANG_PUSH} is preferred
18421 (@pxref{AC_LANG_PUSH}).
18422 @end defmac
18424 @defmac AC_LINK_FILES (@var{source}@dots{}, @var{dest}@dots{})
18425 @acindex{LINK_FILES}
18426 This is an obsolete version of @code{AC_CONFIG_LINKS}
18427 (@pxref{AC_CONFIG_LINKS}.  An updated version of:
18429 @example
18430 AC_LINK_FILES(config/$machine.h config/$obj_format.h,
18431               host.h            object.h)
18432 @end example
18434 @noindent
18437 @example
18438 AC_CONFIG_LINKS([host.h:config/$machine.h
18439                 object.h:config/$obj_format.h])
18440 @end example
18441 @end defmac
18443 @defmac AC_LN_S
18444 @acindex{LN_S}
18445 Replaced by @code{AC_PROG_LN_S} (@pxref{AC_PROG_LN_S}).
18446 @end defmac
18448 @defmac AC_LONG_64_BITS
18449 @acindex{LONG_64_BITS}
18450 @cvindex LONG_64_BITS
18451 Define @code{LONG_64_BITS} if the C type @code{long int} is 64 bits wide.
18452 Use the generic macro @samp{AC_CHECK_SIZEOF([long int])} instead
18453 (@pxref{AC_CHECK_SIZEOF}).
18454 @end defmac
18456 @defmac AC_LONG_DOUBLE
18457 @acindex{LONG_DOUBLE}
18458 If the C compiler supports a working @code{long double} type with more
18459 range or precision than the @code{double} type, define
18460 @code{HAVE_LONG_DOUBLE}.
18462 You should use @code{AC_TYPE_LONG_DOUBLE} or
18463 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
18464 @end defmac
18466 @defmac AC_LONG_FILE_NAMES
18467 @acindex{LONG_FILE_NAMES}
18468 Replaced by
18469 @example
18470 AC_SYS_LONG_FILE_NAMES
18471 @end example
18472 @noindent
18473 @xref{AC_SYS_LONG_FILE_NAMES}.
18474 @end defmac
18476 @defmac AC_MAJOR_HEADER
18477 @acindex{MAJOR_HEADER}
18478 Replaced by @code{AC_HEADER_MAJOR} (@pxref{AC_HEADER_MAJOR}).
18479 @end defmac
18481 @defmac AC_MEMORY_H
18482 @acindex{MEMORY_H}
18483 @cvindex NEED_MEMORY_H
18484 Used to define @code{NEED_MEMORY_H} if the @code{mem} functions were
18485 defined in @file{memory.h}.  Today it is equivalent to
18486 @samp{AC_CHECK_HEADERS([memory.h])} (@pxref{AC_CHECK_HEADERS}).  Adjust
18487 your code to depend upon
18488 @code{HAVE_MEMORY_H}, not @code{NEED_MEMORY_H}; see @ref{Standard
18489 Symbols}.
18490 @end defmac
18492 @defmac AC_MINGW32
18493 @acindex{MINGW32}
18494 Similar to @code{AC_CYGWIN} but checks for the MinGW compiler
18495 environment and sets @code{MINGW32}.  Don't use this macro, the
18496 dignified means to check the nature of the host is using
18497 @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
18498 @end defmac
18500 @defmac AC_MINIX
18501 @acindex{MINIX}
18502 @cvindex _MINIX
18503 @cvindex _POSIX_SOURCE
18504 @cvindex _POSIX_1_SOURCE
18505 This macro is a platform-specific subset of
18506 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
18507 @end defmac
18509 @defmac AC_MINUS_C_MINUS_O
18510 @acindex{MINUS_C_MINUS_O}
18511 Replaced by @code{AC_PROG_CC_C_O} (@pxref{AC_PROG_CC_C_O}).
18512 @end defmac
18514 @defmac AC_MMAP
18515 @acindex{MMAP}
18516 Replaced by @code{AC_FUNC_MMAP} (@pxref{AC_FUNC_MMAP}).
18517 @end defmac
18519 @defmac AC_MODE_T
18520 @acindex{MODE_T}
18521 Replaced by @code{AC_TYPE_MODE_T} (@pxref{AC_TYPE_MODE_T}).
18522 @end defmac
18524 @defmac AC_OBJEXT
18525 @acindex{OBJEXT}
18526 @ovindex OBJEXT
18527 Defined the output variable @code{OBJEXT} based on the output of the
18528 compiler, after .c files have been excluded.  Typically set to @samp{o}
18529 if Posix, @samp{obj} if a @acronym{DOS} variant.
18530 Now the compiler checking macros handle
18531 this automatically.
18532 @end defmac
18534 @defmac AC_OBSOLETE (@var{this-macro-name}, @ovar{suggestion})
18535 @acindex{OBSOLETE}
18536 Make M4 print a message to the standard error output warning that
18537 @var{this-macro-name} is obsolete, and giving the file and line number
18538 where it was called.  @var{this-macro-name} should be the name of the
18539 macro that is calling @code{AC_OBSOLETE}.  If @var{suggestion} is given,
18540 it is printed at the end of the warning message; for example, it can be
18541 a suggestion for what to use instead of @var{this-macro-name}.
18543 For instance
18545 @example
18546 AC_OBSOLETE([$0], [; use AC_CHECK_HEADERS(unistd.h) instead])dnl
18547 @end example
18549 @noindent
18550 You are encouraged to use @code{AU_DEFUN} instead, since it gives better
18551 services to the user (@pxref{AU_DEFUN}).
18552 @end defmac
18554 @defmac AC_OFF_T
18555 @acindex{OFF_T}
18556 Replaced by @code{AC_TYPE_OFF_T} (@pxref{AC_TYPE_OFF_T}).
18557 @end defmac
18559 @defmac AC_OUTPUT (@ovar{file}@dots{}, @ovar{extra-cmds}, @ovar{init-cmds})
18560 @acindex{OUTPUT}
18561 The use of @code{AC_OUTPUT} with arguments is deprecated.  This obsoleted
18562 interface is equivalent to:
18564 @example
18565 @group
18566 AC_CONFIG_FILES(@var{file}@dots{})
18567 AC_CONFIG_COMMANDS([default],
18568                    @var{extra-cmds}, @var{init-cmds})
18569 AC_OUTPUT
18570 @end group
18571 @end example
18573 @noindent
18574 See @ref{AC_CONFIG_FILES}, @ref{AC_CONFIG_COMMANDS}, and @ref{AC_OUTPUT}.
18575 @end defmac
18577 @defmac AC_OUTPUT_COMMANDS (@var{extra-cmds}, @ovar{init-cmds})
18578 @acindex{OUTPUT_COMMANDS}
18579 Specify additional shell commands to run at the end of
18580 @file{config.status}, and shell commands to initialize any variables
18581 from @command{configure}.  This macro may be called multiple times.  It is
18582 obsolete, replaced by @code{AC_CONFIG_COMMANDS} (@pxref{AC_CONFIG_COMMANDS}).
18584 Here is an unrealistic example:
18586 @example
18587 fubar=27
18588 AC_OUTPUT_COMMANDS([echo this is extra $fubar, and so on.],
18589                    [fubar=$fubar])
18590 AC_OUTPUT_COMMANDS([echo this is another, extra, bit],
18591                    [echo init bit])
18592 @end example
18594 Aside from the fact that @code{AC_CONFIG_COMMANDS} requires an
18595 additional key, an important difference is that
18596 @code{AC_OUTPUT_COMMANDS} is quoting its arguments twice, unlike
18597 @code{AC_CONFIG_COMMANDS}.  This means that @code{AC_CONFIG_COMMANDS}
18598 can safely be given macro calls as arguments:
18600 @example
18601 AC_CONFIG_COMMANDS(foo, [my_FOO()])
18602 @end example
18604 @noindent
18605 Conversely, where one level of quoting was enough for literal strings
18606 with @code{AC_OUTPUT_COMMANDS}, you need two with
18607 @code{AC_CONFIG_COMMANDS}.  The following lines are equivalent:
18609 @example
18610 @group
18611 AC_OUTPUT_COMMANDS([echo "Square brackets: []"])
18612 AC_CONFIG_COMMANDS([default], [[echo "Square brackets: []"]])
18613 @end group
18614 @end example
18615 @end defmac
18617 @defmac AC_PID_T
18618 @acindex{PID_T}
18619 Replaced by @code{AC_TYPE_PID_T} (@pxref{AC_TYPE_PID_T}).
18620 @end defmac
18622 @defmac AC_PREFIX
18623 @acindex{PREFIX}
18624 Replaced by @code{AC_PREFIX_PROGRAM} (@pxref{AC_PREFIX_PROGRAM}).
18625 @end defmac
18627 @defmac AC_PROGRAMS_CHECK
18628 @acindex{PROGRAMS_CHECK}
18629 Replaced by @code{AC_CHECK_PROGS} (@pxref{AC_CHECK_PROGS}).
18630 @end defmac
18632 @defmac AC_PROGRAMS_PATH
18633 @acindex{PROGRAMS_PATH}
18634 Replaced by @code{AC_PATH_PROGS} (@pxref{AC_PATH_PROGS}).
18635 @end defmac
18637 @defmac AC_PROGRAM_CHECK
18638 @acindex{PROGRAM_CHECK}
18639 Replaced by @code{AC_CHECK_PROG} (@pxref{AC_CHECK_PROG}).
18640 @end defmac
18642 @defmac AC_PROGRAM_EGREP
18643 @acindex{PROGRAM_EGREP}
18644 Replaced by @code{AC_EGREP_CPP} (@pxref{AC_EGREP_CPP}).
18645 @end defmac
18647 @defmac AC_PROGRAM_PATH
18648 @acindex{PROGRAM_PATH}
18649 Replaced by @code{AC_PATH_PROG} (@pxref{AC_PATH_PROG}).
18650 @end defmac
18652 @defmac AC_REMOTE_TAPE
18653 @acindex{REMOTE_TAPE}
18654 Removed because of limited usefulness.
18655 @end defmac
18657 @defmac AC_RESTARTABLE_SYSCALLS
18658 @acindex{RESTARTABLE_SYSCALLS}
18659 This macro was renamed @code{AC_SYS_RESTARTABLE_SYSCALLS}.  However,
18660 these days portable programs should use @code{sigaction} with
18661 @code{SA_RESTART} if they want restartable system calls.  They should
18662 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
18663 system call is restartable is a dynamic issue, not a configuration-time
18664 issue.
18665 @end defmac
18667 @defmac AC_RETSIGTYPE
18668 @acindex{RETSIGTYPE}
18669 Replaced by @code{AC_TYPE_SIGNAL} (@pxref{AC_TYPE_SIGNAL}).
18670 @end defmac
18672 @defmac AC_RSH
18673 @acindex{RSH}
18674 Removed because of limited usefulness.
18675 @end defmac
18677 @defmac AC_SCO_INTL
18678 @acindex{SCO_INTL}
18679 @ovindex LIBS
18680 If on SCO Unix, add @option{-lintl} to output variable @code{LIBS}.  This
18681 macro used to do this:
18683 @example
18684 AC_CHECK_LIB([intl], [strftime], [LIBS="-lintl $LIBS"])
18685 @end example
18687 @noindent
18688 Now it just calls @code{AC_FUNC_STRFTIME} instead (@pxref{AC_FUNC_STRFTIME}).
18689 @end defmac
18691 @defmac AC_SETVBUF_REVERSED
18692 @acindex{SETVBUF_REVERSED}
18693 Replaced by
18694 @example
18695 AC_FUNC_SETVBUF_REVERSED
18696 @end example
18697 @noindent
18698 @xref{AC_FUNC_SETVBUF_REVERSED}.
18699 @end defmac
18701 @defmac AC_SET_MAKE
18702 @acindex{SET_MAKE}
18703 Replaced by @code{AC_PROG_MAKE_SET} (@pxref{AC_PROG_MAKE_SET}).
18704 @end defmac
18706 @defmac AC_SIZEOF_TYPE
18707 @acindex{SIZEOF_TYPE}
18708 Replaced by @code{AC_CHECK_SIZEOF} (@pxref{AC_CHECK_SIZEOF}).
18709 @end defmac
18711 @defmac AC_SIZE_T
18712 @acindex{SIZE_T}
18713 Replaced by @code{AC_TYPE_SIZE_T} (@pxref{AC_TYPE_SIZE_T}).
18714 @end defmac
18716 @defmac AC_STAT_MACROS_BROKEN
18717 @acindex{STAT_MACROS_BROKEN}
18718 Replaced by @code{AC_HEADER_STAT} (@pxref{AC_HEADER_STAT}).
18719 @end defmac
18721 @defmac AC_STDC_HEADERS
18722 @acindex{STDC_HEADERS}
18723 Replaced by @code{AC_HEADER_STDC} (@pxref{AC_HEADER_STDC}).
18724 @end defmac
18726 @defmac AC_STRCOLL
18727 @acindex{STRCOLL}
18728 Replaced by @code{AC_FUNC_STRCOLL} (@pxref{AC_FUNC_STRCOLL}).
18729 @end defmac
18731 @defmac AC_STRUCT_ST_BLKSIZE
18732 @acindex{STRUCT_ST_BLKSIZE}
18733 @cvindex HAVE_STRUCT_STAT_ST_BLKSIZE
18734 @cvindex HAVE_ST_BLKSIZE
18735 If @code{struct stat} contains an @code{st_blksize} member, define
18736 @code{HAVE_STRUCT_STAT_ST_BLKSIZE}.  The former name,
18737 @code{HAVE_ST_BLKSIZE} is to be avoided, as its support will cease in
18738 the future.  This macro is obsoleted, and should be replaced by
18740 @example
18741 AC_CHECK_MEMBERS([struct stat.st_blksize])
18742 @end example
18743 @noindent
18744 @xref{AC_CHECK_MEMBERS}.
18745 @end defmac
18747 @defmac AC_STRUCT_ST_RDEV
18748 @acindex{STRUCT_ST_RDEV}
18749 @cvindex HAVE_ST_RDEV
18750 @cvindex HAVE_STRUCT_STAT_ST_RDEV
18751 If @code{struct stat} contains an @code{st_rdev} member, define
18752 @code{HAVE_STRUCT_STAT_ST_RDEV}.  The former name for this macro,
18753 @code{HAVE_ST_RDEV}, is to be avoided as it will cease to be supported
18754 in the future.  Actually, even the new macro is obsolete and should be
18755 replaced by:
18756 @example
18757 AC_CHECK_MEMBERS([struct stat.st_rdev])
18758 @end example
18759 @noindent
18760 @xref{AC_CHECK_MEMBERS}.
18761 @end defmac
18763 @defmac AC_ST_BLKSIZE
18764 @acindex{ST_BLKSIZE}
18765 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
18766 @end defmac
18768 @defmac AC_ST_BLOCKS
18769 @acindex{ST_BLOCKS}
18770 Replaced by @code{AC_STRUCT_ST_BLOCKS} (@pxref{AC_STRUCT_ST_BLOCKS}).
18771 @end defmac
18773 @defmac AC_ST_RDEV
18774 @acindex{ST_RDEV}
18775 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
18776 @end defmac
18778 @defmac AC_SYS_RESTARTABLE_SYSCALLS
18779 @acindex{SYS_RESTARTABLE_SYSCALLS}
18780 @cvindex HAVE_RESTARTABLE_SYSCALLS
18781 If the system automatically restarts a system call that is interrupted
18782 by a signal, define @code{HAVE_RESTARTABLE_SYSCALLS}.  This macro does
18783 not check whether system calls are restarted in general---it checks whether a
18784 signal handler installed with @code{signal} (but not @code{sigaction})
18785 causes system calls to be restarted.  It does not check whether system calls
18786 can be restarted when interrupted by signals that have no handler.
18788 These days portable programs should use @code{sigaction} with
18789 @code{SA_RESTART} if they want restartable system calls.  They should
18790 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
18791 system call is restartable is a dynamic issue, not a configuration-time
18792 issue.
18793 @end defmac
18795 @defmac AC_SYS_SIGLIST_DECLARED
18796 @acindex{SYS_SIGLIST_DECLARED}
18797 This macro was renamed @code{AC_DECL_SYS_SIGLIST}.  However, even that
18798 name is obsolete, as the same functionality is now acheived via
18799 @code{AC_CHECK_DECLS} (@pxref{AC_CHECK_DECLS}).
18800 @end defmac
18802 @defmac AC_TEST_CPP
18803 @acindex{TEST_CPP}
18804 This macro was renamed @code{AC_TRY_CPP}, which in turn was replaced by
18805 @code{AC_PREPROC_IFELSE} (@pxref{AC_PREPROC_IFELSE}).
18806 @end defmac
18808 @defmac AC_TEST_PROGRAM
18809 @acindex{TEST_PROGRAM}
18810 This macro was renamed @code{AC_TRY_RUN}, which in turn was replaced by
18811 @code{AC_RUN_IFELSE} (@pxref{AC_RUN_IFELSE}).
18812 @end defmac
18814 @defmac AC_TIMEZONE
18815 @acindex{TIMEZONE}
18816 Replaced by @code{AC_STRUCT_TIMEZONE} (@pxref{AC_STRUCT_TIMEZONE}).
18817 @end defmac
18819 @defmac AC_TIME_WITH_SYS_TIME
18820 @acindex{TIME_WITH_SYS_TIME}
18821 Replaced by @code{AC_HEADER_TIME} (@pxref{AC_HEADER_TIME}).
18822 @end defmac
18824 @defmac AC_TRY_COMPILE (@var{includes}, @var{function-body}, @
18825   @ovar{action-if-true}, @ovar{action-if-false})
18826 @acindex{TRY_COMPILE}
18827 Same as:
18829 @example
18830 AC_COMPILE_IFELSE(
18831   [AC_LANG_PROGRAM([[@var{includes}]],
18832      [[@var{function-body}]])],
18833   [@var{action-if-true}],
18834   [@var{action-if-false}])
18835 @end example
18837 @noindent
18838 @xref{Running the Compiler}.
18840 This macro double quotes both @var{includes} and @var{function-body}.
18842 For C and C++, @var{includes} is any @code{#include} statements needed
18843 by the code in @var{function-body} (@var{includes} is ignored if
18844 the currently selected language is Fortran or Fortran 77).  The compiler
18845 and compilation flags are determined by the current language
18846 (@pxref{Language Choice}).
18847 @end defmac
18849 @defmac AC_TRY_CPP (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
18850 @acindex{TRY_CPP}
18851 Same as:
18853 @example
18854 AC_PREPROC_IFELSE(
18855   [AC_LANG_SOURCE([[@var{input}]])],
18856   [@var{action-if-true}],
18857   [@var{action-if-false}])
18858 @end example
18860 @noindent
18861 @xref{Running the Preprocessor}.
18863 This macro double quotes the @var{input}.
18864 @end defmac
18866 @defmac AC_TRY_LINK (@var{includes}, @var{function-body}, @
18867   @ovar{action-if-true}, @ovar{action-if-false})
18868 @acindex{TRY_LINK}
18869 Same as:
18871 @example
18872 AC_LINK_IFELSE(
18873   [AC_LANG_PROGRAM([[@var{includes}]],
18874      [[@var{function-body}]])],
18875   [@var{action-if-true}],
18876   [@var{action-if-false}])
18877 @end example
18879 @noindent
18880 @xref{Running the Compiler}.
18882 This macro double quotes both @var{includes} and @var{function-body}.
18884 Depending on the current language (@pxref{Language Choice}), create a
18885 test program to see whether a function whose body consists of
18886 @var{function-body} can be compiled and linked.  If the file compiles
18887 and links successfully, run shell commands @var{action-if-found},
18888 otherwise run @var{action-if-not-found}.
18890 This macro double quotes both @var{includes} and @var{function-body}.
18892 For C and C++, @var{includes} is any @code{#include} statements needed
18893 by the code in @var{function-body} (@var{includes} is ignored if
18894 the currently selected language is Fortran or Fortran 77).  The compiler
18895 and compilation flags are determined by the current language
18896 (@pxref{Language Choice}), and in addition @code{LDFLAGS} and
18897 @code{LIBS} are used for linking.
18898 @end defmac
18900 @defmac AC_TRY_LINK_FUNC (@var{function}, @ovar{action-if-found}, @
18901   @ovar{action-if-not-found})
18902 @acindex{TRY_LINK_FUNC}
18903 This macro is equivalent to
18904 @example
18905 AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])],
18906   [@var{action-if-found}], [@var{action-if-not-found}])
18907 @end example
18908 @noindent
18909 @xref{AC_LINK_IFELSE}.
18910 @end defmac
18912 @defmac AC_TRY_RUN (@var{program}, @ovar{action-if-true}, @
18913   @ovar{action-if-false}, @ovar{action-if-cross-compiling})
18914 @acindex{TRY_RUN}
18915 Same as:
18917 @example
18918 AC_RUN_IFELSE(
18919   [AC_LANG_SOURCE([[@var{program}]])],
18920   [@var{action-if-true}],
18921   [@var{action-if-false}],
18922   [@var{action-if-cross-compiling}])
18923 @end example
18925 @noindent
18926 @xref{Runtime}.
18927 @end defmac
18929 @defmac AC_UID_T
18930 @acindex{UID_T}
18931 Replaced by @code{AC_TYPE_UID_T} (@pxref{AC_TYPE_UID_T}).
18932 @end defmac
18934 @defmac AC_UNISTD_H
18935 @acindex{UNISTD_H}
18936 Same as @samp{AC_CHECK_HEADERS([unistd.h])} (@pxref{AC_CHECK_HEADERS}).
18937 @end defmac
18939 @defmac AC_USG
18940 @acindex{USG}
18941 @cvindex USG
18942 Define @code{USG} if the @acronym{BSD} string functions are defined in
18943 @file{strings.h}.  You should no longer depend upon @code{USG}, but on
18944 @code{HAVE_STRING_H}; see @ref{Standard Symbols}.
18945 @end defmac
18947 @defmac AC_UTIME_NULL
18948 @acindex{UTIME_NULL}
18949 Replaced by @code{AC_FUNC_UTIME_NULL} (@pxref{AC_FUNC_UTIME_NULL}).
18950 @end defmac
18952 @defmac AC_VALIDATE_CACHED_SYSTEM_TUPLE (@ovar{cmd})
18953 @acindex{VALIDATE_CACHED_SYSTEM_TUPLE}
18954 If the cache file is inconsistent with the current host, target and
18955 build system types, it used to execute @var{cmd} or print a default
18956 error message.  This is now handled by default.
18957 @end defmac
18959 @defmac AC_VERBOSE (@var{result-description})
18960 @acindex{VERBOSE}
18961 Replaced by @code{AC_MSG_RESULT} (@pxref{AC_MSG_RESULT}).
18962 @end defmac
18964 @defmac AC_VFORK
18965 @acindex{VFORK}
18966 Replaced by @code{AC_FUNC_FORK} (@pxref{AC_FUNC_FORK}).
18967 @end defmac
18969 @defmac AC_VPRINTF
18970 @acindex{VPRINTF}
18971 Replaced by @code{AC_FUNC_VPRINTF} (@pxref{AC_FUNC_VPRINTF}).
18972 @end defmac
18974 @defmac AC_WAIT3
18975 @acindex{WAIT3}
18976 This macro was renamed @code{AC_FUNC_WAIT3}.  However, these days
18977 portable programs should use @code{waitpid}, not @code{wait3}, as
18978 @code{wait3} has been removed from Posix.
18979 @end defmac
18981 @defmac AC_WARN
18982 @acindex{WARN}
18983 Replaced by @code{AC_MSG_WARN} (@pxref{AC_MSG_WARN}).
18984 @end defmac
18986 @defmac AC_WITH (@var{package}, @var{action-if-given}, @
18987   @ovar{action-if-not-given})
18988 @acindex{WITH}
18989 This is an obsolete version of @code{AC_ARG_WITH} that does not
18990 support providing a help string (@pxref{AC_ARG_WITH}).
18991 @end defmac
18993 @defmac AC_WORDS_BIGENDIAN
18994 @acindex{WORDS_BIGENDIAN}
18995 Replaced by @code{AC_C_BIGENDIAN} (@pxref{AC_C_BIGENDIAN}).
18996 @end defmac
18998 @defmac AC_XENIX_DIR
18999 @acindex{XENIX_DIR}
19000 @ovindex LIBS
19001 This macro used to add @option{-lx} to output variable @code{LIBS} if on
19002 Xenix.  Also, if @file{dirent.h} is being checked for, added
19003 @option{-ldir} to @code{LIBS}.  Now it is merely an alias of
19004 @code{AC_HEADER_DIRENT} instead, plus some code to detect whether
19005 running @sc{xenix} on which you should not depend:
19007 @example
19008 AC_MSG_CHECKING([for Xenix])
19009 AC_EGREP_CPP([yes],
19010 [#if defined M_XENIX && !defined M_UNIX
19011   yes
19012 #endif],
19013              [AC_MSG_RESULT([yes]); XENIX=yes],
19014              [AC_MSG_RESULT([no]); XENIX=])
19015 @end example
19016 @noindent
19017 Don't use this macro, the dignified means to check the nature of the
19018 host is using @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
19019 @end defmac
19021 @defmac AC_YYTEXT_POINTER
19022 @acindex{YYTEXT_POINTER}
19023 This macro was renamed @code{AC_DECL_YYTEXT}, which in turn was
19024 integrated into @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
19025 @end defmac
19027 @node Autoconf 1
19028 @section Upgrading From Version 1
19029 @cindex Upgrading autoconf
19030 @cindex Autoconf upgrading
19032 Autoconf version 2 is mostly backward compatible with version 1.
19033 However, it introduces better ways to do some things, and doesn't
19034 support some of the ugly things in version 1.  So, depending on how
19035 sophisticated your @file{configure.ac} files are, you might have to do
19036 some manual work in order to upgrade to version 2.  This chapter points
19037 out some problems to watch for when upgrading.  Also, perhaps your
19038 @command{configure} scripts could benefit from some of the new features in
19039 version 2; the changes are summarized in the file @file{NEWS} in the
19040 Autoconf distribution.
19042 @menu
19043 * Changed File Names::          Files you might rename
19044 * Changed Makefiles::           New things to put in @file{Makefile.in}
19045 * Changed Macros::              Macro calls you might replace
19046 * Changed Results::             Changes in how to check test results
19047 * Changed Macro Writing::       Better ways to write your own macros
19048 @end menu
19050 @node Changed File Names
19051 @subsection Changed File Names
19053 If you have an @file{aclocal.m4} installed with Autoconf (as opposed to
19054 in a particular package's source directory), you must rename it to
19055 @file{acsite.m4}.  @xref{autoconf Invocation}.
19057 If you distribute @file{install.sh} with your package, rename it to
19058 @file{install-sh} so @code{make} builtin rules don't inadvertently
19059 create a file called @file{install} from it.  @code{AC_PROG_INSTALL}
19060 looks for the script under both names, but it is best to use the new name.
19062 If you were using @file{config.h.top}, @file{config.h.bot}, or
19063 @file{acconfig.h}, you still can, but you have less clutter if you
19064 use the @code{AH_} macros.  @xref{Autoheader Macros}.
19066 @node Changed Makefiles
19067 @subsection Changed Makefiles
19069 Add @samp{@@CFLAGS@@}, @samp{@@CPPFLAGS@@}, and @samp{@@LDFLAGS@@} in
19070 your @file{Makefile.in} files, so they can take advantage of the values
19071 of those variables in the environment when @command{configure} is run.
19072 Doing this isn't necessary, but it's a convenience for users.
19074 Also add @samp{@@configure_input@@} in a comment to each input file for
19075 @code{AC_OUTPUT}, so that the output files contain a comment saying
19076 they were produced by @command{configure}.  Automatically selecting the
19077 right comment syntax for all the kinds of files that people call
19078 @code{AC_OUTPUT} on became too much work.
19080 Add @file{config.log} and @file{config.cache} to the list of files you
19081 remove in @code{distclean} targets.
19083 If you have the following in @file{Makefile.in}:
19085 @example
19086 prefix = /usr/local
19087 exec_prefix = $(prefix)
19088 @end example
19090 @noindent
19091 you must change it to:
19093 @example
19094 prefix = @@prefix@@
19095 exec_prefix = @@exec_prefix@@
19096 @end example
19098 @noindent
19099 The old behavior of replacing those variables without @samp{@@}
19100 characters around them has been removed.
19102 @node Changed Macros
19103 @subsection Changed Macros
19105 Many of the macros were renamed in Autoconf version 2.  You can still
19106 use the old names, but the new ones are clearer, and it's easier to find
19107 the documentation for them.  @xref{Obsolete Macros}, for a table showing the
19108 new names for the old macros.  Use the @command{autoupdate} program to
19109 convert your @file{configure.ac} to using the new macro names.
19110 @xref{autoupdate Invocation}.
19112 Some macros have been superseded by similar ones that do the job better,
19113 but are not call-compatible.  If you get warnings about calling obsolete
19114 macros while running @command{autoconf}, you may safely ignore them, but
19115 your @command{configure} script generally works better if you follow
19116 the advice that is printed about what to replace the obsolete macros with.  In
19117 particular, the mechanism for reporting the results of tests has
19118 changed.  If you were using @command{echo} or @code{AC_VERBOSE} (perhaps
19119 via @code{AC_COMPILE_CHECK}), your @command{configure} script's output
19120 looks better if you switch to @code{AC_MSG_CHECKING} and
19121 @code{AC_MSG_RESULT}.  @xref{Printing Messages}.  Those macros work best
19122 in conjunction with cache variables.  @xref{Caching Results}.
19126 @node Changed Results
19127 @subsection Changed Results
19129 If you were checking the results of previous tests by examining the
19130 shell variable @code{DEFS}, you need to switch to checking the values of
19131 the cache variables for those tests.  @code{DEFS} no longer exists while
19132 @command{configure} is running; it is only created when generating output
19133 files.  This difference from version 1 is because properly quoting the
19134 contents of that variable turned out to be too cumbersome and
19135 inefficient to do every time @code{AC_DEFINE} is called.  @xref{Cache
19136 Variable Names}.
19138 For example, here is a @file{configure.ac} fragment written for Autoconf
19139 version 1:
19141 @example
19142 AC_HAVE_FUNCS(syslog)
19143 case "$DEFS" in
19144 *-DHAVE_SYSLOG*) ;;
19145 *) # syslog is not in the default libraries.  See if it's in some other.
19146   saved_LIBS="$LIBS"
19147   for lib in bsd socket inet; do
19148     AC_CHECKING(for syslog in -l$lib)
19149     LIBS="-l$lib $saved_LIBS"
19150     AC_HAVE_FUNCS(syslog)
19151     case "$DEFS" in
19152     *-DHAVE_SYSLOG*) break ;;
19153     *) ;;
19154     esac
19155     LIBS="$saved_LIBS"
19156   done ;;
19157 esac
19158 @end example
19160 Here is a way to write it for version 2:
19162 @example
19163 AC_CHECK_FUNCS([syslog])
19164 if test $ac_cv_func_syslog = no; then
19165   # syslog is not in the default libraries.  See if it's in some other.
19166   for lib in bsd socket inet; do
19167     AC_CHECK_LIB([$lib], [syslog], [AC_DEFINE([HAVE_SYSLOG])
19168       LIBS="-l$lib $LIBS"; break])
19169   done
19171 @end example
19173 If you were working around bugs in @code{AC_DEFINE_UNQUOTED} by adding
19174 backslashes before quotes, you need to remove them.  It now works
19175 predictably, and does not treat quotes (except back quotes) specially.
19176 @xref{Setting Output Variables}.
19178 All of the Boolean shell variables set by Autoconf macros now use
19179 @samp{yes} for the true value.  Most of them use @samp{no} for false,
19180 though for backward compatibility some use the empty string instead.  If
19181 you were relying on a shell variable being set to something like 1 or
19182 @samp{t} for true, you need to change your tests.
19184 @node Changed Macro Writing
19185 @subsection Changed Macro Writing
19187 When defining your own macros, you should now use @code{AC_DEFUN}
19188 instead of @code{define}.  @code{AC_DEFUN} automatically calls
19189 @code{AC_PROVIDE} and ensures that macros called via @code{AC_REQUIRE}
19190 do not interrupt other macros, to prevent nested @samp{checking@dots{}}
19191 messages on the screen.  There's no actual harm in continuing to use the
19192 older way, but it's less convenient and attractive.  @xref{Macro
19193 Definitions}.
19195 You probably looked at the macros that came with Autoconf as a guide for
19196 how to do things.  It would be a good idea to take a look at the new
19197 versions of them, as the style is somewhat improved and they take
19198 advantage of some new features.
19200 If you were doing tricky things with undocumented Autoconf internals
19201 (macros, variables, diversions), check whether you need to change
19202 anything to account for changes that have been made.  Perhaps you can
19203 even use an officially supported technique in version 2 instead of
19204 kludging.  Or perhaps not.
19206 To speed up your locally written feature tests, add caching to them.
19207 See whether any of your tests are of general enough usefulness to
19208 encapsulate them into macros that you can share.
19211 @node Autoconf 2.13
19212 @section Upgrading From Version 2.13
19213 @cindex Upgrading autoconf
19214 @cindex Autoconf upgrading
19216 The introduction of the previous section (@pxref{Autoconf 1}) perfectly
19217 suits this section@enddots{}
19219 @quotation
19220 Autoconf version 2.50 is mostly backward compatible with version 2.13.
19221 However, it introduces better ways to do some things, and doesn't
19222 support some of the ugly things in version 2.13.  So, depending on how
19223 sophisticated your @file{configure.ac} files are, you might have to do
19224 some manual work in order to upgrade to version 2.50.  This chapter
19225 points out some problems to watch for when upgrading.  Also, perhaps
19226 your @command{configure} scripts could benefit from some of the new
19227 features in version 2.50; the changes are summarized in the file
19228 @file{NEWS} in the Autoconf distribution.
19229 @end quotation
19231 @menu
19232 * Changed Quotation::           Broken code which used to work
19233 * New Macros::                  Interaction with foreign macros
19234 * Hosts and Cross-Compilation::  Bugward compatibility kludges
19235 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
19236 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
19237 @end menu
19239 @node Changed Quotation
19240 @subsection Changed Quotation
19242 The most important changes are invisible to you: the implementation of
19243 most macros have completely changed.  This allowed more factorization of
19244 the code, better error messages, a higher uniformity of the user's
19245 interface etc.  Unfortunately, as a side effect, some construct which
19246 used to (miraculously) work might break starting with Autoconf 2.50.
19247 The most common culprit is bad quotation.
19249 For instance, in the following example, the message is not properly
19250 quoted:
19252 @example
19253 AC_INIT
19254 AC_CHECK_HEADERS(foo.h, ,
19255   AC_MSG_ERROR(cannot find foo.h, bailing out))
19256 AC_OUTPUT
19257 @end example
19259 @noindent
19260 Autoconf 2.13 simply ignores it:
19262 @example
19263 $ @kbd{autoconf-2.13; ./configure --silent}
19264 creating cache ./config.cache
19265 configure: error: cannot find foo.h
19267 @end example
19269 @noindent
19270 while Autoconf 2.50 produces a broken @file{configure}:
19272 @example
19273 $ @kbd{autoconf-2.50; ./configure --silent}
19274 configure: error: cannot find foo.h
19275 ./configure: exit: bad non-numeric arg `bailing'
19276 ./configure: exit: bad non-numeric arg `bailing'
19278 @end example
19280 The message needs to be quoted, and the @code{AC_MSG_ERROR} invocation
19281 too!
19283 @example
19284 AC_INIT([Example], [1.0], [bug-example@@example.org])
19285 AC_CHECK_HEADERS([foo.h], [],
19286   [AC_MSG_ERROR([cannot find foo.h, bailing out])])
19287 AC_OUTPUT
19288 @end example
19290 Many many (and many more) Autoconf macros were lacking proper quotation,
19291 including no less than@dots{} @code{AC_DEFUN} itself!
19293 @example
19294 $ @kbd{cat configure.in}
19295 AC_DEFUN([AC_PROG_INSTALL],
19296 [# My own much better version
19298 AC_INIT
19299 AC_PROG_INSTALL
19300 AC_OUTPUT
19301 $ @kbd{autoconf-2.13}
19302 autoconf: Undefined macros:
19303 ***BUG in Autoconf--please report*** AC_FD_MSG
19304 ***BUG in Autoconf--please report*** AC_EPI
19305 configure.in:1:AC_DEFUN([AC_PROG_INSTALL],
19306 configure.in:5:AC_PROG_INSTALL
19307 $ @kbd{autoconf-2.50}
19309 @end example
19312 @node New Macros
19313 @subsection New Macros
19315 @cindex undefined macro
19316 @cindex @code{_m4_divert_diversion}
19318 While Autoconf was relatively dormant in the late 1990s, Automake
19319 provided Autoconf-like macros for a while.  Starting with Autoconf 2.50
19320 in 2001, Autoconf provided
19321 versions of these macros, integrated in the @code{AC_} namespace,
19322 instead of @code{AM_}.  But in order to ease the upgrading via
19323 @command{autoupdate}, bindings to such @code{AM_} macros are provided.
19325 Unfortunately older versions of Automake (e.g., Automake 1.4)
19326 did not quote the names of these macros.
19327 Therefore, when @command{m4} finds something like
19328 @samp{AC_DEFUN(AM_TYPE_PTRDIFF_T, @dots{})} in @file{aclocal.m4},
19329 @code{AM_TYPE_PTRDIFF_T} is
19330 expanded, replaced with its Autoconf definition.
19332 Fortunately Autoconf catches pre-@code{AC_INIT} expansions, and
19333 complains, in its own words:
19335 @example
19336 $ @kbd{cat configure.ac}
19337 AC_INIT([Example], [1.0], [bug-example@@example.org])
19338 AM_TYPE_PTRDIFF_T
19339 $ @kbd{aclocal-1.4}
19340 $ @kbd{autoconf}
19341 aclocal.m4:17: error: m4_defn: undefined macro: _m4_divert_diversion
19342 aclocal.m4:17: the top level
19343 autom4te: m4 failed with exit status: 1
19345 @end example
19347 Modern versions of Automake no longer define most of these
19348 macros, and properly quote the names of the remaining macros.
19349 If you must use an old Automake, do not depend upon macros from Automake
19350 as it is simply not its job
19351 to provide macros (but the one it requires itself):
19353 @example
19354 $ @kbd{cat configure.ac}
19355 AC_INIT([Example], [1.0], [bug-example@@example.org])
19356 AM_TYPE_PTRDIFF_T
19357 $ @kbd{rm aclocal.m4}
19358 $ @kbd{autoupdate}
19359 autoupdate: `configure.ac' is updated
19360 $ @kbd{cat configure.ac}
19361 AC_INIT([Example], [1.0], [bug-example@@example.org])
19362 AC_CHECK_TYPES([ptrdiff_t])
19363 $ @kbd{aclocal-1.4}
19364 $ @kbd{autoconf}
19366 @end example
19369 @node Hosts and Cross-Compilation
19370 @subsection Hosts and Cross-Compilation
19371 @cindex Cross compilation
19373 Based on the experience of compiler writers, and after long public
19374 debates, many aspects of the cross-compilation chain have changed:
19376 @itemize @minus
19377 @item
19378 the relationship between the build, host, and target architecture types,
19380 @item
19381 the command line interface for specifying them to @command{configure},
19383 @item
19384 the variables defined in @command{configure},
19386 @item
19387 the enabling of cross-compilation mode.
19388 @end itemize
19390 @sp 1
19392 The relationship between build, host, and target have been cleaned up:
19393 the chain of default is now simply: target defaults to host, host to
19394 build, and build to the result of @command{config.guess}.  Nevertheless,
19395 in order to ease the transition from 2.13 to 2.50, the following
19396 transition scheme is implemented.  @emph{Do not rely on it}, as it will
19397 be completely disabled in a couple of releases (we cannot keep it, as it
19398 proves to cause more problems than it cures).
19400 They all default to the result of running @command{config.guess}, unless
19401 you specify either @option{--build} or @option{--host}.  In this case,
19402 the default becomes the system type you specified.  If you specify both,
19403 and they're different, @command{configure} enters cross compilation
19404 mode, so it doesn't run any tests that require execution.
19406 Hint: if you mean to override the result of @command{config.guess},
19407 prefer @option{--build} over @option{--host}.  In the future,
19408 @option{--host} will not override the name of the build system type.
19409 Whenever you specify @option{--host}, be sure to specify @option{--build}
19410 too.
19412 @sp 1
19414 For backward compatibility, @command{configure} accepts a system
19415 type as an option by itself.  Such an option overrides the
19416 defaults for build, host, and target system types.  The following
19417 configure statement configures a cross toolchain that runs on
19418 Net@acronym{BSD}/alpha but generates code for @acronym{GNU} Hurd/sparc,
19419 which is also the build platform.
19421 @example
19422 ./configure --host=alpha-netbsd sparc-gnu
19423 @end example
19425 @sp 1
19427 In Autoconf 2.13 and before, the variables @code{build}, @code{host},
19428 and @code{target} had a different semantics before and after the
19429 invocation of @code{AC_CANONICAL_BUILD} etc.  Now, the argument of
19430 @option{--build} is strictly copied into @code{build_alias}, and is left
19431 empty otherwise.  After the @code{AC_CANONICAL_BUILD}, @code{build} is
19432 set to the canonicalized build type.  To ease the transition, before,
19433 its contents is the same as that of @code{build_alias}.  Do @emph{not}
19434 rely on this broken feature.
19436 For consistency with the backward compatibility scheme exposed above,
19437 when @option{--host} is specified but @option{--build} isn't, the build
19438 system is assumed to be the same as @option{--host}, and
19439 @samp{build_alias} is set to that value.  Eventually, this
19440 historically incorrect behavior will go away.
19442 @sp 1
19444 The former scheme to enable cross-compilation proved to cause more harm
19445 than good, in particular, it used to be triggered too easily, leaving
19446 regular end users puzzled in front of cryptic error messages.
19447 @command{configure} could even enter cross-compilation mode only
19448 because the compiler was not functional.  This is mainly because
19449 @command{configure} used to try to detect cross-compilation, instead of
19450 waiting for an explicit flag from the user.
19452 Now, @command{configure} enters cross-compilation mode if and only if
19453 @option{--host} is passed.
19455 That's the short documentation.  To ease the transition between 2.13 and
19456 its successors, a more complicated scheme is implemented.  @emph{Do not
19457 rely on the following}, as it will be removed in the near future.
19459 If you specify @option{--host}, but not @option{--build}, when
19460 @command{configure} performs the first compiler test it tries to run
19461 an executable produced by the compiler.  If the execution fails, it
19462 enters cross-compilation mode.  This is fragile.  Moreover, by the time
19463 the compiler test is performed, it may be too late to modify the
19464 build-system type: other tests may have already been performed.
19465 Therefore, whenever you specify @option{--host}, be sure to specify
19466 @option{--build} too.
19468 @example
19469 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
19470 @end example
19472 @noindent
19473 enters cross-compilation mode.  The former interface, which
19474 consisted in setting the compiler to a cross-compiler without informing
19475 @command{configure} is obsolete.  For instance, @command{configure}
19476 fails if it can't run the code generated by the specified compiler if you
19477 configure as follows:
19479 @example
19480 ./configure CC=m68k-coff-gcc
19481 @end example
19484 @node AC_LIBOBJ vs LIBOBJS
19485 @subsection @code{AC_LIBOBJ} vs.@: @code{LIBOBJS}
19487 Up to Autoconf 2.13, the replacement of functions was triggered via the
19488 variable @code{LIBOBJS}.  Since Autoconf 2.50, the macro
19489 @code{AC_LIBOBJ} should be used instead (@pxref{Generic Functions}).
19490 Starting at Autoconf 2.53, the use of @code{LIBOBJS} is an error.
19492 This change is mandated by the unification of the @acronym{GNU} Build System
19493 components.  In particular, the various fragile techniques used to parse
19494 a @file{configure.ac} are all replaced with the use of traces.  As a
19495 consequence, any action must be traceable, which obsoletes critical
19496 variable assignments.  Fortunately, @code{LIBOBJS} was the only problem,
19497 and it can even be handled gracefully (read, ``without your having to
19498 change something'').
19500 There were two typical uses of @code{LIBOBJS}: asking for a replacement
19501 function, and adjusting @code{LIBOBJS} for Automake and/or Libtool.
19503 @sp 1
19505 As for function replacement, the fix is immediate: use
19506 @code{AC_LIBOBJ}.  For instance:
19508 @example
19509 LIBOBJS="$LIBOBJS fnmatch.o"
19510 LIBOBJS="$LIBOBJS malloc.$ac_objext"
19511 @end example
19513 @noindent
19514 should be replaced with:
19516 @example
19517 AC_LIBOBJ([fnmatch])
19518 AC_LIBOBJ([malloc])
19519 @end example
19521 @sp 1
19523 @ovindex LIBOBJDIR
19524 When used with Automake 1.10 or newer, a suitable value for
19525 @code{LIBOBJDIR} is set so that the @code{LIBOBJS} and @code{LTLIBOBJS}
19526 can be referenced from any @file{Makefile.am}.  Even without Automake,
19527 arranging for @code{LIBOBJDIR} to be set correctly enables
19528 referencing @code{LIBOBJS} and @code{LTLIBOBJS} in another directory.
19529 The @code{LIBOBJDIR} feature is experimental.
19532 @node AC_FOO_IFELSE vs AC_TRY_FOO
19533 @subsection @code{AC_FOO_IFELSE} vs.@: @code{AC_TRY_FOO}
19535 Since Autoconf 2.50, internal codes uses @code{AC_PREPROC_IFELSE},
19536 @code{AC_COMPILE_IFELSE}, @code{AC_LINK_IFELSE}, and
19537 @code{AC_RUN_IFELSE} on one hand and @code{AC_LANG_SOURCES},
19538 and @code{AC_LANG_PROGRAM} on the other hand instead of the deprecated
19539 @code{AC_TRY_CPP}, @code{AC_TRY_COMPILE}, @code{AC_TRY_LINK}, and
19540 @code{AC_TRY_RUN}.  The motivations where:
19541 @itemize @minus
19542 @item
19543 a more consistent interface: @code{AC_TRY_COMPILE} etc.@: were double
19544 quoting their arguments;
19546 @item
19547 the combinatoric explosion is solved by decomposing on the one hand the
19548 generation of sources, and on the other hand executing the program;
19550 @item
19551 this scheme helps supporting more languages than plain C and C++.
19552 @end itemize
19554 In addition to the change of syntax, the philosophy has changed too:
19555 while emphasis was put on speed at the expense of accuracy, today's
19556 Autoconf promotes accuracy of the testing framework at, ahem@dots{}, the
19557 expense of speed.
19560 As a perfect example of what is @emph{not} to be done, here is how to
19561 find out whether a header file contains a particular declaration, such
19562 as a typedef, a structure, a structure member, or a function.  Use
19563 @code{AC_EGREP_HEADER} instead of running @code{grep} directly on the
19564 header file; on some systems the symbol might be defined in another
19565 header file that the file you are checking includes.
19567 As a (bad) example, here is how you should not check for C preprocessor
19568 symbols, either defined by header files or predefined by the C
19569 preprocessor: using @code{AC_EGREP_CPP}:
19571 @example
19572 @group
19573 AC_EGREP_CPP(yes,
19574 [#ifdef _AIX
19575   yes
19576 #endif
19577 ], is_aix=yes, is_aix=no)
19578 @end group
19579 @end example
19581 The above example, properly written would (i) use
19582 @code{AC_LANG_PROGRAM}, and (ii) run the compiler:
19584 @example
19585 @group
19586 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
19587 [[#ifndef _AIX
19588  error: This isn't AIX!
19589 #endif
19590 ]])],
19591                    [is_aix=yes],
19592                    [is_aix=no])
19593 @end group
19594 @end example
19597 @c ============================= Generating Test Suites with Autotest
19599 @node Using Autotest
19600 @chapter Generating Test Suites with Autotest
19602 @cindex Autotest
19604 @display
19605 @strong{N.B.: This section describes an experimental feature which will
19606 be part of Autoconf in a forthcoming release.  Although we believe
19607 Autotest is stabilizing, this documentation describes an interface which
19608 might change in the future: do not depend upon Autotest without
19609 subscribing to the Autoconf mailing lists.}
19610 @end display
19612 It is paradoxical that portable projects depend on nonportable tools
19613 to run their test suite.  Autoconf by itself is the paragon of this
19614 problem: although it aims at perfectly portability, up to 2.13 its
19615 test suite was using Deja@acronym{GNU}, a rich and complex testing
19616 framework, but which is far from being standard on Posix systems.
19617 Worse yet, it was likely to be missing on the most fragile platforms,
19618 the very platforms that are most likely to torture Autoconf and
19619 exhibit deficiencies.
19621 To circumvent this problem, many package maintainers have developed their
19622 own testing framework, based on simple shell scripts whose sole outputs
19623 are exit status values describing whether the test succeeded.  Most of
19624 these tests share common patterns, and this can result in lots of
19625 duplicated code and tedious maintenance.
19627 Following exactly the same reasoning that yielded to the inception of
19628 Autoconf, Autotest provides a test suite generation framework, based on
19629 M4 macros building a portable shell script.  The suite itself is
19630 equipped with automatic logging and tracing facilities which greatly
19631 diminish the interaction with bug reporters, and simple timing reports.
19633 Autoconf itself has been using Autotest for years, and we do attest that
19634 it has considerably improved the strength of the test suite and the
19635 quality of bug reports.  Other projects are known to use some generation
19636 of Autotest, such as Bison, Free Recode, Free Wdiff, @acronym{GNU} Tar, each of
19637 them with different needs, and this usage has validated Autotest as a general
19638 testing framework.
19640 Nonetheless, compared to Deja@acronym{GNU}, Autotest is inadequate for
19641 interactive tool testing, which is probably its main limitation.
19643 @menu
19644 * Using an Autotest Test Suite::  Autotest and the user
19645 * Writing Testsuites::          Autotest macros
19646 * testsuite Invocation::        Running @command{testsuite} scripts
19647 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
19648 @end menu
19650 @node Using an Autotest Test Suite
19651 @section Using an Autotest Test Suite
19653 @menu
19654 * testsuite Scripts::           The concepts of Autotest
19655 * Autotest Logs::               Their contents
19656 @end menu
19658 @node testsuite Scripts
19659 @subsection @command{testsuite} Scripts
19661 @cindex @command{testsuite}
19663 Generating testing or validation suites using Autotest is rather easy.
19664 The whole validation suite is held in a file to be processed through
19665 @command{autom4te}, itself using @acronym{GNU} M4 under the scene, to
19666 produce a stand-alone Bourne shell script which then gets distributed.
19667 Neither @command{autom4te} nor @acronym{GNU} M4 are needed at
19668 the installer's end.
19670 @cindex test group
19671 Each test of the validation suite should be part of some test group.  A
19672 @dfn{test group} is a sequence of interwoven tests that ought to be
19673 executed together, usually because one test in the group creates data
19674 files than a later test in the same group needs to read.  Complex test
19675 groups make later debugging more tedious.  It is much better to
19676 keep only a few tests per test group.  Ideally there is only one test
19677 per test group.
19679 For all but the simplest packages, some file such as @file{testsuite.at}
19680 does not fully hold all test sources, as these are often easier to
19681 maintain in separate files.  Each of these separate files holds a single
19682 test group, or a sequence of test groups all addressing some common
19683 functionality in the package.  In such cases, @file{testsuite.at}
19684 merely initializes the validation suite, and sometimes does elementary
19685 health checking, before listing include statements for all other test
19686 files.  The special file @file{package.m4}, containing the
19687 identification of the package, is automatically included if found.
19689 A convenient alternative consists in moving all the global issues
19690 (local Autotest macros, elementary health checking, and @code{AT_INIT}
19691 invocation) into the file @code{local.at}, and making
19692 @file{testsuite.at} be a simple list of @code{m4_include} of sub test
19693 suites.  In such case, generating the whole test suite or pieces of it
19694 is only a matter of choosing the @command{autom4te} command line
19695 arguments.
19697 The validation scripts that Autotest produces are by convention called
19698 @command{testsuite}.  When run, @command{testsuite} executes each test
19699 group in turn, producing only one summary line per test to say if that
19700 particular test succeeded or failed.  At end of all tests, summarizing
19701 counters get printed.  One debugging directory is left for each test
19702 group which failed, if any: such directories are named
19703 @file{testsuite.dir/@var{nn}}, where @var{nn} is the sequence number of
19704 the test group, and they include:
19706 @itemize @bullet
19707 @item a debugging script named @file{run} which reruns the test in
19708 @dfn{debug mode} (@pxref{testsuite Invocation}).  The automatic generation
19709 of debugging scripts has the purpose of easing the chase for bugs.
19711 @item all the files created with @code{AT_DATA}
19713 @item a log of the run, named @file{testsuite.log}
19714 @end itemize
19716 In the ideal situation, none of the tests fail, and consequently no
19717 debugging directory is left behind for validation.
19719 It often happens in practice that individual tests in the validation
19720 suite need to get information coming out of the configuration process.
19721 Some of this information, common for all validation suites, is provided
19722 through the file @file{atconfig}, automatically created by
19723 @code{AC_CONFIG_TESTDIR}.  For configuration informations which your
19724 testing environment specifically needs, you might prepare an optional
19725 file named @file{atlocal.in}, instantiated by @code{AC_CONFIG_FILES}.
19726 The configuration process produces @file{atconfig} and @file{atlocal}
19727 out of these two input files, and these two produced files are
19728 automatically read by the @file{testsuite} script.
19730 Here is a diagram showing the relationship between files.
19732 @noindent
19733 Files used in preparing a software package for distribution:
19735 @example
19736                 [package.m4] -->.
19737                                  \
19738 subfile-1.at ->.  [local.at] ---->+
19739     ...         \                  \
19740 subfile-i.at ---->-- testsuite.at -->-- autom4te* -->testsuite
19741     ...         /
19742 subfile-n.at ->'
19743 @end example
19745 @noindent
19746 Files used in configuring a software package:
19748 @example
19749                                      .--> atconfig
19750                                     /
19751 [atlocal.in] -->  config.status* --<
19752                                     \
19753                                      `--> [atlocal]
19754 @end example
19756 @noindent
19757 Files created during the test suite execution:
19759 @example
19760 atconfig -->.                    .--> testsuite.log
19761              \                  /
19762               >-- testsuite* --<
19763              /                  \
19764 [atlocal] ->'                    `--> [testsuite.dir]
19765 @end example
19768 @node Autotest Logs
19769 @subsection Autotest Logs
19771 When run, the test suite creates a log file named after itself, e.g., a
19772 test suite named @command{testsuite} creates @file{testsuite.log}.  It
19773 contains a lot of information, usually more than maintainers actually
19774 need, but therefore most of the time it contains all that is needed:
19776 @table @asis
19777 @item command line arguments
19778 @c akim s/to consist in/to consist of/
19779 A bad but unfortunately widespread habit consists of
19780 setting environment variables before the command, such as in
19781 @samp{CC=my-home-grown-cc ./testsuite}.  The test suite does not
19782 know this change, hence (i) it cannot report it to you, and (ii)
19783 it cannot preserve the value of @code{CC} for subsequent runs.
19784 Autoconf faced exactly the same problem, and solved it by asking
19785 users to pass the variable definitions as command line arguments.
19786 Autotest requires this rule, too, but has no means to enforce it; the log
19787 then contains a trace of the variables that were changed by the user.
19789 @item @file{ChangeLog} excerpts
19790 The topmost lines of all the @file{ChangeLog} files found in the source
19791 hierarchy.  This is especially useful when bugs are reported against
19792 development versions of the package, since the version string does not
19793 provide sufficient information to know the exact state of the sources
19794 the user compiled.  Of course, this relies on the use of a
19795 @file{ChangeLog}.
19797 @item build machine
19798 Running a test suite in a cross-compile environment is not an easy task,
19799 since it would mean having the test suite run on a machine @var{build},
19800 while running programs on a machine @var{host}.  It is much simpler to
19801 run both the test suite and the programs on @var{host}, but then, from
19802 the point of view of the test suite, there remains a single environment,
19803 @var{host} = @var{build}.  The log contains relevant information on the
19804 state of the build machine, including some important environment
19805 variables.
19806 @c FIXME: How about having an M4sh macro to say `hey, log the value
19807 @c of `@dots{}'?  This would help both Autoconf and Autotest.
19809 @item tested programs
19810 The absolute file name and answers to @option{--version} of the tested
19811 programs (see @ref{Writing Testsuites}, @code{AT_TESTED}).
19813 @item configuration log
19814 The contents of @file{config.log}, as created by @command{configure},
19815 are appended.  It contains the configuration flags and a detailed report
19816 on the configuration itself.
19817 @end table
19820 @node Writing Testsuites
19821 @section Writing @file{testsuite.at}
19823 The @file{testsuite.at} is a Bourne shell script making use of special
19824 Autotest M4 macros.  It often contains a call to @code{AT_INIT} near
19825 its beginning followed by one call to @code{m4_include} per source file
19826 for tests.  Each such included file, or the remainder of
19827 @file{testsuite.at} if include files are not used, contain a sequence of
19828 test groups.  Each test group begins with a call to @code{AT_SETUP},
19829 then an arbitrary number of shell commands or calls to @code{AT_CHECK},
19830 and then completes with a call to @code{AT_CLEANUP}.
19832 @defmac AT_INIT (@ovar{name})
19833 @atindex{INIT}
19834 @c FIXME: Not clear, plus duplication of the information.
19835 Initialize Autotest.  Giving a @var{name} to the test suite is
19836 encouraged if your package includes several test suites.  In any case,
19837 the test suite always displays the package name and version.  It also
19838 inherits the package bug report address.
19839 @end defmac
19841 @defmac AT_COPYRIGHT (@var{copyright-notice})
19842 @atindex{COPYRIGHT}
19843 @cindex Copyright Notice
19844 State that, in addition to the Free Software Foundation's copyright on
19845 the Autotest macros, parts of your test suite are covered by
19846 @var{copyright-notice}.
19848 The @var{copyright-notice} shows up in both the head of
19849 @command{testsuite} and in @samp{testsuite --version}.
19850 @end defmac
19852 @defmac AT_TESTED (@var{executables})
19853 @atindex{TESTED}
19854 Log the file name and answer to @option{--version} of each program in
19855 space-separated list @var{executables}.  Several invocations register
19856 new executables, in other words, don't fear registering one program
19857 several times.
19858 @end defmac
19860 Autotest test suites rely on @env{PATH} to find the tested program.
19861 This avoids the need to generate absolute names of the various tools, and
19862 makes it possible to test installed programs.  Therefore, knowing which
19863 programs are being exercised is crucial to understanding problems in
19864 the test suite itself, or its occasional misuses.  It is a good idea to
19865 also subscribe foreign programs you depend upon, to avoid incompatible
19866 diagnostics.
19868 @sp 1
19870 @defmac AT_SETUP (@var{test-group-name})
19871 @atindex{SETUP}
19872 This macro starts a group of related tests, all to be executed in the
19873 same subshell.  It accepts a single argument, which holds a few words
19874 (no more than about 30 or 40 characters) quickly describing the purpose
19875 of the test group being started.
19876 @end defmac
19878 @defmac AT_KEYWORDS (@var{keywords})
19879 @atindex{KEYWORDS}
19880 Associate the space-separated list of @var{keywords} to the enclosing
19881 test group.  This makes it possible to run ``slices'' of the test suite.
19882 For instance, if some of your test groups exercise some @samp{foo}
19883 feature, then using @samp{AT_KEYWORDS(foo)} lets you run
19884 @samp{./testsuite -k foo} to run exclusively these test groups.  The
19885 @var{title} of the test group is automatically recorded to
19886 @code{AT_KEYWORDS}.
19888 Several invocations within a test group accumulate new keywords.  In
19889 other words, don't fear registering the same keyword several times in a
19890 test group.
19891 @end defmac
19893 @defmac AT_CAPTURE_FILE (@var{file})
19894 @atindex{CAPTURE_FILE}
19895 If the current test group fails, log the contents of @var{file}.
19896 Several identical calls within one test group have no additional effect.
19897 @end defmac
19899 @defmac AT_XFAIL_IF (@var{shell-condition})
19900 @atindex{XFAIL_IF}
19901 Determine whether the test is expected to fail because it is a known
19902 bug (for unsupported features, you should skip the test).
19903 @var{shell-condition} is a shell expression such as a @code{test}
19904 command; you can instantiate this macro many times from within the
19905 same test group, and one of the conditions is enough to turn
19906 the test into an expected failure.
19907 @end defmac
19909 @defmac AT_CLEANUP
19910 @atindex{CLEANUP}
19911 End the current test group.
19912 @end defmac
19914 @sp 1
19916 @defmac AT_DATA (@var{file}, @var{contents})
19917 @atindex{DATA}
19918 Initialize an input data @var{file} with given @var{contents}.  Of
19919 course, the @var{contents} have to be properly quoted between square
19920 brackets to protect against included commas or spurious M4
19921 expansion.  The contents ought to end with an end of line.
19922 @end defmac
19924 @defmac AT_CHECK (@var{commands}, @dvar{status, 0}, @dvar{stdout, }, @
19925   @dvar{stderr, }, @ovar{run-if-fail}, @ovar{run-if-pass})
19926 @atindex{CHECK}
19927 Execute a test by performing given shell @var{commands}.  These commands
19928 should normally exit with @var{status}, while producing expected
19929 @var{stdout} and @var{stderr} contents.  If @var{commands} exit with
19930 status 77, then the whole test group is skipped.  Otherwise, if this test
19931 fails, run shell commands @var{run-if-fail} or, if this test passes, run shell
19932 commands @var{run-if-pass}.
19934 @c Previously, we had this:
19935 @c  The @var{commands} @emph{must not} redirect the standard output, nor the
19936 @c  standard error.
19937 @c to prevent trigerring the double redirect bug on Ultrix, see
19938 @c `File Descriptors'.  This was too restricting, and Ultrix is pretty
19939 @c much dead, so we dropped the limitation; the obvious workaround on
19940 @c Ultrix is to use a working shell there.
19942 If @var{status}, or @var{stdout}, or @var{stderr} is @samp{ignore}, then
19943 the corresponding value is not checked.
19945 The special value @samp{expout} for @var{stdout} means the expected
19946 output of the @var{commands} is the content of the file @file{expout}.
19947 If @var{stdout} is @samp{stdout}, then the standard output of the
19948 @var{commands} is available for further tests in the file @file{stdout}.
19949 Similarly for @var{stderr} with @samp{experr} and @samp{stderr}.
19950 @end defmac
19953 @node testsuite Invocation
19954 @section Running @command{testsuite} Scripts
19955 @cindex @command{testsuite}
19957 Autotest test suites support the following arguments:
19959 @table @option
19960 @item --help
19961 @itemx -h
19962 Display the list of options and exit successfully.
19964 @item --version
19965 @itemx -V
19966 Display the version of the test suite and exit successfully.
19968 @item --clean
19969 @itemx -c
19970 Remove all the files the test suite might have created and exit.  Meant
19971 for @code{clean} Make targets.
19973 @item --list
19974 @itemx -l
19975 List all the tests (or only the selection), including their possible
19976 keywords.
19977 @end table
19979 @sp 1
19981 By default all tests are performed (or described with
19982 @option{--list}) in the default environment first silently, then
19983 verbosely, but the environment, set of tests, and verbosity level can be
19984 tuned:
19986 @table @samp
19987 @item @var{variable}=@var{value}
19988 Set the environment @var{variable} to @var{value}.  Use this rather
19989 than @samp{FOO=foo ./testsuite} as debugging scripts would then run in a
19990 different environment.
19992 @cindex @code{AUTOTEST_PATH}
19993 The variable @code{AUTOTEST_PATH} specifies the testing path to prepend
19994 to @env{PATH}.  Relative directory names (not starting with
19995 @samp{/}) are considered to be relative to the top level of the
19996 package being built.  All directories are made absolute, first
19997 starting from the top level @emph{build} tree, then from the
19998 @emph{source} tree.  For instance @samp{./testsuite
19999 AUTOTEST_PATH=tests:bin} for a @file{/src/foo-1.0} source package built
20000 in @file{/tmp/foo} results in @samp{/tmp/foo/tests:/tmp/foo/bin} and
20001 then @samp{/src/foo-1.0/tests:/src/foo-1.0/bin} being prepended to
20002 @env{PATH}.
20004 @item @var{number}
20005 @itemx @var{number}-@var{number}
20006 @itemx @var{number}-
20007 @itemx -@var{number}
20008 Add the corresponding test groups, with obvious semantics, to the
20009 selection.
20011 @item --keywords=@var{keywords}
20012 @itemx -k @var{keywords}
20013 Add to the selection the test groups with title or keywords (arguments
20014 to @code{AT_SETUP} or @code{AT_KEYWORDS}) that match @emph{all} keywords
20015 of the comma separated list @var{keywords}, case-insensitively.  Use
20016 @samp{!} immediately before the keyword to invert the selection for this
20017 keyword.  By default, the keywords match whole words; enclose them in
20018 @samp{.*} to also match parts of words.
20020 For example, running
20022 @example
20023 @kbd{./testsuite -k 'autoupdate,.*FUNC.*'}
20024 @end example
20026 @noindent
20027 selects all tests tagged @samp{autoupdate} @emph{and} with tags
20028 containing @samp{FUNC} (as in @samp{AC_CHECK_FUNC}, @samp{AC_FUNC_ALLOCA},
20029 etc.), while
20031 @example
20032 @kbd{./testsuite -k '!autoupdate' -k '.*FUNC.*'}
20033 @end example
20035 @noindent
20036 selects all tests not tagged @samp{autoupdate} @emph{or} with tags
20037 containing @samp{FUNC}.
20039 @item --errexit
20040 @itemx -e
20041 If any test fails, immediately abort testing.  It implies
20042 @option{--debug}: post test group clean up, and top-level logging
20043 are inhibited.  This option is meant for the full test
20044 suite, it is not really useful for generated debugging scripts.
20046 @item --verbose
20047 @itemx -v
20048 Force more verbosity in the detailed output of what is being done.  This
20049 is the default for debugging scripts.
20051 @item --debug
20052 @itemx -d
20053 Do not remove the files after a test group was performed ---but they are
20054 still removed @emph{before}, therefore using this option is sane when
20055 running several test groups.  Create debugging scripts.  Do not
20056 overwrite the top-level
20057 log (in order to preserve supposedly existing full log file).  This is
20058 the default for debugging scripts, but it can also be useful to debug
20059 the testsuite itself.
20061 @item --trace
20062 @itemx -x
20063 Trigger shell tracing of the test groups.
20064 @end table
20067 @node Making testsuite Scripts
20068 @section Making @command{testsuite} Scripts
20070 For putting Autotest into movement, you need some configuration and
20071 makefile machinery.  We recommend, at least if your package uses deep or
20072 shallow hierarchies, that you use @file{tests/} as the name of the
20073 directory holding all your tests and their makefile.  Here is a
20074 check list of things to do.
20076 @itemize @minus
20078 @item
20079 @cindex @file{package.m4}
20080 Make sure to create the file @file{package.m4}, which defines the
20081 identity of the package.  It must define @code{AT_PACKAGE_STRING}, the
20082 full signature of the package, and @code{AT_PACKAGE_BUGREPORT}, the
20083 address to which bug reports should be sent.  For sake of completeness,
20084 we suggest that you also define @code{AT_PACKAGE_NAME},
20085 @code{AT_PACKAGE_TARNAME}, and @code{AT_PACKAGE_VERSION}.
20086 @xref{Initializing configure}, for a description of these variables.  We
20087 suggest the following makefile excerpt:
20089 @smallexample
20090 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
20091         @{                                      \
20092           echo '# Signature of the current package.'; \
20093           echo 'm4_define([AT_PACKAGE_NAME],      [@@PACKAGE_NAME@@])'; \
20094           echo 'm4_define([AT_PACKAGE_TARNAME],   [@@PACKAGE_TARNAME@@])'; \
20095           echo 'm4_define([AT_PACKAGE_VERSION],   [@@PACKAGE_VERSION@@])'; \
20096           echo 'm4_define([AT_PACKAGE_STRING],    [@@PACKAGE_STRING@@])'; \
20097           echo 'm4_define([AT_PACKAGE_BUGREPORT], [@@PACKAGE_BUGREPORT@@])'; \
20098         @} >'$(srcdir)/package.m4'
20099 @end smallexample
20101 @noindent
20102 Be sure to distribute @file{package.m4} and to put it into the source
20103 hierarchy: the test suite ought to be shipped!
20105 @item
20106 Invoke @code{AC_CONFIG_TESTDIR}.
20108 @defmac AC_CONFIG_TESTDIR (@var{directory}, @dvar{test-path, directory})
20109 @acindex{CONFIG_TESTDIR}
20110 An Autotest test suite is to be configured in @var{directory}.  This
20111 macro requires the instantiation of @file{@var{directory}/atconfig} from
20112 @file{@var{directory}/atconfig.in}, and sets the default
20113 @code{AUTOTEST_PATH} to @var{test-path} (@pxref{testsuite Invocation}).
20114 @end defmac
20116 @item
20117 Still within @file{configure.ac}, as appropriate, ensure that some
20118 @code{AC_CONFIG_FILES} command includes substitution for
20119 @file{tests/atlocal}.
20121 @item
20122 The @file{tests/Makefile.in} should be modified so the validation in
20123 your package is triggered by @samp{make check}.  An example is provided
20124 below.
20125 @end itemize
20127 With Automake, here is a minimal example about how to link @samp{make
20128 check} with a validation suite.
20130 @example
20131 EXTRA_DIST = testsuite.at $(TESTSUITE) atlocal.in
20132 TESTSUITE = $(srcdir)/testsuite
20134 check-local: atconfig atlocal $(TESTSUITE)
20135         $(SHELL) '$(TESTSUITE)' $(TESTSUITEFLAGS)
20137 installcheck-local: atconfig atlocal $(TESTSUITE)
20138         $(SHELL) '$(TESTSUITE)' AUTOTEST_PATH='$(bindir)' \
20139           $(TESTSUITEFLAGS)
20141 clean-local:
20142         test ! -f '$(TESTSUITE)' || \
20143          $(SHELL) '$(TESTSUITE)' --clean
20145 AUTOTEST = $(AUTOM4TE) --language=autotest
20146 $(TESTSUITE): $(srcdir)/testsuite.at
20147         $(AUTOTEST) -I '$(srcdir)' -o $@@.tmp $@@.at
20148         mv $@@.tmp $@@
20149 @end example
20151 You might want to list explicitly the dependencies, i.e., the list of
20152 the files @file{testsuite.at} includes.
20154 With strict Autoconf, you might need to add lines inspired from the
20155 following:
20157 @example
20158 subdir = tests
20160 atconfig: $(top_builddir)/config.status
20161         cd $(top_builddir) && \
20162            $(SHELL) ./config.status $(subdir)/$@@
20164 atlocal: $(srcdir)/atlocal.in $(top_builddir)/config.status
20165         cd $(top_builddir) && \
20166            $(SHELL) ./config.status $(subdir)/$@@
20167 @end example
20169 @noindent
20170 and manage to have @file{atconfig.in} and @code{$(EXTRA_DIST)}
20171 distributed.
20173 With all this in place, and if you have not initialized @samp{TESTSUITEFLAGS}
20174 within your makefile, you can fine-tune test suite execution with this
20175 variable, for example:
20177 @example
20178 make check TESTSUITEFLAGS='-v -d -x 75 -k AC_PROG_CC CFLAGS=-g'
20179 @end example
20183 @c =============================== Frequent Autoconf Questions, with answers
20185 @node FAQ
20186 @chapter Frequent Autoconf Questions, with answers
20188 Several questions about Autoconf come up occasionally.  Here some of them
20189 are addressed.
20191 @menu
20192 * Distributing::                Distributing @command{configure} scripts
20193 * Why GNU M4::                  Why not use the standard M4?
20194 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
20195 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
20196 * Defining Directories::        Passing @code{datadir} to program
20197 * Autom4te Cache::              What is it?  Can I remove it?
20198 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
20199 @end menu
20201 @node Distributing
20202 @section Distributing @command{configure} Scripts
20203 @cindex License
20205 @display
20206 What are the restrictions on distributing @command{configure}
20207 scripts that Autoconf generates?  How does that affect my
20208 programs that use them?
20209 @end display
20211 There are no restrictions on how the configuration scripts that Autoconf
20212 produces may be distributed or used.  In Autoconf version 1, they were
20213 covered by the @acronym{GNU} General Public License.  We still encourage
20214 software authors to distribute their work under terms like those of the
20215 @acronym{GPL}, but doing so is not required to use Autoconf.
20217 Of the other files that might be used with @command{configure},
20218 @file{config.h.in} is under whatever copyright you use for your
20219 @file{configure.ac}.  @file{config.sub} and @file{config.guess} have an
20220 exception to the @acronym{GPL} when they are used with an Autoconf-generated
20221 @command{configure} script, which permits you to distribute them under the
20222 same terms as the rest of your package.  @file{install-sh} is from the X
20223 Consortium and is not copyrighted.
20225 @node Why GNU M4
20226 @section Why Require @acronym{GNU} M4?
20228 @display
20229 Why does Autoconf require @acronym{GNU} M4?
20230 @end display
20232 Many M4 implementations have hard-coded limitations on the size and
20233 number of macros that Autoconf exceeds.  They also lack several
20234 builtin macros that it would be difficult to get along without in a
20235 sophisticated application like Autoconf, including:
20237 @example
20238 m4_builtin
20239 m4_indir
20240 m4_bpatsubst
20241 __file__
20242 __line__
20243 @end example
20245 Autoconf requires version 1.4.5 or later of @acronym{GNU} M4.
20247 Since only software maintainers need to use Autoconf, and since @acronym{GNU}
20248 M4 is simple to configure and install, it seems reasonable to require
20249 @acronym{GNU} M4 to be installed also.  Many maintainers of @acronym{GNU} and
20250 other free software already have most of the @acronym{GNU} utilities
20251 installed, since they prefer them.
20253 @node Bootstrapping
20254 @section How Can I Bootstrap?
20255 @cindex Bootstrap
20257 @display
20258 If Autoconf requires @acronym{GNU} M4 and @acronym{GNU} M4 has an Autoconf
20259 @command{configure} script, how do I bootstrap?  It seems like a chicken
20260 and egg problem!
20261 @end display
20263 This is a misunderstanding.  Although @acronym{GNU} M4 does come with a
20264 @command{configure} script produced by Autoconf, Autoconf is not required
20265 in order to run the script and install @acronym{GNU} M4.  Autoconf is only
20266 required if you want to change the M4 @command{configure} script, which few
20267 people have to do (mainly its maintainer).
20269 @node Why Not Imake
20270 @section Why Not Imake?
20271 @cindex Imake
20273 @display
20274 Why not use Imake instead of @command{configure} scripts?
20275 @end display
20277 Several people have written addressing this question, so I include
20278 adaptations of their explanations here.
20280 The following answer is based on one written by Richard Pixley:
20282 @quotation
20283 Autoconf generated scripts frequently work on machines that it has
20284 never been set up to handle before.  That is, it does a good job of
20285 inferring a configuration for a new system.  Imake cannot do this.
20287 Imake uses a common database of host specific data.  For X11, this makes
20288 sense because the distribution is made as a collection of tools, by one
20289 central authority who has control over the database.
20291 @acronym{GNU} tools are not released this way.  Each @acronym{GNU} tool has a
20292 maintainer; these maintainers are scattered across the world.  Using a
20293 common database would be a maintenance nightmare.  Autoconf may appear
20294 to be this kind of database, but in fact it is not.  Instead of listing
20295 host dependencies, it lists program requirements.
20297 If you view the @acronym{GNU} suite as a collection of native tools, then the
20298 problems are similar.  But the @acronym{GNU} development tools can be
20299 configured as cross tools in almost any host+target permutation.  All of
20300 these configurations can be installed concurrently.  They can even be
20301 configured to share host independent files across hosts.  Imake doesn't
20302 address these issues.
20304 Imake templates are a form of standardization.  The @acronym{GNU} coding
20305 standards address the same issues without necessarily imposing the same
20306 restrictions.
20307 @end quotation
20310 Here is some further explanation, written by Per Bothner:
20312 @quotation
20313 One of the advantages of Imake is that it easy to generate large
20314 makefiles using the @samp{#include} and macro mechanisms of @command{cpp}.
20315 However, @code{cpp} is not programmable: it has limited conditional
20316 facilities, and no looping.  And @code{cpp} cannot inspect its
20317 environment.
20319 All of these problems are solved by using @code{sh} instead of
20320 @code{cpp}.  The shell is fully programmable, has macro substitution,
20321 can execute (or source) other shell scripts, and can inspect its
20322 environment.
20323 @end quotation
20326 Paul Eggert elaborates more:
20328 @quotation
20329 With Autoconf, installers need not assume that Imake itself is already
20330 installed and working well.  This may not seem like much of an advantage
20331 to people who are accustomed to Imake.  But on many hosts Imake is not
20332 installed or the default installation is not working well, and requiring
20333 Imake to install a package hinders the acceptance of that package on
20334 those hosts.  For example, the Imake template and configuration files
20335 might not be installed properly on a host, or the Imake build procedure
20336 might wrongly assume that all source files are in one big directory
20337 tree, or the Imake configuration might assume one compiler whereas the
20338 package or the installer needs to use another, or there might be a
20339 version mismatch between the Imake expected by the package and the Imake
20340 supported by the host.  These problems are much rarer with Autoconf,
20341 where each package comes with its own independent configuration
20342 processor.
20344 Also, Imake often suffers from unexpected interactions between
20345 @command{make} and the installer's C preprocessor.  The fundamental problem
20346 here is that the C preprocessor was designed to preprocess C programs,
20347 not makefiles.  This is much less of a problem with Autoconf,
20348 which uses the general-purpose preprocessor M4, and where the
20349 package's author (rather than the installer) does the preprocessing in a
20350 standard way.
20351 @end quotation
20354 Finally, Mark Eichin notes:
20356 @quotation
20357 Imake isn't all that extensible, either.  In order to add new features to
20358 Imake, you need to provide your own project template, and duplicate most
20359 of the features of the existing one.  This means that for a sophisticated
20360 project, using the vendor-provided Imake templates fails to provide any
20361 leverage---since they don't cover anything that your own project needs
20362 (unless it is an X11 program).
20364 On the other side, though:
20366 The one advantage that Imake has over @command{configure}:
20367 @file{Imakefile} files tend to be much shorter (likewise, less redundant)
20368 than @file{Makefile.in} files.  There is a fix to this, however---at least
20369 for the Kerberos V5 tree, we've modified things to call in common
20370 @file{post.in} and @file{pre.in} makefile fragments for the
20371 entire tree.  This means that a lot of common things don't have to be
20372 duplicated, even though they normally are in @command{configure} setups.
20373 @end quotation
20376 @node Defining Directories
20377 @section How Do I @code{#define} Installation Directories?
20379 @display
20380 My program needs library files, installed in @code{datadir} and
20381 similar.  If I use
20383 @example
20384 AC_DEFINE_UNQUOTED([DATADIR], [$datadir],
20385   [Define to the read-only architecture-independent
20386    data directory.])
20387 @end example
20389 @noindent
20390 I get
20392 @example
20393 #define DATADIR "$@{prefix@}/share"
20394 @end example
20395 @end display
20397 As already explained, this behavior is on purpose, mandated by the
20398 @acronym{GNU} Coding Standards, see @ref{Installation Directory
20399 Variables}.  There are several means to achieve a similar goal:
20401 @itemize @minus
20402 @item
20403 Do not use @code{AC_DEFINE} but use your makefile to pass the
20404 actual value of @code{datadir} via compilation flags.
20405 @xref{Installation Directory Variables}, for the details.
20407 @item
20408 This solution can be simplified when compiling a program: you may either
20409 extend the @code{CPPFLAGS}:
20411 @example
20412 CPPFLAGS = -DDATADIR='"$(datadir)"' @@CPPFLAGS@@
20413 @end example
20415 @noindent
20416 If you are using Automake, you should use @code{AM_CPPFLAGS} instead:
20418 @example
20419 AM_CPPFLAGS = -DDATADIR='"$(datadir)"'
20420 @end example
20422 @noindent
20423 Alternatively, create a dedicated header file:
20425 @example
20426 DISTCLEANFILES = myprog-paths.h
20427 myprog-paths.h: Makefile
20428         echo '#define DATADIR "$(datadir)"' >$@@
20429 @end example
20431 @item
20432 Use @code{AC_DEFINE} but have @command{configure} compute the literal
20433 value of @code{datadir} and others.  Many people have wrapped macros to
20434 automate this task.  For instance, the macro @code{AC_DEFINE_DIR} from
20435 the @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
20436 Archive}.
20438 This solution does not conform to the @acronym{GNU} Coding Standards.
20440 @item
20441 Note that all the previous solutions hard wire the absolute name of
20442 these directories in the executables, which is not a good property.  You
20443 may try to compute the names relative to @code{prefix}, and try to
20444 find @code{prefix} at runtime, this way your package is relocatable.
20445 @end itemize
20448 @node Autom4te Cache
20449 @section What is @file{autom4te.cache}?
20451 @display
20452 What is this directory @file{autom4te.cache}?  Can I safely remove it?
20453 @end display
20455 In the @acronym{GNU} Build System, @file{configure.ac} plays a central
20456 role and is read by many tools: @command{autoconf} to create
20457 @file{configure}, @command{autoheader} to create @file{config.h.in},
20458 @command{automake} to create @file{Makefile.in}, @command{autoscan} to
20459 check the completeness of @file{configure.ac}, @command{autoreconf} to
20460 check the @acronym{GNU} Build System components that are used.  To
20461 ``read @file{configure.ac}'' actually means to compile it with M4,
20462 which can be a long process for complex @file{configure.ac}.
20464 This is why all these tools, instead of running directly M4, invoke
20465 @command{autom4te} (@pxref{autom4te Invocation}) which, while answering to
20466 a specific demand, stores additional information in
20467 @file{autom4te.cache} for future runs.  For instance, if you run
20468 @command{autoconf}, behind the scenes, @command{autom4te} also
20469 stores information for the other tools, so that when you invoke
20470 @command{autoheader} or @command{automake} etc., reprocessing
20471 @file{configure.ac} is not needed.  The speed up is frequently of 30%,
20472 and is increasing with the size of @file{configure.ac}.
20474 But it is and remains being simply a cache: you can safely remove it.
20476 @sp 1
20478 @display
20479 Can I permanently get rid of it?
20480 @end display
20482 The creation of this cache can be disabled from
20483 @file{~/.autom4te.cfg}, see @ref{Customizing autom4te}, for more
20484 details.  You should be aware that disabling the cache slows down the
20485 Autoconf test suite by 40%.  The more @acronym{GNU} Build System
20486 components are used, the more the cache is useful; for instance
20487 running @samp{autoreconf -f} on the Core Utilities is twice slower without
20488 the cache @emph{although @option{--force} implies that the cache is
20489 not fully exploited}, and eight times slower than without
20490 @option{--force}.
20493 @node Present But Cannot Be Compiled
20494 @section Header Present But Cannot Be Compiled
20496 The most important guideline to bear in mind when checking for
20497 features is to mimic as much as possible the intended use.
20498 Unfortunately, old versions of @code{AC_CHECK_HEADER} and
20499 @code{AC_CHECK_HEADERS} failed to follow this idea, and called
20500 the preprocessor, instead of the compiler, to check for headers.  As a
20501 result, incompatibilities between headers went unnoticed during
20502 configuration, and maintainers finally had to deal with this issue
20503 elsewhere.
20505 As of Autoconf 2.56 both checks are performed, and @code{configure}
20506 complains loudly if the compiler and the preprocessor do not agree.
20507 For the time being the result used is that of the preprocessor, to give
20508 maintainers time to adjust their @file{configure.ac}, but in the
20509 future, only the compiler will be considered.
20511 Consider the following example:
20513 @smallexample
20514 $ @kbd{cat number.h}
20515 typedef int number;
20516 $ @kbd{cat pi.h}
20517 const number pi = 3;
20518 $ @kbd{cat configure.ac}
20519 AC_INIT([Example], [1.0], [bug-example@@example.org])
20520 AC_CHECK_HEADERS([pi.h])
20521 $ @kbd{autoconf -Wall}
20522 $ @kbd{./configure}
20523 checking for gcc... gcc
20524 checking for C compiler default output file name... a.out
20525 checking whether the C compiler works... yes
20526 checking whether we are cross compiling... no
20527 checking for suffix of executables...
20528 checking for suffix of object files... o
20529 checking whether we are using the GNU C compiler... yes
20530 checking whether gcc accepts -g... yes
20531 checking for gcc option to accept ISO C89... none needed
20532 checking how to run the C preprocessor... gcc -E
20533 checking for grep that handles long lines and -e... grep
20534 checking for egrep... grep -E
20535 checking for ANSI C header files... yes
20536 checking for sys/types.h... yes
20537 checking for sys/stat.h... yes
20538 checking for stdlib.h... yes
20539 checking for string.h... yes
20540 checking for memory.h... yes
20541 checking for strings.h... yes
20542 checking for inttypes.h... yes
20543 checking for stdint.h... yes
20544 checking for unistd.h... yes
20545 checking pi.h usability... no
20546 checking pi.h presence... yes
20547 configure: WARNING: pi.h: present but cannot be compiled
20548 configure: WARNING: pi.h:     check for missing prerequisite headers?
20549 configure: WARNING: pi.h: see the Autoconf documentation
20550 configure: WARNING: pi.h:     section "Present But Cannot Be Compiled"
20551 configure: WARNING: pi.h: proceeding with the preprocessor's result
20552 configure: WARNING: pi.h: in the future, the compiler will take precedence
20553 configure: WARNING:     ## -------------------------------------- ##
20554 configure: WARNING:     ## Report this to bug-example@@example.org ##
20555 configure: WARNING:     ## -------------------------------------- ##
20556 checking for pi.h... yes
20557 @end smallexample
20559 @noindent
20560 The proper way the handle this case is using the fourth argument
20561 (@pxref{Generic Headers}):
20563 @example
20564 $ @kbd{cat configure.ac}
20565 AC_INIT([Example], [1.0], [bug-example@@example.org])
20566 AC_CHECK_HEADERS([number.h pi.h], [], [],
20567 [[#ifdef HAVE_NUMBER_H
20568 # include <number.h>
20569 #endif
20571 $ @kbd{autoconf -Wall}
20572 $ @kbd{./configure}
20573 checking for gcc... gcc
20574 checking for C compiler default output... a.out
20575 checking whether the C compiler works... yes
20576 checking whether we are cross compiling... no
20577 checking for suffix of executables...
20578 checking for suffix of object files... o
20579 checking whether we are using the GNU C compiler... yes
20580 checking whether gcc accepts -g... yes
20581 checking for gcc option to accept ANSI C... none needed
20582 checking for number.h... yes
20583 checking for pi.h... yes
20584 @end example
20586 See @ref{Particular Headers}, for a list of headers with their
20587 prerequisite.
20589 @c ===================================================== History of Autoconf.
20591 @node History
20592 @chapter History of Autoconf
20593 @cindex History of autoconf
20595 You may be wondering, Why was Autoconf originally written?  How did it
20596 get into its present form?  (Why does it look like gorilla spit?)  If
20597 you're not wondering, then this chapter contains no information useful
20598 to you, and you might as well skip it.  If you @emph{are} wondering,
20599 then let there be light@enddots{}
20601 @menu
20602 * Genesis::                     Prehistory and naming of @command{configure}
20603 * Exodus::                      The plagues of M4 and Perl
20604 * Leviticus::                   The priestly code of portability arrives
20605 * Numbers::                     Growth and contributors
20606 * Deuteronomy::                 Approaching the promises of easy configuration
20607 @end menu
20609 @node Genesis
20610 @section Genesis
20612 In June 1991 I was maintaining many of the @acronym{GNU} utilities for the
20613 Free Software Foundation.  As they were ported to more platforms and
20614 more programs were added, the number of @option{-D} options that users
20615 had to select in the makefile (around 20) became burdensome.
20616 Especially for me---I had to test each new release on a bunch of
20617 different systems.  So I wrote a little shell script to guess some of
20618 the correct settings for the fileutils package, and released it as part
20619 of fileutils 2.0.  That @command{configure} script worked well enough that
20620 the next month I adapted it (by hand) to create similar @command{configure}
20621 scripts for several other @acronym{GNU} utilities packages.  Brian Berliner
20622 also adapted one of my scripts for his @acronym{CVS} revision control system.
20624 Later that summer, I learned that Richard Stallman and Richard Pixley
20625 were developing similar scripts to use in the @acronym{GNU} compiler tools;
20626 so I adapted my @command{configure} scripts to support their evolving
20627 interface: using the file name @file{Makefile.in} as the templates;
20628 adding @samp{+srcdir}, the first option (of many); and creating
20629 @file{config.status} files.
20631 @node Exodus
20632 @section Exodus
20634 As I got feedback from users, I incorporated many improvements, using
20635 Emacs to search and replace, cut and paste, similar changes in each of
20636 the scripts.  As I adapted more @acronym{GNU} utilities packages to use
20637 @command{configure} scripts, updating them all by hand became impractical.
20638 Rich Murphey, the maintainer of the @acronym{GNU} graphics utilities, sent me
20639 mail saying that the @command{configure} scripts were great, and asking if
20640 I had a tool for generating them that I could send him.  No, I thought,
20641 but I should!  So I started to work out how to generate them.  And the
20642 journey from the slavery of hand-written @command{configure} scripts to the
20643 abundance and ease of Autoconf began.
20645 Cygnus @command{configure}, which was being developed at around that time,
20646 is table driven; it is meant to deal mainly with a discrete number of
20647 system types with a small number of mainly unguessable features (such as
20648 details of the object file format).  The automatic configuration system
20649 that Brian Fox had developed for Bash takes a similar approach.  For
20650 general use, it seems to me a hopeless cause to try to maintain an
20651 up-to-date database of which features each variant of each operating
20652 system has.  It's easier and more reliable to check for most features on
20653 the fly---especially on hybrid systems that people have hacked on
20654 locally or that have patches from vendors installed.
20656 I considered using an architecture similar to that of Cygnus
20657 @command{configure}, where there is a single @command{configure} script that
20658 reads pieces of @file{configure.in} when run.  But I didn't want to have
20659 to distribute all of the feature tests with every package, so I settled
20660 on having a different @command{configure} made from each
20661 @file{configure.in} by a preprocessor.  That approach also offered more
20662 control and flexibility.
20664 I looked briefly into using the Metaconfig package, by Larry Wall,
20665 Harlan Stenn, and Raphael Manfredi, but I decided not to for several
20666 reasons.  The @command{Configure} scripts it produces are interactive,
20667 which I find quite inconvenient; I didn't like the ways it checked for
20668 some features (such as library functions); I didn't know that it was
20669 still being maintained, and the @command{Configure} scripts I had
20670 seen didn't work on many modern systems (such as System V R4 and NeXT);
20671 it wasn't flexible in what it could do in response to a feature's
20672 presence or absence; I found it confusing to learn; and it was too big
20673 and complex for my needs (I didn't realize then how much Autoconf would
20674 eventually have to grow).
20676 I considered using Perl to generate my style of @command{configure}
20677 scripts, but decided that M4 was better suited to the job of simple
20678 textual substitutions: it gets in the way less, because output is
20679 implicit.  Plus, everyone already has it.  (Initially I didn't rely on
20680 the @acronym{GNU} extensions to M4.)  Also, some of my friends at the
20681 University of Maryland had recently been putting M4 front ends on
20682 several programs, including @code{tvtwm}, and I was interested in trying
20683 out a new language.
20685 @node Leviticus
20686 @section Leviticus
20688 Since my @command{configure} scripts determine the system's capabilities
20689 automatically, with no interactive user intervention, I decided to call
20690 the program that generates them Autoconfig.  But with a version number
20691 tacked on, that name would be too long for old Unix file systems,
20692 so I shortened it to Autoconf.
20694 In the fall of 1991 I called together a group of fellow questers after
20695 the Holy Grail of portability (er, that is, alpha testers) to give me
20696 feedback as I encapsulated pieces of my handwritten scripts in M4 macros
20697 and continued to add features and improve the techniques used in the
20698 checks.  Prominent among the testers were Fran@,{c}ois Pinard, who came up
20699 with the idea of making an Autoconf shell script to run M4
20700 and check for unresolved macro calls; Richard Pixley, who suggested
20701 running the compiler instead of searching the file system to find
20702 include files and symbols, for more accurate results; Karl Berry, who
20703 got Autoconf to configure @TeX{} and added the macro index to the
20704 documentation; and Ian Lance Taylor, who added support for creating a C
20705 header file as an alternative to putting @option{-D} options in a
20706 makefile, so he could use Autoconf for his @acronym{UUCP} package.
20707 The alpha testers cheerfully adjusted their files again and again as the
20708 names and calling conventions of the Autoconf macros changed from
20709 release to release.  They all contributed many specific checks, great
20710 ideas, and bug fixes.
20712 @node Numbers
20713 @section Numbers
20715 In July 1992, after months of alpha testing, I released Autoconf 1.0,
20716 and converted many @acronym{GNU} packages to use it.  I was surprised by how
20717 positive the reaction to it was.  More people started using it than I
20718 could keep track of, including people working on software that wasn't
20719 part of the @acronym{GNU} Project (such as TCL, FSP, and Kerberos V5).
20720 Autoconf continued to improve rapidly, as many people using the
20721 @command{configure} scripts reported problems they encountered.
20723 Autoconf turned out to be a good torture test for M4 implementations.
20724 Unix M4 started to dump core because of the length of the
20725 macros that Autoconf defined, and several bugs showed up in @acronym{GNU}
20726 M4 as well.  Eventually, we realized that we needed to use some
20727 features that only @acronym{GNU} M4 has.  4.3@acronym{BSD} M4, in
20728 particular, has an impoverished set of builtin macros; the System V
20729 version is better, but still doesn't provide everything we need.
20731 More development occurred as people put Autoconf under more stresses
20732 (and to uses I hadn't anticipated).  Karl Berry added checks for X11.
20733 david zuhn contributed C++ support.  Fran@,{c}ois Pinard made it diagnose
20734 invalid arguments.  Jim Blandy bravely coerced it into configuring
20735 @acronym{GNU} Emacs, laying the groundwork for several later improvements.
20736 Roland McGrath got it to configure the @acronym{GNU} C Library, wrote the
20737 @command{autoheader} script to automate the creation of C header file
20738 templates, and added a @option{--verbose} option to @command{configure}.
20739 Noah Friedman added the @option{--autoconf-dir} option and
20740 @code{AC_MACRODIR} environment variable.  (He also coined the term
20741 @dfn{autoconfiscate} to mean ``adapt a software package to use
20742 Autoconf''.)  Roland and Noah improved the quoting protection in
20743 @code{AC_DEFINE} and fixed many bugs, especially when I got sick of
20744 dealing with portability problems from February through June, 1993.
20746 @node Deuteronomy
20747 @section Deuteronomy
20749 A long wish list for major features had accumulated, and the effect of
20750 several years of patching by various people had left some residual
20751 cruft.  In April 1994, while working for Cygnus Support, I began a major
20752 revision of Autoconf.  I added most of the features of the Cygnus
20753 @command{configure} that Autoconf had lacked, largely by adapting the
20754 relevant parts of Cygnus @command{configure} with the help of david zuhn
20755 and Ken Raeburn.  These features include support for using
20756 @file{config.sub}, @file{config.guess}, @option{--host}, and
20757 @option{--target}; making links to files; and running @command{configure}
20758 scripts in subdirectories.  Adding these features enabled Ken to convert
20759 @acronym{GNU} @code{as}, and Rob Savoye to convert Deja@acronym{GNU}, to using
20760 Autoconf.
20762 I added more features in response to other peoples' requests.  Many
20763 people had asked for @command{configure} scripts to share the results of
20764 the checks between runs, because (particularly when configuring a large
20765 source tree, like Cygnus does) they were frustratingly slow.  Mike
20766 Haertel suggested adding site-specific initialization scripts.  People
20767 distributing software that had to unpack on MS-DOS asked for a way to
20768 override the @file{.in} extension on the file names, which produced file
20769 names like @file{config.h.in} containing two dots.  Jim Avera did an
20770 extensive examination of the problems with quoting in @code{AC_DEFINE}
20771 and @code{AC_SUBST}; his insights led to significant improvements.
20772 Richard Stallman asked that compiler output be sent to @file{config.log}
20773 instead of @file{/dev/null}, to help people debug the Emacs
20774 @command{configure} script.
20776 I made some other changes because of my dissatisfaction with the quality
20777 of the program.  I made the messages showing results of the checks less
20778 ambiguous, always printing a result.  I regularized the names of the
20779 macros and cleaned up coding style inconsistencies.  I added some
20780 auxiliary utilities that I had developed to help convert source code
20781 packages to use Autoconf.  With the help of Fran@,{c}ois Pinard, I made
20782 the macros not interrupt each others' messages.  (That feature revealed
20783 some performance bottlenecks in @acronym{GNU} M4, which he hastily
20784 corrected!)  I reorganized the documentation around problems people want
20785 to solve.  And I began a test suite, because experience had shown that
20786 Autoconf has a pronounced tendency to regress when we change it.
20788 Again, several alpha testers gave invaluable feedback, especially
20789 Fran@,{c}ois Pinard, Jim Meyering, Karl Berry, Rob Savoye, Ken Raeburn,
20790 and Mark Eichin.
20792 Finally, version 2.0 was ready.  And there was much rejoicing.  (And I
20793 have free time again.  I think.  Yeah, right.)
20796 @c ========================================================== Appendices
20799 @node GNU Free Documentation License
20800 @appendix GNU Free Documentation License
20802 @include fdl.texi
20804 @node Indices
20805 @appendix Indices
20807 @menu
20808 * Environment Variable Index::  Index of environment variables used
20809 * Output Variable Index::       Index of variables set in output files
20810 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
20811 * Autoconf Macro Index::        Index of Autoconf macros
20812 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
20813 * Autotest Macro Index::        Index of Autotest macros
20814 * Program & Function Index::    Index of those with portability problems
20815 * Concept Index::               General index
20816 @end menu
20818 @node Environment Variable Index
20819 @appendixsec Environment Variable Index
20821 This is an alphabetical list of the environment variables that Autoconf
20822 checks.
20824 @printindex ev
20826 @node Output Variable Index
20827 @appendixsec Output Variable Index
20829 This is an alphabetical list of the variables that Autoconf can
20830 substitute into files that it creates, typically one or more
20831 makefiles.  @xref{Setting Output Variables}, for more information
20832 on how this is done.
20834 @printindex ov
20836 @node Preprocessor Symbol Index
20837 @appendixsec Preprocessor Symbol Index
20839 This is an alphabetical list of the C preprocessor symbols that the
20840 Autoconf macros define.  To work with Autoconf, C source code needs to
20841 use these names in @code{#if} or @code{#ifdef} directives.
20843 @printindex cv
20845 @node Autoconf Macro Index
20846 @appendixsec Autoconf Macro Index
20848 This is an alphabetical list of the Autoconf macros.
20849 @ifset shortindexflag
20850 To make the list easier to use, the macros are listed without their
20851 preceding @samp{AC_}.
20852 @end ifset
20854 @printindex AC
20856 @node M4 Macro Index
20857 @appendixsec M4 Macro Index
20859 This is an alphabetical list of the M4, M4sugar, and M4sh macros.
20860 @ifset shortindexflag
20861 To make the list easier to use, the macros are listed without their
20862 preceding @samp{m4_} or @samp{AS_}.
20863 @end ifset
20865 @printindex MS
20867 @node Autotest Macro Index
20868 @appendixsec Autotest Macro Index
20870 This is an alphabetical list of the Autotest macros.
20871 @ifset shortindexflag
20872 To make the list easier to use, the macros are listed without their
20873 preceding @samp{AT_}.
20874 @end ifset
20876 @printindex AT
20878 @node Program & Function Index
20879 @appendixsec Program and Function Index
20881 This is an alphabetical list of the programs and functions whose
20882 portability is discussed in this document.
20884 @printindex pr
20886 @node Concept Index
20887 @appendixsec Concept Index
20889 This is an alphabetical list of the files, tools, and concepts
20890 introduced in this document.
20892 @printindex cp
20894 @bye
20896 @c  LocalWords:  texinfo setfilename autoconf texi settitle setchapternewpage
20897 @c  LocalWords:  setcontentsaftertitlepage finalout ARG ovar varname dvar acx
20898 @c  LocalWords:  makeinfo dvi defcodeindex ev ov CPP cv Autotest mv defindex fn
20899 @c  LocalWords:  shortindexflag iftex ifset acindex ACindex ifclear ahindex fu
20900 @c  LocalWords:  asindex MSindex atindex ATindex auindex hdrindex prindex FIXME
20901 @c  LocalWords:  msindex alloca fnindex Aaarg indices FSF's dircategory ifnames
20902 @c  LocalWords:  direntry autoscan autoreconf autoheader autoupdate config FDs
20903 @c  LocalWords:  testsuite titlepage Elliston Demaille vskip filll ifnottex hmm
20904 @c  LocalWords:  insertcopying Autoconf's detailmenu Automake Libtool Posix ois
20905 @c  LocalWords:  Systemology Checkpointing Changequote INTERCAL changequote dfn
20906 @c  LocalWords:  Quadrigraphs builtins Shellology acconfig Bugward LIBOBJ Imake
20907 @c  LocalWords:  LIBOBJS IFELSE cindex flushright Pinard Metaconfig uref Simons
20908 @c  LocalWords:  distclean uninstall noindent versioning Tromey dir
20909 @c  LocalWords:  SAMS samp aclocal acsite underquoted emph itemx prepend SUBST
20910 @c  LocalWords:  evindex automake Gettext autopoint gettext symlink libtoolize
20911 @c  LocalWords:  defmac INIT tarname ovindex cvindex BUGREPORT PREREQ asis PROG
20912 @c  LocalWords:  SRCDIR srcdir globbing afterwards cmds foos fooo foooo init cd
20913 @c  LocalWords:  builddir timestamp src Imakefile chmod defvar CFLAGS CPPFLAGS
20914 @c  LocalWords:  CXXFLAGS DEFS DHAVE defvarx FCFLAGS FFLAGS LDFLAGS bindir GCC
20915 @c  LocalWords:  datadir datarootdir docdir dvidir htmldir libdir ifnothtml kbd
20916 @c  LocalWords:  includedir infodir libexecdir localedir localstatedir mandir
20917 @c  LocalWords:  oldincludedir pdfdir PDF psdir PostScript sbindir sysconfdir
20918 @c  LocalWords:  sharedstatedir DDATADIR sed tmp pkgdatadir VPATH conf unistd
20919 @c  LocalWords:  undef endif builtin FUNCS ifndef STACKSEG getb GETB YMP fubar
20920 @c  LocalWords:  PRE dest SUBDIRS subdirs fi struct STDC stdlib stddef INTTYPES
20921 @c  LocalWords:  inttypes STDINT stdint AWK AIX Solaris NeXT env EGREP FGREP yy
20922 @c  LocalWords:  LEXLIB YYTEXT lfl nonportable Automake's LN RANLIB byacc INETD
20923 @c  LocalWords:  inetd prog PROGS progs ranlib lmp lXt lX nsl gethostbyname UX
20924 @c  LocalWords:  NextStep isinf isnan glibc IRIX sunmath lm lsunmath pre sizeof
20925 @c  LocalWords:  ld inline malloc putenv setenv FreeBSD realloc SunOS MinGW
20926 @c  LocalWords:  snprintf vsnprintf sprintf vsprintf sscanf gcc strerror ifdef
20927 @c  LocalWords:  strnlen sysconf PAGESIZE unsetenv va fallback memcpy dst FUNC
20928 @c  LocalWords:  PowerPC GNUC libPW pragma Olibcalls CHOWN chown CLOSEDIR VFORK
20929 @c  LocalWords:  closedir FNMATCH fnmatch vfork FSEEKO LARGEFILE fseeko SVR sc
20930 @c  LocalWords:  largefile GETGROUPS getgroups GETLOADAVG DGUX UMAX NLIST KMEM
20931 @c  LocalWords:  SETGID getloadavg nlist GETMNTENT irix
20932 @c  LocalWords:  getmntent UnixWare GETPGRP getpgid getpgrp Posix's pid LSTAT
20933 @c  LocalWords:  lstat rpl MEMCMP memcmp OpenStep MBRTOWC mbrtowc MKTIME mktime
20934 @c  LocalWords:  localtime MMAP mmap OBSTACK obstack obstacks ARGTYPES timeval
20935 @c  LocalWords:  SETPGRP setpgrp defmacx Hurd SETVBUF setvbuf STRCOLL strcoll
20936 @c  LocalWords:  STRTOD strtod DECL STRFTIME strftime SCO UTIME utime VPRINTF
20937 @c  LocalWords:  DOPRNT vprintf doprnt sp unfixable LIBSOURCE LIBSOURCES Eggert
20938 @c  LocalWords:  linux netinet ia Tru XFree DIRENT NDIR dirent ndir multitable
20939 @c  LocalWords:  NAMLEN strlen namlen MKDEV SYSMACROS makedev RESOLV resolv DNS
20940 @c  LocalWords:  inet structs NAMESER arpa NETDB netdb UTekV UTS GCC's kB
20941 @c  LocalWords:  STDBOOL BOOL stdbool conformant cplusplus bool Bool stdarg tm
20942 @c  LocalWords:  ctype strchr strrchr rindex bcopy memmove memchr WEXITSTATUS
20943 @c  LocalWords:  WIFEXITED TIOCGWINSZ GWINSZ termios preprocess preprocessable
20944 @c  LocalWords:  DECLS strdup calloc BLKSIZE blksize RDEV rdev TZNAME tzname pw
20945 @c  LocalWords:  passwd gecos pwd MBSTATE mbstate wchar RETSIGTYPE hup UID uid
20946 @c  LocalWords:  gid ptrdiff uintmax EXEEXT OBJEXT Ae conftest AXP str
20947 @c  LocalWords:  ALIGNOF WERROR Werror cpp HP's WorkShop egcs un fied stdc CXX
20948 @c  LocalWords:  varargs BIGENDIAN Endianness SPARC endianness grep'ed CONST FC
20949 @c  LocalWords:  const STRINGIZE stringizing PARAMS unprotoize protos KCC cxx
20950 @c  LocalWords:  xlC aCC CXXCPP FREEFORM xlf FLIBS FCLIBS ish SRCEXT XTRA LFS
20951 @c  LocalWords:  ISC lcposix MINIX Minix conditionalized inlines hw dD confdefs
20952 @c  LocalWords:  fputs stdout PREPROC ar UFS HFS QNX realtime fstype STATVFS se
20953 @c  LocalWords:  statvfs STATFS statfs func machfile hdr lelf raboof DEFUN GTK
20954 @c  LocalWords:  GTKMM Grmph ified ine defn baz EOF qar Ahhh changecom algol io
20955 @c  LocalWords:  changeword quadrigraphs quadrigraph dnl SGI atoi overquoting
20956 @c  LocalWords:  Aas Wcross sep args namespace undefine bpatsubst popdef dquote
20957 @c  LocalWords:  bregexp Overquote overquotation meisch maisch meische maische
20958 @c  LocalWords:  miscian DIRNAME dirname MKDIR CATFILE XMKMF TRAVOLTA celsius
20959 @c  LocalWords:  EMX emxos Emacsen Korn DYNIX subshell posix Ksh ksh Pdksh Zsh
20960 @c  LocalWords:  pdksh zsh Allbery Lipe Kubota UWS zorglub stderr eval esac lfn
20961 @c  LocalWords:  drivespec Posixy DJGPP doschk prettybird LPT pfew Zsh's yu yaa
20962 @c  LocalWords:  yM uM aM firebird IP subdir misparses ok Unpatched abc bc zA
20963 @c  LocalWords:  CDPATH DUALCASE LINENO prepass Subshells lineno NULLCMD cmp wc
20964 @c  LocalWords:  MAILPATH scanset arg NetBSD Almquist printf expr cp
20965 @c  LocalWords:  Oliva awk Aaaaarg cmd regex xfoo GNV OpenVMS VM
20966 @c  LocalWords:  sparc Proulx nbar nfoo maxdepth acdilrtu TWG mc
20967 @c  LocalWords:  mkdir exe uname OpenBSD Fileutils mktemp umask TMPDIR guid os
20968 @c  LocalWords:  fooXXXXXX Unicos utimes hpux hppa unescaped
20969 @c  LocalWords:  pmake DOS's gmake ifoo DESTDIR autoconfiscated pc coff mips gg
20970 @c  LocalWords:  dec ultrix cpu wildcards rpcc rdtsc powerpc readline
20971 @c  LocalWords:  withval vxworks gless localcache usr LOFF loff CYGWIN Cygwin
20972 @c  LocalWords:  cygwin SIGLIST siglist SYSNDIR SYSDIR ptx lseq rusage elif MSC
20973 @c  LocalWords:  lfoo POUNDBANG lsun NIS getpwnam SYSCALLS RSH INTL lintl aix
20974 @c  LocalWords:  intl lx ldir syslog bsd EPI toolchain netbsd objext de KNR nn
20975 @c  LocalWords:  fication LTLIBOBJS Wdiff TESTDIR atconfig atlocal akim XFAIL
20976 @c  LocalWords:  ChangeLog prepended errexit smallexample TESTSUITEFLAGS GPL er
20977 @c  LocalWords:  installcheck autotest indir Pixley Bothner Eichin Kerberos adl
20978 @c  LocalWords:  DISTCLEANFILES preprocessor's fileutils Stallman Murphey Stenn
20979 @c  LocalWords:  Manfredi Autoconfig TCL FSP david zuhn Blandy MACRODIR Raeburn
20980 @c  LocalWords:  autoconfiscate Savoye Haertel Avera Meyering fdl appendixsec
20981 @c  LocalWords:  printindex american LIBOBJDIR LibdirTest ERLCFLAGS OBJCFLAGS
20982 @c  LocalWords:  VER Gnulib online xyes strcpy TYPEOF typeof OBJC objcc objc ln
20983 @c  LocalWords:  GOBJC OTP ERLC erl valloc decr dumpdef errprint incr
20984 @c  LocalWords:  esyscmd len maketemp pushdef substr syscmd sysval translit txt
20985 @c  LocalWords:  sinclude foreach myvar tolower toupper uniq BASENAME STDIN
20986 @c  LocalWords:  Dynix descrips basename aname cname macroexpands xno xcheck
20987 @c  LocalWords:  LIBREADLINE lreadline lncurses libreadline
20989 @c Local Variables:
20990 @c fill-column: 72
20991 @c ispell-local-dictionary: "american"
20992 @c indent-tabs-mode: nil
20993 @c whitespace-check-buffer-indent: nil
20994 @c End: