1 \input texinfo @c -*-texinfo-*-
2 @comment ========================================================
3 @comment %**start of header
4 @setfilename autoconf.info
7 @documentencoding UTF-8
8 @set txicodequoteundirected
9 @set txicodequotebacktick
10 @setchapternewpage odd
15 @c The ARG is an optional argument. To be used for macro arguments in
16 @c their documentation (@defmac).
18 @r{[}@var{\varname\}@r{]}
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{]}
29 @c @dvarv(ARG, DEFAULT-VAR)
30 @c ------------------------
31 @c Same as @dvar{ARG, DEFAULT-VAR}, but with @var instead of @samp
32 @c around DEFAULT-VAR.
33 @macro dvarv{varname, default}
34 @r{[}@var{\varname\} = @var{\default\}@r{]}
37 @c Handling the indexes with Texinfo yields several different problems.
39 @c Because we want to drop out the AC_ part of the macro names in the
40 @c printed manual, but not in the other outputs, we need a layer above
41 @c the usual @acindex{} etc. That's why we first define indexes such as
42 @c acx meant to become the macro @acindex. First of all, using 'ac_'
43 @c does not work with makeinfo, and using 'ac1' doesn't work with TeX.
44 @c So use something more regular 'acx'. Then you finish with a printed
45 @c index saying 'index is not existent'. Of course: you ought to use
46 @c two letters :( So you use capitals.
48 @c Second, when defining a macro in the TeX world, following spaces are
49 @c eaten. But then, since we embed @acxindex commands that use the end
50 @c of line as an end marker, the whole things wrecks itself. So make
51 @c sure you do *force* an additional end of line, add a '@c'.
53 @c Finally, you might want to get rid of TeX expansion, using --expand
54 @c with texi2dvi. But then you wake up an old problem: we use macros
55 @c in @defmac etc. where TeX does perform the expansion, but not makeinfo.
57 @c Define an environment variable index, for variables users may set
58 @c in their environment or on the configure command line.
60 @c Define an output variable index, for commonly AC_SUBST'ed variables.
62 @c Define a cache variable index, for variables matching *_cv_*.
64 @c Other shell variables not fitting the above categories should be
65 @c listed in the predefined vrindex, which we merge in the concept index.
67 @c Define a CPP preprocessor macro index, for #define'd strings.
69 @c Define an Autoconf macro index that @defmac doesn't write to.
71 @c Define an Autotest macro index that @defmac doesn't write to.
73 @c Define an M4sugar macro index that @defmac doesn't write to.
75 @c Define an index for *foreign* programs: 'mv' etc. Used for the
76 @c portability sections and so on.
81 @c Shall we factor AC_ out of the Autoconf macro index etc.?
88 @c Registering an AC_\MACRO\.
95 @ifclear shortindexflag
103 @c Registering an AH_\MACRO\.
104 @macro ahindex{macro}
111 @c Registering an AS_\MACRO\.
112 @ifset shortindexflag
113 @macro asindex{macro}
118 @ifclear shortindexflag
119 @macro asindex{macro}
126 @c Registering an AT_\MACRO\.
127 @ifset shortindexflag
128 @macro atindex{macro}
133 @ifclear shortindexflag
134 @macro atindex{macro}
141 @c Registering an AU_\MACRO\.
142 @macro auindex{macro}
149 @c Indexing a header.
150 @macro hdrindex{macro}
151 @prindex @file{\macro\}
157 @c Registering an m4_\MACRO\.
158 @ifset shortindexflag
159 @macro msindex{macro}
164 @ifclear shortindexflag
165 @macro msindex{macro}
171 @c @caindex{VARIABLE}
172 @c ------------------
173 @c Registering an ac_cv_\VARIABLE\ cache variable.
174 @ifset shortindexflag
175 @macro caindex{macro}
179 @ifclear shortindexflag
180 @macro caindex{macro}
181 @CAindex ac_cv_\macro\
185 @c Define an index for functions: 'alloca' etc. Used for the
186 @c portability sections and so on. We can't use 'fn' (aka 'fnindex'),
187 @c since '@defmac' goes into it => we'd get all the macros too.
189 @c FIXME: Aaarg! It seems there are too many indices for TeX :(
191 @c ! No room for a new @write .
192 @c l.112 @defcodeindex fu
194 @c so don't define yet another one :( Just put some tags before each
195 @c @prindex which is actually a @funindex.
200 @c @c Put the programs and functions into their own index.
201 @c @syncodeindex fu pr
203 @comment %**end of header
204 @comment ========================================================
208 This manual (@value{UPDATED}) is for GNU Autoconf
209 (version @value{VERSION}),
210 a package for creating scripts to configure source code packages using
211 templates and an M4 macro package.
213 Copyright @copyright{} 1992--1996, 1998--2017, 2020--2024 Free Software
217 Permission is granted to copy, distribute and/or modify this document
218 under the terms of the GNU Free Documentation License,
219 Version 1.3 or any later version published by the Free Software
220 Foundation; with no Invariant Sections, no Front-Cover texts, and
221 no Back-Cover Texts. A copy of the license is included in the section
222 entitled ``GNU Free Documentation License.''
228 @dircategory Software development
230 * Autoconf: (autoconf). Create source code configuration scripts.
233 @dircategory Individual utilities
235 * autoscan: (autoconf)autoscan Invocation.
236 Semi-automatic @file{configure.ac} writing
237 * ifnames: (autoconf)ifnames Invocation. Listing conditionals in source.
238 * autoconf-invocation: (autoconf)autoconf Invocation.
239 How to create configuration scripts
240 * autoreconf: (autoconf)autoreconf Invocation.
241 Remaking multiple @command{configure} scripts
242 * autoheader: (autoconf)autoheader Invocation.
243 How to create configuration templates
244 * autom4te: (autoconf)autom4te Invocation.
245 The Autoconf executables backbone
246 * configure: (autoconf)configure Invocation. Configuring a package.
247 * autoupdate: (autoconf)autoupdate Invocation.
248 Automatic update of @file{configure.ac}
249 * config.status: (autoconf)config.status Invocation. Recreating configurations.
250 * testsuite: (autoconf)testsuite Invocation. Running an Autotest test suite.
255 @subtitle Creating Automatic Configuration Scripts
256 @subtitle for version @value{VERSION}, @value{UPDATED}
257 @author David MacKenzie
259 @author Akim Demaille
261 @vskip 0pt plus 1filll
274 @c The master menu, created with texinfo-master-menu, goes here.
277 * Introduction:: Autoconf's purpose, strengths, and weaknesses
278 * The GNU Build System:: A set of tools for portable software packages
279 * Making configure Scripts:: How to organize and produce Autoconf scripts
280 * Setup:: Initialization and output
281 * Existing Tests:: Macros that check for particular features
282 * Writing Tests:: How to write new feature checks
283 * Results:: What to do with results from feature checks
284 * Programming in M4:: Layers on top of which Autoconf is written
285 * Programming in M4sh:: Shell portability layer
286 * Writing Autoconf Macros:: Adding new macros to Autoconf
287 * Portable Shell:: Shell script portability pitfalls
288 * Portable Make:: Makefile portability pitfalls
289 * Portable C and C++:: C and C++ portability pitfalls
290 * Manual Configuration:: Selecting features that can't be guessed
291 * Site Configuration:: Local defaults for @command{configure}
292 * Running configure Scripts:: How to use the Autoconf output
293 * config.status Invocation:: Recreating a configuration
294 * Obsolete Constructs:: Kept for backward compatibility
295 * Using Autotest:: Creating portable test suites
296 * FAQ:: Frequent Autoconf Questions, with answers
297 * History:: History of Autoconf
298 * GNU Free Documentation License:: License for copying this manual
299 * Indices:: Indices of symbols, concepts, etc.
302 --- The Detailed Node Listing ---
306 * Automake:: Escaping makefile hell
307 * Gnulib:: The GNU portability library
308 * Libtool:: Building libraries portably
309 * Pointers:: More info on the GNU build system
311 Making @command{configure} Scripts
313 * Writing Autoconf Input:: What to put in an Autoconf input file
314 * autoscan Invocation:: Semi-automatic @file{configure.ac} writing
315 * ifnames Invocation:: Listing the conditionals in source code
316 * autoconf Invocation:: How to create configuration scripts
317 * autoreconf Invocation:: Remaking multiple @command{configure} scripts
319 Writing @file{configure.ac}
321 * Shell Script Compiler:: Autoconf as solution of a problem
322 * Autoconf Language:: Programming in Autoconf
323 * Autoconf Input Layout:: Standard organization of @file{configure.ac}
325 Initialization and Output Files
327 * Initializing configure:: Option processing etc.
328 * Versioning:: Dealing with Autoconf versions
329 * Notices:: Copyright, version numbers in @command{configure}
330 * Input:: Where Autoconf should find files
331 * Output:: Outputting results from the configuration
332 * Configuration Actions:: Preparing the output based on results
333 * Configuration Files:: Creating output files
334 * Makefile Substitutions:: Using output variables in makefiles
335 * Configuration Headers:: Creating a configuration header file
336 * Configuration Commands:: Running arbitrary instantiation commands
337 * Configuration Links:: Links depending on the configuration
338 * Subdirectories:: Configuring independent packages together
339 * Default Prefix:: Changing the default installation prefix
341 Substitutions in Makefiles
343 * Preset Output Variables:: Output variables that are always set
344 * Installation Directory Variables:: Other preset output variables
345 * Changed Directory Variables:: Warnings about @file{datarootdir}
346 * Build Directories:: Supporting multiple concurrent compiles
347 * Automatic Remaking:: Makefile rules for configuring
349 Configuration Header Files
351 * Header Templates:: Input for the configuration headers
352 * autoheader Invocation:: How to create configuration templates
353 * Autoheader Macros:: How to specify CPP templates
357 * Common Behavior:: Macros' standard schemes
358 * Alternative Programs:: Selecting between alternative programs
359 * Files:: Checking for the existence of files
360 * Libraries:: Library archives that might be missing
361 * Library Functions:: C library functions that might be missing
362 * Header Files:: Header files that might be missing
363 * Declarations:: Declarations that may be missing
364 * Structures:: Structures or members that might be missing
365 * Types:: Types that might be missing
366 * Compilers and Preprocessors:: Checking for compiling programs
367 * System Services:: Operating system services
368 * C and Posix Variants:: Kludges for C and Posix variants
369 * Erlang Libraries:: Checking for the existence of Erlang libraries
373 * Standard Symbols:: Symbols defined by the macros
374 * Default Includes:: Includes used by the generic macros
378 * Particular Programs:: Special handling to find certain programs
379 * Generic Programs:: How to find other programs
383 * Function Portability:: Pitfalls with usual functions
384 * Particular Functions:: Special handling to find certain functions
385 * Generic Functions:: How to find other functions
389 * Header Portability:: Collected knowledge on common headers
390 * Particular Headers:: Special handling to find certain headers
391 * Generic Headers:: How to find other headers
395 * Particular Declarations:: Macros to check for certain declarations
396 * Generic Declarations:: How to find other declarations
400 * Particular Structures:: Macros to check for certain structure members
401 * Generic Structures:: How to find other structure members
405 * Particular Types:: Special handling to find certain types
406 * Generic Types:: How to find other types
408 Compilers and Preprocessors
410 * Specific Compiler Characteristics:: Some portability issues
411 * Generic Compiler Characteristics:: Language independent tests and features
412 * C Compiler:: Checking its characteristics
413 * C++ Compiler:: Likewise
414 * Objective C Compiler:: Likewise
415 * Objective C++ Compiler:: Likewise
416 * Erlang Compiler and Interpreter:: Likewise
417 * Fortran Compiler:: Likewise
418 * Go Compiler:: Likewise
422 * Language Choice:: Selecting which language to use for testing
423 * Writing Test Programs:: Forging source files for compilers
424 * Running the Preprocessor:: Detecting preprocessor symbols
425 * Running the Compiler:: Detecting language or header features
426 * Running the Linker:: Detecting library features
427 * Runtime:: Testing for runtime features
428 * Systemology:: A zoology of operating systems
429 * Multiple Cases:: Tests for several possible values
431 Writing Test Programs
433 * Guidelines:: General rules for writing test programs
434 * Test Functions:: Avoiding pitfalls in test programs
435 * Generating Sources:: Source program boilerplate
439 * Defining Symbols:: Defining C preprocessor symbols
440 * Setting Output Variables:: Replacing variables in output files
441 * Special Chars in Variables:: Characters to beware of in variables
442 * Caching Results:: Speeding up subsequent @command{configure} runs
443 * Printing Messages:: Notifying @command{configure} users
447 * Cache Variable Names:: Shell variables used in caches
448 * Cache Files:: Files @command{configure} uses for caching
449 * Cache Checkpointing:: Loading and saving the cache file
453 * M4 Quotation:: Protecting macros from unwanted expansion
454 * Using autom4te:: The Autoconf executables backbone
455 * Programming in M4sugar:: Convenient pure M4 macros
456 * Debugging via autom4te:: Figuring out what M4 was doing
460 * Active Characters:: Characters that change the behavior of M4
461 * One Macro Call:: Quotation and one macro call
462 * Quoting and Parameters:: M4 vs. shell parameters
463 * Quotation and Nested Macros:: Macros calling macros
464 * Changequote is Evil:: Worse than INTERCAL: M4 + changequote
465 * Quadrigraphs:: Another way to escape special characters
466 * Balancing Parentheses:: Dealing with unbalanced parentheses
467 * Quotation Rule Of Thumb:: One parenthesis, one quote
469 Using @command{autom4te}
471 * autom4te Invocation:: A GNU M4 wrapper
472 * Customizing autom4te:: Customizing the Autoconf package
474 Programming in M4sugar
476 * Redefined M4 Macros:: M4 builtins changed in M4sugar
477 * Diagnostic Macros:: Diagnostic messages from M4sugar
478 * Diversion support:: Diversions in M4sugar
479 * Conditional constructs:: Conditions in M4
480 * Looping constructs:: Iteration in M4
481 * Evaluation Macros:: More quotation and evaluation control
482 * Text processing Macros:: String manipulation in M4
483 * Number processing Macros:: Arithmetic computation in M4
484 * Set manipulation Macros:: Set manipulation in M4
485 * Forbidden Patterns:: Catching unexpanded macros
489 * Common Shell Constructs:: Portability layer for common shell constructs
490 * Polymorphic Variables:: Support for indirect variable names
491 * Initialization Macros:: Macros to establish a sane shell environment
492 * File Descriptor Macros:: File descriptor macros for input and output
494 Writing Autoconf Macros
496 * Macro Definitions:: Basic format of an Autoconf macro
497 * Macro Names:: What to call your new macros
498 * Dependencies Between Macros:: What to do when macros depend on other macros
499 * Obsoleting Macros:: Warning about old ways of doing things
500 * Coding Style:: Writing Autoconf macros à la Autoconf
502 Dependencies Between Macros
504 * Prerequisite Macros:: Ensuring required information
505 * Suggested Ordering:: Warning about possible ordering problems
506 * One-Shot Macros:: Ensuring a macro is called only once
508 Portable Shell Programming
510 * Shellology:: A zoology of shells
511 * Invoking the Shell:: Invoking the shell as a command
512 * Here-Documents:: Quirks and tricks
513 * File Descriptors:: FDs and redirections
514 * Signal Handling:: Shells, signals, and headaches
515 * File System Conventions:: File names
516 * Shell Pattern Matching:: Pattern matching
517 * Shell Substitutions:: Variable and command expansions
518 * Assignments:: Varying side effects of assignments
519 * Parentheses:: Parentheses in shell scripts
520 * Slashes:: Slashes in shell scripts
521 * Special Shell Variables:: Variables you should not change
522 * Shell Functions:: What to look out for if you use them
523 * Limitations of Builtins:: Portable use of not so portable /bin/sh
524 * Limitations of Usual Tools:: Portable use of portable tools
526 Portable Make Programming
528 * $< in Ordinary Make Rules:: $< in ordinary rules
529 * Failure in Make Rules:: Failing portably in rules
530 * Special Chars in Names:: Special Characters in Macro Names
531 * Backslash-Newline-Empty:: Empty lines after backslash-newline
532 * Backslash-Newline Comments:: Spanning comments across line boundaries
533 * Long Lines in Makefiles:: Line length limitations
534 * Macros and Submakes:: @code{make macro=value} and submakes
535 * The Make Macro MAKEFLAGS:: @code{$(MAKEFLAGS)} portability issues
536 * The Make Macro SHELL:: @code{$(SHELL)} portability issues
537 * Parallel Make:: Parallel @command{make} quirks
538 * Comments in Make Rules:: Other problems with Make comments
539 * Newlines in Make Rules:: Using literal newlines in rules
540 * Comments in Make Macros:: Other problems with Make comments in macros
541 * Trailing whitespace in Make Macros:: Macro substitution problems
542 * Command-line Macros and whitespace:: Whitespace trimming of values
543 * obj/ and Make:: Don't name a subdirectory @file{obj}
544 * make -k Status:: Exit status of @samp{make -k}
545 * VPATH and Make:: @code{VPATH} woes
546 * Single Suffix Rules:: Single suffix rules and separated dependencies
547 * Timestamps and Make:: Sub-second timestamp resolution
549 @code{VPATH} and Make
551 * Variables listed in VPATH:: @code{VPATH} must be literal on ancient hosts
552 * VPATH and Double-colon:: Problems with @samp{::} on ancient hosts
553 * $< in Explicit Rules:: @code{$<} does not work in ordinary rules
554 * Automatic Rule Rewriting:: @code{VPATH} goes wild on Solaris
555 * Tru64 Directory Magic:: @command{mkdir} goes wild on Tru64
556 * Make Target Lookup:: More details about @code{VPATH} lookup
558 Portable C and C++ Programming
560 * Varieties of Unportability:: How to make your programs unportable
561 * Integer Overflow:: When integers get too large
562 * Preprocessor Arithmetic:: @code{#if} expression problems
563 * Null Pointers:: Properties of null pointers
564 * Buffer Overruns:: Subscript errors and the like
565 * Volatile Objects:: @code{volatile} and signals
566 * Floating Point Portability:: Portable floating-point arithmetic
567 * Exiting Portably:: Exiting and the exit status
571 * Integer Overflow Basics:: Why integer overflow is a problem
572 * Signed Overflow Examples:: Examples of code assuming wraparound
573 * Optimization and Wraparound:: Optimizations that break uses of wraparound
574 * Signed Overflow Advice:: Practical advice for signed overflow issues
575 * Signed Integer Division:: @code{INT_MIN / -1} and @code{INT_MIN % -1}
579 * Specifying Target Triplets:: Specifying target triplets
580 * Canonicalizing:: Getting the canonical system type
581 * Using System Type:: What to do with the system type
585 * Help Formatting:: Customizing @samp{configure --help}
586 * External Software:: Working with other optional software
587 * Package Options:: Selecting optional features
588 * Pretty Help Strings:: Formatting help string
589 * Option Checking:: Controlling checking of @command{configure} options
590 * Site Details:: Configuring site details
591 * Transforming Names:: Changing program names when installing
592 * Site Defaults:: Giving @command{configure} local defaults
594 Transforming Program Names When Installing
596 * Transformation Options:: @command{configure} options to transform names
597 * Transformation Examples:: Sample uses of transforming names
598 * Transformation Rules:: Makefile uses of transforming names
600 Running @command{configure} Scripts
602 * Basic Installation:: Instructions for typical cases
603 * Compilers and Options:: Selecting compilers and optimization
604 * Multiple Architectures:: Compiling for multiple architectures at once
605 * Installation Names:: Installing in different directories
606 * Optional Features:: Selecting optional features
607 * System Types:: Specifying a system type
608 * Sharing Defaults:: Setting site-wide defaults for @command{configure}
609 * Defining Variables:: Specifying the compiler etc.
610 * configure Invocation:: Changing how @command{configure} runs
614 * Obsolete config.status Use:: Obsolete convention for @command{config.status}
615 * acconfig Header:: Additional entries in @file{config.h.in}
616 * autoupdate Invocation:: Automatic update of @file{configure.ac}
617 * Obsolete Macros:: Backward compatibility macros
618 * Autoconf 1:: Tips for upgrading your files
619 * Autoconf 2.13:: Some fresher tips
621 Upgrading From Version 1
623 * Changed File Names:: Files you might rename
624 * Changed Makefiles:: New things to put in @file{Makefile.in}
625 * Changed Macros:: Macro calls you might replace
626 * Changed Results:: Changes in how to check test results
627 * Changed Macro Writing:: Better ways to write your own macros
629 Upgrading From Version 2.13
631 * Changed Quotation:: Broken code which used to work
632 * New Macros:: Interaction with foreign macros
633 * Hosts and Cross-Compilation:: Bugward compatibility kludges
634 * AC_LIBOBJ vs LIBOBJS:: LIBOBJS is a forbidden token
635 * AC_ACT_IFELSE vs AC_TRY_ACT:: A more generic scheme for testing sources
637 Generating Test Suites with Autotest
639 * Using an Autotest Test Suite:: Autotest and the user
640 * Writing Testsuites:: Autotest macros
641 * testsuite Invocation:: Running @command{testsuite} scripts
642 * Making testsuite Scripts:: Using autom4te to create @command{testsuite}
644 Using an Autotest Test Suite
646 * testsuite Scripts:: The concepts of Autotest
647 * Autotest Logs:: Their contents
649 Frequent Autoconf Questions, with answers
651 * Distributing:: Distributing @command{configure} scripts
652 * Why GNU M4:: Why not use the standard M4?
653 * Bootstrapping:: Autoconf and GNU M4 require each other?
654 * Why Not Imake:: Why GNU uses @command{configure} instead of Imake
655 * Defining Directories:: Passing @code{datadir} to program
656 * Autom4te Cache:: What is it? Can I remove it?
657 * Present But Cannot Be Compiled:: Compiler and Preprocessor Disagree
658 * Expanded Before Required:: Expanded Before Required
659 * Debugging:: Debugging @command{configure} scripts
663 * Genesis:: Prehistory and naming of @command{configure}
664 * Exodus:: The plagues of M4 and Perl
665 * Leviticus:: The priestly code of portability arrives
666 * Numbers:: Growth and contributors
667 * Deuteronomy:: Approaching the promises of easy configuration
671 * Environment Variable Index:: Index of environment variables used
672 * Output Variable Index:: Index of variables set in output files
673 * Preprocessor Symbol Index:: Index of C preprocessor symbols defined
674 * Cache Variable Index:: Index of documented cache variables
675 * Autoconf Macro Index:: Index of Autoconf macros
676 * M4 Macro Index:: Index of M4, M4sugar, and M4sh macros
677 * Autotest Macro Index:: Index of Autotest macros
678 * Program & Function Index:: Index of those with portability problems
679 * Concept Index:: General index
684 @c ============================================================= Introduction.
687 @chapter Introduction
691 A physicist, an engineer, and a computer scientist were discussing the
692 nature of God. ``Surely a Physicist,'' said the physicist, ``because
693 early in the Creation, God made Light; and you know, Maxwell's
694 equations, the dual nature of electromagnetic waves, the relativistic
695 consequences@enddots{}'' ``An Engineer!,'' said the engineer, ``because
696 before making Light, God split the Chaos into Land and Water; it takes
697 a hell of an engineer to handle that big amount of mud, and orderly
698 separation of solids from liquids@enddots{}'' The computer scientist
699 shouted: ``And the Chaos, where do you think it was coming from, hmm?''
703 @c (via François Pinard)
705 Autoconf is a tool for producing shell scripts that automatically
706 configure software source code packages to adapt to many kinds of
707 Posix-like systems. The configuration scripts produced by Autoconf
708 are independent of Autoconf when they are run, so their users do not
709 need to have Autoconf.
711 The configuration scripts produced by Autoconf require no manual user
712 intervention when run; they do not normally even need an argument
713 specifying the system type. Instead, they individually test for the
714 presence of each feature that the software package they are for might need.
715 (Before each check, they print a one-line message stating what they are
716 checking for, so the user doesn't get too bored while waiting for the
717 script to finish.) As a result, they deal well with systems that are
718 hybrids or customized from the more common Posix variants. There is
719 no need to maintain files that list the features supported by each
720 release of each variant of Posix.
722 For each software package that Autoconf is used with, it creates a
723 configuration script from a template file that lists the system features
724 that the package needs or can use. After the shell code to recognize
725 and respond to a system feature has been written, Autoconf allows it to
726 be shared by many software packages that can use (or need) that feature.
727 If it later turns out that the shell code needs adjustment for some
728 reason, it needs to be changed in only one place; all of the
729 configuration scripts can be regenerated automatically to take advantage
732 @c "Those who do not understand Unix are condemned to reinvent it, poorly."
733 @c --Henry Spencer, 1987 (see https://en.wikipedia.org/wiki/Unix_philosophy)
734 Those who do not understand Autoconf are condemned to reinvent it, poorly.
735 The primary goal of Autoconf is making the @emph{user's} life easier;
736 making the @emph{maintainer's} life easier is only a secondary goal.
737 Put another way, the primary goal is not to make the generation of
738 @file{configure} automatic for package maintainers (although patches
739 along that front are welcome, since package maintainers form the user
740 base of Autoconf); rather, the goal is to make @file{configure}
741 painless, portable, and predictable for the end user of each
742 @dfn{autoconfiscated} package. And to this degree, Autoconf is highly
743 successful at its goal---most complaints to the Autoconf list are
744 about difficulties in writing Autoconf input, and not in the behavior of
745 the resulting @file{configure}. Even packages that don't use Autoconf
746 will generally provide a @file{configure} script, and the most common
747 complaint about these alternative home-grown scripts is that they fail
748 to meet one or more of the GNU Coding Standards (@pxref{Configuration, , ,
749 standards, The GNU Coding Standards}) that users
750 have come to expect from Autoconf-generated @file{configure} scripts.
752 The Metaconfig package is similar in purpose to Autoconf, but the
753 scripts it produces require manual user intervention, which is quite
754 inconvenient when configuring large source trees. Unlike Metaconfig
755 scripts, Autoconf scripts can support cross-compiling, if some care is
756 taken in writing them.
758 Autoconf does not solve all problems related to making portable
759 software packages---for a more complete solution, it should be used in
760 concert with other GNU build tools like Automake and
761 Libtool. These other tools take on jobs like the creation of a
762 portable, recursive makefile with all of the standard targets,
763 linking of shared libraries, and so on. @xref{The GNU Build System},
764 for more information.
766 Autoconf imposes some restrictions on the names of macros used with
767 @code{#if} in C programs (@pxref{Preprocessor Symbol Index}).
769 Autoconf requires GNU M4 version 1.4.8 or later in order to
770 generate the scripts. It uses features that some versions of M4,
771 including GNU M4 1.3, do not have. Autoconf works better
772 with GNU M4 version 1.4.16 or later, though this is not
775 @xref{Autoconf 1}, for information about upgrading from version 1.
776 @xref{History}, for the story of Autoconf's development. @xref{FAQ},
777 for answers to some common questions about Autoconf.
779 See the @uref{https://@/www.gnu.org/@/software/@/autoconf/,
780 Autoconf web page} for up-to-date information, details on the mailing
781 lists, pointers to a list of known bugs, etc.
783 Mail suggestions to @email{autoconf@@gnu.org, the Autoconf mailing
784 list}. Past suggestions are
785 @uref{https://@/lists.gnu.org/@/archive/@/html/@/autoconf/, archived}.
787 Mail bug reports to @email{bug-autoconf@@gnu.org, the
788 Autoconf Bugs mailing list}. Past bug reports are
789 @uref{https://@/lists.gnu.org/@/archive/@/html/@/bug-autoconf/, archived}.
791 If possible, first check that your bug is
792 not already solved in current development versions, and that it has not
793 been reported yet. Be sure to include all the needed information and a
794 short @file{configure.ac} that demonstrates the problem.
796 Autoconf's development tree is accessible via @command{git}; see the
797 @uref{https://@/savannah.gnu.org/@/projects/@/autoconf/, Autoconf
798 Summary} for details, or view
799 @uref{https://@/git.savannah.gnu.org/@/cgit/@/autoconf.git, the actual
800 repository}. Patches relative to the current @command{git} version can
801 be sent for review to the @email{autoconf-patches@@gnu.org, Autoconf
802 Patches mailing list}, with discussion on prior patches
803 @uref{https://@/lists.gnu.org/@/archive/@/html/@/autoconf-@/patches/,
804 archived}; and all commits are posted in the read-only
805 @email{autoconf-commit@@gnu.org, Autoconf Commit mailing list}, which is
806 also @uref{https://@/lists.gnu.org/@/archive/@/html/@/autoconf-commit/,
809 Because of its mission, the Autoconf package itself
810 includes only a set of often-used
811 macros that have already demonstrated their usefulness. Nevertheless,
812 if you wish to share your macros, or find existing ones, see the
813 @uref{https://@/www.gnu.org/@/software/@/autoconf-archive/, Autoconf Macro
814 Archive}, which is kindly run by @email{simons@@cryp.to,
818 @c ================================================= The GNU Build System
820 @node The GNU Build System
821 @chapter The GNU Build System
822 @cindex GNU build system
824 Autoconf solves an important problem---reliable discovery of
825 system-specific build and runtime information---but this is only one
826 piece of the puzzle for the development of portable software. To this
827 end, the GNU project has developed a suite of integrated
828 utilities to finish the job Autoconf started: the GNU build
829 system, whose most important components are Autoconf, Automake, and
830 Libtool. In this chapter, we introduce you to those tools, point you
831 to sources of more information, and try to convince you to use the
832 entire GNU build system for your software.
835 * Automake:: Escaping makefile hell
836 * Gnulib:: The GNU portability library
837 * Libtool:: Building libraries portably
838 * Pointers:: More info on the GNU build system
844 The ubiquity of @command{make} means that a makefile is almost the
845 only viable way to distribute automatic build rules for software, but
846 one quickly runs into its numerous limitations. Its lack of
847 support for automatic dependency tracking, recursive builds in
848 subdirectories, reliable timestamps (e.g., for network file systems), and
849 so on, mean that developers must painfully (and often incorrectly)
850 reinvent the wheel for each project. Portability is non-trivial, thanks
851 to the quirks of @command{make} on many systems. On top of all this is the
852 manual labor required to implement the many standard targets that users
853 have come to expect (@code{make install}, @code{make distclean},
854 @code{make uninstall}, etc.). Since you are, of course, using Autoconf,
855 you also have to insert repetitive code in your @file{Makefile.in} to
856 recognize @code{@@CC@@}, @code{@@CFLAGS@@}, and other substitutions
857 provided by @command{configure}. Into this mess steps @dfn{Automake}.
860 Automake allows you to specify your build needs in a @file{Makefile.am}
861 file with a vastly simpler and more powerful syntax than that of a plain
862 makefile, and then generates a portable @file{Makefile.in} for
863 use with Autoconf. For example, the @file{Makefile.am} to build and
864 install a simple ``Hello world'' program might look like:
868 hello_SOURCES = hello.c
872 The resulting @file{Makefile.in} (~400 lines) automatically supports all
873 the standard targets, the substitutions provided by Autoconf, automatic
874 dependency tracking, @code{VPATH} building, and so on. @command{make}
875 builds the @code{hello} program, and @code{make install} installs it
876 in @file{/usr/local/bin} (or whatever prefix was given to
877 @command{configure}, if not @file{/usr/local}).
879 The benefits of Automake increase for larger packages (especially ones
880 with subdirectories), but even for small programs the added convenience
881 and portability can be substantial. And that's not all@enddots{}
886 GNU software has a well-deserved reputation for running on
887 many different types of systems. While our primary goal is to write
888 software for the GNU system, many users and developers have
889 been introduced to us through the systems that they were already using.
892 Gnulib is a central location for common GNU code, intended to
893 be shared among free software packages. Its components are typically
894 shared at the source level, rather than being a library that gets built,
895 installed, and linked against. The idea is to copy files from Gnulib
896 into your own source tree. There is no distribution tarball; developers
897 should just grab source modules from the repository. The source files
898 are available online, under various licenses, mostly GNU
901 Gnulib modules typically contain C source code along with Autoconf
902 macros used to configure the source code. For example, the Gnulib
903 @code{stdckdint} module implements a @file{stdckdint.h} header that nearly
904 conforms to C23, even on older hosts that lack @file{stdckdint.h}.
905 This module contains a source file for the replacement header, along
906 with an Autoconf macro that arranges to use the replacement header on
909 For more information, consult the Gnulib website,
910 @uref{https://@/www.gnu.org/@/software/@/gnulib/}.
915 Often, one wants to build not only programs, but libraries, so that
916 other programs can benefit from the fruits of your labor. Ideally, one
917 would like to produce @emph{shared} (dynamically linked) libraries,
918 which can be used by multiple programs without duplication on disk or in
919 memory and can be updated independently of the linked programs.
920 Producing shared libraries portably, however, is the stuff of
921 nightmares---each system has its own incompatible tools, compiler flags,
922 and magic incantations. Fortunately, GNU provides a solution:
926 Libtool handles all the requirements of building shared libraries for
927 you, and at this time seems to be the @emph{only} way to do so with any
928 portability. It also handles many other headaches, such as: the
929 interaction of Make rules with the variable suffixes of
930 shared libraries, linking reliably with shared libraries before they are
931 installed by the superuser, and supplying a consistent versioning system
932 (so that different versions of a library can be installed or upgraded
933 without breaking binary compatibility). Although Libtool, like
934 Autoconf, can be used without Automake, it is most simply utilized in
935 conjunction with Automake---there, Libtool is used automatically
936 whenever shared libraries are needed, and you need not know its syntax.
941 Developers who are used to the simplicity of @command{make} for small
942 projects on a single system might be daunted at the prospect of
943 learning to use Automake and Autoconf. As your software is
944 distributed to more and more users, however, you otherwise
945 quickly find yourself putting lots of effort into reinventing the
946 services that the GNU build tools provide, and making the
947 same mistakes that they once made and overcame. (Besides, since
948 you're already learning Autoconf, Automake is a piece of cake.)
950 There are a number of places that you can go to for more information on
957 The project home pages for
958 @uref{https://@/www@/.gnu@/.org/@/software/@/autoconf/, Autoconf},
959 @uref{https://@/www@/.gnu@/.org/@/software/@/automake/, Automake},
960 @uref{https://@/www@/.gnu@/.org/@/software/@/gnulib/, Gnulib}, and
961 @uref{https://@/www@/.gnu@/.org/@/software/@/libtool/, Libtool}.
963 @item Automake Manual
965 @xref{Top, , Automake, automake, GNU Automake}, for more
966 information on Automake.
970 The book @cite{GNU Autoconf, Automake and
971 Libtool}@footnote{@cite{GNU Autoconf, Automake and Libtool},
972 by G. V. Vaughan, B. Elliston, T. Tromey, and I. L. Taylor. SAMS (originally
973 New Riders), 2000, ISBN 1578701902.} describes the complete GNU
974 build environment. You can also find
975 @uref{https://@/www.sourceware.org/@/autobook/, the entire book on-line}.
979 @c ================================================= Making configure Scripts.
981 @node Making configure Scripts
982 @chapter Making @command{configure} Scripts
983 @cindex @file{aclocal.m4}
984 @cindex @command{configure}
986 The configuration scripts that Autoconf produces are by convention
987 called @command{configure}. When run, @command{configure} creates several
988 files, replacing configuration parameters in them with appropriate
989 values. The files that @command{configure} creates are:
993 one or more @file{Makefile} files, usually one in each subdirectory of the
994 package (@pxref{Makefile Substitutions});
997 optionally, a C header file, the name of which is configurable,
998 containing @code{#define} directives (@pxref{Configuration Headers});
1001 a shell script called @file{config.status} that, when run, recreates
1002 the files listed above (@pxref{config.status Invocation});
1005 an optional shell script normally called @file{config.cache}
1006 (created when using @samp{configure --config-cache}) that
1007 saves the results of running many of the tests (@pxref{Cache Files});
1010 a file called @file{config.log} containing any messages produced by
1011 compilers, to help debugging if @command{configure} makes a mistake.
1014 @cindex @file{configure.ac}
1015 To create a @command{configure} script with Autoconf, you need
1016 to write an Autoconf input file @file{configure.ac} and run
1017 @command{autoconf} on it. If you write your own feature tests to
1018 supplement those that come with Autoconf, you might also write files
1019 called @file{aclocal.m4} and @file{acsite.m4}. If you use a C header
1020 file to contain @code{#define} directives, you might also run
1021 @command{autoheader}, and you can distribute the generated file
1022 @file{config.h.in} with the package.
1024 Here is a diagram showing how the files that can be used in
1025 configuration are produced. Programs that are executed are suffixed by
1026 @samp{*}. Optional files are enclosed in square brackets (@samp{[]}).
1027 @command{autoconf} and @command{autoheader} also read the installed Autoconf
1028 macro files (by reading @file{autoconf.m4}).
1031 Files used in preparing a software package for distribution, when using
1034 your source files --> [autoscan*] --> [configure.scan] --> configure.ac
1038 | .------> autoconf* -----> configure
1039 [aclocal.m4] --+---+
1040 | `-----> [autoheader*] --> [config.h.in]
1048 Additionally, if you use Automake, the following additional productions
1055 [local macros] --+--> aclocal* --> aclocal.m4
1062 +--> automake* --> Makefile.in
1068 Files used in configuring a software package:
1071 .-------------> [config.cache]
1072 configure* ------------+-------------> config.log
1074 [config.h.in] -. v .-> [config.h] -.
1075 +--> config.status* -+ +--> make*
1076 Makefile.in ---' `-> Makefile ---'
1081 * Writing Autoconf Input:: What to put in an Autoconf input file
1082 * autoscan Invocation:: Semi-automatic @file{configure.ac} writing
1083 * ifnames Invocation:: Listing the conditionals in source code
1084 * autoconf Invocation:: How to create configuration scripts
1085 * autoreconf Invocation:: Remaking multiple @command{configure} scripts
1088 @node Writing Autoconf Input
1089 @section Writing @file{configure.ac}
1091 To produce a @command{configure} script for a software package, create a
1092 file called @file{configure.ac} that contains invocations of the
1093 Autoconf macros that test the system features your package needs or can
1094 use. Autoconf macros already exist to check for many features; see
1095 @ref{Existing Tests}, for their descriptions. For most other features,
1096 you can use Autoconf template macros to produce custom checks; see
1097 @ref{Writing Tests}, for information about them. For especially tricky
1098 or specialized features, @file{configure.ac} might need to contain some
1099 hand-crafted shell commands; see @ref{Portable Shell, , Portable Shell
1100 Programming}. The @command{autoscan} program can give you a good start
1101 in writing @file{configure.ac} (@pxref{autoscan Invocation}, for more
1104 @cindex @file{configure.in}
1105 Previous versions of Autoconf promoted the name @file{configure.in},
1106 which is somewhat ambiguous (the tool needed to process this file is not
1107 described by its extension), and introduces a slight confusion with
1108 @file{config.h.in} and so on (for which @samp{.in} means ``to be
1109 processed by @command{configure}''). Using @file{configure.ac} is now
1110 preferred, while the use of @file{configure.in} will cause warnings
1111 from @command{autoconf}.
1114 * Shell Script Compiler:: Autoconf as solution of a problem
1115 * Autoconf Language:: Programming in Autoconf
1116 * Autoconf Input Layout:: Standard organization of @file{configure.ac}
1119 @node Shell Script Compiler
1120 @subsection A Shell Script Compiler
1122 Just as for any other computer language, in order to properly program
1123 @file{configure.ac} in Autoconf you must understand @emph{what} problem
1124 the language tries to address and @emph{how} it does so.
1126 The problem Autoconf addresses is that the world is a mess. After all,
1127 you are using Autoconf in order to have your package compile easily on
1128 all sorts of different systems, some of them being extremely hostile.
1129 Autoconf itself bears the price for these differences: @command{configure}
1130 must run on all those systems, and thus @command{configure} must limit itself
1131 to their lowest common denominator of features.
1133 Naturally, you might then think of shell scripts; who needs
1134 @command{autoconf}? A set of properly written shell functions is enough to
1135 make it easy to write @command{configure} scripts by hand. Sigh!
1136 Unfortunately, even in 2008, where shells without any function support are
1137 far and few between, there are pitfalls to avoid when making use of them.
1138 Also, finding a Bourne shell that accepts shell functions is not trivial,
1139 even though there is almost always one on interesting porting targets.
1141 So, what is really needed is some kind of compiler, @command{autoconf},
1142 that takes an Autoconf program, @file{configure.ac}, and transforms it
1143 into a portable shell script, @command{configure}.
1145 How does @command{autoconf} perform this task?
1147 There are two obvious possibilities: creating a brand new language or
1148 extending an existing one. The former option is attractive: all
1149 sorts of optimizations could easily be implemented in the compiler and
1150 many rigorous checks could be performed on the Autoconf program
1151 (e.g., rejecting any non-portable construct). Alternatively, you can
1152 extend an existing language, such as the @code{sh} (Bourne shell)
1155 Autoconf does the latter: it is a layer on top of @code{sh}. It was
1156 therefore most convenient to implement @command{autoconf} as a macro
1157 expander: a program that repeatedly performs @dfn{macro expansions} on
1158 text input, replacing macro calls with macro bodies and producing a pure
1159 @code{sh} script in the end. Instead of implementing a dedicated
1160 Autoconf macro expander, it is natural to use an existing
1161 general-purpose macro language, such as M4, and implement the extensions
1162 as a set of M4 macros.
1165 @node Autoconf Language
1166 @subsection The Autoconf Language
1169 The Autoconf language differs from many other computer
1170 languages because it treats actual code the same as plain text. Whereas
1171 in C, for instance, data and instructions have different syntactic
1172 status, in Autoconf their status is rigorously the same. Therefore, we
1173 need a means to distinguish literal strings from text to be expanded:
1176 When calling macros that take arguments, there must not be any white
1177 space between the macro name and the open parenthesis.
1180 AC_INIT ([oops], [1.0]) # incorrect
1181 AC_INIT([hello], [1.0]) # good
1185 be enclosed within the quote characters @samp{[} and @samp{]}, and be
1186 separated by commas. Any leading blanks or newlines in arguments are ignored,
1187 unless they are quoted. You should always quote an argument that
1188 might contain a macro name, comma, parenthesis, or a leading blank or
1189 newline. This rule applies recursively for every macro
1190 call, including macros called from other macros. For more details on
1191 quoting rules, see @ref{Programming in M4}.
1196 AC_CHECK_HEADER([stdio.h],
1197 [AC_DEFINE([HAVE_STDIO_H], [1],
1198 [Define to 1 if you have <stdio.h>.])],
1199 [AC_MSG_ERROR([sorry, can't do anything for you])])
1203 is quoted properly. You may safely simplify its quotation to:
1206 AC_CHECK_HEADER([stdio.h],
1207 [AC_DEFINE([HAVE_STDIO_H], 1,
1208 [Define to 1 if you have <stdio.h>.])],
1209 [AC_MSG_ERROR([sorry, can't do anything for you])])
1213 because @samp{1} cannot contain a macro call. Here, the argument of
1214 @code{AC_MSG_ERROR} must be quoted; otherwise, its comma would be
1215 interpreted as an argument separator. Also, the second and third arguments
1216 of @samp{AC_CHECK_HEADER} must be quoted, since they contain
1217 macro calls. The three arguments @samp{HAVE_STDIO_H}, @samp{stdio.h},
1218 and @samp{Define to 1 if you have <stdio.h>.} do not need quoting, but
1219 if you unwisely defined a macro with a name like @samp{Define} or
1220 @samp{stdio} then they would need quoting. Cautious Autoconf users
1221 would keep the quotes, but many Autoconf users find such precautions
1222 annoying, and would rewrite the example as follows:
1225 AC_CHECK_HEADER(stdio.h,
1226 [AC_DEFINE(HAVE_STDIO_H, 1,
1227 [Define to 1 if you have <stdio.h>.])],
1228 [AC_MSG_ERROR([sorry, can't do anything for you])])
1232 This is safe, so long as you adopt good naming conventions and do not
1233 define macros with names like @samp{HAVE_STDIO_H}, @samp{stdio}, or
1234 @samp{h}. Though it is also safe here to omit the quotes around
1235 @samp{Define to 1 if you have <stdio.h>.} this is not recommended, as
1236 message strings are more likely to inadvertently contain commas.
1238 The following example is wrong and dangerous, as it is underquoted:
1241 AC_CHECK_HEADER(stdio.h,
1242 AC_DEFINE(HAVE_STDIO_H, 1,
1243 Define to 1 if you have <stdio.h>.),
1244 AC_MSG_ERROR([sorry, can't do anything for you]))
1247 In other cases, you may want to use text that also resembles a macro
1248 call. You must quote that text (whether just the potential problem, or
1249 the entire line) even when it is not passed as a macro argument; and you
1250 may also have to use @code{m4_pattern_allow} (@pxref{Forbidden
1251 Patterns}), to declare your intention that the resulting configure file
1252 will have a literal that resembles what would otherwise be reserved for
1253 a macro name. For example:
1256 dnl Simulate a possible future autoconf macro
1257 m4_define([AC_DC], [oops])
1259 echo "Hard rock was here! --AC_DC"
1260 dnl Correctly quoted:
1261 m4_pattern_allow([AC_DC])
1262 echo "Hard rock was here! --[AC_DC]"
1263 [echo "Hard rock was here! --AC_DC"]
1267 which results in this text in @file{configure}:
1270 echo "Hard rock was here! --oops"
1271 echo "Hard rock was here! --AC_DC"
1272 echo "Hard rock was here! --AC_DC"
1276 When you use the same text in a macro argument, you must therefore have
1277 an extra quotation level (since one is stripped away by the macro
1278 substitution). In general, then, it is a good idea to @emph{use double
1279 quoting for all literal string arguments}, either around just the
1280 problematic portions, or over the entire argument:
1283 m4_pattern_allow([AC_DC])
1284 AC_MSG_WARN([[AC_DC] stinks --Iron Maiden])
1285 AC_MSG_WARN([[AC_DC stinks --Iron Maiden]])
1288 It is also possible to avoid the problematic patterns in the first
1289 place, by the use of additional escaping (either a quadrigraph, or
1290 creative shell constructs), in which case it is no longer necessary to
1291 use @code{m4_pattern_allow}:
1294 echo "Hard rock was here! --AC""_DC"
1295 AC_MSG_WARN([[AC@@&t@@_DC stinks --Iron Maiden]])
1298 You are now able to understand one of the constructs of Autoconf that
1299 has been continually misunderstood@enddots{} The rule of thumb is that
1300 @emph{whenever you expect macro expansion, expect quote expansion};
1301 i.e., expect one level of quotes to be lost. For instance:
1304 AC_COMPILE_IFELSE(AC_LANG_SOURCE([char b[10];]), [],
1305 [AC_MSG_ERROR([you lose])])
1309 is incorrect: here, the first argument of @code{AC_LANG_SOURCE} is
1310 @samp{char b[10];} and is expanded once, which results in
1311 @samp{char b10;}; and the @code{AC_LANG_SOURCE} is also expanded prior
1312 to being passed to @code{AC_COMPILE_IFELSE}. (There was an idiom common
1313 in Autoconf's past to
1314 address this issue via the M4 @code{changequote} primitive, but do not
1315 use it!) Let's take a closer look: the author meant the first argument
1316 to be understood as a literal, and therefore it must be quoted twice;
1317 likewise, the intermediate @code{AC_LANG_SOURCE} macro should be quoted
1318 once so that it is only expanded after the rest of the body of
1319 @code{AC_COMPILE_IFELSE} is in place:
1322 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char b[10];]])], [],
1323 [AC_MSG_ERROR([you lose])])
1327 Voilà , you actually produce @samp{char b[10];} this time!
1329 On the other hand, descriptions (e.g., the last parameter of
1330 @code{AC_DEFINE} or @code{AS_HELP_STRING}) are not literals---they
1331 are subject to line breaking, for example---and should not be double quoted.
1332 Even if these descriptions are short and are not actually broken, double
1333 quoting them yields weird results.
1335 Some macros take optional arguments, which this documentation represents
1336 as @ovar{arg} (not to be confused with the quote characters). You may
1337 just leave them empty, or use @samp{[]} to make the emptiness of the
1338 argument explicit, or you may simply omit the trailing commas. The
1339 three lines below are equivalent:
1342 AC_CHECK_HEADERS([stdio.h], [], [], [])
1343 AC_CHECK_HEADERS([stdio.h],,,)
1344 AC_CHECK_HEADERS([stdio.h])
1347 It is best to put each macro call on its own line in
1348 @file{configure.ac}. Most of the macros don't add extra newlines; they
1349 rely on the newline after the macro call to terminate the commands.
1350 This approach makes the generated @command{configure} script a little
1351 easier to read by not inserting lots of blank lines. It is generally
1352 safe to set shell variables on the same line as a macro call, because
1353 the shell allows assignments without intervening newlines.
1355 You can include comments in @file{configure.ac} files by starting them
1356 with the @samp{#}. For example, it is helpful to begin
1357 @file{configure.ac} files with a line like this:
1360 # Process this file with autoconf to produce a configure script.
1363 @node Autoconf Input Layout
1364 @subsection Standard @file{configure.ac} Layout
1366 The order in which @file{configure.ac} calls the Autoconf macros is not
1367 important, with a few exceptions. Every @file{configure.ac} must
1368 contain a call to @code{AC_INIT} before the checks, and a call to
1369 @code{AC_OUTPUT} at the end (@pxref{Output}). Additionally, some macros
1370 rely on other macros having been called first, because they check
1371 previously set values of some variables to decide what to do. These
1372 macros are noted in the individual descriptions (@pxref{Existing
1373 Tests}), and they also warn you when @command{configure} is created if they
1374 are called out of order.
1376 To encourage consistency, here is a suggested order for calling the
1377 Autoconf macros. Generally speaking, the things near the end of this
1378 list are those that could depend on things earlier in it. For example,
1379 library functions could be affected by types and libraries.
1383 Autoconf requirements
1384 @code{AC_INIT(@var{package}, @var{version}, @var{bug-report-address})}
1385 information on the package
1387 checks for libraries
1388 checks for header files
1390 checks for structures
1391 checks for compiler characteristics
1392 checks for library functions
1393 checks for system services
1394 @code{AC_CONFIG_FILES(@r{[}@var{file@dots{}}@r{]})}
1400 @node autoscan Invocation
1401 @section Using @command{autoscan} to Create @file{configure.ac}
1402 @cindex @command{autoscan}
1404 The @command{autoscan} program can help you create and/or maintain a
1405 @file{configure.ac} file for a software package. @command{autoscan}
1406 examines source files in the directory tree rooted at a directory given
1407 as a command line argument, or the current directory if none is given.
1408 It searches the source files for common portability problems and creates
1409 a file @file{configure.scan} which is a preliminary @file{configure.ac}
1410 for that package, and checks a possibly existing @file{configure.ac} for
1413 When using @command{autoscan} to create a @file{configure.ac}, you
1414 should manually examine @file{configure.scan} before renaming it to
1415 @file{configure.ac}; it probably needs some adjustments.
1416 Occasionally, @command{autoscan} outputs a macro in the wrong order
1417 relative to another macro, so that @command{autoconf} produces a warning;
1418 you need to move such macros manually. Also, if you want the package to
1419 use a configuration header file, you must add a call to
1420 @code{AC_CONFIG_HEADERS} (@pxref{Configuration Headers}). You might
1421 also have to change or add some @code{#if} directives to your program in
1422 order to make it work with Autoconf (@pxref{ifnames Invocation}, for
1423 information about a program that can help with that job).
1425 When using @command{autoscan} to maintain a @file{configure.ac}, simply
1426 consider adding its suggestions. The file @file{autoscan.log}
1427 contains detailed information on why a macro is requested.
1429 @command{autoscan} uses several data files (installed along with Autoconf)
1430 to determine which macros to output when it finds particular symbols in
1431 a package's source files. These data files all have the same format:
1432 each line consists of a symbol, one or more blanks, and the Autoconf macro to
1433 output if that symbol is encountered. Lines starting with @samp{#} are
1436 @command{autoscan} accepts the following options:
1441 Print a summary of the command line options and exit.
1445 Print the version number of Autoconf and exit.
1449 Print the names of the files it examines and the potentially interesting
1450 symbols it finds in them. This output can be voluminous.
1454 Don't remove temporary files.
1456 @item --include=@var{dir}
1458 Append @var{dir} to the include path. Multiple invocations accumulate.
1460 @item --prepend-include=@var{dir}
1462 Prepend @var{dir} to the include path. Multiple invocations accumulate.
1465 @node ifnames Invocation
1466 @section Using @command{ifnames} to List Conditionals
1467 @cindex @command{ifnames}
1469 @command{ifnames} can help you write @file{configure.ac} for a software
1470 package. It prints the identifiers that the package already uses in C
1471 preprocessor conditionals. If a package has already been set up to have
1472 some portability, @command{ifnames} can thus help you figure out what its
1473 @command{configure} needs to check for. It may help fill in some gaps in a
1474 @file{configure.ac} generated by @command{autoscan} (@pxref{autoscan
1477 @command{ifnames} scans all of the C source files named on the command line
1478 (or the standard input, if none are given) and writes to the standard
1479 output a sorted list of all the identifiers that appear in those files
1480 in @code{#if}, @code{#elif}, @code{#ifdef}, or @code{#ifndef}
1481 directives. It prints each identifier on a line, followed by a
1482 space-separated list of the files in which that identifier occurs.
1485 @command{ifnames} accepts the following options:
1490 Print a summary of the command line options and exit.
1494 Print the version number of Autoconf and exit.
1497 @node autoconf Invocation
1498 @section Using @command{autoconf} to Create @command{configure}
1499 @cindex @command{autoconf}
1501 To create @command{configure} from @file{configure.ac}, run the
1502 @command{autoconf} program with no arguments. @command{autoconf} processes
1503 @file{configure.ac} with the M4 macro processor, using the
1504 Autoconf macros. If you give @command{autoconf} an argument, it reads that
1505 file instead of @file{configure.ac} and writes the configuration script
1506 to the standard output instead of to @command{configure}. If you give
1507 @command{autoconf} the argument @option{-}, it reads from the standard
1508 input instead of @file{configure.ac} and writes the configuration script
1509 to the standard output.
1511 The Autoconf macros are defined in several files. Some of the files are
1512 distributed with Autoconf; @command{autoconf} reads them first. Then it
1513 looks for the optional file @file{acsite.m4} in the directory that
1514 contains the distributed Autoconf macro files, and for the optional file
1515 @file{aclocal.m4} in the current directory. Those files can contain
1516 your site's or the package's own Autoconf macro definitions
1517 (@pxref{Writing Autoconf Macros}, for more information). If a macro is
1518 defined in more than one of the files that @command{autoconf} reads, the
1519 last definition it reads overrides the earlier ones.
1521 @command{autoconf} accepts the following options:
1526 Print a summary of the command line options and exit.
1530 Print the version number of Autoconf and exit.
1534 Report processing steps.
1538 Don't remove the temporary files.
1542 Remake @file{configure} even if newer than its input files.
1544 @item --include=@var{dir}
1546 Append @var{dir} to the include path. Multiple invocations accumulate.
1548 @item --prepend-include=@var{dir}
1550 Prepend @var{dir} to the include path. Multiple invocations accumulate.
1552 @item --output=@var{file}
1553 @itemx -o @var{file}
1554 Save output (script or trace) to @var{file}. The file @option{-} stands
1555 for the standard output.
1557 @item --warnings=@var{category}[,@var{category}...]
1558 @itemx -W@var{category}[,@var{category}...]
1560 Enable or disable warnings related to each @var{category}.
1561 @xref{m4_warn}, for a comprehensive list of categories.
1562 Special values include:
1566 Enable all categories of warnings.
1569 Disable all categories of warnings.
1572 Treat all warnings as errors.
1574 @item no-@var{category}
1575 Disable warnings falling into @var{category}.
1578 The environment variable @env{WARNINGS} may also be set to a
1579 comma-separated list of warning categories to enable or disable.
1580 It is interpreted exactly the same way as the argument of
1581 @option{--warnings}, but unknown categories are silently ignored.
1582 The command line takes precedence; for instance, if @env{WARNINGS}
1583 is set to @code{obsolete}, but @option{-Wnone} is given on the
1584 command line, no warnings will be issued.
1586 Some categories of warnings are on by default.
1587 Again, for details see @ref{m4_warn}.
1589 @item --trace=@var{macro}[:@var{format}]
1590 @itemx -t @var{macro}[:@var{format}]
1591 Do not create the @command{configure} script, but list the calls to
1592 @var{macro} according to the @var{format}. Multiple @option{--trace}
1593 arguments can be used to list several macros. Multiple @option{--trace}
1594 arguments for a single macro are not cumulative; instead, you should
1595 just make @var{format} as long as needed.
1597 The @var{format} is a regular string, with newlines if desired, and
1598 several special escape codes. It defaults to @samp{$f:$l:$n:$%}; see
1599 @ref{autom4te Invocation}, for details on the @var{format}.
1601 @item --initialization
1603 By default, @option{--trace} does not trace the initialization of the
1604 Autoconf macros (typically the @code{AC_DEFUN} definitions). This
1605 results in a noticeable speedup, but can be disabled by this option.
1609 It is often necessary to check the content of a @file{configure.ac}
1610 file, but parsing it yourself is extremely fragile and error-prone. It
1611 is suggested that you rely upon @option{--trace} to scan
1612 @file{configure.ac}. For instance, to find the list of variables that
1613 are substituted, use:
1617 $ @kbd{autoconf -t AC_SUBST}
1618 configure.ac:2:AC_SUBST:ECHO_C
1619 configure.ac:2:AC_SUBST:ECHO_N
1620 configure.ac:2:AC_SUBST:ECHO_T
1621 @i{More traces deleted}
1626 The example below highlights the difference between @samp{$@@},
1627 @samp{$*}, and @samp{$%}.
1631 $ @kbd{cat configure.ac}
1632 AC_DEFINE(This, is, [an
1634 $ @kbd{autoconf -t 'AC_DEFINE:@@: $@@}
1641 %: This:is:an [example]
1646 The @var{format} gives you a lot of freedom:
1650 $ @kbd{autoconf -t 'AC_SUBST:$$ac_subst@{"$1"@} = "$f:$l";'}
1651 $ac_subst@{"ECHO_C"@} = "configure.ac:2";
1652 $ac_subst@{"ECHO_N"@} = "configure.ac:2";
1653 $ac_subst@{"ECHO_T"@} = "configure.ac:2";
1654 @i{More traces deleted}
1659 A long @var{separator} can be used to improve the readability of complex
1660 structures, and to ease their parsing (for instance when no single
1661 character is suitable as a separator):
1665 $ @kbd{autoconf -t 'AM_MISSING_PROG:$@{|:::::|@}*'}
1666 ACLOCAL|:::::|aclocal|:::::|$missing_dir
1667 AUTOCONF|:::::|autoconf|:::::|$missing_dir
1668 AUTOMAKE|:::::|automake|:::::|$missing_dir
1669 @i{More traces deleted}
1673 @node autoreconf Invocation
1674 @section Using @command{autoreconf} to Update @command{configure} Scripts
1675 @cindex @command{autoreconf}
1677 Installing the various components of the GNU Build System can be
1678 tedious: running @command{autopoint} for Gettext, @command{automake} for
1679 @file{Makefile.in} etc.@: in each directory. It may be needed either
1680 because some tools such as @command{automake} have been updated on your
1681 system, or because some of the sources such as @file{configure.ac} have
1682 been updated, or finally, simply in order to install the GNU Build
1683 System in a fresh tree.
1685 @command{autoreconf} runs @command{autoconf}, @command{autoheader},
1686 @command{aclocal}, @command{automake}, @command{libtoolize}, @command{intltoolize},
1687 @command{gtkdocize}, and @command{autopoint} (when appropriate) repeatedly
1688 to update the GNU Build System in the specified directories and their
1689 subdirectories (@pxref{Subdirectories}). By default, it only remakes
1690 those files that are older than their sources. The environment variables
1691 @env{AUTOM4TE}, @env{AUTOCONF}, @env{AUTOHEADER}, @env{AUTOMAKE}, @env{ACLOCAL},
1692 @env{AUTOPOINT}, @env{LIBTOOLIZE}, @env{INTLTOOLIZE}, @env{GTKDOCIZE}, @env{M4},
1693 and @env{MAKE} may be used to override the invocation of the respective tools.
1695 If you install a new version of some tool, you can make
1696 @command{autoreconf} remake @emph{all} of the files by giving it the
1697 @option{--force} option.
1699 @xref{Automatic Remaking}, for Make rules to automatically
1700 rebuild @command{configure} scripts when their source files change. That
1701 method handles the timestamps of configuration header templates
1702 properly, but does not pass @option{--autoconf-dir=@var{dir}} or
1703 @option{--localdir=@var{dir}}.
1706 @cindex @command{autopoint}
1707 Gettext supplies the @command{autopoint} command to add translation
1708 infrastructure to a source package. If you use @command{autopoint},
1709 your @file{configure.ac} should invoke @code{AM_GNU_GETTEXT} and
1710 one of @code{AM_GNU_GETTEXT_VERSION(@var{gettext-version})} or
1711 @code{AM_GNU_GETTEXT_REQUIRE_VERSION(@var{min-gettext-version})}.
1712 @xref{autopoint Invocation, , Invoking the @code{autopoint} Program,
1713 gettext, GNU @code{gettext} utilities}, for further details.
1716 @command{autoreconf} accepts the following options:
1721 Print a summary of the command line options and exit.
1725 Print the version number of Autoconf and exit.
1729 Print the name of each directory @command{autoreconf} examines and the
1730 commands it runs. If given two or more times, pass @option{--verbose}
1731 to subordinate tools that support it.
1735 Don't remove the temporary files.
1739 Consider all generated and standard auxiliary files to be obsolete.
1740 This remakes even @file{configure} scripts and configuration headers
1741 that are newer than their input files (@file{configure.ac} and, if
1742 present, @file{aclocal.m4}).
1744 If deemed appropriate, this option triggers calls to @samp{automake
1745 --force-missing}. Passing both @option{--force} and @option{--install}
1746 to @command{autoreconf} will in turn undo any customizations to standard
1747 files. Note that the macro @code{AM_INIT_AUTOMAKE} has some options
1748 which change the set of files considered to be standard.
1752 Install any missing standard auxiliary files in the package. By
1753 default, files are copied; this can be changed with @option{--symlink}.
1755 If deemed appropriate, this option triggers calls to
1756 @samp{automake --add-missing},
1757 @samp{libtoolize}, @samp{autopoint}, etc.
1759 @item --no-recursive
1760 Do not rebuild files in subdirectories to configure (see @ref{Subdirectories},
1761 macro @code{AC_CONFIG_SUBDIRS}).
1765 When used with @option{--install}, install symbolic links to the missing
1766 auxiliary files instead of copying them.
1770 When the directories were configured, update the configuration by
1771 running @samp{./config.status --recheck && ./config.status}, and then
1774 @item --include=@var{dir}
1776 Append @var{dir} to the include path. Multiple invocations accumulate.
1777 Passed on to @command{aclocal}, @command{autoconf} and
1778 @command{autoheader} internally.
1780 @item --prepend-include=@var{dir}
1782 Prepend @var{dir} to the include path. Multiple invocations accumulate.
1783 Passed on to @command{autoconf} and @command{autoheader} internally.
1785 @item --warnings=@var{category}[,@var{category}...]
1786 @itemx -W@var{category}[,@var{category}...]
1788 Enable or disable warnings related to each @var{category}.
1789 @xref{m4_warn}, for a comprehensive list of categories.
1790 Special values include:
1794 Enable all categories of warnings.
1797 Disable all categories of warnings.
1800 Treat all warnings as errors.
1802 @item no-@var{category}
1803 Disable warnings falling into @var{category}.
1806 The environment variable @env{WARNINGS} may also be set to a
1807 comma-separated list of warning categories to enable or disable.
1808 It is interpreted exactly the same way as the argument of
1809 @option{--warnings}, but unknown categories are silently ignored.
1810 The command line takes precedence; for instance, if @env{WARNINGS}
1811 is set to @code{obsolete}, but @option{-Wnone} is given on the
1812 command line, no warnings will be issued.
1814 Some categories of warnings are on by default.
1815 Again, for details see @ref{m4_warn}.
1818 If you want @command{autoreconf} to pass flags that are not listed here
1819 on to @command{aclocal}, set @code{ACLOCAL_AMFLAGS} in your @file{Makefile.am}.
1820 Due to a limitation in the Autoconf implementation these flags currently
1821 must be set on a single line in @file{Makefile.am}, without any
1822 backslash-newlines. Also, be aware that future Automake releases might
1823 start flagging @code{ACLOCAL_AMFLAGS} as obsolescent, or even remove
1826 @c ========================================= Initialization and Output Files.
1829 @chapter Initialization and Output Files
1831 Autoconf-generated @command{configure} scripts need some information about
1832 how to initialize, such as how to find the package's source files and
1833 about the output files to produce. The following sections describe the
1834 initialization and the creation of output files.
1837 * Initializing configure:: Option processing etc.
1838 * Versioning:: Dealing with Autoconf versions
1839 * Notices:: Copyright, version numbers in @command{configure}
1840 * Input:: Where Autoconf should find files
1841 * Output:: Outputting results from the configuration
1842 * Configuration Actions:: Preparing the output based on results
1843 * Configuration Files:: Creating output files
1844 * Makefile Substitutions:: Using output variables in makefiles
1845 * Configuration Headers:: Creating a configuration header file
1846 * Configuration Commands:: Running arbitrary instantiation commands
1847 * Configuration Links:: Links depending on the configuration
1848 * Subdirectories:: Configuring independent packages together
1849 * Default Prefix:: Changing the default installation prefix
1852 @node Initializing configure
1853 @section Initializing @command{configure}
1855 Every @command{configure} script must call @code{AC_INIT} before doing
1856 anything else that produces output. Calls to silent macros, such as
1857 @code{AC_DEFUN}, may also occur prior to @code{AC_INIT}, although these
1858 are generally used via @file{aclocal.m4}, since that is implicitly
1859 included before the start of @file{configure.ac}. The only other
1860 required macro is @code{AC_OUTPUT} (@pxref{Output}).
1863 @defmac AC_INIT (@var{package}, @var{version}, @ovar{bug-report}, @
1864 @ovar{tarname}, @ovar{url})
1866 Process any command-line arguments and perform initialization
1869 Set the name of the @var{package} and its @var{version}. These are
1870 typically used in @option{--version} support, including that of
1871 @command{configure}. The optional argument @var{bug-report} should be
1872 the email to which users should send bug reports. The package
1873 @var{tarname} differs from @var{package}: the latter designates the full
1874 package name (e.g., @samp{GNU Autoconf}), while the former is meant for
1875 distribution tar ball names (e.g., @samp{autoconf}). It defaults to
1876 @var{package} with @samp{GNU } stripped, lower-cased, and all characters
1877 other than alphanumerics and underscores are changed to @samp{-}. If
1878 provided, @var{url} should be the home page for the package.
1880 Leading and trailing whitespace is stripped from all the arguments to
1881 @code{AC_INIT}, and interior whitespace is collapsed to a single space.
1882 This means that, for instance, if you want to put several email
1883 addresses in @var{bug-report}, you can put each one on its own line:
1887 # We keep having problems with the mail hosting for
1888 # gnomovision.example, so give people an alternative.
1889 AC_INIT([Gnomovision], [17.0.1], [
1890 bugs@@gnomovision.example
1891 or gnomo-bugs@@reliable-email.example
1896 The arguments to @code{AC_INIT} may be computed by M4, when
1897 @command{autoconf} is run. For instance, if you want to include the
1898 package's version number in the @var{tarname}, but you don't want to
1899 repeat it, you can use a helper macro:
1903 m4_define([gnomo_VERSION], [17.0.1])
1904 AC_INIT([Gnomovision],
1905 m4_defn([gnomo_VERSION]),
1906 [bugs@@gnomovision.example],
1907 [gnomo-]m4_defn([gnomo_VERSION]))
1911 This uses @code{m4_defn} to produce the expansion of
1912 @code{gnomo_VERSION} @emph{as a quoted string}, so that if there happen
1913 to be any more M4 macro names in @code{gnomo_VERSION}, they will not be
1914 expanded. @xref{Defn,,Renaming Macros,m4,GNU m4 macro processor}.
1916 Continuing this example, if you don't want to embed the version number
1917 in @file{configure.ac} at all, you can use @code{m4_esyscmd} to look it
1918 up somewhere else when @command{autoconf} is run:
1922 m4_define([gnomo_VERSION],
1923 m4_esyscmd([build-aux/git-version-gen .tarball-version]))
1924 AC_INIT([Gnomovision],
1925 m4_defn([gnomo_VERSION]),
1926 [bugs@@gnomovision.example],
1927 [gnomo-]m4_defn([gnomo_VERSION]))
1931 This uses the utility script @command{git-version-gen} to look up
1932 the package's version in its version control metadata. This script
1933 is part of Gnulib (@pxref{Gnulib}).
1935 The arguments to @code{AC_INIT} are written into @file{configure} in
1936 several different places. Therefore, we strongly recommend that you
1937 write any M4 logic in @code{AC_INIT} arguments to be evaluated
1938 @emph{before} @code{AC_INIT} itself is evaluated. For instance, in the
1939 above example, the second argument to @code{m4_define} is @emph{not}
1940 quoted, so the @code{m4_esyscmd} is evaluated only once, and
1941 @code{gnomo_VERSION} is defined to the output of the command. If the
1942 second argument to @code{m4_define} were quoted, @code{m4_esyscmd} would
1943 be evaluated each time the @var{version} or @var{tarname} arguments were
1944 written to @file{configure}, and the command would be run repeatedly.
1946 In some of the places where the arguments to @code{AC_INIT} are used,
1947 within @file{configure}, shell evaluation cannot happen. Therefore, the
1948 arguments to @code{AC_INIT} may @emph{not} be computed when
1949 @command{configure} is run. If they contain any construct that isn't
1950 always treated as literal by the shell (e.g.@: variable expansions),
1951 @command{autoconf} will issue an error.
1953 The @var{tarname} argument is used to construct filenames. It should
1954 not contain wildcard characters, white space, or anything else that
1955 could be troublesome as part of a file or directory name.
1957 Some of M4's active characters (notably parentheses, square brackets,
1958 @samp{,} and @samp{#}) commonly appear in URLs and lists of email
1959 addresses. If any of these characters appear in an argument to AC_INIT,
1960 that argument will probably need to be double-quoted to avoid errors
1961 and mistranscriptions. @xref{M4 Quotation}.
1963 The following M4 macros (e.g., @code{AC_PACKAGE_NAME}), output variables
1964 (e.g., @code{PACKAGE_NAME}), and preprocessor symbols (e.g.,
1965 @code{PACKAGE_NAME}), are defined by @code{AC_INIT}:
1968 @item @code{AC_PACKAGE_NAME}, @code{PACKAGE_NAME}
1969 @acindex{PACKAGE_NAME}
1970 @ovindex PACKAGE_NAME
1971 @cvindex PACKAGE_NAME
1972 Exactly @var{package}.
1974 @item @code{AC_PACKAGE_TARNAME}, @code{PACKAGE_TARNAME}
1975 @acindex{PACKAGE_TARNAME}
1976 @ovindex PACKAGE_TARNAME
1977 @cvindex PACKAGE_TARNAME
1978 Exactly @var{tarname}, possibly generated from @var{package}.
1980 @item @code{AC_PACKAGE_VERSION}, @code{PACKAGE_VERSION}
1981 @acindex{PACKAGE_VERSION}
1982 @ovindex PACKAGE_VERSION
1983 @cvindex PACKAGE_VERSION
1984 Exactly @var{version}.
1986 @item @code{AC_PACKAGE_STRING}, @code{PACKAGE_STRING}
1987 @acindex{PACKAGE_STRING}
1988 @ovindex PACKAGE_STRING
1989 @cvindex PACKAGE_STRING
1990 Exactly @samp{@var{package} @var{version}}.
1992 @item @code{AC_PACKAGE_BUGREPORT}, @code{PACKAGE_BUGREPORT}
1993 @acindex{PACKAGE_BUGREPORT}
1994 @ovindex PACKAGE_BUGREPORT
1995 @cvindex PACKAGE_BUGREPORT
1996 Exactly @var{bug-report}, if one was provided. Typically an email
1997 address, or URL to a bug management web page.
1999 @item @code{AC_PACKAGE_URL}, @code{PACKAGE_URL}
2000 @acindex{PACKAGE_URL}
2001 @ovindex PACKAGE_URL
2002 @cvindex PACKAGE_URL
2003 Exactly @var{url}, if one was provided. If @var{url} was empty, but
2004 @var{package} begins with @samp{GNU }, then this defaults to
2005 @samp{https://@/www.gnu.org/@/software/@/@var{tarname}/}, otherwise, no URL is
2010 If your @command{configure} script does its own option processing, it
2011 should inspect @samp{$@@} or @samp{$*} immediately after calling
2012 @code{AC_INIT}, because other Autoconf macros liberally use the
2013 @command{set} command to process strings, and this has the side effect
2014 of updating @samp{$@@} and @samp{$*}. However, we suggest that you use
2015 standard macros like @code{AC_ARG_ENABLE} instead of attempting to
2016 implement your own option processing. @xref{Site Configuration}.
2019 @section Dealing with Autoconf versions
2020 @cindex Autoconf version
2021 @cindex version, Autoconf
2023 The following optional macros can be used to help choose the minimum
2024 version of Autoconf that can successfully compile a given
2025 @file{configure.ac}.
2027 @defmac AC_PREREQ (@var{version})
2030 Ensure that a recent enough version of Autoconf is being used. If the
2031 version of Autoconf being used to create @command{configure} is
2032 earlier than @var{version}, print an error message to the standard
2033 error output and exit with failure (exit status is 63). For example:
2036 AC_PREREQ([@value{VERSION}])
2039 This macro may be used before @code{AC_INIT}.
2042 @defmac AC_AUTOCONF_VERSION
2043 @acindex{AUTOCONF_VERSION}
2044 This macro was introduced in Autoconf 2.62. It identifies the version
2045 of Autoconf that is currently parsing the input file, in a format
2046 suitable for @code{m4_version_compare} (@pxref{m4_version_compare}); in
2047 other words, for this release of Autoconf, its value is
2048 @samp{@value{VERSION}}. One potential use of this macro is for writing
2049 conditional fallbacks based on when a feature was added to Autoconf,
2050 rather than using @code{AC_PREREQ} to require the newer version of
2051 Autoconf. However, remember that the Autoconf philosophy favors feature
2052 checks over version checks.
2054 You should not expand this macro directly; use
2055 @samp{m4_defn([AC_AUTOCONF_VERSION])} instead. This is because some
2057 have a beta version of Autoconf installed, with arbitrary letters
2058 included in its version string. This means it is possible for the
2059 version string to contain the name of a defined macro, such that
2060 expanding @code{AC_AUTOCONF_VERSION} would trigger the expansion of that
2061 macro during rescanning, and change the version string to be different
2062 than what you intended to check.
2066 @section Notices in @command{configure}
2067 @cindex Notices in @command{configure}
2069 The following macros manage version numbers for @command{configure}
2070 scripts. Using them is optional.
2072 @defmac AC_COPYRIGHT (@var{copyright-notice})
2074 @cindex Copyright Notice
2075 State that, in addition to the Free Software Foundation's copyright on
2076 the Autoconf macros, parts of your @command{configure} are covered by the
2077 @var{copyright-notice}.
2079 The @var{copyright-notice} shows up in both the head of
2080 @command{configure} and in @samp{configure --version}.
2084 @defmac AC_REVISION (@var{revision-info})
2087 Copy revision stamp @var{revision-info} into the @command{configure}
2088 script, with any dollar signs or double-quotes removed. This macro lets
2089 you put a revision stamp from @file{configure.ac} into @command{configure}
2090 without RCS or CVS changing it when you check in
2091 @command{configure}. That way, you can determine easily which revision of
2092 @file{configure.ac} a particular @command{configure} corresponds to.
2094 For example, this line in @file{configure.ac}:
2096 @c The @w prevents RCS from changing the example in the manual.
2098 AC_REVISION([@w{$}Revision: 1.30 $])
2102 produces this in @command{configure}:
2106 # From configure.ac Revision: 1.30
2112 @section Configure Input: Source Code, Macros, and Auxiliary Files
2114 The following macros help you manage the contents of your source tree.
2116 @anchor{AC_CONFIG_SRCDIR}
2117 @defmac AC_CONFIG_SRCDIR (@var{unique-file-in-source-dir})
2118 @acindex{CONFIG_SRCDIR}
2119 Distinguish this package's source directory from other source
2120 directories that might happen to exist in the file system.
2121 @var{unique-file-in-source-dir} should name a file that is unique to
2122 this package. @command{configure} will verify that this file exists in
2123 @file{@var{srcdir}}, before it runs any other checks.
2125 Use of this macro is strongly recommended. It protects against people
2126 accidentally specifying the wrong directory with @option{--srcdir}.
2127 @xref{configure Invocation}, for more information.
2130 Packages that use @command{aclocal} to generate @file{aclocal.m4}
2131 should declare where local macros can be found using
2132 @code{AC_CONFIG_MACRO_DIRS}.
2134 @defmac AC_CONFIG_MACRO_DIRS (@var{dir1} [@var{dir2} ... @var{dirN}])
2135 @defmacx AC_CONFIG_MACRO_DIR (@var{dir})
2136 @acindex{CONFIG_MACRO_DIRS}
2137 @acindex{CONFIG_MACRO_DIR}
2138 @acindex{CONFIG_MACRO_DIR_TRACE}
2139 Specify the given directories as the location of additional local Autoconf
2140 macros. These macros are intended for use by commands like
2141 @command{autoreconf} or @command{aclocal} that trace macro calls; they should
2142 be called directly from @file{configure.ac} so that tools that install
2143 macros for @command{aclocal} can find the macros' declarations. Tools
2144 that want to learn which directories have been selected should trace
2145 @code{AC_CONFIG_MACRO_DIR_TRACE}, which will be called once per directory.
2147 AC_CONFIG_MACRO_DIRS is the preferred form, and can be called multiple
2148 times and with multiple arguments; in such cases, directories in earlier
2149 calls are expected to be searched before directories in later calls, and
2150 directories appearing in the same call are expected to be searched in
2151 the order in which they appear in the call. For historical reasons, the
2152 macro AC_CONFIG_MACRO_DIR can also be used once, if it appears first,
2153 for tools such as older @command{libtool} that weren't prepared to
2154 handle multiple directories. For example, a usage like
2157 AC_CONFIG_MACRO_DIR([dir1])
2158 AC_CONFIG_MACRO_DIRS([dir2])
2159 AC_CONFIG_MACRO_DIRS([dir3 dir4])
2162 will cause the trace of AC_CONFIG_MACRO_DIR_TRACE to appear four times,
2163 and should cause the directories to be searched in this order:
2164 @samp{dir1 dir2 dir3 dir4}.
2166 Note that if you use @command{aclocal} from an Automake release prior to
2167 1.13 to generate @file{aclocal.m4}, you must also set
2168 @code{ACLOCAL_AMFLAGS = -I @var{dir1} [-I @var{dir2} ... -I @var{dirN}]}
2169 in your top-level @file{Makefile.am}. Due to a limitation in
2170 the Autoconf implementation of @command{autoreconf}, these include
2171 directives currently must be set on a single line in @file{Makefile.am},
2172 without any backslash-newlines.
2175 @prindex @command{config.guess}
2176 @prindex @command{config.sub}
2177 @prindex @command{install-sh}
2179 Some Autoconf macros require auxiliary scripts. @code{AC_PROG_INSTALL}
2180 (@pxref{Particular Programs}) requires a
2181 fallback implementation of @command{install} called @file{install-sh},
2182 and the @code{AC_CANONICAL} macros (@pxref{Manual Configuration})
2183 require the system-identification scripts @file{config.sub} and
2184 @file{config.guess}. Third-party tools, such as Automake and Libtool,
2185 may require additional auxiliary scripts.
2187 By default, @command{configure} looks for these scripts next to itself,
2188 in @file{@var{srcdir}}. For convenience when working with subdirectories
2189 with their own configure scripts (@pxref{Subdirectories}), if the
2190 scripts are not in @file{@var{srcdir}} it will also look in
2191 @file{@var{srcdir}/..} and @file{@var{srcdir}/../..}. All of the
2192 scripts must be found in the same directory.
2194 If these default locations are not adequate, or simply to reduce clutter
2195 at the top level of the source tree, packages can use
2196 @code{AC_CONFIG_AUX_DIR} to declare where to look for auxiliary scripts.
2198 @defmac AC_CONFIG_AUX_DIR (@var{dir})
2199 @acindex{CONFIG_AUX_DIR}
2200 Look for auxiliary scripts in @var{dir}. Normally, @var{dir} should be a
2201 relative path, which is taken as relative to @file{@var{srcdir}}.
2202 If @var{dir} is an absolute path or contains shell variables, however,
2205 When the goal of using @code{AC_CONFIG_AUX_DIR} is to reduce clutter at
2206 the top level of the source tree, the conventional name for @var{dir} is
2207 @file{build-aux}. If you need portability to DOS variants, do not name
2208 the auxiliary directory @file{aux}. @xref{File System Conventions}.
2211 @defmac AC_REQUIRE_AUX_FILE (@var{file})
2212 @acindex{REQUIRE_AUX_FILE}
2214 Declare that @var{file} is an auxiliary script needed by this configure
2215 script, and set the shell variable @code{ac_aux_dir} to the directory
2216 where it can be found. The value of @code{ac_aux_dir} is guaranteed to
2217 end with a @samp{/}.
2219 Macros that need auxiliary scripts must use this macro to register each
2223 @command{configure} checks for all the auxiliary scripts it needs on
2224 startup, and exits with an error if any are missing.
2226 @command{autoreconf} also detects missing auxiliary scripts. When used
2227 with the @option{--install} option, @command{autoreconf} will try to add
2228 missing scripts to the directory specified by @code{AC_CONFIG_AUX_DIR},
2229 or to the top level of the source tree if @code{AC_CONFIG_AUX_DIR} was
2230 not used. It can always do this for the scripts needed by Autoconf core
2231 macros: @file{install-sh}, @file{config.sub}, and @file{config.guess}.
2232 Many other commonly-needed scripts are installed by the third-party
2233 tools that @command{autoreconf} knows how to run, such as @file{missing}
2234 for Automake and @file{ltmain.sh} for Libtool.
2236 If you are using Automake, auxiliary scripts will automatically be
2237 included in the tarball created by @command{make dist}. If you are
2238 not using Automake you will need to arrange for auxiliary scripts to
2239 be included in tarballs yourself. Auxiliary scripts should normally
2240 @emph{not} be checked into a version control system, for the same
2241 reasons that @command{configure} shouldn't be.
2243 The scripts needed by Autoconf core macros can be found in
2244 @file{$(datadir)/autoconf/build-aux} of the Autoconf installation
2245 (@pxref{Installation Directory Variables}).
2246 @file{install-sh} can be downloaded from
2247 @url{https://git.savannah.gnu.org/cgit/automake.git/plain/lib/install-sh}.
2248 @file{config.sub} and @file{config.guess} can be downloaded from
2249 @url{https://git.savannah.gnu.org/cgit/config.git/tree/}.
2252 @section Outputting Files
2253 @cindex Outputting files
2255 Every Autoconf script, e.g., @file{configure.ac}, should finish by
2256 calling @code{AC_OUTPUT}. That is the macro that generates and runs
2257 @file{config.status}, which in turn creates the makefiles and any
2258 other files resulting from configuration. This is the only required
2259 macro besides @code{AC_INIT} (@pxref{Input}).
2264 @cindex Instantiation
2265 Generate @file{config.status} and launch it. Call this macro once, at
2266 the end of @file{configure.ac}.
2268 @file{config.status} performs all the configuration actions: all the
2269 output files (see @ref{Configuration Files}, macro
2270 @code{AC_CONFIG_FILES}), header files (see @ref{Configuration Headers},
2271 macro @code{AC_CONFIG_HEADERS}), commands (see @ref{Configuration
2272 Commands}, macro @code{AC_CONFIG_COMMANDS}), links (see
2273 @ref{Configuration Links}, macro @code{AC_CONFIG_LINKS}), subdirectories
2274 to configure (see @ref{Subdirectories}, macro @code{AC_CONFIG_SUBDIRS})
2277 The location of your @code{AC_OUTPUT} invocation is the exact point
2278 where configuration actions are taken: any code afterwards is
2279 executed by @command{configure} once @command{config.status} was run. If
2280 you want to bind actions to @command{config.status} itself
2281 (independently of whether @command{configure} is being run), see
2282 @ref{Configuration Commands, , Running Arbitrary Configuration
2286 Historically, the usage of @code{AC_OUTPUT} was somewhat different.
2287 @xref{Obsolete Macros}, for a description of the arguments that
2288 @code{AC_OUTPUT} used to support.
2291 If you run @command{make} in subdirectories, you should run it using the
2292 @command{make} variable @code{MAKE}. Most versions of @command{make} set
2293 @code{MAKE} to the name of the @command{make} program plus any options it
2294 was given. (But many do not include in it the values of any variables
2295 set on the command line, so those are not passed on automatically.)
2296 Some old versions of @command{make} do not set this variable. The
2297 following macro allows you to use it even with those versions.
2299 @anchor{AC_PROG_MAKE_SET}
2300 @defmac AC_PROG_MAKE_SET
2301 @acindex{PROG_MAKE_SET}
2303 If the Make command, @code{$MAKE} if set or else @samp{make}, predefines
2304 @code{$(MAKE)}, define output variable @code{SET_MAKE} to be empty.
2305 Otherwise, define @code{SET_MAKE} to a macro definition that sets
2306 @code{$(MAKE)}, such as @samp{MAKE=make}. Calls @code{AC_SUBST} for
2310 If you use this macro, place a line like this in each @file{Makefile.in}
2311 that runs @command{MAKE} on other directories:
2319 @node Configuration Actions
2320 @section Performing Configuration Actions
2321 @cindex Configuration actions
2323 @file{configure} is designed so that it appears to do everything itself,
2324 but there is actually a hidden slave: @file{config.status}.
2325 @file{configure} is in charge of examining your system, but it is
2326 @file{config.status} that actually takes the proper actions based on the
2327 results of @file{configure}. The most typical task of
2328 @file{config.status} is to @emph{instantiate} files.
2330 @acindex{CONFIG_@var{ITEMS}}
2331 This section describes the common behavior of the four standard
2332 instantiating macros: @code{AC_CONFIG_FILES}, @code{AC_CONFIG_HEADERS},
2333 @code{AC_CONFIG_COMMANDS} and @code{AC_CONFIG_LINKS}. They all
2334 have this prototype:
2336 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
2339 AC_CONFIG_@var{ITEMS}(@var{tag}@dots{}, @r{[}@var{commands}@r{]}, @r{[}@var{init-cmds}@r{]})
2343 where the arguments are:
2347 A blank-or-newline-separated list of tags, which are typically the names of
2348 the files to instantiate.
2350 You are encouraged to use literals as @var{tags}. In particular, you
2354 AS_IF([@dots{}], [my_foos="$my_foos fooo"])
2355 AS_IF([@dots{}], [my_foos="$my_foos foooo"])
2356 AC_CONFIG_@var{ITEMS}([$my_foos])
2360 and use this instead:
2363 AS_IF([@dots{}], [AC_CONFIG_@var{ITEMS}([fooo])])
2364 AS_IF([@dots{}], [AC_CONFIG_@var{ITEMS}([foooo])])
2367 The macros @code{AC_CONFIG_FILES} and @code{AC_CONFIG_HEADERS} use
2368 special @var{tag} values: they may have the form @samp{@var{output}} or
2369 @samp{@var{output}:@var{inputs}}. The file @var{output} is instantiated
2370 from its templates, @var{inputs} (defaulting to @samp{@var{output}.in}).
2372 @samp{AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk])},
2373 for example, asks for
2374 the creation of the file @file{Makefile} that contains the expansion of the
2375 output variables in the concatenation of @file{boiler/top.mk} and
2376 @file{boiler/bot.mk}.
2378 The special value @samp{-} might be used to denote the standard output
2379 when used in @var{output}, or the standard input when used in the
2380 @var{inputs}. You most probably don't need to use this in
2381 @file{configure.ac}, but it is convenient when using the command line
2382 interface of @file{./config.status}, see @ref{config.status Invocation},
2385 The @var{inputs} may be absolute or relative file names. In the latter
2386 case they are first looked for in the build tree, and then in the source
2387 tree. Input files should be text files, and a line length below 2000
2388 bytes should be safe.
2391 Shell commands output literally into @file{config.status}, and
2392 associated with a tag that the user can use to tell @file{config.status}
2393 which commands to run. The commands are run each time a @var{tag}
2394 request is given to @file{config.status}, typically each time the file
2395 @file{@var{tag}} is created.
2397 The variables set during the execution of @command{configure} are
2398 @emph{not} available here: you first need to set them via the
2399 @var{init-cmds}. Nonetheless the following variables are pre-computed:
2404 The name of the top source directory, assuming that the working
2405 directory is the top build directory. This
2406 is what @command{configure}'s @option{--srcdir} option sets.
2409 @vrindex ac_top_srcdir
2410 The name of the top source directory, assuming that the working
2411 directory is the current build directory.
2413 @item ac_top_build_prefix
2414 @vrindex ac_top_build_prefix
2415 The name of the top build directory, assuming that the working
2416 directory is the current build directory.
2417 It can be empty, or else ends with a slash, so that you may concatenate
2422 The name of the corresponding source directory, assuming that the
2423 working directory is the current build directory.
2427 The name of a temporary directory within the build tree, which you
2428 can use if you need to create additional temporary files. The
2429 directory is cleaned up when @command{config.status} is done or
2430 interrupted. Please use package-specific file name prefixes to
2431 avoid clashing with files that @command{config.status} may use
2436 The @dfn{current} directory refers to the directory (or
2437 pseudo-directory) containing the input part of @var{tags}. For
2441 AC_CONFIG_COMMANDS([deep/dir/out:in/in.in], [@dots{}], [@dots{}])
2445 with @option{--srcdir=../package} produces the following values:
2448 # Argument of --srcdir
2450 # Reversing deep/dir
2451 ac_top_build_prefix='../../'
2452 # Concatenation of $ac_top_build_prefix and srcdir
2453 ac_top_srcdir='../../../package'
2454 # Concatenation of $ac_top_srcdir and deep/dir
2455 ac_srcdir='../../../package/deep/dir'
2459 independently of @samp{in/in.in}.
2462 Shell commands output @emph{unquoted} near the beginning of
2463 @file{config.status}, and executed each time @file{config.status} runs
2464 (regardless of the tag). Because they are unquoted, for example,
2465 @samp{$var} is output as the value of @code{var}. @var{init-cmds}
2466 is typically used by @file{configure} to give @file{config.status} some
2467 variables it needs to run the @var{commands}.
2469 You should be extremely cautious in your variable names: all the
2470 @var{init-cmds} share the same name space and may overwrite each other
2471 in unpredictable ways. Sorry@enddots{}
2474 All these macros can be called multiple times, with different
2475 @var{tag} values, of course!
2478 @node Configuration Files
2479 @section Creating Configuration Files
2480 @cindex Creating configuration files
2481 @cindex Configuration file creation
2483 Be sure to read the previous section, @ref{Configuration Actions}.
2485 @anchor{AC_CONFIG_FILES}
2486 @defmac AC_CONFIG_FILES (@var{file}@dots{}, @ovar{cmds}, @ovar{init-cmds})
2487 @acindex{CONFIG_FILES}
2488 Make @code{AC_OUTPUT} create each @file{@var{file}} by copying an input
2489 file (by default @file{@var{file}.in}), substituting the output variable
2491 @c Before we used to have this feature, which was later rejected
2492 @c because it complicates the writing of makefiles:
2493 @c If the file would be unchanged, it is left untouched, to preserve
2495 This macro is one of the instantiating macros; see @ref{Configuration
2496 Actions}. @xref{Makefile Substitutions}, for more information on using
2497 output variables. @xref{Setting Output Variables}, for more information
2498 on creating them. This macro creates the directory that the file is in
2499 if it doesn't exist. Usually, makefiles are created this way,
2500 but other files, such as @file{.gdbinit}, can be specified as well.
2502 Typical calls to @code{AC_CONFIG_FILES} look like this:
2505 AC_CONFIG_FILES([Makefile src/Makefile man/Makefile X/Imakefile])
2506 AC_CONFIG_FILES([autoconf], [chmod +x autoconf])
2509 You can override an input file name by appending to @var{file} a
2510 colon-separated list of input files. Examples:
2513 AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk]
2514 [lib/Makefile:boiler/lib.mk])
2518 Doing this allows you to keep your file names acceptable to
2520 to prepend and/or append boilerplate to the file.
2522 The @var{file} names should not contain shell metacharacters.
2523 @xref{Special Chars in Variables}.
2528 @node Makefile Substitutions
2529 @section Substitutions in Makefiles
2530 @cindex Substitutions in makefiles
2531 @cindex Makefile substitutions
2533 Each subdirectory in a distribution that contains something to be
2534 compiled or installed should come with a file @file{Makefile.in}, from
2535 which @command{configure} creates a file @file{Makefile} in that directory.
2536 To create @file{Makefile}, @command{configure} performs a simple variable
2537 substitution, replacing occurrences of @samp{@@@var{variable}@@} in
2538 @file{Makefile.in} with the value that @command{configure} has determined
2539 for that variable. Variables that are substituted into output files in
2540 this way are called @dfn{output variables}. They are ordinary shell
2541 variables that are set in @command{configure}. To make @command{configure}
2542 substitute a particular variable into the output files, the macro
2543 @code{AC_SUBST} must be called with that variable name as an argument.
2544 Any occurrences of @samp{@@@var{variable}@@} for other variables are
2545 left unchanged. @xref{Setting Output Variables}, for more information
2546 on creating output variables with @code{AC_SUBST}.
2548 A software package that uses a @command{configure} script should be
2549 distributed with a file @file{Makefile.in}, but no makefile; that
2550 way, the user has to properly configure the package for the local system
2551 before compiling it.
2553 @xref{Makefile Conventions, , Makefile Conventions, standards, The
2554 GNU Coding Standards}, for more information on what to put in
2558 * Preset Output Variables:: Output variables that are always set
2559 * Installation Directory Variables:: Other preset output variables
2560 * Changed Directory Variables:: Warnings about @file{datarootdir}
2561 * Build Directories:: Supporting multiple concurrent compiles
2562 * Automatic Remaking:: Makefile rules for configuring
2565 @node Preset Output Variables
2566 @subsection Preset Output Variables
2567 @cindex Output variables
2569 Some output variables are preset by the Autoconf macros. Some of the
2570 Autoconf macros set additional output variables, which are mentioned in
2571 the descriptions for those macros. @xref{Output Variable Index}, for a
2572 complete list of output variables. @xref{Installation Directory
2573 Variables}, for the list of the preset ones related to installation
2574 directories. Below are listed the other preset ones, many of which are
2575 precious variables (@pxref{Setting Output Variables},
2578 The preset variables which are available during @file{config.status}
2579 (@pxref{Configuration Actions}) may also be used during
2580 @command{configure} tests. For example, it is permissible to reference
2581 @samp{$srcdir} when constructing a list of directories to pass via
2582 the @option{-I} option during a compiler feature check. When used in this
2583 manner, coupled with the fact that @command{configure} is always run
2584 from the top build directory, it is sufficient to use just
2585 @samp{$srcdir} instead of @samp{$top_srcdir}.
2587 @c Just say no to ASCII sorting! We're humans, not computers.
2588 @c These variables are listed as they would be in a dictionary:
2596 Debugging and optimization options for the C compiler. If it is not set
2597 in the environment when @command{configure} runs, the default value is set
2598 when you call @code{AC_PROG_CC} (or empty if you don't). @command{configure}
2599 uses this variable when compiling or linking programs to test for C features.
2601 If a compiler option affects only the behavior of the preprocessor
2602 (e.g., @option{-D@var{name}}), it should be put into @code{CPPFLAGS}
2603 instead. If it affects only the linker (e.g., @option{-L@var{directory}}),
2604 it should be put into @code{LDFLAGS} instead. If it
2605 affects only the compiler proper, @code{CFLAGS} is the natural home for
2606 it. If an option affects multiple phases of the compiler, though,
2611 If an option selects a 32-bit or 64-bit build on a bi-arch system, it
2612 must be put direcly into @code{CC}, e.g., @code{CC='gcc -m64'}. This is
2613 necessary for @code{config.guess} to work right.
2615 Otherwise one approach is to put the option into @code{CC}. Another is
2616 to put it into both @code{CPPFLAGS} and @code{LDFLAGS}, but not into
2620 However, remember that some @file{Makefile} variables are reserved by
2621 the GNU Coding Standards for the use of the ``user''---the person
2622 building the package. For instance, @code{CFLAGS} is one such variable.
2624 Sometimes package developers are tempted to set user variables such as
2625 @code{CFLAGS} because it appears to make their job easier. However, the
2626 package itself should never set a user variable, particularly not to
2627 include switches that are required for proper compilation of the
2628 package. Since these variables are documented as being for the package
2629 builder, that person rightfully expects to be able to override any of
2630 these variables at build time. If the package developer needs to add
2631 switches without interfering with the user, the proper way to do that is
2632 to introduce an additional variable. Automake makes this easy by
2633 introducing @code{AM_CFLAGS} (@pxref{Flag Variables Ordering, , ,
2634 automake, GNU Automake}), but the concept is the same even if
2635 Automake is not used.
2638 @defvar configure_input
2639 @ovindex configure_input
2640 A comment saying that the file was generated automatically by
2641 @command{configure} and giving the name of the input file.
2642 @code{AC_OUTPUT} adds a comment line containing this variable to the top
2643 of every makefile it creates. For other files, you should
2644 reference this variable in a comment at the top of each input file. For
2645 example, an input shell script should begin like this:
2649 # @@configure_input@@
2653 The presence of that line also reminds people editing the file that it
2654 needs to be processed by @command{configure} in order to be used.
2660 Preprocessor options for the C, C++, Objective C, and Objective C++
2661 preprocessors and compilers. If
2662 it is not set in the environment when @command{configure} runs, the default
2663 value is empty. @command{configure} uses this variable when preprocessing
2664 or compiling programs to test for C, C++, Objective C, and Objective C++
2667 This variable's contents should contain options like @option{-I},
2668 @option{-D}, and @option{-U} that affect only the behavior of the
2669 preprocessor. Please see the explanation of @code{CFLAGS} for what you
2670 can do if an option affects other phases of the compiler as well.
2672 Currently, @command{configure} always links as part of a single
2673 invocation of the compiler that also preprocesses and compiles, so it
2674 uses this variable also when linking programs. However, it is unwise to
2675 depend on this behavior because the GNU Coding Standards do
2676 not require it and many packages do not use @code{CPPFLAGS} when linking
2679 @xref{Special Chars in Variables}, for limitations that @code{CPPFLAGS}
2686 Debugging and optimization options for the C++ compiler. It acts like
2687 @code{CFLAGS}, but for C++ instead of C.
2692 @option{-D} options to pass to the C compiler. If @code{AC_CONFIG_HEADERS}
2693 is called, @command{configure} replaces @samp{@@DEFS@@} with
2694 @option{-DHAVE_CONFIG_H} instead (@pxref{Configuration Headers}). This
2695 variable is not defined while @command{configure} is performing its tests,
2696 only when creating the output files. @xref{Setting Output Variables}, for
2697 how to check the results of previous tests.
2706 How does one suppress the trailing newline from @command{echo} for
2707 question-answer message pairs? These variables provide a way:
2710 echo $ECHO_N "And the winner is... $ECHO_C"
2712 echo "$@{ECHO_T@}dead."
2716 Some old and uncommon @command{echo} implementations offer no means to
2717 achieve this, in which case @code{ECHO_T} is set to tab. You might not
2724 Debugging and optimization options for the Erlang compiler. If it is not set
2725 in the environment when @command{configure} runs, the default value is empty.
2726 @command{configure} uses this variable when compiling
2727 programs to test for Erlang features.
2733 Debugging and optimization options for the Fortran compiler. If it
2734 is not set in the environment when @command{configure} runs, the default
2735 value is set when you call @code{AC_PROG_FC} (or empty if you don't).
2736 @command{configure} uses this variable when compiling or linking
2737 programs to test for Fortran features.
2743 Debugging and optimization options for the Fortran 77 compiler. If it
2744 is not set in the environment when @command{configure} runs, the default
2745 value is set when you call @code{AC_PROG_F77} (or empty if you don't).
2746 @command{configure} uses this variable when compiling or linking
2747 programs to test for Fortran 77 features.
2753 Options for the linker. If it is not set
2754 in the environment when @command{configure} runs, the default value is empty.
2755 @command{configure} uses this variable when linking programs to test for
2756 C, C++, Objective C, Objective C++, Fortran, and Go features.
2758 This variable's contents should contain options like @option{-s} and
2759 @option{-L} that affect only the behavior of the linker. Please see the
2760 explanation of @code{CFLAGS} for what you can do if an option also
2761 affects other phases of the compiler.
2763 Don't use this variable to pass library names
2764 (@option{-l}) to the linker; use @code{LIBS} instead.
2770 @option{-l} options to pass to the linker. The default value is empty,
2771 but some Autoconf macros may prepend extra libraries to this variable if
2772 those libraries are found and provide necessary functions, see
2773 @ref{Libraries}. @command{configure} uses this variable when linking
2774 programs to test for C, C++, Objective C, Objective C++, Fortran, and Go
2781 Debugging and optimization options for the Objective C compiler. It
2782 acts like @code{CFLAGS}, but for Objective C instead of C.
2786 @evindex OBJCXXFLAGS
2787 @ovindex OBJCXXFLAGS
2788 Debugging and optimization options for the Objective C++ compiler. It
2789 acts like @code{CXXFLAGS}, but for Objective C++ instead of C++.
2795 Debugging and optimization options for the Go compiler. It acts like
2796 @code{CFLAGS}, but for Go instead of C.
2801 Rigorously equal to @samp{.}. Added for symmetry only.
2804 @defvar abs_builddir
2805 @ovindex abs_builddir
2806 Absolute name of @code{builddir}.
2809 @defvar top_builddir
2810 @ovindex top_builddir
2811 The relative name of the top level of the current build tree. In the
2812 top-level directory, this is the same as @code{builddir}.
2815 @defvar top_build_prefix
2816 @ovindex top_build_prefix
2817 The relative name of the top level of the current build tree with final
2818 slash if nonempty. This is the same as @code{top_builddir}, except that
2819 it contains zero or more runs of @code{../}, so it should not be
2820 appended with a slash for concatenation. This helps for @command{make}
2821 implementations that otherwise do not treat @file{./file} and @file{file}
2822 as equal in the top-level build directory.
2825 @defvar abs_top_builddir
2826 @ovindex abs_top_builddir
2827 Absolute name of @code{top_builddir}.
2832 The name of the directory that contains the source code for
2838 Absolute name of @code{srcdir}.
2843 The name of the top-level source code directory for the
2844 package. In the top-level directory, this is the same as @code{srcdir}.
2847 @defvar abs_top_srcdir
2848 @ovindex abs_top_srcdir
2849 Absolute name of @code{top_srcdir}.
2852 @node Installation Directory Variables
2853 @subsection Installation Directory Variables
2854 @cindex Installation directories
2855 @cindex Directories, installation
2857 The following variables specify the directories for
2858 package installation, see @ref{Directory Variables, , Variables for
2859 Installation Directories, standards, The GNU Coding
2860 Standards}, for more information. Each variable corresponds to an
2861 argument of @command{configure}; trailing slashes are stripped so that
2862 expressions such as @samp{$@{prefix@}/lib} expand with only one slash
2863 between directory names. See the end of this section for
2864 details on when and how to use these variables.
2868 The directory for installing executables that users run.
2873 The directory for installing idiosyncratic read-only
2874 architecture-independent data.
2878 @ovindex datarootdir
2879 The root of the directory tree for read-only architecture-independent
2885 The directory for installing documentation files (other than Info and
2891 The directory for installing documentation files in DVI format.
2895 @ovindex exec_prefix
2896 The installation prefix for architecture-dependent files. By default
2897 it's the same as @code{prefix}. You should avoid installing anything
2898 directly to @code{exec_prefix}. However, the default value for
2899 directories containing architecture-dependent files should be relative
2900 to @code{exec_prefix}.
2905 The directory for installing HTML documentation.
2910 The directory for installing C header files.
2915 The directory for installing documentation in Info format.
2920 The directory for installing object code libraries.
2925 The directory for installing executables that other programs run.
2930 The directory for installing locale-dependent but
2931 architecture-independent data, such as message catalogs. This directory
2932 usually has a subdirectory per locale.
2935 @defvar localstatedir
2936 @ovindex localstatedir
2937 The directory for installing modifiable single-machine data. Content in
2938 this directory typically survives a reboot.
2942 @ovindex runstatedir
2943 The directory for installing temporary modifiable single-machine data.
2944 Content in this directory survives as long as the process is running
2945 (such as pid files), as contrasted with @file{/tmp} that may be
2946 periodically cleaned. Conversely, this directory is typically cleaned
2947 on a reboot. By default, this is a subdirectory of
2948 @code{localstatedir}.
2953 The top-level directory for installing documentation in man format.
2956 @defvar oldincludedir
2957 @ovindex oldincludedir
2958 The directory for installing C header files for non-GCC compilers.
2963 The directory for installing PDF documentation.
2968 The common installation prefix for all files. If @code{exec_prefix}
2969 is defined to a different value, @code{prefix} is used only for
2970 architecture-independent files.
2975 The directory for installing PostScript documentation.
2980 The directory for installing executables that system
2984 @defvar sharedstatedir
2985 @ovindex sharedstatedir
2986 The directory for installing modifiable architecture-independent data.
2991 The directory for installing read-only single-machine data.
2995 Most of these variables have values that rely on @code{prefix} or
2996 @code{exec_prefix}. It is deliberate that the directory output
2997 variables keep them unexpanded: typically @samp{@@datarootdir@@} is
2998 replaced by @samp{$@{prefix@}/share}, not @samp{/usr/local/share}, and
2999 @samp{@@datadir@@} is replaced by @samp{$@{datarootdir@}}.
3001 This behavior is mandated by the GNU Coding Standards, so that when
3006 she can still specify a different prefix from the one specified to
3007 @command{configure}, in which case, if needed, the package should hard
3008 code dependencies corresponding to the make-specified prefix.
3011 she can specify a different installation location, in which case the
3012 package @emph{must} still depend on the location which was compiled in
3013 (i.e., never recompile when @samp{make install} is run). This is an
3014 extremely important feature, as many people may decide to install all
3015 the files of a package grouped together, and then install links from
3016 the final locations to there.
3019 In order to support these features, it is essential that
3020 @code{datarootdir} remains defined as @samp{$@{prefix@}/share},
3021 so that its value can be expanded based
3022 on the current value of @code{prefix}.
3024 A corollary is that you should not use these variables except in
3025 makefiles. For instance, instead of trying to evaluate @code{datadir}
3026 in @file{configure} and hard-coding it in makefiles using
3027 e.g., @samp{AC_DEFINE_UNQUOTED([DATADIR], ["$datadir"], [Data directory.])},
3029 @option{-DDATADIR='$(datadir)'} to your makefile's definition of
3030 @code{CPPFLAGS} (@code{AM_CPPFLAGS} if you are also using Automake).
3032 Similarly, you should not rely on @code{AC_CONFIG_FILES} to replace
3033 @code{bindir} and friends in your shell scripts and other files; instead,
3034 let @command{make} manage their replacement. For instance Autoconf
3035 ships templates of its shell scripts ending with @samp{.in}, and uses a
3036 makefile snippet similar to the following to build scripts like
3037 @command{autoheader} and @command{autom4te}:
3042 -e 's|@@bindir[@@]|$(bindir)|g' \
3043 -e 's|@@pkgdatadir[@@]|$(pkgdatadir)|g' \
3044 -e 's|@@prefix[@@]|$(prefix)|g'
3048 autoheader autom4te: Makefile
3051 test -f ./$@@.in || srcdir=$(srcdir)/; \
3052 $(edit) $$@{srcdir@}$@@.in >$@@.tmp
3053 @c $$ restore font-lock
3060 autoheader: $(srcdir)/autoheader.in
3061 autom4te: $(srcdir)/autom4te.in
3065 Some details are noteworthy:
3068 @item @samp{@@bindir[@@]}
3069 The brackets prevent @command{configure} from replacing
3070 @samp{@@bindir@@} in the Sed expression itself.
3071 Brackets are preferable to a backslash here, since
3072 Posix says @samp{\@@} is not portable.
3074 @item @samp{$(bindir)}
3075 Don't use @samp{@@bindir@@}! Use the matching makefile variable
3078 @item @samp{$(pkgdatadir)}
3079 The example takes advantage of the variable @samp{$(pkgdatadir)}
3080 provided by Automake; it is equivalent to @samp{$(datadir)/$(PACKAGE)}.
3083 Don't use @samp{/} in the Sed expressions that replace file names since
3085 variables you use, such as @samp{$(bindir)}, contain @samp{/}.
3086 Use a shell metacharacter instead, such as @samp{|}.
3088 @item special characters
3089 File names, file name components, and the value of @code{VPATH} should
3090 not contain shell metacharacters or white
3091 space. @xref{Special Chars in Variables}.
3093 @item dependency on @file{Makefile}
3094 Since @code{edit} uses values that depend on the configuration specific
3095 values (@code{prefix}, etc.)@: and not only on @code{VERSION} and so forth,
3096 the output depends on @file{Makefile}, not @file{configure.ac}.
3099 The main rule is generic, and uses @samp{$@@} extensively to
3100 avoid the need for multiple copies of the rule.
3102 @item Separated dependencies and single suffix rules
3103 You can't use them! The above snippet cannot be (portably) rewritten
3107 autoconf autoheader: Makefile
3117 @xref{Single Suffix Rules}, for details.
3119 @item @samp{$(srcdir)}
3120 Be sure to specify the name of the source directory,
3121 otherwise the package won't support separated builds.
3124 For the more specific installation of Erlang libraries, the following variables
3127 @defvar ERLANG_INSTALL_LIB_DIR
3128 @ovindex ERLANG_INSTALL_LIB_DIR
3129 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
3130 The common parent directory of Erlang library installation directories.
3131 This variable is set by calling the @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR}
3132 macro in @file{configure.ac}.
3135 @defvar ERLANG_INSTALL_LIB_DIR_@var{library}
3136 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
3137 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
3138 The installation directory for Erlang library @var{library}.
3139 This variable is set by using the
3140 @samp{AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR}
3141 macro in @file{configure.ac}.
3144 @xref{Erlang Libraries}, for details.
3147 @node Changed Directory Variables
3148 @subsection Changed Directory Variables
3149 @cindex @file{datarootdir}
3151 In Autoconf 2.60, the set of directory variables has changed, and the
3152 defaults of some variables have been adjusted
3153 (@pxref{Installation Directory Variables}) to changes in the
3154 GNU Coding Standards. Notably, @file{datadir}, @file{infodir}, and
3155 @file{mandir} are now expressed in terms of @file{datarootdir}. If you are
3156 upgrading from an earlier Autoconf version, you may need to adjust your files
3157 to ensure that the directory variables are substituted correctly
3158 (@pxref{Defining Directories}), and that a definition of @file{datarootdir} is
3159 in place. For example, in a @file{Makefile.in}, adding
3162 datarootdir = @@datarootdir@@
3166 is usually sufficient. If you use Automake to create @file{Makefile.in},
3167 it will add this for you.
3169 To help with the transition, Autoconf warns about files that seem to use
3170 @code{datarootdir} without defining it. In some cases, it then expands
3171 the value of @code{$datarootdir} in substitutions of the directory
3172 variables. The following example shows such a warning:
3175 $ @kbd{cat configure.ac}
3177 AC_CONFIG_FILES([Makefile])
3179 $ @kbd{cat Makefile.in}
3181 datadir = @@datadir@@
3184 configure: creating ./config.status
3185 config.status: creating Makefile
3186 config.status: WARNING:
3187 Makefile.in seems to ignore the --datarootdir setting
3188 $ @kbd{cat Makefile}
3190 datadir = $@{prefix@}/share
3193 Usually one can easily change the file to accommodate both older and newer
3197 $ @kbd{cat Makefile.in}
3199 datarootdir = @@datarootdir@@
3200 datadir = @@datadir@@
3202 configure: creating ./config.status
3203 config.status: creating Makefile
3204 $ @kbd{cat Makefile}
3206 datarootdir = $@{prefix@}/share
3207 datadir = $@{datarootdir@}
3210 @acindex{DATAROOTDIR_CHECKED}
3211 In some cases, however, the checks may not be able to detect that a suitable
3212 initialization of @code{datarootdir} is in place, or they may fail to detect
3213 that such an initialization is necessary in the output file. If, after
3214 auditing your package, there are still spurious @file{configure} warnings about
3215 @code{datarootdir}, you may add the line
3218 AC_DEFUN([AC_DATAROOTDIR_CHECKED])
3222 to your @file{configure.ac} to disable the warnings. This is an exception
3223 to the usual rule that you should not define a macro whose name begins with
3224 @code{AC_} (@pxref{Macro Names}).
3228 @node Build Directories
3229 @subsection Build Directories
3230 @cindex Build directories
3231 @cindex Directories, build
3233 You can support compiling a software package for several architectures
3234 simultaneously from the same copy of the source code. The object files
3235 for each architecture are kept in their own directory.
3237 To support doing this, @command{make} uses the @code{VPATH} variable to
3238 find the files that are in the source directory. GNU Make
3239 can do this. Most other recent @command{make} programs can do this as
3240 well, though they may have difficulties and it is often simpler to
3241 recommend GNU @command{make} (@pxref{VPATH and Make}). Older
3242 @command{make} programs do not support @code{VPATH}; when using them, the
3243 source code must be in the same directory as the object files.
3245 If you are using GNU Automake, the remaining details in this
3246 section are already covered for you, based on the contents of your
3247 @file{Makefile.am}. But if you are using Autoconf in isolation, then
3248 supporting @code{VPATH} requires the following in your
3256 Do not set @code{VPATH} to the value of another variable (@pxref{Variables
3259 @command{configure} substitutes the correct value for @code{srcdir} when
3260 it produces @file{Makefile}.
3262 Do not use the @command{make} variable @code{$<}, which expands to the
3263 file name of the file in the source directory (found with @code{VPATH}),
3264 except in implicit rules. (An implicit rule is one such as @samp{.c.o},
3265 which tells how to create a @file{.o} file from a @file{.c} file.) Some
3266 versions of @command{make} do not set @code{$<} in explicit rules; they
3267 expand it to an empty value.
3269 Instead, Make command lines should always refer to source
3270 files by prefixing them with @samp{$(srcdir)/}. It's safer
3271 to quote the source directory name, in case it contains characters that
3272 are special to the shell. Because @samp{$(srcdir)} is expanded by Make,
3273 single-quoting works and is safer than double-quoting. For example:
3276 time.info: time.texinfo
3277 $(MAKEINFO) '$(srcdir)/time.texinfo'
3280 @node Automatic Remaking
3281 @subsection Automatic Remaking
3282 @cindex Automatic remaking
3283 @cindex Remaking automatically
3285 You can put rules like the following in the top-level @file{Makefile.in}
3286 for a package to automatically update the configuration information when
3287 you change the configuration files. This example includes all of the
3288 optional files, such as @file{aclocal.m4} and those related to
3289 configuration header files. Omit from the @file{Makefile.in} rules for
3290 any of these files that your package does not use.
3292 The @samp{$(srcdir)/} prefix is included because of limitations in the
3293 @code{VPATH} mechanism.
3295 The @file{stamp-} files are necessary because the timestamps of
3296 @file{config.h.in} and @file{config.h} are not changed if remaking
3297 them does not change their contents. This feature avoids unnecessary
3298 recompilation. You should include the file @file{stamp-h.in} in your
3299 package's distribution, so that @command{make} considers
3300 @file{config.h.in} up to date. Don't use @command{touch}
3301 (@pxref{touch, , Limitations of Usual Tools}); instead, use
3302 @command{echo} (using
3303 @command{date} would cause needless differences, hence CVS
3308 $(srcdir)/configure: configure.ac aclocal.m4
3309 cd '$(srcdir)' && autoconf
3311 # autoheader might not change config.h.in, so touch a stamp file.
3312 $(srcdir)/config.h.in: stamp-h.in ;
3313 $(srcdir)/stamp-h.in: configure.ac aclocal.m4
3314 cd '$(srcdir)' && autoheader
3315 echo timestamp > '$(srcdir)/stamp-h.in'
3318 stamp-h: config.h.in config.status
3321 Makefile: Makefile.in config.status
3324 config.status: configure
3325 ./config.status --recheck
3330 (Be careful if you copy these lines directly into your makefile, as you
3331 need to convert the indented lines to start with the tab character.)
3333 In addition, you should use
3336 AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h])
3340 so @file{config.status} ensures that @file{config.h} is considered up to
3341 date. @xref{Output}, for more information about @code{AC_OUTPUT}.
3343 @xref{config.status Invocation}, for more examples of handling
3344 configuration-related dependencies.
3346 @node Configuration Headers
3347 @section Configuration Header Files
3348 @cindex Configuration Header
3349 @cindex @file{config.h}
3351 When a package contains more than a few tests that define C preprocessor
3352 symbols, the command lines to pass @option{-D} options to the compiler
3353 can get quite long. This causes two problems. One is that the
3354 @command{make} output is hard to visually scan for errors. More
3355 seriously, the command lines can exceed the length limits of some
3356 operating systems. As an alternative to passing @option{-D} options to
3357 the compiler, @command{configure} scripts can create a C header file
3358 containing @samp{#define} directives. The @code{AC_CONFIG_HEADERS}
3359 macro selects this kind of output. Though it can be called anywhere
3360 between @code{AC_INIT} and @code{AC_OUTPUT}, it is customary to call
3361 it right after @code{AC_INIT}.
3363 The package should @samp{#include} the configuration header file before
3364 any other header files, to prevent inconsistencies in declarations (for
3365 example, if it redefines @code{const}, or if it defines a macro like
3366 @code{_FILE_OFFSET_BITS} that affects the behavior of system
3367 headers). Note that it is okay to only include @file{config.h} from
3368 @file{.c} files; the project's @file{.h} files can rely on
3369 @file{config.h} already being included first by the corresponding
3372 To provide for VPATH builds, remember to pass the C compiler a @option{-I.}
3373 option (or @option{-I..}; whichever directory contains @file{config.h}).
3374 Even if you use @samp{#include "config.h"}, the preprocessor searches only
3375 the directory of the currently read file, i.e., the source directory, not
3376 the build directory.
3378 With the appropriate @option{-I} option, you can use
3379 @samp{#include <config.h>}. Actually, it's a good habit to use it,
3380 because in the rare case when the source directory contains another
3381 @file{config.h}, the build directory should be searched first.
3384 @defmac AC_CONFIG_HEADERS (@var{header} @dots{}, @ovar{cmds}, @ovar{init-cmds})
3385 @acindex{CONFIG_HEADERS}
3386 @cvindex HAVE_CONFIG_H
3387 This macro is one of the instantiating macros; see @ref{Configuration
3388 Actions}. Make @code{AC_OUTPUT} create the file(s) in the
3389 blank-or-newline-separated list @var{header} containing C preprocessor
3390 @code{#define} statements, and replace @samp{@@DEFS@@} in generated
3391 files with @option{-DHAVE_CONFIG_H} instead of the value of @code{DEFS}.
3392 The usual name for @var{header} is @file{config.h};
3393 @var{header} should not contain shell metacharacters.
3394 @xref{Special Chars in Variables}.
3396 If @var{header} already exists and its contents are identical to what
3397 @code{AC_OUTPUT} would put in it, it is left alone. Doing this allows
3398 making some changes in the configuration without needlessly causing
3399 object files that depend on the header file to be recompiled.
3401 Usually the input file is named @file{@var{header}.in}; however, you can
3402 override the input file name by appending to @var{header} a
3403 colon-separated list of input files. For example, you might need to make
3404 the input file name acceptable to DOS variants:
3407 AC_CONFIG_HEADERS([config.h:config.hin])
3414 This macro is defined as the name of the first declared config header
3415 and undefined if no config headers have been declared up to this point.
3416 A third-party macro may, for example, require use of a config header
3417 without invoking AC_CONFIG_HEADERS twice, like this:
3420 AC_CONFIG_COMMANDS_PRE(
3421 [m4_ifndef([AH_HEADER], [AC_CONFIG_HEADERS([config.h])])])
3426 @xref{Configuration Actions}, for more details on @var{header}.
3429 * Header Templates:: Input for the configuration headers
3430 * autoheader Invocation:: How to create configuration templates
3431 * Autoheader Macros:: How to specify CPP templates
3434 @node Header Templates
3435 @subsection Configuration Header Templates
3436 @cindex Configuration Header Template
3437 @cindex Header templates
3438 @cindex @file{config.h.in}
3440 Your distribution should contain a template file that looks as you want
3441 the final header file to look, including comments, with @code{#undef}
3442 statements which are used as hooks. For example, suppose your
3443 @file{configure.ac} makes these calls:
3446 AC_CONFIG_HEADERS([conf.h])
3447 AC_CHECK_HEADERS([unistd.h])
3451 Then you could have code like the following in @file{conf.h.in}.
3452 The @file{conf.h} created by @command{configure} defines @samp{HAVE_UNISTD_H}
3453 to 1, if and only if the system has @file{unistd.h}.
3457 /* Define as 1 if you have unistd.h. */
3458 #undef HAVE_UNISTD_H
3462 The format of the template file is stricter than what the C preprocessor
3463 is required to accept. A directive line should contain only whitespace,
3464 @samp{#undef}, and @samp{HAVE_UNISTD_H}. The use of @samp{#define}
3465 instead of @samp{#undef}, or of comments on the same line as
3466 @samp{#undef}, is strongly discouraged. Each hook should only be listed
3467 once. Other preprocessor lines, such as @samp{#ifdef} or
3468 @samp{#include}, are copied verbatim from the template into the
3471 Since it is a tedious task to keep a template header up to date, you may
3472 use @command{autoheader} to generate it, see @ref{autoheader Invocation}.
3474 During the instantiation of the header, each @samp{#undef} line in the
3475 template file for each symbol defined by @samp{AC_DEFINE} is changed to an
3476 appropriate @samp{#define}. If the corresponding @samp{AC_DEFINE} has not
3477 been executed during the @command{configure} run, the @samp{#undef} line is
3478 commented out. (This is important, e.g., for @samp{_POSIX_SOURCE}:
3479 on many systems, it can be implicitly defined by the compiler, and
3480 undefining it in the header would then break compilation of subsequent
3483 Currently, @emph{all} remaining @samp{#undef} lines in the header
3484 template are commented out, whether or not there was a corresponding
3485 @samp{AC_DEFINE} for the macro name; but this behavior is not guaranteed
3486 for future releases of Autoconf.
3488 Generally speaking, since you should not use @samp{#define}, and you
3489 cannot guarantee whether a @samp{#undef} directive in the header
3490 template will be converted to a @samp{#define} or commented out in the
3491 generated header file, the template file cannot be used for conditional
3492 definition effects. Consequently, if you need to use the construct
3503 you must place it outside of the template.
3504 If you absolutely need to hook it to the config header itself, please put
3505 the directives to a separate file, and @samp{#include} that file from the
3506 config header template. If you are using @command{autoheader}, you would
3507 probably use @samp{AH_BOTTOM} to append the @samp{#include} directive.
3510 @node autoheader Invocation
3511 @subsection Using @command{autoheader} to Create @file{config.h.in}
3512 @cindex @command{autoheader}
3514 The @command{autoheader} program can create a template file of C
3515 @samp{#define} statements for @command{configure} to use.
3516 It searches for the first invocation of @code{AC_CONFIG_HEADERS} in
3517 @file{configure} sources to determine the name of the template.
3518 (If the first call of @code{AC_CONFIG_HEADERS} specifies more than one
3519 input file name, @command{autoheader} uses the first one.)
3521 It is recommended that only one input file is used. If you want to append
3522 a boilerplate code, it is preferable to use
3523 @samp{AH_BOTTOM([#include <conf_post.h>])}.
3524 File @file{conf_post.h} is not processed during the configuration then,
3525 which make things clearer. Analogically, @code{AH_TOP} can be used to
3526 prepend a boilerplate code.
3528 In order to do its job, @command{autoheader} needs you to document all
3529 of the symbols that you might use. Typically this is done via an
3530 @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED} call whose first argument
3531 is a literal symbol and whose third argument describes the symbol
3532 (@pxref{Defining Symbols}). Alternatively, you can use
3533 @code{AH_TEMPLATE} (@pxref{Autoheader Macros}), or you can supply a
3534 suitable input file for a subsequent configuration header file.
3535 Symbols defined by Autoconf's builtin tests are already documented properly;
3536 you need to document only those that you
3539 You might wonder why @command{autoheader} is needed: after all, why
3540 would @command{configure} need to ``patch'' a @file{config.h.in} to
3541 produce a @file{config.h} instead of just creating @file{config.h} from
3542 scratch? Well, when everything rocks, the answer is just that we are
3543 wasting our time maintaining @command{autoheader}: generating
3544 @file{config.h} directly is all that is needed. When things go wrong,
3545 however, you'll be thankful for the existence of @command{autoheader}.
3547 The fact that the symbols are documented is important in order to
3548 @emph{check} that @file{config.h} makes sense. The fact that there is a
3549 well-defined list of symbols that should be defined (or not) is
3550 also important for people who are porting packages to environments where
3551 @command{configure} cannot be run: they just have to @emph{fill in the
3554 But let's come back to the point: the invocation of @command{autoheader}@dots{}
3556 If you give @command{autoheader} an argument, it uses that file instead
3557 of @file{configure.ac} and writes the header file to the standard output
3558 instead of to @file{config.h.in}. If you give @command{autoheader} an
3559 argument of @option{-}, it reads the standard input instead of
3560 @file{configure.ac} and writes the header file to the standard output.
3562 @command{autoheader} accepts the following options:
3567 Print a summary of the command line options and exit.
3571 Print the version number of Autoconf and exit.
3575 Report processing steps.
3579 Don't remove the temporary files.
3583 Remake the template file even if newer than its input files.
3585 @item --include=@var{dir}
3587 Append @var{dir} to the include path. Multiple invocations accumulate.
3589 @item --prepend-include=@var{dir}
3591 Prepend @var{dir} to the include path. Multiple invocations accumulate.
3593 @item --warnings=@var{category}[,@var{category}...]
3594 @itemx -W@var{category}[,@var{category}...]
3596 Enable or disable warnings related to each @var{category}.
3597 @xref{m4_warn}, for a comprehensive list of categories.
3598 Special values include:
3602 Enable all categories of warnings.
3605 Disable all categories of warnings.
3608 Treat all warnings as errors.
3610 @item no-@var{category}
3611 Disable warnings falling into @var{category}.
3614 The environment variable @env{WARNINGS} may also be set to a
3615 comma-separated list of warning categories to enable or disable.
3616 It is interpreted exactly the same way as the argument of
3617 @option{--warnings}, but unknown categories are silently ignored.
3618 The command line takes precedence; for instance, if @env{WARNINGS}
3619 is set to @code{obsolete}, but @option{-Wnone} is given on the
3620 command line, no warnings will be issued.
3622 Some categories of warnings are on by default.
3623 Again, for details see @ref{m4_warn}.
3628 @node Autoheader Macros
3629 @subsection Autoheader Macros
3630 @cindex Autoheader macros
3632 @command{autoheader} scans @file{configure.ac} and figures out which C
3633 preprocessor symbols it might define. It knows how to generate
3634 templates for symbols defined by @code{AC_CHECK_HEADERS},
3635 @code{AC_CHECK_FUNCS} etc., but if you @code{AC_DEFINE} any additional
3636 symbol, you must define a template for it. If there are missing
3637 templates, @command{autoheader} fails with an error message.
3639 The template for a @var{symbol} is created
3640 by @command{autoheader} from
3641 the @var{description} argument to an @code{AC_DEFINE};
3642 see @ref{Defining Symbols}.
3644 For special needs, you can use the following macros.
3647 @defmac AH_TEMPLATE (@var{key}, @var{description})
3649 Tell @command{autoheader} to generate a template for @var{key}. This macro
3650 generates standard templates just like @code{AC_DEFINE} when a
3651 @var{description} is given.
3656 AH_TEMPLATE([NULL_DEVICE],
3657 [Name of the file to open to get
3658 a null file, or a data sink.])
3662 generates the following template, with the description properly
3666 /* Name of the file to open to get a null file, or a data sink. */
3672 @defmac AH_VERBATIM (@var{key}, @var{template})
3674 Tell @command{autoheader} to include the @var{template} as-is in the header
3675 template file. This @var{template} is associated with the @var{key},
3676 which is used to sort all the different templates and guarantee their
3677 uniqueness. It should be a symbol that can be defined via @code{AC_DEFINE}.
3681 @defmac AH_TOP (@var{text})
3683 Include @var{text} at the top of the header template file.
3687 @defmac AH_BOTTOM (@var{text})
3689 Include @var{text} at the bottom of the header template file.
3693 Please note that @var{text} gets included ``verbatim'' to the template file,
3694 not to the resulting config header, so it can easily get mangled when the
3695 template is processed. There is rarely a need for something other than
3698 AH_BOTTOM([#include <custom.h>])
3703 @node Configuration Commands
3704 @section Running Arbitrary Configuration Commands
3705 @cindex Configuration commands
3706 @cindex Commands for configuration
3708 You can execute arbitrary commands before, during, and after
3709 @file{config.status} is run. The three following macros accumulate the
3710 commands to run when they are called multiple times.
3711 @code{AC_CONFIG_COMMANDS} replaces the obsolete macro
3712 @code{AC_OUTPUT_COMMANDS}; see @ref{Obsolete Macros}, for details.
3714 @anchor{AC_CONFIG_COMMANDS}
3715 @defmac AC_CONFIG_COMMANDS (@var{tag}@dots{}, @ovar{cmds}, @ovar{init-cmds})
3716 @acindex{CONFIG_COMMANDS}
3717 Specify additional shell commands to run at the end of
3718 @file{config.status}, and shell commands to initialize any variables
3719 from @command{configure}. Associate the commands with @var{tag}.
3720 Since typically the @var{cmds} create a file, @var{tag} should
3721 naturally be the name of that file. If needed, the directory hosting
3722 @var{tag} is created. The @var{tag} should not contain shell
3723 metacharacters. @xref{Special Chars in Variables}.
3724 This macro is one of the instantiating macros;
3725 see @ref{Configuration Actions}.
3727 Here is an unrealistic example:
3730 AC_CONFIG_COMMANDS([fubar],
3731 [echo this is extra $fubar, and so on.],
3735 Here is a better one:
3737 AC_CONFIG_COMMANDS([timestamp], [date >timestamp])
3741 The following two macros look similar, but in fact they are not of the same
3742 breed: they are executed directly by @file{configure}, so you cannot use
3743 @file{config.status} to rerun them.
3745 @c Yet it is good to leave them here. The user sees them together and
3746 @c decides which best fits their needs.
3748 @defmac AC_CONFIG_COMMANDS_PRE (@var{cmds})
3749 @acindex{CONFIG_COMMANDS_PRE}
3750 Execute the @var{cmds} right before creating @file{config.status}.
3752 This macro presents the last opportunity to call @code{AC_SUBST},
3753 @code{AC_DEFINE}, or @code{AC_CONFIG_@var{ITEMS}} macros.
3756 @defmac AC_CONFIG_COMMANDS_POST (@var{cmds})
3757 @acindex{CONFIG_COMMANDS_POST}
3758 Execute the @var{cmds} right after creating @file{config.status}.
3764 @node Configuration Links
3765 @section Creating Configuration Links
3766 @cindex Configuration links
3767 @cindex Links for configuration
3769 You may find it convenient to create links whose destinations depend upon
3770 results of tests. One can use @code{AC_CONFIG_COMMANDS} but the
3771 creation of relative symbolic links can be delicate when the package is
3772 built in a directory different from the source directory.
3774 @anchor{AC_CONFIG_LINKS}
3775 @defmac AC_CONFIG_LINKS (@var{dest}:@var{source}@dots{}, @ovar{cmds}, @
3777 @acindex{CONFIG_LINKS}
3779 Make @code{AC_OUTPUT} link each of the existing files @var{source} to
3780 the corresponding link name @var{dest}. Makes a symbolic link if
3781 possible, otherwise a hard link if possible, otherwise a copy. The
3782 @var{dest} and @var{source} names should be relative to the top level
3783 source or build directory, and should not contain shell metacharacters.
3784 @xref{Special Chars in Variables}.
3786 This macro is one of the instantiating
3787 macros; see @ref{Configuration Actions}.
3789 For example, this call:
3792 AC_CONFIG_LINKS([host.h:config/$machine.h
3793 object.h:config/$obj_format.h])
3797 creates in the current directory @file{host.h} as a link to
3798 @file{@var{srcdir}/config/$machine.h}, and @file{object.h} as a
3799 link to @file{@var{srcdir}/config/$obj_format.h}.
3801 The tempting value @samp{.} for @var{dest} is invalid: it makes it
3802 impossible for @samp{config.status} to guess the links to establish.
3806 ./config.status host.h object.h
3809 to create the links.
3814 @node Subdirectories
3815 @section Configuring Other Packages in Subdirectories
3816 @cindex Configure subdirectories
3817 @cindex Subdirectory configure
3819 In most situations, calling @code{AC_OUTPUT} is sufficient to produce
3820 makefiles in subdirectories. However, @command{configure} scripts
3821 that control more than one independent package can use
3822 @code{AC_CONFIG_SUBDIRS} to run @command{configure} scripts for other
3823 packages in subdirectories.
3825 @defmac AC_CONFIG_SUBDIRS (@var{dir} @dots{})
3826 @acindex{CONFIG_SUBDIRS}
3828 Make @code{AC_OUTPUT} run @command{configure} in each subdirectory
3829 @var{dir} in the given blank-or-newline-separated list. Each @var{dir} should
3830 be a literal, i.e., please do not use:
3833 @c If you change this example, adjust tests/torture.at:Non-literal AC_CONFIG_SUBDIRS.
3834 if test "x$package_foo_enabled" = xyes; then
3835 my_subdirs="$my_subdirs foo"
3837 AC_CONFIG_SUBDIRS([$my_subdirs])
3841 because this prevents @samp{./configure --help=recursive} from
3842 displaying the options of the package @code{foo}. Instead, you should
3846 AS_IF([test "x$package_foo_enabled" = xyes],
3847 [AC_CONFIG_SUBDIRS([foo])])
3850 If a given @var{dir} is not found at @command{configure} run time, a
3851 warning is reported; if the subdirectory is optional, write:
3854 AS_IF([test -d "$srcdir/foo"],
3855 [AC_CONFIG_SUBDIRS([foo])])
3858 These examples use @code{AS_IF} instead of ordinary shell @code{if} to
3859 avoid problems that Autoconf has with macro calls in shell conditionals
3860 outside macro definitions. @xref{Common Shell Constructs}.
3862 If a given @var{dir} contains @command{configure.gnu}, it is run instead
3863 of @command{configure}. This is for packages that might use a
3864 non-Autoconf script @command{Configure}, which can't be called through a
3865 wrapper @command{configure} since it would be the same file on
3866 case-insensitive file systems.
3868 The subdirectory @command{configure} scripts are given the same command
3869 line options that were given to this @command{configure} script, with minor
3870 changes if needed, which include:
3874 adjusting a relative name for the cache file;
3877 adjusting a relative name for the source directory;
3880 propagating the current value of @code{$prefix}, including if it was
3881 defaulted, and if the default values of the top level and of the subdirectory
3882 @file{configure} differ.
3885 This macro also sets the output variable @code{subdirs} to the list of
3886 directories @samp{@var{dir} @dots{}}. Make rules can use
3887 this variable to determine which subdirectories to recurse into.
3889 This macro may be called multiple times.
3892 @node Default Prefix
3893 @section Default Prefix
3894 @cindex Install prefix
3895 @cindex Prefix for install
3897 By default, @command{configure} sets the prefix for files it installs to
3898 @file{/usr/local}. The user of @command{configure} can select a different
3899 prefix using the @option{--prefix} and @option{--exec-prefix} options.
3900 There are two ways to change the default: when creating
3901 @command{configure}, and when running it.
3903 Some software packages might want to install in a directory other than
3904 @file{/usr/local} by default. To accomplish that, use the
3905 @code{AC_PREFIX_DEFAULT} macro.
3907 @defmac AC_PREFIX_DEFAULT (@var{prefix})
3908 @acindex{PREFIX_DEFAULT}
3909 Set the default installation prefix to @var{prefix} instead of
3913 It may be convenient for users to have @command{configure} guess the
3914 installation prefix from the location of a related program that they
3915 have already installed. If you wish to do that, you can call
3916 @code{AC_PREFIX_PROGRAM}.
3918 @anchor{AC_PREFIX_PROGRAM}
3919 @defmac AC_PREFIX_PROGRAM (@var{program})
3920 @acindex{PREFIX_PROGRAM}
3921 If the user did not specify an installation prefix (using the
3922 @option{--prefix} option), guess a value for it by looking for
3923 @var{program} in @env{PATH}, the way the shell does. If @var{program}
3924 is found, set the prefix to the parent of the directory containing
3925 @var{program}, else default the prefix as described above
3926 (@file{/usr/local} or @code{AC_PREFIX_DEFAULT}). For example, if
3927 @var{program} is @code{gcc} and the @env{PATH} contains
3928 @file{/usr/local/gnu/bin/gcc}, set the prefix to @file{/usr/local/gnu}.
3933 @c ======================================================== Existing tests
3935 @node Existing Tests
3936 @chapter Existing Tests
3938 These macros test for particular system features that packages might
3939 need or want to use. If you need to test for a kind of feature that
3940 none of these macros check for, you can probably do it by calling
3941 primitive test macros with appropriate arguments (@pxref{Writing
3944 These tests print messages telling the user which feature they're
3945 checking for, and what they find. They cache their results for future
3946 @command{configure} runs (@pxref{Caching Results}).
3948 Some of these macros set output variables. @xref{Makefile
3949 Substitutions}, for how to get their values. The phrase ``define
3950 @var{name}'' is used below as a shorthand to mean ``define the C
3951 preprocessor symbol @var{name} to the value 1''. @xref{Defining
3952 Symbols}, for how to get those symbol definitions into your program.
3955 * Common Behavior:: Macros' standard schemes
3956 * Alternative Programs:: Selecting between alternative programs
3957 * Files:: Checking for the existence of files
3958 * Libraries:: Library archives that might be missing
3959 * Library Functions:: C library functions that might be missing
3960 * Header Files:: Header files that might be missing
3961 * Declarations:: Declarations that may be missing
3962 * Structures:: Structures or members that might be missing
3963 * Types:: Types that might be missing
3964 * Compilers and Preprocessors:: Checking for compiling programs
3965 * System Services:: Operating system services
3966 * C and Posix Variants:: Kludges for C and Posix variants
3967 * Erlang Libraries:: Checking for the existence of Erlang libraries
3970 @node Common Behavior
3971 @section Common Behavior
3972 @cindex Common autoconf behavior
3974 Much effort has been expended to make Autoconf easy to learn. The most
3975 obvious way to reach this goal is simply to enforce standard interfaces
3976 and behaviors, avoiding exceptions as much as possible. Because of
3977 history and inertia, unfortunately, there are still too many exceptions
3978 in Autoconf; nevertheless, this section describes some of the common
3982 * Standard Symbols:: Symbols defined by the macros
3983 * Default Includes:: Includes used by the generic macros
3986 @node Standard Symbols
3987 @subsection Standard Symbols
3988 @cindex Standard symbols
3990 All the generic macros that @code{AC_DEFINE} a symbol as a result of
3991 their test transform their @var{argument} values to a standard alphabet.
3992 First, @var{argument} is converted to upper case and any asterisks
3993 (@samp{*}) are each converted to @samp{P}. Any remaining characters
3994 that are not alphanumeric are converted to underscores.
3999 AC_CHECK_TYPES([struct $Expensive*])
4003 defines the symbol @samp{HAVE_STRUCT__EXPENSIVEP} if the check
4007 @node Default Includes
4008 @subsection Default Includes
4009 @cindex Default includes
4010 @cindex Includes, default
4030 Test programs frequently need to include headers that may or may not be
4031 available on the system whose features are being tested. Each test can
4032 use all the preprocessor macros that have been @code{AC_DEFINE}d by
4033 previous tests, so for example one may write
4038 #ifdef HAVE_SYS_TIME_H
4039 # include <sys/time.h>
4045 if @file{sys/time.h} has already been tested for.
4047 All hosted environments that are still of interest for portable code
4048 provide all of the headers specified in C89 (as amended in 1995):
4049 @file{assert.h}, @file{ctype.h}, @file{errno.h}, @file{float.h},
4050 @file{iso646.h}, @file{limits.h}, @file{locale.h}, @file{math.h},
4051 @file{setjmp.h}, @file{signal.h}, @file{stdarg.h}, @file{stddef.h},
4052 @file{stdio.h}, @file{stdlib.h}, @file{string.h}, @file{time.h},
4053 @file{wchar.h}, and @file{wctype.h}. Most programs can safely include
4054 these headers unconditionally. A program not intended to be portable to
4055 C89 can also safely include the C99-specified header @file{stdbool.h}.
4056 Other headers, including headers from C99 and later revisions of the C
4057 standard, might need to be tested for (@pxref{Header Files}) or their
4058 bugs may need to be worked around (@pxref{Gnulib}).
4060 If your program needs to be portable to a @emph{freestanding}
4061 environment, such as an embedded OS that doesn't provide all of the
4062 facilities of the C89 standard library, you may need to test for some of
4063 the above headers as well. Note that many Autoconf macros internally
4064 assume that the complete set of C89 headers are available.
4066 Most generic macros use the following macro to provide a default set
4069 @defmac AC_INCLUDES_DEFAULT (@ovar{include-directives})
4070 @acindex{INCLUDES_DEFAULT}
4071 Expand to @var{include-directives} if present and nonempty, otherwise to:
4079 #ifdef HAVE_STDLIB_H
4080 # include <stdlib.h>
4082 #ifdef HAVE_STRING_H
4083 # include <string.h>
4085 #ifdef HAVE_INTTYPES_H
4086 # include <inttypes.h>
4088 #ifdef HAVE_STDINT_H
4089 # include <stdint.h>
4091 #ifdef HAVE_STRINGS_H
4092 # include <strings.h>
4094 #ifdef HAVE_SYS_TYPES_H
4095 # include <sys/types.h>
4097 #ifdef HAVE_SYS_STAT_H
4098 # include <sys/stat.h>
4100 #ifdef HAVE_UNISTD_H
4101 # include <unistd.h>
4106 Using this macro without @var{include-directives} has the side effect of
4107 checking for @file{stdio.h}, @file{stdlib.h}, @file{string.h},
4108 @file{inttypes.h}, @file{stdint.h}, @file{strings.h},
4109 @file{sys/types.h}, @file{sys/stat.h}, and @file{unistd.h}, as if by
4110 @code{AC_CHECK_HEADERS_ONCE}. For backward compatibility, the macro
4111 @code{STDC_HEADERS} will be defined when both @file{stdlib.h} and
4112 @file{string.h} are available.
4114 @strong{Portability Note:} It is safe for most programs to assume the
4115 presence of all of the headers required by the original 1990 C standard.
4116 @code{AC_INCLUDES_DEFAULT} checks for @file{stdio.h}, @file{stdlib.h},
4117 and @file{string.h}, even though they are in that list, because they
4118 might not be available when compiling for a ``freestanding environment''
4119 (in which most of the features of the C library are optional). You
4120 probably do not need to write @samp{#ifdef HAVE_STDIO_H} in your own
4123 @file{inttypes.h} and @file{stdint.h} were added to C in the 1999
4124 revision of the standard, and @file{strings.h}, @file{sys/types.h},
4125 @file{sys/stat.h}, and @file{unistd.h} are POSIX extensions. You
4126 @emph{should} guard uses of these headers with appropriate conditionals.
4129 @defmac AC_CHECK_INCLUDES_DEFAULT
4130 @acindex{CHECK_INCLUDES_DEFAULT}
4131 Check for all the headers that @code{AC_INCLUDES_DEFAULT} would check
4132 for as a side-effect, if this has not already happened.
4134 This macro mainly exists so that @code{autoupdate} can replace certain
4135 obsolete constructs with it. You should not need to use it yourself; in
4136 fact, it is likely to be safe to delete it from any script in which it
4137 appears. (@code{autoupdate} does not know whether preprocessor macros
4138 such as @code{HAVE_STDINT_H} are used in the program, nor whether they
4139 would get defined as a side-effect of other checks.)
4142 @node Alternative Programs
4143 @section Alternative Programs
4144 @cindex Programs, checking
4146 These macros check for the presence or behavior of particular programs.
4147 They are used to choose between several alternative programs and to
4148 decide what to do once one has been chosen. If there is no macro
4149 specifically defined to check for a program you need, and you don't need
4150 to check for any special properties of it, then you can use one of the
4151 general program-check macros.
4154 * Particular Programs:: Special handling to find certain programs
4155 * Generic Programs:: How to find other programs
4158 @node Particular Programs
4159 @subsection Particular Program Checks
4161 These macros check for particular programs---whether they exist, and
4162 in some cases whether they support certain features.
4168 @c @caindex prog_ac_ct_AR
4169 Set output variable @code{AR} to @samp{ar} if @code{ar} is found, and
4170 otherwise to @samp{:} (do nothing).
4177 Check for @code{gawk}, @code{mawk}, @code{nawk}, and @code{awk}, in that
4178 order, and set output variable @code{AWK} to the first one that is found.
4179 It tries @code{gawk} first because that is reported to be the
4180 best implementation. The result can be overridden by setting the
4181 variable @code{AWK} or the cache variable @code{ac_cv_prog_AWK}.
4183 Using this macro is sufficient to avoid the pitfalls of traditional
4184 @command{awk} (@pxref{awk, , Limitations of Usual Tools}).
4187 @defmac AC_PROG_GREP
4191 Look for the best available @code{grep} or @code{ggrep} that accepts the
4192 longest input lines possible, and that supports multiple @option{-e} options.
4193 Set the output variable @code{GREP} to whatever is chosen.
4194 @xref{grep, , Limitations of Usual Tools}, for more information about
4195 portability problems with the @command{grep} command family. The result
4196 can be overridden by setting the @code{GREP} variable and is cached in the
4197 @code{ac_cv_path_GREP} variable.
4200 @defmac AC_PROG_EGREP
4201 @acindex{PROG_EGREP}
4204 Check whether @code{$GREP -E} works, or else look for the best available
4205 @code{egrep} or @code{gegrep} that accepts the longest input lines possible.
4206 Set the output variable @code{EGREP} to whatever is chosen. The result
4207 can be overridden by setting the @code{EGREP} variable and is cached in the
4208 @code{ac_cv_path_EGREP} variable.
4211 @defmac AC_PROG_FGREP
4212 @acindex{PROG_FGREP}
4215 Check whether @code{$GREP -F} works, or else look for the best available
4216 @code{fgrep} or @code{gfgrep} that accepts the longest input lines possible.
4217 Set the output variable @code{FGREP} to whatever is chosen. The result
4218 can be overridden by setting the @code{FGREP} variable and is cached in the
4219 @code{ac_cv_path_FGREP} variable.
4222 @defmac AC_PROG_INSTALL
4223 @acindex{PROG_INSTALL}
4225 @ovindex INSTALL_PROGRAM
4226 @ovindex INSTALL_DATA
4227 @ovindex INSTALL_SCRIPT
4228 @caindex path_install
4229 @prindex @command{install-sh}
4230 Set output variable @code{INSTALL} to the name of a BSD-compatible
4231 @command{install} program, if one is found in the current @env{PATH}.
4232 Otherwise, set @code{INSTALL} to @samp{@var{dir}/install-sh -c},
4233 checking the directories specified to @code{AC_CONFIG_AUX_DIR} (or its
4234 default directories) to determine @var{dir} (@pxref{Output}). Also set
4235 the variables @code{INSTALL_PROGRAM} and @code{INSTALL_SCRIPT} to
4236 @samp{$@{INSTALL@}} and @code{INSTALL_DATA} to @samp{$@{INSTALL@} -m 644}.
4238 @samp{@@INSTALL@@} is special, as its value may vary for different
4239 configuration files.
4241 This macro screens out various instances of @command{install} known not to
4242 work. It prefers to find a C program rather than a shell script, for
4243 speed. Instead of @file{install-sh}, it can also use @file{install.sh},
4244 but that name is obsolete because some @command{make} programs have a rule
4245 that creates @file{install} from it if there is no makefile. Further, this
4246 macro requires @command{install} to be able to install multiple files into a
4247 target directory in a single invocation.
4249 Autoconf comes with a copy of @file{install-sh} that you can use.
4250 If you use @code{AC_PROG_INSTALL}, you must include @file{install-sh} in
4251 your distribution; otherwise @command{autoreconf} and @command{configure}
4252 will produce an error message saying they can't find it---even if the
4253 system you're on has a good @command{install} program. This check is a
4254 safety measure to prevent you from accidentally leaving that file out,
4255 which would prevent your package from installing on systems that don't
4256 have a BSD-compatible @command{install} program.
4258 If you need to use your own installation program because it has features
4259 not found in standard @command{install} programs, there is no reason to use
4260 @code{AC_PROG_INSTALL}; just put the file name of your program into your
4261 @file{Makefile.in} files.
4263 The result of the test can be overridden by setting the variable
4264 @code{INSTALL} or the cache variable @code{ac_cv_path_install}.
4267 @defmac AC_PROG_MKDIR_P
4268 @acindex{PROG_MKDIR_P}
4271 @prindex @command{install-sh}
4272 Set output variable @code{MKDIR_P} to a program that ensures that for
4273 each argument, a directory named by this argument exists, creating it
4274 and its parent directories if needed, and without race conditions when
4275 two instances of the program attempt to make the same directory at
4276 nearly the same time.
4278 This macro uses the equivalent of the @samp{mkdir -p} command. Ancient
4279 versions of @command{mkdir} are vulnerable to race conditions, so if you
4280 want to support parallel installs from different packages into the same
4281 directory you should use a non-ancient @command{mkdir}.
4283 This macro is related to the @code{AS_MKDIR_P} macro (@pxref{Programming
4284 in M4sh}), but it sets an output variable intended for use in other
4285 files, whereas @code{AS_MKDIR_P} is intended for use in scripts like
4286 @command{configure}. Also, @code{AS_MKDIR_P} does not accept options,
4287 but @code{MKDIR_P} supports the @option{-m} option, e.g., a makefile
4288 might invoke @code{$(MKDIR_P) -m 0 dir} to create an inaccessible
4289 directory, and conversely a makefile should use @code{$(MKDIR_P) --
4290 $(FOO)} if @var{FOO} might yield a value that begins with @samp{-}.
4292 The result of the test can be overridden by setting the variable
4293 @code{MKDIR_P} or the cache variable @code{ac_cv_path_mkdir}.
4296 @anchor{AC_PROG_LEX}
4297 @defmac AC_PROG_LEX (@var{options})
4301 @cvindex YYTEXT_POINTER
4302 @ovindex LEX_OUTPUT_ROOT
4304 Search for a lexical analyzer generator, preferring @code{flex}
4305 to plain @code{lex}. Output variable @code{LEX} is set to whichever
4306 program is available. If neither program is available, @code{LEX}
4308 for packages that ship the generated @file{file.yy.c}
4309 alongside the source @file{file.l}, this default allows users without a
4310 lexer generator to still build the package even if the timestamp for
4311 @file{file.l} is inadvertently changed.
4313 The name of the program to use can be overridden by setting the
4314 output variable @code{LEX} or the cache variable @code{ac_cv_prog_LEX}
4315 when running @command{configure}.
4317 If a lexical analyzer generator is found, this macro performs additional
4318 checks for common portability pitfalls. If these additional checks
4319 fail, @code{LEX} is reset to @samp{:}; otherwise the following
4320 additional macros and variables are provided.
4322 Preprocessor macro @code{YYTEXT_POINTER} is defined if the lexer
4323 skeleton, by default, declares @code{yytext} as a @samp{@w{char *}}
4324 rather than a @samp{@w{char []}}.
4326 Output variable @code{LEX_OUTPUT_ROOT} is set to the base of the file
4327 name that the lexer generates; this is usually either @file{lex.yy} or
4330 If generated lexers need a library to work, output variable
4331 @code{LEXLIB} is set to a link option for that library (e.g.,
4332 @option{-ll}), otherwise it is set to empty.
4334 The @var{options} argument modifies the behavior of @code{AC_PROG_LEX}.
4335 It should be a whitespace-separated list of options. Currently there
4336 are only two options, and they are mutually exclusive:
4340 Indicate that the library in @code{LEXLIB} needs to define the function
4341 @code{yywrap}. If a library that defines this function cannot be found,
4342 @code{LEX} will be reset to @samp{:}.
4345 Indicate that the library in @code{LEXLIB} does not need to define the
4346 function @code{yywrap}. @command{configure} will not search for it at
4350 Prior to Autoconf 2.70, @code{AC_PROG_LEX} did not take any arguments,
4351 and its behavior was different from either of the above possibilities:
4352 it would search for a library that defines @code{yywrap}, and would set
4353 @code{LEXLIB} to that library if it finds one. However, if a library
4354 that defines this function could not be found, @code{LEXLIB} would be
4355 left empty and @code{LEX} would @emph{not} be reset. This behavior was
4356 due to a bug, but several packages came to depend on it, so
4357 @code{AC_PROG_LEX} still does this if neither the @code{yywrap} nor the
4358 @code{noyywrap} option is given.
4360 Usage of @code{AC_PROG_LEX} without choosing one of the @code{yywrap}
4361 or @code{noyywrap} options is deprecated. It is usually better to
4362 use @code{noyywrap} and define the @code{yywrap} function yourself,
4363 as this almost always renders the @code{LEXLIB} unnecessary.
4365 @strong{Caution:} As a side-effect of the test, this macro may delete
4366 any file in the configure script's current working directory named
4367 @file{lex.yy.c} or @file{lexyy.c}.
4369 @strong{Caution:} Packages that ship a generated @file{lex.yy.c}
4370 cannot assume that the definition of @code{YYTEXT_POINTER} matches
4371 the code in that file. They also cannot assume that @code{LEXLIB}
4372 provides the library routines required by the code in that file.
4374 If you use Flex to generate @file{lex.yy.c}, you can work around these
4375 limitations by defining @code{yywrap} and @code{main} yourself
4376 (rendering @code{-lfl} unnecessary), and by using either the
4377 @option{--array} or @option{--pointer} options to control how
4378 @code{yytext} is declared. The code generated by Flex is also more
4379 portable than the code generated by historical versions of Lex.
4381 If you have used Flex to generate @file{lex.yy.c}, and especially if
4382 your scanner depends on Flex features, we recommend you use this
4383 Autoconf snippet to prevent the scanner being regenerated with
4388 AS_IF([test "x$LEX" != xflex],
4389 [LEX="$SHELL $missing_dir/missing flex"
4390 AC_SUBST([LEX_OUTPUT_ROOT], [lex.yy])
4391 AC_SUBST([LEXLIB], [''])])
4394 The shell script @command{missing} can be found in the Automake
4397 Remember that the user may have supplied an alternate location in
4398 @env{LEX}, so if Flex is required, it is better to check that the user
4399 provided something sufficient by parsing the output of @samp{$LEX
4400 --version} than by simply relying on @code{test "x$LEX" = xflex}.
4403 @anchor{AC_PROG_LN_S}
4404 @defmac AC_PROG_LN_S
4407 If @samp{ln -s} works on the current file system (the operating system
4408 and file system support symbolic links), set the output variable
4409 @code{LN_S} to @samp{ln -s}; otherwise, if @samp{ln} works, set
4410 @code{LN_S} to @samp{ln}, and otherwise set it to @samp{cp -pR}.
4412 If you make a link in a directory other than the current directory, its
4413 meaning depends on whether @samp{ln} or @samp{ln -s} is used. To safely
4414 create links using @samp{$(LN_S)}, either find out which form is used
4415 and adjust the arguments, or always invoke @code{ln} in the directory
4416 where the link is to be created.
4418 In other words, it does not work to do:
4426 (cd /x && $(LN_S) foo bar)
4430 @defmac AC_PROG_RANLIB
4431 @acindex{PROG_RANLIB}
4433 @c @caindex prog_RANLIB
4434 @c @caindex prog_ac_ct_RANLIB
4435 Set output variable @code{RANLIB} to @samp{ranlib} if @code{ranlib}
4436 is found, and otherwise to @samp{:} (do nothing).
4443 Set output variable @code{SED} to a Sed implementation that conforms to
4444 Posix and does not have arbitrary length limits. Report an error if no
4445 acceptable Sed is found. @xref{sed, , Limitations of Usual Tools}, for more
4446 information about portability problems with Sed.
4448 The result of this test can be overridden by setting the @code{SED} variable
4449 and is cached in the @code{ac_cv_path_SED} variable.
4452 @defmac AC_PROG_YACC
4458 If @code{bison} is found, set output variable @code{YACC} to @samp{bison
4459 -y}. Otherwise, if @code{byacc} is found, set @code{YACC} to
4460 @samp{byacc}. Otherwise set @code{YACC} to @samp{yacc}.
4461 The result of this test can be influenced by setting the variable
4462 @code{YACC} or the cache variable @code{ac_cv_prog_YACC}.
4465 @node Generic Programs
4466 @subsection Generic Program and File Checks
4468 These macros are used to find programs not covered by the ``particular''
4469 test macros. If you need to check the behavior of a program as well as
4470 find out whether it is present, you have to write your own test for it
4471 (@pxref{Writing Tests}). By default, these macros use the environment
4472 variable @env{PATH}. If you need to check for a program that might not
4473 be in the user's @env{PATH}, you can pass a modified path to use
4477 AC_PATH_PROG([INETD], [inetd], [/usr/libexec/inetd],
4478 [$PATH$PATH_SEPARATOR/usr/libexec$PATH_SEPARATOR]dnl
4479 [/usr/sbin$PATH_SEPARATOR/usr/etc$PATH_SEPARATOR/etc])
4482 You are strongly encouraged to declare the @var{variable} passed to
4483 @code{AC_CHECK_PROG} etc.@: as precious. @xref{Setting Output Variables},
4484 @code{AC_ARG_VAR}, for more details.
4486 @anchor{AC_CHECK_PROG}
4487 @defmac AC_CHECK_PROG (@var{variable}, @var{prog-to-check-for}, @
4488 @var{value-if-found}, @ovar{value-if-not-found}, @dvar{path, $PATH}, @
4490 @acindex{CHECK_PROG}
4491 @caindex prog_@var{variable}
4492 Check whether program @var{prog-to-check-for} exists in @var{path}. If
4493 it is found, set @var{variable} to @var{value-if-found}, otherwise to
4494 @var{value-if-not-found}, if given. Always pass over @var{reject} (an
4495 absolute file name) even if it is the first found in the search path; in
4496 that case, set @var{variable} using the absolute file name of the
4497 @var{prog-to-check-for} found that is not @var{reject}. If
4498 @var{variable} was already set, do nothing. Calls @code{AC_SUBST} for
4499 @var{variable}. The result of this test can be overridden by setting the
4500 @var{variable} variable or the cache variable
4501 @code{ac_cv_prog_@var{variable}}.
4504 @anchor{AC_CHECK_PROGS}
4505 @defmac AC_CHECK_PROGS (@var{variable}, @var{progs-to-check-for}, @
4506 @ovar{value-if-not-found}, @dvar{path, $PATH})
4507 @acindex{CHECK_PROGS}
4508 @caindex prog_@var{variable}
4509 Check for each program in the blank-separated list
4510 @var{progs-to-check-for} existing in the @var{path}. If one is found, set
4511 @var{variable} to the name of that program. Otherwise, continue
4512 checking the next program in the list. If none of the programs in the
4513 list are found, set @var{variable} to @var{value-if-not-found}; if
4514 @var{value-if-not-found} is not specified, the value of @var{variable}
4515 is not changed. Calls @code{AC_SUBST} for @var{variable}. The result of
4516 this test can be overridden by setting the @var{variable} variable or the
4517 cache variable @code{ac_cv_prog_@var{variable}}.
4520 @defmac AC_CHECK_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
4521 @ovar{value-if-not-found}, @dvar{path, $PATH})
4522 @acindex{CHECK_TARGET_TOOL}
4523 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
4524 with a prefix of the target type as determined by
4525 @code{AC_CANONICAL_TARGET}, followed by a dash (@pxref{Canonicalizing}).
4526 If the tool cannot be found with a prefix, and if the build and target
4527 types are equal, then it is also searched for without a prefix.
4529 As noted in @ref{Specifying Target Triplets}, the
4530 target is rarely specified, because most of the time it is the same
4531 as the host: it is the type of system for which any compiler tool in
4532 the package produces code. What this macro looks for is,
4533 for example, @emph{a tool @r{(assembler, linker, etc.)}@: that the
4534 compiler driver @r{(@command{gcc} for the GNU C Compiler)}
4535 uses to produce objects, archives or executables}.
4538 @defmac AC_CHECK_TOOL (@var{variable}, @var{prog-to-check-for}, @
4539 @ovar{value-if-not-found}, @dvar{path, $PATH})
4540 @acindex{CHECK_TOOL}
4541 @c @caindex prog_@var{VARIABLE}
4542 @c @caindex prog_ac_ct_@var{VARIABLE}
4543 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
4544 with a prefix of the host type as specified by @option{--host}, followed by a
4545 dash. For example, if the user runs
4546 @samp{configure --build=x86_64-gnu --host=aarch64-linux-gnu}, then this call:
4548 AC_CHECK_TOOL([RANLIB], [ranlib], [:])
4551 sets @code{RANLIB} to @file{aarch64-linux-gnu-ranlib} if that program exists in
4552 @var{path}, or otherwise to @samp{ranlib} if that program exists in
4553 @var{path}, or to @samp{:} if neither program exists.
4555 When cross-compiling, this macro will issue a warning if no program
4556 prefixed with the host type could be found.
4557 For more information, see @ref{Specifying Target Triplets}.
4560 @defmac AC_CHECK_TARGET_TOOLS (@var{variable}, @var{progs-to-check-for}, @
4561 @ovar{value-if-not-found}, @dvar{path, $PATH})
4562 @acindex{CHECK_TARGET_TOOLS}
4563 Like @code{AC_CHECK_TARGET_TOOL}, each of the tools in the list
4564 @var{progs-to-check-for} are checked with a prefix of the target type as
4565 determined by @code{AC_CANONICAL_TARGET}, followed by a dash
4566 (@pxref{Canonicalizing}). If none of the tools can be found with a
4567 prefix, and if the build and target types are equal, then the first one
4568 without a prefix is used. If a tool is found, set @var{variable} to
4569 the name of that program. If none of the tools in the list are found,
4570 set @var{variable} to @var{value-if-not-found}; if @var{value-if-not-found}
4571 is not specified, the value of @var{variable} is not changed. Calls
4572 @code{AC_SUBST} for @var{variable}.
4575 @defmac AC_CHECK_TOOLS (@var{variable}, @var{progs-to-check-for}, @
4576 @ovar{value-if-not-found}, @dvar{path, $PATH})
4577 @acindex{CHECK_TOOLS}
4578 Like @code{AC_CHECK_TOOL}, each of the tools in the list
4579 @var{progs-to-check-for} are checked with a prefix of the host type as
4580 determined by @code{AC_CANONICAL_HOST}, followed by a dash
4581 (@pxref{Canonicalizing}). If none of the tools can be found with a
4582 prefix, then the first one without a prefix is used. If a tool is found,
4583 set @var{variable} to the name of that program. If none of the tools in
4584 the list are found, set @var{variable} to @var{value-if-not-found}; if
4585 @var{value-if-not-found} is not specified, the value of @var{variable}
4586 is not changed. Calls @code{AC_SUBST} for @var{variable}.
4588 When cross-compiling, this macro will issue a warning if no program
4589 prefixed with the host type could be found.
4590 For more information, see @ref{Specifying Target Triplets}.
4593 @anchor{AC_PATH_PROG}
4594 @defmac AC_PATH_PROG (@var{variable}, @var{prog-to-check-for}, @
4595 @ovar{value-if-not-found}, @dvar{path, $PATH})
4597 @caindex path_@var{variable}
4598 Like @code{AC_CHECK_PROG}, but set @var{variable} to the absolute
4599 name of @var{prog-to-check-for} if found. The result of this test
4600 can be overridden by setting the @var{variable} variable. A positive
4601 result of this test is cached in the @code{ac_cv_path_@var{variable}}
4605 @anchor{AC_PATH_PROGS}
4606 @defmac AC_PATH_PROGS (@var{variable}, @var{progs-to-check-for}, @
4607 @ovar{value-if-not-found}, @dvar{path, $PATH})
4608 @acindex{PATH_PROGS}
4609 @caindex path_@var{variable}
4610 Like @code{AC_CHECK_PROGS}, but if any of @var{progs-to-check-for}
4611 are found, set @var{variable} to the absolute name of the program
4612 found. The result of this test can be overridden by setting the
4613 @var{variable} variable. A positive result of this test is cached in
4614 the @code{ac_cv_path_@var{variable}} variable.
4617 @defmac AC_PATH_PROGS_FEATURE_CHECK (@var{variable}, @
4618 @var{progs-to-check-for}, @var{feature-test}, @
4619 @ovar{action-if-not-found}, @dvar{path, $PATH})
4620 @acindex{PATH_PROGS_FEATURE_CHECK}
4621 @caindex path_@var{variable}
4622 @vrindex ac_path_@var{variable}
4623 @vrindex ac_path_@var{variable}_found
4624 This macro was introduced in Autoconf 2.62. If @var{variable} is not
4625 empty, then set the cache variable @code{ac_cv_path_@var{variable}} to
4626 its value. Otherwise, check for each program in the blank-separated
4627 list @var{progs-to-check-for} existing in @var{path}. For each program
4628 found, execute @var{feature-test} with @code{ac_path_@var{variable}}
4629 set to the absolute name of the candidate program. If no invocation of
4630 @var{feature-test} sets the shell variable
4631 @code{ac_cv_path_@var{variable}}, then @var{action-if-not-found} is
4632 executed. @var{feature-test} will be run even when
4633 @code{ac_cv_path_@var{variable}} is set, to provide the ability to
4634 choose a better candidate found later in @var{path}; to accept the
4635 current setting and bypass all further checks, @var{feature-test} can
4636 execute @code{ac_path_@var{variable}_found=:}.
4638 Note that this macro has some subtle differences from
4639 @code{AC_CHECK_PROGS}. It is designed to be run inside
4640 @code{AC_CACHE_VAL}, therefore, it should have no side effects. In
4641 particular, @var{variable} is not set to the final value of
4642 @code{ac_cv_path_@var{variable}}, nor is @code{AC_SUBST} automatically
4643 run. Also, on failure, any action can be performed, whereas
4644 @code{AC_CHECK_PROGS} only performs
4645 @code{@var{variable}=@var{value-if-not-found}}.
4647 Here is an example, similar to what Autoconf uses in its own configure
4648 script. It will search for an implementation of @command{m4} that
4649 supports the @code{indir} builtin, even if it goes by the name
4650 @command{gm4} or is not the first implementation on @env{PATH}.
4653 AC_CACHE_CHECK([for m4 that supports indir], [ac_cv_path_M4],
4654 [AC_PATH_PROGS_FEATURE_CHECK([M4], [m4 gm4],
4655 [[m4out=`echo 'changequote([,])indir([divnum])' | $ac_path_M4`
4656 test "x$m4out" = x0 \
4657 && ac_cv_path_M4=$ac_path_M4 ac_path_M4_found=:]],
4658 [AC_MSG_ERROR([could not find m4 that supports indir])])])
4659 AC_SUBST([M4], [$ac_cv_path_M4])
4663 @defmac AC_PATH_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
4664 @ovar{value-if-not-found}, @dvar{path, $PATH})
4665 @acindex{PATH_TARGET_TOOL}
4666 Like @code{AC_CHECK_TARGET_TOOL}, but set @var{variable} to the absolute
4667 name of the program if it is found.
4670 @defmac AC_PATH_TOOL (@var{variable}, @var{prog-to-check-for}, @
4671 @ovar{value-if-not-found}, @dvar{path, $PATH})
4673 Like @code{AC_CHECK_TOOL}, but set @var{variable} to the absolute
4674 name of the program if it is found.
4676 When cross-compiling, this macro will issue a warning if no program
4677 prefixed with the host type could be found.
4678 For more information, see @ref{Specifying Target Triplets}.
4684 @cindex File, checking
4686 You might also need to check for the existence of files. Before using
4687 these macros, ask yourself whether a runtime test might not be a better
4688 solution. Be aware that, like most Autoconf macros, they test a feature
4689 of the host machine, and therefore, they die when cross-compiling.
4691 @defmac AC_CHECK_FILE (@var{file}, @ovar{action-if-found}, @
4692 @ovar{action-if-not-found})
4693 @acindex{CHECK_FILE}
4694 @caindex file_@var{file}
4695 Check whether file @var{file} exists on the native system. If it is
4696 found, execute @var{action-if-found}, otherwise do
4697 @var{action-if-not-found}, if given. Cache the result of this test
4698 in the @code{ac_cv_file_@var{file}} variable, with characters not
4699 suitable for a variable name mapped to underscores.
4702 @defmac AC_CHECK_FILES (@var{files}, @ovar{action-if-found}, @
4703 @ovar{action-if-not-found})
4704 @acindex{CHECK_FILES}
4705 @caindex file_@var{file}
4706 For each file listed in @var{files}, execute @code{AC_CHECK_FILE}
4707 and perform either @var{action-if-found} or @var{action-if-not-found}.
4708 Like @code{AC_CHECK_FILE}, this defines @samp{HAVE_@var{file}}
4709 (@pxref{Standard Symbols}) for each file found and caches the results of
4710 each test in the @code{ac_cv_file_@var{file}} variable, with characters
4711 not suitable for a variable name mapped to underscores.
4716 @section Library Files
4717 @cindex Library, checking
4719 The following macros check for the presence of certain C, C++, Fortran,
4720 or Go library archive files.
4722 @anchor{AC_CHECK_LIB}
4723 @defmac AC_CHECK_LIB (@var{library}, @var{function}, @
4724 @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4726 @caindex lib_@var{library}_@var{function}
4727 Test whether the library @var{library} is available by trying to link
4728 a test program that calls function @var{function} with the library.
4729 @var{function} should be a function provided by the library.
4731 name of the library; e.g., to check for @option{-lmp}, use @samp{mp} as
4732 the @var{library} argument.
4734 @var{action-if-found} is a list of shell commands to run if the link
4735 with the library succeeds; @var{action-if-not-found} is a list of shell
4736 commands to run if the link fails. If @var{action-if-found} is not
4737 specified, the default action prepends @option{-l@var{library}} to
4738 @code{LIBS} and defines @samp{HAVE_LIB@var{library}} (in all
4739 capitals). This macro is intended to support building @code{LIBS} in
4740 a right-to-left (least-dependent to most-dependent) fashion such that
4741 library dependencies are satisfied as a natural side effect of
4742 consecutive tests. Linkers are sensitive to library ordering
4743 so the order in which @code{LIBS} is generated is important to reliable
4744 detection of libraries.
4746 If linking with @var{library} results in unresolved symbols that would
4747 be resolved by linking with additional libraries, give those libraries
4748 as the @var{other-libraries} argument, separated by spaces:
4749 e.g., @option{-lXt -lX11}. Otherwise, this macro may fail to detect
4750 that @var{library} is present, because linking the test program can
4751 fail with unresolved symbols. The @var{other-libraries} argument
4752 should be limited to cases where it is desirable to test for one library
4753 in the presence of another that is not already in @code{LIBS}.
4755 @code{AC_CHECK_LIB} requires some care in usage, and should be avoided
4756 in some common cases. Many standard functions like @code{gethostbyname}
4757 appear in the standard C library on some hosts, and in special libraries
4758 like @code{nsl} on other hosts. On some hosts the special libraries
4759 contain variant implementations that you may not want to use. These
4760 days it is normally better to use @code{AC_SEARCH_LIBS([gethostbyname],
4761 [nsl])} instead of @code{AC_CHECK_LIB([nsl], [gethostbyname])}.
4763 The result of this test is cached in the
4764 @code{ac_cv_lib_@var{library}_@var{function}} variable.
4767 @anchor{AC_SEARCH_LIBS}
4768 @defmac AC_SEARCH_LIBS (@var{function}, @var{search-libs}, @
4769 @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4770 @acindex{SEARCH_LIBS}
4771 @caindex search_@var{function}
4772 Search for a library defining @var{function} if it's not already
4773 available. This equates to calling
4774 @samp{AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])])} first with
4775 no libraries, then for each library listed in @var{search-libs}.
4777 Prepend @option{-l@var{library}} to @code{LIBS} for the first library found
4778 to contain @var{function}, and run @var{action-if-found}. If the
4779 function is not found, run @var{action-if-not-found}.
4781 If linking with @var{library} results in unresolved symbols that would
4782 be resolved by linking with additional libraries, give those libraries
4783 as the @var{other-libraries} argument, separated by spaces:
4784 e.g., @option{-lXt -lX11}. Otherwise, this macro fails to detect
4785 that @var{function} is present, because linking the test program
4786 always fails with unresolved symbols.
4788 The result of this test is cached in the
4789 @code{ac_cv_search_@var{function}} variable as @samp{none required} if
4790 @var{function} is already available, as @samp{no} if no library
4791 containing @var{function} was found, otherwise as the
4792 @option{-l@var{library}} option that needs to be prepended to @code{LIBS}.
4797 @node Library Functions
4798 @section Library Functions
4800 The following macros check for particular C library functions.
4801 If there is no macro specifically defined to check for a function you need,
4802 and you don't need to check for any special properties of
4803 it, then you can use one of the general function-check macros.
4806 * Function Portability:: Pitfalls with usual functions
4807 * Particular Functions:: Special handling to find certain functions
4808 * Generic Functions:: How to find other functions
4811 @node Function Portability
4812 @subsection Portability of C Functions
4813 @cindex Portability of C functions
4814 @cindex C function portability
4816 Most usual functions can either be missing, or be buggy, or be limited
4817 on some architectures. This section tries to make an inventory of these
4818 portability issues. By definition, this list always requires
4819 additions. A much more complete list is maintained by the Gnulib
4820 project (@pxref{Gnulib}), covering @ref{Function Substitutes, ,
4821 Current Posix Functions, gnulib, Gnulib}, @ref{Legacy Function
4822 Substitutes, , Legacy Functions, gnulib, Gnulib}, and @ref{Glibc
4823 Function Substitutes, , Glibc Functions, gnulib, Gnulib}. Please
4824 help us keep the Gnulib list as complete as possible.
4829 @prindex @code{exit}
4830 On ancient hosts, @code{exit} returned @code{int}.
4831 This is because @code{exit} predates @code{void}, and there was a long
4832 tradition of it returning @code{int}.
4834 On current hosts, the problem more likely is that @code{exit} is not
4835 declared, due to C++ problems of some sort or another. For this reason
4836 we suggest that test programs not invoke @code{exit}, but return from
4837 @code{main} instead.
4843 @prindex @code{isinf}
4844 @prindex @code{isnan}
4845 In C99 and later, @code{isinf} and @code{isnan} are
4846 macros. On some systems just macros are available
4847 (e.g., HP-UX and Solaris 10), on
4848 some systems both macros and functions (e.g., glibc 2.3.2), and on some
4849 systems only functions (e.g., IRIX 6). In some cases
4850 these functions are declared in nonstandard headers like
4851 @code{<sunmath.h>} and defined in non-default libraries like
4852 @option{-lm} or @option{-lsunmath}.
4854 In C99 and later, @code{isinf} and @code{isnan} macros work correctly with
4855 @code{long double} arguments, but pre-C99 systems that use functions
4856 typically assume @code{double} arguments. On such a system,
4857 @code{isinf} incorrectly returns true for a finite @code{long double}
4858 argument that is outside the range of @code{double}.
4860 The best workaround for these issues is to use Gnulib modules
4861 @code{isinf} and @code{isnan} (@pxref{Gnulib}). But a lighter weight
4862 solution involves code like the following.
4869 (sizeof (x) == sizeof (long double) ? isnan_ld (x) \
4870 : sizeof (x) == sizeof (double) ? isnan_d (x) \
4872 static int isnan_f (float x) @{ return x != x; @}
4873 static int isnan_d (double x) @{ return x != x; @}
4874 static int isnan_ld (long double x) @{ return x != x; @}
4879 (sizeof (x) == sizeof (long double) ? isinf_ld (x) \
4880 : sizeof (x) == sizeof (double) ? isinf_d (x) \
4882 static int isinf_f (float x)
4883 @{ return !isnan (x) && isnan (x - x); @}
4884 static int isinf_d (double x)
4885 @{ return !isnan (x) && isnan (x - x); @}
4886 static int isinf_ld (long double x)
4887 @{ return !isnan (x) && isnan (x - x); @}
4891 Some optimizing compilers mishandle these definitions, but systems with that bug
4892 typically have many other floating point corner-case compliance problems
4893 anyway, so it's probably not worth worrying about.
4897 @prindex @code{malloc}
4898 The C standard says a successful call @code{malloc (0)} is implementation
4899 dependent. It can return either @code{NULL} or a new non-null pointer.
4900 The latter is more common (e.g., the GNU C Library) but is by
4901 no means universal. @code{AC_FUNC_MALLOC}
4902 can be used to insist on non-@code{NULL} (@pxref{Particular Functions}).
4906 @prindex @code{putenv}
4907 Posix prefers @code{setenv} to @code{putenv}; among other things,
4908 @code{putenv} is not required of all Posix implementations, but
4911 Posix specifies that @code{putenv} puts the given string directly in
4912 @code{environ}, but some systems make a copy of it instead (e.g.,
4913 glibc 2.0, or BSD). And when a copy is made, @code{unsetenv} might
4914 not free it, causing a memory leak (e.g., FreeBSD 4).
4916 On some systems @code{putenv ("FOO")} removes @samp{FOO} from the
4917 environment, but this is not standard usage and it dumps core
4918 on some systems (e.g., AIX).
4920 On MinGW, a call @code{putenv ("FOO=")} removes @samp{FOO} from the
4921 environment, rather than inserting it with an empty value.
4923 @item @code{realloc}
4925 @prindex @code{realloc}
4926 It is problematic to call @code{realloc} with a zero size.
4927 The C standard says @code{realloc (NULL, 0)} is equivalent to
4928 @code{malloc (0)}, which means one cannot portably tell whether the call
4929 has succeeded if it returns a null pointer. If @code{ptr} is non-null,
4930 the C standard says @code{realloc (ptr, 0)} has undefined behavior.
4932 The @code{AC_FUNC_REALLOC} macro avoids some of these portability issues,
4933 and the Gnulib module @code{realloc-gnu} avoids more of them.
4934 @xref{Particular Functions}.
4936 @item @code{signal} handler
4938 @prindex @code{signal}
4939 @prindex @code{sigaction}
4940 In most cases, it is more robust to use @code{sigaction} when it is
4941 available, rather than @code{signal}.
4943 @item @code{snprintf}
4944 @c @fuindex snprintf
4945 @prindex @code{snprintf}
4946 @c @fuindex vsnprintf
4947 @prindex @code{vsnprintf}
4948 In C99 and later, if the output array isn't big enough
4949 and if no other errors occur, @code{snprintf} and @code{vsnprintf}
4950 truncate the output and return the number of bytes that ought to have
4951 been produced. Some ancient systems returned the truncated length (e.g.,
4952 GNU C Library 2.0.x or IRIX 6.5), and some a negative value
4953 (e.g., earlier GNU C Library versions).
4955 @item @code{strerror_r}
4956 @c @fuindex strerror_r
4957 @prindex @code{strerror_r}
4958 Posix specifies that @code{strerror_r} returns an @code{int}, but many
4959 systems (e.g., GNU C Library version 2.36) provide a
4960 different version returning a @code{char *}. @code{AC_FUNC_STRERROR_R}
4961 can detect which is in use (@pxref{Particular Functions}).
4963 @item @code{strnlen}
4965 @prindex @code{strnlen}
4966 AIX 4.3 provided a broken version which produces the
4970 strnlen ("foobar", 0) = 0
4971 strnlen ("foobar", 1) = 3
4972 strnlen ("foobar", 2) = 2
4973 strnlen ("foobar", 3) = 1
4974 strnlen ("foobar", 4) = 0
4975 strnlen ("foobar", 5) = 6
4976 strnlen ("foobar", 6) = 6
4977 strnlen ("foobar", 7) = 6
4978 strnlen ("foobar", 8) = 6
4979 strnlen ("foobar", 9) = 6
4982 @item @code{sysconf}
4984 @prindex @code{sysconf}
4985 @code{_SC_PAGESIZE} is standard, but some older systems (e.g., HP-UX
4986 9) have @code{_SC_PAGE_SIZE} instead. This can be tested with
4991 @prindex @code{unlink}
4992 The Posix spec says that @code{unlink} causes the given file to be
4993 removed only after there are no more open file handles for it. Some
4994 non-Posix hosts have trouble with this requirement, though,
4995 and some DOS variants even corrupt the file system.
4997 @item @code{unsetenv}
4998 @c @fuindex unsetenv
4999 @prindex @code{unsetenv}
5000 On MinGW, @code{unsetenv} is not available, but a variable @samp{FOO}
5001 can be removed with a call @code{putenv ("FOO=")}, as described under
5002 @code{putenv} above.
5004 @item @code{va_copy}
5006 @prindex @code{va_copy}
5007 C99 and later provide @code{va_copy} for copying
5008 @code{va_list} variables. It may be available in older environments
5009 too, though possibly as @code{__va_copy} (e.g., @command{gcc} in strict
5010 pre-C99 mode). These can be tested with @code{#ifdef}. A fallback to
5011 @code{memcpy (&dst, &src, sizeof (va_list))} gives maximum
5014 @item @code{va_list}
5016 @prindex @code{va_list}
5017 @code{va_list} is not necessarily just a pointer. It can be a
5018 @code{struct} (e.g., @command{gcc} on Alpha), which means @code{NULL} is
5019 not portable. Or it can be an array (e.g., @command{gcc} in some
5020 PowerPC configurations), which means as a function parameter it can be
5021 effectively call-by-reference and library routines might modify the
5022 value back in the caller (e.g., @code{vsnprintf} in the GNU C Library
5025 @item Signed @code{>>}
5026 Normally the C @code{>>} right shift of a signed type replicates the
5027 high bit, giving a so-called ``arithmetic'' shift. But care should be
5028 taken since Standard C doesn't require that behavior. On a few platforms
5029 (e.g., Cray C by default) zero bits are shifted in, the same as a shift of an
5032 @item Integer @code{/}
5033 C divides signed integers by truncating their quotient toward zero,
5034 yielding the same result as Fortran. However, before C99 the standard
5035 allowed C implementations to take the floor or ceiling of the quotient
5036 in some cases. Hardly any implementations took advantage of this
5037 freedom, though, and it's probably not worth worrying about this issue
5042 @node Particular Functions
5043 @subsection Particular Function Checks
5044 @cindex Function, checking
5046 These macros check for particular C functions---whether they exist, and
5047 in some cases how they respond when given certain arguments.
5049 @anchor{AC_FUNC_ALLOCA}
5050 @defmac AC_FUNC_ALLOCA
5051 @acindex{FUNC_ALLOCA}
5053 @cvindex HAVE_ALLOCA_H
5056 @prindex @code{alloca}
5058 @c @caindex working_alloca_h
5059 Check for the @code{alloca} function. Define @code{HAVE_ALLOCA_H} if
5060 @file{alloca.h} defines a working @code{alloca}. If not, look for a
5061 builtin alternative. If either method succeeds, define
5062 @code{HAVE_ALLOCA}. Otherwise, set the output variable @code{ALLOCA} to
5063 @samp{$@{LIBOBJDIR@}alloca.o} and define
5064 @code{C_ALLOCA} (so programs can periodically call @samp{alloca (0)} to
5065 garbage collect). This variable is separate from @code{LIBOBJS} so
5066 multiple programs can share the value of @code{ALLOCA} without needing
5067 to create an actual library, in case only some of them use the code in
5068 @code{LIBOBJS}. The @samp{$@{LIBOBJDIR@}} prefix serves the same
5069 purpose as in @code{LIBOBJS} (@pxref{AC_LIBOBJ vs LIBOBJS}).
5071 Source files that use @code{alloca} should start with a piece of code
5072 like the following, to declare it properly.
5078 #ifdef HAVE_ALLOCA_H
5079 # include <alloca.h>
5080 #elif !defined alloca
5082 # define alloca __builtin_alloca
5083 # elif defined _MSC_VER
5084 # include <malloc.h>
5085 # define alloca _alloca
5086 # elif !defined HAVE_ALLOCA
5090 void *alloca (size_t);
5096 If you don't want to maintain this piece of code in your package manually,
5097 you can instead use the Gnulib module @code{alloca-opt} or @code{alloca}.
5101 @defmac AC_FUNC_CHOWN
5102 @acindex{FUNC_CHOWN}
5105 @prindex @code{chown}
5106 @caindex func_chown_works
5107 If the @code{chown} function is available and works (in particular, it
5108 should accept @option{-1} for @code{uid} and @code{gid}), define
5109 @code{HAVE_CHOWN}. The result of this macro is cached in the
5110 @code{ac_cv_func_chown_works} variable.
5112 If you want a workaround, that is, a @code{chown} function that is
5113 available and works, you can use the Gnulib module @code{chown}.
5117 @anchor{AC_FUNC_CLOSEDIR_VOID}
5118 @defmac AC_FUNC_CLOSEDIR_VOID
5119 @acindex{FUNC_CLOSEDIR_VOID}
5120 @cvindex CLOSEDIR_VOID
5121 @c @fuindex closedir
5122 @prindex @code{closedir}
5123 @caindex func_closedir_void
5124 If the @code{closedir} function does not return a meaningful value,
5125 define @code{CLOSEDIR_VOID}. Otherwise, callers ought to check its
5126 return value for an error indicator.
5128 Currently this test is implemented by running a test program. When
5129 cross compiling the pessimistic assumption that @code{closedir} does not
5130 return a meaningful value is made.
5132 The result of this macro is cached in the @code{ac_cv_func_closedir_void}
5135 This macro is obsolescent, as @code{closedir} returns a meaningful value
5136 on current systems. New programs need not use this macro.
5139 @defmac AC_FUNC_ERROR_AT_LINE
5140 @acindex{FUNC_ERROR_AT_LINE}
5141 @c @fuindex error_at_line
5142 @prindex @code{error_at_line}
5143 @caindex lib_error_at_line
5144 If the @code{error_at_line} function is not found, require an
5145 @code{AC_LIBOBJ} replacement of @samp{error}.
5147 The result of this macro is cached in the @code{ac_cv_lib_error_at_line}
5150 The @code{AC_FUNC_ERROR_AT_LINE} macro is obsolescent. New programs
5151 should use Gnulib's @code{error} module. @xref{Gnulib}.
5154 @defmac AC_FUNC_FNMATCH
5155 @acindex{FUNC_FNMATCH}
5157 @prindex @code{fnmatch}
5158 @caindex func_fnmatch_works
5159 If the @code{fnmatch} function conforms to Posix, define
5160 @code{HAVE_FNMATCH}.
5162 Unlike the other specific
5163 @code{AC_FUNC} macros, @code{AC_FUNC_FNMATCH} does not replace a
5164 broken/missing @code{fnmatch}. This is for historical reasons.
5165 See @code{AC_REPLACE_FNMATCH} below.
5167 The result of this macro is cached in the @code{ac_cv_func_fnmatch_works}
5170 This macro is obsolescent. New programs should use Gnulib's
5171 @code{fnmatch-posix} module. @xref{Gnulib}.
5174 @defmac AC_FUNC_FNMATCH_GNU
5175 @acindex{FUNC_FNMATCH_GNU}
5177 @prindex @code{fnmatch}
5178 @caindex func_fnmatch_gnu
5179 Behave like @code{AC_REPLACE_FNMATCH} (@emph{replace}) but also test
5180 whether @code{fnmatch} supports GNU extensions. Detect common
5181 implementation bugs, for example, the bugs in the GNU C
5184 The result of this macro is cached in the @code{ac_cv_func_fnmatch_gnu}
5187 This macro is obsolescent. New programs should use Gnulib's
5188 @code{fnmatch-gnu} module. @xref{Gnulib}.
5191 @anchor{AC_FUNC_FORK}
5192 @defmac AC_FUNC_FORK
5194 @cvindex HAVE_VFORK_H
5195 @cvindex HAVE_WORKING_FORK
5196 @cvindex HAVE_WORKING_VFORK
5199 @prindex @code{fork}
5201 @prindex @code{vfork}
5203 @c @caindex func_fork
5204 @c @caindex func_fork_works
5205 This macro checks for the @code{fork} and @code{vfork} functions. If a
5206 working @code{fork} is found, define @code{HAVE_WORKING_FORK}. This macro
5207 checks whether @code{fork} is just a stub by trying to run it.
5209 If @file{vfork.h} is found, define @code{HAVE_VFORK_H}. If a working
5210 @code{vfork} is found, define @code{HAVE_WORKING_VFORK}. Otherwise,
5211 define @code{vfork} to be @code{fork} for backward compatibility with
5212 previous versions of @command{autoconf}. This macro checks for several known
5213 errors in implementations of @code{vfork} and considers the system to not
5214 have a working @code{vfork} if it detects any of them.
5216 Since this macro defines @code{vfork} only for backward compatibility with
5217 previous versions of @command{autoconf} you're encouraged to define it
5218 yourself in new code:
5221 #ifndef HAVE_WORKING_VFORK
5227 The results of this macro are cached in the @code{ac_cv_func_fork_works}
5228 and @code{ac_cv_func_vfork_works} variables. In order to override the
5229 test, you also need to set the @code{ac_cv_func_fork} and
5230 @code{ac_cv_func_vfork} variables.
5233 @anchor{AC_FUNC_FSEEKO}
5234 @defmac AC_FUNC_FSEEKO
5235 @acindex{FUNC_FSEEKO}
5236 @cvindex _LARGEFILE_SOURCE
5237 @cvindex HAVE_FSEEKO
5239 @prindex @code{fseeko}
5241 @prindex @code{ftello}
5242 @c @caindex sys_largefile_source
5243 If the @code{fseeko} and @code{ftello} functions are available, define
5244 @code{HAVE_FSEEKO}. Define @code{_LARGEFILE_SOURCE} if necessary to
5245 make the prototype visible.
5247 Configure scripts that use @code{AC_FUNC_FSEEKO} should normally also
5248 use @code{AC_SYS_LARGEFILE} to ensure that @code{off_t} can represent
5249 all supported file sizes. @xref{AC_SYS_LARGEFILE}.
5251 The Gnulib module @code{fseeko} invokes @code{AC_FUNC_FSEEKO}
5252 and also contains workarounds for other portability problems of
5253 @code{fseeko}. @xref{Gnulib}.
5256 @defmac AC_FUNC_GETGROUPS
5257 @acindex{FUNC_GETGROUPS}
5258 @cvindex HAVE_GETGROUPS
5259 @ovindex GETGROUPS_LIB
5260 @c @fuindex getgroups
5261 @prindex @code{getgroups}
5262 @caindex func_getgroups_works
5263 Perform all the checks performed by @code{AC_TYPE_GETGROUPS}
5264 (@pxref{AC_TYPE_GETGROUPS}).
5265 Then, if the @code{getgroups} function is available
5266 and known to work correctly, define @code{HAVE_GETGROUPS}.
5267 Set the output variable @code{GETGROUPS_LIB} to any libraries
5268 needed to get that function.
5270 This macro relies on a list of systems with known, serious bugs in
5271 @code{getgroups}. If this list mis-identifies your system's
5272 @code{getgroups} as buggy, or as not buggy, you can override it by
5273 setting the cache variable @code{ac_cv_func_getgroups_works} in a
5274 @file{config.site} file (@pxref{Site Defaults}). Please also report the
5275 error to @email{bug-autoconf@@gnu.org, the Autoconf Bugs mailing list}.
5277 The Gnulib module @code{getgroups} provides workarounds for additional,
5278 less severe portability problems with this function.
5281 @anchor{AC_FUNC_GETLOADAVG}
5282 @defmac AC_FUNC_GETLOADAVG
5283 @acindex{FUNC_GETLOADAVG}
5288 @cvindex HAVE_NLIST_H
5289 @cvindex NLIST_NAME_UNION
5290 @cvindex GETLOADAVG_PRIVILEGED
5291 @cvindex NEED_SETGID
5292 @cvindex C_GETLOADAVG
5294 @ovindex NEED_SETGID
5296 @ovindex GETLOADAVG_LIBS
5297 @c @fuindex getloadavg
5298 @prindex @code{getloadavg}
5299 Check how to get the system load averages. To perform its tests
5300 properly, this macro needs the file @file{getloadavg.c}; therefore, be
5301 sure to set the @code{AC_LIBOBJ} replacement directory properly (see
5302 @ref{Generic Functions}, @code{AC_CONFIG_LIBOBJ_DIR}).
5304 If the system has the @code{getloadavg} function, define
5305 @code{HAVE_GETLOADAVG}, and set @code{GETLOADAVG_LIBS} to any libraries
5306 necessary to get that function. Also add @code{GETLOADAVG_LIBS} to
5307 @code{LIBS}. Otherwise, require an @code{AC_LIBOBJ} replacement for
5308 @samp{getloadavg} and possibly define several other C preprocessor
5309 macros and output variables:
5313 Define @code{C_GETLOADAVG}.
5316 Define @code{SVR4}, @code{DGUX}, @code{UMAX}, or @code{UMAX4_3} if on
5321 If @file{nlist.h} is found, define @code{HAVE_NLIST_H}.
5324 If @samp{struct nlist} has an @samp{n_un.n_name} member, define
5325 @code{HAVE_STRUCT_NLIST_N_UN_N_NAME}. The obsolete symbol
5326 @code{NLIST_NAME_UNION} is still defined, but do not depend upon it.
5329 Programs may need to be installed set-group-ID (or set-user-ID) for
5330 @code{getloadavg} to work. In this case, define
5331 @code{GETLOADAVG_PRIVILEGED}, set the output variable @code{NEED_SETGID}
5332 to @samp{true} (and otherwise to @samp{false}), and set
5333 @code{KMEM_GROUP} to the name of the group that should own the installed
5337 The @code{AC_FUNC_GETLOADAVG} macro is obsolescent. New programs should
5338 use Gnulib's @code{getloadavg} module. @xref{Gnulib}.
5341 @anchor{AC_FUNC_GETMNTENT}
5342 @defmac AC_FUNC_GETMNTENT
5343 @acindex{FUNC_GETMNTENT}
5344 @cvindex HAVE_GETMNTENT
5345 @c @fuindex getmntent
5346 @prindex @code{getmntent}
5347 @caindex search_getmntent
5348 Check for @code{getmntent} in the standard C library, and then in the
5349 @file{sun}, @file{seq}, and @file{gen} libraries, for UNICOS,
5350 IRIX 4, PTX, and UnixWare, respectively. Then, if
5351 @code{getmntent} is available, define @code{HAVE_GETMNTENT} and set
5352 @code{ac_cv_func_getmntent} to @code{yes}. Otherwise set
5353 @code{ac_cv_func_getmntent} to @code{no}.
5355 The result of this macro can be overridden by setting the cache variable
5356 @code{ac_cv_search_getmntent}.
5358 The @code{AC_FUNC_GETMNTENT} macro is obsolescent. New programs should
5359 use Gnulib's @code{mountlist} module. @xref{Gnulib}.
5362 @defmac AC_FUNC_GETPGRP
5363 @acindex{FUNC_GETPGRP}
5364 @cvindex GETPGRP_VOID
5367 @prindex @code{getpgid}
5368 @prindex @code{getpgrp}
5369 @caindex func_getpgrp_void
5370 Define @code{GETPGRP_VOID} if it is an error to pass 0 to
5371 @code{getpgrp}; this is the Posix behavior. On older BSD
5372 systems, you must pass 0 to @code{getpgrp}, as it takes an argument and
5373 behaves like Posix's @code{getpgid}.
5383 This macro does not check whether
5384 @code{getpgrp} exists at all; if you need to work in that situation,
5385 first call @code{AC_CHECK_FUNC} for @code{getpgrp}.
5387 The result of this macro is cached in the @code{ac_cv_func_getpgrp_void}
5390 This macro is obsolescent, as current systems have a @code{getpgrp}
5391 whose signature conforms to Posix. New programs need not use this macro.
5394 @defmac AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
5395 @acindex{FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK}
5396 @cvindex LSTAT_FOLLOWS_SLASHED_SYMLINK
5398 @prindex @code{lstat}
5399 @caindex func_lstat_dereferences_slashed_symlink
5400 If @file{link} is a symbolic link, then @code{lstat} should treat
5401 @file{link/} the same as @file{link/.}. However, many older
5402 @code{lstat} implementations incorrectly ignore trailing slashes.
5404 It is safe to assume that if @code{lstat} incorrectly ignores
5405 trailing slashes, then other symbolic-link-aware functions like
5406 @code{unlink} also incorrectly ignore trailing slashes.
5408 If @code{lstat} behaves properly, define
5409 @code{LSTAT_FOLLOWS_SLASHED_SYMLINK}, otherwise require an
5410 @code{AC_LIBOBJ} replacement of @code{lstat}.
5412 The result of this macro is cached in the
5413 @code{ac_cv_func_lstat_dereferences_slashed_symlink} variable.
5415 The @code{AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK} macro is obsolescent.
5416 New programs should use Gnulib's @code{lstat} module. @xref{Gnulib}.
5419 @defmac AC_FUNC_MALLOC
5420 @acindex{FUNC_MALLOC}
5421 @cvindex HAVE_MALLOC
5424 @prindex @code{malloc}
5425 @caindex func_malloc_0_nonnull
5426 If the @code{malloc} function is compatible with the GNU C
5427 library @code{malloc} (i.e., @samp{malloc (0)} returns a valid
5428 pointer), define @code{HAVE_MALLOC} to 1. Otherwise define
5429 @code{HAVE_MALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
5430 @samp{malloc}, and define @code{malloc} to @code{rpl_malloc} so that the
5431 native @code{malloc} is not used in the main project.
5433 Typically, the replacement file @file{malloc.c} should look like (note
5434 the @samp{#undef malloc}):
5442 /* Allocate an N-byte block of memory from the heap.
5443 If N is zero, allocate a 1-byte block. */
5446 rpl_malloc (size_t n)
5454 The result of this macro is cached in the
5455 @code{ac_cv_func_malloc_0_nonnull} variable.
5457 If you don't want to maintain a @code{malloc.c} file in your package
5458 manually, you can instead use the Gnulib module @code{malloc-gnu}.
5461 @defmac AC_FUNC_MBRTOWC
5462 @acindex{FUNC_MBRTOWC}
5463 @cvindex HAVE_MBRTOWC
5465 @prindex @code{mbrtowc}
5466 @caindex func_mbrtowc
5467 Define @code{HAVE_MBRTOWC} to 1 if the function @code{mbrtowc} and the
5468 type @code{mbstate_t} are properly declared.
5470 The result of this macro is cached in the @code{ac_cv_func_mbrtowc}
5473 The Gnulib module @code{mbrtowc} not only ensures that the
5474 function is declared, but also works around other portability
5475 problems of this function.
5478 @defmac AC_FUNC_MEMCMP
5479 @acindex{FUNC_MEMCMP}
5482 @prindex @code{memcmp}
5483 @caindex func_memcmp_working
5484 If the @code{memcmp} function is not available or does not work, require an
5485 @code{AC_LIBOBJ} replacement for @samp{memcmp}.
5487 The result of this macro is cached in the
5488 @code{ac_cv_func_memcmp_working} variable.
5490 This macro is obsolescent, as current systems have a working
5491 @code{memcmp}. New programs need not use this macro.
5494 @defmac AC_FUNC_MKTIME
5495 @acindex{FUNC_MKTIME}
5498 @prindex @code{mktime}
5499 @caindex func_working_mktime
5500 If the @code{mktime} function is not available, or does not work
5501 correctly, require an @code{AC_LIBOBJ} replacement for @samp{mktime}.
5502 For the purposes of this test, @code{mktime} should conform to the
5503 Posix standard and should be the inverse of
5506 The result of this macro is cached in the
5507 @code{ac_cv_func_working_mktime} variable.
5509 The @code{AC_FUNC_MKTIME} macro is obsolescent. New programs should
5510 use Gnulib's @code{mktime} module. @xref{Gnulib}.
5513 @anchor{AC_FUNC_MMAP}
5514 @defmac AC_FUNC_MMAP
5518 @prindex @code{mmap}
5519 @caindex func_mmap_fixed_mapped
5520 If the @code{mmap} function exists and works correctly, define
5521 @code{HAVE_MMAP}. This checks only private fixed mapping of already-mapped
5524 The result of this macro is cached in the
5525 @code{ac_cv_func_mmap_fixed_mapped} variable.
5527 Note: This macro asks for more than what an average program needs from
5528 @code{mmap}. In particular, the use of @code{MAP_FIXED} fails on
5529 HP-UX 11, whereas @code{mmap} otherwise works fine on this platform.
5532 @defmac AC_FUNC_OBSTACK
5533 @acindex{FUNC_OBSTACK}
5534 @cvindex HAVE_OBSTACK
5536 @caindex func_obstack
5537 If the obstacks are found, define @code{HAVE_OBSTACK}, else require an
5538 @code{AC_LIBOBJ} replacement for @samp{obstack}.
5540 The result of this macro is cached in the @code{ac_cv_func_obstack}
5543 The @code{AC_FUNC_OBSTACK} macro is obsolescent. New programs should use
5544 Gnulib's @code{obstack} module. @xref{Gnulib}.
5547 @defmac AC_FUNC_REALLOC
5548 @acindex{FUNC_REALLOC}
5549 @cvindex HAVE_REALLOC
5552 @prindex @code{realloc}
5553 @caindex func_realloc_0_nonnull
5554 If the @code{realloc} function is compatible with the GNU C
5555 library @code{realloc} (i.e., @samp{realloc (NULL, 0)} returns a
5556 valid pointer), define @code{HAVE_REALLOC} to 1. Otherwise define
5557 @code{HAVE_REALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
5558 @samp{realloc}, and define @code{realloc} to @code{rpl_realloc} so that
5559 the native @code{realloc} is not used in the main project. See
5560 @code{AC_FUNC_MALLOC} for details.
5562 The result of this macro is cached in the
5563 @code{ac_cv_func_realloc_0_nonnull} variable.
5565 If you don't want to maintain a @code{realloc.c} file in your package
5566 manually, you can instead use the Gnulib module @code{realloc-gnu}.
5569 @defmac AC_FUNC_SELECT_ARGTYPES
5570 @acindex{FUNC_SELECT_ARGTYPES}
5571 @cvindex SELECT_TYPE_ARG1
5572 @cvindex SELECT_TYPE_ARG234
5573 @cvindex SELECT_TYPE_ARG5
5575 @prindex @code{select}
5576 @c @caindex func_select_args
5577 Determines the correct type to be passed for each of the
5578 @code{select} function's arguments, and defines those types
5579 in @code{SELECT_TYPE_ARG1}, @code{SELECT_TYPE_ARG234}, and
5580 @code{SELECT_TYPE_ARG5} respectively. @code{SELECT_TYPE_ARG1} defaults
5581 to @samp{int}, @code{SELECT_TYPE_ARG234} defaults to @samp{int *},
5582 and @code{SELECT_TYPE_ARG5} defaults to @samp{struct timeval *}.
5584 This macro is obsolescent, as current systems have a @code{select} whose
5585 signature conforms to Posix. New programs need not use this macro.
5588 @defmac AC_FUNC_SETPGRP
5589 @acindex{FUNC_SETPGRP}
5590 @cvindex SETPGRP_VOID
5592 @prindex @code{setpgrp}
5593 @caindex func_setpgrp_void
5594 If @code{setpgrp} takes no argument (the Posix version), define
5595 @code{SETPGRP_VOID}. Otherwise, it is the BSD version, which takes
5596 two process IDs as arguments. This macro does not check whether
5597 @code{setpgrp} exists at all; if you need to work in that situation,
5598 first call @code{AC_CHECK_FUNC} for @code{setpgrp}. This macro also
5599 does not check for the Solaris variant of @code{setpgrp}, which returns
5600 a @code{pid_t} instead of an @code{int}; portable code should only use
5601 the return value by comparing it against @code{-1} to check for errors.
5603 The result of this macro is cached in the @code{ac_cv_func_setpgrp_void}
5606 This macro is obsolescent, as all forms of @code{setpgrp} are also
5607 obsolescent. New programs should use the Posix function @code{setpgid},
5608 which takes two process IDs as arguments (like the BSD @code{setpgrp}).
5611 @defmac AC_FUNC_STAT
5612 @defmacx AC_FUNC_LSTAT
5614 @acindex{FUNC_LSTAT}
5615 @cvindex HAVE_STAT_EMPTY_STRING_BUG
5616 @cvindex HAVE_LSTAT_EMPTY_STRING_BUG
5618 @prindex @code{stat}
5620 @prindex @code{lstat}
5621 @caindex func_stat_empty_string_bug
5622 @caindex func_lstat_empty_string_bug
5623 Determine whether @code{stat} or @code{lstat} have the bug that it
5624 succeeds when given the zero-length file name as argument.
5626 If it does, then define @code{HAVE_STAT_EMPTY_STRING_BUG} (or
5627 @code{HAVE_LSTAT_EMPTY_STRING_BUG}) and ask for an @code{AC_LIBOBJ}
5630 The results of these macros are cached in the
5631 @code{ac_cv_func_stat_empty_string_bug} and the
5632 @code{ac_cv_func_lstat_empty_string_bug} variables, respectively.
5634 These macros are obsolescent, as no current systems have the bug.
5635 New programs need not use these macros.
5638 @anchor{AC_FUNC_STRCOLL}
5639 @defmac AC_FUNC_STRCOLL
5640 @acindex{FUNC_STRCOLL}
5641 @cvindex HAVE_STRCOLL
5643 @prindex @code{strcoll}
5644 @caindex func_strcoll_works
5645 If the @code{strcoll} function exists and works correctly, define
5646 @code{HAVE_STRCOLL}. This does a bit more than
5647 @samp{AC_CHECK_FUNCS(strcoll)}, because some systems have incorrect
5648 definitions of @code{strcoll} that should not be used. But it does
5649 not check against a known bug of this function on Solaris 10.
5651 The result of this macro is cached in the @code{ac_cv_func_strcoll_works}
5655 @defmac AC_FUNC_STRERROR_R
5656 @acindex{FUNC_STRERROR_R}
5657 @cvindex HAVE_STRERROR_R
5658 @cvindex HAVE_DECL_STRERROR_R
5659 @cvindex STRERROR_R_CHAR_P
5660 @c @fuindex strerror_r
5661 @caindex func_strerror_r_char_p
5662 @prindex @code{strerror_r}
5663 If @code{strerror_r} is available, define @code{HAVE_STRERROR_R}, and if
5664 it is declared, define @code{HAVE_DECL_STRERROR_R}. If it returns a
5665 @code{char *} message, define @code{STRERROR_R_CHAR_P}; otherwise it
5666 returns an @code{int} error number. The Thread-Safe Functions option of
5667 Posix requires @code{strerror_r} to return @code{int}, but
5668 many systems (including, for example, version 2.2.4 of the GNU C
5669 Library) return a @code{char *} value that is not necessarily equal to
5670 the buffer argument.
5672 The result of this macro is cached in the
5673 @code{ac_cv_func_strerror_r_char_p} variable.
5675 The Gnulib module @code{strerror_r} not only ensures that the function
5676 has the return type specified by Posix, but also works around other
5677 portability problems of this function.
5680 @anchor{AC_FUNC_STRFTIME}
5681 @defmac AC_FUNC_STRFTIME
5682 @acindex{FUNC_STRFTIME}
5683 @cvindex HAVE_STRFTIME
5684 @c @fuindex strftime
5685 @prindex @code{strftime}
5686 Check for @code{strftime} in the @file{intl} library, for SCO Unix.
5687 Then, if @code{strftime} is available, define @code{HAVE_STRFTIME}.
5689 This macro is obsolescent, as no current systems require the @file{intl}
5690 library for @code{strftime}. New programs need not use this macro.
5693 @defmac AC_FUNC_STRTOD
5694 @acindex{FUNC_STRTOD}
5697 @prindex @code{strtod}
5698 @caindex func_strtod
5700 If the @code{strtod} function does not exist or doesn't work correctly,
5701 ask for an @code{AC_LIBOBJ} replacement of @samp{strtod}. In this case,
5702 because @file{strtod.c} is likely to need @samp{pow}, set the output
5703 variable @code{POW_LIB} to the extra library needed.
5705 This macro caches its result in the @code{ac_cv_func_strtod} variable
5706 and depends upon the result in the @code{ac_cv_func_pow} variable.
5708 The @code{AC_FUNC_STRTOD} macro is obsolescent. New programs should
5709 use Gnulib's @code{strtod} module. @xref{Gnulib}.
5712 @defmac AC_FUNC_STRTOLD
5713 @acindex{FUNC_STRTOLD}
5714 @cvindex HAVE_STRTOLD
5715 @prindex @code{strtold}
5716 @caindex func_strtold
5717 If the @code{strtold} function exists and conforms to C99 or later, define
5718 @code{HAVE_STRTOLD}.
5720 This macro caches its result in the @code{ac_cv_func_strtold} variable.
5722 The Gnulib module @code{strtold} not only ensures that the
5723 function exists, but also works around other portability
5724 problems of this function.
5727 @defmac AC_FUNC_STRNLEN
5728 @acindex{FUNC_STRNLEN}
5729 @cvindex HAVE_STRNLEN
5731 @prindex @code{strnlen}
5732 @caindex func_strnlen_working
5733 If the @code{strnlen} function is not available, or is buggy (like the one
5734 from AIX 4.3), require an @code{AC_LIBOBJ} replacement for it.
5736 This macro caches its result in the @code{ac_cv_func_strnlen_working}
5739 The @code{AC_FUNC_STRNLEN} macro is obsolescent. New programs should
5740 use Gnulib's @code{strnlen} module. @xref{Gnulib}.
5743 @anchor{AC_FUNC_UTIME_NULL}
5744 @defmac AC_FUNC_UTIME_NULL
5745 @acindex{FUNC_UTIME_NULL}
5746 @cvindex HAVE_UTIME_NULL
5748 @prindex @code{utime}
5749 @caindex func_utime_null
5750 If @samp{utime (@var{file}, NULL)} sets @var{file}'s timestamp to
5751 the present, define @code{HAVE_UTIME_NULL}.
5753 This macro caches its result in the @code{ac_cv_func_utime_null}
5756 This macro is obsolescent, as all current systems have a @code{utime}
5757 that behaves this way. New programs need not use this macro.
5760 @anchor{AC_FUNC_VPRINTF}
5761 @defmac AC_FUNC_VPRINTF
5762 @acindex{FUNC_VPRINTF}
5763 @cvindex HAVE_VPRINTF
5764 @cvindex HAVE_DOPRNT
5766 @prindex @code{vprintf}
5767 @c @fuindex vsprintf
5768 @prindex @code{vsprintf}
5769 If @code{vprintf} is found, define @code{HAVE_VPRINTF}. Otherwise, if
5770 @code{_doprnt} is found, define @code{HAVE_DOPRNT}. (If @code{vprintf}
5771 is available, you may assume that @code{vfprintf} and @code{vsprintf}
5772 are also available.)
5774 This macro is obsolescent, as all current systems have @code{vprintf}.
5775 New programs need not use this macro.
5778 @defmac AC_REPLACE_FNMATCH
5779 @acindex{REPLACE_FNMATCH}
5781 @prindex @code{fnmatch}
5782 @hdrindex{fnmatch.h}
5783 @caindex func_fnmatch_works
5784 If the @code{fnmatch} function does not conform to Posix (see
5785 @code{AC_FUNC_FNMATCH}), ask for its @code{AC_LIBOBJ} replacement.
5787 The files @file{fnmatch.c}, @file{fnmatch_loop.c}, and @file{fnmatch_.h}
5788 in the @code{AC_LIBOBJ} replacement directory are assumed to contain a
5789 copy of the source code of GNU @code{fnmatch}. If necessary,
5790 this source code is compiled as an @code{AC_LIBOBJ} replacement, and the
5791 @file{fnmatch_.h} file is linked to @file{fnmatch.h} so that it can be
5792 included in place of the system @code{<fnmatch.h>}.
5794 This macro caches its result in the @code{ac_cv_func_fnmatch_works}
5797 This macro is obsolescent, as it assumes the use of particular source
5798 files. New programs should use Gnulib's @code{fnmatch-posix} module,
5799 which provides this macro along with the source files. @xref{Gnulib}.
5804 @node Generic Functions
5805 @subsection Generic Function Checks
5807 These macros are used to find functions not covered by the ``particular''
5808 test macros. If the functions might be in libraries other than the
5809 default C library, first call @code{AC_CHECK_LIB} for those libraries.
5810 If you need to check the behavior of a function as well as find out
5811 whether it is present, you have to write your own test for
5812 it (@pxref{Writing Tests}).
5814 @anchor{AC_CHECK_FUNC}
5815 @defmac AC_CHECK_FUNC (@var{function}, @ovar{action-if-found}, @
5816 @ovar{action-if-not-found})
5817 @acindex{CHECK_FUNC}
5818 @caindex func_@var{function}
5819 If C function @var{function} is available, run shell commands
5820 @var{action-if-found}, otherwise @var{action-if-not-found}. If you just
5821 want to define a symbol if the function is available, consider using
5822 @code{AC_CHECK_FUNCS} instead. This macro checks for functions with C
5823 linkage even when @code{AC_LANG(C++)} has been called, since C is more
5824 standardized than C++. (@pxref{Language Choice}, for more information
5825 about selecting the language for checks.)
5827 This macro caches its result in the @code{ac_cv_func_@var{function}}
5831 @anchor{AC_CHECK_FUNCS}
5832 @defmac AC_CHECK_FUNCS (@var{function}@dots{}, @ovar{action-if-found}, @
5833 @ovar{action-if-not-found})
5834 @acindex{CHECK_FUNCS}
5835 @cvindex HAVE_@var{function}
5836 For each @var{function} enumerated in the blank-or-newline-separated argument
5837 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5838 If @var{action-if-found} is given, it is additional shell code to
5839 execute when one of the functions is found. You can give it a value of
5840 @samp{break} to break out of the loop on the first match. If
5841 @var{action-if-not-found} is given, it is executed when one of the
5842 functions is not found.
5844 Results are cached for each @var{function} as in @code{AC_CHECK_FUNC}.
5847 @defmac AC_CHECK_FUNCS_ONCE (@var{function}@dots{})
5848 @acindex{CHECK_FUNCS_ONCE}
5849 @cvindex HAVE_@var{function}
5850 For each @var{function} enumerated in the blank-or-newline-separated argument
5851 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5852 This is a once-only variant of @code{AC_CHECK_FUNCS}. It generates the
5853 checking code at most once, so that @command{configure} is smaller and
5854 faster; but the checks cannot be conditionalized and are always done once,
5855 early during the @command{configure} run.
5860 Autoconf follows a philosophy that was formed over the years by those
5861 who have struggled for portability: isolate the portability issues in
5862 specific files, and then program as if you were in a Posix
5863 environment. Some functions may be missing or unfixable, and your
5864 package must be ready to replace them.
5866 Suitable replacements for many such problem functions are available from
5867 Gnulib (@pxref{Gnulib}).
5869 @defmac AC_LIBOBJ (@var{function})
5872 Specify that @samp{@var{function}.c} must be included in the executables
5873 to replace a missing or broken implementation of @var{function}.
5876 Technically, it adds @samp{@var{function}.$ac_objext} to the output
5877 variable @code{LIBOBJS} if it is not already in, and calls
5878 @code{AC_LIBSOURCE} for @samp{@var{function}.c}. You should not
5879 directly change @code{LIBOBJS}, since this is not traceable.
5882 @defmac AC_LIBSOURCE (@var{file})
5884 Specify that @var{file} might be needed to compile the project. If you
5885 need to know what files might be needed by a @file{configure.ac}, you
5886 should trace @code{AC_LIBSOURCE}. @var{file} must be a literal.
5888 This macro is called automatically from @code{AC_LIBOBJ}, but you must
5889 call it explicitly if you pass a shell variable to @code{AC_LIBOBJ}. In
5890 that case, since shell variables cannot be traced statically, you must
5891 pass to @code{AC_LIBSOURCE} any possible files that the shell variable
5892 might cause @code{AC_LIBOBJ} to need. For example, if you want to pass
5893 a variable @code{$foo_or_bar} to @code{AC_LIBOBJ} that holds either
5894 @code{"foo"} or @code{"bar"}, you should do:
5897 AC_LIBSOURCE([foo.c])
5898 AC_LIBSOURCE([bar.c])
5899 AC_LIBOBJ([$foo_or_bar])
5903 There is usually a way to avoid this, however, and you are encouraged to
5904 simply call @code{AC_LIBOBJ} with literal arguments.
5906 Note that this macro replaces the obsolete @code{AC_LIBOBJ_DECL}, with
5907 slightly different semantics: the old macro took the function name,
5908 e.g., @code{foo}, as its argument rather than the file name.
5911 @defmac AC_LIBSOURCES (@var{files})
5912 @acindex{LIBSOURCES}
5913 Like @code{AC_LIBSOURCE}, but accepts one or more @var{files} in a
5914 comma-separated M4 list. Thus, the above example might be rewritten:
5917 AC_LIBSOURCES([foo.c, bar.c])
5918 AC_LIBOBJ([$foo_or_bar])
5922 @defmac AC_CONFIG_LIBOBJ_DIR (@var{directory})
5923 @acindex{CONFIG_LIBOBJ_DIR}
5924 Specify that @code{AC_LIBOBJ} replacement files are to be found in
5925 @var{directory}, a name relative to the top level of the
5926 source tree. The replacement directory defaults to @file{.}, the top
5927 level directory, and the most typical value is @file{lib}, corresponding
5928 to @samp{AC_CONFIG_LIBOBJ_DIR([lib])}.
5930 @command{configure} might need to know the replacement directory for the
5931 following reasons: (i) some checks use the replacement files, (ii) some
5932 macros bypass broken system headers by installing links to the
5933 replacement headers (iii) when used in conjunction with Automake,
5934 within each makefile, @var{directory} is used as a relative path
5935 from @code{$(top_srcdir)} to each object named in @code{LIBOBJS} and
5936 @code{LTLIBOBJS}, etc.
5941 It is common to merely check for the existence of a function, and ask
5942 for its @code{AC_LIBOBJ} replacement if missing. The following macro is
5943 a convenient shorthand.
5945 @defmac AC_REPLACE_FUNCS (@var{function}@dots{})
5946 @acindex{REPLACE_FUNCS}
5947 @cvindex HAVE_@var{function}
5949 Like @code{AC_CHECK_FUNCS}, but uses @samp{AC_LIBOBJ(@var{function})} as
5950 @var{action-if-not-found}. You can declare your replacement function by
5951 enclosing the prototype in @samp{#ifndef HAVE_@var{function}}. If the
5952 system has the function, it probably declares it in a header file you
5953 should be including, so you shouldn't redeclare it lest your declaration
5958 @section Header Files
5959 @cindex Header, checking
5961 The following macros check for the presence of certain C header files.
5962 If there is no macro specifically defined to check for a header file you need,
5963 and you don't need to check for any special properties of
5964 it, then you can use one of the general header-file check macros.
5967 * Header Portability:: Collected knowledge on common headers
5968 * Particular Headers:: Special handling to find certain headers
5969 * Generic Headers:: How to find other headers
5972 @node Header Portability
5973 @subsection Portability of Headers
5974 @cindex Portability of headers
5975 @cindex Header portability
5977 This section documents some collected knowledge about common headers,
5978 and the problems they cause. By definition, this list always requires
5979 additions. A much more complete list is maintained by the Gnulib
5980 project (@pxref{Gnulib}), covering @ref{Header File Substitutes, ,
5981 Posix Headers, gnulib, Gnulib} and @ref{Glibc Header File
5982 Substitutes, , Glibc Headers, gnulib, Gnulib}. Please help us keep
5983 the Gnulib list as complete as possible.
5985 When we say that a header ``may require'' some set of other headers, we
5986 mean that it may be necessary for you to manually include those other
5987 headers first, or the contents of the header under test will fail to
5988 compile. When checking for these headers, you must provide the
5989 potentially-required headers in the @var{includes} argument to
5990 @code{AC_CHECK_HEADER} or @code{AC_CHECK_HEADERS}, or the check will
5991 fail spuriously. @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes})
5992 arranges to include a number of common requirements and should normally
5993 come first in your @var{includes}. For example, @file{net/if.h} may
5994 require @file{sys/types.h}, @file{sys/socket.h}, or both, and
5995 @code{AC_INCLUDES_DEFAULT} handles @file{sys/types.h} but not
5996 @file{sys/socket.h}, so you should check for it like this:
5999 AC_CHECK_HEADERS([sys/socket.h])
6000 AC_CHECK_HEADERS([net/if.h], [], [],
6001 [AC_INCLUDES_DEFAULT[
6002 #ifdef HAVE_SYS_SOCKET_H
6003 # include <sys/socket.h>
6008 Note that the example mixes single quoting (for@code{AC_INCLUDES_DEFAULT},
6009 so that it gets expanded) and double quoting (to ensure that each
6010 preprocessor @code{#} gets treated as a literal string rather than a
6015 @item @file{limits.h}
6016 In C99 and later, @file{limits.h} defines @code{LLONG_MIN},
6017 @code{LLONG_MAX}, and @code{ULLONG_MAX}, but many almost-C99
6018 environments (e.g., default GCC 4.0.2 + glibc 2.4) do not
6021 @item @file{memory.h}
6023 This header file is obsolete; use @file{string.h} instead.
6025 @item @file{strings.h}
6026 @hdrindex{strings.h}
6027 On some systems, this is the only header that declares
6028 @code{strcasecmp}, @code{strncasecmp}, and @code{ffs}.
6030 This header may or may not include @file{string.h} for you. However, on
6031 all recent systems it is safe to include both @file{string.h} and
6032 @file{strings.h}, in either order, in the same source file.
6034 @item @file{inttypes.h} vs.@: @file{stdint.h}
6035 @hdrindex{inttypes.h}
6037 C99 specifies that @file{inttypes.h} includes @file{stdint.h}, so there's
6038 no need to include @file{stdint.h} separately in a standard environment.
6039 However, some implementations have @file{stdint.h} but not @file{inttypes.h}
6040 (e.g. MSVC 2012). Therefore, it is necessary to check for each and include
6041 each only if available.
6043 @item @file{linux/irda.h}
6044 @hdrindex{linux/irda.h}
6045 This header may require @file{linux/types.h} and/or @file{sys/socket.h}.
6047 @item @file{linux/random.h}
6048 @hdrindex{linux/random.h}
6049 This header may require @file{linux/types.h}.
6051 @item @file{net/if.h}
6053 This header may require @file{sys/types.h} and/or @file{sys/socket.h}.
6055 @item @file{netinet/if_ether.h}
6056 @hdrindex{netinet/if_ether.h}
6057 This header may require some combination of @file{sys/types.h},
6058 @file{sys/socket.h}, @file{netinet/in.h}, and @file{net/if.h}.
6060 @item @file{sys/mount.h}
6061 @hdrindex{sys/mount.h}
6062 This header may require @file{sys/params.h}.
6064 @item @file{sys/ptem.h}
6065 @hdrindex{sys/ptem.h}
6066 This header may require @file{sys/stream.h}.
6068 @item @file{sys/socket.h}
6069 @hdrindex{sys/socket.h}
6070 This header may require @file{sys/types.h}.
6072 @item @file{sys/ucred.h}
6073 @hdrindex{sys/ucred.h}
6074 This header may require @file{sys/types.h}.
6076 @item @file{X11/extensions/scrnsaver.h}
6077 @hdrindex{X11/extensions/scrnsaver.h}
6078 Using XFree86, this header requires @file{X11/Xlib.h}, which is probably
6079 so required that you might not even consider looking for it.
6084 @node Particular Headers
6085 @subsection Particular Header Checks
6087 These macros check for particular system header files---whether they
6088 exist, and in some cases whether they declare certain symbols.
6090 @defmac AC_CHECK_HEADER_STDBOOL
6091 @acindex{CHECK_HEADER_STDBOOL}
6093 @hdrindex{stdbool.h}
6094 @caindex header_stdbool_h
6095 Check whether @file{stdbool.h} exists and conforms to C99 or later,
6096 and cache the result in the @code{ac_cv_header_stdbool_h} variable.
6097 If the type @code{_Bool} is defined, define @code{HAVE__BOOL} to 1.
6099 This macro is obsolescent, as all current C compilers have @file{stdbool.h},
6100 a header that is itself obsolescent as of C23.
6102 This macro is intended for use by Gnulib (@pxref{Gnulib}) and other
6103 packages that supply a substitute @file{stdbool.h} on platforms lacking
6104 a conforming one. The @code{AC_HEADER_STDBOOL} macro is better for code
6105 that explicitly checks for @file{stdbool.h}.
6108 @defmac AC_HEADER_ASSERT
6109 @acindex{HEADER_ASSERT}
6112 Check whether to enable assertions in the style of @file{assert.h}.
6113 Assertions are enabled by default, but the user can override this by
6114 invoking @command{configure} with the @option{--disable-assert} option.
6117 @anchor{AC_HEADER_DIRENT}
6118 @defmac AC_HEADER_DIRENT
6119 @acindex{HEADER_DIRENT}
6120 @cvindex HAVE_DIRENT_H
6121 @cvindex HAVE_NDIR_H
6122 @cvindex HAVE_SYS_DIR_H
6123 @cvindex HAVE_SYS_NDIR_H
6125 @hdrindex{sys/ndir.h}
6126 @hdrindex{sys/dir.h}
6128 Check for the following header files. For the first one that is
6129 found and defines @samp{DIR}, define the listed C preprocessor macro:
6131 @multitable {@file{sys/ndir.h}} {@code{HAVE_SYS_NDIR_H}}
6132 @item @file{dirent.h} @tab @code{HAVE_DIRENT_H}
6133 @item @file{sys/ndir.h} @tab @code{HAVE_SYS_NDIR_H}
6134 @item @file{sys/dir.h} @tab @code{HAVE_SYS_DIR_H}
6135 @item @file{ndir.h} @tab @code{HAVE_NDIR_H}
6138 The directory-library declarations in your source code should look
6139 something like the following:
6143 #include <sys/types.h>
6144 #ifdef HAVE_DIRENT_H
6145 # include <dirent.h>
6146 # define NAMLEN(dirent) strlen ((dirent)->d_name)
6148 # define dirent direct
6149 # define NAMLEN(dirent) ((dirent)->d_namlen)
6150 # ifdef HAVE_SYS_NDIR_H
6151 # include <sys/ndir.h>
6153 # ifdef HAVE_SYS_DIR_H
6154 # include <sys/dir.h>
6163 Using the above declarations, the program would declare variables to be
6164 of type @code{struct dirent}, not @code{struct direct}, and would access
6165 the length of a directory entry name by passing a pointer to a
6166 @code{struct dirent} to the @code{NAMLEN} macro.
6168 This macro also checks for the SCO Xenix @file{dir} and @file{x} libraries.
6170 This macro is obsolescent, as all current systems with directory
6171 libraries have @code{<dirent.h>}. New programs need not use this macro.
6173 Also see @code{AC_STRUCT_DIRENT_D_INO} and
6174 @code{AC_STRUCT_DIRENT_D_TYPE} (@pxref{Particular Structures}).
6177 @anchor{AC_HEADER_MAJOR}
6178 @defmac AC_HEADER_MAJOR
6179 @acindex{HEADER_MAJOR}
6180 @cvindex MAJOR_IN_MKDEV
6181 @cvindex MAJOR_IN_SYSMACROS
6182 @hdrindex{sys/mkdev.h}
6183 @hdrindex{sys/sysmacros.h}
6184 Detect the headers required to use @code{makedev}, @code{major}, and
6185 @code{minor}. These functions may be defined by @file{sys/mkdev.h},
6186 @code{sys/sysmacros.h}, or @file{sys/types.h}.
6188 @code{AC_HEADER_MAJOR} defines @code{MAJOR_IN_MKDEV} if they are in
6189 @file{sys/mkdev.h}, or @code{MAJOR_IN_SYSMACROS} if they are in
6190 @file{sys/sysmacros.h}. If neither macro is defined, they are either in
6191 @file{sys/types.h} or unavailable.
6193 To properly use these functions, your code should contain something
6197 #include <sys/types.h>
6198 #ifdef MAJOR_IN_MKDEV
6199 # include <sys/mkdev.h>
6200 #elif defined MAJOR_IN_SYSMACROS
6201 # include <sys/sysmacros.h>
6205 Note: Configure scripts built with Autoconf 2.69 or earlier will not
6206 detect a problem if @file{sys/types.h} contains definitions of
6207 @code{major}, @code{minor}, and/or @code{makedev} that trigger compiler
6208 warnings upon use. This is known to occur with GNU libc 2.25, where
6209 those definitions are being deprecated to reduce namespace pollution.
6210 If it is not practical to use Autoconf 2.70 to regenerate the configure
6211 script of affected software, you can work around the problem by setting
6212 @samp{ac_cv_header_sys_types_h_makedev=no}, as an argument to
6213 @command{configure} or as part of a @file{config.site} site default file
6214 (@pxref{Site Defaults}).
6217 @defmac AC_HEADER_RESOLV
6218 @acindex{HEADER_RESOLV}
6219 @cvindex HAVE_RESOLV_H
6221 Checks for header @file{resolv.h}, checking for prerequisites first.
6222 To properly use @file{resolv.h}, your code should contain something like
6226 #ifdef HAVE_SYS_TYPES_H
6227 # include <sys/types.h>
6229 #ifdef HAVE_NETINET_IN_H
6230 # include <netinet/in.h> /* inet_ functions / structs */
6232 #ifdef HAVE_ARPA_NAMESER_H
6233 # include <arpa/nameser.h> /* DNS HEADER struct */
6242 @anchor{AC_HEADER_STAT}
6243 @defmac AC_HEADER_STAT
6244 @acindex{HEADER_STAT}
6245 @cvindex STAT_MACROS_BROKEN
6246 @hdrindex{sys/stat.h}
6247 If the macros @code{S_ISDIR}, @code{S_ISREG}, etc.@: defined in
6248 @file{sys/stat.h} do not work properly (returning false positives),
6249 define @code{STAT_MACROS_BROKEN}. This is the case on Tektronix UTekV,
6250 Amdahl UTS and Motorola System V/88.
6252 This macro is obsolescent, as no current systems have the bug.
6253 New programs need not use this macro.
6256 @defmac AC_HEADER_STDBOOL
6257 @acindex{HEADER_STDBOOL}
6258 @cvindex HAVE_STDBOOL_H
6260 @hdrindex{stdbool.h}
6261 @caindex header_stdbool_h
6262 If @file{stdbool.h} exists and conforms to C99 or later, define
6263 @code{HAVE_STDBOOL_H} to 1; if the type @code{_Bool} is defined, define
6264 @code{HAVE__BOOL} to 1.
6266 This macro is obsolescent, as all current C compilers have
6267 @file{stdbool.h}, a header that is itself obsolescent as of C23.
6268 Nowadays programs that need @code{bool}, @code{true} and @code{false}
6269 can include @file{stdbool.h} unconditionally, without using
6270 @code{AC_HEADER_STDBOOL}, and if such a program needs to be portable
6271 only to C23 or later it need not even include @file{stdbool.h}.
6273 This macro caches its result in the @code{ac_cv_header_stdbool_h}
6276 This macro differs from @code{AC_CHECK_HEADER_STDBOOL} only in that it
6277 defines @code{HAVE_STDBOOL_H} whereas @code{AC_CHECK_HEADER_STDBOOL}
6281 @anchor{AC_HEADER_STDC}
6282 @defmac AC_HEADER_STDC
6283 @acindex{HEADER_STDC}
6284 @cvindex STDC_HEADERS
6285 @caindex header_stdc
6287 This macro is obsolescent. Its sole effect is to make sure that all the
6288 headers that are included by @code{AC_INCLUDES_DEFAULT} (@pxref{Default
6289 Includes}), but not part of C89, have been checked for.
6291 All hosted environments that are still of interest for portable code
6292 provide all of the headers specified in C89 (as amended in 1995).
6295 @defmac AC_HEADER_SYS_WAIT
6296 @acindex{HEADER_SYS_WAIT}
6297 @cvindex HAVE_SYS_WAIT_H
6298 @hdrindex{sys/wait.h}
6299 @caindex header_sys_wait_h
6300 If @file{sys/wait.h} exists and is compatible with Posix, define
6301 @code{HAVE_SYS_WAIT_H}. Incompatibility can occur if @file{sys/wait.h}
6302 does not exist, or if it uses the old BSD @code{union wait} instead
6303 of @code{int} to store a status value. If @file{sys/wait.h} is not
6304 Posix compatible, then instead of including it, define the
6305 Posix macros with their usual interpretations. Here is an
6310 #include <sys/types.h>
6311 #ifdef HAVE_SYS_WAIT_H
6312 # include <sys/wait.h>
6315 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
6318 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
6324 This macro caches its result in the @code{ac_cv_header_sys_wait_h}
6327 This macro is obsolescent, as current systems are compatible with Posix.
6328 New programs need not use this macro.
6331 @cvindex _POSIX_VERSION
6333 @code{_POSIX_VERSION} is defined when @file{unistd.h} is included on
6334 Posix systems. If there is no @file{unistd.h}, it is definitely
6335 not a Posix system. However, some non-Posix systems do
6336 have @file{unistd.h}.
6338 The way to check whether the system supports Posix is:
6342 #ifdef HAVE_UNISTD_H
6343 # include <sys/types.h>
6344 # include <unistd.h>
6347 #ifdef _POSIX_VERSION
6348 /* Code for Posix systems. */
6353 @defmac AC_HEADER_TIOCGWINSZ
6354 @acindex{HEADER_TIOCGWINSZ}
6355 @cvindex GWINSZ_IN_SYS_IOCTL
6356 @hdrindex{sys/ioctl.h}
6357 @hdrindex{termios.h}
6358 @c FIXME: I need clarifications from Jim.
6359 If the use of @code{TIOCGWINSZ} requires @file{<sys/ioctl.h>}, then
6360 define @code{GWINSZ_IN_SYS_IOCTL}. Otherwise @code{TIOCGWINSZ} can be
6361 found in @file{<termios.h>}.
6367 #ifdef HAVE_TERMIOS_H
6368 # include <termios.h>
6371 #ifdef GWINSZ_IN_SYS_IOCTL
6372 # include <sys/ioctl.h>
6378 @node Generic Headers
6379 @subsection Generic Header Checks
6381 These macros are used to find system header files not covered by the
6382 ``particular'' test macros. If you need to check the contents of a header
6383 as well as find out whether it is present, you have to write your own
6384 test for it (@pxref{Writing Tests}).
6386 @anchor{AC_CHECK_HEADER}
6387 @defmac AC_CHECK_HEADER (@var{header-file}, @ovar{action-if-found}, @
6388 @ovar{action-if-not-found}, @ovar{includes})
6389 @acindex{CHECK_HEADER}
6390 @caindex header_@var{header-file}
6391 If the system header file @var{header-file} is compilable, execute shell
6392 commands @var{action-if-found}, otherwise execute
6393 @var{action-if-not-found}. If you just want to define a symbol if the
6394 header file is available, consider using @code{AC_CHECK_HEADERS}
6397 @var{includes} should be the appropriate @dfn{prerequisite} code, i.e.@:
6398 whatever might be required to appear above
6399 @samp{#include <@var{header-file}>} for it to compile without error.
6400 This can be anything, but will normally be additional @samp{#include}
6401 directives. If @var{includes} is omitted or empty, @file{configure} will
6402 use the contents of the macro @code{AC_INCLUDES_DEFAULT}.
6403 @xref{Default Includes}.
6405 This macro used to check only for the @emph{presence} of a header, not
6406 whether its contents were acceptable to the compiler. Some older
6407 @command{configure} scripts rely on this behavior, so it is still
6408 available by specifying @samp{-} as @var{includes}. This mechanism is
6409 deprecated as of Autoconf 2.70; situations where a preprocessor-only
6410 check is required should use @code{AC_PREPROC_IFELSE}.
6411 @xref{Running the Preprocessor}.
6413 This macro caches its result in the @code{ac_cv_header_@var{header-file}}
6414 variable, with characters not suitable for a variable name mapped to
6418 @anchor{AC_CHECK_HEADERS}
6419 @defmac AC_CHECK_HEADERS (@var{header-file}@dots{}, @
6420 @ovar{action-if-found}, @ovar{action-if-not-found}, @
6422 @acindex{CHECK_HEADERS}
6423 @cvindex HAVE_@var{header}
6424 @caindex header_@var{header-file}
6425 For each given system header file @var{header-file} in the
6426 blank-separated argument list that exists, define
6427 @code{HAVE_@var{header-file}} (in all capitals). If @var{action-if-found}
6428 is given, it is additional shell code to execute when one of the header
6429 files is found. You can give it a value of @samp{break} to break out of
6430 the loop on the first match. If @var{action-if-not-found} is given, it
6431 is executed when one of the header files is not found.
6433 @var{includes} is interpreted as in @code{AC_CHECK_HEADER}, in order to
6434 choose the set of preprocessor directives supplied before the header
6437 This macro caches its result in the @code{ac_cv_header_@var{header-file}}
6438 variable, with characters not suitable for a variable name mapped to
6442 @defmac AC_CHECK_HEADERS_ONCE (@var{header-file}@dots{})
6443 @acindex{CHECK_HEADERS_ONCE}
6444 @cvindex HAVE_@var{header}
6445 For each given system header file @var{header-file} in the
6446 blank-separated argument list that exists, define
6447 @code{HAVE_@var{header-file}} (in all capitals).
6449 If you do not need the full power of @code{AC_CHECK_HEADERS}, this
6450 variant generates smaller, faster @command{configure} files. All
6451 headers passed to @code{AC_CHECK_HEADERS_ONCE} are checked for in one
6452 pass, early during the @command{configure} run. The checks cannot be
6453 conditionalized, you cannot specify an @var{action-if-found} or
6454 @var{action-if-not-found}, and @code{AC_INCLUDES_DEFAULT} is always used
6455 for the prerequisites.
6458 In previous versions of Autoconf, these macros merely checked whether
6459 the header was accepted by the preprocessor. This was changed because
6460 the old test was inappropriate for typical uses. Headers are typically
6461 used to compile, not merely to preprocess, and the old behavior
6462 sometimes accepted headers that clashed at compile-time
6463 (@pxref{Present But Cannot Be Compiled}). If for some reason it is
6464 inappropriate to check whether a header is compilable, you should use
6465 @code{AC_PREPROC_IFELSE} (@pxref{Running the Preprocessor}) instead of
6468 Requiring each header to compile improves the robustness of the test,
6469 but it also requires you to make sure that the @var{includes} are
6470 correct. Most system headers nowadays make sure to @code{#include}
6471 whatever they require, or else have their dependencies satisfied by
6472 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), but
6473 @pxref{Header Portability}, for known exceptions. In general, if you
6474 are looking for @file{bar.h}, which requires that @file{foo.h} be
6475 included first if it exists, you should do something like this:
6478 AC_CHECK_HEADERS([foo.h])
6479 AC_CHECK_HEADERS([bar.h], [], [],
6487 @section Declarations
6488 @cindex Declaration, checking
6490 The following macros check for the declaration of variables and
6491 functions. If there is no macro specifically defined to check for a
6492 symbol you need, then you can use the general macros (@pxref{Generic
6493 Declarations}) or, for more complex tests, you may use
6494 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
6497 * Particular Declarations:: Macros to check for certain declarations
6498 * Generic Declarations:: How to find other declarations
6501 @node Particular Declarations
6502 @subsection Particular Declaration Checks
6504 There are no specific macros for declarations.
6506 @node Generic Declarations
6507 @subsection Generic Declaration Checks
6509 These macros are used to find declarations not covered by the ``particular''
6512 @defmac AC_CHECK_DECL (@var{symbol}, @ovar{action-if-found}, @
6513 @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
6514 @acindex{CHECK_DECL}
6515 @caindex have_decl_@var{symbol}
6516 If @var{symbol} (a function, variable, or constant) is not declared in
6517 @var{includes} and a declaration is needed, run the shell commands
6518 @var{action-if-not-found}, otherwise @var{action-if-found}.
6519 @var{includes} is a series of include directives, defaulting to
6520 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), which are used
6521 prior to the declaration under test.
6523 This macro actually tests whether @var{symbol} is defined as a macro or
6524 can be used as an r-value, not whether it is really declared, because it
6525 is much safer to avoid introducing extra declarations when they are not
6526 needed. In order to facilitate use of C++ and overloaded function
6527 declarations, it is possible to specify function argument types in
6528 parentheses for types which can be zero-initialized:
6531 AC_CHECK_DECL([basename(char *)])
6534 This macro caches its result in the @code{ac_cv_have_decl_@var{symbol}}
6535 variable, with characters not suitable for a variable name mapped to
6539 @anchor{AC_CHECK_DECLS}
6540 @defmac AC_CHECK_DECLS (@var{symbols}, @ovar{action-if-found}, @
6541 @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
6542 @acindex{CHECK_DECLS}
6543 @cvindex HAVE_DECL_@var{symbol}
6544 @caindex have_decl_@var{symbol}
6545 For each of the @var{symbols} (@emph{comma}-separated list with optional
6546 function argument types for C++ overloads), define
6547 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
6548 @var{symbol} is declared, otherwise to @samp{0}. If
6549 @var{action-if-not-found} is given, it is additional shell code to
6550 execute when one of the function declarations is needed, otherwise
6551 @var{action-if-found} is executed.
6553 @var{includes} is a series of include directives, defaulting to
6554 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), which are used
6555 prior to the declarations under test.
6557 This macro uses an M4 list as first argument:
6559 AC_CHECK_DECLS([strdup])
6560 AC_CHECK_DECLS([strlen])
6561 AC_CHECK_DECLS([malloc, realloc, calloc, free])
6562 AC_CHECK_DECLS([j0], [], [], [[#include <math.h>]])
6563 AC_CHECK_DECLS([[basename(char *)], [dirname(char *)]])
6566 Unlike the other @samp{AC_CHECK_*S} macros, when a @var{symbol} is not
6567 declared, @code{HAVE_DECL_@var{symbol}} is defined to @samp{0} instead
6568 of leaving @code{HAVE_DECL_@var{symbol}} undeclared. When you are
6569 @emph{sure} that the check was performed, use
6570 @code{HAVE_DECL_@var{symbol}} in @code{#if}:
6573 #if !HAVE_DECL_SYMBOL
6574 extern char *symbol;
6579 If the test may have not been performed, however, because it is safer
6580 @emph{not} to declare a symbol than to use a declaration that conflicts
6581 with the system's one, you should use:
6584 #if defined HAVE_DECL_MALLOC && !HAVE_DECL_MALLOC
6585 void *malloc (size_t *s);
6590 You fall into the second category only in extreme situations: either
6591 your files may be used without being configured, or they are used during
6592 the configuration. In most cases the traditional approach is enough.
6594 This macro caches its results in @code{ac_cv_have_decl_@var{symbol}}
6595 variables, with characters not suitable for a variable name mapped to
6599 @defmac AC_CHECK_DECLS_ONCE (@var{symbols})
6600 @acindex{CHECK_DECLS_ONCE}
6601 @cvindex HAVE_DECL_@var{symbol}
6602 For each of the @var{symbols} (@emph{comma}-separated list), define
6603 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
6604 @var{symbol} is declared in the default include files, otherwise to
6605 @samp{0}. This is a once-only variant of @code{AC_CHECK_DECLS}. It
6606 generates the checking code at most once, so that @command{configure} is
6607 smaller and faster; but the checks cannot be conditionalized and are
6608 always done once, early during the @command{configure} run.
6614 @cindex Structure, checking
6616 The following macros check for the presence of certain members in C
6617 structures. If there is no macro specifically defined to check for a
6618 member you need, then you can use the general structure-member macros
6619 (@pxref{Generic Structures}) or, for more complex tests, you may use
6620 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
6623 * Particular Structures:: Macros to check for certain structure members
6624 * Generic Structures:: How to find other structure members
6627 @node Particular Structures
6628 @subsection Particular Structure Checks
6630 The following macros check for certain structures or structure members.
6632 @defmac AC_STRUCT_DIRENT_D_INO
6633 @acindex{STRUCT_DIRENT_D_INO}
6634 @cvindex HAVE_STRUCT_DIRENT_D_INO
6635 @c @caindex header_dirent_dirent_h
6636 @c @caindex member_struct_dirent_d_ino
6637 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
6638 Headers}). Then, if @code{struct dirent} contains a @code{d_ino}
6639 member, define @code{HAVE_STRUCT_DIRENT_D_INO}.
6641 @code{HAVE_STRUCT_DIRENT_D_INO} indicates only the presence of
6642 @code{d_ino}, not whether its contents are always reliable.
6643 Traditionally, a zero @code{d_ino} indicated a deleted directory entry,
6644 though current systems hide this detail from the user and never return
6645 zero @code{d_ino} values.
6646 Many current systems report an incorrect @code{d_ino} for a directory
6647 entry that is a mount point.
6650 @defmac AC_STRUCT_DIRENT_D_TYPE
6651 @acindex{STRUCT_DIRENT_D_TYPE}
6652 @cvindex HAVE_STRUCT_DIRENT_D_TYPE
6653 @c @caindex header_dirent_dirent_h
6654 @c @caindex member_struct_dirent_d_type
6655 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
6656 Headers}). Then, if @code{struct dirent} contains a @code{d_type}
6657 member, define @code{HAVE_STRUCT_DIRENT_D_TYPE}.
6660 @anchor{AC_STRUCT_ST_BLOCKS}
6661 @defmac AC_STRUCT_ST_BLOCKS
6662 @acindex{STRUCT_ST_BLOCKS}
6663 @cvindex HAVE_STRUCT_STAT_ST_BLOCKS
6664 @cvindex HAVE_ST_BLOCKS
6666 @caindex member_struct_stat_st_blocks
6667 If @code{struct stat} contains an @code{st_blocks} member, define
6668 @code{HAVE_STRUCT_STAT_ST_BLOCKS}. Otherwise, require an
6669 @code{AC_LIBOBJ} replacement of @samp{fileblocks}. The former name,
6670 @code{HAVE_ST_BLOCKS} is to be avoided, as its support will cease in the
6673 This macro caches its result in the @code{ac_cv_member_struct_stat_st_blocks}
6677 @defmac AC_STRUCT_TM
6679 @cvindex TM_IN_SYS_TIME
6681 @hdrindex{sys/time.h}
6682 If @file{time.h} does not define @code{struct tm}, define
6683 @code{TM_IN_SYS_TIME}, which means that including @file{sys/time.h}
6684 had better define @code{struct tm}.
6686 This macro is obsolescent, as @file{time.h} defines @code{struct tm} in
6687 current systems. New programs need not use this macro.
6690 @anchor{AC_STRUCT_TIMEZONE}
6691 @defmac AC_STRUCT_TIMEZONE
6692 @acindex{STRUCT_TIMEZONE}
6693 @cvindex HAVE_DECL_TZNAME
6694 @cvindex HAVE_STRUCT_TM_TM_ZONE
6695 @cvindex HAVE_TM_ZONE
6696 @cvindex HAVE_TZNAME
6697 @c @caindex member_struct_tm_tm_zone
6698 @c @caindex struct_tm
6699 Figure out how to get the current timezone. If @code{struct tm} has a
6700 @code{tm_zone} member, define @code{HAVE_STRUCT_TM_TM_ZONE} (and the
6701 obsoleted @code{HAVE_TM_ZONE}). Otherwise, if the external array
6702 @code{tzname} is found, define @code{HAVE_TZNAME}; if it is declared,
6703 define @code{HAVE_DECL_TZNAME}.
6706 @node Generic Structures
6707 @subsection Generic Structure Checks
6709 These macros are used to find structure members not covered by the
6710 ``particular'' test macros.
6712 @defmac AC_CHECK_MEMBER (@var{aggregate}.@var{member}, @
6713 @ovar{action-if-found}, @ovar{action-if-not-found}, @
6714 @dvar{includes, AC_INCLUDES_DEFAULT})
6715 @acindex{CHECK_MEMBER}
6716 @caindex member_@var{aggregate}_@var{member}
6717 Check whether @var{member} is a member of the aggregate @var{aggregate}.
6718 If no @var{includes} are specified, the default includes are used
6719 (@pxref{Default Includes}).
6722 AC_CHECK_MEMBER([struct passwd.pw_gecos], [],
6723 [AC_MSG_ERROR([we need 'passwd.pw_gecos'])],
6724 [[#include <pwd.h>]])
6727 You can use this macro for submembers:
6730 AC_CHECK_MEMBER(struct top.middle.bot)
6733 This macro caches its result in the
6734 @code{ac_cv_member_@var{aggregate}_@var{member}} variable, with
6735 characters not suitable for a variable name mapped to underscores.
6738 @anchor{AC_CHECK_MEMBERS}
6739 @defmac AC_CHECK_MEMBERS (@var{members}, @ovar{action-if-found}, @
6740 @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
6741 @acindex{CHECK_MEMBERS}
6742 @cvindex HAVE_@var{aggregate}_@var{member}
6743 Check for the existence of each @samp{@var{aggregate}.@var{member}} of
6744 @var{members} using the previous macro. When @var{member} belongs to
6745 @var{aggregate}, define @code{HAVE_@var{aggregate}_@var{member}} (in all
6746 capitals, with spaces and dots replaced by underscores). If
6747 @var{action-if-found} is given, it is executed for each of the found
6748 members. If @var{action-if-not-found} is given, it is executed for each
6749 of the members that could not be found.
6751 @var{includes} is a series of include directives, defaulting to
6752 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), which are used
6753 prior to the members under test.
6755 This macro uses M4 lists:
6757 AC_CHECK_MEMBERS([struct stat.st_rdev, struct stat.st_blksize])
6767 The following macros check for C types, either builtin or typedefs. If
6768 there is no macro specifically defined to check for a type you need, and
6769 you don't need to check for any special properties of it, then you can
6770 use a general type-check macro.
6773 * Particular Types:: Special handling to find certain types
6774 * Generic Types:: How to find other types
6777 @node Particular Types
6778 @subsection Particular Type Checks
6780 @hdrindex{sys/types.h}
6783 @hdrindex{inttypes.h}
6784 These macros check for particular C types in @file{sys/types.h},
6785 @file{stdlib.h}, @file{stdint.h}, @file{inttypes.h} and others, if they
6788 The Gnulib @code{stdint} module is an alternate way to define many of
6789 these symbols; it is useful if you prefer your code to assume a
6790 C99-or-better environment. @xref{Gnulib}.
6792 @anchor{AC_TYPE_GETGROUPS}
6793 @defmac AC_TYPE_GETGROUPS
6794 @acindex{TYPE_GETGROUPS}
6795 @cvindex GETGROUPS_T
6796 @caindex type_getgroups
6797 Define @code{GETGROUPS_T} to be whichever of @code{gid_t} or @code{int}
6798 is the base type of the array argument to @code{getgroups}.
6800 This macro caches the base type in the @code{ac_cv_type_getgroups}
6804 @defmac AC_TYPE_INT8_T
6805 @acindex{TYPE_INT8_T}
6806 @cvindex HAVE_INT8_T
6809 If @file{stdint.h} or @file{inttypes.h} does not define the type
6810 @code{int8_t}, define @code{int8_t} to a signed
6811 integer type that is exactly 8 bits wide and that uses two's complement
6812 representation, if such a type exists.
6813 If you are worried about porting to hosts that lack such a type, you can
6814 use the results of this macro as follows:
6818 # include <stdint.h>
6820 #if defined INT8_MAX || defined int8_t
6821 @emph{code using int8_t}
6823 @emph{complicated alternative using >8-bit 'signed char'}
6827 This macro caches the type in the @code{ac_cv_c_int8_t} variable.
6830 @defmac AC_TYPE_INT16_T
6831 @acindex{TYPE_INT16_T}
6832 @cvindex HAVE_INT16_T
6835 This is like @code{AC_TYPE_INT8_T}, except for 16-bit integers.
6838 @defmac AC_TYPE_INT32_T
6839 @acindex{TYPE_INT32_T}
6840 @cvindex HAVE_INT32_T
6843 This is like @code{AC_TYPE_INT8_T}, except for 32-bit integers.
6846 @defmac AC_TYPE_INT64_T
6847 @acindex{TYPE_INT64_T}
6848 @cvindex HAVE_INT64_T
6851 This is like @code{AC_TYPE_INT8_T}, except for 64-bit integers.
6854 @defmac AC_TYPE_INTMAX_T
6855 @acindex{TYPE_INTMAX_T}
6856 @cvindex HAVE_INTMAX_T
6858 @c @caindex type_intmax_t
6859 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intmax_t},
6860 define @code{HAVE_INTMAX_T}. Otherwise, define @code{intmax_t} to the
6861 widest signed integer type.
6864 @defmac AC_TYPE_INTPTR_T
6865 @acindex{TYPE_INTPTR_T}
6866 @cvindex HAVE_INTPTR_T
6868 @c @caindex type_intptr_t
6869 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intptr_t},
6870 define @code{HAVE_INTPTR_T}. Otherwise, define @code{intptr_t} to a
6871 signed integer type wide enough to hold a pointer, if such a type
6875 @defmac AC_TYPE_LONG_DOUBLE
6876 @acindex{TYPE_LONG_DOUBLE}
6877 @cvindex HAVE_LONG_DOUBLE
6878 @caindex type_long_double
6879 If the C compiler supports a working @code{long double} type, define
6880 @code{HAVE_LONG_DOUBLE}. The @code{long double} type might have the
6881 same range and precision as @code{double}.
6883 This macro caches its result in the @code{ac_cv_type_long_double}
6886 This macro is obsolescent, as current C compilers support @code{long
6887 double}. New programs need not use this macro.
6890 @defmac AC_TYPE_LONG_DOUBLE_WIDER
6891 @acindex{TYPE_LONG_DOUBLE_WIDER}
6892 @cvindex HAVE_LONG_DOUBLE_WIDER
6893 @caindex type_long_double_wider
6894 If the C compiler supports a working @code{long double} type with more
6895 range or precision than the @code{double} type, define
6896 @code{HAVE_LONG_DOUBLE_WIDER}.
6898 This macro caches its result in the @code{ac_cv_type_long_double_wider}
6902 @defmac AC_TYPE_LONG_LONG_INT
6903 @acindex{TYPE_LONG_LONG_INT}
6904 @cvindex HAVE_LONG_LONG_INT
6905 @caindex type_long_long_int
6906 If the C compiler supports a working @code{long long int} type, define
6907 @code{HAVE_LONG_LONG_INT}. However, this test does not test
6908 @code{long long int} values in preprocessor @code{#if} expressions,
6909 because too many compilers mishandle such expressions.
6910 @xref{Preprocessor Arithmetic}.
6912 This macro caches its result in the @code{ac_cv_type_long_long_int}
6916 @defmac AC_TYPE_MBSTATE_T
6917 @acindex{TYPE_MBSTATE_T}
6920 @caindex type_mbstate_t
6921 Define @code{HAVE_MBSTATE_T} if @code{<wchar.h>} declares the
6922 @code{mbstate_t} type. Also, define @code{mbstate_t} to be a type if
6923 @code{<wchar.h>} does not declare it.
6925 This macro caches its result in the @code{ac_cv_type_mbstate_t}
6929 @anchor{AC_TYPE_MODE_T}
6930 @defmac AC_TYPE_MODE_T
6931 @acindex{TYPE_MODE_T}
6933 @caindex type_mode_t
6934 Define @code{mode_t} to a suitable type, if standard headers do not
6937 This macro caches its result in the @code{ac_cv_type_mode_t} variable.
6940 @anchor{AC_TYPE_OFF_T}
6941 @defmac AC_TYPE_OFF_T
6942 @acindex{TYPE_OFF_T}
6945 Define @code{off_t} to a suitable type, if standard headers do not
6948 This macro caches its result in the @code{ac_cv_type_off_t} variable.
6951 @anchor{AC_TYPE_PID_T}
6952 @defmac AC_TYPE_PID_T
6953 @acindex{TYPE_PID_T}
6956 Define @code{pid_t} to a suitable type, if standard headers do not
6959 This macro caches its result in the @code{ac_cv_type_pid_t} variable.
6962 @anchor{AC_TYPE_SIZE_T}
6963 @defmac AC_TYPE_SIZE_T
6964 @acindex{TYPE_SIZE_T}
6966 @caindex type_size_t
6967 Define @code{size_t} to a suitable type, if standard headers do not
6970 This macro caches its result in the @code{ac_cv_type_size_t} variable.
6973 @defmac AC_TYPE_SSIZE_T
6974 @acindex{TYPE_SSIZE_T}
6976 @caindex type_ssize_t
6977 Define @code{ssize_t} to a suitable type, if standard headers do not
6980 This macro caches its result in the @code{ac_cv_type_ssize_t} variable.
6983 @anchor{AC_TYPE_UID_T}
6984 @defmac AC_TYPE_UID_T
6985 @acindex{TYPE_UID_T}
6989 Define @code{uid_t} and @code{gid_t} to suitable types, if standard
6990 headers do not define them.
6992 This macro caches its result in the @code{ac_cv_type_uid_t} variable.
6995 @defmac AC_TYPE_UINT8_T
6996 @acindex{TYPE_UINT8_T}
6997 @cvindex HAVE_UINT8_T
7000 If @file{stdint.h} or @file{inttypes.h} does not define the type
7001 @code{uint8_t}, define @code{uint8_t} to an
7002 unsigned integer type that is exactly 8 bits wide, if such a type
7004 This is like @code{AC_TYPE_INT8_T}, except for unsigned integers.
7007 @defmac AC_TYPE_UINT16_T
7008 @acindex{TYPE_UINT16_T}
7009 @cvindex HAVE_UINT16_T
7012 This is like @code{AC_TYPE_UINT8_T}, except for 16-bit integers.
7015 @defmac AC_TYPE_UINT32_T
7016 @acindex{TYPE_UINT32_T}
7017 @cvindex HAVE_UINT32_T
7020 This is like @code{AC_TYPE_UINT8_T}, except for 32-bit integers.
7023 @defmac AC_TYPE_UINT64_T
7024 @acindex{TYPE_UINT64_T}
7025 @cvindex HAVE_UINT64_T
7028 This is like @code{AC_TYPE_UINT8_T}, except for 64-bit integers.
7031 @defmac AC_TYPE_UINTMAX_T
7032 @acindex{TYPE_UINTMAX_T}
7033 @cvindex HAVE_UINTMAX_T
7035 @c @caindex type_uintmax_t
7036 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintmax_t},
7037 define @code{HAVE_UINTMAX_T}. Otherwise, define @code{uintmax_t} to the
7038 widest unsigned integer type.
7041 @defmac AC_TYPE_UINTPTR_T
7042 @acindex{TYPE_UINTPTR_T}
7043 @cvindex HAVE_UINTPTR_T
7045 @c @caindex type_uintptr_t
7046 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintptr_t},
7047 define @code{HAVE_UINTPTR_T}. Otherwise, define @code{uintptr_t} to an
7048 unsigned integer type wide enough to hold a pointer, if such a type
7052 @defmac AC_TYPE_UNSIGNED_LONG_LONG_INT
7053 @acindex{TYPE_UNSIGNED_LONG_LONG_INT}
7054 @cvindex HAVE_UNSIGNED_LONG_LONG_INT
7055 @caindex type_unsigned_long_long_int
7056 If the C compiler supports a working @code{unsigned long long int} type,
7057 define @code{HAVE_UNSIGNED_LONG_LONG_INT}. However, this test does not test
7058 @code{unsigned long long int} values in preprocessor @code{#if} expressions,
7059 because too many compilers mishandle such expressions.
7060 @xref{Preprocessor Arithmetic}.
7062 This macro caches its result in the @code{ac_cv_type_unsigned_long_long_int}
7067 @subsection Generic Type Checks
7069 These macros are used to check for types not covered by the ``particular''
7072 @defmac AC_CHECK_TYPE (@var{type}, @ovar{action-if-found}, @
7073 @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
7074 @acindex{CHECK_TYPE}
7075 @caindex type_@var{type}
7076 Check whether @var{type} is defined. It may be a compiler builtin type
7077 or defined by the @var{includes}. @var{includes} is a series of include
7078 directives, defaulting to @code{AC_INCLUDES_DEFAULT} (@pxref{Default
7079 Includes}), which are used prior to the type under test.
7081 In C, @var{type} must be a type-name, so that the expression @samp{sizeof
7082 (@var{type})} is valid (but @samp{sizeof ((@var{type}))} is not). The
7083 same test is applied when compiling for C++, which means that in C++
7084 @var{type} should be a type-id and should not be an anonymous
7085 @samp{struct} or @samp{union}.
7087 This macro caches its result in the @code{ac_cv_type_@var{type}}
7088 variable, with @samp{*} mapped to @samp{p} and other characters not
7089 suitable for a variable name mapped to underscores.
7093 @defmac AC_CHECK_TYPES (@var{types}, @ovar{action-if-found}, @
7094 @ovar{action-if-not-found}, @dvar{includes, AC_INCLUDES_DEFAULT})
7095 @acindex{CHECK_TYPES}
7096 @cvindex HAVE_@var{type}
7097 For each @var{type} of the @var{types} that is defined, define
7098 @code{HAVE_@var{type}} (in all capitals). Each @var{type} must follow
7099 the rules of @code{AC_CHECK_TYPE}. If no @var{includes} are
7100 specified, the default includes are used (@pxref{Default Includes}). If
7101 @var{action-if-found} is given, it is additional shell code to execute
7102 when one of the types is found. If @var{action-if-not-found} is given,
7103 it is executed when one of the types is not found.
7105 This macro uses M4 lists:
7107 AC_CHECK_TYPES([ptrdiff_t])
7108 AC_CHECK_TYPES([unsigned long long int, uintmax_t])
7109 AC_CHECK_TYPES([float_t], [], [], [[#include <math.h>]])
7114 Autoconf, up to 2.13, used to provide to another version of
7115 @code{AC_CHECK_TYPE}, broken by design. In order to keep backward
7116 compatibility, a simple heuristic, quite safe but not totally, is
7117 implemented. In case of doubt, read the documentation of the former
7118 @code{AC_CHECK_TYPE}, see @ref{Obsolete Macros}.
7121 @node Compilers and Preprocessors
7122 @section Compilers and Preprocessors
7124 @cindex Preprocessors
7127 All the tests for compilers (@code{AC_PROG_CC}, @code{AC_PROG_CXX},
7128 @code{AC_PROG_F77}) define the output variable @code{EXEEXT} based on
7129 the output of the compiler, typically to the empty string if
7130 Posix and @samp{.exe} if a DOS variant.
7133 They also define the output variable @code{OBJEXT} based on the
7134 output of the compiler, after @file{.c} files have been excluded, typically
7135 to @samp{o} if Posix, @samp{obj} if a DOS variant.
7137 If the compiler being used does not produce executables, the tests fail. If
7138 the executables can't be run, and cross-compilation is not enabled, they
7139 fail too. @xref{Manual Configuration}, for more on support for cross
7143 * Specific Compiler Characteristics:: Some portability issues
7144 * Generic Compiler Characteristics:: Language independent tests and features
7145 * C Compiler:: Checking its characteristics
7146 * C++ Compiler:: Likewise
7147 * Objective C Compiler:: Likewise
7148 * Objective C++ Compiler:: Likewise
7149 * Erlang Compiler and Interpreter:: Likewise
7150 * Fortran Compiler:: Likewise
7151 * Go Compiler:: Likewise
7154 @node Specific Compiler Characteristics
7155 @subsection Specific Compiler Characteristics
7157 Some compilers exhibit different behaviors.
7160 @item Static/Dynamic Expressions
7161 Autoconf relies on a trick to extract one bit of information from the C
7162 compiler: using negative array sizes. For instance the following
7163 excerpt of a C source demonstrates how to test whether @samp{int} objects are 4
7167 static int test_array[sizeof (int) == 4 ? 1 : -1];
7171 To our knowledge, there is a single compiler that does not support this
7172 trick: the HP C compilers (the real ones, not only the
7173 ``bundled'') on HP-UX 11.00.
7174 They incorrectly reject the above program with the diagnostic
7175 ``Variable-length arrays cannot have static storage.''
7176 This bug comes from HP compilers' mishandling of @code{sizeof (int)},
7177 not from the @code{? 1 : -1}, and
7178 Autoconf works around this problem by casting @code{sizeof (int)} to
7179 @code{long int} before comparing it.
7182 @node Generic Compiler Characteristics
7183 @subsection Generic Compiler Characteristics
7185 @anchor{AC_CHECK_SIZEOF}
7186 @defmac AC_CHECK_SIZEOF (@var{type-or-expr}, @ovar{unused}, @
7187 @dvar{includes, AC_INCLUDES_DEFAULT})
7188 @acindex{CHECK_SIZEOF}
7189 @cvindex SIZEOF_@var{type-or-expr}
7190 @caindex sizeof_@var{type-or-expr}
7191 Define @code{SIZEOF_@var{type-or-expr}} (@pxref{Standard Symbols}) to be
7192 the size in bytes of @var{type-or-expr}, which may be either a type or
7193 an expression returning a value that has a size. If the expression
7194 @samp{sizeof (@var{type-or-expr})} is invalid, the result is 0.
7195 @var{includes} is a series of include directives, defaulting to
7196 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), which are used
7197 prior to the expression under test.
7199 This macro now works even when cross-compiling. The @var{unused}
7200 argument was used when cross-compiling.
7202 For example, the call
7205 @c If you change this example, adjust tests/semantics.at:AC_CHECK_SIZEOF struct.
7206 AC_CHECK_SIZEOF([int *])
7210 defines @code{SIZEOF_INT_P} to be 8 on DEC Alpha AXP systems.
7212 This macro caches its result in the @code{ac_cv_sizeof_@var{type-or-expr}}
7213 variable, with @samp{*} mapped to @samp{p} and other characters not
7214 suitable for a variable name mapped to underscores.
7217 @defmac AC_CHECK_ALIGNOF (@var{type}, @dvar{includes, AC_INCLUDES_DEFAULT})
7218 @acindex{CHECK_ALIGNOF}
7219 @cvindex ALIGNOF_@var{type}
7220 @caindex alignof_@var{type-or-expr}
7221 Define @code{ALIGNOF_@var{type}} (@pxref{Standard Symbols}) to be the
7222 alignment in bytes of @var{type}. @samp{@var{type} y;} must be valid as
7223 a structure member declaration. If @samp{type} is unknown, the result
7224 is 0. If no @var{includes} are specified, the default includes are used
7225 (@pxref{Default Includes}).
7227 This macro caches its result in the @code{ac_cv_alignof_@var{type-or-expr}}
7228 variable, with @samp{*} mapped to @samp{p} and other characters not
7229 suitable for a variable name mapped to underscores.
7232 @defmac AC_COMPUTE_INT (@var{var}, @var{expression}, @
7233 @dvar{includes, AC_INCLUDES_DEFAULT}, @ovar{action-if-fails})
7234 @acindex{COMPUTE_INT}
7235 Store into the shell variable @var{var} the value of the integer
7236 @var{expression}. The
7237 value should fit in an initializer in a C variable of type @code{signed
7238 long}. To support cross compilation, it should be possible to evaluate
7239 the expression at compile-time. If no @var{includes} are specified, the
7240 default includes are used (@pxref{Default Includes}).
7242 Execute @var{action-if-fails} if the value cannot be determined correctly.
7245 @defmac AC_LANG_WERROR
7246 @acindex{LANG_WERROR}
7247 Normally Autoconf ignores warnings generated by the compiler, linker, and
7248 preprocessor. If this macro is used, warnings count as fatal
7249 errors for the current language. This macro is useful when the
7250 results of configuration are used where warnings are unacceptable; for
7251 instance, if parts of a program are built with the GCC
7253 option. If the whole program is built using @option{-Werror} it is
7254 often simpler to put @option{-Werror} in the compiler flags (@code{CFLAGS},
7261 @ovindex OPENMP_CFLAGS
7262 @ovindex OPENMP_CXXFLAGS
7263 @ovindex OPENMP_FFLAGS
7264 @ovindex OPENMP_FCFLAGS
7265 @caindex prog_c_openmp
7266 @caindex prog_cxx_openmp
7267 @caindex prog_f77_openmp
7268 @caindex prog_fc_openmp
7269 @uref{http://@/www.openmp.org/, OpenMP} specifies extensions of C, C++,
7270 and Fortran that simplify optimization of shared memory parallelism,
7271 which is a common problem on multi-core CPUs.
7273 If the current language is C, the macro @code{AC_OPENMP} sets the
7274 variable @code{OPENMP_CFLAGS} to the C compiler flags needed for
7275 supporting OpenMP@. @code{OPENMP_CFLAGS} is set to empty if the
7276 compiler already supports OpenMP, if it has no way to activate OpenMP
7277 support, or if the user rejects OpenMP support by invoking
7278 @samp{configure} with the @samp{--disable-openmp} option.
7280 @code{OPENMP_CFLAGS} needs to be used when compiling programs, when
7281 preprocessing program source, and when linking programs. Therefore you
7282 need to add @code{$(OPENMP_CFLAGS)} to the @code{CFLAGS} of C programs
7283 that use OpenMP@. If you preprocess OpenMP-specific C code, you also
7284 need to add @code{$(OPENMP_CFLAGS)} to @code{CPPFLAGS}. The presence of
7285 OpenMP support is revealed at compile time by the preprocessor macro
7288 Linking a program with @code{OPENMP_CFLAGS} typically adds one more
7289 shared library to the program's dependencies, so its use is recommended
7290 only on programs that actually require OpenMP.
7292 If the current language is C++, @code{AC_OPENMP} sets the variable
7293 @code{OPENMP_CXXFLAGS}, suitably for the C++ compiler. The same remarks
7296 If the current language is Fortran 77 or Fortran, @code{AC_OPENMP} sets
7297 the variable @code{OPENMP_FFLAGS} or @code{OPENMP_FCFLAGS},
7298 respectively. Similar remarks as for C hold, except that
7299 @code{CPPFLAGS} is not used for Fortran, and no preprocessor macro
7300 signals OpenMP support.
7302 For portability, it is best to avoid spaces between @samp{#} and
7303 @samp{pragma omp}. That is, write @samp{#pragma omp}, not
7304 @samp{# pragma omp}. The Sun WorkShop 6.2 C compiler chokes on the
7307 This macro caches its result in the @code{ac_cv_prog_c_openmp},
7308 @code{ac_cv_prog_cxx_openmp}, @code{ac_cv_prog_f77_openmp}, or
7309 @code{ac_cv_prog_fc_openmp} variable, depending on the current language.
7311 @strong{Caution:} Some of the compiler options that @code{AC_OPENMP}
7312 tests, mean ``enable OpenMP'' to one compiler, but ``write output to a
7313 file named @file{mp} or @file{penmp}'' to other compilers. We cannot
7314 guarantee that the implementation of @code{AC_OPENMP} will not overwrite
7315 an existing file with either of these names.
7317 Therefore, as a defensive measure, a @command{configure} script that
7318 uses @code{AC_OPENMP} will issue an error and stop (before doing any of
7319 the operations that might overwrite these files) upon encountering
7320 either of these files in its working directory.
7321 @command{autoconf} will also issue an error if it finds either of
7322 these files in the same directory as a @file{configure.ac} that
7323 uses @code{AC_OPENMP}.
7325 If you have files with either of these names at the top level of your
7326 source tree, and you need to use @code{AC_OPENMP}, we recommend you
7327 either change their names or move them into a subdirectory.
7331 @subsection C Compiler Characteristics
7333 The following macros provide ways to find and exercise a C Compiler.
7334 There are a few constructs that ought to be avoided, but do not deserve
7335 being checked for, since they can easily be worked around.
7338 @item Don't use lines containing solitary backslashes
7339 They tickle a bug in the HP-UX C compiler (checked on
7341 11.00, and 11i). When given the following source:
7346 * A comment with backslash-newlines in it. %@{ %@} *\
7350 " A string with backslash-newlines in it %@{ %@} \\
7352 char apostrophe = '\\
7360 the compiler incorrectly fails with the diagnostics ``Non-terminating
7361 comment at end of file'' and ``Missing @samp{#endif} at end of file.''
7362 Removing the lines with solitary backslashes solves the problem.
7364 @item Don't compile several files at once if output matters to you
7365 Some compilers, such as HP's, report names of files being
7366 compiled when given more than one file operand. For instance:
7375 This can cause problems if you observe the output of the compiler to
7376 detect failures. Invoking @samp{cc -c a.c && cc -c b.c && cc -o c a.o
7377 b.o} solves the issue.
7379 @item Don't rely on @code{#error} failing
7380 The IRIX C compiler does not fail when #error is preprocessed; it
7381 simply emits a diagnostic and continues, exiting successfully. So,
7382 instead of an error directive like @code{#error "Unsupported word size"}
7383 it is more portable to use an invalid directive like @code{#Unsupported
7384 word size} in Autoconf tests. In ordinary source code, @code{#error} is
7385 OK, since installers with inadequate compilers like IRIX can simply
7386 examine these compilers' diagnostic output.
7388 @item Don't rely on correct @code{#line} support
7389 On Solaris, @command{c89} (at least through Oracle Developer Studio 12.6)
7390 diagnoses @code{#line} directives whose line
7391 numbers are greater than 32767. Nothing in Posix
7392 makes this invalid. That is why Autoconf stopped issuing
7393 @code{#line} directives.
7397 @defmac AC_PROG_CC (@ovar{compiler-search-list})
7403 Determine a C compiler to use.
7405 If the environment variable @code{CC} is set, its value will be taken as
7406 the name of the C compiler to use. Otherwise, search for a C compiler
7407 under a series of likely names, trying @code{gcc} and @code{cc} first.
7408 Regardless, the output variable @code{CC} is set to the chosen compiler.
7410 If the optional first argument to the macro is used, it must be a
7411 whitespace-separated list of potential names for a C compiler,
7412 which overrides the built-in list.
7414 If no C compiler can be found, @command{configure} will error out.
7416 If the selected C compiler is found to be GNU C (regardless of
7417 its name), the shell variable @code{GCC} will be set to @samp{yes}.
7418 If the shell variable @code{CFLAGS} was not already set, it is set
7419 to @option{-g -O2} for the GNU C compiler (@option{-O2} on systems
7420 where GCC does not accept @option{-g}), or @option{-g} for other
7421 compilers. @code{CFLAGS} is then made an output variable.
7422 You can override the default for @code{CFLAGS} by inserting a shell
7423 default assignment between @code{AC_INIT} and @code{AC_PROG_CC}:
7426 : $@{CFLAGS="@var{options}"@}
7429 where @var{options} are the appropriate set of options to use by
7430 default. (It is important to use this construct rather than a normal
7431 assignment, so that @code{CFLAGS} can still be overridden by the
7432 person building the package. @xref{Preset Output Variables}.)
7434 If necessary, options are added to @code{CC} to enable support for
7435 ISO Standard C features with extensions, preferring the newest edition
7436 of the C standard for which detection is supported. Currently the
7437 newest edition Autoconf knows how to detect support for is C11, as there is
7438 little reason to prefer C17 to C11, and C23 is still too new. After calling
7439 this macro you can check whether the C compiler has been set to accept
7440 standard C by inspecting the shell variable @code{ac_prog_cc_stdc}.
7441 Its value will be @samp{c11}, @samp{c99}, or @samp{c89}, respectively,
7442 if the C compiler has been set to use the 2011, 1999, or 1990 edition of
7443 the C standard, and @samp{no} if the compiler does not support compiling
7446 The tests for standard conformance are not comprehensive. They test the
7447 values of @code{__STDC__} and @code{__STDC_VERSION__}, and a
7448 representative sample of the language features added in each version of
7449 the C standard. They do not test the C standard library, because the C
7450 compiler might be generating code for a ``freestanding environment''
7451 (in which most of the standard library is optional). If you need to know
7452 whether a particular C standard header exists, use @code{AC_CHECK_HEADER}.
7454 None of the options that may be added to @code{CC} by this macro
7455 enable @emph{strict} conformance to the C standard. In particular,
7456 system-specific extensions are not disabled. (For example, for GNU C,
7457 the @option{-std=gnu@var{nn}} options may be used, but not the
7458 @option{-std=c@var{nn}} options.)
7460 Many Autoconf macros use a compiler, and thus call
7461 @samp{AC_REQUIRE([AC_PROG_CC])} to ensure that the compiler has been
7462 determined before the body of the outermost @code{AC_DEFUN} macro.
7463 Although @code{AC_PROG_CC} is safe to directly expand multiple times, it
7464 performs certain checks (such as the proper value of @env{EXEEXT}) only
7465 on the first invocation. Therefore, care must be used when invoking
7466 this macro from within another macro rather than at the top level
7467 (@pxref{Expanded Before Required}).
7470 @anchor{AC_PROG_CC_C_O}
7471 @defmac AC_PROG_CC_C_O
7472 @acindex{PROG_CC_C_O}
7473 @cvindex NO_MINUS_C_MINUS_O
7474 @caindex prog_cc_@var{compiler}_c_o
7475 If the C compiler does not accept the @option{-c} and @option{-o} options
7476 simultaneously, define @code{NO_MINUS_C_MINUS_O}. This macro actually
7477 tests both the compiler found by @code{AC_PROG_CC}, and, if different,
7478 the first @code{cc} in the path. The test fails if one fails. This
7479 macro was created for GNU Make to choose the default C compilation
7482 For the compiler @var{compiler}, this macro caches its result in the
7483 @code{ac_cv_prog_cc_@var{compiler}_c_o} variable.
7491 Set output variable @code{CPP} to a command that runs the
7492 C preprocessor. If @samp{$CC -E} doesn't work, tries @code{cpp} and
7493 @file{/lib/cpp}, in that order.
7495 It is only portable to run @code{CPP} on files with a @file{.c}
7498 Some preprocessors don't indicate missing include files by the error
7499 status. For such preprocessors an internal variable is set that causes
7500 other macros to check the standard error from the preprocessor and
7501 consider the test failed if any warnings have been reported.
7502 For most preprocessors, though, warnings do not cause include-file
7503 tests to fail unless @code{AC_PROG_CPP_WERROR} is also specified.
7506 @defmac AC_PROG_CPP_WERROR
7507 @acindex{PROG_CPP_WERROR}
7509 This acts like @code{AC_PROG_CPP}, except it treats warnings from the
7510 preprocessor as errors even if the preprocessor exit status indicates
7511 success. This is useful for avoiding headers that generate mandatory
7512 warnings, such as deprecation notices.
7516 The following macros check for C compiler or machine architecture
7517 features. To check for characteristics not listed here, use
7518 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
7519 @code{AC_RUN_IFELSE} (@pxref{Runtime}).
7521 @defmac AC_C_BACKSLASH_A
7522 @acindex{C_BACKSLASH_A}
7523 @cvindex HAVE_C_BACKSLASH_A
7524 Define @samp{HAVE_C_BACKSLASH_A} to 1 if the C compiler understands
7527 This macro is obsolescent, as current C compilers understand @samp{\a}.
7528 New programs need not use this macro.
7531 @anchor{AC_C_BIGENDIAN}
7532 @defmac AC_C_BIGENDIAN (@ovar{action-if-true}, @ovar{action-if-false}, @
7533 @ovar{action-if-unknown}, @ovar{action-if-universal})
7534 @acindex{C_BIGENDIAN}
7535 @cvindex WORDS_BIGENDIAN
7537 If words are stored with the most significant byte first (like Motorola
7538 and SPARC CPUs), execute @var{action-if-true}. If words are stored with
7539 the least significant byte first (like Intel and VAX CPUs), execute
7540 @var{action-if-false}.
7542 This macro runs a test-case if endianness cannot be determined from the
7543 system header files. When cross-compiling, the test-case is not run but
7544 grep'ed for some magic values. @var{action-if-unknown} is executed if
7545 the latter case fails to determine the byte sex of the host system.
7547 In some cases a single run of a compiler can generate code for multiple
7548 architectures. This can happen, for example, when generating Mac OS X
7549 universal binary files, which work on both PowerPC and Intel
7550 architectures. In this case, the different variants might be for
7551 architectures with differing endianness. If
7552 @command{configure} detects this, it executes @var{action-if-universal}
7553 instead of @var{action-if-unknown}.
7555 The default for @var{action-if-true} is to define
7556 @samp{WORDS_BIGENDIAN}. The default for @var{action-if-false} is to do
7557 nothing. The default for @var{action-if-unknown} is to
7558 abort configure and tell the installer how to bypass this test.
7559 And finally, the default for @var{action-if-universal} is to ensure that
7560 @samp{WORDS_BIGENDIAN} is defined if and only if a universal build is
7561 detected and the current code is big-endian; this default works only if
7562 @command{autoheader} is used (@pxref{autoheader Invocation}).
7564 If you use this macro without specifying @var{action-if-universal}, you
7565 should also use @code{AC_CONFIG_HEADERS}; otherwise
7566 @samp{WORDS_BIGENDIAN} may be set incorrectly for Mac OS X universal
7575 If the C compiler does not fully support the @code{const} keyword,
7576 define @code{const} to be empty. Some C compilers that do
7577 not define @code{__STDC__} do support @code{const}; some compilers that
7578 define @code{__STDC__} do not completely support @code{const}. Programs
7579 can simply use @code{const} as if every C compiler supported it; for
7580 those that don't, the makefile or configuration header file
7581 defines it as empty.
7583 Occasionally installers use a C++ compiler to compile C code, typically
7584 because they lack a C compiler. This causes problems with @code{const},
7585 because C and C++ treat @code{const} differently. For example:
7592 is valid in C but not in C++. These differences unfortunately cannot be
7593 papered over by defining @code{const} to be empty.
7595 If @command{autoconf} detects this situation, it leaves @code{const} alone,
7596 as this generally yields better results in practice. However, using a
7597 C++ compiler to compile C code is not recommended or supported, and
7598 installers who run into trouble in this area should get a C compiler
7599 like GCC to compile their C code.
7601 This macro caches its result in the @code{ac_cv_c_const} variable.
7603 This macro is obsolescent, as current C compilers support @code{const}.
7604 New programs need not use this macro.
7607 @defmac AC_C__GENERIC
7608 @acindex{C__GENERIC}
7610 If the C compiler supports C11-style generic selection using the
7611 @code{_Generic} keyword, define @code{HAVE_C__GENERIC}.
7614 @defmac AC_C_RESTRICT
7615 @acindex{C_RESTRICT}
7618 If the C compiler recognizes a variant spelling for the @code{restrict}
7619 keyword (@code{__restrict}, @code{__restrict__}, or @code{_Restrict}),
7620 then define @code{restrict} to that; this is more likely to do the right
7621 thing with compilers that support language variants where plain
7622 @code{restrict} is not a keyword. Otherwise, if the C compiler
7623 recognizes the @code{restrict} keyword, don't do anything.
7624 Otherwise, define @code{restrict} to be empty.
7625 Thus, programs may simply use @code{restrict} as if every C compiler
7626 supported it; for those that do not, the makefile
7627 or configuration header defines it away.
7629 Although support in C++ for the @code{restrict} keyword is not
7630 required, several C++ compilers do accept the keyword.
7631 This macro works for them, too.
7633 This macro caches @samp{no} in the @code{ac_cv_c_restrict} variable
7634 if @code{restrict} is not supported, and a supported spelling otherwise.
7637 @defmac AC_C_VOLATILE
7638 @acindex{C_VOLATILE}
7640 If the C compiler does not understand the keyword @code{volatile},
7641 define @code{volatile} to be empty. Programs can simply use
7642 @code{volatile} as if every C compiler supported it; for those that do
7643 not, the makefile or configuration header defines it as
7646 If the correctness of your program depends on the semantics of
7647 @code{volatile}, simply defining it to be empty does, in a sense, break
7648 your code. However, given that the compiler does not support
7649 @code{volatile}, you are at its mercy anyway. At least your
7650 program compiles, when it wouldn't before.
7651 @xref{Volatile Objects}, for more about @code{volatile}.
7653 In general, the @code{volatile} keyword is a standard C feature, so
7654 you might expect that @code{volatile} is available only when
7655 @code{__STDC__} is defined. However, Ultrix 4.3's native compiler does
7656 support volatile, but does not define @code{__STDC__}.
7658 This macro is obsolescent, as current C compilers support @code{volatile}.
7659 New programs need not use this macro.
7662 @anchor{AC_C_INLINE}
7666 If the C compiler supports the keyword @code{inline}, do nothing.
7667 Otherwise define @code{inline} to @code{__inline__} or @code{__inline}
7668 if it accepts one of those, otherwise define @code{inline} to be empty.
7671 @anchor{AC_C_CHAR_UNSIGNED}
7672 @defmac AC_C_CHAR_UNSIGNED
7673 @acindex{C_CHAR_UNSIGNED}
7674 @cvindex __CHAR_UNSIGNED__
7675 If the C type @code{char} is unsigned, define @code{__CHAR_UNSIGNED__},
7676 unless the C compiler predefines it.
7678 These days, using this macro is not necessary. The same information can
7679 be determined by this portable alternative, thus avoiding the use of
7680 preprocessor macros in the namespace reserved for the implementation.
7685 # define CHAR_UNSIGNED 1
7690 @defmac AC_C_STRINGIZE
7691 @acindex{C_STRINGIZE}
7692 @cvindex HAVE_STRINGIZE
7693 If the C preprocessor supports the stringizing operator, define
7694 @code{HAVE_STRINGIZE}. The stringizing operator is @samp{#} and is
7695 found in macros such as this:
7701 This macro is obsolescent, as current C compilers support the
7702 stringizing operator. New programs need not use this macro.
7705 @defmac AC_C_FLEXIBLE_ARRAY_MEMBER
7706 @acindex{C_FLEXIBLE_ARRAY_MEMBER}
7707 @cvindex FLEXIBLE_ARRAY_MEMBER
7708 If the C compiler supports flexible array members, define
7709 @code{FLEXIBLE_ARRAY_MEMBER} to nothing; otherwise define it to 1.
7710 That way, a declaration like this:
7716 double val[FLEXIBLE_ARRAY_MEMBER];
7721 will let applications use the ``struct hack'' even with compilers that
7722 do not support flexible array members. To allocate and use such an
7723 object, you can use code like this:
7727 size_t n = compute_value_count ();
7729 malloc (offsetof (struct s, val)
7730 + n * sizeof (double));
7732 for (i = 0; i < n; i++)
7733 p->val[i] = compute_value (i);
7737 @defmac AC_C_VARARRAYS
7738 @acindex{C_VARARRAYS}
7739 @cvindex __STDC_NO_VLA__
7740 @cvindex HAVE_C_VARARRAYS
7741 If the C compiler does not support variable-length arrays, define the
7742 macro @code{__STDC_NO_VLA__} to be 1 if it is not already defined. A
7743 variable-length array is an array of automatic storage duration whose
7744 length is determined at run time, when the array is declared. For
7745 backward compatibility this macro also defines @code{HAVE_C_VARARRAYS}
7746 if the C compiler supports variable-length arrays, but this usage is
7747 obsolescent and new programs should use @code{__STDC_NO_VLA__}.
7752 @cvindex HAVE_TYPEOF
7754 If the C compiler supports GNU C's @code{typeof} syntax either
7756 through a different spelling of the keyword (e.g., @code{__typeof__}),
7757 define @code{HAVE_TYPEOF}. If the support is available only through a
7758 different spelling, define @code{typeof} to that spelling.
7761 @defmac AC_C_PROTOTYPES
7762 @acindex{C_PROTOTYPES}
7764 @cvindex __PROTOTYPES
7766 If function prototypes are understood by the compiler (as determined by
7767 @code{AC_PROG_CC}), define @code{PROTOTYPES} and @code{__PROTOTYPES}.
7768 Defining @code{__PROTOTYPES} is for the benefit of
7769 header files that cannot use macros that infringe on user name space.
7771 This macro is obsolescent, as current C compilers support prototypes.
7772 New programs need not use this macro.
7776 @subsection C++ Compiler Characteristics
7779 @defmac AC_PROG_CXX (@ovar{compiler-search-list})
7785 Determine a C++ compiler to use.
7787 If either the environment variable @code{CXX} or the environment
7788 variable @code{CCC} is set, its value will be taken as the name of a
7789 C++ compiler. If both are set, @code{CXX} is preferred. If neither
7790 are set, search for a C++ compiler under a series of likely names,
7791 trying @code{g++} and @code{c++} first. Regardless, the output
7792 variable @code{CXX} is set to the chosen compiler.
7794 If the optional first argument to the macro is used, it must be a
7795 whitespace-separated list of potential names for a C++ compiler,
7796 which overrides the built-in list.
7798 If no C++ compiler can be found, as a last resort @code{CXX} is set to
7799 @code{g++} (and subsequent tests will probably fail).
7801 If the selected C++ compiler is found to be GNU C++ (regardless of
7802 its name), the shell variable @code{GXX} will be set to @samp{yes}.
7803 If the shell variable @code{CXXFLAGS} was not already set, it is set
7804 to @option{-g -O2} for the GNU C++ compiler (@option{-O2} on systems
7805 where G++ does not accept @option{-g}), or @option{-g} for other
7806 compilers. @code{CXXFLAGS} is then made an output variable.
7807 You can override the default for @code{CXXFLAGS} by inserting a shell
7808 default assignment between @code{AC_INIT} and @code{AC_PROG_CXX}:
7811 : $@{CXXFLAGS="@var{options}"@}
7814 where @var{options} are the appropriate set of options to use by
7815 default. (It is important to use this construct rather than a normal
7816 assignment, so that @code{CXXFLAGS} can still be overridden by the
7817 person building the package. @xref{Preset Output Variables}.)
7819 If necessary, options are added to @code{CXX} to enable support for
7820 ISO Standard C++ features with extensions, preferring the newest edition
7821 of the C++ standard that is supported. Currently the newest edition
7822 Autoconf knows how to detect support for is C++11. After calling
7823 this macro, you can check whether the C++ compiler has been set to
7824 accept standard C++ by inspecting the shell variable @code{ac_prog_cxx_stdcxx}.
7825 Its value will be @samp{cxx11} or @samp{cxx98}, respectively,
7826 if the C++ compiler has been set to use the 2011 or 1990 edition of the
7827 C++ standard, and @samp{no} if the compiler does not support compiling
7828 standard C++ at all.
7830 The tests for standard conformance are not comprehensive. They test
7831 the value of @code{__cplusplus} and a representative sample of the
7832 language features added in each version of the C++ standard. They
7833 do not test the C++ standard library, because this can be extremely
7834 slow, and because the C++ compiler might be generating code for a
7835 ``freestanding environment'' (in which most of the C++ standard library
7836 is optional). If you need to know whether a particular C++ standard
7837 header exists, use @code{AC_CHECK_HEADER}.
7839 None of the options that may be added to @code{CXX} by this macro
7840 enable @emph{strict} conformance to the C++ standard. In particular,
7841 system-specific extensions are not disabled. (For example, for GNU
7842 C++, the @option{-std=gnu++@var{nn}} options may be used, but not the
7843 @option{-std=c++@var{nn}} options.)
7846 @defmac AC_PROG_CXXCPP
7847 @acindex{PROG_CXXCPP}
7850 Set output variable @code{CXXCPP} to a command that runs the C++
7851 preprocessor. If @samp{$CXX -E} doesn't work, tries @code{cpp} and
7852 @file{/lib/cpp}, in that order. Because of this fallback, @code{CXXCPP}
7853 may or may not set C++-specific predefined macros (such as @code{__cplusplus}).
7855 It is portable to run @code{CXXCPP} only on files with a @file{.c},
7856 @file{.C}, @file{.cc}, or @file{.cpp} extension.
7858 Some preprocessors don't indicate missing include files by the error
7859 status. For such preprocessors an internal variable is set that causes
7860 other macros to check the standard error from the preprocessor and
7861 consider the test failed if any warnings have been reported. However,
7862 it is not known whether such broken preprocessors exist for C++.
7865 @defmac AC_PROG_CXX_C_O
7866 @acindex{PROG_CXX_C_O}
7867 @cvindex CXX_NO_MINUS_C_MINUS_O
7868 Test whether the C++ compiler accepts the options @option{-c} and
7869 @option{-o} simultaneously, and define @code{CXX_NO_MINUS_C_MINUS_O},
7874 @node Objective C Compiler
7875 @subsection Objective C Compiler Characteristics
7878 @defmac AC_PROG_OBJC (@ovar{compiler-search-list})
7884 Determine an Objective C compiler to use. If @code{OBJC} is not already
7885 set in the environment, check for Objective C compilers. Set output
7886 variable @code{OBJC} to the name of the compiler found.
7888 This macro may, however, be invoked with an optional first argument
7889 which, if specified, must be a blank-separated list of Objective C compilers to
7890 search for. This just gives the user an opportunity to specify an
7891 alternative search list for the Objective C compiler. For example, if you
7892 didn't like the default order, then you could invoke @code{AC_PROG_OBJC}
7896 AC_PROG_OBJC([gcc objcc objc])
7899 If using a compiler that supports GNU Objective C, set shell variable
7900 @code{GOBJC} to @samp{yes}. If output variable @code{OBJCFLAGS} was not
7901 already set, set it to @option{-g -O2} for a GNU Objective C
7902 compiler (@option{-O2} on systems where the compiler does not accept
7903 @option{-g}), or @option{-g} for other compilers.
7906 @defmac AC_PROG_OBJCPP
7907 @acindex{PROG_OBJCPP}
7910 Set output variable @code{OBJCPP} to a command that runs the Objective C
7911 preprocessor. If @samp{$OBJC -E} doesn't work, tries @code{cpp} and
7912 @file{/lib/cpp}, in that order. Because of this fallback, @code{CXXCPP}
7913 may or may not set Objective-C-specific predefined macros (such as
7918 @node Objective C++ Compiler
7919 @subsection Objective C++ Compiler Characteristics
7922 @defmac AC_PROG_OBJCXX (@ovar{compiler-search-list})
7923 @acindex{PROG_OBJCXX}
7925 @evindex OBJCXXFLAGS
7927 @ovindex OBJCXXFLAGS
7928 Determine an Objective C++ compiler to use. If @code{OBJCXX} is not already
7929 set in the environment, check for Objective C++ compilers. Set output
7930 variable @code{OBJCXX} to the name of the compiler found.
7932 This macro may, however, be invoked with an optional first argument
7933 which, if specified, must be a blank-separated list of Objective C++ compilers
7934 to search for. This just gives the user an opportunity to specify an
7935 alternative search list for the Objective C++ compiler. For example, if you
7936 didn't like the default order, then you could invoke @code{AC_PROG_OBJCXX}
7940 AC_PROG_OBJCXX([gcc g++ objcc++ objcxx])
7943 If using a compiler that supports GNU Objective C++, set shell variable
7944 @code{GOBJCXX} to @samp{yes}. If output variable @code{OBJCXXFLAGS} was not
7945 already set, set it to @option{-g -O2} for a GNU Objective C++
7946 compiler (@option{-O2} on systems where the compiler does not accept
7947 @option{-g}), or @option{-g} for other compilers.
7950 @defmac AC_PROG_OBJCXXCPP
7951 @acindex{PROG_OBJCXXCPP}
7954 Set output variable @code{OBJCXXCPP} to a command that runs the Objective C++
7955 preprocessor. If @samp{$OBJCXX -E} doesn't work, tries @code{cpp} and
7956 @file{/lib/cpp}, in that order. Because of this fallback, @code{CXXCPP}
7957 may or may not set Objective-C++-specific predefined macros (such as
7958 @code{__cplusplus} and @code{__OBJC__}).
7962 @node Erlang Compiler and Interpreter
7963 @subsection Erlang Compiler and Interpreter Characteristics
7966 Autoconf defines the following macros for determining paths to the essential
7967 Erlang/OTP programs:
7969 @defmac AC_ERLANG_PATH_ERLC (@ovar{value-if-not-found}, @dvar{path, $PATH})
7970 @acindex{ERLANG_PATH_ERLC}
7975 Determine an Erlang compiler to use. If @code{ERLC} is not already set in the
7976 environment, check for @command{erlc}. Set output variable @code{ERLC} to the
7977 complete path of the compiler command found. In addition, if @code{ERLCFLAGS}
7978 is not set in the environment, set it to an empty value.
7980 The two optional arguments have the same meaning as the two last arguments of
7981 macro @code{AC_PATH_PROG} for looking for the @command{erlc} program. For
7982 example, to look for @command{erlc} only in the @file{/usr/lib/erlang/bin}
7986 AC_ERLANG_PATH_ERLC([not found], [/usr/lib/erlang/bin])
7990 @defmac AC_ERLANG_NEED_ERLC (@dvar{path, $PATH})
7991 @acindex{ERLANG_NEED_ERLC}
7992 A simplified variant of the @code{AC_ERLANG_PATH_ERLC} macro, that prints an
7993 error message and exits the @command{configure} script if the @command{erlc}
7994 program is not found.
7997 @defmac AC_ERLANG_PATH_ERL (@ovar{value-if-not-found}, @dvar{path, $PATH})
7998 @acindex{ERLANG_PATH_ERL}
8001 Determine an Erlang interpreter to use. If @code{ERL} is not already
8003 environment, check for @command{erl}. Set output variable @code{ERL} to the
8004 complete path of the interpreter command found.
8006 The two optional arguments have the same meaning as the two last arguments of
8007 macro @code{AC_PATH_PROG} for looking for the @command{erl} program. For
8008 example, to look for @command{erl} only in the @file{/usr/lib/erlang/bin}
8012 AC_ERLANG_PATH_ERL([not found], [/usr/lib/erlang/bin])
8016 @defmac AC_ERLANG_NEED_ERL (@dvar{path, $PATH})
8017 @acindex{ERLANG_NEED_ERL}
8018 A simplified variant of the @code{AC_ERLANG_PATH_ERL} macro, that prints an
8019 error message and exits the @command{configure} script if the @command{erl}
8020 program is not found.
8024 @node Fortran Compiler
8025 @subsection Fortran Compiler Characteristics
8029 The Autoconf Fortran support is divided into two categories: legacy
8030 Fortran 77 macros (@code{F77}), and modern Fortran macros (@code{FC}).
8031 The former are intended for traditional Fortran 77 code, and have output
8032 variables like @code{F77}, @code{FFLAGS}, and @code{FLIBS}. The latter
8033 are for newer programs that can (or must) compile under the newer
8034 Fortran standards, and have output variables like @code{FC},
8035 @code{FCFLAGS}, and @code{FCLIBS}.
8037 Except for the macros @code{AC_FC_SRCEXT}, @code{AC_FC_FREEFORM},
8038 @code{AC_FC_FIXEDFORM}, and @code{AC_FC_LINE_LENGTH} (see below), the
8039 @code{FC} and @code{F77} macros behave almost identically, and so they
8040 are documented together in this section.
8043 @defmac AC_PROG_F77 (@ovar{compiler-search-list})
8049 @caindex f77_compiler_gnu
8051 Determine a Fortran 77 compiler to use. If @code{F77} is not already
8052 set in the environment, then check for @code{g77} and @code{f77}, and
8053 then some other names. Set the output variable @code{F77} to the name
8054 of the compiler found.
8056 This macro may, however, be invoked with an optional first argument
8057 which, if specified, must be a blank-separated list of Fortran 77
8058 compilers to search for. This just gives the user an opportunity to
8059 specify an alternative search list for the Fortran 77 compiler. For
8060 example, if you didn't like the default order, then you could invoke
8061 @code{AC_PROG_F77} like this:
8064 AC_PROG_F77([fl32 f77 fort77 xlf g77 f90 xlf90])
8067 If using a compiler that supports GNU Fortran 77,
8068 set the shell variable @code{G77} to @samp{yes}.
8069 If the output variable @code{FFLAGS} was not already set in the
8070 environment, set it to @option{-g -02} for @code{g77} (or @option{-O2}
8071 where the GNU Fortran 77 compiler does not accept @option{-g}), or
8072 @option{-g} for other compilers.
8074 The result of the GNU test is cached in the
8075 @code{ac_cv_f77_compiler_gnu} variable, acceptance of @option{-g} in the
8076 @code{ac_cv_prog_f77_g} variable.
8079 @defmac AC_PROG_FC (@ovar{compiler-search-list}, @ovar{dialect})
8085 @caindex fc_compiler_gnu
8087 Determine a Fortran compiler to use. If @code{FC} is not already set in
8088 the environment, then @code{dialect} is a hint to indicate what Fortran
8089 dialect to search for; the default is to search for the newest available
8090 dialect. Set the output variable @code{FC} to the name of the compiler
8093 By default, newer dialects are preferred over older dialects, but if
8094 @code{dialect} is specified then older dialects are preferred starting
8095 with the specified dialect. @code{dialect} can currently be one of
8096 Fortran 77, Fortran 90, or Fortran 95. However, this is only a hint of
8097 which compiler @emph{name} to prefer (e.g., @code{f90} or @code{f95}),
8098 and no attempt is made to guarantee that a particular language standard
8099 is actually supported. Thus, it is preferable that you avoid the
8100 @code{dialect} option, and use AC_PROG_FC only for code compatible with
8101 the latest Fortran standard.
8103 This macro may, alternatively, be invoked with an optional first argument
8104 which, if specified, must be a blank-separated list of Fortran
8105 compilers to search for, just as in @code{AC_PROG_F77}.
8107 If using a compiler that supports GNU Fortran,
8108 set the shell variable @code{GFC} to @samp{yes}.
8109 If the output variable @code{FCFLAGS} was not already set in the
8110 environment, then set it to @option{-g -02} for a GNU Fortran compiler (or
8111 @option{-O2} where the compiler does not accept @option{-g}), or
8112 @option{-g} for other compilers.
8114 The result of the GNU test is cached in the @code{ac_cv_fc_compiler_gnu}
8115 variable, acceptance of @option{-g} in the @code{ac_cv_prog_fc_g}
8119 @defmac AC_PROG_F77_C_O
8120 @defmacx AC_PROG_FC_C_O
8121 @acindex{PROG_F77_C_O}
8122 @acindex{PROG_FC_C_O}
8123 @cvindex F77_NO_MINUS_C_MINUS_O
8124 @cvindex FC_NO_MINUS_C_MINUS_O
8125 @caindex prog_f77_c_o
8126 @caindex prog_fc_c_o
8127 Test whether the Fortran compiler accepts the options @option{-c} and
8128 @option{-o} simultaneously, and define @code{F77_NO_MINUS_C_MINUS_O} or
8129 @code{FC_NO_MINUS_C_MINUS_O}, respectively, if it does not.
8131 The result of the test is cached in the @code{ac_cv_prog_f77_c_o} or
8132 @code{ac_cv_prog_fc_c_o} variable, respectively.
8135 The following macros check for Fortran compiler characteristics.
8136 To check for characteristics not listed here, use
8137 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
8138 @code{AC_RUN_IFELSE} (@pxref{Runtime}), making sure to first set the
8139 current language to Fortran 77 or Fortran via @code{AC_LANG([Fortran 77])}
8140 or @code{AC_LANG(Fortran)} (@pxref{Language Choice}).
8143 @defmac AC_F77_LIBRARY_LDFLAGS
8144 @defmacx AC_FC_LIBRARY_LDFLAGS
8145 @acindex{F77_LIBRARY_LDFLAGS}
8147 @acindex{FC_LIBRARY_LDFLAGS}
8153 Determine the linker flags (e.g., @option{-L} and @option{-l}) for the
8154 @dfn{Fortran intrinsic and runtime libraries} that are required to
8155 successfully link a Fortran program or shared library. The output
8156 variable @code{FLIBS} or @code{FCLIBS} is set to these flags (which
8157 should be included after @code{LIBS} when linking).
8159 This macro is intended to be used in those situations when it is
8160 necessary to mix, e.g., C++ and Fortran source code in a single
8161 program or shared library (@pxref{Mixing Fortran 77 With C and C++, , ,
8162 automake, GNU Automake}).
8164 For example, if object files from a C++ and Fortran compiler must be
8165 linked together, then the C++ compiler/linker must be used for linking
8166 (since special C++-ish things need to happen at link time like calling
8167 global constructors, instantiating templates, enabling exception
8170 However, the Fortran intrinsic and runtime libraries must be linked in
8171 as well, but the C++ compiler/linker doesn't know by default how to add
8172 these Fortran 77 libraries. Hence, this macro was created to determine
8173 these Fortran libraries.
8175 The macros @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
8176 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} are probably also necessary to
8177 link C/C++ with Fortran; see below. Further, it is highly recommended
8178 that you use @code{AC_CONFIG_HEADERS} (@pxref{Configuration Headers})
8179 because the complex defines that the function wrapper macros create
8180 may not work with C/C++ compiler drivers.
8182 These macros internally compute the flag needed to verbose linking
8183 output and cache it in @code{ac_cv_prog_f77_v} or @code{ac_cv_prog_fc_v}
8184 variables, respectively. The computed linker flags are cached in
8185 @code{ac_cv_f77_libs} or @code{ac_cv_fc_libs}, respectively.
8188 @defmac AC_F77_DUMMY_MAIN (@ovar{action-if-found}, @
8189 @dvar{action-if-not-found, AC_MSG_FAILURE})
8190 @defmacx AC_FC_DUMMY_MAIN (@ovar{action-if-found}, @
8191 @dvar{action-if-not-found, AC_MSG_FAILURE})
8192 @acindex{F77_DUMMY_MAIN}
8193 @cvindex F77_DUMMY_MAIN
8194 @acindex{FC_DUMMY_MAIN}
8195 @cvindex FC_DUMMY_MAIN
8196 @caindex f77_dummy_main
8197 @caindex fc_dummy_main
8198 With many compilers, the Fortran libraries detected by
8199 @code{AC_F77_LIBRARY_LDFLAGS} or @code{AC_FC_LIBRARY_LDFLAGS} provide
8200 their own @code{main} entry function that initializes things like
8201 Fortran I/O, and which then calls a user-provided entry function named
8202 (say) @code{MAIN__} to run the user's program. The
8203 @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
8204 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros figure out how to deal with
8207 When using Fortran for purely numerical functions (no I/O, etc.)@: often
8208 one prefers to provide one's own @code{main} and skip the Fortran
8209 library initializations. In this case, however, one may still need to
8210 provide a dummy @code{MAIN__} routine in order to prevent linking errors
8211 on some systems. @code{AC_F77_DUMMY_MAIN} or @code{AC_FC_DUMMY_MAIN}
8212 detects whether any such routine is @emph{required} for linking, and
8213 what its name is; the shell variable @code{F77_DUMMY_MAIN} or
8214 @code{FC_DUMMY_MAIN} holds this name, @code{unknown} when no solution
8215 was found, and @code{none} when no such dummy main is needed.
8217 By default, @var{action-if-found} defines @code{F77_DUMMY_MAIN} or
8218 @code{FC_DUMMY_MAIN} to the name of this routine (e.g., @code{MAIN__})
8219 @emph{if} it is required. @var{action-if-not-found} defaults to
8220 exiting with an error.
8222 In order to link with Fortran routines, the user's C/C++ program should
8223 then include the following code to define the dummy main if it is
8227 @c If you change this example, adjust tests/fortran.at:AC_F77_DUMMY_MAIN usage.
8228 #ifdef F77_DUMMY_MAIN
8232 int F77_DUMMY_MAIN (void) @{ return 1; @}
8236 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
8238 Note that this macro is called automatically from @code{AC_F77_WRAPPERS}
8239 or @code{AC_FC_WRAPPERS}; there is generally no need to call it
8240 explicitly unless one wants to change the default actions.
8242 The result of this macro is cached in the @code{ac_cv_f77_dummy_main} or
8243 @code{ac_cv_fc_dummy_main} variable, respectively.
8254 As discussed above, many Fortran libraries allow you to provide an entry
8255 point called (say) @code{MAIN__} instead of the usual @code{main}, which
8256 is then called by a @code{main} function in the Fortran libraries that
8257 initializes things like Fortran I/O@. The
8258 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros detect whether it is
8259 @emph{possible} to utilize such an alternate main function, and defines
8260 @code{F77_MAIN} and @code{FC_MAIN} to the name of the function. (If no
8261 alternate main function name is found, @code{F77_MAIN} and @code{FC_MAIN} are
8262 simply defined to @code{main}.)
8264 Thus, when calling Fortran routines from C that perform things like I/O,
8265 one should use this macro and declare the "main" function like so:
8268 @c If you change this example, adjust tests/fortran.at:AC_F77_DUMMY_MAIN usage.
8272 int F77_MAIN (int argc, char *argv[]);
8275 (Again, replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
8277 The result of this macro is cached in the @code{ac_cv_f77_main} or
8278 @code{ac_cv_fc_main} variable, respectively.
8281 @defmac AC_F77_WRAPPERS
8282 @defmacx AC_FC_WRAPPERS
8283 @acindex{F77_WRAPPERS}
8286 @acindex{FC_WRAPPERS}
8289 @caindex f77_mangling
8290 @caindex fc_mangling
8291 Defines C macros @code{F77_FUNC (name, NAME)}, @code{FC_FUNC (name, NAME)},
8292 @code{F77_FUNC_(name, NAME)}, and @code{FC_FUNC_(name, NAME)} to properly
8293 mangle the names of C/C++ identifiers, and identifiers with underscores,
8294 respectively, so that they match the name-mangling scheme used by the
8297 Fortran is case-insensitive, and in order to achieve this the Fortran
8298 compiler converts all identifiers into a canonical case and format. To
8299 call a Fortran subroutine from C or to write a C function that is
8300 callable from Fortran, the C program must explicitly use identifiers in
8301 the format expected by the Fortran compiler. In order to do this, one
8302 simply wraps all C identifiers in one of the macros provided by
8303 @code{AC_F77_WRAPPERS} or @code{AC_FC_WRAPPERS}. For example, suppose
8304 you have the following Fortran 77 subroutine:
8307 @c If you change this example, adjust tests/fortran.at:AC_F77_DUMMY_MAIN usage.
8308 subroutine foobar (x, y)
8309 double precision x, y
8315 You would then declare its prototype in C or C++ as:
8318 @c If you change this example, adjust tests/fortran.at:AC_F77_DUMMY_MAIN usage.
8319 #define FOOBAR_F77 F77_FUNC (foobar, FOOBAR)
8321 extern "C" /* prevent C++ name mangling */
8323 void FOOBAR_F77 (double *x, double *y);
8326 Note that we pass both the lowercase and uppercase versions of the
8327 function name to @code{F77_FUNC} so that it can select the right one.
8328 Note also that all parameters to Fortran 77 routines are passed as
8329 pointers (@pxref{Mixing Fortran 77 With C and C++, , , automake, GNU
8332 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
8334 Although Autoconf tries to be intelligent about detecting the
8335 name-mangling scheme of the Fortran compiler, there may be Fortran
8336 compilers that it doesn't support yet. In this case, the above code
8337 generates a compile-time error, but some other behavior
8338 (e.g., disabling Fortran-related features) can be induced by checking
8339 whether @code{F77_FUNC} or @code{FC_FUNC} is defined.
8341 Now, to call that routine from a C program, we would do something like:
8344 @c If you change this example, adjust tests/fortran.at:AC_F77_DUMMY_MAIN usage.
8346 double x = 2.7183, y;
8347 FOOBAR_F77 (&x, &y);
8351 If the Fortran identifier contains an underscore (e.g., @code{foo_bar}),
8352 you should use @code{F77_FUNC_} or @code{FC_FUNC_} instead of
8353 @code{F77_FUNC} or @code{FC_FUNC} (with the same arguments). This is
8354 because some Fortran compilers mangle names differently if they contain
8357 The name mangling scheme is encoded in the @code{ac_cv_f77_mangling} or
8358 @code{ac_cv_fc_mangling} cache variable, respectively, and also used for
8359 the @code{AC_F77_FUNC} and @code{AC_FC_FUNC} macros described below.
8362 @defmac AC_F77_FUNC (@var{name}, @ovar{shellvar})
8363 @defmacx AC_FC_FUNC (@var{name}, @ovar{shellvar})
8366 Given an identifier @var{name}, set the shell variable @var{shellvar} to
8367 hold the mangled version @var{name} according to the rules of the
8368 Fortran linker (see also @code{AC_F77_WRAPPERS} or
8369 @code{AC_FC_WRAPPERS}). @var{shellvar} is optional; if it is not
8370 supplied, the shell variable is simply @var{name}. The purpose of
8371 this macro is to give the caller a way to access the name-mangling
8372 information other than through the C preprocessor as above, for example,
8373 to call Fortran routines from some language other than C/C++.
8376 @defmac AC_FC_SRCEXT (@var{ext}, @ovar{action-if-success}, @
8377 @dvar{action-if-failure, AC_MSG_FAILURE})
8378 @defmacx AC_FC_PP_SRCEXT (@var{ext}, @ovar{action-if-success}, @
8379 @dvar{action-if-failure, AC_MSG_FAILURE})
8381 @acindex{FC_PP_SRCEXT}
8382 @caindex fc_srcext_@var{ext}
8383 @caindex fc_pp_srcext_@var{ext}
8384 By default, the @code{FC} macros perform their tests using a @file{.f}
8385 extension for source-code files. Some compilers, however, only enable
8386 newer language features for appropriately named files, e.g., Fortran 90
8387 features only for @file{.f90} files, or preprocessing only with
8388 @file{.F} files or maybe other upper-case extensions. On the other
8389 hand, some other compilers expect all source files to end in @file{.f}
8390 and require special flags to support other file name extensions. The
8391 @code{AC_FC_SRCEXT} and @code{AC_FC_PP_SRCEXT} macros deal with these
8394 The @code{AC_FC_SRCEXT} macro tries to get the @code{FC} compiler to
8395 accept files ending with the extension @file{.@var{ext}} (i.e.,
8396 @var{ext} does @emph{not} contain the dot). If any special compiler
8397 flags are needed for this, it stores them in the output variable
8398 @code{FCFLAGS_@var{ext}}. This extension and these flags are then used
8399 for all subsequent @code{FC} tests (until @code{AC_FC_SRCEXT} or
8400 @code{AC_FC_PP_SRCEXT} is called another time).
8402 For example, you would use @code{AC_FC_SRCEXT(f90)} to employ the
8403 @file{.f90} extension in future tests, and it would set the
8404 @code{FCFLAGS_f90} output variable with any extra flags that are needed
8405 to compile such files.
8407 Similarly, the @code{AC_FC_PP_SRCEXT} macro tries to get the @code{FC}
8408 compiler to preprocess and compile files with the extension
8409 @file{.@var{ext}}. When both @command{fpp} and @command{cpp} style
8410 preprocessing are provided, the former is preferred, as the latter may
8411 treat continuation lines, @code{//} tokens, and white space differently
8412 from what some Fortran dialects expect. Conversely, if you do not want
8413 files to be preprocessed, use only lower-case characters in the file
8414 name extension. Like with @code{AC_FC_SRCEXT(f90)}, any needed flags
8415 are stored in the @code{FCFLAGS_@var{ext}} variable.
8417 The @code{FCFLAGS_@var{ext}} flags can @emph{not} be simply absorbed
8418 into @code{FCFLAGS}, for two reasons based on the limitations of some
8419 compilers. First, only one @code{FCFLAGS_@var{ext}} can be used at a
8420 time, so files with different extensions must be compiled separately.
8421 Second, @code{FCFLAGS_@var{ext}} must appear @emph{immediately} before
8422 the source-code file name when compiling. So, continuing the example
8423 above, you might compile a @file{foo.f90} file in your makefile with the
8428 $(FC) -c $(FCFLAGS) $(FCFLAGS_f90) '$(srcdir)/foo.f90'
8431 If @code{AC_FC_SRCEXT} or @code{AC_FC_PP_SRCEXT} succeeds in compiling
8432 files with the @var{ext} extension, it calls @var{action-if-success}
8433 (defaults to nothing). If it fails, and cannot find a way to make the
8434 @code{FC} compiler accept such files, it calls @var{action-if-failure}
8435 (defaults to exiting with an error message).
8437 The @code{AC_FC_SRCEXT} and @code{AC_FC_PP_SRCEXT} macros cache their
8438 results in @code{ac_cv_fc_srcext_@var{ext}} and
8439 @code{ac_cv_fc_pp_srcext_@var{ext}} variables, respectively.
8442 @defmac AC_FC_PP_DEFINE (@ovar{action-if-success}, @
8443 @dvar{action-if-failure, AC_MSG_FAILURE})
8444 @acindex{FC_PP_DEFINE}
8445 @caindex fc_pp_define
8447 Find a flag to specify defines for preprocessed Fortran. Not all
8448 Fortran compilers use @option{-D}. Substitute @code{FC_DEFINE} with
8449 the result and call @var{action-if-success} (defaults to nothing) if
8450 successful, and @var{action-if-failure} (defaults to failing with an
8451 error message) if not.
8453 This macro calls @code{AC_FC_PP_SRCEXT([F])} in order to learn how to
8454 preprocess a @file{conftest.F} file, but restores a previously used
8455 Fortran source file extension afterwards again.
8457 The result of this test is cached in the @code{ac_cv_fc_pp_define}
8461 @defmac AC_FC_FREEFORM (@ovar{action-if-success}, @
8462 @dvar{action-if-failure, AC_MSG_FAILURE})
8463 @acindex{FC_FREEFORM}
8464 @caindex fc_freeform
8466 Try to ensure that the Fortran compiler (@code{$FC}) allows free-format
8467 source code (as opposed to the older fixed-format style from Fortran
8468 77). If necessary, it may add some additional flags to @code{FCFLAGS}.
8470 This macro is most important if you are using the default @file{.f}
8471 extension, since many compilers interpret this extension as indicating
8472 fixed-format source unless an additional flag is supplied. If you
8473 specify a different extension with @code{AC_FC_SRCEXT}, such as
8474 @file{.f90}, then @code{AC_FC_FREEFORM} ordinarily succeeds without
8475 modifying @code{FCFLAGS}. For extensions which the compiler does not
8476 know about, the flag set by the @code{AC_FC_SRCEXT} macro might let
8477 the compiler assume Fortran 77 by default, however.
8479 If @code{AC_FC_FREEFORM} succeeds in compiling free-form source, it
8480 calls @var{action-if-success} (defaults to nothing). If it fails, it
8481 calls @var{action-if-failure} (defaults to exiting with an error
8484 The result of this test, or @samp{none} or @samp{unknown}, is cached in
8485 the @code{ac_cv_fc_freeform} variable.
8488 @defmac AC_FC_FIXEDFORM (@ovar{action-if-success}, @
8489 @dvar{action-if-failure, AC_MSG_FAILURE})
8490 @acindex{FC_FIXEDFORM}
8491 @caindex fc_fixedform
8493 Try to ensure that the Fortran compiler (@code{$FC}) allows the old
8494 fixed-format source code (as opposed to free-format style). If
8495 necessary, it may add some additional flags to @code{FCFLAGS}.
8497 This macro is needed for some compilers alias names like @command{xlf95}
8498 which assume free-form source code by default, and in case you want to
8499 use fixed-form source with an extension like @file{.f90} which many
8500 compilers interpret as free-form by default. If you specify a different
8501 extension with @code{AC_FC_SRCEXT}, such as @file{.f}, then
8502 @code{AC_FC_FIXEDFORM} ordinarily succeeds without modifying
8505 If @code{AC_FC_FIXEDFORM} succeeds in compiling fixed-form source, it
8506 calls @var{action-if-success} (defaults to nothing). If it fails, it
8507 calls @var{action-if-failure} (defaults to exiting with an error
8510 The result of this test, or @samp{none} or @samp{unknown}, is cached in
8511 the @code{ac_cv_fc_fixedform} variable.
8514 @defmac AC_FC_LINE_LENGTH (@ovar{length}, @ovar{action-if-success}, @
8515 @dvar{action-if-failure, AC_MSG_FAILURE})
8516 @acindex{FC_LINE_LENGTH}
8517 @caindex fc_line_length
8519 Try to ensure that the Fortran compiler (@code{$FC}) accepts long source
8520 code lines. The @var{length} argument may be given as 80, 132, or
8521 unlimited, and defaults to 132. Note that line lengths above 250
8522 columns are not portable, and some compilers do not accept more than 132
8523 columns at least for fixed format source. If necessary, it may add some
8524 additional flags to @code{FCFLAGS}.
8526 If @code{AC_FC_LINE_LENGTH} succeeds in compiling fixed-form source, it
8527 calls @var{action-if-success} (defaults to nothing). If it fails, it
8528 calls @var{action-if-failure} (defaults to exiting with an error
8531 The result of this test, or @samp{none} or @samp{unknown}, is cached in
8532 the @code{ac_cv_fc_line_length} variable.
8535 @defmac AC_FC_CHECK_BOUNDS (@ovar{action-if-success}, @
8536 @dvar{action-if-failure, AC_MSG_FAILURE})
8537 @acindex{FC_CHECK_BOUNDS}
8538 @caindex fc_check_bounds
8540 The @code{AC_FC_CHECK_BOUNDS} macro tries to enable array bounds checking
8541 in the Fortran compiler. If successful, the @var{action-if-success}
8542 is called and any needed flags are added to @code{FCFLAGS}. Otherwise,
8543 @var{action-if-failure} is called, which defaults to failing with an error
8544 message. The macro currently requires Fortran 90 or a newer dialect.
8546 The result of the macro is cached in the @code{ac_cv_fc_check_bounds}
8550 @defmac AC_F77_IMPLICIT_NONE (@ovar{action-if-success}, @
8551 @dvar{action-if-failure, AC_MSG_FAILURE})
8552 @defmacx AC_FC_IMPLICIT_NONE (@ovar{action-if-success}, @
8553 @dvar{action-if-failure, AC_MSG_FAILURE})
8554 @acindex{F77_IMPLICIT_NONE}
8555 @acindex{FC_IMPLICIT_NONE}
8556 @caindex f77_implicit_none
8557 @caindex fc_implicit_none
8559 Try to disallow implicit declarations in the Fortran compiler. If
8560 successful, @var{action-if-success} is called and any needed flags
8561 are added to @code{FFLAGS} or @code{FCFLAGS}, respectively. Otherwise,
8562 @var{action-if-failure} is called, which defaults to failing with an error
8565 The result of these macros are cached in the
8566 @code{ac_cv_f77_implicit_none} and @code{ac_cv_fc_implicit_none}
8567 variables, respectively.
8570 @defmac AC_FC_MODULE_EXTENSION
8571 @acindex{FC_MODULE_EXTENSION}
8572 @caindex fc_module_ext
8575 Find the Fortran 90 module file name extension. Most Fortran 90
8576 compilers store module information in files separate from the object
8577 files. The module files are usually named after the name of the module
8578 rather than the source file name, with characters possibly turned to
8579 upper case, plus an extension, often @file{.mod}.
8581 Not all compilers use module files at all, or by default. The Cray
8582 Fortran compiler requires @option{-e m} in order to store and search
8583 module information in @file{.mod} files rather than in object files.
8584 Likewise, the Fujitsu Fortran compilers uses the @option{-Am} option to
8585 indicate how module information is stored.
8587 The @code{AC_FC_MODULE_EXTENSION} macro computes the module extension
8588 without the leading dot, and stores that in the @code{FC_MODEXT}
8589 variable. If the compiler does not produce module files, or the
8590 extension cannot be determined, @code{FC_MODEXT} is empty. Typically,
8591 the result of this macro may be used in cleanup @command{make} rules as
8596 -test -z "$(FC_MODEXT)" || rm -f *.$(FC_MODEXT)
8599 The extension, or @samp{unknown}, is cached in the
8600 @code{ac_cv_fc_module_ext} variable.
8603 @defmac AC_FC_MODULE_FLAG (@ovar{action-if-success}, @
8604 @dvar{action-if-failure, AC_MSG_FAILURE})
8605 @acindex{FC_MODULE_FLAG}
8606 @caindex fc_module_flag
8610 Find the compiler flag to include Fortran 90 module information from
8611 another directory, and store that in the @code{FC_MODINC} variable.
8612 Call @var{action-if-success} (defaults to nothing) if successful, and
8613 set @code{FC_MODINC} to empty and call @var{action-if-failure} (defaults
8614 to exiting with an error message) if not.
8616 Most Fortran 90 compilers provide a way to specify module directories.
8617 Some have separate flags for the directory to write module files to,
8618 and directories to search them in, whereas others only allow writing to
8619 the current directory or to the first directory specified in the include
8620 path. Further, with some compilers, the module search path and the
8621 preprocessor search path can only be modified with the same flag. Thus,
8622 for portability, write module files to the current directory only and
8623 list that as first directory in the search path.
8625 There may be no whitespace between @code{FC_MODINC} and the following
8626 directory name, but @code{FC_MODINC} may contain trailing white space.
8627 For example, if you use Automake and would like to search @file{../lib}
8628 for module files, you can use the following:
8631 AM_FCFLAGS = $(FC_MODINC). $(FC_MODINC)../lib
8634 Inside @command{configure} tests, you can use:
8637 if test -n "$FC_MODINC"; then
8638 FCFLAGS="$FCFLAGS $FC_MODINC. $FC_MODINC../lib"
8642 The flag is cached in the @code{ac_cv_fc_module_flag} variable.
8643 The substituted value of @code{FC_MODINC} may refer to the
8644 @code{ac_empty} dummy placeholder empty variable, to avoid losing
8645 the significant trailing whitespace in a @file{Makefile}.
8648 @defmac AC_FC_MODULE_OUTPUT_FLAG (@ovar{action-if-success}, @
8649 @dvar{action-if-failure, AC_MSG_FAILURE})
8650 @acindex{FC_MODULE_OUTPUT_FLAG}
8651 @caindex fc_module_output_flag
8654 Find the compiler flag to write Fortran 90 module information to
8655 another directory, and store that in the @code{FC_MODOUT} variable.
8656 Call @var{action-if-success} (defaults to nothing) if successful, and
8657 set @code{FC_MODOUT} to empty and call @var{action-if-failure} (defaults
8658 to exiting with an error message) if not.
8660 Not all Fortran 90 compilers write module files, and of those that do,
8661 not all allow writing to a directory other than the current one, nor
8662 do all have separate flags for writing and reading; see the description
8663 of @code{AC_FC_MODULE_FLAG} above. If you need to be able to write to
8664 another directory, for maximum portability use @code{FC_MODOUT} before
8665 any @code{FC_MODINC} and include both the current directory and the one
8666 you write to in the search path:
8669 AM_FCFLAGS = $(FC_MODOUT)../mod $(FC_MODINC)../mod $(FC_MODINC). @dots{}
8672 The flag is cached in the @code{ac_cv_fc_module_output_flag} variable.
8673 The substituted value of @code{FC_MODOUT} may refer to the
8674 @code{ac_empty} dummy placeholder empty variable, to avoid losing
8675 the significant trailing whitespace in a @file{Makefile}.
8678 @defmac AC_F77_CRAY_POINTERS (@ovar{action-if-success}, @
8679 @dvar{action-if-failure, AC_MSG_FAILURE})
8680 @defmacx AC_FC_CRAY_POINTERS (@ovar{action-if-success}, @
8681 @dvar{action-if-failure, AC_MSG_FAILURE})
8682 @acindex{F77_CRAY_POINTERS}
8683 @acindex{FC_CRAY_POINTERS}
8684 @caindex fc_cray_pointer
8686 Try to ensure that the Fortran compiler (@code{$F77} or @code{$FC})
8687 accepts Cray pointers. If successful, the @var{action-if-success} is
8688 called and any needed flags are added to @code{FFLAGS} or
8689 @code{FCFLAGS}. Otherwise, @var{action-if-failure} is called, which
8690 defaults to failing with an error message.
8692 Cray pointers are a non-standard extension supported by many Fortran
8693 compilers which allow an integer to be declared as C-like pointer to
8696 The result of this test, or @samp{none} or @samp{unknown}, is cached in
8697 the @code{ac_cv_f77_cray_ptr} or @code{ac_cv_fc_cray_ptr} variable.
8702 @subsection Go Compiler Characteristics
8705 Autoconf provides basic support for the Go programming language when
8706 using the @code{gccgo} compiler (there is currently no support for the
8707 @code{6g} and @code{8g} compilers).
8709 @defmac AC_PROG_GO (@ovar{compiler-search-list})
8710 Find the Go compiler to use. Check whether the environment variable
8711 @code{GOC} is set; if so, then set output variable @code{GOC} to its
8714 Otherwise, if the macro is invoked without an argument, then search for
8715 a Go compiler named @code{gccgo}. If it is not found, then as a last
8716 resort set @code{GOC} to @code{gccgo}.
8718 This macro may be invoked with an optional first argument which, if
8719 specified, must be a blank-separated list of Go compilers to search for.
8721 If output variable @code{GOFLAGS} was not already set, set it to
8722 @option{-g -O2}. If your package does not like this default,
8723 @code{GOFLAGS} may be set before @code{AC_PROG_GO}.
8727 @node System Services
8728 @section System Services
8730 The following macros check for operating system services or capabilities.
8736 @cindex X Window System
8737 Try to locate the X Window System include files and libraries. If the
8738 user gave the command line options @option{--x-includes=@var{dir}} and
8739 @option{--x-libraries=@var{dir}}, use those directories.
8741 If either or both were not given, get the missing values by running
8742 @code{xmkmf} (or an executable pointed to by the @code{XMKMF}
8743 environment variable) on a trivial @file{Imakefile} and examining the
8744 makefile that it produces. Setting @code{XMKMF} to @samp{false}
8745 disables this method.
8747 If this method fails to find the X Window System, @command{configure}
8748 looks for the files in several directories where they often reside.
8749 If either method is successful, set the shell variables
8750 @code{x_includes} and @code{x_libraries} to their locations, unless they
8751 are in directories the compiler searches by default.
8753 If both methods fail, or the user gave the command line option
8754 @option{--without-x}, set the shell variable @code{no_x} to @samp{yes};
8755 otherwise set it to the empty string.
8758 @anchor{AC_PATH_XTRA}
8759 @defmac AC_PATH_XTRA
8763 @ovindex X_EXTRA_LIBS
8765 @cvindex X_DISPLAY_MISSING
8766 An enhanced version of @code{AC_PATH_X}. It adds the C compiler flags
8767 that X needs to output variable @code{X_CFLAGS}, and the X linker flags
8768 to @code{X_LIBS}. Define @code{X_DISPLAY_MISSING} if X is not
8771 This macro also checks for special libraries that some systems need in
8772 order to compile X programs. It adds any that the system needs to
8773 output variable @code{X_EXTRA_LIBS}. And it checks for special X11R6
8774 libraries that need to be linked with before @option{-lX11}, and adds
8775 any found to the output variable @code{X_PRE_LIBS}.
8777 @c This is an incomplete kludge. Make a real way to do it.
8778 @c If you need to check for other X functions or libraries yourself, then
8779 @c after calling this macro, add the contents of @code{X_EXTRA_LIBS} to
8780 @c @code{LIBS} temporarily, like this: (FIXME - add example)
8783 @anchor{AC_SYS_INTERPRETER}
8784 @defmac AC_SYS_INTERPRETER
8785 @acindex{SYS_INTERPRETER}
8786 Check whether the system supports starting scripts with a line of the
8787 form @samp{#!/bin/sh} to select the interpreter to use for the script.
8788 After running this macro, shell code in @file{configure.ac} can check
8789 the shell variable @code{interpval}; it is set to @samp{yes}
8790 if the system supports @samp{#!}, @samp{no} if not.
8793 @anchor{AC_SYS_LARGEFILE}
8794 @defmac AC_SYS_LARGEFILE
8795 @acindex{SYS_LARGEFILE}
8796 @cvindex _FILE_OFFSET_BITS
8799 @cindex Large file support
8801 If the default @code{off_t} type is a 32-bit integer,
8802 and therefore cannot be used with files 2 GiB or larger,
8803 make a wider @code{off_t} available if the system supports it.
8804 Similarly, widen other types related to sizes of files and file systems
8805 if possible. These types may include @code{blkcnt_t}, @code{dev_t},
8806 @code{ino_t}, @code{fsblkcnt_t}, @code{fsfilcnt_t}, and @code{rlim_t}.
8808 Also, arrange for a @command{configure} option @code{--enable-year2038}
8809 to request widening the type @code{time_t} as needed to represent file
8810 wand other timestamps after mid-January 2038. This widening is possible
8811 only on 32-bit GNU/Linux x86 and ARM systems with glibc 2.34 or later.
8812 If year-2038 support is requested but @command{configure} fails to find a way
8813 to widen @code{time_t} and inspection of the system suggests that
8814 this feature is available somehow, @command{configure} will error out.
8815 If you want the default to be @code{--enable-year2038}, you can use
8816 @code{AC_SYS_YEAR2038} or @code{AC_SYS_YEAR2038_RECOMMENDED}
8817 instead of @code{AC_SYS_LARGEFILE}.
8818 In other words, older packages that have long used @code{AC_SYS_LARGEFILE}
8819 can have year-2038 support on 32-bit GNU/Linux x86 and ARM systems either by
8820 regenerating @file{configure} with current Autoconf and configuring with
8821 @option{--enable-year2038}, or by using @code{AC_SYS_YEAR2038} or
8822 @code{AC_SYS_YEAR2038_RECOMMENDED} and configuring without
8823 @option{--disable-year2038}.
8824 A future version of Autoconf might change the @code{AC_SYS_LARGEFILE}
8825 default to @code{--enable-year2038}; if and when that happens,
8826 @code{AC_SYS_LARGEFILE} and @code{AC_SYS_YEAR2038} will become equivalent.
8827 @xref{AC_SYS_YEAR2038}.
8829 Set the shell variable @code{ac_have_largefile} to @samp{yes} or
8830 @code{no} depending on whether a wide @code{off_t} is available,
8831 regardless of whether arrangements were necessary.
8832 Similarly, set the shell variable @code{ac_have_year2038} to @code{yes}
8833 or @code{no} depending on whether a wide-enough @code{time_t} is available.
8835 Define preprocessor macros if necessary to make types wider;
8836 for example, on GNU/Linux systems the macros @code{_FILE_OFFSET_BITS}
8837 and @code{_TIME_BITS} can be defined. Some of these macros work only if
8838 defined before the first system header is included;
8839 therefore, when using this macro in concert with
8840 @code{AC_CONFIG_HEADERS}, make sure that @file{config.h} is included
8841 before any system headers.
8843 On obsolete IRIX systems, also change the output variable @code{CC} to
8844 add compiler options needed for wide @code{off_t}.
8846 Large-file support can be disabled by configuring with the
8847 @option{--disable-largefile} option, and year-2038 support can
8848 be enabled and disabled via the @option{--enable-year2038} and
8849 @option{--disable-year2038} options. These options have no effect on
8850 systems where types are wide enough by default.
8851 Large-file support is required for year-2038 support: if you configure
8852 with @option{--disable-largefile} on a platform with 32-bit
8853 @code{time_t}, then year-2038 support is not available.
8855 Disabling large-file or year-2038 support can have surprising effects,
8856 such as causing functions like @code{readdir} and @code{stat} to fail
8857 even on a small file because its inode number or timestamp is out of range.
8859 Regardless of whether you use this macro, portable programs should not
8860 assume that any of the types listed above fit into a @code{long int}.
8861 For example, it is not portable to print an arbitrary @code{off_t} or
8862 @code{time_t} value @code{X} with @code{printf ("%ld", (long int) X)}.
8864 The standard C library functions @code{fseek} and @code{ftell}
8865 do not use @code{off_t}. If you need to use either of these functions,
8866 you should use @code{AC_FUNC_FSEEKO} as well as @code{AC_SYS_LARGEFILE},
8867 and then use their Posix replacements @code{fseeko} and @code{ftello}.
8868 @xref{AC_FUNC_FSEEKO}.
8870 When using @code{AC_SYS_LARGEFILE} in different packages that are linked
8871 together and that have interfaces that depend on the width of @code{off_t},
8872 @code{time_t} or related types, the simplest thing is to configure all
8873 components the same way. For example, if an application uses
8874 @code{AC_SYS_LARGEFILE} and is configured with
8875 @option{--enable-year2038}, libraries it links to with an @code{off_t}-
8876 or @code{time_t}-dependent interface should be configured equivalently.
8877 Alternatively, you can modify libraries to support both 32- and 64-bit
8878 interfaces though this is more work and few libraries other than the C
8879 library itself are modified in this way.
8881 Applications and libraries should be configured compatibly.
8882 If @code{off_t}, @code{time_t} or related types appear in a library's
8883 public interface, enabling or disabling the library's large-file or
8884 year-2038 support may break binary compatibility with applications or
8885 with other libraries. Similarly, if an application links to a such a
8886 library, enabling or disabling the application's large-file support may
8887 break binary compatibility with that library.
8890 @anchor{AC_SYS_LONG_FILE_NAMES}
8891 @defmac AC_SYS_LONG_FILE_NAMES
8892 @acindex{SYS_LONG_FILE_NAMES}
8893 @cvindex HAVE_LONG_FILE_NAMES
8894 If the system supports file names longer than 14 characters, define
8895 @code{HAVE_LONG_FILE_NAMES}.
8898 @defmac AC_SYS_POSIX_TERMIOS
8899 @acindex{SYS_POSIX_TERMIOS}
8900 @cindex Posix termios headers
8901 @cindex termios Posix headers
8902 @caindex sys_posix_termios
8903 Check to see if the Posix termios headers and functions are available on the
8904 system. If so, set the shell variable @code{ac_cv_sys_posix_termios} to
8905 @samp{yes}. If not, set the variable to @samp{no}.
8908 @anchor{AC_SYS_YEAR2038}
8909 @defmac AC_SYS_YEAR2038
8910 @acindex{SYS_YEAR2038}
8912 This is like @code{AC_SYS_LARGEFILE} except it defaults to enabling
8913 instead of disabling year-2038 support. Year-2038 support for
8914 applications and libraries should be configured compatibly.
8915 @xref{AC_SYS_LARGEFILE}.
8918 @defmac AC_SYS_YEAR2038_RECOMMENDED
8919 @acindex{SYS_YEAR2038_RECOMMENDED}
8920 This macro has the same effect as @code{AC_SYS_YEAR2038},
8921 but also declares that the program being configured
8922 should support timestamps after mid-January 2038.
8923 If a large @code{time_t} is unavailable, @command{configure} will error
8924 out unless the @option{--disable-year2038} option is specified.
8926 Year-2038 support for applications and libraries should be configured
8927 compatibly. @xref{AC_SYS_YEAR2038}.
8930 @node C and Posix Variants
8931 @section C and Posix Variants
8933 The following macro makes it possible to use C language and library
8934 extensions defined by the C standards committee, features of Posix that
8935 are extensions to C, and platform extensions not defined by Posix.
8937 @anchor{AC_USE_SYSTEM_EXTENSIONS}
8938 @defmac AC_USE_SYSTEM_EXTENSIONS
8939 @acindex{USE_SYSTEM_EXTENSIONS}
8940 If possible, enable extensions to C or Posix on hosts that normally
8941 disable the extensions, typically due to standards-conformance namespace
8942 issues. This should be called before any macros that run the C
8943 compiler. Also, when using this macro in concert with
8944 @code{AC_CONFIG_HEADERS}, be sure that @file{config.h} is included
8945 before any system header.
8947 The following preprocessor macros are defined unconditionally:
8951 @cvindex _ALL_SOURCE
8952 Enable extensions on AIX 3 and Interix.
8953 @item _DARWIN_C_SOURCE
8954 @cvindex _DARWIN_C_SOURCE
8955 Enable extensions on macOS.
8957 @cvindex _GNU_SOURCE
8958 Enable extensions on GNU systems.
8959 @item _NETBSD_SOURCE
8960 @cvindex _NETBSD_SOURCE
8961 Enable general extensions on NetBSD.
8962 Enable NetBSD compatibility extensions on Minix.
8963 @item _OPENBSD_SOURCE
8964 @cvindex _OPENBSD_SOURCE
8965 Enable OpenBSD compatibility extensions on NetBSD.
8966 Oddly enough, this does nothing on OpenBSD.
8967 @item _POSIX_PTHREAD_SEMANTICS
8968 @cvindex _POSIX_PTHREAD_SEMANTICS
8969 Enable Posix-compatible threading on Solaris.
8970 @item __STDC_WANT_IEC_60559_ATTRIBS_EXT__
8971 @cvindex __STDC_WANT_IEC_60559_ATTRIBS_EXT__
8972 Enable extensions specified by ISO/IEC TS 18661-5:2014.
8973 @item __STDC_WANT_IEC_60559_BFP_EXT__
8974 @cvindex __STDC_WANT_IEC_60559_BFP_EXT__
8975 Enable extensions specified by ISO/IEC TS 18661-1:2014.
8976 @item __STDC_WANT_IEC_60559_DFP_EXT__
8977 @cvindex __STDC_WANT_IEC_60559_DFP_EXT__
8978 Enable extensions specified by ISO/IEC TS 18661-2:2015.
8979 @item __STDC_WANT_IEC_60559_EXT__
8980 @cvindex __STDC_WANT_IEC_60559_EXT__
8981 Enable extensions specified by C23 Annex F.
8982 @item __STDC_WANT_IEC_60559_FUNCS_EXT__
8983 @cvindex __STDC_WANT_IEC_60559_FUNCS_EXT__
8984 Enable extensions specified by ISO/IEC TS 18661-4:2015.
8985 @item __STDC_WANT_IEC_60559_TYPES_EXT__
8986 @cvindex __STDC_WANT_IEC_60559_TYPES_EXT__
8987 Enable extensions specified by C23 Annex H and by ISO/IEC TS 18661-3:2015.
8988 @item __STDC_WANT_LIB_EXT2__
8989 @cvindex __STDC_WANT_LIB_EXT2__
8990 Enable extensions specified by ISO/IEC TR 24731-2:2010.
8991 @item __STDC_WANT_MATH_SPEC_FUNCS__
8992 @cvindex __STDC_WANT_MATH_SPEC_FUNCS__
8993 Enable extensions specified by ISO/IEC 24747:2009.
8994 @item _TANDEM_SOURCE
8995 @cvindex _TANDEM_SOURCE
8996 Enable extensions on HP NonStop systems.
8999 The following preprocessor macros are defined only when necessary;
9000 they enable access to extensions on some operating systems but
9001 @emph{disable} extensions on other operating systems.
9004 @item __EXTENSIONS__
9005 @cvindex __EXTENSIONS__
9006 Enable general extensions on Solaris. This macro is defined only if
9007 the headers included by @code{AC_INCLUDES_DEFAULT}
9008 (@pxref{Default Includes}) work correctly with it defined.
9011 @itemx _POSIX_SOURCE
9012 @itemx _POSIX_1_SOURCE
9014 @cvindex _POSIX_SOURCE
9015 @cvindex _POSIX_1_SOURCE
9016 Defined only on MINIX. @code{_POSIX_SOURCE} and @code{_POSIX_1_SOURCE}
9017 are needed to enable a number of POSIX features on this OS.
9018 @code{_MINIX} does not affect the system headers' behavior;
9019 future versions of Autoconf may stop defining it.
9020 Programs that need to recognize Minix should use @code{AC_CANONICAL_HOST}.
9023 @cvindex _XOPEN_SOURCE
9024 Defined (with value 500) only if needed to make @file{wchar.h} declare
9025 @code{mbstate_t}. This is known to be necessary on some versions of HP/UX.
9028 @cvindex __STDC_WANT_DEC_FP__
9029 The C preprocessor macro @code{__STDC_WANT_DEC_FP__} is not defined.
9030 ISO/IEC TR 24732:2009 was superseded by ISO/IEC TS 18661-2:2015.
9032 @cvindex __STDC_WANT_LIB_EXT1__
9033 The C preprocessor macro @code{__STDC_WANT_LIB_EXT1__} is not defined,
9034 as the C standard's Annex K is problematic. See: O'Donell C, Sebor M.
9035 @uref{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm, Field
9036 Experience With Annex K---Bounds Checking Interfaces}.
9038 The Autoconf macro @code{AC_USE_SYSTEM_EXTENSIONS} was introduced in
9043 @node Erlang Libraries
9044 @section Erlang Libraries
9045 @cindex Erlang, Library, checking
9047 The following macros check for an installation of Erlang/OTP, and for the
9048 presence of certain Erlang libraries. All those macros require the
9049 configuration of an Erlang interpreter and an Erlang compiler
9050 (@pxref{Erlang Compiler and Interpreter}).
9052 @defmac AC_ERLANG_SUBST_ERTS_VER
9053 @acindex{ERLANG_SUBST_ERTS_VER}
9054 @ovindex ERLANG_ERTS_VER
9055 Set the output variable @code{ERLANG_ERTS_VER} to the version of the
9056 Erlang runtime system (as returned by Erlang's
9057 @code{erlang:system_info(version)} function). The result of this test
9058 is cached if caching is enabled when running @command{configure}. The
9059 @code{ERLANG_ERTS_VER} variable is not intended to be used for testing
9060 for features of specific ERTS versions, but to be used for substituting
9061 the ERTS version in Erlang/OTP release resource files (@code{.rel}
9062 files), as shown below.
9065 @defmac AC_ERLANG_SUBST_ROOT_DIR
9066 @acindex{ERLANG_SUBST_ROOT_DIR}
9067 @ovindex ERLANG_ROOT_DIR
9068 Set the output variable @code{ERLANG_ROOT_DIR} to the path to the base
9069 directory in which Erlang/OTP is installed (as returned by Erlang's
9070 @code{code:root_dir/0} function). The result of this test is cached if
9071 caching is enabled when running @command{configure}.
9074 @defmac AC_ERLANG_SUBST_LIB_DIR
9075 @acindex{ERLANG_SUBST_LIB_DIR}
9076 @ovindex ERLANG_LIB_DIR
9077 Set the output variable @code{ERLANG_LIB_DIR} to the path of the library
9078 directory of Erlang/OTP (as returned by Erlang's
9079 @code{code:lib_dir/0} function), which subdirectories each contain an installed
9080 Erlang/OTP library. The result of this test is cached if caching is enabled
9081 when running @command{configure}.
9084 @defmac AC_ERLANG_CHECK_LIB (@var{library}, @ovar{action-if-found}, @
9085 @ovar{action-if-not-found})
9086 @acindex{ERLANG_CHECK_LIB}
9087 @ovindex ERLANG_LIB_DIR_@var{library}
9088 @ovindex ERLANG_LIB_VER_@var{library}
9089 Test whether the Erlang/OTP library @var{library} is installed by
9090 calling Erlang's @code{code:lib_dir/1} function. The result of this
9091 test is cached if caching is enabled when running @command{configure}.
9092 @var{action-if-found} is a list of shell commands to run if the library
9093 is installed; @var{action-if-not-found} is a list of shell commands to
9094 run if it is not. Additionally, if the library is installed, the output
9095 variable @samp{ERLANG_LIB_DIR_@var{library}} is set to the path to the
9096 library installation directory, and the output variable
9097 @samp{ERLANG_LIB_VER_@var{library}} is set to the version number that is
9098 part of the subdirectory name, if it is in the standard form
9099 (@code{@var{library}-@var{version}}). If the directory name does not
9100 have a version part, @samp{ERLANG_LIB_VER_@var{library}} is set to the
9101 empty string. If the library is not installed,
9102 @samp{ERLANG_LIB_DIR_@var{library}} and
9103 @samp{ERLANG_LIB_VER_@var{library}} are set to @code{"not found"}. For
9104 example, to check if library @code{stdlib} is installed:
9107 AC_ERLANG_CHECK_LIB([stdlib],
9108 [echo "stdlib version \"$ERLANG_LIB_VER_stdlib\""
9109 echo "is installed in \"$ERLANG_LIB_DIR_stdlib\""],
9110 [AC_MSG_ERROR([stdlib was not found!])])
9113 The @samp{ERLANG_LIB_VER_@var{library}} variables (set by
9114 @code{AC_ERLANG_CHECK_LIB}) and the @code{ERLANG_ERTS_VER} variable (set
9115 by @code{AC_ERLANG_SUBST_ERTS_VER}) are not intended to be used for
9116 testing for features of specific versions of libraries or of the Erlang
9117 runtime system. Those variables are intended to be substituted in
9118 Erlang release resource files (@code{.rel} files). For instance, to
9119 generate a @file{example.rel} file for an application depending on the
9120 @code{stdlib} library, @file{configure.ac} could contain:
9123 AC_ERLANG_SUBST_ERTS_VER
9124 AC_ERLANG_CHECK_LIB([stdlib],
9126 [AC_MSG_ERROR([stdlib was not found!])])
9127 AC_CONFIG_FILES([example.rel])
9131 The @file{example.rel.in} file used to generate @file{example.rel}
9136 @{"@@PACKAGE@@", "@@VERSION@@"@},
9137 @{erts, "@@ERLANG_ERTS_VER@@"@},
9138 [@{stdlib, "@@ERLANG_LIB_VER_stdlib@@"@},
9139 @{@@PACKAGE@@, "@@VERSION@@"@}]@}.
9143 In addition to the above macros, which test installed Erlang libraries, the
9144 following macros determine the paths to the directories into which newly built
9145 Erlang libraries are to be installed:
9147 @defmac AC_ERLANG_SUBST_INSTALL_LIB_DIR
9148 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
9149 @ovindex ERLANG_INSTALL_LIB_DIR
9151 Set the @code{ERLANG_INSTALL_LIB_DIR} output variable to the directory into
9152 which every built Erlang library should be installed in a separate
9154 If this variable is not set in the environment when @command{configure} runs,
9155 its default value is @code{$@{libdir@}/erlang/lib}.
9158 @defmac AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR (@var{library}, @var{version})
9159 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
9160 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
9162 Set the @samp{ERLANG_INSTALL_LIB_DIR_@var{library}} output variable to the
9163 directory into which the built Erlang library @var{library} version
9164 @var{version} should be installed. If this variable is not set in the
9165 environment when @command{configure} runs, its default value is
9166 @samp{$ERLANG_INSTALL_LIB_DIR/@var{library}-@var{version}}, the value of the
9167 @code{ERLANG_INSTALL_LIB_DIR} variable being set by the
9168 @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR} macro.
9175 @c ========================================================= Writing Tests
9178 @chapter Writing Tests
9180 If the existing feature tests don't do something you need, you have to
9181 write new ones. These macros are the building blocks. They provide
9182 ways for other macros to check whether various kinds of features are
9183 available and report the results.
9185 This chapter contains some suggestions and some of the reasons why the
9186 existing tests are written the way they are. You can also learn a lot
9187 about how to write Autoconf tests by looking at the existing ones. If
9188 something goes wrong in one or more of the Autoconf tests, this
9189 information can help you understand the assumptions behind them, which
9190 might help you figure out how to best solve the problem.
9192 These macros check the output of the compiler system of the current
9193 language (@pxref{Language Choice}). They do not cache the results of
9194 their tests for future use (@pxref{Caching Results}), because they don't
9195 know enough about the information they are checking for to generate a
9196 cache variable name. They also do not print any messages, for the same
9197 reason. The checks for particular kinds of features call these macros
9198 and do cache their results and print messages about what they're
9201 When you write a feature test that could be applicable to more than one
9202 software package, the best thing to do is encapsulate it in a new macro.
9203 @xref{Writing Autoconf Macros}, for how to do that.
9206 * Language Choice:: Selecting which language to use for testing
9207 * Writing Test Programs:: Forging source files for compilers
9208 * Running the Preprocessor:: Detecting preprocessor symbols
9209 * Running the Compiler:: Detecting language or header features
9210 * Running the Linker:: Detecting library features
9211 * Runtime:: Testing for runtime features
9212 * Systemology:: A zoology of operating systems
9213 * Multiple Cases:: Tests for several possible values
9216 @node Language Choice
9217 @section Language Choice
9220 Autoconf-generated @command{configure} scripts check for the C compiler and
9221 its features by default. Packages that use other programming languages
9222 (maybe more than one, e.g., C and C++) need to test features of the
9223 compilers for the respective languages. The following macros determine
9224 which programming language is used in the subsequent tests in
9225 @file{configure.ac}.
9228 @defmac AC_LANG (@var{language})
9230 Do compilation tests using the compiler, preprocessor, and file
9231 extensions for the specified @var{language}.
9233 Supported languages are:
9237 Do compilation tests using @code{CC} and @code{CPP} and use extension
9238 @file{.c} for test programs. Use compilation flags: @code{CPPFLAGS} with
9239 @code{CPP}, and both @code{CPPFLAGS} and @code{CFLAGS} with @code{CC}.
9242 Do compilation tests using @code{CXX} and @code{CXXCPP} and use
9243 extension @file{.C} for test programs. Use compilation flags:
9244 @code{CPPFLAGS} with @code{CXXCPP}, and both @code{CPPFLAGS} and
9245 @code{CXXFLAGS} with @code{CXX}.
9248 Do compilation tests using @code{F77} and use extension @file{.f} for
9249 test programs. Use compilation flags: @code{FFLAGS}.
9252 Do compilation tests using @code{FC} and use extension @file{.f} (or
9253 whatever has been set by @code{AC_FC_SRCEXT}) for test programs. Use
9254 compilation flags: @code{FCFLAGS}.
9260 Compile and execute tests using @code{ERLC} and @code{ERL} and use extension
9261 @file{.erl} for test Erlang modules. Use compilation flags: @code{ERLCFLAGS}.
9264 Do compilation tests using @code{OBJC} and @code{OBJCPP} and use
9265 extension @file{.m} for test programs. Use compilation flags:
9266 @code{CPPFLAGS} with @code{OBJCPP}, and both @code{CPPFLAGS} and
9267 @code{OBJCFLAGS} with @code{OBJC}.
9270 Do compilation tests using @code{OBJCXX} and @code{OBJCXXCPP} and use
9271 extension @file{.mm} for test programs. Use compilation flags:
9272 @code{CPPFLAGS} with @code{OBJCXXCPP}, and both @code{CPPFLAGS} and
9273 @code{OBJCXXFLAGS} with @code{OBJCXX}.
9276 Do compilation tests using @code{GOC} and use extension @file{.go} for
9277 test programs. Use compilation flags @code{GOFLAGS}.
9281 @anchor{AC_LANG_PUSH}
9282 @defmac AC_LANG_PUSH (@var{language})
9284 Remember the current language (as set by @code{AC_LANG}) on a stack, and
9285 then select the @var{language}. Use this macro and @code{AC_LANG_POP}
9286 in macros that need to temporarily switch to a particular language.
9289 @defmac AC_LANG_POP (@ovar{language})
9291 Select the language that is saved on the top of the stack, as set by
9292 @code{AC_LANG_PUSH}, and remove it from the stack.
9294 If given, @var{language} specifies the language we just @emph{quit}. It
9295 is a good idea to specify it when it's known (which should be the
9296 case@dots{}), since Autoconf detects inconsistencies.
9299 AC_LANG_PUSH([Fortran 77])
9300 # Perform some tests on Fortran 77.
9302 AC_LANG_POP([Fortran 77])
9306 @defmac AC_LANG_ASSERT (@var{language})
9307 @acindex{LANG_ASSERT}
9308 Check statically that the current language is @var{language}.
9309 You should use this in your language specific macros
9310 to avoid that they be called with an inappropriate language.
9312 This macro runs only at @command{autoconf} time, and incurs no cost at
9313 @command{configure} time. Sadly enough and because Autoconf is a two
9314 layer language @footnote{Because M4 is not aware of Sh code,
9315 especially conditionals, some optimizations that look nice statically
9316 may produce incorrect results at runtime.}, the macros
9317 @code{AC_LANG_PUSH} and @code{AC_LANG_POP} cannot be ``optimizing'',
9318 therefore as much as possible you ought to avoid using them to wrap
9319 your code, rather, require from the user to run the macro with a
9320 correct current language, and check it with @code{AC_LANG_ASSERT}.
9321 And anyway, that may help the user understand she is running a Fortran
9322 macro while expecting a result about her Fortran 77 compiler@enddots{}
9326 @defmac AC_REQUIRE_CPP
9327 @acindex{REQUIRE_CPP}
9328 Ensure that whichever preprocessor would currently be used for tests has
9329 been found. Calls @code{AC_REQUIRE} (@pxref{Prerequisite Macros}) with an
9330 argument of either @code{AC_PROG_CPP} or @code{AC_PROG_CXXCPP},
9331 depending on which language is current.
9335 @node Writing Test Programs
9336 @section Writing Test Programs
9338 Autoconf tests follow a common scheme: feed some program with some
9339 input, and most of the time, feed a compiler with some source file.
9340 This section is dedicated to these source samples.
9343 * Guidelines:: General rules for writing test programs
9344 * Test Functions:: Avoiding pitfalls in test programs
9345 * Generating Sources:: Source program boilerplate
9349 @subsection Guidelines for Test Programs
9351 The most important rule to follow when writing testing samples is:
9353 @center @emph{Look for realism.}
9355 This motto means that testing samples must be written with the same
9356 strictness as real programs are written. In particular, you should
9357 avoid ``shortcuts'' and simplifications.
9359 Don't just play with the preprocessor if you want to prepare a
9360 compilation. For instance, using @command{cpp} to check whether a header is
9361 functional might let your @command{configure} accept a header which
9362 causes some @emph{compiler} error. Do not hesitate to check a header with
9363 other headers included before, especially required headers.
9365 Make sure the symbols you use are properly defined, i.e., refrain from
9366 simply declaring a function yourself instead of including the proper
9369 Test programs should not write to standard output. They
9370 should exit with status 0 if the test succeeds, and with status 1
9371 otherwise, so that success
9372 can be distinguished easily from a core dump or other failure;
9373 segmentation violations and other failures produce a nonzero exit
9374 status. Unless you arrange for @code{exit} to be declared, test
9375 programs should @code{return}, not @code{exit}, from @code{main},
9376 because on many systems @code{exit} is not declared by default.
9378 Test programs can use @code{#if} or @code{#ifdef} to check the values of
9379 preprocessor macros defined by tests that have already run. For
9380 example, if you call @code{AC_HEADER_STDBOOL}, then later on in
9381 @file{configure.ac} you can have a test program that includes
9382 @file{stdbool.h} conditionally:
9386 #ifdef HAVE_STDBOOL_H
9387 # include <stdbool.h>
9392 Both @code{#if HAVE_STDBOOL_H} and @code{#ifdef HAVE_STDBOOL_H} will
9393 work with any standard C compiler. Some developers prefer @code{#if}
9394 because it is easier to read, while others prefer @code{#ifdef} because
9395 it avoids diagnostics with picky compilers like GCC with the
9396 @option{-Wundef} option.
9398 If a test program needs to use or create a data file, give it a name
9399 that starts with @file{conftest}, such as @file{conftest.data}. The
9400 @command{configure} script cleans up by running @samp{rm -f -r conftest*}
9401 after running test programs and if the script is interrupted.
9403 @node Test Functions
9404 @subsection Test Functions
9406 Functions in test code should use function prototypes, introduced in C89
9407 and required in C23.
9409 Functions that test programs declare should also be conditionalized for
9410 C++, which requires @samp{extern "C"} prototypes. Make sure to not
9411 include any header files containing clashing prototypes.
9417 void *valloc (size_t);
9420 If a test program calls a function with invalid parameters (just to see
9421 whether it exists), organize the program to ensure that it never invokes
9422 that function. You can do this by calling it in another function that is
9423 never invoked. You can't do it by putting it after a call to
9424 @code{exit}, because GCC version 2 knows that @code{exit}
9426 and optimizes out any code that follows it in the same block.
9428 If you include any header files, be sure to call the functions
9429 relevant to them with the correct number of arguments, even if they are
9430 just 0, to avoid compilation errors due to prototypes. GCC
9432 has internal prototypes for several functions that it automatically
9433 inlines; for example, @code{memcpy}. To avoid errors when checking for
9434 them, either pass them the correct number of arguments or redeclare them
9435 with a different return type (such as @code{char}).
9438 @node Generating Sources
9439 @subsection Generating Sources
9441 Autoconf provides a set of macros that can be used to generate test
9442 source files. They are written to be language generic, i.e., they
9443 actually depend on the current language (@pxref{Language Choice}) to
9444 ``format'' the output properly.
9447 @defmac AC_LANG_CONFTEST (@var{source})
9448 @acindex{LANG_CONFTEST}
9449 Save the @var{source} text in the current test source file:
9450 @file{conftest.@var{extension}} where the @var{extension} depends on the
9451 current language. As of Autoconf 2.63b, the source file also contains
9452 the results of all of the @code{AC_DEFINE} performed so far.
9454 Note that the @var{source} is evaluated exactly once, like regular
9455 Autoconf macro arguments, and therefore (i) you may pass a macro
9456 invocation, (ii) if not, be sure to double quote if needed.
9458 The @var{source} text is expanded as an unquoted here-document, so
9459 @samp{$}, @samp{`} and some @samp{\}s should be backslash-escaped.
9460 @xref{Here-Documents}.
9462 This macro issues a warning during @command{autoconf} processing if
9463 @var{source} does not include an expansion of the macro
9464 @code{AC_LANG_DEFINES_PROVIDED} (note that both @code{AC_LANG_SOURCE} and
9465 @code{AC_LANG_PROGRAM} call this macro, and thus avoid the warning).
9467 This macro is seldom called directly, but is used under the hood by more
9468 common macros such as @code{AC_COMPILE_IFELSE} and @code{AC_RUN_IFELSE}.
9471 @defmac AC_LANG_DEFINES_PROVIDED
9472 @acindex{LANG_DEFINES_PROVIDED}
9473 This macro is called as a witness that the file
9474 @file{conftest.@var{extension}} appropriate for the current language is
9475 complete, including all previously determined results from
9476 @code{AC_DEFINE}. This macro is seldom called directly, but exists if
9477 you have a compelling reason to write a conftest file without using
9478 @code{AC_LANG_SOURCE}, yet still want to avoid a syntax warning from
9479 @code{AC_LANG_CONFTEST}.
9482 @defmac AC_LANG_SOURCE (@var{source})
9483 @acindex{LANG_SOURCE}
9484 Expands into the @var{source}, with the definition of
9485 all the @code{AC_DEFINE} performed so far. This macro includes an
9486 expansion of @code{AC_LANG_DEFINES_PROVIDED}.
9488 In many cases, you may find it more convenient to use the wrapper
9489 @code{AC_LANG_PROGRAM}.
9492 For instance, executing (observe the double quotation!):
9495 @c If you change this example, adjust tests/compile.at:AC_LANG_SOURCE example.
9496 AC_INIT([Hello], [1.0], [bug-hello@@example.org], [],
9497 [https://www.example.org/])
9498 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
9499 [Greetings string.])
9502 [AC_LANG_SOURCE([[const char hw[] = "Hello, World\n";]])])
9503 gcc -E -dD conftest.c
9507 on a system with @command{gcc} installed, results in:
9510 @c If you change this example, adjust tests/compile.at:AC_LANG_SOURCE example.
9512 @asis{#} 1 "conftest.c"
9514 #define PACKAGE_NAME "Hello"
9515 #define PACKAGE_TARNAME "hello"
9516 #define PACKAGE_VERSION "1.0"
9517 #define PACKAGE_STRING "Hello 1.0"
9518 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
9519 #define PACKAGE_URL "https://www.example.org/"
9520 #define HELLO_WORLD "Hello, World\n"
9522 const char hw[] = "Hello, World\n";
9525 When the test language is Fortran, Erlang, or Go, the @code{AC_DEFINE}
9526 definitions are not automatically translated into constants in the
9527 source code by this macro.
9529 @defmac AC_LANG_PROGRAM (@var{prologue}, @var{body})
9530 @acindex{LANG_PROGRAM}
9531 Expands into a source file which consists of the @var{prologue}, and
9532 then @var{body} as body of the main function (e.g., @code{main} in
9533 C). Since it uses @code{AC_LANG_SOURCE}, the features of the latter are
9540 @c If you change this example, adjust tests/compile.at:AC_LANG_PROGRAM example.
9541 AC_INIT([Hello], [1.0], [bug-hello@@example.org], [],
9542 [https://www.example.org/])
9543 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
9544 [Greetings string.])
9546 [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
9547 [[fputs (hw, stdout);]])])
9548 gcc -E -dD conftest.c
9552 on a system with @command{gcc} installed, results in:
9555 @c If you change this example, adjust tests/compile.at:AC_LANG_PROGRAM example.
9557 @asis{#} 1 "conftest.c"
9559 #define PACKAGE_NAME "Hello"
9560 #define PACKAGE_TARNAME "hello"
9561 #define PACKAGE_VERSION "1.0"
9562 #define PACKAGE_STRING "Hello 1.0"
9563 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
9564 #define PACKAGE_URL "https://www.example.org/"
9565 #define HELLO_WORLD "Hello, World\n"
9567 const char hw[] = "Hello, World\n";
9577 In Erlang tests, the created source file is that of an Erlang module called
9578 @code{conftest} (@file{conftest.erl}). This module defines and exports
9580 one @code{start/0} function, which is called to perform the test. The
9581 @var{prologue} is optional code that is inserted between the module header and
9582 the @code{start/0} function definition. @var{body} is the body of the
9583 @code{start/0} function without the final period (@pxref{Runtime}, about
9584 constraints on this function's behavior).
9589 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
9592 [AC_LANG_PROGRAM([[-define(HELLO_WORLD, "Hello, world!").]],
9593 [[io:format("~s~n", [?HELLO_WORLD])]])])
9603 -define(HELLO_WORLD, "Hello, world!").
9605 io:format("~s~n", [?HELLO_WORLD])
9609 @defmac AC_LANG_CALL (@var{prologue}, @var{function})
9611 Expands into a source file which consists of the @var{prologue}, and
9612 then a call to the @var{function} as body of the main function (e.g.,
9613 @code{main} in C). Since it uses @code{AC_LANG_PROGRAM}, the feature
9614 of the latter are available.
9616 This function will probably be replaced in the future by a version
9617 which would enable specifying the arguments. The use of this macro is
9618 not encouraged, as it violates strongly the typing system.
9620 This macro cannot be used for Erlang tests.
9623 @defmac AC_LANG_FUNC_LINK_TRY (@var{function})
9624 @acindex{LANG_FUNC_LINK_TRY}
9625 Expands into a source file which uses the @var{function} in the body of
9626 the main function (e.g., @code{main} in C). Since it uses
9627 @code{AC_LANG_PROGRAM}, the features of the latter are available.
9629 As @code{AC_LANG_CALL}, this macro is documented only for completeness.
9630 It is considered to be severely broken, and in the future will be
9631 removed in favor of actual function calls (with properly typed
9634 This macro cannot be used for Erlang tests.
9637 @node Running the Preprocessor
9638 @section Running the Preprocessor
9640 Sometimes one might need to run the preprocessor on some source file.
9641 @emph{Usually it is a bad idea}, as you typically need to @emph{compile}
9642 your project, not merely run the preprocessor on it; therefore you
9643 certainly want to run the compiler, not the preprocessor. Resist the
9644 temptation of following the easiest path.
9646 Nevertheless, if you need to run the preprocessor, then use
9647 @code{AC_PREPROC_IFELSE}.
9649 The macros described in this section cannot be used for tests in Erlang,
9650 Fortran, or Go, since those languages require no preprocessor.
9652 @anchor{AC_PREPROC_IFELSE}
9653 @defmac AC_PREPROC_IFELSE (@var{input}, @ovar{action-if-true}, @
9654 @ovar{action-if-false})
9655 @acindex{PREPROC_IFELSE}
9656 Run the preprocessor of the current language (@pxref{Language Choice})
9657 on the @var{input}, run the shell commands @var{action-if-true} on
9658 success, @var{action-if-false} otherwise.
9660 If @var{input} is nonempty use the equivalent of
9661 @code{AC_LANG_CONFTEST(@var{input})} to generate the current test source
9662 file; otherwise reuse the already-existing test source file.
9663 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
9664 The @var{input} text is expanded as an unquoted here-document, so
9665 @samp{$}, @samp{`} and some @samp{\}s should be backslash-escaped.
9666 @xref{Here-Documents}.
9668 This macro uses @code{CPPFLAGS}, but not @code{CFLAGS}, because
9669 @option{-g}, @option{-O}, etc.@: are not valid options to many C
9672 It is customary to report unexpected failures with
9673 @code{AC_MSG_FAILURE}. If needed, @var{action-if-true} can further access
9674 the preprocessed output in the file @file{conftest.i}.
9680 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
9681 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
9682 [Greetings string.])
9684 [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
9685 [[fputs (hw, stdout);]])],
9686 [AC_MSG_RESULT([OK])],
9687 [AC_MSG_FAILURE([unexpected preprocessor failure])])
9694 checking for gcc... gcc
9695 checking whether the C compiler works... yes
9696 checking for C compiler default output file name... a.out
9697 checking for suffix of executables...
9698 checking whether we are cross compiling... no
9699 checking for suffix of object files... o
9700 checking whether the compiler supports GNU C... yes
9701 checking whether gcc accepts -g... yes
9702 checking for gcc option to enable C11 features... -std=gnu11
9703 checking how to run the C preprocessor... gcc -std=gnu11 -E
9709 The macro @code{AC_TRY_CPP} (@pxref{Obsolete Macros}) used to play the
9710 role of @code{AC_PREPROC_IFELSE}, but double quotes its argument, making
9711 it impossible to use it to elaborate sources. You are encouraged to
9712 get rid of your old use of the macro @code{AC_TRY_CPP} in favor of
9713 @code{AC_PREPROC_IFELSE}, but, in the first place, are you sure you need
9714 to run the @emph{preprocessor} and not the compiler?
9716 @anchor{AC_EGREP_HEADER}
9717 @defmac AC_EGREP_HEADER (@var{pattern}, @var{header-file}, @
9718 @var{action-if-found}, @ovar{action-if-not-found})
9719 @acindex{EGREP_HEADER}
9720 @var{pattern}, after being expanded as if in a double-quoted shell string,
9721 is an extended regular expression.
9722 If the output of running the preprocessor on the system header file
9723 @var{header-file} contains a line matching
9724 @var{pattern}, execute shell commands @var{action-if-found}, otherwise
9725 execute @var{action-if-not-found}.
9727 See below for some problems involving this macro.
9730 @anchor{AC_EGREP_CPP}
9731 @defmac AC_EGREP_CPP (@var{pattern}, @var{program}, @
9732 @ovar{action-if-found}, @ovar{action-if-not-found})
9734 @var{pattern}, after being expanded as if in a double-quoted shell string,
9735 is an extended regular expression.
9736 @var{program} is the text of a C or C++ program, which is expanded as an
9737 unquoted here-document (@pxref{Here-Documents}). If the
9738 output of running the preprocessor on @var{program} contains a line
9739 matching @var{pattern}, execute shell commands
9740 @var{action-if-found}, otherwise execute @var{action-if-not-found}.
9742 See below for some problems involving this macro.
9745 @code{AC_EGREP_CPP} and @code{AC_EGREP_HEADER} should be used with care,
9746 as preprocessors can insert line breaks between output tokens. For
9747 example, the preprocessor might transform this:
9752 Version MAJOR . MINOR
9766 Because preprocessors are allowed to insert white space, change escapes
9767 in string constants, insert backlash-newline pairs, or do any of a number
9768 of things that do not change the meaning of the preprocessed program, it
9769 is better to rely on @code{AC_PREPROC_IFELSE} than to resort to
9770 @code{AC_EGREP_CPP} or @code{AC_EGREP_HEADER}.
9772 For more information about what can appear in portable extended regular
9773 expressions, @pxref{Problematic Expressions,,,grep, GNU Grep}.
9775 @node Running the Compiler
9776 @section Running the Compiler
9778 To check for a syntax feature of the current language's (@pxref{Language
9779 Choice}) compiler, such as whether it recognizes a certain keyword, or
9780 simply to try some library feature, use @code{AC_COMPILE_IFELSE} to try
9781 to compile a small program that uses that feature.
9783 @defmac AC_COMPILE_IFELSE (@var{input}, @ovar{action-if-true}, @
9784 @ovar{action-if-false})
9785 @acindex{COMPILE_IFELSE}
9786 Run the compiler and compilation flags of the current language
9787 (@pxref{Language Choice}) on the @var{input}, run the shell commands
9788 @var{action-if-true} on success, @var{action-if-false} otherwise.
9790 If @var{input} is nonempty use the equivalent of
9791 @code{AC_LANG_CONFTEST(@var{input})} to generate the current test source
9792 file; otherwise reuse the already-existing test source file.
9793 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
9794 The @var{input} text is expanded as an unquoted here-document, so
9795 @samp{$}, @samp{`} and some @samp{\}s should be backslash-escaped.
9796 @xref{Here-Documents}.
9798 It is customary to report unexpected failures with
9799 @code{AC_MSG_FAILURE}. This macro does not try to link; use
9800 @code{AC_LINK_IFELSE} if you need to do that (@pxref{Running the
9801 Linker}). If needed, @var{action-if-true} can further access the
9802 just-compiled object file @file{conftest.$OBJEXT}.
9804 This macro uses @code{AC_REQUIRE} for the compiler associated with the
9805 current language, which means that if the compiler has not yet been
9806 determined, the compiler determination will be made prior to the body of
9807 the outermost @code{AC_DEFUN} macro that triggered this macro to
9808 expand (@pxref{Expanded Before Required}).
9812 For tests in Erlang, the @var{input} must be the source code of a module named
9813 @code{conftest}. @code{AC_COMPILE_IFELSE} generates a @file{conftest.beam}
9814 file that can be interpreted by the Erlang virtual machine (@code{ERL}). It is
9815 recommended to use @code{AC_LANG_PROGRAM} to specify the test program,
9816 to ensure that the Erlang module has the right name.
9818 @node Running the Linker
9819 @section Running the Linker
9821 To check for a library, a function, or a global variable, Autoconf
9822 @command{configure} scripts try to compile and link a small program that
9823 uses it. This is unlike Metaconfig, which by default uses @code{nm} or
9824 @code{ar} on the C library to try to figure out which functions are
9825 available. Trying to link with the function is usually a more reliable
9826 approach because it avoids dealing with the variations in the options
9827 and output formats of @code{nm} and @code{ar} and in the location of the
9828 standard libraries. It also allows configuring for cross-compilation or
9829 checking a function's runtime behavior if needed. On the other hand,
9830 it can be slower than scanning the libraries once, but accuracy is more
9831 important than speed.
9833 @code{AC_LINK_IFELSE} is used to compile test programs to test for
9834 functions and global variables. It is also used by @code{AC_CHECK_LIB}
9835 to check for libraries (@pxref{Libraries}), by adding the library being
9836 checked for to @code{LIBS} temporarily and trying to link a small
9839 @anchor{AC_LINK_IFELSE}
9840 @defmac AC_LINK_IFELSE (@var{input}, @ovar{action-if-true}, @
9841 @ovar{action-if-false})
9842 @acindex{LINK_IFELSE}
9843 Run the compiler (and compilation flags) and the linker of the current
9844 language (@pxref{Language Choice}) on the @var{input}, run the shell
9845 commands @var{action-if-true} on success, @var{action-if-false}
9846 otherwise. If needed, @var{action-if-true} can further access the
9847 just-linked program file @file{conftest$EXEEXT}.
9849 If @var{input} is nonempty use the equivalent of
9850 @code{AC_LANG_CONFTEST(@var{input})} to generate the current test source
9851 file; otherwise reuse the already-existing test source file.
9852 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
9853 The @var{input} text is expanded as an unquoted here-document, so
9854 @samp{$}, @samp{`} and some @samp{\}s should be backslash-escaped.
9855 @xref{Here-Documents}.
9857 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
9858 current compilation flags.
9860 It is customary to report unexpected failures with
9861 @code{AC_MSG_FAILURE}. This macro does not try to execute the program;
9862 use @code{AC_RUN_IFELSE} if you need to do that (@pxref{Runtime}).
9865 The @code{AC_LINK_IFELSE} macro cannot be used for Erlang tests, since Erlang
9866 programs are interpreted and do not require linking.
9871 @section Checking Runtime Behavior
9873 Sometimes you need to find out how a system performs at runtime, such
9874 as whether a given function has a certain capability or bug. If you
9875 can, make such checks when your program runs instead of when it is
9876 configured. You can check for things like the machine's endianness when
9877 your program initializes itself.
9879 If you really need to test for a runtime behavior while configuring,
9880 you can write a test program to determine the result, and compile and
9881 run it using @code{AC_RUN_IFELSE}. Avoid running test programs if
9882 possible, because this prevents people from configuring your package for
9885 @anchor{AC_RUN_IFELSE}
9886 @defmac AC_RUN_IFELSE (@var{input}, @ovar{action-if-true}, @
9887 @ovar{action-if-false}, @dvar{action-if-cross-compiling, AC_MSG_FAILURE})
9888 @acindex{RUN_IFELSE}
9889 Run the compiler (and compilation flags) and the linker of the current
9890 language (@pxref{Language Choice}) on the @var{input}, then execute the
9891 resulting program. If the program returns an exit
9892 status of 0 when executed, run shell commands @var{action-if-true}.
9893 Otherwise, run shell commands @var{action-if-false}.
9895 If @var{input} is nonempty use the equivalent of
9896 @code{AC_LANG_CONFTEST(@var{input})} to generate the current test source
9897 file; otherwise reuse the already-existing test source file.
9898 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
9899 The @var{input} text is expanded as an unquoted here-document, so
9900 @samp{$}, @samp{`} and some @samp{\}s should be backslash-escaped.
9901 @xref{Here-Documents}.
9903 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
9904 compilation flags of the current language (@pxref{Language Choice}).
9905 Additionally, @var{action-if-true} can run @command{./conftest$EXEEXT}
9906 for further testing.
9908 In the @var{action-if-false} section, the failing exit status is
9909 available in the shell variable @samp{$?}. This exit status might be
9910 that of a failed compilation, or it might be that of a failed program
9913 If cross-compilation mode is enabled (this is the case if either the
9914 compiler being used does not produce executables that run on the system
9915 where @command{configure} is being run, or if the options @code{--build}
9916 and @code{--host} were both specified and their values are different),
9917 then the test program is
9918 not run. If the optional shell commands @var{action-if-cross-compiling}
9919 are given, those commands are run instead; typically these commands
9920 provide pessimistic defaults that allow cross-compilation to work even
9921 if the guess was wrong. If the fourth argument is empty or omitted, but
9922 cross-compilation is detected, then @command{configure} prints an error
9923 message and exits. If you want your package to be useful in a
9924 cross-compilation scenario, you @emph{should} provide a non-empty
9925 @var{action-if-cross-compiling} clause, as well as wrap the
9926 @code{AC_RUN_IFELSE} compilation inside an @code{AC_CACHE_CHECK}
9927 (@pxref{Caching Results}) which allows the user to override the
9928 pessimistic default if needed.
9930 It is customary to report unexpected failures with
9931 @code{AC_MSG_FAILURE}.
9934 @command{autoconf} prints a warning message when creating
9935 @command{configure} each time it encounters a call to
9936 @code{AC_RUN_IFELSE} with no @var{action-if-cross-compiling} argument
9937 given. If you are not concerned about users configuring your package
9938 for cross-compilation, you may ignore the warning. A few of the macros
9939 distributed with Autoconf produce this warning message; but if this is a
9940 problem for you, please report it as a bug, along with an appropriate
9941 pessimistic guess to use instead.
9943 To configure for cross-compiling you can also choose a value for those
9944 parameters based on the canonical system name (@pxref{Manual
9945 Configuration}). Alternatively, set up a test results cache file with
9946 the correct values for the host system (@pxref{Caching Results}).
9948 @ovindex cross_compiling
9949 To provide a default for calls of @code{AC_RUN_IFELSE} that are embedded
9950 in other macros, including a few of the ones that come with Autoconf,
9951 you can test whether the shell variable @code{cross_compiling} is set to
9952 @samp{yes}, and then use an alternate method to get the results instead
9953 of calling the macros.
9955 It is also permissible to temporarily assign to @code{cross_compiling}
9956 in order to force tests to behave as though they are in a
9957 cross-compilation environment, particularly since this provides a way to
9958 test your @var{action-if-cross-compiling} even when you are not using a
9962 # We temporarily set cross-compile mode to force AC_COMPUTE_INT
9963 # to use the slow link-only method
9964 save_cross_compiling=$cross_compiling
9966 AC_COMPUTE_INT([@dots{}])
9967 cross_compiling=$save_cross_compiling
9970 A C or C++ runtime test should be portable.
9971 @xref{Portable C and C++}.
9973 Erlang tests must exit themselves the Erlang VM by calling the @code{halt/1}
9974 function: the given status code is used to determine the success of the test
9975 (status is @code{0}) or its failure (status is different than @code{0}), as
9976 explained above. It must be noted that data output through the standard output
9977 (e.g., using @code{io:format/2}) may be truncated when halting the VM.
9978 Therefore, if a test must output configuration information, it is recommended
9979 to create and to output data into the temporary file named @file{conftest.out},
9980 using the functions of module @code{file}. The @code{conftest.out} file is
9981 automatically deleted by the @code{AC_RUN_IFELSE} macro. For instance, a
9982 simplified implementation of Autoconf's @code{AC_ERLANG_SUBST_LIB_DIR}
9986 AC_INIT([LibdirTest], [1.0], [bug-libdirtest@@example.org])
9990 [AC_LANG_PROGRAM([], [dnl
9991 file:write_file("conftest.out", code:lib_dir()),
9993 [echo "code:lib_dir() returned: `cat conftest.out`"],
9994 [AC_MSG_FAILURE([test Erlang program execution failed])])
9999 @section Systemology
10000 @cindex Systemology
10002 This section aims at presenting some systems and pointers to
10003 documentation. It may help you addressing particular problems reported
10006 @uref{https://@/en.wikipedia.org/@/wiki/@/POSIX, Posix-conforming
10007 systems} are derived from the
10008 @uref{https://@/en.wikipedia.org/@/wiki/@/Unix, Unix operating system}.
10010 The @uref{http://@/bhami.com/@/rosetta.html, Rosetta Stone for Unix}
10011 contains a table correlating the features of various Posix-conforming
10012 systems. @uref{https://@/www.levenez.com/@/unix/, Unix History} is a
10013 simplified diagram of how many Unix systems were derived from each
10016 @uref{http://@/heirloom.sourceforge.net/, The Heirloom Project}
10017 provides some variants of traditional implementations of Unix utilities.
10022 Darwin is also known as Mac OS X@. Beware that the file system @emph{can} be
10023 case-preserving, but case insensitive. This can cause nasty problems,
10024 since for instance the installation attempt for a package having an
10025 @file{INSTALL} file can result in @samp{make install} report that
10026 nothing was to be done!
10028 That's all dependent on whether the file system is a UFS (case
10029 sensitive) or HFS+ (case preserving). By default Apple wants you to
10030 install the OS on HFS+. Unfortunately, there are some pieces of
10031 software which really need to be built on UFS@. We may want to rebuild
10032 Darwin to have both UFS and HFS+ available (and put the /local/build
10037 @c FIXME: Please, if you feel like writing something more precise,
10038 @c it'd be great. In particular, I can't understand the difference with
10040 QNX is a realtime operating system running on Intel architecture
10041 meant to be scalable from the small embedded systems to the hundred
10042 processor super-computer. It claims to be Posix certified. More
10043 information is available on the
10044 @uref{https://@/blackberry.qnx.com/@/en, QNX home page}.
10046 @item Unix version 7
10047 @cindex Unix version 7
10049 Officially this was called the ``Seventh Edition'' of ``the UNIX
10050 time-sharing system'' but we use the more-common name ``Unix version 7''.
10051 Documentation is available in the
10052 @uref{https://@/s3.amazonaws.com/@/plan9-bell-labs/@/7thEdMan/@/index.html,
10053 Unix Seventh Edition Manual}.
10054 Previous versions of Unix are called ``Unix version 6'', etc., but
10055 they were not as widely used.
10059 @node Multiple Cases
10060 @section Multiple Cases
10062 Some operations are accomplished in several possible ways, depending on
10063 the OS variant. Checking for them essentially requires a ``case
10064 statement''. Autoconf does not directly provide one; however, it is
10065 easy to simulate by using a shell variable to keep track of whether a
10066 way to perform the operation has been found yet.
10068 Here is an example that uses the shell variable @code{fstype} to keep
10069 track of whether the remaining cases need to be checked. Note that
10070 since the value of @code{fstype} is under our control, we don't have to
10071 use the longer @samp{test "x$fstype" = xno}.
10075 AC_MSG_CHECKING([how to get file system type])
10077 # The order of these tests is important.
10078 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statvfs.h>
10079 #include <sys/fstyp.h>
10081 [AC_DEFINE([FSTYPE_STATVFS], [1],
10082 [Define if statvfs exists.])
10084 AS_IF([test $fstype = no],
10085 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
10086 #include <sys/fstyp.h>
10088 [AC_DEFINE([FSTYPE_USG_STATFS], [1],
10089 [Define if USG statfs.])
10091 AS_IF([test $fstype = no],
10092 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
10093 #include <sys/vmount.h>
10095 [AC_DEFINE([FSTYPE_AIX_STATFS], [1],
10096 [Define if AIX statfs.])
10098 # (more cases omitted here)
10099 AC_MSG_RESULT([$fstype])
10103 @c ====================================================== Results of Tests.
10106 @chapter Results of Tests
10108 Once @command{configure} has determined whether a feature exists, what can
10109 it do to record that information? There are four sorts of things it can
10110 do: define a C preprocessor symbol, set a variable in the output files,
10111 save the result in a cache file for future @command{configure} runs, and
10112 print a message letting the user know the result of the test.
10115 * Defining Symbols:: Defining C preprocessor symbols
10116 * Setting Output Variables:: Replacing variables in output files
10117 * Special Chars in Variables:: Characters to beware of in variables
10118 * Caching Results:: Speeding up subsequent @command{configure} runs
10119 * Printing Messages:: Notifying @command{configure} users
10122 @node Defining Symbols
10123 @section Defining C Preprocessor Symbols
10125 A common action to take in response to a feature test is to define a C
10126 preprocessor symbol indicating the results of the test. That is done by
10127 calling @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}.
10129 By default, @code{AC_OUTPUT} places the symbols defined by these macros
10130 into the output variable @code{DEFS}, which contains an option
10131 @option{-D@var{symbol}=@var{value}} for each symbol defined. Unlike in
10132 Autoconf version 1, there is no variable @code{DEFS} defined while
10133 @command{configure} is running. To check whether Autoconf macros have
10134 already defined a certain C preprocessor symbol, test the value of the
10135 appropriate cache variable, as in this example:
10138 AC_CHECK_FUNC([vprintf],
10139 [AC_DEFINE([HAVE_VPRINTF], [1],
10140 [Define if vprintf exists.])])
10141 AS_IF([test "x$ac_cv_func_vprintf" != xyes],
10142 [AC_CHECK_FUNC([_doprnt],
10143 [AC_DEFINE([HAVE_DOPRNT], [1],
10144 [Define if _doprnt exists.])])])
10147 If @code{AC_CONFIG_HEADERS} has been called, then instead of creating
10148 @code{DEFS}, @code{AC_OUTPUT} creates a header file by substituting the
10149 correct values into @code{#define} statements in a template file.
10150 @xref{Configuration Headers}, for more information about this kind of
10153 @defmac AC_DEFINE (@var{variable}, @var{value}, @ovar{description})
10154 @defmacx AC_DEFINE (@var{variable})
10155 @cvindex @var{variable}
10157 Define @var{variable} to @var{value} (verbatim), by defining a C
10158 preprocessor macro for @var{variable}. @var{variable} should be a C
10159 identifier, optionally suffixed by a parenthesized argument list to
10160 define a C preprocessor macro with arguments. The macro argument list,
10161 if present, should be a comma-separated list of C identifiers, possibly
10162 terminated by an ellipsis @samp{...} if C99-or-later syntax is employed.
10163 @var{variable} should not contain comments, white space, trigraphs,
10164 backslash-newlines, universal character names, or non-ASCII
10167 @var{value} may contain backslash-escaped newlines, which will be
10168 preserved if you use @code{AC_CONFIG_HEADERS} but flattened if passed
10169 via @code{@@DEFS@@} (with no effect on the compilation, since the
10170 preprocessor sees only one line in the first place). @var{value} should
10171 not contain raw newlines. If you are not using
10172 @code{AC_CONFIG_HEADERS}, @var{value} should not contain any @samp{#}
10173 characters, as @command{make} tends to eat them. To use a shell
10174 variable, use @code{AC_DEFINE_UNQUOTED} instead.
10176 @var{description} is only useful if you are using
10177 @code{AC_CONFIG_HEADERS}. In this case, @var{description} is put into
10178 the generated @file{config.h.in} as the comment before the macro define.
10179 The following example defines the C preprocessor variable
10180 @code{EQUATION} to be the string constant @samp{"$a > $b"}:
10183 AC_DEFINE([EQUATION], ["$a > $b"],
10184 [Equation string.])
10187 If neither @var{value} nor @var{description} are given, then
10188 @var{value} defaults to 1 instead of to the empty string. This is for
10189 backwards compatibility with older versions of Autoconf, but this usage
10190 is obsolescent and may be withdrawn in future versions of Autoconf.
10192 If the @var{variable} is a literal string, it is passed to
10193 @code{m4_pattern_allow} (@pxref{Forbidden Patterns}).
10195 If multiple @code{AC_DEFINE} statements are executed for the same
10196 @var{variable} name (not counting any parenthesized argument list),
10200 @defmac AC_DEFINE_UNQUOTED (@var{variable}, @var{value}, @ovar{description})
10201 @defmacx AC_DEFINE_UNQUOTED (@var{variable})
10202 @acindex{DEFINE_UNQUOTED}
10203 @cvindex @var{variable}
10204 Like @code{AC_DEFINE}, but three shell expansions are
10205 performed---once---on @var{variable} and @var{value}: variable expansion
10206 (@samp{$}), command substitution (@samp{`}), and backslash escaping
10207 (@samp{\}), as if in an unquoted here-document. Single and double quote
10208 characters in the value have no
10209 special meaning. Use this macro instead of @code{AC_DEFINE} when
10210 @var{variable} or @var{value} is a shell variable. Examples:
10213 AC_DEFINE_UNQUOTED([config_machfile], ["$machfile"],
10214 [Configuration machine file.])
10215 AC_DEFINE_UNQUOTED([GETGROUPS_T], [$ac_cv_type_getgroups],
10216 [getgroups return type.])
10217 AC_DEFINE_UNQUOTED([$ac_tr_hdr], [1],
10218 [Translated header name.])
10222 Due to a syntactical oddity of the Bourne shell, do not use
10223 semicolons to separate @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}
10224 calls from other macro calls or shell code; that can cause syntax errors
10225 in the resulting @command{configure} script. Use either blanks or
10226 newlines. That is, do this:
10229 AC_CHECK_HEADER([elf.h],
10230 [AC_DEFINE([SVR4], [1], [System V Release 4]) LIBS="-lelf $LIBS"])
10237 AC_CHECK_HEADER([elf.h],
10238 [AC_DEFINE([SVR4], [1], [System V Release 4])
10239 LIBS="-lelf $LIBS"])
10246 AC_CHECK_HEADER([elf.h],
10247 [AC_DEFINE([SVR4], [1], [System V Release 4]); LIBS="-lelf $LIBS"])
10250 @node Setting Output Variables
10251 @section Setting Output Variables
10252 @cindex Output variables
10254 Another way to record the results of tests is to set @dfn{output
10255 variables}, which are shell variables whose values are substituted into
10256 files that @command{configure} outputs. The two macros below create new
10257 output variables. @xref{Preset Output Variables}, for a list of output
10258 variables that are always available.
10260 @defmac AC_SUBST (@var{variable}, @ovar{value})
10262 Create an output variable from a shell variable. Make @code{AC_OUTPUT}
10263 substitute the variable @var{variable} into output files (typically one
10264 or more makefiles). This means that @code{AC_OUTPUT}
10265 replaces instances of @samp{@@@var{variable}@@} in input files with the
10266 value that the shell variable @var{variable} has when @code{AC_OUTPUT}
10267 is called. The value can contain any non-@code{NUL} character, including
10268 newline. If you are using Automake 1.11 or newer, for newlines in values
10269 you might want to consider using @code{AM_SUBST_NOTMAKE} to prevent
10270 @command{automake} from adding a line @code{@var{variable} =
10271 @@@var{variable}@@} to the @file{Makefile.in} files (@pxref{Optional, ,
10272 Automake, automake, Other things Automake recognizes}).
10274 Variable occurrences should not overlap: e.g., an input file should
10275 not contain @samp{@@@var{var1}@@@var{var2}@@} if @var{var1} and @var{var2}
10276 are variable names.
10277 The substituted value is not rescanned for more output variables;
10278 occurrences of @samp{@@@var{variable}@@} in the value are inserted
10279 literally into the output file. (The algorithm uses the special marker
10280 @code{|#_!!_#|} internally, so neither the substituted value nor the
10281 output file may contain @code{|#_!!_#|}.)
10283 If @var{value} is given, in addition assign it to @var{variable}.
10285 The string @var{variable} is passed to @code{m4_pattern_allow}
10286 (@pxref{Forbidden Patterns}). @var{variable} is not further expanded,
10287 even if there is another macro by the same name.
10290 @defmac AC_SUBST_FILE (@var{variable})
10291 @acindex{SUBST_FILE}
10292 Another way to create an output variable from a shell variable. Make
10293 @code{AC_OUTPUT} insert (without substitutions) the contents of the file
10294 named by shell variable @var{variable} into output files. This means
10295 that @code{AC_OUTPUT} replaces instances of
10296 @samp{@@@var{variable}@@} in output files (such as @file{Makefile.in})
10297 with the contents of the file that the shell variable @var{variable}
10298 names when @code{AC_OUTPUT} is called. Set the variable to
10299 @file{/dev/null} for cases that do not have a file to insert.
10300 This substitution occurs only when the @samp{@@@var{variable}@@} is on a
10301 line by itself, optionally surrounded by spaces and tabs. The
10302 substitution replaces the whole line, including the spaces, tabs, and
10303 the terminating newline.
10305 This macro is useful for inserting makefile fragments containing
10306 special dependencies or other @command{make} directives for particular host
10307 or target types into makefiles. For example, @file{configure.ac}
10311 AC_SUBST_FILE([host_frag])
10312 host_frag=$srcdir/conf/sun4.mh
10316 and then a @file{Makefile.in} could contain:
10322 The string @var{variable} is passed to @code{m4_pattern_allow}
10323 (@pxref{Forbidden Patterns}).
10326 @cindex Precious Variable
10327 @cindex Variable, Precious
10328 Running @command{configure} in varying environments can be extremely
10329 dangerous. If for instance the user runs @samp{CC=bizarre-cc
10330 ./configure}, then the cache, @file{config.h}, and many other output
10331 files depend upon @command{bizarre-cc} being the C compiler. If
10332 for some reason the user runs @command{./configure} again, or if it is
10333 run via @samp{./config.status --recheck}, (@xref{Automatic Remaking},
10334 and @pxref{config.status Invocation}), then the configuration can be
10335 inconsistent, composed of results depending upon two different
10338 Environment variables that affect this situation, such as @samp{CC}
10339 above, are called @dfn{precious variables}, and can be declared as such
10340 by @code{AC_ARG_VAR}.
10342 @defmac AC_ARG_VAR (@var{variable}, @var{description})
10344 Declare @var{variable} is a precious variable, and include its
10345 @var{description} in the variable section of @samp{./configure --help}.
10347 Being precious means that
10350 @var{variable} is substituted via @code{AC_SUBST}.
10353 The value of @var{variable} when @command{configure} was launched is
10354 saved in the cache, including if it was not specified on the command
10355 line but via the environment. Indeed, while @command{configure} can
10356 notice the definition of @code{CC} in @samp{./configure CC=bizarre-cc},
10357 it is impossible to notice it in @samp{CC=bizarre-cc ./configure},
10358 which, unfortunately, is what most users do.
10360 We emphasize that it is the @emph{initial} value of @var{variable} which
10361 is saved, not that found during the execution of @command{configure}.
10362 Indeed, specifying @samp{./configure FOO=foo} and letting
10363 @samp{./configure} guess that @code{FOO} is @code{foo} can be two
10367 @var{variable} is checked for consistency between two
10368 @command{configure} runs. For instance:
10371 $ @kbd{./configure --silent --config-cache}
10372 $ @kbd{CC=cc ./configure --silent --config-cache}
10373 configure: error: 'CC' was not set in the previous run
10374 configure: error: changes in the environment can compromise \
10376 configure: error: run 'make distclean' and/or \
10377 'rm config.cache' and start over
10381 and similarly if the variable is unset, or if its content is changed.
10382 If the content has white space changes only, then the error is degraded
10383 to a warning only, but the old value is reused.
10386 @var{variable} is kept during automatic reconfiguration
10387 (@pxref{config.status Invocation}) as if it had been passed as a command
10388 line argument, including when no cache is used:
10391 $ @kbd{CC=/usr/bin/cc ./configure var=raboof --silent}
10392 $ @kbd{./config.status --recheck}
10393 running CONFIG_SHELL=/bin/sh /bin/sh ./configure var=raboof \
10394 CC=/usr/bin/cc --no-create --no-recursion
10399 @node Special Chars in Variables
10400 @section Special Characters in Output Variables
10401 @cindex Output variables, special characters in
10403 Many output variables are intended to be evaluated both by
10404 @command{make} and by the shell. Some characters are expanded
10405 differently in these two contexts, so to avoid confusion these
10406 variables' values should not contain any of the following characters:
10409 " # $ & ' ( ) * ; < > ? [ \ ^ ` |
10412 Also, these variables' values should neither contain newlines, nor start
10413 with @samp{~}, nor contain white space or @samp{:} immediately followed
10414 by @samp{~}. The values can contain nonempty sequences of white space
10415 characters like tabs and spaces, but each such sequence might
10416 arbitrarily be replaced by a single space during substitution.
10418 These restrictions apply both to the values that @command{configure}
10419 computes, and to the values set directly by the user. For example, the
10420 following invocations of @command{configure} are problematic, since they
10421 attempt to use special characters within @code{CPPFLAGS} and white space
10422 within @code{$(srcdir)}:
10425 CPPFLAGS='-DOUCH="&\"#$*?"' '../My Source/ouch-1.0/configure'
10427 '../My Source/ouch-1.0/configure' CPPFLAGS='-DOUCH="&\"#$*?"'
10430 @node Caching Results
10431 @section Caching Results
10434 To avoid checking for the same features repeatedly in various
10435 @command{configure} scripts (or in repeated runs of one script),
10436 @command{configure} can optionally save the results of many checks in a
10437 @dfn{cache file} (@pxref{Cache Files}). If a @command{configure} script
10438 runs with caching enabled and finds a cache file, it reads the results
10439 of previous runs from the cache and avoids rerunning those checks. As a
10440 result, @command{configure} can then run much faster than if it had to
10441 perform all of the checks every time.
10443 @defmac AC_CACHE_VAL (@var{cache-id}, @var{commands-to-set-it})
10444 @acindex{CACHE_VAL}
10445 Ensure that the results of the check identified by @var{cache-id} are
10446 available. If the results of the check were in the cache file that was
10447 read, and @command{configure} was not given the @option{--quiet} or
10448 @option{--silent} option, print a message saying that the result was
10449 cached; otherwise, run the shell commands @var{commands-to-set-it}. If
10450 the shell commands are run to determine the value, the value is
10451 saved in the cache file just before @command{configure} creates its output
10452 files. @xref{Cache Variable Names}, for how to choose the name of the
10453 @var{cache-id} variable.
10455 The @var{commands-to-set-it} @emph{must have no side effects} except for
10456 setting the variable @var{cache-id}, see below.
10459 @defmac AC_CACHE_CHECK (@var{message}, @var{cache-id}, @
10460 @var{commands-to-set-it})
10461 @acindex{CACHE_CHECK}
10462 A wrapper for @code{AC_CACHE_VAL} that takes care of printing the
10463 messages. This macro provides a convenient shorthand for the most
10464 common way to use these macros. It calls @code{AC_MSG_CHECKING} for
10465 @var{message}, then @code{AC_CACHE_VAL} with the @var{cache-id} and
10466 @var{commands} arguments, and @code{AC_MSG_RESULT} with @var{cache-id}.
10468 The @var{commands-to-set-it} @emph{must have no side effects} except for
10469 setting the variable @var{cache-id}, see below.
10472 It is common to find buggy macros using @code{AC_CACHE_VAL} or
10473 @code{AC_CACHE_CHECK}, because people are tempted to call
10474 @code{AC_DEFINE} in the @var{commands-to-set-it}. Instead, the code that
10475 @emph{follows} the call to @code{AC_CACHE_VAL} should call
10476 @code{AC_DEFINE}, by examining the value of the cache variable. For
10477 instance, the following macro is broken:
10480 @c If you change this example, adjust tests/base.at:AC_CACHE_CHECK.
10482 AC_DEFUN([AC_SHELL_TRUE],
10483 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
10484 [my_cv_shell_true_works=no
10485 (true) 2>/dev/null && my_cv_shell_true_works=yes
10486 if test "x$my_cv_shell_true_works" = xyes; then
10487 AC_DEFINE([TRUE_WORKS], [1],
10488 [Define if 'true(1)' works properly.])
10495 This fails if the cache is enabled: the second time this macro is run,
10496 @code{TRUE_WORKS} @emph{will not be defined}. The proper implementation
10500 @c If you change this example, adjust tests/base.at:AC_CACHE_CHECK.
10502 AC_DEFUN([AC_SHELL_TRUE],
10503 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
10504 [my_cv_shell_true_works=no
10505 (true) 2>/dev/null && my_cv_shell_true_works=yes])
10506 if test "x$my_cv_shell_true_works" = xyes; then
10507 AC_DEFINE([TRUE_WORKS], [1],
10508 [Define if 'true(1)' works properly.])
10514 Also, @var{commands-to-set-it} should not print any messages, for
10515 example with @code{AC_MSG_CHECKING}; do that before calling
10516 @code{AC_CACHE_VAL}, so the messages are printed regardless of whether
10517 the results of the check are retrieved from the cache or determined by
10518 running the shell commands.
10521 * Cache Variable Names:: Shell variables used in caches
10522 * Cache Files:: Files @command{configure} uses for caching
10523 * Cache Checkpointing:: Loading and saving the cache file
10526 @node Cache Variable Names
10527 @subsection Cache Variable Names
10528 @cindex Cache variable
10530 The names of cache variables should have the following format:
10533 @var{package-prefix}_cv_@var{value-type}_@var{specific-value}_@ovar{additional-options}
10537 for example, @samp{ac_cv_header_stat_broken} or
10538 @samp{ac_cv_prog_gcc_traditional}. The parts of the variable name are:
10541 @item @var{package-prefix}
10542 An abbreviation for your package or organization; the same prefix you
10543 begin local Autoconf macros with, except lowercase by convention.
10544 For cache values used by the distributed Autoconf macros, this value is
10548 Indicates that this shell variable is a cache value. This string
10549 @emph{must} be present in the variable name, including the leading
10552 @item @var{value-type}
10553 A convention for classifying cache values, to produce a rational naming
10554 system. The values used in Autoconf are listed in @ref{Macro Names}.
10556 @item @var{specific-value}
10557 Which member of the class of cache values this test applies to.
10558 For example, which function (@samp{alloca}), program (@samp{gcc}), or
10559 output variable (@samp{INSTALL}).
10561 @item @var{additional-options}
10562 Any particular behavior of the specific member that this test applies to.
10563 For example, @samp{broken} or @samp{set}. This part of the name may
10564 be omitted if it does not apply.
10567 The values assigned to cache variables may not contain newlines.
10568 Usually, their values are Boolean (@samp{yes} or @samp{no}) or the
10569 names of files or functions; so this is not an important restriction.
10570 @ref{Cache Variable Index} for an index of cache variables with
10571 documented semantics.
10575 @subsection Cache Files
10577 A cache file is a shell script that caches the results of configure
10578 tests run on one system so they can be shared between configure scripts
10579 and configure runs. It is not useful on other systems. If its contents
10580 are invalid for some reason, the user may delete or edit it, or override
10581 documented cache variables on the @command{configure} command line.
10583 By default, @command{configure} uses no cache file,
10584 to avoid problems caused by accidental
10585 use of stale cache files.
10587 To enable caching, @command{configure} accepts @option{--config-cache} (or
10588 @option{-C}) to cache results in the file @file{config.cache}.
10589 Alternatively, @option{--cache-file=@var{file}} specifies that
10590 @var{file} be the cache file. The cache file is created if it does not
10591 exist already. When @command{configure} calls @command{configure} scripts in
10592 subdirectories, it uses the @option{--cache-file} argument so that they
10593 share the same cache. @xref{Subdirectories}, for information on
10594 configuring subdirectories with the @code{AC_CONFIG_SUBDIRS} macro.
10596 @file{config.status} only pays attention to the cache file if it is
10597 given the @option{--recheck} option, which makes it rerun
10598 @command{configure}.
10600 It is wrong to try to distribute cache files for particular system types.
10601 There is too much room for error in doing that, and too much
10602 administrative overhead in maintaining them. For any features that
10603 can't be guessed automatically, use the standard method of the canonical
10604 system type and linking files (@pxref{Manual Configuration}).
10606 The site initialization script can specify a site-wide cache file to
10607 use, instead of the usual per-program cache. In this case, the cache
10608 file gradually accumulates information whenever someone runs a new
10609 @command{configure} script. (Running @command{configure} merges the new cache
10610 results with the existing cache file.) This may cause problems,
10611 however, if the system configuration (e.g., the installed libraries or
10612 compilers) changes and the stale cache file is not deleted.
10614 If @command{configure} is interrupted at the right time when it updates
10615 a cache file outside of the build directory where the @command{configure}
10616 script is run, it may leave behind a temporary file named after the
10617 cache file with digits following it. You may safely delete such a file.
10620 @node Cache Checkpointing
10621 @subsection Cache Checkpointing
10623 If your configure script, or a macro called from @file{configure.ac}, happens
10624 to abort the configure process, it may be useful to checkpoint the cache
10625 a few times at key points using @code{AC_CACHE_SAVE}. Doing so
10626 reduces the amount of time it takes to rerun the configure script with
10627 (hopefully) the error that caused the previous abort corrected.
10629 @c FIXME: Do we really want to document this guy?
10630 @defmac AC_CACHE_LOAD
10631 @acindex{CACHE_LOAD}
10632 Loads values from existing cache file, or creates a new cache file if a
10633 cache file is not found. Called automatically from @code{AC_INIT}.
10636 @defmac AC_CACHE_SAVE
10637 @acindex{CACHE_SAVE}
10638 Flushes all cached values to the cache file. Called automatically from
10639 @code{AC_OUTPUT}, but it can be quite useful to call
10640 @code{AC_CACHE_SAVE} at key points in @file{configure.ac}.
10646 @r{ @dots{} AC_INIT, etc. @dots{}}
10648 # Checks for programs.
10651 @r{ @dots{} more program checks @dots{}}
10656 # Checks for libraries.
10657 AC_CHECK_LIB([nsl], [gethostbyname])
10658 AC_CHECK_LIB([socket], [connect])
10659 @r{ @dots{} more lib checks @dots{}}
10664 # Might abort@dots{}
10665 AM_PATH_GTK([1.0.2], [], [AC_MSG_ERROR([GTK not in path])])
10666 AM_PATH_GTKMM([0.9.5], [], [AC_MSG_ERROR([GTK not in path])])
10668 @r{ @dots{} AC_OUTPUT, etc. @dots{}}
10671 @node Printing Messages
10672 @section Printing Messages
10673 @cindex Messages, from @command{configure}
10675 @command{configure} scripts need to give users running them several kinds
10676 of information. The following macros print messages in ways appropriate
10677 for each kind. The arguments to all of them get enclosed in shell
10678 double quotes, so the shell performs variable and back-quote
10679 substitution on them.
10681 These macros are all wrappers around the @command{echo} shell command.
10682 They direct output to the appropriate file descriptor (@pxref{File
10683 Descriptor Macros}).
10684 @command{configure} scripts should rarely need to run @command{echo} directly
10685 to print messages for the user. Using these macros makes it easy to
10686 change how and when each kind of message is printed; such changes need
10687 only be made to the macro definitions and all the callers change
10690 To diagnose static issues, i.e., when @command{autoconf} is run, see
10691 @ref{Diagnostic Macros}.
10693 @defmac AC_MSG_CHECKING (@var{feature-description})
10694 @acindex{MSG_CHECKING}
10695 Notify the user that @command{configure} is checking for a particular
10696 feature. This macro prints a message that starts with @samp{checking }
10697 and ends with @samp{...} and no newline. It must be followed by a call
10698 to @code{AC_MSG_RESULT} to print the result of the check and the
10699 newline. The @var{feature-description} should be something like
10700 @samp{whether the Fortran compiler accepts C++ comments} or @samp{for
10703 This macro prints nothing if @command{configure} is run with the
10704 @option{--quiet} or @option{--silent} option.
10707 @anchor{AC_MSG_RESULT}
10708 @defmac AC_MSG_RESULT (@var{result-description})
10709 @acindex{MSG_RESULT}
10710 Notify the user of the results of a check. @var{result-description} is
10711 almost always the value of the cache variable for the check, typically
10712 @samp{yes}, @samp{no}, or a file name. This macro should follow a call
10713 to @code{AC_MSG_CHECKING}, and the @var{result-description} should be
10714 the completion of the message printed by the call to
10715 @code{AC_MSG_CHECKING}.
10717 This macro prints nothing if @command{configure} is run with the
10718 @option{--quiet} or @option{--silent} option.
10721 @anchor{AC_MSG_NOTICE}
10722 @defmac AC_MSG_NOTICE (@var{message})
10723 @acindex{MSG_NOTICE}
10724 Deliver the @var{message} to the user. It is useful mainly to print a
10725 general description of the overall purpose of a group of feature checks,
10729 AC_MSG_NOTICE([checking if stack overflow is detectable])
10732 This macro prints nothing if @command{configure} is run with the
10733 @option{--quiet} or @option{--silent} option.
10736 @anchor{AC_MSG_ERROR}
10737 @defmac AC_MSG_ERROR (@var{error-description}, @dvar{exit-status, $?/1})
10738 @acindex{MSG_ERROR}
10739 Notify the user of an error that prevents @command{configure} from
10740 completing. This macro prints an error message to the standard error
10741 output and exits @command{configure} with @var{exit-status} (@samp{$?}
10742 by default, except that @samp{0} is converted to @samp{1}).
10743 @var{error-description} should be something like @samp{invalid value
10746 The @var{error-description} should start with a lower-case letter, and
10747 ``cannot'' is preferred to ``can't''.
10750 @defmac AC_MSG_FAILURE (@var{error-description}, @ovar{exit-status})
10751 @acindex{MSG_FAILURE}
10752 This @code{AC_MSG_ERROR} wrapper notifies the user of an error that
10753 prevents @command{configure} from completing @emph{and} that additional
10754 details are provided in @file{config.log}. This is typically used when
10755 abnormal results are found during a compilation.
10758 @anchor{AC_MSG_WARN}
10759 @defmac AC_MSG_WARN (@var{problem-description})
10761 Notify the @command{configure} user of a possible problem. This macro
10762 prints the message to the standard error output; @command{configure}
10763 continues running afterward, so macros that call @code{AC_MSG_WARN} should
10764 provide a default (back-up) behavior for the situations they warn about.
10765 @var{problem-description} should be something like @samp{ln -s seems to
10771 @c ====================================================== Programming in M4.
10773 @node Programming in M4
10774 @chapter Programming in M4
10777 Autoconf is written on top of two layers: @dfn{M4sugar}, which provides
10778 convenient macros for pure M4 programming, and @dfn{M4sh}, which
10779 provides macros dedicated to shell script generation.
10781 As of this version of Autoconf, these two layers still contain
10782 experimental macros, whose interface might change in the future. As a
10783 matter of fact, @emph{anything that is not documented must not be used}.
10786 * M4 Quotation:: Protecting macros from unwanted expansion
10787 * Using autom4te:: The Autoconf executables backbone
10788 * Programming in M4sugar:: Convenient pure M4 macros
10789 * Debugging via autom4te:: Figuring out what M4 was doing
10793 @section M4 Quotation
10794 @cindex M4 quotation
10797 The most common problem with existing macros is an improper quotation.
10798 This section, which users of Autoconf can skip, but which macro writers
10799 @emph{must} read, first justifies the quotation scheme that was chosen
10800 for Autoconf and then ends with a rule of thumb. Understanding the
10801 former helps one to follow the latter.
10804 * Active Characters:: Characters that change the behavior of M4
10805 * One Macro Call:: Quotation and one macro call
10806 * Quoting and Parameters:: M4 vs. shell parameters
10807 * Quotation and Nested Macros:: Macros calling macros
10808 * Changequote is Evil:: Worse than INTERCAL: M4 + changequote
10809 * Quadrigraphs:: Another way to escape special characters
10810 * Balancing Parentheses:: Dealing with unbalanced parentheses
10811 * Quotation Rule Of Thumb:: One parenthesis, one quote
10814 @node Active Characters
10815 @subsection Active Characters
10817 To fully understand where proper quotation is important, you first need
10818 to know what the special characters are in Autoconf: @samp{#} introduces
10819 a comment inside which no macro expansion is performed, @samp{,}
10820 separates arguments, @samp{[} and @samp{]} are the quotes
10821 themselves@footnote{By itself, M4 uses @samp{`} and @samp{'}; it is the
10822 M4sugar layer that sets up the preferred quotes of @samp{[} and @samp{]}.},
10823 @samp{(} and @samp{)} (which M4 tries to match by pairs), and finally
10824 @samp{$} inside a macro definition.
10826 In order to understand the delicate case of macro calls, we first have
10827 to present some obvious failures. Below they are ``obvious-ified'',
10828 but when you find them in real life, they are usually in disguise.
10830 Comments, introduced by a hash and running up to the newline, are opaque
10831 tokens to the top level: active characters are turned off, and there is
10832 no macro expansion:
10835 # define([def], ine)
10836 @result{}# define([def], ine)
10839 Each time there can be a macro expansion, there is a quotation
10840 expansion, i.e., one level of quotes is stripped:
10844 @result{}int tab10;
10846 @result{}int tab[10];
10849 Without this in mind, the reader might try hopelessly to use her macro
10853 define([array], [int tab[10];])
10855 @result{}int tab10;
10861 How can you correctly output the intended results@footnote{Using
10865 @node One Macro Call
10866 @subsection One Macro Call
10868 Let's proceed on the interaction between active characters and macros
10869 with this small macro, which just returns its first argument:
10872 define([car], [$1])
10876 The two pairs of quotes above are not part of the arguments of
10877 @code{define}; rather, they are understood by the top level when it
10878 tries to find the arguments of @code{define}. Therefore, assuming
10879 @code{car} is not already defined, it is equivalent to write:
10886 But, while it is acceptable for a @file{configure.ac} to avoid unnecessary
10887 quotes, it is bad practice for Autoconf macros which must both be more
10888 robust and also advocate perfect style.
10890 At the top level, there are only two possibilities: either you
10891 quote or you don't:
10896 [car(foo, bar, baz)]
10897 @result{}car(foo, bar, baz)
10900 Let's pay attention to the special characters:
10904 @error{}EOF in argument list
10907 The closing parenthesis is hidden in the comment; with a hypothetical
10908 quoting, the top level understood it this way:
10915 Proper quotation, of course, fixes the problem:
10922 Here are more examples:
10930 @result{}(foo, bar)
10931 car([(foo], [bar)])
10945 @node Quoting and Parameters
10946 @subsection Quoting and Parameters
10948 When M4 encounters @samp{$} within a macro definition, followed
10949 immediately by a character it recognizes (@samp{0}@dots{}@samp{9},
10950 @samp{#}, @samp{@@}, or @samp{*}), it will perform M4 parameter
10951 expansion. This happens regardless of how many layers of quotes the
10952 parameter expansion is nested within, or even if it occurs in text that
10953 will be rescanned as a comment.
10956 define([none], [$1])
10958 define([one], [[$1]])
10960 define([two], [[[$1]]])
10962 define([comment], [# $1])
10964 define([active], [ACTIVE])
10976 On the other hand, since autoconf generates shell code, you often want
10977 to output shell variable expansion, rather than performing M4 parameter
10978 expansion. To do this, you must use M4 quoting to separate the @samp{$}
10979 from the next character in the definition of your macro. If the macro
10980 definition occurs in single-quoted text, then insert another level of
10981 quoting; if the usage is already inside a double-quoted string, then
10982 split it into concatenated strings.
10985 define([foo], [a single-quoted $[]1 definition])
10987 define([bar], [[a double-quoted $][1 definition]])
10990 @result{}a single-quoted $1 definition
10992 @result{}a double-quoted $1 definition
10995 Posix states that M4 implementations are free to provide implementation
10996 extensions when @samp{$@{} is encountered in a macro definition.
10997 Autoconf reserves the longer sequence @samp{$@{@{} for use with planned
10998 extensions that will be available in the future GNU M4 2.0,
10999 but guarantees that all other instances of @samp{$@{} will be output
11000 literally. Therefore, this idiom can also be used to output shell code
11001 parameter references:
11004 define([first], [$@{1@}])first
11008 Posix also states that @samp{$11} should expand to the first parameter
11009 concatenated with a literal @samp{1}, although some versions of
11010 GNU M4 expand the eleventh parameter instead. For
11011 portability, you should only use single-digit M4 parameter expansion.
11013 With this in mind, we can explore the cases where macros invoke
11016 @node Quotation and Nested Macros
11017 @subsection Quotation and Nested Macros
11019 The examples below use the following macros:
11022 define([car], [$1])
11023 define([active], [ACT, IVE])
11024 define([array], [int tab[10]])
11027 Each additional embedded macro call introduces other possible
11028 interesting quotations:
11039 In the first case, the top level looks for the arguments of @code{car},
11040 and finds @samp{active}. Because M4 evaluates its arguments
11041 before applying the macro, @samp{active} is expanded, which results in:
11049 In the second case, the top level gives @samp{active} as first and only
11050 argument of @code{car}, which results in:
11058 i.e., the argument is evaluated @emph{after} the macro that invokes it.
11059 In the third case, @code{car} receives @samp{[active]}, which results in:
11067 exactly as we already saw above.
11069 The example above, applied to a more realistic example, gives:
11073 @result{}int tab10;
11074 car([int tab[10];])
11075 @result{}int tab10;
11076 car([[int tab[10];]])
11077 @result{}int tab[10];
11081 Huh? The first case is easily understood, but why is the second wrong,
11082 and the third right? To understand that, you must know that after
11083 M4 expands a macro, the resulting text is immediately subjected
11084 to macro expansion and quote removal. This means that the quote removal
11085 occurs twice---first before the argument is passed to the @code{car}
11086 macro, and second after the @code{car} macro expands to the first
11089 As the author of the Autoconf macro @code{car}, you then consider it to
11090 be incorrect that your users have to double-quote the arguments of
11091 @code{car}, so you ``fix'' your macro. Let's call it @code{qar} for
11095 define([qar], [[$1]])
11099 and check that @code{qar} is properly fixed:
11102 qar([int tab[10];])
11103 @result{}int tab[10];
11107 Ahhh! That's much better.
11109 But note what you've done: now that the result of @code{qar} is always
11110 a literal string, the only time a user can use nested macros is if she
11111 relies on an @emph{unquoted} macro call:
11121 leaving no way for her to reproduce what she used to do with @code{car}:
11129 Worse yet: she wants to use a macro that produces a set of @code{cpp}
11133 define([my_includes], [#include <stdio.h>])
11135 @result{}#include <stdio.h>
11137 @error{}EOF in argument list
11140 This macro, @code{qar}, because it double quotes its arguments, forces
11141 its users to leave their macro calls unquoted, which is dangerous.
11142 Commas and other active symbols are interpreted by M4 before
11143 they are given to the macro, often not in the way the users expect.
11144 Also, because @code{qar} behaves differently from the other macros,
11145 it's an exception that should be avoided in Autoconf.
11147 @node Changequote is Evil
11148 @subsection @code{changequote} is Evil
11149 @cindex @code{changequote}
11151 The temptation is often high to bypass proper quotation, in particular
11152 when it's late at night. Then, many experienced Autoconf hackers
11153 finally surrender to the dark side of the force and use the ultimate
11154 weapon: @code{changequote}.
11156 The M4 builtin @code{changequote} belongs to a set of primitives that
11157 allow one to adjust the syntax of the language to adjust it to one's
11158 needs. For instance, by default M4 uses @samp{`} and @samp{'} as
11159 quotes, but in the context of shell programming (and actually of most
11160 programming languages), that's about the worst choice one can make:
11161 because of strings and back-quoted expressions in shell code (such as
11162 @samp{'this'} and @samp{`that`}), and because of literal characters in usual
11163 programming languages (as in @samp{'0'}), there are many unbalanced
11164 @samp{`} and @samp{'}. Proper M4 quotation then becomes a nightmare, if
11165 not impossible. In order to make M4 useful in such a context, its
11166 designers have equipped it with @code{changequote}, which makes it
11167 possible to choose another pair of quotes. M4sugar, M4sh, Autoconf, and
11168 Autotest all have chosen to use @samp{[} and @samp{]}. Not especially
11169 because they are unlikely characters, but @emph{because they are
11170 characters unlikely to be unbalanced}.
11172 There are other magic primitives, such as @code{changecom} to specify
11173 what syntactic forms are comments (it is common to see
11174 @samp{changecom(<!--, -->)} when M4 is used to produce HTML pages),
11175 @code{changeword} and @code{changesyntax} to change other syntactic
11176 details (such as the character to denote the @var{n}th argument, @samp{$} by
11177 default, the parentheses around arguments, etc.).
11179 These primitives are really meant to make M4 more useful for specific
11180 domains: they should be considered like command line options:
11181 @option{--quotes}, @option{--comments}, @option{--words}, and
11182 @option{--syntax}. Nevertheless, they are implemented as M4 builtins, as
11183 it makes M4 libraries self contained (no need for additional options).
11185 There lies the problem@enddots{}
11189 The problem is that it is then tempting to use them in the middle of an
11190 M4 script, as opposed to its initialization. This, if not carefully
11191 thought out, can lead to disastrous effects: @emph{you are changing the
11192 language in the middle of the execution}. Changing and restoring the
11193 syntax is often not enough: if you happened to invoke macros in between,
11194 these macros are lost, as the current syntax is probably not
11195 the one they were implemented with.
11197 @c FIXME: I've been looking for a short, real case example, but I
11198 @c lost them all :(
11202 @subsection Quadrigraphs
11203 @cindex quadrigraphs
11204 @cindex @samp{@@S|@@}
11205 @cindex @samp{@@&t@@}
11206 @c Info cannot handle ':' in index entries.
11208 @cindex @samp{@@<:@@}
11209 @cindex @samp{@@:>@@}
11210 @cindex @samp{@@%:@@}
11211 @cindex @samp{@@@{:@@}
11212 @cindex @samp{@@:@}@@}
11215 When writing an Autoconf macro you may occasionally need to generate
11216 special characters that are difficult to express with the standard
11217 Autoconf quoting rules. For example, you may need to output the regular
11218 expression @samp{[^[]}, which matches any character other than @samp{[}.
11219 This expression contains unbalanced brackets so it cannot be put easily
11222 Additionally, there are a few m4sugar macros (such as @code{m4_split}
11223 and @code{m4_expand}) which internally use special markers in addition
11224 to the regular quoting characters. If the arguments to these macros
11225 contain the literal strings @samp{-=<@{(} or @samp{)@}>=-}, the macros
11226 might behave incorrectly.
11228 You can work around these problems by using one of the following
11229 @dfn{quadrigraphs}:
11245 Expands to nothing.
11248 Quadrigraphs are replaced at a late stage of the translation process,
11249 after @command{m4} is run, so they do not get in the way of M4 quoting.
11250 For example, the string @samp{^@@<:@@}, independently of its quotation,
11251 appears as @samp{^[} in the output.
11253 The empty quadrigraph can be used:
11256 @item to mark trailing spaces explicitly
11258 Trailing spaces are smashed by @command{autom4te}. This is a feature.
11260 @item to produce quadrigraphs and other strings reserved by m4sugar
11262 For instance @samp{@@<@@&t@@:@@} produces @samp{@@<:@@}. For a more
11266 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])dnl
11267 m4_split([a )@}>=- b -=<@{( c])
11268 @result{}[a], [], [B], [], [c]
11269 m4_split([a )@}@@&t@@>=- b -=<@@&t@@@{( c])
11270 @result{}[a], [)@}>=-], [b], [-=<@{(], [c]
11273 @item to escape @emph{occurrences} of forbidden patterns
11275 For instance you might want to mention @code{AC_FOO} in a comment, while
11276 still being sure that @command{autom4te} still catches unexpanded
11277 @samp{AC_*}. Then write @samp{AC@@&t@@_FOO}.
11280 The name @samp{@@&t@@} was suggested by Paul Eggert:
11283 I should give some credit to the @samp{@@&t@@} pun. The @samp{&} is my
11284 own invention, but the @samp{t} came from the source code of the
11285 ALGOL68C compiler, written by Steve Bourne (of Bourne shell fame),
11286 and which used @samp{mt} to denote the empty string. In C, it would
11287 have looked like something like:
11290 char const mt[] = "";
11294 but of course the source code was written in Algol 68.
11296 I don't know where he got @samp{mt} from: it could have been his own
11297 invention, and I suppose it could have been a common pun around the
11298 Cambridge University computer lab at the time.
11302 @node Balancing Parentheses
11303 @subsection Dealing with unbalanced parentheses
11304 @cindex balancing parentheses
11305 @cindex parentheses, balancing
11306 @cindex unbalanced parentheses, managing
11308 One of the pitfalls of portable shell programming is that
11309 if you intend your script to run with obsolescent shells,
11310 @command{case} statements require unbalanced parentheses.
11311 @xref{case, , Limitations of Shell Builtins}.
11312 With syntax highlighting
11313 editors, the presence of unbalanced @samp{)} can interfere with editors
11314 that perform syntax highlighting of macro contents based on finding the
11315 matching @samp{(}. Another concern is how much editing must be done
11316 when transferring code snippets between shell scripts and macro
11317 definitions. But most importantly, the presence of unbalanced
11318 parentheses can introduce expansion bugs.
11320 For an example, here is an underquoted attempt to use the macro
11321 @code{my_case}, which happens to expand to a portable @command{case}
11325 AC_DEFUN([my_case],
11326 [case $file_name in
11327 *.c) echo "C source code";;
11333 In the above example, the @code{AS_IF} call under-quotes its arguments.
11334 As a result, the unbalanced @samp{)} generated by the premature
11335 expansion of @code{my_case} results in expanding @code{AS_IF} with a
11336 truncated parameter, and the expansion is syntactically invalid:
11343 fi echo "C source code";;
11347 If nothing else, this should emphasize the importance of the quoting
11348 arguments to macro calls. On the other hand, there are several
11349 variations for defining @code{my_case} to be more robust, even when used
11350 without proper quoting, each with some benefits and some drawbacks.
11353 @item Use left parenthesis before pattern
11355 AC_DEFUN([my_case],
11356 [case $file_name in
11357 (*.c) echo "C source code";;
11361 This is simple and provides balanced parentheses. Although this is not
11362 portable to obsolescent shells (notably Solaris 10 @command{/bin/sh}),
11363 platforms with these shells invariably have a more-modern shell
11364 available somewhere so this approach typically suffices nowadays.
11366 @item Creative literal shell comment
11368 AC_DEFUN([my_case],
11369 [case $file_name in #(
11370 *.c) echo "C source code";;
11374 This version provides balanced parentheses to several editors, and can
11375 be copied and pasted into a terminal as is. Unfortunately, it is still
11376 unbalanced as an Autoconf argument, since @samp{#(} is an M4 comment
11377 that masks the normal properties of @samp{(}.
11379 @item Quadrigraph shell comment
11381 AC_DEFUN([my_case],
11382 [case $file_name in @@%:@@(
11383 *.c) echo "C source code";;
11387 This version provides balanced parentheses to even more editors, and can
11388 be used as a balanced Autoconf argument. Unfortunately, it requires
11389 some editing before it can be copied and pasted into a terminal, and the
11390 use of the quadrigraph @samp{@@%:@@} for @samp{#} reduces readability.
11392 @item Quoting just the parenthesis
11394 AC_DEFUN([my_case],
11395 [case $file_name in
11396 *.c[)] echo "C source code";;
11400 This version quotes the @samp{)}, so that it can be used as a balanced
11401 Autoconf argument. As written, this is not balanced to an editor, but
11402 it can be coupled with @samp{[#(]} to meet that need, too. However, it
11403 still requires some edits before it can be copied and pasted into a
11406 @item Double-quoting the entire statement
11408 AC_DEFUN([my_case],
11409 [[case $file_name in #(
11410 *.c) echo "C source code";;
11414 Since the entire macro is double-quoted, there is no problem with using
11415 this as an Autoconf argument; and since the double-quoting is over the
11416 entire statement, this code can be easily copied and pasted into a
11417 terminal. However, the double quoting prevents the expansion of any
11418 macros inside the case statement, which may cause its own set of
11421 @item Using @code{AS_CASE}
11423 AC_DEFUN([my_case],
11424 [AS_CASE([$file_name],
11425 [*.c], [echo "C source code"])])
11428 This version avoids the balancing issue altogether, by relying on
11429 @code{AS_CASE} (@pxref{Common Shell Constructs}); it also allows for the
11430 expansion of @code{AC_REQUIRE} to occur prior to the entire case
11431 statement, rather than within a branch of the case statement that might
11432 not be taken. However, the abstraction comes with a penalty that it is
11433 no longer a quick copy, paste, and edit to get back to shell code.
11437 @node Quotation Rule Of Thumb
11438 @subsection Quotation Rule Of Thumb
11440 To conclude, the quotation rule of thumb is:
11442 @center @emph{One pair of quotes per pair of parentheses.}
11444 Never over-quote, never under-quote, in particular in the definition of
11445 macros. In the few places where the macros need to use brackets
11446 (usually in C program text or regular expressions), properly quote
11447 @emph{the arguments}!
11449 It is common to read Autoconf programs with snippets like:
11453 changequote(<<, >>)dnl
11454 <<#include <time.h>
11455 #ifndef tzname /* For SGI. */
11456 extern char *tzname[]; /* RS6000 and others reject char **tzname. */
11458 changequote([, ])dnl
11459 [atoi (*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
11463 which is incredibly useless since @code{AC_TRY_LINK} is @emph{already}
11464 double quoting, so you just need:
11469 #ifndef tzname /* For SGI. */
11470 extern char *tzname[]; /* RS6000 and others reject char **tzname. */
11473 [ac_cv_var_tzname=yes],
11474 [ac_cv_var_tzname=no])
11478 The M4-fluent reader might note that these two examples are rigorously
11479 equivalent, since M4 swallows both the @samp{changequote(<<, >>)}
11480 and @samp{<<} @samp{>>} when it @dfn{collects} the arguments: these
11481 quotes are not part of the arguments!
11483 Simplified, the example above is just doing this:
11486 changequote(<<, >>)dnl
11488 changequote([, ])dnl
11498 With macros that do not double quote their arguments (which is the
11499 rule), double-quote the (risky) literals:
11502 AC_LINK_IFELSE([AC_LANG_PROGRAM(
11503 [[#include <time.h>
11504 #ifndef tzname /* For SGI. */
11505 extern char *tzname[]; /* RS6000 and others reject char **tzname. */
11507 [atoi (*tzname);])],
11508 [ac_cv_var_tzname=yes],
11509 [ac_cv_var_tzname=no])
11512 Please note that the macro @code{AC_TRY_LINK} is obsolete, so you really
11513 should be using @code{AC_LINK_IFELSE} instead.
11515 @xref{Quadrigraphs}, for what to do if you run into a hopeless case
11516 where quoting does not suffice.
11518 When you create a @command{configure} script using newly written macros,
11519 examine it carefully to check whether you need to add more quotes in
11520 your macros. If one or more words have disappeared in the M4
11521 output, you need more quotes. When in doubt, quote.
11523 However, it's also possible to put on too many layers of quotes. If
11524 this happens, the resulting @command{configure} script may contain
11525 unexpanded macros. The @command{autoconf} program checks for this problem
11526 by looking for the string @samp{AC_} in @file{configure}. However, this
11527 heuristic does not work in general: for example, it does not catch
11528 overquoting in @code{AC_DEFINE} descriptions.
11531 @c ---------------------------------------- Using autom4te
11533 @node Using autom4te
11534 @section Using @command{autom4te}
11536 The Autoconf suite, including M4sugar, M4sh, and Autotest, in addition
11537 to Autoconf per se, heavily rely on M4. All these different uses
11538 revealed common needs factored into a layer over M4:
11539 @command{autom4te}@footnote{
11541 Yet another great name from Lars J. Aas.
11545 @command{autom4te} is a preprocessor that is like @command{m4}.
11546 It supports M4 extensions designed for use in tools like Autoconf.
11549 * autom4te Invocation:: A GNU M4 wrapper
11550 * Customizing autom4te:: Customizing the Autoconf package
11553 @node autom4te Invocation
11554 @subsection Invoking @command{autom4te}
11556 The command line arguments are modeled after M4's:
11559 autom4te @var{options} @var{files}
11564 where the @var{files} are directly passed to @command{m4}. By default,
11565 GNU M4 is found during configuration, but the environment
11567 @env{M4} can be set to tell @command{autom4te} where to look. In addition
11568 to the regular expansion, it handles the replacement of the quadrigraphs
11569 (@pxref{Quadrigraphs}), and of @samp{__oline__}, the current line in the
11570 output. It supports an extended syntax for the @var{files}:
11573 @item @var{file}.m4f
11574 This file is an M4 frozen file. Note that @emph{all the previous files
11575 are ignored}. See the @option{--melt} option for the rationale.
11578 If found in the library path, the @var{file} is included for expansion,
11579 otherwise it is ignored instead of triggering a failure.
11584 Of course, it supports the Autoconf common subset of options:
11589 Print a summary of the command line options and exit.
11593 Print the version number of Autoconf and exit.
11597 Report processing steps.
11601 Don't remove the temporary files and be even more verbose.
11603 @item --include=@var{dir}
11604 @itemx -I @var{dir}
11605 Also look for input files in @var{dir}. Multiple invocations
11608 @item --output=@var{file}
11609 @itemx -o @var{file}
11610 Save output (script or trace) to @var{file}. The file @option{-} stands
11611 for the standard output.
11616 As an extension of @command{m4}, it includes the following options:
11620 @item --warnings=@var{category}[,@var{category}...]
11621 @itemx -W@var{category}[,@var{category}...]
11623 Enable or disable warnings related to each @var{category}.
11624 @xref{m4_warn}, for a comprehensive list of categories.
11625 Special values include:
11629 Enable all categories of warnings.
11632 Disable all categories of warnings.
11635 Treat all warnings as errors.
11637 @item no-@var{category}
11638 Disable warnings falling into @var{category}.
11641 The environment variable @env{WARNINGS} may also be set to a
11642 comma-separated list of warning categories to enable or disable.
11643 It is interpreted exactly the same way as the argument of
11644 @option{--warnings}, but unknown categories are silently ignored.
11645 The command line takes precedence; for instance, if @env{WARNINGS}
11646 is set to @code{obsolete}, but @option{-Wnone} is given on the
11647 command line, no warnings will be issued.
11649 Some categories of warnings are on by default.
11650 Again, for details see @ref{m4_warn}.
11654 Do not use frozen files. Any argument @code{@var{file}.m4f} is
11655 replaced by @code{@var{file}.m4}. This helps tracing the macros which
11656 are executed only when the files are frozen, typically
11657 @code{m4_define}. For instance, running:
11660 autom4te --melt 1.m4 2.m4f 3.m4 4.m4f input.m4
11664 is roughly equivalent to running:
11667 m4 1.m4 2.m4 3.m4 4.m4 input.m4
11674 autom4te 1.m4 2.m4f 3.m4 4.m4f input.m4
11681 m4 --reload-state=4.m4f input.m4
11686 Produce a frozen state file. @command{autom4te} freezing is stricter
11687 than M4's: it must produce no warnings, and no output other than empty
11688 lines (a line with white space is @emph{not} empty) and comments
11689 (starting with @samp{#}). Unlike @command{m4}'s similarly-named option,
11690 this option takes no argument:
11693 autom4te 1.m4 2.m4 3.m4 --freeze --output=3.m4f
11700 m4 1.m4 2.m4 3.m4 --freeze-state=3.m4f
11703 @item --mode=@var{octal-mode}
11704 @itemx -m @var{octal-mode}
11705 Set the mode of the non-traces output to @var{octal-mode}; by default
11711 @cindex @file{autom4te.cache}
11712 As another additional feature over @command{m4}, @command{autom4te}
11713 caches its results. GNU M4 is able to produce a regular
11714 output and traces at the same time. Traces are heavily used in the
11715 GNU Build System: @command{autoheader} uses them to build
11716 @file{config.h.in}, @command{autoreconf} to determine what
11717 GNU Build System components are used, @command{automake} to
11718 ``parse'' @file{configure.ac} etc. To avoid recomputation,
11719 traces are cached while performing regular expansion,
11720 and conversely. This cache is (actually, the caches are) stored in
11721 the directory @file{autom4te.cache}. @emph{It can safely be removed}
11722 at any moment (especially if for some reason @command{autom4te}
11723 considers it trashed).
11726 @item --cache=@var{directory}
11727 @itemx -C @var{directory}
11728 Specify the name of the directory where the result should be cached.
11729 Passing an empty value disables caching. Be sure to pass a relative
11730 file name, as for the time being, global caches are not supported.
11733 Don't cache the results.
11737 If a cache is used, consider it obsolete (but update it anyway).
11742 Because traces are so important to the GNU Build System,
11743 @command{autom4te} provides high level tracing features as compared to
11744 M4, and helps exploiting the cache:
11747 @item --trace=@var{macro}[:@var{format}]
11748 @itemx -t @var{macro}[:@var{format}]
11749 Trace the invocations of @var{macro} according to the @var{format}.
11750 Multiple @option{--trace} arguments can be used to list several macros.
11751 Multiple @option{--trace} arguments for a single macro are not
11752 cumulative; instead, you should just make @var{format} as long as
11755 The @var{format} is a regular string, with newlines if desired, and
11756 several special escape codes. It defaults to @samp{$f:$l:$n:$%}. It can
11757 use the following special escapes:
11761 @c $$ restore font-lock
11762 The character @samp{$}.
11765 The file name from which @var{macro} is called.
11768 The line number from which @var{macro} is called.
11771 The depth of the @var{macro} call. This is an M4 technical detail that
11772 you probably don't want to know about.
11775 The name of the @var{macro}.
11778 The @var{num}th argument of the call to @var{macro}.
11781 @itemx $@var{sep}@@
11782 @itemx $@{@var{separator}@}@@
11783 All the arguments passed to @var{macro}, separated by the character
11784 @var{sep} or the string @var{separator} (@samp{,} by default). Each
11785 argument is quoted, i.e., enclosed in a pair of square brackets.
11789 @itemx $@{@var{separator}@}*
11790 As above, but the arguments are not quoted.
11794 @itemx $@{@var{separator}@}%
11795 As above, but the arguments are not quoted, all new line characters in
11796 the arguments are smashed, and the default separator is @samp{:}.
11798 The escape @samp{$%} produces single-line trace outputs (unless you put
11799 newlines in the @samp{separator}), while @samp{$@@} and @samp{$*} do
11803 @xref{autoconf Invocation}, for examples of trace uses.
11805 @item --preselect=@var{macro}
11806 @itemx -p @var{macro}
11807 Cache the traces of @var{macro}, but do not enable traces. This is
11808 especially important to save CPU cycles in the future. For instance,
11809 when invoked, @command{autoconf} pre-selects all the macros that
11810 @command{autoheader}, @command{automake}, @command{autoreconf}, etc.,
11811 trace, so that running @command{m4} is not needed to trace them: the
11812 cache suffices. This results in a huge speed-up.
11817 @cindex Autom4te Library
11818 Finally, @command{autom4te} introduces the concept of @dfn{Autom4te
11819 libraries}. They consists in a powerful yet extremely simple feature:
11820 sets of combined command line arguments:
11823 @item --language=@var{language}
11824 @itemx -l @var{language}
11825 Use the @var{language} Autom4te library. Current languages include:
11829 create M4sugar output.
11832 create M4sh executable shell scripts.
11835 create Autotest executable test suites.
11837 @item Autoconf-without-aclocal-m4
11838 create Autoconf executable configure scripts without
11839 reading @file{aclocal.m4}.
11842 create Autoconf executable configure scripts. This language inherits
11843 all the characteristics of @code{Autoconf-without-aclocal-m4} and
11844 additionally reads @file{aclocal.m4}.
11847 @item --prepend-include=@var{dir}
11848 @itemx -B @var{dir}
11849 Prepend directory @var{dir} to the search path. This is used to include
11850 the language-specific files before any third-party macros.
11854 @cindex @file{autom4te.cfg}
11855 As an example, if Autoconf is installed in its default location,
11856 @file{/usr/local}, the command @samp{autom4te -l m4sugar foo.m4} is
11857 strictly equivalent to the command:
11860 autom4te --prepend-include /usr/local/share/autoconf \
11861 m4sugar/m4sugar.m4f foo.m4
11865 Recursive expansion applies here: the command @samp{autom4te -l m4sh foo.m4}
11866 is the same as @samp{autom4te --language M4sugar m4sugar/m4sh.m4f
11870 autom4te --prepend-include /usr/local/share/autoconf \
11871 m4sugar/m4sugar.m4f m4sugar/m4sh.m4f --mode 777 foo.m4
11875 The definition of the languages is stored in @file{autom4te.cfg}.
11877 @node Customizing autom4te
11878 @subsection Customizing @command{autom4te}
11880 One can customize @command{autom4te} via @file{~/.autom4te.cfg} (i.e.,
11881 as found in the user home directory), and @file{./.autom4te.cfg} (i.e.,
11882 as found in the directory from which @command{autom4te} is run). The
11883 order is first reading @file{autom4te.cfg}, then @file{~/.autom4te.cfg},
11884 then @file{./.autom4te.cfg}, and finally the command line arguments.
11886 In these text files, comments are introduced with @code{#}, and empty
11887 lines are ignored. Customization is performed on a per-language basis,
11888 wrapped in between a @samp{begin-language: "@var{language}"},
11889 @samp{end-language: "@var{language}"} pair.
11891 Customizing a language stands for appending options (@pxref{autom4te
11892 Invocation}) to the current definition of the language. Options, and
11893 more generally arguments, are introduced by @samp{args:
11894 @var{arguments}}. You may use the traditional shell syntax to quote the
11897 As an example, to disable Autoconf caches (@file{autom4te.cache})
11898 globally, include the following lines in @file{~/.autom4te.cfg}:
11901 ## ------------------ ##
11902 ## User Preferences. ##
11903 ## ------------------ ##
11905 begin-language: "Autoconf-without-aclocal-m4"
11907 end-language: "Autoconf-without-aclocal-m4"
11911 @node Programming in M4sugar
11912 @section Programming in M4sugar
11915 M4 by itself provides only a small, but sufficient, set of all-purpose
11916 macros. M4sugar introduces additional generic macros. Its name was
11917 coined by Lars J. Aas: ``Readability And Greater Understanding Stands 4
11920 M4sugar reserves the macro namespace @samp{^_m4_} for internal use, and
11921 the macro namespace @samp{^m4_} for M4sugar macros. You should not
11922 define your own macros into these namespaces.
11925 * Redefined M4 Macros:: M4 builtins changed in M4sugar
11926 * Diagnostic Macros:: Diagnostic messages from M4sugar
11927 * Diversion support:: Diversions in M4sugar
11928 * Conditional constructs:: Conditions in M4
11929 * Looping constructs:: Iteration in M4
11930 * Evaluation Macros:: More quotation and evaluation control
11931 * Text processing Macros:: String manipulation in M4
11932 * Number processing Macros:: Arithmetic computation in M4
11933 * Set manipulation Macros:: Set manipulation in M4
11934 * Forbidden Patterns:: Catching unexpanded macros
11937 @node Redefined M4 Macros
11938 @subsection Redefined M4 Macros
11941 @msindex{changecom}
11942 @msindex{changequote}
11943 @msindex{debugfile}
11944 @msindex{debugmode}
11965 With a few exceptions, all the M4 native macros are moved in the
11966 @samp{m4_} pseudo-namespace, e.g., M4sugar renames @code{define} as
11967 @code{m4_define} etc.
11969 The list of macros unchanged from M4, except for their name, is:
11973 @item m4_changequote
11998 Some M4 macros are redefined, and are slightly incompatible with their
12005 All M4 macros starting with @samp{__} retain their original name: for
12006 example, no @code{m4__file__} is defined.
12011 This is not technically a macro, but a feature of Autom4te. The
12012 sequence @code{__oline__} can be used similarly to the other m4sugar
12013 location macros, but rather than expanding to the location of the input
12014 file, it is translated to the line number where it appears in the output
12015 file after all other M4 expansions.
12020 This macro kept its original name: no @code{m4_dnl} is defined.
12023 @defmac m4_bpatsubst (@var{string}, @var{regexp}, @ovar{replacement})
12024 @msindex{bpatsubst}
12025 This macro corresponds to @code{patsubst}. The name @code{m4_patsubst}
12026 is kept for future versions of M4sugar, once GNU M4 2.0 is
12027 released and supports extended regular expression syntax.
12030 @defmac m4_bregexp (@var{string}, @var{regexp}, @ovar{replacement})
12032 This macro corresponds to @code{regexp}. The name @code{m4_regexp}
12033 is kept for future versions of M4sugar, once GNU M4 2.0 is
12034 released and supports extended regular expression syntax.
12037 @defmac m4_copy (@var{source}, @var{dest})
12038 @defmacx m4_copy_force (@var{source}, @var{dest})
12039 @defmacx m4_rename (@var{source}, @var{dest})
12040 @defmacx m4_rename_force (@var{source}, @var{dest})
12042 @msindex{copy_force}
12044 @msindex{rename_force}
12045 These macros aren't directly builtins, but are closely related to
12046 @code{m4_pushdef} and @code{m4_defn}. @code{m4_copy} and
12047 @code{m4_rename} ensure that @var{dest} is undefined, while
12048 @code{m4_copy_force} and @code{m4_rename_force} overwrite any existing
12049 definition. All four macros then proceed to copy the entire pushdef
12050 stack of definitions of @var{source} over to @var{dest}. @code{m4_copy}
12051 and @code{m4_copy_force} preserve the source (including in the special
12052 case where @var{source} is undefined), while @code{m4_rename} and
12053 @code{m4_rename_force} undefine the original macro name (making it an
12054 error to rename an undefined @var{source}).
12056 Note that attempting to invoke a renamed macro might not work, since the
12057 macro may have a dependence on helper macros accessed via composition of
12058 @samp{$0} but that were not also renamed; likewise, other macros may
12059 have a hard-coded dependence on @var{source} and could break if
12060 @var{source} has been deleted. On the other hand, it is always safe to
12061 rename a macro to temporarily move it out of the way, then rename it
12062 back later to restore original semantics.
12065 @defmac m4_defn (@var{macro}@dots{})
12067 This macro fails if @var{macro} is not defined, even when using older
12068 versions of M4 that did not warn. See @code{m4_undefine}.
12069 Unfortunately, in order to support these older versions of M4, there are
12070 some situations involving unbalanced quotes where concatenating multiple
12071 macros together will work in newer M4 but not in m4sugar; use
12072 quadrigraphs to work around this.
12075 @defmac m4_divert (@var{diversion})
12077 M4sugar relies heavily on diversions, so rather than behaving as a
12078 primitive, @code{m4_divert} behaves like:
12080 m4_divert_pop()m4_divert_push([@var{diversion}])
12083 @xref{Diversion support}, for more details about the use of the
12084 diversion stack. In particular, this implies that @var{diversion}
12085 should be a named diversion rather than a raw number. But be aware that
12086 it is seldom necessary to explicitly change the diversion stack, and
12087 that when done incorrectly, it can lead to syntactically invalid
12091 @defmac m4_dumpdef (@var{name}@dots{})
12092 @defmacx m4_dumpdefs (@var{name}@dots{})
12095 @code{m4_dumpdef} is like the M4 builtin, except that this version
12096 requires at least one argument, output always goes to standard error
12097 rather than the current debug file, no sorting is done on multiple
12098 arguments, and an error is issued if any
12099 @var{name} is undefined. @code{m4_dumpdefs} is a convenience macro that
12100 calls @code{m4_dumpdef} for all of the
12101 @code{m4_pushdef} stack of definitions, starting with the current, and
12102 silently does nothing if @var{name} is undefined.
12104 Unfortunately, due to a limitation in M4 1.4.x, any macro defined as a
12105 builtin is output as the empty string. This behavior is rectified by
12106 using M4 1.6 or newer. However, this behavior difference means that
12107 @code{m4_dumpdef} should only be used while developing m4sugar macros,
12108 and never in the final published form of a macro.
12111 @defmac m4_esyscmd_s (@var{command})
12112 @msindex{esyscmd_s}
12113 Like @code{m4_esyscmd}, this macro expands to the result of running
12114 @var{command} in a shell. The difference is that any trailing newlines
12115 are removed, so that the output behaves more like shell command
12119 @defmac m4_exit (@var{exit-status})
12121 This macro corresponds to @code{m4exit}.
12124 @defmac m4_if (@var{comment})
12125 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @ovar{not-equal})
12126 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal-1}, @
12127 @var{string-3}, @var{string-4}, @var{equal-2}, @dots{}, @ovar{not-equal})
12129 This macro corresponds to @code{ifelse}. @var{string-1} and
12130 @var{string-2} are compared literally, so usually one of the two
12131 arguments is passed unquoted. @xref{Conditional constructs}, for more
12132 conditional idioms.
12135 @defmac m4_include (@var{file})
12136 @defmacx m4_sinclude (@var{file})
12139 Like the M4 builtins, but warn against multiple inclusions of @var{file}.
12142 @defmac m4_mkstemp (@var{template})
12143 @defmacx m4_maketemp (@var{template})
12146 Posix requires @code{maketemp} to replace the trailing @samp{X}
12147 characters in @var{template} with the process id, without regards to the
12148 existence of a file by that name, but this a security hole. When this
12149 was pointed out to the Posix folks, they agreed to invent a new macro
12150 @code{mkstemp} that always creates a uniquely named file, but not all
12151 versions of GNU M4 support the new macro. In M4sugar,
12152 @code{m4_maketemp} and @code{m4_mkstemp} are synonyms for each other,
12153 and both have the secure semantics regardless of which macro the
12154 underlying M4 provides.
12157 @defmac m4_popdef (@var{macro}@dots{})
12159 This macro fails if @var{macro} is not defined, even when using older
12160 versions of M4 that did not warn. See @code{m4_undefine}.
12163 @defmac m4_undefine (@var{macro}@dots{})
12165 This macro fails if @var{macro} is not defined, even when using older
12166 versions of M4 that did not warn. Use
12169 m4_ifdef([@var{macro}], [m4_undefine([@var{macro}])])
12173 if you are not sure whether @var{macro} is defined.
12176 @defmac m4_undivert (@var{diversion}@dots{})
12178 Unlike the M4 builtin, at least one @var{diversion} must be specified.
12179 Also, since the M4sugar diversion stack prefers named
12180 diversions, the use of @code{m4_undivert} to include files is risky.
12181 @xref{Diversion support}, for more details about the use of the
12182 diversion stack. But be aware that it is seldom necessary to explicitly
12183 change the diversion stack, and that when done incorrectly, it can lead
12184 to syntactically invalid scripts.
12187 @defmac m4_wrap (@var{text})
12188 @defmacx m4_wrap_lifo (@var{text})
12190 @msindex{wrap_lifo}
12191 These macros correspond to @code{m4wrap}. Posix requires arguments of
12192 multiple wrap calls to be reprocessed at EOF in the same order
12193 as the original calls (first-in, first-out). GNU M4 versions
12194 through 1.4.10, however, reprocess them in reverse order (last-in,
12195 first-out). Both orders are useful, therefore, you can rely on
12196 @code{m4_wrap} to provide FIFO semantics and @code{m4_wrap_lifo} for
12197 LIFO semantics, regardless of the underlying GNU M4 version.
12199 Unlike the GNU M4 builtin, these macros only recognize one
12200 argument, and avoid token pasting between consecutive invocations. On
12201 the other hand, nested calls to @code{m4_wrap} from within wrapped text
12202 work just as in the builtin.
12206 @node Diagnostic Macros
12207 @subsection Diagnostic messages from M4sugar
12208 @cindex Messages, from @command{M4sugar}
12210 When macros statically diagnose abnormal situations, benign or fatal,
12211 they should report them using these macros. For issuing dynamic issues,
12212 i.e., when @command{configure} is run, see @ref{Printing Messages}.
12214 @defmac m4_assert (@var{expression}, @dvar{exit-status, 1})
12216 Assert that the arithmetic @var{expression} evaluates to non-zero.
12217 Otherwise, issue a fatal error, and exit @command{autom4te} with
12221 @defmac m4_errprintn (@var{message})
12222 @msindex{errprintn}
12223 Similar to the builtin @code{m4_errprint}, except that a newline is
12224 guaranteed after @var{message}.
12228 @defmac m4_fatal (@var{message})
12230 Report a severe error @var{message} prefixed with the current location,
12231 and have @command{autom4te} die.
12234 @defmac m4_location
12236 Useful as a prefix in a message line. Short for:
12243 @defmac m4_warn (@var{category}, @var{message})
12245 Report @var{message} as a warning (or as an error if requested by the
12246 user) if warnings of the @var{category} are turned on. If the message
12247 is emitted, it is prefixed with the current location, and followed by a
12248 call trace of all macros defined via @code{AC_DEFUN} used to get to the
12251 The @var{category} must be one of:
12255 Warnings about constructs that may interfere with cross-compilation,
12256 such as using @code{AC_RUN_IFELSE} without a default.
12259 Warnings related to the GNU Coding Standards
12260 (@pxref{Top,,, standards, The GNU Coding Standards}).
12264 Warnings about obsolete features. On by default.
12267 Warnings about redefinitions of Autoconf internals.
12270 Warnings about non-portable constructs.
12272 @item portability-recursive
12273 Warnings about recursive Make variable expansions (@code{$(foo$(x))}).
12275 @item extra-portability
12276 Extra warnings about non-portable constructs, covering rarely-used
12280 Warnings about questionable syntactic constructs, incorrectly ordered
12281 macro calls, typos, etc. On by default.
12284 Warnings about unsupported features. On by default.
12287 @strong{Hacking Note:} The set of categories is defined by code in
12288 @command{autom4te}, not by M4sugar itself. Additions should be
12289 coordinated with Automake, so that both sets of tools accept the same
12293 @node Diversion support
12294 @subsection Diversion support
12296 M4sugar makes heavy use of diversions under the hood, because it is
12297 often the case that
12298 text that must appear early in the output is not discovered until late
12299 in the input. Additionally, some of the topological sorting algorithms
12300 used in resolving macro dependencies use diversions. However, most
12301 macros should not need to change diversions directly, but rather rely on
12302 higher-level M4sugar macros to manage diversions transparently. If you
12303 change diversions improperly, you risk generating a syntactically
12304 invalid script, because an incorrect diversion will violate assumptions
12305 made by many macros about whether prerequisite text has been previously
12306 output. In short, if you manually change the diversion, you should not
12307 expect any macros provided by the Autoconf package to work until you
12308 have restored the diversion stack back to its original state.
12310 In the rare case that it is necessary to write a macro that explicitly
12311 outputs text to a different diversion, it is important to be aware of an
12312 M4 limitation regarding diversions: text only goes to a diversion if it
12313 is not part of argument collection. Therefore, any macro that changes
12314 the current diversion cannot be used as an unquoted argument to another
12315 macro, but must be expanded at the top level. The macro
12316 @code{m4_expand} will diagnose any attempt to change diversions, since
12317 it is generally useful only as an argument to another macro. The
12318 following example shows what happens when diversion manipulation is
12319 attempted within macro arguments:
12322 m4_do([normal text]
12323 m4_divert_push([KILL])unwanted[]m4_divert_pop([KILL])
12324 [m4_divert_push([KILL])discarded[]m4_divert_pop([KILL])])dnl
12325 @result{}normal text
12330 Notice that the unquoted text @code{unwanted} is output, even though it
12331 was processed while the current diversion was @code{KILL}, because it
12332 was collected as part of the argument to @code{m4_do}. However, the
12333 text @code{discarded} disappeared as desired, because the diversion
12334 changes were single-quoted, and were not expanded until the top-level
12335 rescan of the output of @code{m4_do}.
12337 To make diversion management easier, M4sugar uses the concept of named
12338 diversions. Rather than using diversion numbers directly, it is nicer
12339 to associate a name with each diversion. The diversion number associated
12340 with a particular diversion name is an implementation detail, and a
12341 syntax warning is issued if a diversion number is used instead of a
12342 name. In general, you should not output text
12343 to a named diversion until after calling the appropriate initialization
12344 routine for your language (@code{m4_init}, @code{AS_INIT},
12345 @code{AT_INIT}, @dots{}), although there are some exceptions documented
12348 M4sugar defines two named diversions.
12351 Text written to this diversion is discarded. This is the default
12352 diversion once M4sugar is initialized.
12354 This diversion is used behind the scenes by topological sorting macros,
12355 such as @code{AC_REQUIRE}.
12358 M4sh adds several more named diversions.
12361 This diversion is reserved for the @samp{#!} interpreter line.
12362 @item HEADER-REVISION
12363 This diversion holds text from @code{AC_REVISION}.
12364 @item HEADER-COMMENT
12365 This diversion holds comments about the purpose of a file.
12366 @item HEADER-COPYRIGHT
12367 This diversion is managed by @code{AC_COPYRIGHT}.
12368 @item M4SH-SANITIZE
12369 This diversion contains M4sh sanitization code, used to ensure M4sh is
12370 executing in a reasonable shell environment.
12372 This diversion contains M4sh initialization code, initializing variables
12373 that are required by other M4sh macros.
12375 This diversion contains the body of the shell code, and is the default
12376 diversion once M4sh is initialized.
12379 Autotest inherits diversions from M4sh, and changes the default
12380 diversion from @code{BODY} back to @code{KILL}. It also adds several
12381 more named diversions, with the following subset designed for developer
12384 @item PREPARE_TESTS
12385 This diversion contains initialization sequences which are executed
12386 after @file{atconfig} and @file{atlocal}, and after all command line
12387 arguments have been parsed, but prior to running any tests. It can be
12388 used to set up state that is required across all tests. This diversion
12389 will work even before @code{AT_INIT}.
12392 Autoconf inherits diversions from M4sh, and adds the following named
12393 diversions which developers can utilize.
12396 This diversion contains shell variable assignments to set defaults that
12397 must be in place before arguments are parsed. This diversion is placed
12398 early enough in @file{configure} that it is unsafe to expand any
12399 autoconf macros into this diversion.
12401 If @code{AC_PRESERVE_HELP_ORDER} was used, then text placed in this
12402 diversion will be included as part of a quoted here-doc providing all of
12403 the @option{--help} output of @file{configure} related to options
12404 created by @code{AC_ARG_WITH} and @code{AC_ARG_ENABLE}.
12406 This diversion occurs after all command line options have been parsed,
12407 but prior to the main body of the @file{configure} script. This
12408 diversion is the last chance to insert shell code such as variable
12409 assignments or shell function declarations that will used by the
12410 expansion of other macros.
12413 For now, the remaining named diversions of Autoconf, Autoheader, and
12414 Autotest are not documented. In other words,
12415 intentionally outputting text into an undocumented diversion is subject
12416 to breakage in a future release of Autoconf.
12418 @defmac m4_cleardivert (@var{diversion}@dots{})
12419 @msindex{cleardivert}
12420 Permanently discard any text that has been diverted into
12424 @defmac m4_divert_once (@var{diversion}, @ovar{content})
12425 @msindex{divert_once}
12426 Similar to @code{m4_divert_text}, except that @var{content} is only
12427 output to @var{diversion} if this is the first time that
12428 @code{m4_divert_once} has been called with its particular arguments.
12431 @defmac m4_divert_pop (@ovar{diversion})
12432 @msindex{divert_pop}
12433 If provided, check that the current diversion is indeed @var{diversion}.
12434 Then change to the diversion located earlier on the stack, giving an
12435 error if an attempt is made to pop beyond the initial m4sugar diversion
12439 @defmac m4_divert_push (@var{diversion})
12440 @msindex{divert_push}
12441 Remember the former diversion on the diversion stack, and output
12442 subsequent text into @var{diversion}. M4sugar maintains a diversion
12443 stack, and issues an error if there is not a matching pop for every
12447 @anchor{m4_divert_text}
12448 @defmac m4_divert_text (@var{diversion}, @ovar{content})
12449 @msindex{divert_text}
12450 Output @var{content} and a newline into @var{diversion}, without
12451 affecting the current diversion. Shorthand for:
12453 m4_divert_push([@var{diversion}])@var{content}
12454 m4_divert_pop([@var{diversion}])dnl
12457 One use of @code{m4_divert_text} is to develop two related macros, where
12458 macro @samp{MY_A} does the work, but adjusts what work is performed
12459 based on whether the optional macro @samp{MY_B} has also been expanded.
12460 Of course, it is possible to use @code{AC_BEFORE} within @code{MY_A} to
12461 require that @samp{MY_B} occurs first, if it occurs at all. But this
12462 imposes an ordering restriction on the user; it would be nicer if macros
12463 @samp{MY_A} and @samp{MY_B} can be invoked in either order. The trick
12464 is to let @samp{MY_B} leave a breadcrumb in an early diversion, which
12465 @samp{MY_A} can then use to determine whether @samp{MY_B} has been
12471 if test -n "$b_was_used"; then
12475 [AC_REQUIRE([MY_A])dnl
12476 m4_divert_text([INIT_PREPARE], [b_was_used=true])])
12483 Initialize the M4sugar environment, setting up the default named
12484 diversion to be @code{KILL}.
12487 @node Conditional constructs
12488 @subsection Conditional constructs
12490 The following macros provide additional conditional constructs as
12491 convenience wrappers around @code{m4_if}.
12493 @defmac m4_bmatch (@var{string}, @var{regex-1}, @var{value-1}, @
12494 @ovar{regex-2}, @ovar{value-2}, @dots{}, @ovar{default})
12496 The string @var{string} is repeatedly compared against a series of
12497 @var{regex} arguments; if a match is found, the expansion is the
12498 corresponding @var{value}, otherwise, the macro moves on to the next
12499 @var{regex}. If no @var{regex} match, then the result is the optional
12500 @var{default}, or nothing.
12503 @defmac m4_bpatsubsts (@var{string}, @var{regex-1}, @var{subst-1}, @
12504 @ovar{regex-2}, @ovar{subst-2}, @dots{})
12505 @msindex{bpatsubsts}
12506 The string @var{string} is altered by @var{regex-1} and @var{subst-1},
12509 m4_bpatsubst([[@var{string}]], [@var{regex}], [@var{subst}])
12513 The result of the substitution is then passed through the next set of
12514 @var{regex} and @var{subst}, and so forth. An empty @var{subst} implies
12515 deletion of any matched portions in the current string. Note that this
12516 macro over-quotes @var{string}; this behavior is intentional, so that
12517 the result of each step of the recursion remains as a quoted string.
12518 However, it means that anchors (@samp{^} and @samp{$} in the @var{regex}
12519 will line up with the extra quotations, and not the characters of the
12520 original string. The overquoting is removed after the final
12524 @defmac m4_case (@var{string}, @var{value-1}, @var{if-value-1}, @
12525 @ovar{value-2}, @ovar{if-value-2}, @dots{}, @ovar{default})
12527 Test @var{string} against multiple @var{value} possibilities, resulting
12528 in the first @var{if-value} for a match, or in the optional
12529 @var{default}. This is shorthand for:
12531 m4_if([@var{string}], [@var{value-1}], [@var{if-value-1}],
12532 [@var{string}], [@var{value-2}], [@var{if-value-2}], @dots{},
12537 @defmac m4_cond (@var{test-1}, @var{value-1}, @var{if-value-1}, @
12538 @ovar{test-2}, @ovar{value-2}, @ovar{if-value-2}, @dots{}, @ovar{default})
12540 This macro was introduced in Autoconf 2.62. Similar to @code{m4_if},
12541 except that each @var{test} is expanded only when it is encountered.
12542 This is useful for short-circuiting expensive tests; while @code{m4_if}
12543 requires all its strings to be expanded up front before doing
12544 comparisons, @code{m4_cond} only expands a @var{test} when all earlier
12547 For an example, these two sequences give the same result, but in the
12548 case where @samp{$1} does not contain a backslash, the @code{m4_cond}
12549 version only expands @code{m4_index} once, instead of five times, for
12550 faster computation if this is a common case for @samp{$1}. Notice that
12551 every third argument is unquoted for @code{m4_if}, and quoted for
12555 m4_if(m4_index([$1], [\]), [-1], [$2],
12556 m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
12557 m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
12558 m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
12559 m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
12561 m4_cond([m4_index([$1], [\])], [-1], [$2],
12562 [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
12563 [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
12564 [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
12565 [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
12570 @defmac m4_default (@var{expr-1}, @var{expr-2})
12571 @defmacx m4_default_quoted (@var{expr-1}, @var{expr-2})
12572 @defmacx m4_default_nblank (@var{expr-1}, @ovar{expr-2})
12573 @defmacx m4_default_nblank_quoted (@var{expr-1}, @ovar{expr-2})
12575 @msindex{default_quoted}
12576 @msindex{default_nblank}
12577 @msindex{default_nblank_quoted}
12578 If @var{expr-1} contains text, use it. Otherwise, select @var{expr-2}.
12579 @code{m4_default} expands the result, while @code{m4_default_quoted}
12580 does not. Useful for providing a fixed default if the expression that
12581 results in @var{expr-1} would otherwise be empty. The difference
12582 between @code{m4_default} and @code{m4_default_nblank} is whether an
12583 argument consisting of just blanks (space, tab, newline) is
12584 significant. When using the expanding versions, note that an argument
12585 may contain text but still expand to an empty string.
12588 m4_define([active], [ACTIVE])dnl
12589 m4_define([empty], [])dnl
12590 m4_define([demo1], [m4_default([$1], [$2])])dnl
12591 m4_define([demo2], [m4_default_quoted([$1], [$2])])dnl
12592 m4_define([demo3], [m4_default_nblank([$1], [$2])])dnl
12593 m4_define([demo4], [m4_default_nblank_quoted([$1], [$2])])dnl
12594 demo1([active], [default])
12596 demo1([], [active])
12598 demo1([empty], [text])
12600 -demo1([ ], [active])-
12602 demo2([active], [default])
12604 demo2([], [active])
12606 demo2([empty], [text])
12608 -demo2([ ], [active])-
12610 demo3([active], [default])
12612 demo3([], [active])
12614 demo3([empty], [text])
12616 -demo3([ ], [active])-
12618 demo4([active], [default])
12620 demo4([], [active])
12622 demo4([empty], [text])
12624 -demo4([ ], [active])-
12629 @defmac m4_define_default (@var{macro}, @ovar{default-definition})
12630 @msindex{define_default}
12631 If @var{macro} does not already have a definition, then define it to
12632 @var{default-definition}.
12635 @defmac m4_ifblank (@var{cond}, @ovar{if-blank}, @ovar{if-text})
12636 @defmacx m4_ifnblank (@var{cond}, @ovar{if-text}, @ovar{if-blank})
12639 If @var{cond} is empty or consists only of blanks (space, tab, newline),
12640 then expand @var{if-blank}; otherwise, expand @var{if-text}. Two
12641 variants exist, in order to make it easier to select the correct logical
12642 sense when using only two parameters. Note that this is more efficient
12643 than the equivalent behavior of:
12645 m4_ifval(m4_normalize([@var{cond}]), @var{if-text}, @var{if-blank})
12649 @defmac m4_ifndef (@var{macro}, @var{if-not-defined}, @ovar{if-defined})
12651 This is shorthand for:
12653 m4_ifdef([@var{macro}], [@var{if-defined}], [@var{if-not-defined}])
12657 @defmac m4_ifset (@var{macro}, @ovar{if-true}, @ovar{if-false})
12659 If @var{macro} is undefined, or is defined as the empty string, expand
12660 to @var{if-false}. Otherwise, expands to @var{if-true}. Similar to:
12662 m4_ifval(m4_defn([@var{macro}]), [@var{if-true}], [@var{if-false}])
12665 except that it is not an error if @var{macro} is undefined.
12668 @defmac m4_ifval (@var{cond}, @ovar{if-true}, @ovar{if-false})
12670 Expands to @var{if-true} if @var{cond} is not empty, otherwise to
12671 @var{if-false}. This is shorthand for:
12673 m4_if([@var{cond}], [], [@var{if-false}], [@var{if-true}])
12677 @defmac m4_ifvaln (@var{cond}, @ovar{if-true}, @ovar{if-false})
12679 Similar to @code{m4_ifval}, except guarantee that a newline is present
12680 after any non-empty expansion. Often followed by @code{dnl}.
12683 @defmac m4_n (@var{text})
12685 Expand to @var{text}, and add a newline if @var{text} is not empty.
12686 Often followed by @code{dnl}.
12690 @node Looping constructs
12691 @subsection Looping constructs
12693 The following macros are useful in implementing recursive algorithms in
12694 M4, including loop operations. An M4 list is formed by quoting a list
12695 of quoted elements; generally the lists are comma-separated, although
12696 @code{m4_foreach_w} is whitespace-separated. For example, the list
12697 @samp{[[a], [b,c]]} contains two elements: @samp{[a]} and @samp{[b,c]}.
12698 It is common to see lists with unquoted elements when those elements are
12699 not likely to be macro names, as in @samp{[fputc_unlocked,
12702 Although not generally recommended, it is possible for quoted lists to
12703 have side effects; all side effects are expanded only once, and prior to
12704 visiting any list element. On the other hand, the fact that unquoted
12705 macros are expanded exactly once means that macros without side effects
12706 can be used to generate lists. For example,
12709 m4_foreach([i], [[1], [2], [3]m4_errprintn([hi])], [i])
12712 m4_define([list], [[1], [2], [3]])
12714 m4_foreach([i], [list], [i])
12718 @defmac m4_argn (@var{n}, @ovar{arg}@dots{})
12720 Extracts argument @var{n} (larger than 0) from the remaining arguments.
12721 If there are too few arguments, the empty string is used. For any
12722 @var{n} besides 1, this is more efficient than the similar
12723 @samp{m4_car(m4_shiftn([@var{n}], [], [@var{arg}@dots{}]))}.
12726 @defmac m4_car (@var{arg}@dots{})
12728 Expands to the quoted first @var{arg}. Can be used with @code{m4_cdr}
12729 to recursively iterate
12730 through a list. Generally, when using quoted lists of quoted elements,
12731 @code{m4_car} should be called without any extra quotes.
12734 @defmac m4_cdr (@var{arg}@dots{})
12736 Expands to a quoted list of all but the first @var{arg}, or the empty
12737 string if there was only one argument. Generally, when using quoted
12738 lists of quoted elements, @code{m4_cdr} should be called without any
12741 For example, this is a simple implementation of @code{m4_map}; note how
12742 each iteration checks for the end of recursion, then merely applies the
12743 first argument to the first element of the list, then repeats with the
12744 rest of the list. (The actual implementation in M4sugar is a bit more
12745 involved, to gain some speed and share code with @code{m4_map_sep}, and
12746 also to avoid expanding side effects in @samp{$2} twice).
12748 m4_define([m4_map], [m4_ifval([$2],
12749 [m4_apply([$1], m4_car($2))[]$0([$1], m4_cdr($2))])])dnl
12750 m4_map([ m4_eval], [[[1]], [[1+1]], [[10],[16]]])
12755 @defmac m4_for (@var{var}, @var{first}, @var{last}, @ovar{step}, @
12758 Loop over the numeric values between @var{first} and @var{last}
12759 including bounds by increments of @var{step}. For each iteration,
12760 expand @var{expression} with the numeric value assigned to @var{var}.
12761 If @var{step} is omitted, it defaults to @samp{1} or @samp{-1} depending
12762 on the order of the limits. If given, @var{step} has to match this
12763 order. The number of iterations is determined independently from
12764 definition of @var{var}; iteration cannot be short-circuited or
12765 lengthened by modifying @var{var} from within @var{expression}.
12768 @defmac m4_foreach (@var{var}, @var{list}, @var{expression})
12770 Loop over the comma-separated M4 list @var{list}, assigning each value
12771 to @var{var}, and expand @var{expression}. The following example
12775 m4_foreach([myvar], [[foo], [bar, baz]],
12779 @result{}echo bar, baz
12782 Note that for some forms of @var{expression}, it may be faster to use
12783 @code{m4_map_args}.
12786 @anchor{m4_foreach_w}
12787 @defmac m4_foreach_w (@var{var}, @var{list}, @var{expression})
12788 @msindex{foreach_w}
12789 Loop over the white-space-separated list @var{list}, assigning each value
12790 to @var{var}, and expand @var{expression}. If @var{var} is only
12791 referenced once in @var{expression}, it is more efficient to use
12792 @code{m4_map_args_w}.
12794 The deprecated macro @code{AC_FOREACH} is an alias of
12795 @code{m4_foreach_w}.
12798 @defmac m4_map (@var{macro}, @var{list})
12799 @defmacx m4_mapall (@var{macro}, @var{list})
12800 @defmacx m4_map_sep (@var{macro}, @var{separator}, @var{list})
12801 @defmacx m4_mapall_sep (@var{macro}, @var{separator}, @var{list})
12805 @msindex{mapall_sep}
12806 Loop over the comma separated quoted list of argument descriptions in
12807 @var{list}, and invoke @var{macro} with the arguments. An argument
12808 description is in turn a comma-separated quoted list of quoted elements,
12809 suitable for @code{m4_apply}. The macros @code{m4_map} and
12810 @code{m4_map_sep} ignore empty argument descriptions, while
12811 @code{m4_mapall} and @code{m4_mapall_sep} invoke @var{macro} with no
12812 arguments. The macros @code{m4_map_sep} and @code{m4_mapall_sep}
12813 additionally expand @var{separator} between invocations of @var{macro}.
12815 Note that @var{separator} is expanded, unlike in @code{m4_join}. When
12816 separating output with commas, this means that the map result can be
12817 used as a series of arguments, by using a single-quoted comma as
12818 @var{separator}, or as a single string, by using a double-quoted comma.
12821 m4_map([m4_count], [])
12823 m4_map([ m4_count], [[],
12827 m4_mapall([ m4_count], [[],
12831 m4_map_sep([m4_eval], [,], [[[1+2]],
12834 m4_map_sep([m4_echo], [,], [[[a]], [[b]]])
12836 m4_count(m4_map_sep([m4_echo], [,], [[[a]], [[b]]]))
12838 m4_map_sep([m4_echo], [[,]], [[[a]], [[b]]])
12840 m4_count(m4_map_sep([m4_echo], [[,]], [[[a]], [[b]]]))
12845 @defmac m4_map_args (@var{macro}, @var{arg}@dots{})
12847 Repeatedly invoke @var{macro} with each successive @var{arg} as its only
12848 argument. In the following example, three solutions are presented with
12849 the same expansion; the solution using @code{m4_map_args} is the most
12852 m4_define([active], [ACTIVE])dnl
12853 m4_foreach([var], [[plain], [active]], [ m4_echo(m4_defn([var]))])
12854 @result{} plain active
12855 m4_map([ m4_echo], [[[plain]], [[active]]])
12856 @result{} plain active
12857 m4_map_args([ m4_echo], [plain], [active])
12858 @result{} plain active
12861 In cases where it is useful to operate on additional parameters besides
12862 the list elements, the macro @code{m4_curry} can be used in @var{macro}
12863 to supply the argument currying necessary to generate the desired
12864 argument list. In the following example, @code{list_add_n} is more
12865 efficient than @code{list_add_x}. On the other hand, using
12866 @code{m4_map_args_sep} can be even more efficient.
12869 m4_define([list], [[1], [2], [3]])dnl
12870 m4_define([add], [m4_eval(([$1]) + ([$2]))])dnl
12871 dnl list_add_n(N, ARG...)
12872 dnl Output a list consisting of each ARG added to N
12873 m4_define([list_add_n],
12874 [m4_shift(m4_map_args([,m4_curry([add], [$1])], m4_shift($@@)))])dnl
12875 list_add_n([1], list)
12877 list_add_n([2], list)
12879 m4_define([list_add_x],
12880 [m4_shift(m4_foreach([var], m4_dquote(m4_shift($@@)),
12881 [,add([$1],m4_defn([var]))]))])dnl
12882 list_add_x([1], list)
12887 @defmac m4_map_args_pair (@var{macro}, @dvarv{macro-end, macro}, @
12889 @msindex{map_args_pair}
12890 For every pair of arguments @var{arg}, invoke @var{macro} with two
12891 arguments. If there is an odd number of arguments, invoke
12892 @var{macro-end}, which defaults to @var{macro}, with the remaining
12896 m4_map_args_pair([, m4_reverse], [], [1], [2], [3])
12898 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3])
12899 @result{}, 2, 1, [3]
12900 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3], [4])
12901 @result{}, 2, 1, 4, 3
12905 @defmac m4_map_args_sep (@ovar{pre}, @ovar{post}, @ovar{sep}, @var{arg}@dots{})
12906 @msindex{map_args_sep}
12907 Expand the sequence @code{@var{pre}[@var{arg}]@var{post}} for each
12908 argument, additionally expanding @var{sep} between arguments. One
12909 common use of this macro is constructing a macro call, where the opening
12910 and closing parentheses are split between @var{pre} and @var{post}; in
12911 particular, @code{m4_map_args([@var{macro}], [@var{arg}])} is equivalent
12912 to @code{m4_map_args_sep([@var{macro}(], [)], [], [@var{arg}])}. This
12913 macro provides the most efficient means for iterating over an arbitrary
12914 list of arguments, particularly when repeatedly constructing a macro
12915 call with more arguments than @var{arg}.
12918 @defmac m4_map_args_w (@var{string}, @ovar{pre}, @ovar{post}, @ovar{sep})
12919 @msindex{map_args_w}
12920 Expand the sequence @code{@var{pre}[word]@var{post}} for each word in
12921 the whitespace-separated @var{string}, additionally expanding @var{sep}
12922 between words. This macro provides the most efficient means for
12923 iterating over a whitespace-separated string. In particular,
12924 @code{m4_map_args_w([@var{string}], [@var{action}(], [)])} is more
12925 efficient than @code{m4_foreach_w([var], [@var{string}],
12926 [@var{action}(m4_defn([var]))])}.
12929 @defmac m4_shiftn (@var{count}, @dots{})
12930 @defmacx m4_shift2 (@dots{})
12931 @defmacx m4_shift3 (@dots{})
12935 @code{m4_shiftn} performs @var{count} iterations of @code{m4_shift},
12936 along with validation that enough arguments were passed in to match the
12937 shift count, and that the count is positive. @code{m4_shift2} and
12938 @code{m4_shift3} are specializations
12939 of @code{m4_shiftn}, introduced in Autoconf 2.62, and are more efficient
12940 for two and three shifts, respectively.
12943 @defmac m4_stack_foreach (@var{macro}, @var{action})
12944 @defmacx m4_stack_foreach_lifo (@var{macro}, @var{action})
12945 @msindex{stack_foreach}
12946 @msindex{stack_foreach_lifo}
12947 For each of the @code{m4_pushdef} definitions of @var{macro}, expand
12948 @var{action} with the single argument of a definition of @var{macro}.
12949 @code{m4_stack_foreach} starts with the oldest definition, while
12950 @code{m4_stack_foreach_lifo} starts with the current definition.
12951 @var{action} should not push or pop definitions of @var{macro}, nor is
12952 there any guarantee that the current definition of @var{macro} matches
12953 the argument that was passed to @var{action}. The macro @code{m4_curry}
12954 can be used if @var{action} needs more than one argument, although in
12955 that case it is more efficient to use @var{m4_stack_foreach_sep}.
12957 Due to technical limitations, there are a few low-level m4sugar
12958 functions, such as @code{m4_pushdef}, that cannot be used as the
12959 @var{macro} argument.
12962 m4_pushdef([a], [1])m4_pushdef([a], [2])dnl
12963 m4_stack_foreach([a], [ m4_incr])
12965 m4_stack_foreach_lifo([a], [ m4_curry([m4_substr], [abcd])])
12970 @defmac m4_stack_foreach_sep (@var{macro}, @ovar{pre}, @ovar{post}, @ovar{sep})
12971 @defmacx m4_stack_foreach_sep_lifo (@var{macro}, @ovar{pre}, @ovar{post}, @
12973 @msindex{stack_foreach_sep}
12974 @msindex{stack_foreach_sep_lifo}
12975 Expand the sequence @code{@var{pre}[definition]@var{post}} for each
12976 @code{m4_pushdef} definition of @var{macro}, additionally expanding
12977 @var{sep} between definitions. @code{m4_stack_foreach_sep} visits the
12978 oldest definition first, while @code{m4_stack_foreach_sep_lifo} visits
12979 the current definition first. This macro provides the most efficient
12980 means for iterating over a pushdef stack. In particular,
12981 @code{m4_stack_foreach([@var{macro}], [@var{action}])} is short for
12982 @code{m4_stack_foreach_sep([@var{macro}], [@var{action}(], [)])}.
12985 @node Evaluation Macros
12986 @subsection Evaluation Macros
12988 The following macros give some control over the order of the evaluation
12989 by adding or removing levels of quotes.
12991 @defmac m4_apply (@var{macro}, @var{list})
12993 Apply the elements of the quoted, comma-separated @var{list} as the
12994 arguments to @var{macro}. If @var{list} is empty, invoke @var{macro}
12995 without arguments. Note the difference between @code{m4_indir}, which
12996 expects its first argument to be a macro name but can use names that are
12997 otherwise invalid, and @code{m4_apply}, where @var{macro} can contain
12998 other text, but must end in a valid macro name.
13000 m4_apply([m4_count], [])
13002 m4_apply([m4_count], [[]])
13004 m4_apply([m4_count], [[1], [2]])
13006 m4_apply([m4_join], [[|], [1], [2]])
13011 @defmac m4_count (@var{arg}, @dots{})
13013 This macro returns the number of arguments it was passed.
13016 @defmac m4_curry (@var{macro}, @var{arg}@dots{})
13018 This macro performs argument currying. The expansion of this macro is
13019 another macro name that expects exactly one argument; that argument is
13020 then appended to the @var{arg} list, and then @var{macro} is expanded
13021 with the resulting argument list.
13024 m4_curry([m4_curry], [m4_reverse], [1])([2])([3])
13028 Unfortunately, due to a limitation in M4 1.4.x, it is not possible to
13029 pass the definition of a builtin macro as the argument to the output of
13030 @code{m4_curry}; the empty string is used instead of the builtin token.
13031 This behavior is rectified by using M4 1.6 or newer.
13034 @defmac m4_do (@var{arg}, @dots{})
13036 This macro loops over its arguments and expands each @var{arg} in
13037 sequence. Its main use is for readability; it allows the use of
13038 indentation and fewer @code{dnl} to result in the same expansion. This
13039 macro guarantees that no expansion will be concatenated with subsequent
13040 text; to achieve full concatenation, use @code{m4_unquote(m4_join([],
13041 @var{arg@dots{}}))}.
13044 m4_define([ab],[1])m4_define([bc],[2])m4_define([abc],[3])dnl
13047 m4_unquote(m4_join([],[a],[b]))c
13049 m4_define([a],[A])m4_define([b],[B])m4_define([c],[C])dnl
13050 m4_define([AB],[4])m4_define([BC],[5])m4_define([ABC],[6])dnl
13053 m4_unquote(m4_join([],[a],[b]))c
13058 @defmac m4_dquote (@var{arg}, @dots{})
13060 Return the arguments as a quoted list of quoted arguments.
13061 Conveniently, if there is just one @var{arg}, this effectively adds a
13065 @defmac m4_dquote_elt (@var{arg}, @dots{})
13066 @msindex{dquote_elt}
13067 Return the arguments as a series of double-quoted arguments. Whereas
13068 @code{m4_dquote} returns a single argument, @code{m4_dquote_elt} returns
13069 as many arguments as it was passed.
13072 @defmac m4_echo (@var{arg}, @dots{})
13074 Return the arguments, with the same level of quoting. Other than
13075 discarding whitespace after unquoted commas, this macro is a no-op.
13078 @defmac m4_expand (@var{arg})
13080 Return the expansion of @var{arg} as a quoted string. Whereas
13081 @code{m4_quote} is designed to collect expanded text into a single
13082 argument, @code{m4_expand} is designed to perform one level of expansion
13083 on quoted text. One distinction is in the treatment of whitespace
13084 following a comma in the original @var{arg}. Any time multiple
13085 arguments are collected into one with @code{m4_quote}, the M4 argument
13086 collection rules discard the whitespace. However, with @code{m4_expand},
13087 whitespace is preserved, even after the expansion of macros contained in
13088 @var{arg}. Additionally, @code{m4_expand} is able to expand text that
13089 would involve an unterminated comment, whereas expanding that same text
13090 as the argument to @code{m4_quote} runs into difficulty in finding the
13091 end of the argument. Since manipulating diversions during argument
13092 collection is inherently unsafe, @code{m4_expand} issues an error if
13093 @var{arg} attempts to change the current diversion (@pxref{Diversion
13097 m4_define([active], [ACT, IVE])dnl
13098 m4_define([active2], [[ACT, IVE]])dnl
13099 m4_quote(active, active)
13100 @result{}ACT,IVE,ACT,IVE
13101 m4_expand([active, active])
13102 @result{}ACT, IVE, ACT, IVE
13103 m4_quote(active2, active2)
13104 @result{}ACT, IVE,ACT, IVE
13105 m4_expand([active2, active2])
13106 @result{}ACT, IVE, ACT, IVE
13107 m4_expand([# m4_echo])
13109 m4_quote(# m4_echo)
13111 @result{}# m4_echo)
13115 Note that @code{m4_expand} cannot handle an @var{arg} that expands to
13116 literal unbalanced quotes, but that quadrigraphs can be used when
13117 unbalanced output is necessary. Likewise, unbalanced parentheses should
13118 be supplied with double quoting or a quadrigraph.
13121 m4_define([pattern], [[!@@<:@@]])dnl
13122 m4_define([bar], [BAR])dnl
13123 m4_expand([case $foo in
13124 m4_defn([pattern])@@:@}@@ bar ;;
13127 @result{}case $foo in
13128 @result{} [![]) BAR ;;
13129 @result{} *) blah ;;
13134 @defmac m4_ignore (@dots{})
13136 This macro was introduced in Autoconf 2.62. Expands to nothing,
13137 ignoring all of its arguments. By itself, this isn't very useful.
13138 However, it can be used to conditionally ignore an arbitrary number of
13139 arguments, by deciding which macro name to apply to a list of arguments.
13141 dnl foo outputs a message only if [debug] is defined.
13143 [m4_ifdef([debug],[AC_MSG_NOTICE],[m4_ignore])([debug message])])
13146 Note that for earlier versions of Autoconf, the macro @code{__gnu__} can
13147 serve the same purpose, although it is less readable.
13150 @defmac m4_make_list (@var{arg}, @dots{})
13151 @msindex{make_list}
13152 This macro exists to aid debugging of M4sugar algorithms. Its net
13153 effect is similar to @code{m4_dquote}---it produces a quoted list of
13154 quoted arguments, for each @var{arg}. The difference is that this
13155 version uses a comma-newline separator instead of just comma, to improve
13156 readability of the list; with the result that it is less efficient than
13159 m4_define([zero],[0])m4_define([one],[1])m4_define([two],[2])dnl
13160 m4_dquote(zero, [one], [[two]])
13161 @result{}[0],[one],[[two]]
13162 m4_make_list(zero, [one], [[two]])
13166 m4_foreach([number], m4_dquote(zero, [one], [[two]]), [ number])
13168 m4_foreach([number], m4_make_list(zero, [one], [[two]]), [ number])
13173 @c m4_noquote is too dangerous to document - it invokes macros that
13174 @c probably rely on @samp{[]} nested quoting for proper operation. The
13175 @c user should generally prefer m4_unquote instead.
13177 @defmac m4_quote (@var{arg}, @dots{})
13179 Return the arguments as a single entity, i.e., wrap them into a pair of
13180 quotes. This effectively collapses multiple arguments into one,
13181 although it loses whitespace after unquoted commas in the process.
13184 @defmac m4_reverse (@var{arg}, @dots{})
13186 Outputs each argument with the same level of quoting, but in reverse
13187 order, and with space following each comma for readability.
13190 m4_define([active], [ACT,IVE])
13192 m4_reverse(active, [active])
13193 @result{}active, IVE, ACT
13197 @defmac m4_unquote (@var{arg}, @dots{})
13199 This macro was introduced in Autoconf 2.62. Expand each argument,
13200 separated by commas. For a single @var{arg}, this effectively removes a
13201 layer of quoting, and @code{m4_unquote([@var{arg}])} is more efficient
13202 than the equivalent @code{m4_do([@var{arg}])}. For multiple arguments,
13203 this results in an unquoted list of expansions. This is commonly used
13204 with @code{m4_split}, in order to convert a single quoted list into a
13205 series of quoted elements.
13208 The following example aims at emphasizing the difference between several
13209 scenarios: not using these macros, using @code{m4_defn}, using
13210 @code{m4_quote}, using @code{m4_dquote}, and using @code{m4_expand}.
13213 $ @kbd{cat example.m4}
13214 dnl Overquote, so that quotes are visible.
13215 m4_define([show], [$[]1 = [$1], $[]@@ = [$@@]])
13216 m4_define([a], [A])
13217 m4_define([mkargs], [1, 2[,] 3])
13218 m4_define([arg1], [[$1]])
13222 show(m4_quote(a, b))
13223 show(m4_dquote(a, b))
13224 show(m4_expand([a, b]))
13228 arg1(m4_defn([mkargs]))
13229 arg1(m4_quote(mkargs))
13230 arg1(m4_dquote(mkargs))
13231 arg1(m4_expand([mkargs]))
13232 $ @kbd{autom4te -l m4sugar example.m4}
13233 $1 = A, $@@ = [A],[b]
13234 $1 = a, b, $@@ = [a, b]
13235 $1 = A,b, $@@ = [A,b]
13236 $1 = [A],[b], $@@ = [[A],[b]]
13237 $1 = A, b, $@@ = [A, b]
13248 @node Text processing Macros
13249 @subsection String manipulation in M4
13251 The following macros may be used to manipulate strings in M4. Many of
13252 the macros in this section intentionally result in quoted strings as
13253 output, rather than subjecting the arguments to further expansions. As
13254 a result, if you are manipulating text that contains active M4
13255 characters, the arguments are passed with single quoting rather than
13258 @defmac m4_append (@var{macro-name}, @var{string}, @ovar{separator})
13259 @defmacx m4_append_uniq (@var{macro-name}, @var{string}, @ovar{separator} @
13260 @ovar{if-uniq}, @ovar{if-duplicate})
13262 @msindex{append_uniq}
13263 Redefine @var{macro-name} to its former contents with @var{separator}
13264 and @var{string} added at the end. If @var{macro-name} was undefined
13265 before (but not if it was defined but empty), then no @var{separator} is
13266 added. As of Autoconf 2.62, neither @var{string} nor @var{separator}
13267 are expanded during this macro; instead, they are expanded when
13268 @var{macro-name} is invoked.
13270 @code{m4_append} can be used to grow strings, and @code{m4_append_uniq}
13271 to grow strings without duplicating substrings. Additionally,
13272 @code{m4_append_uniq} takes two optional parameters as of Autoconf 2.62;
13273 @var{if-uniq} is expanded if @var{string} was appended, and
13274 @var{if-duplicate} is expanded if @var{string} was already present.
13275 Also, @code{m4_append_uniq} warns if @var{separator} is not empty, but
13276 occurs within @var{string}, since that can lead to duplicates.
13278 Note that @code{m4_append} can scale linearly in the length of the final
13279 string, depending on the quality of the underlying M4 implementation,
13280 while @code{m4_append_uniq} has an inherent quadratic scaling factor.
13281 If an algorithm can tolerate duplicates in the final string, use the
13282 former for speed. If duplicates must be avoided, consider using
13283 @code{m4_set_add} instead (@pxref{Set manipulation Macros}).
13286 m4_define([active], [ACTIVE])dnl
13287 m4_append([sentence], [This is an])dnl
13288 m4_append([sentence], [ active ])dnl
13289 m4_append([sentence], [symbol.])dnl
13291 @result{}This is an ACTIVE symbol.
13292 m4_undefine([active])dnl
13293 @result{}This is an active symbol.
13294 m4_append_uniq([list], [one], [, ], [new], [existing])
13296 m4_append_uniq([list], [one], [, ], [new], [existing])
13298 m4_append_uniq([list], [two], [, ], [new], [existing])
13300 m4_append_uniq([list], [three], [, ], [new], [existing])
13302 m4_append_uniq([list], [two], [, ], [new], [existing])
13305 @result{}one, two, three
13307 @result{}[one],[two],[three]
13308 m4_append([list2], [one], [[, ]])dnl
13309 m4_append_uniq([list2], [two], [[, ]])dnl
13310 m4_append([list2], [three], [[, ]])dnl
13312 @result{}one, two, three
13314 @result{}[one, two, three]
13318 @defmac m4_append_uniq_w (@var{macro-name}, @var{strings})
13319 @msindex{append_uniq_w}
13320 This macro was introduced in Autoconf 2.62. It is similar to
13321 @code{m4_append_uniq}, but treats @var{strings} as a whitespace
13322 separated list of words to append, and only appends unique words.
13323 @var{macro-name} is updated with a single space between new words.
13325 m4_append_uniq_w([numbers], [1 1 2])dnl
13326 m4_append_uniq_w([numbers], [ 2 3 ])dnl
13332 @defmac m4_chomp (@var{string})
13333 @defmacx m4_chomp_all (@var{string})
13335 @msindex{chomp_all}
13336 Output @var{string} in quotes, but without a trailing newline. The
13337 macro @code{m4_chomp} is slightly faster, and removes at most one
13338 newline; the macro @code{m4_chomp_all} removes all consecutive trailing
13339 newlines. Unlike @code{m4_flatten}, embedded newlines are left intact,
13340 and backslash does not influence the result.
13343 @defmac m4_combine (@ovar{separator}, @var{prefix-list}, @ovar{infix}, @
13344 @var{suffix-1}, @ovar{suffix-2}, @dots{})
13346 This macro produces a quoted string containing the pairwise combination
13347 of every element of the quoted, comma-separated @var{prefix-list}, and
13348 every element from the @var{suffix} arguments. Each pairwise
13349 combination is joined with @var{infix} in the middle, and successive
13350 pairs are joined by @var{separator}. No expansion occurs on any of the
13351 arguments. No output occurs if either the @var{prefix} or @var{suffix}
13352 list is empty, but the lists can contain empty elements.
13354 m4_define([a], [oops])dnl
13355 m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
13356 @result{}a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
13357 m4_combine([, ], [[a], [b]], [-])
13359 m4_combine([, ], [[a], [b]], [-], [])
13361 m4_combine([, ], [], [-], [1], [2])
13363 m4_combine([, ], [[]], [-], [1], [2])
13368 @defmac m4_escape (@var{string})
13370 Convert all instances of @samp{[}, @samp{]}, @samp{#}, and @samp{$}
13371 within @var{string} into their respective quadrigraphs. The result is
13372 still a quoted string.
13375 @defmac m4_flatten (@var{string})
13377 Flatten @var{string} into a single line. Delete all backslash-newline
13378 pairs, and replace all remaining newlines with a space. The result is
13379 still a quoted string.
13382 @defmac m4_join (@ovar{separator}, @var{args}@dots{})
13383 @defmacx m4_joinall (@ovar{separator}, @var{args}@dots{})
13386 Concatenate each @var{arg}, separated by @var{separator}.
13387 @code{joinall} uses every argument, while @code{join} omits empty
13388 arguments so that there are no back-to-back separators in the output.
13389 The result is a quoted string.
13391 m4_define([active], [ACTIVE])dnl
13392 m4_join([|], [one], [], [active], [two])
13393 @result{}one|active|two
13394 m4_joinall([|], [one], [], [active], [two])
13395 @result{}one||active|two
13398 Note that if all you intend to do is join @var{args} with commas between
13399 them, to form a quoted list suitable for @code{m4_foreach}, it is more
13400 efficient to use @code{m4_dquote}.
13403 @defmac m4_newline (@ovar{text})
13405 This macro was introduced in Autoconf 2.62, and expands to a newline,
13406 followed by any @var{text}.
13407 It is primarily useful for maintaining macro formatting, and ensuring
13408 that M4 does not discard leading whitespace during argument collection.
13411 @defmac m4_normalize (@var{string})
13412 @msindex{normalize}
13413 Remove leading and trailing spaces and tabs, sequences of
13414 backslash-then-newline, and replace multiple spaces, tabs, and newlines
13415 with a single space. This is a combination of @code{m4_flatten} and
13416 @code{m4_strip}. To determine if @var{string} consists only of bytes
13417 that would be removed by @code{m4_normalize}, you can use
13421 @defmac m4_re_escape (@var{string})
13422 @msindex{re_escape}
13423 Backslash-escape all characters in @var{string} that are active in
13427 @c We cannot use @dvar because the macro expansion mistreats backslashes.
13428 @defmac m4_split (@var{string}, @r{[}@var{regexp} = @samp{[\t ]+}@r{]})
13430 Split @var{string} into an M4 list of elements quoted by @samp{[} and
13431 @samp{]}, while keeping white space at the beginning and at the end.
13432 If @var{regexp} is given, use it instead of @samp{[\t ]+} for splitting.
13433 If @var{string} is empty, the result is an empty list.
13436 @defmac m4_strip (@var{string})
13438 Strip whitespace from @var{string}. Sequences of spaces and tabs are
13439 reduced to a single space, then leading and trailing spaces are removed.
13440 The result is still a quoted string. Note that this does not interfere
13441 with newlines; if you want newlines stripped as well, consider
13442 @code{m4_flatten}, or do it all at once with @code{m4_normalize}. To
13443 quickly test if @var{string} has only whitespace, use @code{m4_ifblank}.
13446 @defmac m4_text_box (@var{message}, @dvar{frame, -})
13448 Add a text box around @var{message}, using @var{frame} as the border
13449 character above and below the message. The @var{frame} argument must be
13450 a single byte, and does not support quadrigraphs.
13451 The frame correctly accounts for
13452 the subsequent expansion of @var{message}. For example:
13454 m4_define([macro], [abc])dnl
13455 m4_text_box([macro])
13461 The @var{message} must contain balanced quotes and parentheses, although
13462 quadrigraphs can be used to work around this.
13465 @defmac m4_text_wrap (@var{string}, @ovar{prefix}, @
13466 @dvarv{prefix1, prefix}, @dvar{width, 79})
13467 @msindex{text_wrap}
13468 Break @var{string} into a series of whitespace-separated words, then
13469 output those words separated by spaces, and wrapping lines any time the
13470 output would exceed @var{width} columns. If given, @var{prefix1} begins
13471 the first line, and @var{prefix} begins all wrapped lines. If
13472 @var{prefix1} is longer than @var{prefix}, then the first line consists
13473 of just @var{prefix1}. If @var{prefix} is longer than @var{prefix1},
13474 padding is inserted so that the first word of @var{string} begins at the
13475 same indentation as all wrapped lines. Note that using literal tab
13476 characters in any of the arguments will interfere with the calculation
13477 of width. No expansions occur on @var{prefix}, @var{prefix1}, or the
13478 words of @var{string}, although quadrigraphs are recognized.
13482 m4_text_wrap([Short string */], [ ], [/* ], [20])
13483 @result{}/* Short string */
13484 m4_text_wrap([Much longer string */], [ ], [/* ], [20])
13485 @result{}/* Much longer
13486 @result{} string */
13487 m4_text_wrap([Short doc.], [ ], [ --short ], [30])
13488 @result{} --short Short doc.
13489 m4_text_wrap([Short doc.], [ ], [ --too-wide ], [30])
13490 @result{} --too-wide
13491 @result{} Short doc.
13492 m4_text_wrap([Super long documentation.], [ ],
13493 [ --too-wide ], 30)
13494 @result{} --too-wide
13495 @result{} Super long
13496 @result{} documentation.
13500 @defmac m4_tolower (@var{string})
13501 @defmacx m4_toupper (@var{string})
13504 Return @var{string} with letters converted to upper or lower case,
13508 @node Number processing Macros
13509 @subsection Arithmetic computation in M4
13511 The following macros facilitate integer arithmetic operations.
13513 Where a parameter is documented as taking an arithmetic expression, you
13514 can use anything that can be parsed by @code{m4_eval}.
13515 Any other numeric parameter should consist of an optional sign followed
13516 by one or more decimal digits; it is treated as a decimal integer.
13518 Macros that expand to a number do so as either @samp{0}, or an optional
13519 @samp{-} followed by a nonzero decimal digit followed by zero or more
13522 Due to @command{m4} limitations, arithmetic expressions and numeric
13523 parameters should use only numbers that fit into a 32-bit signed
13526 @defmac m4_cmp (@var{expr-1}, @var{expr-2})
13528 Compare the arithmetic expressions @var{expr-1} and @var{expr-2}, and
13529 expand to @samp{-1} if @var{expr-1} is smaller, @samp{0} if they are
13530 equal, and @samp{1} if @var{expr-1} is larger.
13533 @defmac m4_list_cmp (@var{list-1}, @var{list-2})
13535 Compare the two M4 lists consisting of comma-separated arithmetic
13536 expressions, left to right. Expand to @samp{-1} for the first element
13537 pairing where the value from @var{list-1} is smaller, @samp{1} where the
13538 value from @var{list-2} is smaller, or @samp{0} if both lists have the
13539 same values. If one list is shorter than the other, the remaining
13540 elements of the longer list are compared against zero.
13542 m4_list_cmp([1, 0], [1])
13544 m4_list_cmp([1, [1 * 0]], [1, 0])
13546 m4_list_cmp([1, 2], [1, 0])
13548 m4_list_cmp([1, [1+1], 3],[1, 2])
13550 m4_list_cmp([1, 2, -3], [1, 2])
13552 m4_list_cmp([1, 0], [1, 2])
13554 m4_list_cmp([1], [1, 2])
13559 @defmac m4_max (@var{arg}, @dots{})
13561 This macro was introduced in Autoconf 2.62. Expand to the value
13562 of the maximum arithmetic expression among all the arguments.
13565 @defmac m4_min (@var{arg}, @dots{})
13567 This macro was introduced in Autoconf 2.62. Expand to the value
13568 of the minimum arithmetic expression among all the arguments.
13571 @defmac m4_sign (@var{expr})
13573 Expand to @samp{-1} if the arithmetic expression @var{expr} is negative,
13574 @samp{1} if it is positive, and @samp{0} if it is zero.
13577 @anchor{m4_version_compare}
13578 @defmac m4_version_compare (@var{version-1}, @var{version-2})
13579 @msindex{version_compare}
13580 This macro was introduced in Autoconf 2.53, but had a number of
13581 usability limitations that were not lifted until Autoconf 2.62. Compare
13582 the version strings @var{version-1} and @var{version-2}, and expand to
13583 @samp{-1} if @var{version-1} is smaller, @samp{0} if they are the same,
13584 or @samp{1} @var{version-2} is smaller. Version strings must be a list
13585 of elements separated by @samp{.}, @samp{,} or @samp{-}, where each
13586 element is a number along with optional case-insensitive letters
13587 designating beta releases. The comparison stops at the leftmost element
13588 that contains a difference, although a 0 element compares equal to a
13591 It is permissible to include commit identifiers in @var{version}, such
13592 as an abbreviated SHA1 of the commit, provided there is still a
13593 monotonically increasing prefix to allow for accurate version-based
13594 comparisons. For example, this paragraph was written when the
13595 development snapshot of autoconf claimed to be at version
13596 @samp{2.61a-248-dc51}, or 248 commits after the 2.61a release, with an
13597 abbreviated commit identification of @samp{dc51}.
13600 m4_version_compare([1.1], [2.0])
13602 m4_version_compare([2.0b], [2.0a])
13604 m4_version_compare([1.1.1], [1.1.1a])
13606 m4_version_compare([1.2], [1.1.1a])
13608 m4_version_compare([1.0], [1])
13610 m4_version_compare([1.1pre], [1.1PRE])
13612 m4_version_compare([1.1a], [1,10])
13614 m4_version_compare([2.61a], [2.61a-248-dc51])
13616 m4_version_compare([2.61b], [2.61a-248-dc51])
13621 @defmac m4_version_prereq (@var{version}, @ovar{if-new-enough}, @
13622 @dvar{if-old, m4_fatal})
13623 @msindex{version_prereq}
13624 Compares @var{version} against the version of Autoconf currently
13625 running. If the running version is at @var{version} or newer, expand
13626 @var{if-new-enough}, but if @var{version} is larger than the version
13627 currently executing, expand @var{if-old}, which defaults to printing an
13628 error message and exiting m4sugar with status 63. When given only one
13629 argument, this behaves like @code{AC_PREREQ} (@pxref{Versioning}).
13630 Remember that the autoconf philosophy favors feature checks over version
13634 @node Set manipulation Macros
13635 @subsection Set manipulation in M4
13636 @cindex Set manipulation
13637 @cindex Data structure, set
13638 @cindex Unordered set manipulation
13640 Sometimes, it is necessary to track a set of data, where the order does
13641 not matter and where there are no duplicates in the set. The following
13642 macros facilitate set manipulations. Each set is an opaque object,
13643 which can only be accessed via these basic operations. The underlying
13644 implementation guarantees linear scaling for set creation, which is more
13645 efficient than using the quadratic @code{m4_append_uniq}. Both set
13646 names and values can be arbitrary strings, except for unbalanced quotes.
13647 This implementation ties up memory for removed elements until the next
13648 operation that must traverse all the elements of a set; and although
13649 that may slow down some operations until the memory for removed elements
13650 is pruned, it still guarantees linear performance.
13652 @defmac m4_set_add (@var{set}, @var{value}, @ovar{if-uniq}, @ovar{if-dup})
13654 Adds the string @var{value} as a member of set @var{set}. Expand
13655 @var{if-uniq} if the element was added, or @var{if-dup} if it was
13656 previously in the set. Operates in amortized constant time, so that set
13657 creation scales linearly.
13660 @defmac m4_set_add_all (@var{set}, @var{value}@dots{})
13661 @msindex{set_add_all}
13662 Adds each @var{value} to the set @var{set}. This is slightly more
13663 efficient than repeatedly invoking @code{m4_set_add}.
13666 @defmac m4_set_contains (@var{set}, @var{value}, @ovar{if-present}, @
13668 @msindex{set_contains}
13669 Expands @var{if-present} if the string @var{value} is a member of
13670 @var{set}, otherwise @var{if-absent}.
13673 m4_set_contains([a], [1], [yes], [no])
13675 m4_set_add([a], [1], [added], [dup])
13677 m4_set_add([a], [1], [added], [dup])
13679 m4_set_contains([a], [1], [yes], [no])
13681 m4_set_remove([a], [1], [removed], [missing])
13683 m4_set_contains([a], [1], [yes], [no])
13685 m4_set_remove([a], [1], [removed], [missing])
13690 @defmac m4_set_contents (@var{set}, @ovar{sep})
13691 @defmacx m4_set_dump (@var{set}, @ovar{sep})
13692 @msindex{set_contents}
13694 Expands to a single string consisting of all the members of the set
13695 @var{set}, each separated by @var{sep}, which is not expanded.
13696 @code{m4_set_contents} leaves the elements in @var{set} but reclaims any
13697 memory occupied by removed elements, while @code{m4_set_dump} is a
13698 faster one-shot action that also deletes the set. No provision is made
13699 for disambiguating members that contain a non-empty @var{sep} as a
13700 substring; use @code{m4_set_empty} to distinguish between an empty set
13701 and the set containing only the empty string. The order of the output
13702 is unspecified; in the current implementation, part of the speed of
13703 @code{m4_set_dump} results from using a different output order than
13704 @code{m4_set_contents}. These macros scale linearly in the size of the
13705 set before memory pruning, and @code{m4_set_contents([@var{set}],
13706 [@var{sep}])} is faster than
13707 @code{m4_joinall([@var{sep}]m4_set_listc([@var{set}]))}.
13710 m4_set_add_all([a], [1], [2], [3])
13712 m4_set_contents([a], [-])
13714 m4_joinall([-]m4_set_listc([a]))
13716 m4_set_dump([a], [-])
13718 m4_set_contents([a])
13720 m4_set_add([a], [])
13722 m4_set_contents([a], [-])
13727 @defmac m4_set_delete (@var{set})
13728 @msindex{set_delete}
13729 Delete all elements and memory associated with @var{set}. This is
13730 linear in the set size, and faster than removing one element at a time.
13733 @defmac m4_set_difference (@var{seta}, @var{setb})
13734 @defmacx m4_set_intersection (@var{seta}, @var{setb})
13735 @defmacx m4_set_union (@var{seta}, @var{setb})
13736 @msindex{set_difference}
13737 @msindex{set_intersection}
13738 @msindex{set_union}
13739 Compute the relation between @var{seta} and @var{setb}, and output the
13740 result as a list of quoted arguments without duplicates and with a
13741 leading comma. Set difference selects the elements in @var{seta} but
13742 not @var{setb}, intersection selects only elements in both sets, and
13743 union selects elements in either set. These actions are linear in the
13744 sum of the set sizes. The leading comma is necessary to distinguish
13745 between no elements and the empty string as the only element.
13748 m4_set_add_all([a], [1], [2], [3])
13750 m4_set_add_all([b], [3], [], [4])
13752 m4_set_difference([a], [b])
13754 m4_set_difference([b], [a])
13756 m4_set_intersection([a], [b])
13758 m4_set_union([a], [b])
13763 @defmac m4_set_empty (@var{set}, @ovar{if-empty}, @ovar{if-elements})
13764 @msindex{set_empty}
13765 Expand @var{if-empty} if the set @var{set} has no elements, otherwise
13766 expand @var{if-elements}. This macro operates in constant time. Using
13767 this macro can help disambiguate output from @code{m4_set_contents} or
13768 @code{m4_set_list}.
13771 @defmac m4_set_foreach (@var{set}, @var{variable}, @var{action})
13772 @msindex{set_foreach}
13773 For each element in the set @var{set}, expand @var{action} with the
13774 macro @var{variable} defined as the set element. Behavior is
13775 unspecified if @var{action} recursively lists the contents of @var{set}
13776 (although listing other sets is acceptable), or if it modifies the set
13777 in any way other than removing the element currently contained in
13778 @var{variable}. This macro is faster than the corresponding
13779 @code{m4_foreach([@var{variable}],
13780 m4_indir([m4_dquote]m4_set_listc([@var{set}])), [@var{action}])},
13781 although @code{m4_set_map} might be faster still.
13784 m4_set_add_all([a]m4_for([i], [1], [5], [], [,i]))
13786 m4_set_contents([a])
13788 m4_set_foreach([a], [i],
13789 [m4_if(m4_eval(i&1), [0], [m4_set_remove([a], i, [i])])])
13791 m4_set_contents([a])
13796 @defmac m4_set_list (@var{set})
13797 @defmacx m4_set_listc (@var{set})
13799 @msindex{set_listc}
13800 Produce a list of arguments, where each argument is a quoted element
13801 from the set @var{set}. The variant @code{m4_set_listc} is unambiguous,
13802 by adding a leading comma if there are any set elements, whereas the
13803 variant @code{m4_set_list} cannot distinguish between an empty set and a
13804 set containing only the empty string. These can be directly used in
13805 macros that take multiple arguments, such as @code{m4_join} or
13806 @code{m4_set_add_all}, or wrapped by @code{m4_dquote} for macros that
13807 take a quoted list, such as @code{m4_map} or @code{m4_foreach}. Any
13808 memory occupied by removed elements is reclaimed during these macros.
13811 m4_set_add_all([a], [1], [2], [3])
13819 m4_count(m4_set_list([b]))
13821 m4_set_empty([b], [0], [m4_count(m4_set_list([b]))])
13823 m4_set_add([b], [])
13829 m4_count(m4_set_list([b]))
13831 m4_set_empty([b], [0], [m4_count(m4_set_list([b]))])
13836 @defmac m4_set_map (@var{set}, @var{action})
13838 For each element in the set @var{set}, expand @var{action} with a single
13839 argument of the set element. Behavior is unspecified if @var{action}
13840 recursively lists the contents of @var{set} (although listing other sets
13841 is acceptable), or if it modifies the set in any way other than removing
13842 the element passed as an argument. This macro is faster than either
13843 corresponding counterpart of
13844 @code{m4_map_args([@var{action}]m4_set_listc([@var{set}]))} or
13845 @code{m4_set_foreach([@var{set}], [var],
13846 [@var{action}(m4_defn([var]))])}. It is possible to use @code{m4_curry}
13847 if more than one argument is needed for @var{action}, although it is
13848 more efficient to use @code{m4_set_map_sep} in that case.
13851 @defmac m4_set_map_sep (@var{set}, @ovar{pre}, @ovar{post}, @ovar{sep})
13852 @msindex{set_map_sep}
13853 For each element in the set @var{set}, expand
13854 @code{@var{pre}[element]@var{post}}, additionally expanding @var{sep}
13855 between elements. Behavior is unspecified if the expansion recursively
13856 lists the contents of @var{set} (although listing other sets
13857 is acceptable), or if it modifies the set in any way other than removing
13858 the element visited by the expansion. This macro provides the most
13859 efficient means for non-destructively visiting the elements of a set; in
13860 particular, @code{m4_set_map([@var{set}], [@var{action}])} is equivalent
13861 to @code{m4_set_map_sep([@var{set}], [@var{action}(], [)])}.
13864 @defmac m4_set_remove (@var{set}, @var{value}, @ovar{if-present}, @
13866 @msindex{set_remove}
13867 If @var{value} is an element in the set @var{set}, then remove it and
13868 expand @var{if-present}. Otherwise expand @var{if-absent}. This macro
13869 operates in constant time so that multiple removals will scale linearly
13870 rather than quadratically; but when used outside of
13871 @code{m4_set_foreach} or @code{m4_set_map}, it leaves memory occupied
13872 until the set is later
13873 compacted by @code{m4_set_contents} or @code{m4_set_list}. Several
13874 other set operations are then less efficient between the time of element
13875 removal and subsequent memory compaction, but still maintain their
13876 guaranteed scaling performance.
13879 @defmac m4_set_size (@var{set})
13881 Expand to the size of the set @var{set}. This implementation operates
13882 in constant time, and is thus more efficient than
13883 @code{m4_eval(m4_count(m4_set_listc([set])) - 1)}.
13887 @node Forbidden Patterns
13888 @subsection Forbidden Patterns
13889 @cindex Forbidden patterns
13890 @cindex Patterns, forbidden
13892 M4sugar provides a means to define suspicious patterns, patterns
13893 describing tokens which should not be found in the output. For
13894 instance, if an Autoconf @file{configure} script includes tokens such as
13895 @samp{AC_DEFINE}, or @samp{dnl}, then most probably something went
13896 wrong (typically a macro was not evaluated because of overquotation).
13898 M4sugar forbids all the tokens matching @samp{^_?m4_} and @samp{^dnl$}.
13899 Additional layers, such as M4sh and Autoconf, add additional forbidden
13900 patterns to the list.
13902 @defmac m4_pattern_forbid (@var{pattern})
13903 @msindex{pattern_forbid}
13904 Declare that no token matching @var{pattern} must be found in the
13905 output. The output file is (temporarily) split into one word per line
13906 as part of the @command{autom4te} post-processing, with each line (and
13907 therefore word) then being checked against the Perl regular expression
13908 @var{pattern}. If the regular expression matches, and
13909 @code{m4_pattern_allow} does not also match, then an error is raised.
13911 Comments are not checked; this can be a problem if, for instance, you
13912 have some macro left unexpanded after an @samp{#include}. No consensus
13913 is currently found in the Autoconf community, as some people consider it
13914 should be valid to name macros in comments (which doesn't make sense to
13915 the authors of this documentation: input, such as macros, should be
13916 documented by @samp{dnl} comments; reserving @samp{#}-comments to
13917 document the output).
13919 As an example, if you define your own macros that begin with @samp{M_}
13920 and are composed from capital letters and underscores, the specification
13921 of @code{m4_pattern_forbid([^M_[A-Z_]+])} will ensure all your macros
13922 are expanded when not used in comments.
13924 As an example of a common use of this macro, consider what happens in
13925 packages that want to use the @command{pkg-config} script via the
13926 third-party @code{PKG_CHECK_MODULES} macro. By default, if a developer
13927 checks out the development tree but has not yet installed the pkg-config
13928 macros locally, they can manage to successfully run @command{autoconf}
13929 on the package, but the resulting @file{configure} file will likely
13930 result in a confusing shell message about a syntax error on the line
13931 mentioning the unexpanded @code{PKG_CHECK_MODULES} macro. On the other hand,
13932 if @file{configure.ac} includes @code{m4_pattern_forbid([^PKG_])}, the
13933 missing pkg-config macros will be detected immediately without allowing
13934 @command{autoconf} to succeed.
13937 Of course, you might encounter exceptions to these generic rules, for
13938 instance you might have to refer to @samp{$m4_flags}.
13940 @defmac m4_pattern_allow (@var{pattern})
13941 @msindex{pattern_allow}
13942 Any token matching @var{pattern} is allowed, including if it matches an
13943 @code{m4_pattern_forbid} pattern.
13945 For example, Gnulib uses @code{m4_pattern_forbid([^gl_])} to reserve the
13946 @code{gl_} namespace for itself, but also uses
13947 @code{m4_pattern_allow([^gl_ES$])} to avoid a false negative on the
13951 @node Debugging via autom4te
13952 @section Debugging via autom4te
13953 @cindex debugging tips
13954 @cindex autom4te debugging tips
13955 @cindex m4sugar debugging tips
13956 At times, it is desirable to see what was happening inside m4, to see
13957 why output was not matching expectations. However, post-processing done
13958 by @command{autom4te} means that directly using the m4 builtin
13959 @code{m4_traceon} is likely to interfere with operation. Also, frequent
13960 diversion changes and the concept of forbidden tokens make it difficult
13961 to use @code{m4_defn} to generate inline comments in the final output.
13963 There are a couple of tools to help with this. One is the use of the
13964 @option{--trace} option provided by @command{autom4te} (as well as each
13965 of the programs that wrap @command{autom4te}, such as
13966 @command{autoconf}), in order to inspect when a macro is called and with
13967 which arguments. For example, when this paragraph was written, the
13968 autoconf version could be found by:
13971 $ @kbd{autoconf --trace=AC_INIT}
13972 configure.ac:23:AC_INIT:GNU Autoconf:2.63b.95-3963:bug-autoconf@@gnu.org
13973 $ @kbd{autoconf --trace='AC_INIT:version is $2'}
13974 version is 2.63b.95-3963
13977 Another trick is to print out the expansion of various m4 expressions to
13978 standard error or to an independent file, with no further m4 expansion,
13979 and without interfering with diversion changes or the post-processing
13980 done to standard output. @code{m4_errprintn} shows a given expression
13981 on standard error. For example, if you want to see the expansion of an
13982 autoconf primitive or of one of your autoconf macros, you can do it like
13986 $ @kbd{cat <<\EOF > configure.ac}
13988 m4_errprintn([The definition of AC_DEFINE_UNQUOTED:])
13989 m4_errprintn(m4_defn([AC_DEFINE_UNQUOTED]))
13993 @error{}The definition of AC_DEFINE_UNQUOTED:
13994 @error{}_AC_DEFINE_Q([], $@@)
13997 @node Programming in M4sh
13998 @chapter Programming in M4sh
14000 M4sh, pronounced ``mash'', is aiming at producing portable Bourne shell
14001 scripts. This name was coined by Lars J. Aas, who notes that,
14002 according to the Webster's Revised Unabridged Dictionary (1913):
14005 Mash \Mash\, n. [Akin to G. meisch, maisch, meische, maische, mash,
14006 wash, and prob.@: to AS. miscian to mix. See ``Mix''.]
14010 A mass of mixed ingredients reduced to a soft pulpy state by beating or
14014 A mixture of meal or bran and water fed to animals.
14017 A mess; trouble. [Obs.] --Beau.@: & Fl.
14021 M4sh reserves the M4 macro namespace @samp{^_AS_} for internal use, and
14022 the namespace @samp{^AS_} for M4sh macros. It also reserves the shell
14023 and environment variable namespace @samp{^as_}, and the here-document
14024 delimiter namespace @samp{^_AS[A-Z]} in the output file. You should not
14025 define your own macros or output shell code that conflicts with these
14029 * Common Shell Constructs:: Portability layer for common shell constructs
14030 * Polymorphic Variables:: Support for indirect variable names
14031 * Initialization Macros:: Macros to establish a sane shell environment
14032 * File Descriptor Macros:: File descriptor macros for input and output
14035 @node Common Shell Constructs
14036 @section Common Shell Constructs
14038 M4sh provides portable alternatives for some common shell constructs
14039 that unfortunately are not portable in practice.
14041 @c Deprecated, to be replaced by a better API
14043 @defmac AS_BASENAME (@var{file-name})
14045 Output the non-directory portion of @var{file-name}. For example,
14046 if @code{$file} is @samp{/one/two/three}, the command
14047 @code{base=`AS_BASENAME(["$file"])`} sets @code{base} to @samp{three}.
14051 @defmac AS_BOX (@var{text}, @dvar{char, -})
14053 Expand into shell code that will output @var{text} surrounded by a box
14054 with @var{char} in the top and bottom border. @var{text} should not
14055 contain a newline, but may contain shell expansions valid for unquoted
14056 here-documents. @var{char} defaults to @samp{-}, but can be any
14057 character except @samp{/}, @samp{'}, @samp{"}, @samp{\},
14058 @samp{&}, or @samp{`}. This is useful for outputting a comment box into
14059 log files to separate distinct phases of script operation.
14062 @defmac AS_CASE (@var{word}, @ovar{pattern1}, @ovar{if-matched1}, @
14063 @dots{}, @ovar{default})
14065 Expand into a shell @samp{case} statement, where @var{word} is matched
14066 against one or more patterns. @var{if-matched} is run if the
14067 corresponding pattern matched @var{word}, else @var{default} is run.
14068 @xref{Prerequisite Macros} for why
14069 this macro should be used instead of plain @samp{case} in code
14070 outside of an @code{AC_DEFUN} macro, when the contents of the
14071 @samp{case} use @code{AC_REQUIRE} directly or indirectly.
14072 @xref{case, , Limitations of Shell Builtins},
14073 for how this macro avoids some portability issues.
14074 @xref{Balancing Parentheses}
14075 for how this macro lets you write code with balanced parentheses
14076 even if your code must run on obsolescent shells.
14079 @c Deprecated, to be replaced by a better API
14080 @defmac AS_DIRNAME (@var{file-name})
14082 Output the directory portion of @var{file-name}. For example,
14083 if @code{$file} is @samp{/one/two/three}, the command
14084 @code{dir=`AS_DIRNAME(["$file"])`} sets @code{dir} to @samp{/one/two}.
14086 @code{AS_DIRNAME} was designed long ago when
14087 the @command{dirname} command was not universally supported.
14088 Nowadays one can safely use @code{dir=`dirname -- "$file"`} instead.
14089 This interface may be improved in the future to avoid forks and losing
14093 @defmac AS_ECHO (@var{word})
14095 Emits @var{word} to the standard output, followed by a newline. @var{word}
14096 must be a single shell word (typically a quoted string). The bytes of
14097 @var{word} are output as-is, even if it starts with "-" or contains "\".
14098 Redirections can be placed outside the macro invocation. This is much
14099 more portable than using @command{echo} (@pxref{echo, , Limitations of
14103 @defmac AS_ECHO_N (@var{word})
14105 Emits @var{word} to the standard output, without a following newline.
14106 @var{word} must be a single shell word (typically a quoted string) and,
14107 for portability, should not include more than one newline. The bytes of
14108 @var{word} are output as-is, even if it starts with "-" or contains "\".
14109 Redirections can be placed outside the macro invocation.
14112 @c We cannot use @dvar because the macro expansion mistreats backslashes.
14113 @defmac AS_ESCAPE (@var{string}, @r{[}@var{chars} = @samp{`\"$}@r{]})
14115 Expands to @var{string}, with any characters in @var{chars} escaped with
14116 a backslash (@samp{\}). @var{chars} should be at most four bytes long,
14117 and only contain characters from the set @samp{`\"$}; however,
14118 characters may be safely listed more than once in @var{chars} for the
14119 sake of syntax highlighting editors. The current implementation expands
14120 @var{string} after adding escapes; if @var{string} contains macro calls
14121 that in turn expand to text needing shell quoting, you can use
14122 @code{AS_ESCAPE(m4_dquote(m4_expand([string])))}.
14124 The default for @var{chars} (@samp{\"$`}) is the set of characters
14125 needing escapes when @var{string} will be used literally within double
14126 quotes. One common variant is the set of characters to protect when
14127 @var{string} will be used literally within back-ticks or an unquoted
14128 here-document (@samp{\$`}). Another common variant is @samp{""}, which can
14129 be used to form a double-quoted string containing the same expansions
14130 that would have occurred if @var{string} were expanded in an unquoted
14131 here-document; however, when using this variant, care must be taken that
14132 @var{string} does not use double quotes within complex variable
14133 expansions (such as @samp{$@{foo-`echo "hi"`@}}) that would be broken
14134 with improper escapes.
14136 This macro is often used with @code{AS_ECHO}. For an example, observe
14137 the output generated by the shell code generated from this snippet:
14141 AS_ECHO(["AS_ESCAPE(["$foo" = ])AS_ESCAPE(["$foo"], [""])"])
14142 @result{}"$foo" = "bar"
14143 m4_define([macro], [a, [\b]])
14144 AS_ECHO(["AS_ESCAPE([[macro]])"])
14146 AS_ECHO(["AS_ESCAPE([macro])"])
14148 AS_ECHO(["AS_ESCAPE(m4_dquote(m4_expand([macro])))"])
14152 @comment Should we add AS_ESCAPE_SINGLE? If we do, we can optimize in
14153 @comment the case of @var{string} that does not contain '.
14154 To escape a string that will be placed within single quotes, use:
14157 m4_bpatsubst([[@var{string}]], ['], ['\\''])
14161 @defmac AS_EXECUTABLE_P (@var{file})
14162 @asindex{EXECUTABLE_P}
14163 Emit code to probe whether @var{file} is a regular file with executable
14164 permissions (and not a directory with search permissions). The caller
14165 is responsible for quoting @var{file}.
14168 @defmac AS_EXIT (@dvar{status, $?})
14170 Emit code to exit the shell with @var{status}, defaulting to @samp{$?}.
14172 works around shells that see the exit status of the command prior to
14173 @code{exit} inside a @samp{trap 0} handler (@pxref{trap, , Limitations
14174 of Shell Builtins}).
14177 @defmac AS_IF (@var{test1}, @ovar{run-if-true1}, @dots{}, @ovar{run-if-false})
14179 Run shell code @var{test1}. If @var{test1} exits with a zero status then
14180 run shell code @var{run-if-true1}, else examine further tests. If no test
14181 exits with a zero status, run shell code @var{run-if-false}, with
14182 simplifications if either @var{run-if-true1} or @var{run-if-false}
14183 is empty. For example,
14186 AS_IF([test "x$foo" = xyes], [HANDLE_FOO([yes])],
14187 [test "x$foo" != xno], [HANDLE_FOO([maybe])],
14188 [echo foo not specified])
14192 ensures any required macros of @code{HANDLE_FOO}
14193 are expanded before the first test.
14195 This macro should be used instead of plain @samp{if} in code
14196 outside of an @code{AC_DEFUN} macro, when the contents of the @samp{if}
14197 use @code{AC_REQUIRE} directly or indirectly (@pxref{Prerequisite Macros}).
14200 @defmac AS_MKDIR_P (@var{file-name})
14202 Make the directory @var{file-name}, including intervening directories
14203 as necessary. This is equivalent to @samp{mkdir -p -- @var{file-name}}.
14204 If creation of @var{file-name} fails, exit the script.
14206 Also see the @code{AC_PROG_MKDIR_P} macro (@pxref{Particular Programs}).
14209 @defmac AS_SET_STATUS (@var{status})
14210 @asindex{SET_STATUS}
14211 Emit shell code to set the value of @samp{$?} to @var{status}, as
14212 efficiently as possible. However, this is not guaranteed to abort a
14213 shell running with @code{set -e} (@pxref{set, , Limitations of Shell
14214 Builtins}). This should also be used at the end of a complex shell
14215 function instead of @samp{return} (@pxref{Shell Functions}) to avoid
14219 @defmac AS_TR_CPP (@var{expression})
14221 Transform @var{expression} into a valid right-hand side for a C @code{#define}.
14225 # This outputs "#define HAVE_CHAR_P 1".
14226 # Notice the m4 quoting around #, to prevent an m4 comment
14228 echo "[#]define AS_TR_CPP([HAVE_$type]) 1"
14232 @defmac AS_TR_SH (@var{expression})
14234 Transform @var{expression} into shell code that generates a valid shell
14235 variable name. The result is literal when possible at m4 time, but must
14236 be used with @code{eval} if @var{expression} causes shell indirections.
14240 # This outputs "Have it!".
14241 header="sys/some file.h"
14242 eval AS_TR_SH([HAVE_$header])=yes
14243 if test "x$HAVE_sys_some_file_h" = xyes; then echo "Have it!"; fi
14247 @defmac AS_SET_CATFILE (@var{var}, @var{dir}, @var{file})
14248 @asindex{SET_CATFILE}
14249 Set the polymorphic shell variable @var{var} to @var{dir}/@var{file},
14250 but optimizing the common cases (@var{dir} or @var{file} is @samp{.},
14251 @var{file} is absolute, etc.).
14254 @defmac AS_UNSET (@var{var})
14256 Unsets the shell variable @var{var}, working around bugs in older
14257 shells (@pxref{unset, , Limitations of Shell
14258 Builtins}). @var{var} can be a literal or indirect variable name.
14261 @defmac AS_VERSION_COMPARE (@var{version-1}, @var{version-2}, @
14262 @ovar{action-if-less}, @ovar{action-if-equal}, @ovar{action-if-greater})
14263 @asindex{VERSION_COMPARE}
14264 Compare two strings @var{version-1} and @var{version-2}, possibly
14265 containing shell variables, as version strings, and expand
14266 @var{action-if-less}, @var{action-if-equal}, or @var{action-if-greater}
14267 depending upon the result.
14268 The algorithm to compare is similar to the one used by strverscmp in
14269 glibc (@pxref{String/Array Comparison, , String/Array Comparison, libc,
14270 The GNU C Library}).
14273 @node Polymorphic Variables
14274 @section Support for indirect variable names
14275 @cindex variable name indirection
14276 @cindex polymorphic variable name
14277 @cindex indirection, variable name
14279 Often, it is convenient to write a macro that will emit shell code
14280 operating on a shell variable. The simplest case is when the variable
14281 name is known. But a more powerful idiom is writing shell code that can
14282 work through an indirection, where another variable or command
14283 substitution produces the name of the variable to actually manipulate.
14284 M4sh supports the notion of polymorphic shell variables, making it easy
14285 to write a macro that can deal with either literal or indirect variable
14286 names and output shell code appropriate for both use cases. Behavior is
14287 undefined if expansion of an indirect variable does not result in a
14288 literal variable name.
14290 @defmac AS_LITERAL_IF (@var{expression}, @ovar{if-literal}, @ovar{if-not}, @
14291 @dvarv{if-simple-ref, if-not})
14292 @defmacx AS_LITERAL_WORD_IF (@var{expression}, @ovar{if-literal}, @
14293 @ovar{if-not}, @dvarv{if-simple-ref, if-not})
14294 @asindex{LITERAL_IF}
14295 @asindex{LITERAL_WORD_IF}
14296 If the expansion of @var{expression} is definitely a shell literal,
14297 expand @var{if-literal}. If the expansion of @var{expression} looks
14298 like it might contain shell indirections (such as @code{$var} or
14299 @code{`expr`}), then @var{if-not} is expanded. Sometimes, it is
14300 possible to output optimized code if @var{expression} consists only of
14301 shell variable expansions (such as @code{$@{var@}}), in which case
14302 @var{if-simple-ref} can be provided; but defaulting to @var{if-not}
14303 should always be safe. @code{AS_LITERAL_WORD_IF} only expands
14304 @var{if-literal} if @var{expression} looks like a single shell word,
14305 containing no whitespace; while @code{AS_LITERAL_IF} allows whitespace
14306 in @var{expression}.
14308 In order to reduce the time spent recognizing whether an
14309 @var{expression} qualifies as a literal or a simple indirection, the
14310 implementation is somewhat conservative: @var{expression} must be a
14311 single shell word (possibly after stripping whitespace), consisting only
14312 of bytes that would have the same meaning whether unquoted or enclosed
14313 in double quotes (for example, @samp{a.b} results in @var{if-literal},
14314 even though it is not a valid shell variable name; while both @samp{'a'}
14315 and @samp{[$]} result in @var{if-not}, because they behave differently
14316 than @samp{"'a'"} and @samp{"[$]"}). This macro can be used in contexts
14317 for recognizing portable file names (such as in the implementation of
14318 @code{AC_LIBSOURCE}), or coupled with some transliterations for forming
14319 valid variable names (such as in the implementation of @code{AS_TR_SH},
14320 which uses an additional @code{m4_translit} to convert @samp{.} to
14323 This example shows how to read the contents of the shell variable
14324 @code{bar}, exercising all three arguments to @code{AS_LITERAL_IF}. It
14325 results in a script that will output the line @samp{hello} three times.
14328 AC_DEFUN([MY_ACTION],
14329 [AS_LITERAL_IF([$1],
14332 [AS_VAR_COPY([var], [$1])
14334 [eval 'echo "$'"$1"\"])])
14337 MY_ACTION([`echo bar`])
14342 @defmac AS_VAR_APPEND (@var{var}, @var{text})
14343 @asindex{VAR_APPEND}
14344 Emit shell code to append the shell expansion of @var{text} to the end
14345 of the current contents of the polymorphic shell variable @var{var},
14346 taking advantage of shells that provide the @samp{+=} extension for more
14349 For situations where the final contents of @var{var} are relatively
14350 short (less than 256 bytes), it is more efficient to use the simpler
14351 code sequence of @code{@var{var}=$@{@var{var}@}@var{text}} (or its
14352 polymorphic equivalent of @code{AS_VAR_COPY([t], [@var{var}])} and
14353 @code{AS_VAR_SET([@var{var}], ["$t"@var{text}])}). But in the case
14354 when the script will be repeatedly appending text into @code{var},
14355 issues of scaling start to become apparent. A naive implementation
14356 requires execution time linear to the length of the current contents of
14357 @var{var} as well as the length of @var{text} for a single append, for
14358 an overall quadratic scaling with multiple appends. This macro takes
14359 advantage of shells which provide the extension
14360 @code{@var{var}+=@var{text}}, which can provide amortized constant time
14361 for a single append, for an overall linear scaling with multiple
14362 appends. Note that unlike @code{AS_VAR_SET}, this macro requires that
14363 @var{text} be quoted properly to avoid field splitting and file name
14367 @defmac AS_VAR_ARITH (@var{var}, @var{expression})
14368 @asindex{VAR_ARITH}
14369 Emit shell code to compute the arithmetic expansion of @var{expression},
14370 assigning the result as the contents of the polymorphic shell variable
14371 @var{var}. The code takes advantage of shells that provide @samp{$(())}
14372 for fewer forks, but uses @command{expr} as a fallback. Therefore, the
14373 syntax for a valid @var{expression} is rather limited: all operators
14374 must occur as separate shell arguments and with proper quoting;
14375 the only operators supported are @samp{*}, @samp{/}, @samp{%}, binary
14376 @samp{+}, binary @samp{-}, @samp{>}, @samp{>=}, @samp{<}, @samp{<=},
14377 @samp{!=}, @samp{&}, and @samp{|};
14378 all variables containing numbers must be expanded prior to the computation;
14379 the first shell argument must not start with @samp{-};
14380 and each number must be an optional @samp{-} followed by one or more
14381 decimal digits, where the first digit is nonzero if there is more than
14382 one digit. In the following example, this snippet
14383 will print @samp{(2+3)*4 == 20}.
14387 AS_VAR_ARITH([foo], [\( 2 + $bar \) \* 4])
14388 echo "(2+$bar)*4 == $foo"
14392 @defmac AS_VAR_COPY (@var{dest}, @var{source})
14394 Emit shell code to assign the contents of the polymorphic shell variable
14395 @var{source} to the polymorphic shell variable @var{dest}. For example,
14396 executing this M4sh snippet will output @samp{bar hi}:
14400 AS_VAR_COPY([a], [foo])
14401 AS_VAR_COPY([b], [$foo])
14405 When it is necessary to access the contents of an indirect variable
14406 inside a shell double-quoted context, the recommended idiom is to first
14407 copy the contents into a temporary literal shell variable.
14410 for header in stdint_h inttypes_h ; do
14411 AS_VAR_COPY([var], [ac_cv_header_$header])
14412 echo "$header detected: $var"
14417 @comment AS_VAR_GET is intentionally undocumented; it can't handle
14418 @comment trailing newlines uniformly, and forks too much.
14420 @defmac AS_VAR_IF (@var{var}, @ovar{word}, @ovar{if-equal}, @
14421 @ovar{if-not-equal})
14423 Output a shell conditional statement. If the contents of the
14424 polymorphic shell variable @var{var} match the string @var{word},
14425 execute @var{if-equal}; otherwise execute @var{if-not-equal}. @var{word}
14426 must be a single shell word (typically a quoted string). Avoids
14427 shell bugs if an interrupt signal arrives while a command substitution
14428 in @var{var} is being expanded.
14431 @defmac AS_VAR_PUSHDEF (@var{m4-name}, @var{value})
14432 @defmacx AS_VAR_POPDEF (@var{m4-name})
14433 @asindex{VAR_PUSHDEF}
14434 @asindex{VAR_POPDEF}
14435 @cindex composing variable names
14436 @cindex variable names, composing
14437 A common M4sh idiom involves composing shell variable names from an m4
14438 argument (for example, writing a macro that uses a cache variable).
14439 @var{value} can be an arbitrary string, which will be transliterated
14440 into a valid shell name by @code{AS_TR_SH}. In order to access the
14441 composed variable name based on @var{value}, it is easier to declare a
14442 temporary m4 macro @var{m4-name} with @code{AS_VAR_PUSHDEF}, then use
14443 that macro as the argument to subsequent @code{AS_VAR} macros as a
14444 polymorphic variable name, and finally free the temporary macro with
14445 @code{AS_VAR_POPDEF}. These macros are often followed with @code{dnl},
14446 to avoid excess newlines in the output.
14448 Here is an involved example, that shows the power of writing macros that
14449 can handle composed shell variable names:
14452 m4_define([MY_CHECK_HEADER],
14453 [AS_VAR_PUSHDEF([my_Header], [ac_cv_header_$1])dnl
14454 AS_VAR_IF([my_Header], [yes], [echo "header $1 detected"])dnl
14455 AS_VAR_POPDEF([my_Header])dnl
14457 MY_CHECK_HEADER([stdint.h])
14458 for header in inttypes.h stdlib.h ; do
14459 MY_CHECK_HEADER([$header])
14464 In the above example, @code{MY_CHECK_HEADER} can operate on polymorphic
14465 variable names. In the first invocation, the m4 argument is
14466 @code{stdint.h}, which transliterates into a literal @code{stdint_h}.
14467 As a result, the temporary macro @code{my_Header} expands to the literal
14468 shell name @samp{ac_cv_header_stdint_h}. In the second invocation, the
14469 m4 argument to @code{MY_CHECK_HEADER} is @code{$header}, and the
14470 temporary macro @code{my_Header} expands to the indirect shell name
14471 @samp{$as_my_Header}. During the shell execution of the for loop, when
14472 @samp{$header} contains @samp{inttypes.h}, then @samp{$as_my_Header}
14473 contains @samp{ac_cv_header_inttypes_h}. If this script is then run on a
14474 platform where all three headers have been previously detected, the
14475 output of the script will include:
14478 header stdint.h detected
14479 header inttypes.h detected
14480 header stdlib.h detected
14484 @defmac AS_VAR_SET (@var{var}, @ovar{value})
14486 Emit shell code to assign the contents of the polymorphic shell variable
14487 @var{var} to the shell expansion of @var{value}. @var{value} is not
14488 subject to field splitting or file name expansion, so if command
14489 substitution is used, it may be done with @samp{`""`} rather than using
14490 an intermediate variable (@pxref{Shell Substitutions}). However,
14491 @var{value} does undergo rescanning for additional macro names; behavior
14492 is unspecified if late expansion results in any shell meta-characters.
14495 @defmac AS_VAR_SET_IF (@var{var}, @ovar{if-set}, @ovar{if-undef})
14496 @asindex{VAR_SET_IF}
14497 Emit a shell conditional statement, which executes @var{if-set} if the
14498 polymorphic shell variable @code{var} is set to any value, and
14499 @var{if-undef} otherwise.
14502 @defmac AS_VAR_TEST_SET (@var{var})
14503 @asindex{VAR_TEST_SET}
14504 Emit a shell statement that results in a successful exit status only if
14505 the polymorphic shell variable @code{var} is set.
14508 @node Initialization Macros
14509 @section Initialization Macros
14511 @defmac AS_BOURNE_COMPATIBLE
14512 @asindex{BOURNE_COMPATIBLE}
14513 Set up the shell to be more compatible with the Bourne shell as
14514 standardized by Posix, if possible. This may involve setting
14515 environment variables, or setting options, or similar
14516 implementation-specific actions. This macro is deprecated, since
14517 @code{AS_INIT} already invokes it.
14524 Initialize the M4sh environment. This macro calls @code{m4_init}, then
14525 outputs the @code{#! /bin/sh} line, a notice about where the output was
14526 generated from, and code to sanitize the environment for the rest of the
14527 script. Among other initializations, this sets @env{SHELL} to the shell
14528 chosen to run the script (@pxref{CONFIG_SHELL}), and @env{LC_ALL} to
14529 ensure the C locale. Finally, it changes the current diversion to
14530 @code{BODY}. @code{AS_INIT} is called automatically by @code{AC_INIT}
14531 and @code{AT_INIT}, so shell code in @file{configure},
14532 @file{config.status}, and @file{testsuite} all benefit from a sanitized
14536 @defmac AS_INIT_GENERATED (@var{file}, @ovar{comment})
14537 @asindex{INIT_GENERATED}
14538 Emit shell code to start the creation of a subsidiary shell script in
14539 @var{file}, including changing @var{file} to be executable. This macro
14540 populates the child script with information learned from the parent
14541 (thus, the emitted code is equivalent in effect, but more efficient,
14542 than the code output by @code{AS_INIT}, @code{AS_BOURNE_COMPATIBLE}, and
14543 @code{AS_SHELL_SANITIZE}). If present, @var{comment} is output near the
14544 beginning of the child, prior to the shell initialization code, and is
14545 subject to parameter expansion, command substitution, and backslash
14547 parent script should check the exit status after this macro, in case
14548 @var{file} could not be properly created (for example, if the disk was
14549 full). If successfully created, the parent script can then proceed to
14550 append additional M4sh constructs into the child script.
14552 Note that the child script starts life without a log file open, so if
14553 the parent script uses logging (@pxref{AS_MESSAGE_LOG_FD}), you
14554 must temporarily disable any attempts to use the log file until after
14555 emitting code to open a log within the child. On the other hand, if the
14556 parent script has @code{AS_MESSAGE_FD} redirected somewhere besides
14557 @samp{1}, then the child script already has code that copies stdout to
14558 that descriptor. Currently, the suggested
14559 idiom for writing a M4sh shell script from within another script is:
14562 AS_INIT_GENERATED([@var{file}], [[# My child script.
14563 ]]) || @{ AS_ECHO(["Failed to create child script"]); AS_EXIT; @}
14564 m4_pushdef([AS_MESSAGE_LOG_FD])dnl
14565 cat >> "@var{file}" <<\__EOF__
14566 # Code to initialize AS_MESSAGE_LOG_FD
14567 m4_popdef([AS_MESSAGE_LOG_FD])dnl
14572 This, however, may change in the future as the M4sh interface is
14573 stabilized further.
14575 Also, be aware that use of @env{LINENO} within the child script may
14576 report line numbers relative to their location in the parent script,
14577 even when using @code{AS_LINENO_PREPARE}, if the parent script was
14578 unable to locate a shell with working @env{LINENO} support.
14581 @defmac AS_LINENO_PREPARE
14582 @asindex{LINENO_PREPARE}
14584 Find a shell that supports the special variable @env{LINENO}, which
14585 contains the number of the currently executing line. This macro is
14586 automatically invoked by @code{AC_INIT} in configure scripts.
14589 @defmac AS_ME_PREPARE
14590 @asindex{ME_PREPARE}
14591 Set up variable @env{as_me} to be the basename of the currently executing
14592 script. This macro is automatically invoked by @code{AC_INIT} in
14596 @defmac AS_TMPDIR (@var{prefix}, @dvar{dir, $@{TMPDIR:=/tmp@}})
14600 Create, as safely as possible, a temporary sub-directory within
14601 @var{dir} with a name starting with @var{prefix}. @var{prefix} should
14602 be 2--4 characters, to make it slightly easier to identify the owner of
14603 the directory. If @var{dir} is omitted, then the value of @env{TMPDIR}
14604 will be used (defaulting to @samp{/tmp}). On success, the name of the
14605 newly created directory is stored in the shell variable @code{tmp}. On
14606 error, the script is aborted.
14608 Typically, this macro is coupled with some exit traps to delete the created
14609 directory and its contents on exit or interrupt. However, there is a
14610 slight window between when the directory is created and when the name is
14611 actually known to the shell, so an interrupt at the right moment might
14612 leave the temporary directory behind. Hence it is important to use a
14613 @var{prefix} that makes it easier to determine if a leftover temporary
14614 directory from an interrupted script is safe to delete.
14616 If you set @code{TMPDIR=$tmp} after invoking this macro, you should
14617 reset @code{TMPDIR} before deleting the created directory, to avoid
14618 breaking commands that rely on @code{$TMPDIR}.
14620 The use of the output variable @samp{$tmp} rather than something in the
14621 @samp{as_} namespace is historical; it has the unfortunate consequence
14622 that reusing this otherwise common name for any other purpose inside
14623 your script has the potential to break any cleanup traps designed to
14624 remove the temporary directory.
14627 @defmac AS_SHELL_SANITIZE
14628 @asindex{SHELL_SANITIZE}
14629 Initialize the shell suitably for @command{configure} scripts. This has
14630 the effect of @code{AS_BOURNE_COMPATIBLE}, and sets some other
14631 environment variables for predictable results from configuration tests.
14632 For example, it sets @env{LC_ALL} to change to the default C locale.
14633 @xref{Special Shell Variables}. This macro is deprecated, since
14634 @code{AS_INIT} already invokes it.
14638 @node File Descriptor Macros
14639 @section File Descriptor Macros
14641 @cindex standard input
14642 @cindex file descriptors
14643 @cindex descriptors
14644 @cindex low-level output
14645 @cindex output, low-level
14647 The following macros define file descriptors used to output messages
14648 (or input values) from @file{configure} scripts.
14652 echo "$wombats found" >&AS_MESSAGE_LOG_FD
14653 echo 'Enter desired kangaroo count:' >&AS_MESSAGE_FD
14654 read kangaroos <&AS_ORIGINAL_STDIN_FD`
14658 However doing so is seldom needed, because Autoconf provides higher
14659 level macros as described below.
14661 @defmac AS_MESSAGE_FD
14662 @asindex{MESSAGE_FD}
14663 The file descriptor for @samp{checking for...} messages and results.
14664 By default, @code{AS_INIT} sets this to @samp{1} for standalone M4sh
14665 clients. However, @code{AC_INIT} shuffles things around to another file
14666 descriptor, in order to allow the @option{-q} option of
14667 @command{configure} to choose whether messages should go to the script's
14668 standard output or be discarded.
14670 If you want to display some messages, consider using one of the printing
14671 macros (@pxref{Printing Messages}) instead. Copies of messages output
14672 via these macros are also recorded in @file{config.log}.
14675 @anchor{AS_MESSAGE_LOG_FD}
14676 @defmac AS_MESSAGE_LOG_FD
14677 @asindex{MESSAGE_LOG_FD}
14678 This must either be empty, or expand to a file descriptor for log
14679 messages. By default, @code{AS_INIT} sets this macro to the empty
14680 string for standalone M4sh clients, thus disabling logging. However,
14681 @code{AC_INIT} shuffles things around so that both @command{configure}
14682 and @command{config.status} use @file{config.log} for log messages.
14683 Macros that run tools, like @code{AC_COMPILE_IFELSE} (@pxref{Running the
14684 Compiler}), redirect all output to this descriptor. You may want to do
14685 so if you develop such a low-level macro.
14688 @defmac AS_ORIGINAL_STDIN_FD
14689 @asindex{ORIGINAL_STDIN_FD}
14690 This must expand to a file descriptor for the original standard input.
14691 By default, @code{AS_INIT} sets this macro to @samp{0} for standalone
14692 M4sh clients. However, @code{AC_INIT} shuffles things around for
14695 When @command{configure} runs, it may accidentally execute an
14696 interactive command that has the same name as the non-interactive meant
14697 to be used or checked. If the standard input was the terminal, such
14698 interactive programs would cause @command{configure} to stop, pending
14699 some user input. Therefore @command{configure} redirects its standard
14700 input from @file{/dev/null} during its initialization. This is not
14701 normally a problem, since @command{configure} normally does not need
14704 In the extreme case where your @file{configure} script really needs to
14705 obtain some values from the original standard input, you can read them
14706 explicitly from @code{AS_ORIGINAL_STDIN_FD}.
14710 @c =================================================== Writing Autoconf Macros.
14712 @node Writing Autoconf Macros
14713 @chapter Writing Autoconf Macros
14715 When you write a feature test that could be applicable to more than one
14716 software package, the best thing to do is encapsulate it in a new macro.
14717 Here are some instructions and guidelines for writing Autoconf macros.
14718 You should also familiarize yourself with M4sugar (@pxref{Programming in M4})
14719 and M4sh (@pxref{Programming in M4sh}).
14722 * Macro Definitions:: Basic format of an Autoconf macro
14723 * Macro Names:: What to call your new macros
14724 * Dependencies Between Macros:: What to do when macros depend on other macros
14725 * Obsoleting Macros:: Warning about old ways of doing things
14726 * Coding Style:: Writing Autoconf macros à la Autoconf
14729 @node Macro Definitions
14730 @section Macro Definitions
14732 @defmac AC_DEFUN (@var{name}, @ovar{body})
14734 Autoconf macros are defined using the @code{AC_DEFUN} macro, which is
14735 similar to the M4 builtin @code{m4_define} macro; this creates a macro
14736 named @var{name} and with @var{body} as its expansion. In addition to
14737 defining a macro, @code{AC_DEFUN} adds to it some code that is used to
14738 constrain the order in which macros are called, while avoiding redundant
14739 output (@pxref{Prerequisite Macros}).
14742 An Autoconf macro definition looks like this:
14745 AC_DEFUN(@var{macro-name}, @var{macro-body})
14748 You can refer to any arguments passed to the macro as @samp{$1},
14749 @samp{$2}, etc. @xref{Definitions, , How to define new macros, m4,
14750 GNU M4}, for more complete information on writing M4 macros.
14752 Most macros fall in one of two general categories. The first category
14753 includes macros which take arguments, in order to generate output
14754 parameterized by those arguments. Macros in this category are designed
14755 to be directly expanded, often multiple times, and should not be used as
14756 the argument to @code{AC_REQUIRE}. The other category includes macros
14757 which are shorthand for a fixed block of text, and therefore do not take
14758 arguments. For this category of macros, directly expanding the macro
14759 multiple times results in redundant output, so it is more common to use
14760 the macro as the argument to @code{AC_REQUIRE}, or to declare the macro
14761 with @code{AC_DEFUN_ONCE} (@pxref{One-Shot Macros}).
14763 Be sure to properly quote both the @var{macro-body} @emph{and} the
14764 @var{macro-name} to avoid any problems if the macro happens to have
14765 been previously defined.
14767 Each macro should have a header comment that gives its prototype, and a
14768 brief description. When arguments have default values, display them in
14769 the prototype. For example:
14772 # AC_MSG_ERROR(ERROR, [EXIT-STATUS = 1])
14773 # --------------------------------------
14774 m4_define([AC_MSG_ERROR],
14775 [@{ AS_MESSAGE([error: $1], [2])
14776 exit m4_default([$2], [1]); @}])
14779 Comments about the macro should be left in the header comment. Most
14780 other comments make their way into @file{configure}, so just keep
14781 using @samp{#} to introduce comments.
14784 If you have some special comments about pure M4 code, comments
14785 that make no sense in @file{configure} and in the header comment, then
14786 use the builtin @code{dnl}: it causes M4 to discard the text
14787 through the next newline.
14789 Keep in mind that @code{dnl} is rarely needed to introduce comments;
14790 @code{dnl} is more useful to get rid of the newlines following macros
14791 that produce no output, such as @code{AC_REQUIRE}.
14793 Public third-party macros need to use @code{AC_DEFUN}, and not
14794 @code{m4_define}, in order to be found by @command{aclocal}
14795 (@pxref{Extending aclocal,,, automake, GNU Automake}).
14796 Additionally, if it is ever determined that a macro should be made
14797 obsolete, it is easy to convert from @code{AC_DEFUN} to @code{AU_DEFUN}
14798 in order to have @command{autoupdate} assist the user in choosing a
14799 better alternative, but there is no corresponding way to make
14800 @code{m4_define} issue an upgrade notice (@pxref{AU_DEFUN}).
14802 There is another subtle, but important, difference between using
14803 @code{m4_define} and @code{AC_DEFUN}: only the former is unaffected by
14804 @code{AC_REQUIRE}. When writing a file, it is always safe to replace a
14805 block of text with a @code{m4_define} macro that will expand to the same
14806 text. But replacing a block of text with an @code{AC_DEFUN} macro with
14807 the same content does not necessarily give the same results, because it
14808 changes the location where any embedded but unsatisfied
14809 @code{AC_REQUIRE} invocations within the block will be expanded. For an
14810 example of this, see @ref{Expanded Before Required}.
14813 @section Macro Names
14815 All of the public Autoconf macros have all-uppercase names in the
14816 namespace @samp{^AC_} to prevent them from accidentally conflicting with
14817 other text; Autoconf also reserves the namespace @samp{^_AC_} for
14818 internal macros. All shell variables that they use for internal
14819 purposes have mostly-lowercase names starting with @samp{ac_}. Autoconf
14820 also uses here-document delimiters in the namespace @samp{^_AC[A-Z]}. During
14821 @command{configure}, files produced by Autoconf make heavy use of the
14822 file system namespace @samp{^conf}.
14824 Since Autoconf is built on top of M4sugar (@pxref{Programming in
14825 M4sugar}) and M4sh (@pxref{Programming in M4sh}), you must also be aware
14826 of those namespaces (@samp{^_?\(m4\|AS\)_}). And since
14827 @file{configure.ac} is also designed to be scanned by Autoheader,
14828 Autoscan, Autoupdate, and Automake, you should be aware of the
14829 @samp{^_?A[HNUM]_} namespaces. In general, you @emph{should not use}
14830 the namespace of a package that does not own the macro or shell code you
14833 To ensure that your macros don't conflict with present or future
14834 Autoconf macros, you should prefix your own macro names and any shell
14835 variables they use with some other sequence. Possibilities include your
14836 initials, or an abbreviation for the name of your organization or
14837 software package. Historically, people have not always followed the
14838 rule of using a namespace appropriate for their package, and this has
14839 made it difficult for determining the origin of a macro (and where to
14840 report bugs about that macro), as well as difficult for the true
14841 namespace owner to add new macros without interference from pre-existing
14842 uses of third-party macros. Perhaps the best example of this confusion
14843 is the @code{AM_GNU_GETTEXT} macro, which belongs, not to Automake, but
14846 Most of the Autoconf macros' names follow a structured naming convention
14847 that indicates the kind of feature check by the name. The macro names
14848 consist of several words, separated by underscores, going from most
14849 general to most specific. The names of their cache variables use the
14850 same convention (@pxref{Cache Variable Names}, for more information on
14853 The first word of the name after the namespace initials (such as
14854 @samp{AC_}) usually tells the category
14855 of the feature being tested. Here are the categories used in Autoconf for
14856 specific test macros, the kind of macro that you are more likely to
14857 write. They are also used for cache variables, in all-lowercase. Use
14858 them where applicable; where they're not, invent your own categories.
14862 C language builtin features.
14864 Declarations of C variables in header files.
14866 Functions in libraries.
14868 Posix group owners of files.
14874 The base names of programs.
14876 Members of aggregates.
14878 Operating system features.
14880 C builtin or declared types.
14882 C variables in libraries.
14885 After the category comes the name of the particular feature being
14886 tested. Any further words in the macro name indicate particular aspects
14887 of the feature. For example, @code{AC_PROG_MAKE_SET} checks whether
14888 @command{make} sets a variable to its own name.
14890 An internal macro should have a name that starts with an underscore;
14891 Autoconf internals should therefore start with @samp{_AC_}.
14892 Additionally, a macro that is an internal subroutine of another macro
14893 should have a name that starts with an underscore and the name of that
14894 other macro, followed by one or more words saying what the internal
14895 macro does. For example, @code{AC_PATH_X} has internal macros
14896 @code{_AC_PATH_X_XMKMF} and @code{_AC_PATH_X_DIRECT}.
14898 @node Dependencies Between Macros
14899 @section Dependencies Between Macros
14900 @cindex Dependencies between macros
14902 Some Autoconf macros depend on other macros having been called first in
14903 order to work correctly. Autoconf provides a way to ensure that certain
14904 macros are called if needed and a way to warn the user if macros are
14905 called in an order that might cause incorrect operation.
14908 * Prerequisite Macros:: Ensuring required information
14909 * Suggested Ordering:: Warning about possible ordering problems
14910 * One-Shot Macros:: Ensuring a macro is called only once
14913 @node Prerequisite Macros
14914 @subsection Prerequisite Macros
14915 @cindex Prerequisite macros
14916 @cindex Macros, prerequisites
14918 A macro that you write might need to use values that have previously
14919 been computed by other macros. For example, @code{AC_DECL_YYTEXT}
14920 examines the output of @code{flex} or @code{lex}, so it depends on
14921 @code{AC_PROG_LEX} having been called first to set the shell variable
14924 Rather than forcing the user of the macros to keep track of the
14925 dependencies between them, you can use the @code{AC_REQUIRE} macro to do
14926 it automatically. @code{AC_REQUIRE} can ensure that a macro is only
14927 called if it is needed, and only called once.
14929 @defmac AC_REQUIRE (@var{macro-name})
14931 If the M4 macro @var{macro-name} has not already been called, call it
14932 (without any arguments). Make sure to quote @var{macro-name} with
14933 square brackets. @var{macro-name} must have been defined using
14934 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
14935 that it has been called.
14937 @code{AC_REQUIRE} must be used inside a macro defined by @code{AC_DEFUN}; it
14938 must not be called from the top level. Also, it does not make sense to
14939 require a macro that takes parameters.
14942 @code{AC_REQUIRE} is often misunderstood. It really implements
14943 dependencies between macros in the sense that if one macro depends upon
14944 another, the latter is expanded @emph{before} the body of the
14945 former. To be more precise, the required macro is expanded before
14946 the outermost defined macro in the current expansion stack.
14947 In particular, @samp{AC_REQUIRE([FOO])} is not replaced with the body of
14948 @code{FOO}. For instance, this definition of macros:
14952 AC_DEFUN([TRAVOLTA],
14953 [test "$body_temperature_in_Celsius" -gt 38 &&
14954 dance_floor=occupied])
14955 AC_DEFUN([NEWTON_JOHN],
14956 [test "x$hair_style" = xcurly &&
14957 dance_floor=occupied])
14961 AC_DEFUN([RESERVE_DANCE_FLOOR],
14962 [if test "x`date +%A`" = xSaturday; then
14963 AC_REQUIRE([TRAVOLTA])
14964 AC_REQUIRE([NEWTON_JOHN])
14970 with this @file{configure.ac}
14973 AC_INIT([Dance Manager], [1.0], [bug-dance@@example.org])
14974 RESERVE_DANCE_FLOOR
14975 if test "x$dance_floor" = xoccupied; then
14976 AC_MSG_ERROR([cannot pick up here, let's move])
14981 does not leave you with a better chance to meet a kindred soul on
14982 days other than Saturday, since the call to @code{RESERVE_DANCE_FLOOR}
14987 test "$body_temperature_in_Celsius" -gt 38 &&
14988 dance_floor=occupied
14989 test "x$hair_style" = xcurly &&
14990 dance_floor=occupied
14991 if test "x`date +%A`" = xSaturday; then
14998 This behavior was chosen on purpose: (i) it prevents messages in
14999 required macros from interrupting the messages in the requiring macros;
15000 (ii) it avoids bad surprises when shell conditionals are used, as in:
15005 AC_REQUIRE([SOME_CHECK])
15012 However, this implementation can lead to another class of problems.
15013 Consider the case where an outer macro first expands, then indirectly
15014 requires, an inner macro:
15017 AC_DEFUN([TESTA], [[echo in A
15018 if test -n "$SEEN_A" ; then echo duplicate ; fi
15020 AC_DEFUN([TESTB], [AC_REQUIRE([TESTA])[echo in B
15021 if test -z "$SEEN_A" ; then echo bug ; fi]])
15022 AC_DEFUN([TESTC], [AC_REQUIRE([TESTB])[echo in C]])
15023 AC_DEFUN([OUTER], [[echo in OUTER]
15030 Prior to Autoconf 2.64, the implementation of @code{AC_REQUIRE}
15031 recognized that @code{TESTB} needed to be hoisted prior to the expansion
15032 of @code{OUTER}, but because @code{TESTA} had already been directly
15033 expanded, it failed to hoist @code{TESTA}. Therefore, the expansion of
15034 @code{TESTB} occurs prior to its prerequisites, leading to the following
15046 Newer Autoconf is smart enough to recognize this situation, and hoists
15047 @code{TESTA} even though it has already been expanded, but issues a
15048 syntax warning in the process. This is because the hoisted expansion of
15049 @code{TESTA} defeats the purpose of using @code{AC_REQUIRE} to avoid
15050 redundant code, and causes its own set of problems if the hoisted macro
15062 The bug is not in Autoconf, but in the macro definitions. If you ever
15063 pass a particular macro name to @code{AC_REQUIRE}, then you are implying
15064 that the macro only needs to be expanded once. But to enforce this,
15065 either the macro must be declared with @code{AC_DEFUN_ONCE} (although
15066 this only helps in Autoconf 2.64 or newer), or all
15067 uses of that macro should be through @code{AC_REQUIRE}; directly
15068 expanding the macro defeats the point of using @code{AC_REQUIRE} to
15069 eliminate redundant expansion. In the example, this rule of thumb was
15070 violated because @code{TESTB} requires @code{TESTA} while @code{OUTER}
15071 directly expands it. One way of fixing the bug is to factor
15072 @code{TESTA} into two macros, the portion designed for direct and
15073 repeated use (here, named @code{TESTA}), and the portion designed for
15074 one-shot output and used only inside @code{AC_REQUIRE} (here, named
15075 @code{TESTA_PREREQ}). Then, by fixing all clients to use the correct
15076 calling convention according to their needs:
15079 AC_DEFUN([TESTA], [AC_REQUIRE([TESTA_PREREQ])[echo in A]])
15080 AC_DEFUN([TESTA_PREREQ], [[echo in A_PREREQ
15081 if test -n "$SEEN_A" ; then echo duplicate ; fi
15083 AC_DEFUN([TESTB], [AC_REQUIRE([TESTA_PREREQ])[echo in B
15084 if test -z "$SEEN_A" ; then echo bug ; fi]])
15085 AC_DEFUN([TESTC], [AC_REQUIRE([TESTB])[echo in C]])
15086 AC_DEFUN([OUTER], [[echo in OUTER]
15093 the resulting output will then obey all dependency rules and avoid any
15094 syntax warnings, whether the script is built with old or new Autoconf
15105 You can use the helper macros @code{AS_IF} and @code{AS_CASE} in
15106 top-level code to enforce expansion of required macros outside of shell
15107 conditional constructs; these helpers are not needed in the bodies of
15108 macros defined by @code{AC_DEFUN}.
15109 You are furthermore encouraged, although not required, to
15110 put all @code{AC_REQUIRE} calls
15111 at the beginning of a macro. You can use @code{dnl} to avoid the empty
15114 Autoconf will normally warn if an @code{AC_REQUIRE} call refers to a
15115 macro that has not been defined. However, the @command{aclocal} tool
15116 relies on parsing an incomplete set of input files to trace which macros
15117 have been required, in order to then pull in additional files that
15118 provide those macros; for this particular use case, pre-defining the
15119 macro @code{m4_require_silent_probe} will avoid the warnings.
15121 @node Suggested Ordering
15122 @subsection Suggested Ordering
15123 @cindex Macros, ordering
15124 @cindex Ordering macros
15126 Some macros should be run before another macro if both are called, but
15127 neither @emph{requires} that the other be called. For example, a macro
15128 that changes the behavior of the C compiler should be called before any
15129 macros that run the C compiler. Many of these dependencies are noted in
15132 Autoconf provides the @code{AC_BEFORE} macro to warn users when macros
15133 with this kind of dependency appear out of order in a
15134 @file{configure.ac} file. The warning occurs when creating
15135 @command{configure} from @file{configure.ac}, not when running
15136 @command{configure}.
15138 For example, @code{AC_PROG_CPP} checks whether the C compiler
15139 can run the C preprocessor when given the @option{-E} option. It should
15140 therefore be called after any macros that change which C compiler is
15141 being used, such as @code{AC_PROG_CC}. So @code{AC_PROG_CC} contains:
15144 AC_BEFORE([$0], [AC_PROG_CPP])dnl
15148 This warns the user if a call to @code{AC_PROG_CPP} has already occurred
15149 when @code{AC_PROG_CC} is called.
15151 @defmac AC_BEFORE (@var{this-macro-name}, @var{called-macro-name})
15153 Make M4 print a warning message to the standard error output if
15154 @var{called-macro-name} has already been called. @var{this-macro-name}
15155 should be the name of the macro that is calling @code{AC_BEFORE}. The
15156 macro @var{called-macro-name} must have been defined using
15157 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
15158 that it has been called.
15161 @node One-Shot Macros
15162 @subsection One-Shot Macros
15163 @cindex One-shot macros
15164 @cindex Macros, called once
15166 Some macros should be called only once, either because calling them
15167 multiple time is unsafe, or because it is bad style. For instance
15168 Autoconf ensures that @code{AC_CANONICAL_BUILD} and cousins
15169 (@pxref{Canonicalizing}) are evaluated only once, because it makes no
15170 sense to run these expensive checks more than once. Such one-shot
15171 macros can be defined using @code{AC_DEFUN_ONCE}.
15173 @defmac AC_DEFUN_ONCE (@var{macro-name}, @var{macro-body})
15174 @acindex{DEFUN_ONCE}
15175 Declare macro @var{macro-name} like @code{AC_DEFUN} would (@pxref{Macro
15176 Definitions}), but add additional logic that guarantees that only the
15177 first use of the macro (whether by direct expansion or
15178 @code{AC_REQUIRE}) causes an expansion of @var{macro-body}; the
15179 expansion will occur before the start of any enclosing macro defined by
15180 @code{AC_DEFUN}. Subsequent expansions are silently ignored.
15181 Generally, it does not make sense for @var{macro-body} to use parameters
15185 Prior to Autoconf 2.64, a macro defined by @code{AC_DEFUN_ONCE} would
15186 emit a warning if it was directly expanded a second time, so for
15187 portability, it is better to use @code{AC_REQUIRE} than direct
15188 invocation of @var{macro-name} inside a macro defined by @code{AC_DEFUN}
15189 (@pxref{Prerequisite Macros}).
15191 @node Obsoleting Macros
15192 @section Obsoleting Macros
15193 @cindex Obsoleting macros
15194 @cindex Macros, obsoleting
15196 Configuration and portability technology has evolved over the years.
15197 Often better ways of solving a particular problem are developed, or
15198 ad-hoc approaches are systematized. This process has occurred in many
15199 parts of Autoconf. One result is that some of the macros are now
15200 considered @dfn{obsolete}; they still work, but are no longer considered
15201 the best thing to do, hence they should be replaced with more modern
15202 macros. Ideally, @command{autoupdate} should replace the old macro calls
15203 with their modern implementation.
15205 Autoconf provides a simple means to obsolete a macro.
15208 @defmac AU_DEFUN (@var{old-macro}, @var{implementation}, @ovar{message}, @ovar{silent})
15210 Define @var{old-macro} as @var{implementation}, just like
15211 @code{AC_DEFUN}, but also declare @var{old-macro} to be obsolete.
15212 When @command{autoupdate} is run, occurrences of @var{old-macro} will
15213 be replaced by the text of @var{implementation} in the updated
15214 @file{configure.ac} file.
15216 If a simple textual replacement is not enough to finish the job of
15217 updating a @file{configure.ac} to modern style, provide instructions for
15218 whatever additional manual work is required as @var{message}. These
15219 instructions will be printed by @command{autoupdate}, and embedded in the
15220 updated @file{configure.ac} file, next to the text of @var{implementation}.
15222 Normally, @command{autoconf} will also issue a warning (in the
15223 ``obsolete'' category) when it expands @var{old-macro}. This warning
15224 does not include @var{message}; it only advises the maintainer to run
15225 @command{autoupdate}. If it is inappropriate to issue this warning, set
15226 the @var{silent} argument to the word @code{silent}. One might want to
15227 use a silent @code{AU_DEFUN} when @var{old-macro} is used in a
15228 widely-distributed third-party macro. If that macro's maintainers are
15229 aware of the need to update their code, it's unnecessary to nag all
15230 of the transitive users of @var{old-macro} as well. This capability
15231 was added to @code{AU_DEFUN} in Autoconf 2.70; older versions of
15232 autoconf will ignore the @var{silent} argument and issue the warning
15235 @strong{Caution:} If @var{implementation} contains M4 or M4sugar macros,
15236 they will be evaluated when @command{autoupdate} is run, not emitted
15237 verbatim like the rest of @var{implementation}. This cannot be avoided
15238 with extra quotation, because then @var{old-macro} will not work when
15239 it is called normally. See the definition of @code{AC_FOREACH} in
15240 @file{general.m4} for a workaround.
15243 @defmac AU_ALIAS (@var{old-name}, @var{new-name}, @ovar{silent})
15245 A shorthand version of @code{AU_DEFUN}, to be used when a macro has
15246 simply been renamed. @command{autoupdate} will replace calls to
15247 @var{old-name} with calls to @var{new-name}, keeping any arguments
15248 intact. No instructions for additional manual work will be printed.
15250 The @var{silent} argument works the same as the @var{silent} argument
15251 to @code{AU_DEFUN}. It was added to @code{AU_ALIAS} in Autoconf 2.70.
15253 @strong{Caution:} @code{AU_ALIAS} cannot be used when @var{new-name} is
15254 an M4 or M4sugar macro. See above.
15258 @section Coding Style
15259 @cindex Coding style
15261 The Autoconf macros follow a strict coding style. You are encouraged to
15262 follow this style, especially if you intend to distribute your macro,
15263 either by contributing it to Autoconf itself or the
15264 @uref{https://@/www.gnu.org/@/software/@/autoconf-archive/, Autoconf Macro
15265 Archive}, or by other means.
15267 The first requirement is to pay great attention to the quotation. For
15268 more details, see @ref{Autoconf Language}, and @ref{M4 Quotation}.
15270 Do not try to invent new interfaces. It is likely that there is a macro
15271 in Autoconf that resembles the macro you are defining: try to stick to
15272 this existing interface (order of arguments, default values, etc.). We
15273 @emph{are} conscious that some of these interfaces are not perfect;
15274 nevertheless, when harmless, homogeneity should be preferred over
15277 Be careful about clashes both between M4 symbols and between shell
15280 If you stick to the suggested M4 naming scheme (@pxref{Macro Names}),
15281 you are unlikely to generate conflicts. Nevertheless, when you need to
15282 set a special value, @emph{avoid using a regular macro name}; rather,
15283 use an ``impossible'' name. For instance, up to version 2.13, the macro
15284 @code{AC_SUBST} used to remember what @var{symbol} macros were already defined
15285 by setting @code{AC_SUBST_@var{symbol}}, which is a regular macro name.
15286 But since there is a macro named @code{AC_SUBST_FILE}, it was just
15287 impossible to @samp{AC_SUBST(FILE)}! In this case,
15288 @code{AC_SUBST(@var{symbol})} or @code{_AC_SUBST(@var{symbol})} should
15289 have been used (yes, with the parentheses).
15290 @c or better yet, high-level macros such as @code{m4_expand_once}
15292 No Autoconf macro should ever enter the user-variable name space; i.e.,
15293 except for the variables that are the actual result of running the
15294 macro, all shell variables should start with @code{ac_}. In
15295 addition, small macros or any macro that is likely to be embedded in
15296 other macros should be careful not to use obvious names.
15299 Do not use @code{dnl} to introduce comments: most of the comments you
15300 are likely to write are either header comments which are not output
15301 anyway, or comments that should make their way into @file{configure}.
15302 There are exceptional cases where you do want to comment special M4
15303 constructs, in which case @code{dnl} is right, but keep in mind that it
15306 M4 ignores the leading blanks and newlines before each argument.
15307 Use this feature to
15308 indent in such a way that arguments are (more or less) aligned with the
15309 opening parenthesis of the macro being called. For instance, instead of
15312 AC_CACHE_CHECK(for EMX OS/2 environment,
15314 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, [return __EMX__;])],
15315 [ac_cv_emxos2=yes], [ac_cv_emxos2=no])])
15322 AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
15323 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
15324 [ac_cv_emxos2=yes],
15325 [ac_cv_emxos2=no])])
15332 AC_CACHE_CHECK([for EMX OS/2 environment],
15334 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
15335 [return __EMX__;])],
15336 [ac_cv_emxos2=yes],
15337 [ac_cv_emxos2=no])])
15340 When using @code{AC_RUN_IFELSE} or any macro that cannot work when
15341 cross-compiling, provide a pessimistic value (typically @samp{no}).
15343 Feel free to use various tricks to prevent auxiliary tools, such as
15344 syntax-highlighting editors, from behaving improperly. For instance,
15348 m4_bpatsubst([$1], [$"])
15355 m4_bpatsubst([$1], [$""])
15359 so that Emacsen do not open an endless ``string'' at the first quote.
15360 For the same reasons, avoid:
15370 test $[@@%:@@] != 0
15374 Otherwise, the closing bracket would be hidden inside a @samp{#}-comment,
15375 breaking the bracket-matching highlighting from Emacsen. Note the
15376 preferred style to escape from M4: @samp{$[1]}, @samp{$[@@]}, etc. Do
15377 not escape when it is unnecessary. Common examples of useless quotation
15378 are @samp{[$]$1} (write @samp{$$1}), @samp{[$]var} (use @samp{$var}),
15379 etc. If you add portability issues to the picture, you'll prefer
15380 @samp{$@{1+"$[@@]"@}} to @samp{"[$]@@"}, and you'll prefer do something
15381 better than hacking Autoconf @code{:-)}.
15383 When using @command{sed}, don't use @option{-e} except for indenting
15384 purposes. With the @code{s} and @code{y} commands, the preferred
15385 separator is @samp{/} unless @samp{/} itself might appear in the pattern
15386 or replacement, in which case you should use @samp{|}, or optionally
15387 @samp{,} if you know the pattern and replacement cannot contain a file
15388 name. If none of these characters will do, choose a printable character
15389 that cannot appear in the pattern or replacement. Characters from the
15390 set @samp{"#$&'()*;<=>?`|~} are good choices if the pattern or
15391 replacement might contain a file name, since they have special meaning
15392 to the shell and are less likely to occur in file names.
15394 @xref{Macro Definitions}, for details on how to define a macro. If a
15395 macro doesn't use @code{AC_REQUIRE}, is expected to never be the object
15396 of an @code{AC_REQUIRE} directive, and macros required by other macros
15397 inside arguments do not need to be expanded before this macro, then
15398 use @code{m4_define}. In case of doubt, use @code{AC_DEFUN}.
15399 Also take into account that public third-party macros need to use
15400 @code{AC_DEFUN} in order to be found by @command{aclocal}
15401 (@pxref{Extending aclocal,,, automake, GNU Automake}).
15402 All the @code{AC_REQUIRE} statements should be at the beginning of the
15403 macro, and each statement should be followed by @code{dnl}.
15405 You should not rely on the number of arguments: instead of checking
15406 whether an argument is missing, test that it is not empty. It provides
15407 both a simpler and a more predictable interface to the user, and saves
15408 room for further arguments.
15410 Unless the macro is short, try to leave the closing @samp{])} at the
15411 beginning of a line, followed by a comment that repeats the name of the
15412 macro being defined. This introduces an additional newline in
15413 @command{configure}; normally, that is not a problem, but if you want to
15414 remove it you can use @samp{[]dnl} on the last line. You can similarly
15415 use @samp{[]dnl} after a macro call to remove its newline. @samp{[]dnl}
15416 is recommended instead of @samp{dnl} to ensure that M4 does not
15417 interpret the @samp{dnl} as being attached to the preceding text or
15418 macro output. For example, instead of:
15421 AC_DEFUN([AC_PATH_X],
15422 [AC_MSG_CHECKING([for X])
15424 @r{# @dots{}omitted@dots{}}
15425 AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
15433 AC_DEFUN([AC_PATH_X],
15434 [AC_REQUIRE_CPP()[]dnl
15435 AC_MSG_CHECKING([for X])
15436 @r{# @dots{}omitted@dots{}}
15437 AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
15442 If the macro is long, try to split it into logical chunks. Typically,
15443 macros that check for a bug in a function and prepare its
15444 @code{AC_LIBOBJ} replacement should have an auxiliary macro to perform
15445 this setup. Do not hesitate to introduce auxiliary macros to factor
15448 In order to highlight the recommended coding style, here is a macro
15449 written the old way:
15452 dnl Check for EMX on OS/2.
15454 AC_DEFUN(_AC_EMXOS2,
15455 [AC_CACHE_CHECK(for EMX OS/2 environment, ac_cv_emxos2,
15456 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, return __EMX__;)],
15457 ac_cv_emxos2=yes, ac_cv_emxos2=no)])
15458 test "x$ac_cv_emxos2" = xyes && EMXOS2=yes])
15467 # Check for EMX on OS/2.
15468 m4_define([_AC_EMXOS2],
15469 [AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
15470 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
15471 [ac_cv_emxos2=yes],
15472 [ac_cv_emxos2=no])])
15473 test "x$ac_cv_emxos2" = xyes && EMXOS2=yes[]dnl
15480 @c ============================================= Portable Shell Programming
15482 @node Portable Shell
15483 @chapter Portable Shell Programming
15484 @cindex Portable shell programming
15486 When writing your own checks, there are some shell-script programming
15487 techniques you should avoid in order to make your code portable. The
15488 Bourne shell and upward-compatible shells like the Korn shell and Bash
15489 have evolved over the years, and many features added to the original
15490 System7 shell are now supported on all interesting porting targets.
15491 However, the following discussion between Russ Allbery and Robert Lipe
15498 The GNU assumption that @command{/bin/sh} is the one and only shell
15499 leads to a permanent deadlock. Vendors don't want to break users'
15500 existing shell scripts, and there are some corner cases in the Bourne
15501 shell that are not completely compatible with a Posix shell. Thus,
15502 vendors who have taken this route will @emph{never} (OK@dots{}``never say
15503 never'') replace the Bourne shell (as @command{/bin/sh}) with a
15511 This is exactly the problem. While most (at least most System V's) do
15512 have a Bourne shell that accepts shell functions most vendor
15513 @command{/bin/sh} programs are not the Posix shell.
15515 So while most modern systems do have a shell @emph{somewhere} that meets the
15516 Posix standard, the challenge is to find it.
15519 For this reason, part of the job of M4sh (@pxref{Programming in M4sh})
15520 is to find such a shell. But to prevent trouble, if you're not using
15521 M4sh you should not take advantage of features that were added after Unix
15522 version 7, circa 1977 (@pxref{Systemology}); you should not use aliases,
15523 negated character classes, or even @command{unset}. @code{#} comments,
15524 while not in Unix version 7, were retrofitted in the original Bourne
15525 shell and can be assumed to be part of the least common denominator.
15527 On the other hand, if you're using M4sh you can assume that the shell
15528 has the features that were added in SVR2 (circa 1984), including shell
15530 @command{return}, @command{unset}, and I/O redirection for builtins. For
15531 more information, refer to @uref{https://@/www.in-ulm.de/@/~mascheck/@/bourne/}.
15532 However, some pitfalls have to be avoided for portable use of these
15533 constructs; these will be documented in the rest of this chapter.
15534 See in particular @ref{Shell Functions} and @ref{Limitations of
15535 Builtins, , Limitations of Shell Builtins}.
15537 The set of external programs you should run in a @command{configure} script
15538 is fairly small. @xref{Utilities in Makefiles, , Utilities in
15539 Makefiles, standards, The GNU Coding Standards}, for the list. This
15540 restriction allows users to start out with a fairly small set of
15541 programs and build the rest, avoiding too many interdependencies between
15544 Some of these external utilities have a portable subset of features; see
15545 @ref{Limitations of Usual Tools}.
15547 There are other sources of documentation about shells. The
15548 specification for the Posix
15549 @uref{https://@/pubs.opengroup.org/@/onlinepubs/@/9699919799/@/utilities/@/V3_chap02.html,
15550 Shell Command Language}, though more generous than the restrictive shell
15551 subset described above, is fairly portable nowadays. Also please see
15552 @uref{http://@/www.faqs.org/@/faqs/@/unix-faq/@/shell/, the Shell FAQs}.
15555 * Shellology:: A zoology of shells
15556 * Invoking the Shell:: Invoking the shell as a command
15557 * Here-Documents:: Quirks and tricks
15558 * File Descriptors:: FDs and redirections
15559 * Signal Handling:: Shells, signals, and headaches
15560 * File System Conventions:: File names
15561 * Shell Pattern Matching:: Pattern matching
15562 * Shell Substitutions:: Variable and command expansions
15563 * Assignments:: Varying side effects of assignments
15564 * Parentheses:: Parentheses in shell scripts
15565 * Slashes:: Slashes in shell scripts
15566 * Special Shell Variables:: Variables you should not change
15567 * Shell Functions:: What to look out for if you use them
15568 * Limitations of Builtins:: Portable use of not so portable /bin/sh
15569 * Limitations of Usual Tools:: Portable use of portable tools
15573 @section Shellology
15576 There are several families of shells, most prominently the Bourne family
15577 and the C shell family which are deeply incompatible. If you want to
15578 write portable shell scripts, avoid members of the C shell family. The
15579 @uref{http://@/www.faqs.org/@/faqs/@/unix-faq/@/shell/@/shell-differences/, the
15580 Shell difference FAQ} includes a small history of Posix shells, and a
15581 comparison between several of them.
15583 Below we describe some of the members of the Bourne shell family.
15588 Ash is often used on GNU/Linux and BSD
15589 systems as a light-weight Bourne-compatible shell. Ash 0.2 has some
15590 bugs that are fixed in the 0.3.x series, but portable shell scripts
15591 should work around them, since version 0.2 is still shipped with many
15592 GNU/Linux distributions.
15594 To be compatible with Ash 0.2:
15598 don't use @samp{$?} after expanding empty or unset variables,
15599 or at the start of an @command{eval}:
15605 echo "Do not use it: $?"
15607 eval 'echo "Do not use it: $?"'
15611 don't use command substitution within variable expansion:
15618 beware that single builtin substitutions are not performed by a
15619 subshell, hence their effect applies to the current shell! @xref{Shell
15620 Substitutions}, item ``Command Substitution''.
15625 To detect whether you are running Bash, test whether
15626 @code{BASH_VERSION} is set. To require
15627 Posix compatibility, run @samp{set -o posix}. @xref{Bash POSIX
15628 Mode, , Bash Posix Mode, bash, The GNU Bash Reference
15629 Manual}, for details.
15631 @item Bash 2.05 and later
15632 @cindex Bash 2.05 and later
15633 Versions 2.05 and later of Bash use a different format for the
15634 output of the @command{set} builtin, designed to make evaluating its
15635 output easier. However, this output is not compatible with earlier
15636 versions of Bash (or with many other shells, probably). So if
15637 you use Bash 2.05 or higher to execute @command{configure},
15638 you'll need to use Bash 2.05 for all other build tasks as well.
15643 @prindex @samp{ksh}
15644 @prindex @samp{ksh88}
15645 @prindex @samp{ksh93}
15646 The Korn shell is compatible with the Bourne family and it mostly
15647 conforms to Posix. It has two major variants commonly
15648 called @samp{ksh88} and @samp{ksh93}, named after the years of initial
15649 release. It is usually called @command{ksh}, but is called @command{sh}
15650 on some hosts if you set your path appropriately.
15652 On Solaris 11, @command{/bin/sh} and @command{/usr/bin/ksh} are both
15653 @samp{ksh93}. On Solaris 10 and earlier, @command{/bin/sh} is a
15654 pre-Posix Bourne shell and the Korn shell is found elsewhere:
15655 @prindex @command{/usr/bin/ksh} on Solaris
15656 @command{/usr/bin/ksh} is @samp{ksh88} on Solaris 10,
15657 @prindex @command{/usr/xpg4/bin/sh} on Solaris
15658 @command{/usr/xpg4/bin/sh} is a Posix-compliant variant of
15659 @samp{ksh88} on Solaris 10 and later,
15660 @prindex @command{/usr/dt/bin/dtksh} on Solaris
15661 and @command{/usr/dt/bin/dtksh} is @samp{ksh93}.
15662 Variants that are not standard may be parts of optional
15663 packages. There is no extra charge for these packages, but they are
15664 not part of a minimal OS install and therefore some installations may
15667 Starting with Tru64 Version 4.0, the Korn shell @command{/usr/bin/ksh}
15668 is also available as @command{/usr/bin/posix/sh}. If the environment
15669 variable @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
15670 the standard shell conform to Posix.
15673 @prindex @samp{pdksh}
15674 A public-domain clone of the Korn shell called @command{pdksh} is widely
15675 available: it has most of the @samp{ksh88} features along with a few of
15676 its own. It usually sets @code{KSH_VERSION}, except if invoked as
15677 @command{/bin/sh} on OpenBSD, and similarly to Bash you can require
15678 Posix compatibility by running @samp{set -o posix}. Unfortunately, with
15679 @command{pdksh} 5.2.14 (the latest stable version as of January 2007)
15680 Posix mode is buggy and causes @command{pdksh} to depart from Posix in
15681 at least one respect, see @ref{Shell Substitutions}.
15685 To detect whether you are running @command{zsh}, test whether
15686 @code{ZSH_VERSION} is set. By default @command{zsh} is @emph{not}
15687 compatible with the Bourne shell: you must execute @samp{emulate sh},
15688 and for @command{zsh} versions before 3.1.6-dev-18 you must also
15689 set @code{NULLCMD} to @samp{:}. @xref{Compatibility, , Compatibility,
15690 zsh, The Z Shell Manual}, for details.
15692 The default Mac OS X @command{sh} was originally Zsh; it was changed to
15693 Bash in Mac OS X 10.2.
15696 @node Invoking the Shell
15697 @section Invoking the Shell
15698 @cindex invoking the shell
15699 @cindex shell invocation
15701 The Korn shell (up to at least version M-12/28/93d) has a bug when
15702 invoked on a file whose name does not contain a slash. It first
15703 searches for the file's name in @env{PATH}, and if found it executes
15704 that rather than the original file. For example, assuming there is a
15705 binary executable @file{/usr/bin/script} in your @env{PATH}, the last
15706 command in the following example fails because the Korn shell finds
15707 @file{/usr/bin/script} and refuses to execute it as a shell script:
15710 $ @kbd{touch xxyzzyz script}
15711 $ @kbd{ksh xxyzzyz}
15712 $ @kbd{ksh ./script}
15714 ksh: script: cannot execute
15717 Bash 2.03 has a bug when invoked with the @option{-c} option: if the
15718 option-argument ends in backslash-newline, Bash incorrectly reports a
15719 syntax error. The problem does not occur if a character follows the
15723 $ @kbd{$ bash -c 'echo foo \}
15725 bash: -c: line 2: syntax error: unexpected end of file
15726 $ @kbd{bash -c 'echo foo \}
15732 @xref{Backslash-Newline-Empty}, for how this can cause problems in makefiles.
15734 @node Here-Documents
15735 @section Here-Documents
15736 @cindex Here-documents
15737 @cindex Shell here-documents
15739 Because unquoted here-documents are subject to parameter expansion and
15740 command substitution, the characters @samp{$} and @samp{`} are special
15741 in unquoted here-documents and should be escaped by @samp{\} if you want
15742 them as-is. Also, @samp{\} is special if it precedes @samp{$},
15743 @samp{`}, newline or @samp{\} itself, so @samp{\} should be doubled if
15744 it appears before these characters and you want it as-is.
15746 Using command substitutions in a here-document that is fed to a shell
15747 function is not portable. For example, with Solaris 10 @command{/bin/sh}:
15750 $ @kbd{kitty () @{ cat; @}}
15754 /tmp/sh199886: cannot open
15759 Some shells mishandle large here-documents: for example,
15760 Solaris 10 @command{dtksh} and the UnixWare 7.1.1 Posix shell, which are
15761 derived from Korn shell version M-12/28/93d, mishandle braced variable
15762 expansion that crosses a 1024- or 4096-byte buffer boundary
15763 within a here-document. Only the part of the variable name after the boundary
15764 is used. For example, @code{$@{variable@}} could be replaced by the expansion
15765 of @code{$@{ble@}}. If the end of the variable name is aligned with the block
15766 boundary, the shell reports an error, as if you used @code{$@{@}}.
15767 Instead of @code{$@{variable-default@}}, the shell may expand
15768 @code{$@{riable-default@}}, or even @code{$@{fault@}}. This bug can often
15769 be worked around by omitting the braces: @code{$variable}. The bug was
15771 @samp{ksh93g} (1998-04-30) but as of 2006 many operating systems were
15772 still shipping older versions with the bug.
15774 Empty here-documents are not portable either; with the following code,
15775 @command{zsh} up to at least version 4.3.10 creates a file with a single
15776 newline, whereas other shells create an empty file:
15783 Many shells (including the Bourne shell) implement here-documents
15784 inefficiently. In particular, some shells can be extremely inefficient when
15785 a single statement contains many here-documents. For instance if your
15786 @file{configure.ac} includes something like:
15790 AS_IF([<cross_compiling>],
15791 [assume this and that],
15794 check something else
15801 A shell parses the whole @code{if}/@code{fi} construct generated by
15802 @code{AS_IF}, creating
15803 temporary files for each here-document in it. Some shells create links
15804 for such here-documents on every @code{fork}, so that the clean-up code
15805 they had installed correctly removes them. It is creating the links
15806 that can take the shell forever.
15808 Moving the tests out of the @code{if}/@code{fi}, or creating multiple
15809 @code{if}/@code{fi} constructs, would improve the performance
15810 significantly. Anyway, this kind of construct is not exactly the
15811 typical use of Autoconf. In fact, it's even not recommended, because M4
15812 macros can't look into shell conditionals, so we may fail to expand a
15813 macro when it was expanded before in a conditional path, and the
15814 condition turned out to be false at runtime, and we end up not
15815 executing the macro at all.
15817 Be careful with the use of @samp{<<-} to unindent here-documents. The
15818 behavior is only portable for stripping leading @key{TAB}s, and things
15819 can silently break if an overzealous editor converts to using leading
15820 spaces (not all shells are nice enough to warn about unterminated
15824 $ @kbd{printf 'cat <<-x\n\t1\n\t 2\n\tx\n' | bash && echo done}
15828 $ @kbd{printf 'cat <<-x\n 1\n 2\n x\n' | bash-3.2 && echo done}
15835 @node File Descriptors
15836 @section File Descriptors
15837 @cindex Descriptors
15838 @cindex File descriptors
15839 @cindex Shell file descriptors
15841 Most shells, if not all (including Bash, Zsh, Ash), output traces on
15842 stderr, even for subshells. This might result in undesirable content
15843 if you meant to capture the standard-error output of the inner command:
15846 $ @kbd{ash -x -c '(eval "echo foo >&2") 2>stderr'}
15848 + eval echo foo >&2
15851 $ @kbd{bash -x -c '(eval "echo foo >&2") 2>stderr'}
15853 + eval 'echo foo >&2'
15856 $ @kbd{zsh -x -c '(eval "echo foo >&2") 2>stderr'}
15857 @i{# Traces on startup files deleted here.}
15859 +zsh:1> eval echo foo >&2
15865 One workaround is to grep out uninteresting lines, hoping not to remove
15868 If you intend to redirect both standard error and standard output,
15869 redirect standard output first. This works better with HP-UX,
15870 since its shell mishandles tracing if standard error is redirected
15874 $ @kbd{sh -x -c ': 2>err >out'}
15876 + 2> err $ @kbd{cat err}
15880 Don't try to redirect the standard error of a command substitution. It
15881 must be done @emph{inside} the command substitution. When running
15882 @samp{: `cd /zorglub` 2>/dev/null} expect the error message to
15883 escape, while @samp{: `cd /zorglub 2>/dev/null`} works properly.
15885 On the other hand, some shells, such as Solaris or FreeBSD
15886 @command{/bin/sh}, warn about missing programs before performing
15887 redirections. Therefore, to silently check whether a program exists, it
15888 is necessary to perform redirections on a subshell or brace group:
15890 $ @kbd{/bin/sh -c 'nosuch 2>/dev/null'}
15892 $ @kbd{/bin/sh -c '(nosuch) 2>/dev/null'}
15893 $ @kbd{/bin/sh -c '@{ nosuch; @} 2>/dev/null'}
15894 $ @kbd{bash -c 'nosuch 2>/dev/null'}
15897 FreeBSD 6.2 sh may mix the trace output lines from the statements in a
15900 It is worth noting that Zsh (but not Ash nor Bash) makes it possible
15901 in assignments though: @samp{foo=`cd /zorglub` 2>/dev/null}.
15903 Some shells, like @command{ash}, don't recognize bi-directional
15904 redirection (@samp{<>}). And even on shells that recognize it, it is
15905 not portable to use on fifos: Posix does not require read-write support
15906 for named pipes, and Cygwin does not support it:
15909 $ @kbd{mkfifo fifo}
15910 $ @kbd{exec 5<>fifo}
15911 $ @kbd{echo hi >&5}
15912 bash: echo: write error: Communication error on send
15916 Furthermore, versions of @command{dash} before 0.5.6 mistakenly truncate
15917 regular files when using @samp{<>}:
15920 $ @kbd{echo a > file}
15921 $ @kbd{bash -c ': 1<>file'; cat file}
15923 $ @kbd{dash -c ': 1<>file'; cat file}
15927 Solaris 10 @code{/bin/sh} executes redirected compound commands
15928 in a subshell, while other shells don't:
15931 $ @kbd{/bin/sh -c 'foo=0; @{ foo=1; @} 2>/dev/null; echo $foo'}
15933 $ @kbd{ksh -c 'foo=0; @{ foo=1; @} 2>/dev/null; echo $foo'}
15935 $ @kbd{bash -c 'foo=0; @{ foo=1; @} 2>/dev/null; echo $foo'}
15939 When catering to old systems, don't redirect the same file descriptor
15940 several times, as you are doomed to failure under Ultrix.
15943 ULTRIX V4.4 (Rev. 69) System #31: Thu Aug 10 19:42:23 GMT 1995
15945 $ @kbd{eval 'echo matter >fullness' >void}
15947 $ @kbd{eval '(echo matter >fullness)' >void}
15949 $ @kbd{(eval '(echo matter >fullness)') >void}
15950 Ambiguous output redirect.
15954 In each case the expected result is of course @file{fullness} containing
15955 @samp{matter} and @file{void} being empty. However, this bug is
15956 probably not of practical concern to modern platforms.
15958 Solaris 10 @command{sh} will try to optimize away a @command{:} command
15959 (even if it is redirected) in a loop after the first iteration, or in a
15960 shell function after the first call:
15963 $ @kbd{for i in 1 2 3 ; do : >x$i; done}
15966 $ @kbd{f () @{ : >$1; @}; f y1; f y2; f y3;}
15972 As a workaround, @command{echo} or @command{eval} can be used.
15974 Don't rely on file descriptors 0, 1, and 2 remaining closed in a
15975 subsidiary program. If any of these descriptors is closed, the
15976 operating system may open an unspecified file for the descriptor in the
15977 new process image. Posix 2008 says this may be done only if the
15978 subsidiary program is set-user-ID or set-group-ID, but HP-UX 11.23 does
15979 it even for ordinary programs, and the next version of Posix will allow
15982 If you want a file descriptor above 2 to be inherited into a child
15983 process, then you must use redirections specific to that command or a
15984 containing subshell or command group, rather than relying on
15985 @command{exec} in the shell. In @command{ksh} as well as HP-UX
15986 @command{sh}, file descriptors above 2 which are opened using
15987 @samp{exec @var{n}>file} are closed by a subsequent @samp{exec} (such as
15988 that involved in the fork-and-exec which runs a program or script):
15991 $ @kbd{echo 'echo hello >&5' >k}
15992 $ @kbd{/bin/sh -c 'exec 5>t; ksh ./k; exec 5>&-; cat t}
15994 $ @kbd{bash -c 'exec 5>t; ksh ./k; exec 5>&-; cat t}
15996 $ @kbd{ksh -c 'exec 5>t; ksh ./k; exec 5>&-; cat t}
15997 ./k[1]: 5: cannot open [Bad file number]
15998 $ @kbd{ksh -c '(ksh ./k) 5>t; cat t'}
16000 $ @kbd{ksh -c '@{ ksh ./k; @} 5>t; cat t'}
16002 $ @kbd{ksh -c '5>t ksh ./k; cat t}
16006 Don't rely on duplicating a closed file descriptor to cause an
16007 error. With Solaris 10 @command{/bin/sh}, failed duplication is silently
16008 ignored, which can cause unintended leaks to the original file
16009 descriptor. In this example, observe the leak to standard output:
16012 $ @kbd{bash -c 'echo hi >&3' 3>&-; echo $?}
16013 bash: 3: Bad file descriptor
16015 $ @kbd{/bin/sh -c 'echo hi >&3' 3>&-; echo $?}
16020 Fortunately, an attempt to close an already closed file descriptor will
16021 portably succeed. Likewise, it is safe to use either style of
16022 @samp{@var{n}<&-} or @samp{@var{n}>&-} for closing a file descriptor,
16023 even if it doesn't match the read/write mode that the file descriptor
16026 DOS variants cannot rename or remove open files, such as in
16027 @samp{mv foo bar >foo} or @samp{rm foo >foo}, even though this is
16028 perfectly portable among Posix hosts.
16030 A few ancient systems reserved some file descriptors. By convention,
16031 file descriptor 3 was opened to @file{/dev/tty} when you logged into
16032 Eighth Edition (1985) through Tenth Edition Unix (1989). File
16033 descriptor 4 had a special use on the Stardent/Kubota Titan (circa
16034 1990), though we don't now remember what it was. Both these systems are
16035 obsolete, so it's now safe to treat file descriptors 3 and 4 like any
16036 other file descriptors.
16038 On the other hand, you can't portably use multi-digit file descriptors.
16039 @command{dash} and Solaris @command{ksh} don't understand any file
16040 descriptor larger than @samp{9}:
16043 $ @kbd{bash -c 'exec 10>&-'; echo $?}
16045 $ @kbd{ksh -c 'exec 9>&-'; echo $?}
16047 $ @kbd{ksh -c 'exec 10>&-'; echo $?}
16048 ksh[1]: exec: 10: not found
16050 $ @kbd{dash -c 'exec 9>&-'; echo $?}
16052 $ @kbd{dash -c 'exec 10>&-'; echo $?}
16053 exec: 1: 10: not found
16057 @c <https://lists.gnu.org/archive/html/bug-autoconf/2011-09/msg00004.html>
16058 @node Signal Handling
16059 @section Signal Handling
16060 @cindex Signal handling in the shell
16061 @cindex Signals, shells and
16063 Portable handling of signals within the shell is another major source of
16064 headaches. This is worsened by the fact that various different, mutually
16065 incompatible approaches are possible in this area, each with its
16066 distinctive merits and demerits. A detailed description of these possible
16067 approaches, as well as of their pros and cons, can be found in
16068 @uref{https://www.cons.org/cracauer/sigint.html, this article}.
16070 Solaris 10 @command{/bin/sh} automatically traps most signals by default;
16071 the shell still exits with error upon termination by one of those signals,
16072 but in such a case the exit status might be somewhat unexpected (even if
16073 allowed by POSIX, strictly speaking):
16074 @c FIXME: We had a reference for this behavior but the website no longer
16075 @c exists and the page is not in the Internet Archive. --zw 2020-07-10.
16078 $ @kbd{bash -c 'kill -1 $$'; echo $?} # Will exit 128 + (signal number).
16081 $ @kbd{/bin/ksh -c 'kill -15 $$'; echo $?} # Likewise.
16084 $ @kbd{for sig in 1 2 3 15; do}
16085 > @kbd{ echo $sig:}
16086 > @kbd{ /bin/sh -c "kill -$s \$\$"; echo $?}
16099 This gets even worse if one is using the POSIX ``wait'' interface to get
16100 details about the shell process terminations: it will result in the shell
16101 having exited normally, rather than by receiving a signal.
16104 $ @kbd{cat > foo.c <<'END'}
16105 #include <stdio.h> /* for printf */
16106 #include <stdlib.h> /* for system */
16107 #include <sys/wait.h> /* for WIF* macros */
16110 int status = system ("kill -15 $$");
16111 printf ("Terminated by signal: %s\n",
16112 WIFSIGNALED (status) ? "yes" : "no");
16113 printf ("Exited normally: %s\n",
16114 WIFEXITED (status) ? "yes" : "no");
16119 $ @kbd{cc -o foo foo.c}
16120 $ @kbd{./a.out} # On GNU/Linux
16121 Terminated by signal: no
16122 Exited normally: yes
16123 $ @kbd{./a.out} # On Solaris 10
16124 Terminated by signal: yes
16125 Exited normally: no
16128 Various shells seem to handle @code{SIGQUIT} specially: they ignore it even
16129 if it is not blocked, and even if the shell is not running interactively
16130 (in fact, even if the shell has no attached tty); among these shells
16131 are at least Bash (from version 2 onward), Zsh 4.3.12, Solaris 10
16132 @code{/bin/ksh} and @code{/usr/xpg4/bin/sh}, and AT&T @code{ksh93} (2011).
16133 Still, @code{SIGQUIT} seems to be trappable quite portably within all
16134 these shells. OTOH, some other shells doesn't special-case the handling
16135 of @code{SIGQUIT}; among these shells are at least @code{pdksh} 5.2.14,
16136 Solaris 10 and NetBSD 5.1 @code{/bin/sh}, and the Almquist Shell 0.5.5.1.
16138 Some shells (especially Korn shells and derivatives) might try to
16139 propagate to themselves a signal that has killed a child process; this is
16140 not a bug, but a conscious design choice (although its overall value might
16141 be debatable). The exact details of how this is attained vary from shell
16142 to shell. For example, upon running @code{perl -e 'kill 2, $$'}, after
16143 the perl process has been interrupted, AT&T @code{ksh93} (2011) will
16144 proceed to send itself a @code{SIGINT}, while Solaris 10 @code{/bin/ksh}
16145 and @code{/usr/xpg4/bin/sh} will proceed to exit with status 130 (i.e.,
16146 128 + 2). In any case, if there is an active trap associated with
16147 @code{SIGINT}, those shells will correctly execute it.
16149 @c See: <https://www.austingroupbugs.net/view.php?id=51>
16150 Some Korn shells, when a child process die due receiving a signal with
16151 signal number @var{n}, can leave in @samp{$?} an exit status of
16152 256+@var{n} instead of the more common 128+@var{n}. Observe the
16153 difference between AT&T @code{ksh93} (2011) and @code{bash} 4.1.5 on
16157 $ @kbd{/bin/ksh -c 'sh -c "kill -1 \$\$"; echo $?'}
16158 /bin/ksh: line 1: 7837: Hangup
16160 $ @kbd{/bin/bash -c 'sh -c "kill -1 \$\$"; echo $?'}
16161 /bin/bash: line 1: 7861 Hangup (sh -c "kill -1 \$\$")
16166 This @command{ksh} behavior is allowed by POSIX, if implemented with
16167 due care; see this @uref{https://www.austingroupbugs.net/view.php?id=51,
16168 Austin Group discussion} for more background. However, if it is not
16169 implemented with proper care, such a behavior might cause problems
16170 in some corner cases. To see why, assume we have a ``wrapper'' script
16175 # Ignore some signals in the shell only, not in its child processes.
16177 wrapped_command "$@@"
16184 If @command{wrapped_command} is interrupted by a @code{SIGHUP} (which
16185 has signal number 1), @code{ret} will be set to 257. Unless the
16186 @command{exit} shell builtin is smart enough to understand that such
16187 a value can only have originated from a signal, and adjust the final
16188 wait status of the shell appropriately, the value 257 will just get
16189 truncated to 1 by the closing @code{exit} call, so that a caller of
16190 the script will have no way to determine that termination by a signal
16191 was involved. Observe the different behavior of AT&T @code{ksh93}
16192 (2011) and @code{bash} 4.1.5 on Debian:
16201 $ @kbd{/bin/ksh foo.sh; echo $?}
16202 foo.sh: line 2: 12479: Hangup
16205 $ @kbd{/bin/bash foo.sh; echo $?}
16206 foo.sh: line 2: 12487 Hangup (sh -c 'kill -1 $$')
16211 @node File System Conventions
16212 @section File System Conventions
16213 @cindex File system conventions
16215 Autoconf uses shell-script processing extensively, so the file names
16216 that it processes should not contain characters that are special to the
16217 shell. Special characters include space, tab, newline, NUL, and
16221 " # $ & ' ( ) * ; < = > ? [ \ ` |
16224 Also, file names should not begin with @samp{~} or @samp{-}, and should
16225 contain neither @samp{-} immediately after @samp{/} nor @samp{~}
16226 immediately after @samp{:}. On Posix-like platforms, directory names
16227 should not contain @samp{:}, as this runs afoul of @samp{:} used as the
16230 These restrictions apply not only to the files that you distribute, but
16231 also to the absolute file names of your source, build, and destination
16234 On some Posix-like platforms, @samp{!} and @samp{^} are special too, so
16235 they should be avoided.
16237 Posix lets implementations treat leading @file{//} specially, but
16238 requires leading @file{///} and beyond to be equivalent to @file{/}.
16239 Most Unix variants treat @file{//} like @file{/}. However, some treat
16240 @file{//} as a ``super-root'' that can provide access to files that are
16241 not otherwise reachable from @file{/}. The super-root tradition began
16242 with Apollo Domain/OS, which died out long ago, but unfortunately Cygwin
16245 While @command{autoconf} and friends are usually run on some Posix
16246 variety, they can be used on other systems, most notably DOS
16247 variants. This impacts several assumptions regarding file names.
16250 For example, the following code:
16257 foo_dir=$dots$foo_dir ;;
16262 fails to properly detect absolute file names on those systems, because
16263 they can use a drivespec, and usually use a backslash as directory
16264 separator. If you want to be portable to DOS variants (at the
16265 price of rejecting valid but oddball Posix file names like @file{a:\b}),
16266 you can check for absolute file names like this:
16268 @cindex absolute file names, detect
16271 [\\/]* | ?:[\\/]* ) # Absolute
16274 foo_dir=$dots$foo_dir ;;
16279 Make sure you quote the brackets if appropriate and keep the backslash as
16280 first character. @xref{case, , Limitations of Shell Builtins}.
16282 Also, because the colon is used as part of a drivespec, these systems don't
16283 use it as path separator. When creating or accessing paths, you can use the
16284 @code{PATH_SEPARATOR} output variable instead. @command{configure} sets this
16285 to the appropriate value for the build system (@samp{:} or @samp{;}) when it
16288 File names need extra care as well. While DOS variants
16289 that are Posixy enough to run @command{autoconf} (such as DJGPP)
16290 are usually able to handle long file names properly, there are still
16291 limitations that can seriously break packages. Several of these issues
16292 can be easily detected by the
16293 @uref{https://@/ftp.gnu.org/@/gnu/@/non-gnu/@/doschk/@/doschk-1.1.tar.gz, doschk}
16296 A short overview follows; problems are marked with SFN/LFN to
16297 indicate where they apply: SFN means the issues are only relevant to
16298 plain DOS, not to DOS under Microsoft Windows
16299 variants, while LFN identifies problems that exist even under
16300 Microsoft Windows variants.
16303 @item No multiple dots (SFN)
16304 DOS cannot handle multiple dots in file names. This is an especially
16305 important thing to remember when building a portable configure script,
16306 as @command{autoconf} uses a .in suffix for template files.
16308 This is perfectly OK on Posix variants:
16311 AC_CONFIG_HEADERS([config.h])
16312 AC_CONFIG_FILES([source.c foo.bar])
16317 but it causes problems on DOS, as it requires @samp{config.h.in},
16318 @samp{source.c.in} and @samp{foo.bar.in}. To make your package more portable
16319 to DOS-based environments, you should use this instead:
16322 AC_CONFIG_HEADERS([config.h:config.hin])
16323 AC_CONFIG_FILES([source.c:source.cin foo.bar:foobar.in])
16327 @item No leading dot (SFN)
16328 DOS cannot handle file names that start with a dot. This is usually
16329 not important for @command{autoconf}.
16331 @item Case insensitivity (LFN)
16332 DOS is case insensitive, so you cannot, for example, have both a
16333 file called @samp{INSTALL} and a directory called @samp{install}. This
16334 also affects @command{make}; if there's a file called @samp{INSTALL} in
16335 the directory, @samp{make install} does nothing (unless the
16336 @samp{install} target is marked as PHONY).
16338 @item The 8+3 limit (SFN)
16339 Because the DOS file system only stores the first 8 characters of
16340 the file name and the first 3 of the extension, those must be unique.
16341 That means that @file{foobar-part1.c}, @file{foobar-part2.c} and
16342 @file{foobar-prettybird.c} all resolve to the same file name
16343 (@file{FOOBAR-P.C}). The same goes for @file{foo.bar} and
16344 @file{foo.bartender}.
16346 The 8+3 limit is not usually a problem under Microsoft Windows, as it
16348 tails in the short version of file names to make them unique. However, a
16349 registry setting can turn this behavior off. While this makes it
16350 possible to share file trees containing long file names between SFN
16351 and LFN environments, it also means the above problem applies there
16354 @item Invalid characters (LFN)
16355 Some characters are invalid in DOS file names, and should therefore
16356 be avoided. In a LFN environment, these are @samp{/}, @samp{\},
16357 @samp{?}, @samp{*}, @samp{:}, @samp{<}, @samp{>}, @samp{|} and @samp{"}.
16358 In a SFN environment, other characters are also invalid. These
16359 include @samp{+}, @samp{,}, @samp{[} and @samp{]}.
16361 @item Invalid names (LFN)
16362 Some DOS file names are reserved, and cause problems if you
16363 try to use files with those names. These names include @file{CON},
16364 @file{AUX}, @file{COM1}, @file{COM2}, @file{COM3}, @file{COM4},
16365 @file{LPT1}, @file{LPT2}, @file{LPT3}, @file{NUL}, and @file{PRN}.
16366 File names are case insensitive, so even names like
16367 @file{aux/config.guess} are disallowed.
16371 @node Shell Pattern Matching
16372 @section Shell Pattern Matching
16373 @cindex Shell pattern matching
16375 Nowadays portable patterns can use negated character classes like
16376 @samp{[!-aeiou]}. The older syntax @samp{[^-aeiou]} is supported by
16377 some shells but not others; hence portable scripts should never use
16378 @samp{^} as the first character of a bracket pattern.
16380 Outside the C locale, patterns like @samp{[a-z]} are problematic since
16381 they may match characters that are not lower-case letters.
16383 @node Shell Substitutions
16384 @section Shell Substitutions
16385 @cindex Shell substitutions
16387 Contrary to a persistent urban legend, the Bourne shell does not
16388 systematically split variables and back-quoted expressions, in particular
16389 on the right-hand side of assignments and in the argument of @code{case}.
16390 For instance, the following code:
16393 case "$given_srcdir" in
16394 .) top_srcdir="`echo "$dots" | sed 's|/$||'`" ;;
16395 *) top_srcdir="$dots$given_srcdir" ;;
16400 is more readable when written as:
16403 case $given_srcdir in
16404 .) top_srcdir=`echo "$dots" | sed 's|/$||'` ;;
16405 *) top_srcdir=$dots$given_srcdir ;;
16410 and in fact it is even @emph{more} portable: in the first case of the
16411 first attempt, the computation of @code{top_srcdir} is not portable,
16412 since not all shells properly understand @code{"`@dots{}"@dots{}"@dots{}`"},
16413 for example Solaris 10 @command{ksh}:
16416 $ @kbd{foo="`echo " bar" | sed 's, ,,'`"}
16417 ksh: : cannot execute
16418 ksh: bar | sed 's, ,,': cannot execute
16422 Posix does not specify behavior for this sequence. On the other hand,
16423 behavior for @code{"`@dots{}\"@dots{}\"@dots{}`"} is specified by Posix,
16424 but in practice, not all shells understand it the same way: pdksh 5.2.14
16425 prints spurious quotes when in Posix mode:
16428 $ @kbd{echo "`echo \"hello\"`"}
16430 $ @kbd{set -o posix}
16431 $ @kbd{echo "`echo \"hello\"`"}
16436 There is just no portable way to use double-quoted strings inside
16437 double-quoted back-quoted expressions (pfew!).
16439 Bash 4.1 has a bug where quoted empty strings adjacent to unquoted
16440 parameter expansions are elided during word splitting. Meanwhile, zsh
16441 does not perform word splitting except when in Bourne compatibility
16442 mode. In the example below, the correct behavior is to have five
16443 arguments to the function, and exactly two spaces on either side of the
16444 middle @samp{-}, since word splitting collapses multiple spaces in
16445 @samp{$f} but leaves empty arguments intact.
16448 $ @kbd{bash -c 'n() @{ echo "$#$@@"; @}; f=" - "; n - ""$f"" -'}
16450 $ @kbd{ksh -c 'n() @{ echo "$#$@@"; @}; f=" - "; n - ""$f"" -'}
16452 $ @kbd{zsh -c 'n() @{ echo "$#$@@"; @}; f=" - "; n - ""$f"" -'}
16454 $ @kbd{zsh -c 'emulate sh;}
16455 > @kbd{n() @{ echo "$#$@@"; @}; f=" - "; n - ""$f"" -'}
16460 You can work around this by doing manual word splitting, such as using
16461 @samp{"$str" $list} rather than @samp{"$str"$list}.
16463 There are also portability pitfalls with particular expansions:
16467 @cindex @code{"$@@"}
16468 One of the most famous shell-portability issues is related to
16469 @samp{"$@@"}. When there are no positional arguments, Posix says
16470 that @samp{"$@@"} is supposed to be equivalent to nothing, but the
16471 original Unix version 7 Bourne shell treated it as equivalent to
16472 @samp{""} instead, and this behavior survives in later implementations
16473 like Digital Unix 5.0.
16475 The traditional way to work around this portability problem is to use
16476 @samp{$@{1+"$@@"@}}. Unfortunately this method does not work with
16477 Zsh (3.x and 4.x), which is used on Mac OS X@. When emulating
16478 the Bourne shell, Zsh performs word splitting on @samp{$@{1+"$@@"@}}:
16481 zsh $ @kbd{emulate sh}
16482 zsh $ @kbd{for i in "$@@"; do echo $i; done}
16485 zsh $ @kbd{for i in $@{1+"$@@"@}; do echo $i; done}
16492 Zsh handles plain @samp{"$@@"} properly, but we can't use plain
16493 @samp{"$@@"} because of the portability problems mentioned above.
16494 One workaround relies on Zsh's ``global aliases'' to convert
16495 @samp{$@{1+"$@@"@}} into @samp{"$@@"} by itself:
16498 test $@{ZSH_VERSION+y@} && alias -g '$@{1+"$@@"@}'='"$@@"'
16501 Zsh only recognizes this alias when a shell word matches it exactly;
16502 @samp{"foo"$@{1+"$@@"@}} remains subject to word splitting. Since this
16503 case always yields at least one shell word, use plain @samp{"$@@"}.
16505 A more conservative workaround is to avoid @samp{"$@@"} if it is
16506 possible that there may be no positional arguments. For example,
16510 cat conftest.c "$@@"
16513 you can use this instead:
16517 0) cat conftest.c;;
16518 *) cat conftest.c "$@@";;
16522 Autoconf macros often use the @command{set} command to update
16523 @samp{$@@}, so if you are writing shell code intended for
16524 @command{configure} you should not assume that the value of @samp{$@@}
16525 persists for any length of time.
16529 @cindex positional parameters
16530 The 10th, 11th, @dots{} positional parameters can be accessed only after
16531 a @code{shift}. The 7th Edition shell reported an error if given
16532 @code{$@{10@}}, and
16533 Solaris 10 @command{/bin/sh} still acts that way:
16536 $ @kbd{set 1 2 3 4 5 6 7 8 9 10}
16537 $ @kbd{echo $@{10@}}
16541 Conversely, not all shells obey the Posix rule that when braces are
16542 omitted, multiple digits beyond a @samp{$} imply the single-digit
16543 positional parameter expansion concatenated with the remaining literal
16544 digits. To work around the issue, you must use braces.
16547 $ @kbd{bash -c 'set a b c d e f g h i j; echo $10 $@{1@}0'}
16549 $ @kbd{dash -c 'set a b c d e f g h i j; echo $10 $@{1@}0'}
16553 @item $@{@var{var}:-@var{value}@}
16554 @itemx $@{@var{var}:=@var{value}@}
16555 @itemx $@{@var{var}:?@var{value}@}
16556 @itemx $@{@var{var}:+@var{value}@}
16557 @c Info cannot handle ':' in index entries.
16559 @cindex @code{$@{@var{var}:-@var{value}@}}
16560 @cindex @code{$@{@var{var}:=@var{value}@}}
16561 @cindex @code{$@{@var{var}:?@var{value}@}}
16562 @cindex @code{$@{@var{var}:+@var{value}@}}
16564 Old BSD shells, including the Ultrix @code{sh}, don't accept the
16565 colon for any shell substitution, and complain and die.
16566 Similarly for @code{$@{@var{var}:=@var{value}@}},
16567 @code{$@{@var{var}:?@var{value}@}}, etc.
16568 However, all shells that support functions allow the use of colon in
16569 shell substitution, and since m4sh requires functions, you can portably
16570 use null variable substitution patterns in configure scripts.
16572 @item $@{@var{var}-@var{value}@}
16573 @itemx $@{@var{var}:-@var{value}@}
16574 @itemx $@{@var{var}=@var{value}@}
16575 @itemx $@{@var{var}:=@var{value}@}
16576 @itemx $@{@var{var}?@var{value}@}
16577 @itemx $@{@var{var}:?@var{value}@}
16578 @itemx $@{@var{var}+@var{value}@}
16579 @itemx $@{@var{var}:+@var{value}@}
16580 @cindex @code{$@{@var{var}-@var{value}@}}
16581 @cindex @code{$@{@var{var}=@var{value}@}}
16582 @cindex @code{$@{@var{var}?@var{value}@}}
16583 @cindex @code{$@{@var{var}+@var{value}@}}
16584 @c Info cannot handle ':' in index entries.
16586 @cindex @code{$@{@var{var}:-@var{value}@}}
16587 @cindex @code{$@{@var{var}:=@var{value}@}}
16588 @cindex @code{$@{@var{var}:?@var{value}@}}
16589 @cindex @code{$@{@var{var}:+@var{value}@}}
16591 When using @samp{$@{@var{var}-@var{value}@}} or
16592 similar notations that modify a parameter expansion,
16593 Posix requires that @var{value} must be a single shell word,
16594 which can contain quoted strings but cannot contain unquoted spaces.
16595 If this requirement is not met Solaris 10 @command{/bin/sh}
16596 sometimes complains, and anyway the behavior is not portable.
16599 $ @kbd{/bin/sh -c 'echo $@{a-b c@}'}
16600 /bin/sh: bad substitution
16601 $ @kbd{/bin/sh -c 'echo $@{a-'\''b c'\''@}'}
16603 $ @kbd{/bin/sh -c 'echo "$@{a-b c@}"'}
16605 $ @kbd{/bin/sh -c 'cat <<EOF
16611 Most shells treat the special parameters @code{*} and @code{@@} as being
16612 unset if there are no positional parameters. However, some shells treat
16613 them as being set to the empty string. Posix does not clearly specify
16617 $ @kbd{bash -c 'echo "* is $@{*-unset@}."'}
16619 $ @kbd{dash -c 'echo "* is $@{*-unset@}."'}
16623 According to Posix, if an expansion occurs inside double quotes, then
16624 the use of unquoted double quotes within @var{value} is unspecified, and
16625 any single quotes become literal characters; in that case, escaping must
16626 be done with backslash. Likewise, the use of unquoted here-documents is
16627 a case where double quotes have unspecified results:
16630 $ @kbd{/bin/sh -c 'echo "$@{a-"b c"@}"'}
16631 /bin/sh: bad substitution
16632 $ @kbd{ksh -c 'echo "$@{a-"b c"@}"'}
16634 $ @kbd{bash -c 'echo "$@{a-"b c"@}"'}
16636 $ @kbd{/bin/sh -c 'a=; echo $@{a+'\''b c'\''@}'}
16638 $ @kbd{/bin/sh -c 'a=; echo "$@{a+'\''b c'\''@}"'}
16640 $ @kbd{/bin/sh -c 'a=; echo "$@{a+\"b c\"@}"'}
16642 $ @kbd{/bin/sh -c 'a=; echo "$@{a+b c@}"'}
16644 $ @kbd{/bin/sh -c 'cat <<EOF
16648 $ @kbd{/bin/sh -c 'cat <<EOF
16652 $ @kbd{bash -c 'cat <<EOF
16656 $ @kbd{bash -c 'cat <<EOF
16662 Perhaps the easiest way to work around quoting issues in a manner
16663 portable to all shells is to place the results in a temporary variable,
16664 then use @samp{$t} as the @var{value}, rather than trying to inline
16665 the expression needing quoting.
16668 $ @kbd{/bin/sh -c 't="b c\"'\''@}\\"; echo "$@{a-$t@}"'}
16670 $ @kbd{ksh -c 't="b c\"'\''@}\\"; echo "$@{a-$t@}"'}
16672 $ @kbd{bash -c 't="b c\"'\''@}\\"; echo "$@{a-$t@}"'}
16676 @item $@{@var{var}=@var{value}@}
16677 @cindex @code{$@{@var{var}=@var{value}@}}
16678 When using @samp{$@{@var{var}=@var{value}@}} to assign a default value
16679 to @var{var}, remember that even though the assignment to @var{var} does
16680 not undergo file name expansion, the result of the variable expansion
16681 does unless the expansion occurred within double quotes. In particular,
16682 when using @command{:} followed by unquoted variable expansion for the
16683 side effect of setting a default value, if the final value of
16684 @samp{$var} contains any globbing characters (either from @var{value} or
16685 from prior contents), the shell has to spend time performing file name
16686 expansion and field splitting even though those results will not be
16687 used. Therefore, it is a good idea to consider double quotes when performing
16688 default initialization; while remembering how this impacts any quoting
16689 characters appearing in @var{value}.
16692 $ @kbd{time bash -c ': "$@{a=/usr/bin/*@}"; echo "$a"'}
16698 $ @kbd{time bash -c ': $@{a=/usr/bin/*@}; echo "$a"'}
16704 $ @kbd{time bash -c 'a=/usr/bin/*; : $@{a=noglob@}; echo "$a"'}
16711 $ @kbd{time bash -c 'a=/usr/bin/*; : "$@{a=noglob@}"; echo "$a"'}
16719 As with @samp{+} and @samp{-}, @var{value} must be a single shell word,
16720 otherwise some shells, such as Solaris 10 @command{/bin/sh} or on Digital
16721 Unix V 5.0, die because of a ``bad substitution''. Meanwhile, Posix
16722 requires that with @samp{=}, quote removal happens prior to the
16723 assignment, and the expansion be the final contents of @var{var} without
16724 quoting (and thus subject to field splitting), in contrast to the
16725 behavior with @samp{-} passing the quoting through to the final
16726 expansion. However, @command{bash} 4.1 does not obey this rule.
16729 $ @kbd{ksh -c 'echo $@{var-a\ \ b@}'}
16731 $ @kbd{ksh -c 'echo $@{var=a\ \ b@}'}
16733 $ @kbd{bash -c 'echo $@{var=a\ \ b@}'}
16737 Finally, Posix states that when mixing @samp{$@{a=b@}} with regular
16738 commands, it is unspecified whether the assignments affect the parent
16739 shell environment. It is best to perform assignments independently from
16740 commands, to avoid the problems demonstrated in this example:
16743 $ @kbd{bash -c 'x= y=$@{x:=b@} sh -c "echo +\$x+\$y+";echo -$x-'}
16746 $ @kbd{/bin/sh -c 'x= y=$@{x:=b@} sh -c "echo +\$x+\$y+";echo -$x-'}
16749 $ @kbd{ksh -c 'x= y=$@{x:=b@} sh -c "echo +\$x+\$y+";echo -$x-'}
16754 @item $@{@var{var}=@var{value}@}
16755 @cindex @code{$@{@var{var}=@var{literal}@}}
16756 Solaris 10 @command{/bin/sh} has a frightening bug in its handling of
16757 literal assignments. Imagine you need set a variable to a string containing
16758 @samp{@}}. This @samp{@}} character confuses Solaris 10 @command{/bin/sh}
16759 when the affected variable was already set. This bug can be exercised
16764 $ @kbd{foo=$@{foo='@}'@}}
16767 $ @kbd{foo=$@{foo='@}' # no error; this hints to what the bug is}
16770 $ @kbd{foo=$@{foo='@}'@}}
16776 It seems that @samp{@}} is interpreted as matching @samp{$@{}, even
16777 though it is enclosed in single quotes. The problem doesn't happen
16778 using double quotes, or when using a temporary variable holding the
16779 problematic string.
16781 @item $@{@var{var}=@var{expanded-value}@}
16782 @cindex @code{$@{@var{var}=@var{expanded-value}@}}
16788 : $@{var="$default"@}
16792 sets @var{var} to @samp{M-yM-uM-,M-yM-aM-a}, i.e., the 8th bit of
16793 each char is set. You don't observe the phenomenon using a simple
16794 @samp{echo $var} since apparently the shell resets the 8th bit when it
16795 expands $var. Here are two means to make this shell confess its sins:
16798 $ @kbd{cat -v <<EOF
16807 $ @kbd{set | grep '^var=' | cat -v}
16810 One classic incarnation of this bug is:
16814 : $@{list="$default"@}
16821 You'll get @samp{a b c} on a single line. Why? Because there are no
16822 spaces in @samp{$list}: there are @samp{M- }, i.e., spaces with the 8th
16823 bit set, hence no IFS splitting is performed!!!
16825 One piece of good news is that Ultrix works fine with @samp{:
16826 $@{list=$default@}}; i.e., if you @emph{don't} quote. The bad news is
16827 then that QNX 4.25 then sets @var{list} to the @emph{last} item of
16830 The portable way out consists in using a double assignment, to switch
16831 the 8th bit twice on Ultrix:
16834 list=$@{list="$default"@}
16838 @dots{}but beware of the @samp{@}} bug from Solaris 10 (see above).
16842 test $@{var+y@} || var=@var{@{value@}}
16845 @item $@{#@var{var}@}
16846 @itemx $@{@var{var}%@var{word}@}
16847 @itemx $@{@var{var}%%@var{word}@}
16848 @itemx $@{@var{var}#@var{word}@}
16849 @itemx $@{@var{var}##@var{word}@}
16850 @cindex @code{$@{#@var{var}@}}
16851 @cindex @code{$@{@var{var}%@var{word}@}}
16852 @cindex @code{$@{@var{var}%%@var{word}@}}
16853 @cindex @code{$@{@var{var}#@var{word}@}}
16854 @cindex @code{$@{@var{var}##@var{word}@}}
16855 Posix requires support for these usages, but they do not work with many
16856 traditional shells, e.g., Solaris 10 @command{/bin/sh}.
16858 Also, @command{pdksh} 5.2.14 mishandles some @var{word} forms. For
16859 example if @samp{$1} is @samp{a/b} and @samp{$2} is @samp{a}, then
16860 @samp{$@{1#$2@}} should yield @samp{/b}, but with @command{pdksh} it
16861 yields the empty string.
16864 @item `@var{commands}`
16865 @cindex @code{`@var{commands}`}
16866 @cindex Command Substitution
16867 Posix requires shells to trim all trailing newlines from command
16868 output before substituting it, so assignments like
16869 @samp{dir=`echo "$file" | tr a A`} do not work as expected if
16870 @samp{$file} ends in a newline.
16872 While in general it makes no sense, do not substitute a single builtin
16873 with side effects, because Ash 0.2, trying to optimize, does not fork a
16874 subshell to perform the command.
16876 For instance, if you wanted to check that @command{cd} is silent, do not
16877 use @samp{test -z "`cd /`"} because the following can happen:
16882 $ @kbd{test -z "`cd /`" && pwd}
16887 The result of @samp{foo=`exit 1`} is left as an exercise to the reader.
16889 The MSYS shell leaves a stray byte in the expansion of a double-quoted
16890 command substitution of a native program, if the end of the substitution
16891 is not aligned with the end of the double quote. This may be worked
16892 around by inserting another pair of quotes:
16895 $ @kbd{echo "`printf 'foo\r\n'` bar" > broken}
16896 $ @kbd{echo "`printf 'foo\r\n'`"" bar" | cmp - broken}
16897 - broken differ: char 4, line 1
16900 Upon interrupt or SIGTERM, some shells may abort a command substitution,
16901 replace it with a null string, and wrongly evaluate the enclosing
16902 command before entering the trap or ending the script. This can lead to
16906 $ @kbd{sh -c 'if test `sleep 5; echo hi` = hi; then echo yes; fi'}
16908 sh: test: hi: unexpected operator/operand
16912 You can avoid this by assigning the command substitution to a temporary
16916 $ @kbd{sh -c 'res=`sleep 5; echo hi`
16917 if test "x$res" = xhi; then echo yes; fi'}
16921 @item $(@var{commands})
16922 @cindex @code{$(@var{commands})}
16923 This construct is meant to replace @samp{`@var{commands}`},
16924 and it has most of the problems listed under @code{`@var{commands}`}.
16926 This construct can be
16927 nested while this is impossible to do portably with back quotes.
16928 Although it is almost universally supported, unfortunately Solaris 10
16929 and earlier releases lack it:
16932 $ @kbd{showrev -c /bin/sh | grep version}
16933 Command version: SunOS 5.10 Generic 142251-02 Sep 2010
16934 $ @kbd{echo $(echo blah)}
16935 syntax error: `(' unexpected
16939 nor does IRIX 6.5's Bourne shell:
16942 IRIX firebird-image 6.5 07151432 IP22
16943 $ @kbd{echo $(echo blah)}
16947 If you do use @samp{$(@var{commands})}, make sure that the commands
16948 do not start with a parenthesis, as that would cause confusion with
16949 a different notation @samp{$((@var{expression}))} that in modern
16950 shells is an arithmetic expression not a command. To avoid the
16951 confusion, insert a space between the two opening parentheses.
16953 Avoid @var{commands} that contain unbalanced parentheses in
16954 here-documents, comments, or case statement patterns, as many shells
16955 mishandle them. For example, Bash 3.1, @samp{ksh88}, @command{pdksh}
16956 5.2.14, and Zsh 4.2.6 all mishandle the following valid command:
16959 echo $(case x in x) echo hello;; esac)
16963 @item $((@var{expression}))
16964 @cindex @code{$((@var{expression}))}
16965 Arithmetic expansion is not portable as some shells (most
16966 notably Solaris 10 @command{/bin/sh}) don't support it.
16968 Among shells that do support @samp{$(( ))}, not all of them obey the
16969 Posix rule that octal and hexadecimal constants must be recognized:
16972 $ @kbd{bash -c 'echo $(( 010 + 0x10 ))'}
16974 $ @kbd{zsh -c 'echo $(( 010 + 0x10 ))'}
16976 $ @kbd{zsh -c 'emulate sh; echo $(( 010 + 0x10 ))'}
16978 $ @kbd{pdksh -c 'echo $(( 010 + 0x10 ))'}
16979 pdksh: 010 + 0x10 : bad number `0x10'
16980 $ @kbd{pdksh -c 'echo $(( 010 ))'}
16984 When it is available, using arithmetic expansion provides a noticeable
16985 speedup in script execution; but testing for support requires
16986 @command{eval} to avoid syntax errors. The following construct is used
16987 by @code{AS_VAR_ARITH} to provide arithmetic computation when all
16988 arguments are decimal integers without leading zeros, and all
16989 operators are properly quoted and appear as distinct arguments:
16992 if ( eval 'test $(( 1 + 1 )) = 2' ) 2>/dev/null; then
16993 eval 'func_arith ()
16995 func_arith_result=$(( $* ))
17000 func_arith_result=`expr "$@@"`
17004 foo=$func_arith_result
17009 @cindex @code{^} quoting
17010 Always quote @samp{^}, otherwise traditional shells such as
17011 @command{/bin/sh} on Solaris 10 treat this like @samp{|}.
17017 @section Assignments
17018 @cindex Shell assignments
17020 When setting several variables in a row, be aware that the order of the
17021 evaluation is undefined. For instance @samp{foo=1 foo=2; echo $foo}
17022 gives @samp{1} with Solaris 10 @command{/bin/sh}, but @samp{2} with Bash.
17024 @samp{;} to enforce the order: @samp{foo=1; foo=2; echo $foo}.
17026 Don't rely on the following to find @file{subdir/program}:
17029 PATH=subdir$PATH_SEPARATOR$PATH program
17033 as this does not work with Zsh 3.0.6. Use something like this
17037 (PATH=subdir$PATH_SEPARATOR$PATH; export PATH; exec program)
17040 Don't rely on the exit status of an assignment: Ash 0.2 does not change
17041 the status and propagates that of the last statement:
17044 $ @kbd{false || foo=bar; echo $?}
17046 $ @kbd{false || foo=`:`; echo $?}
17051 and to make things even worse, QNX 4.25 just sets the exit status
17055 $ @kbd{foo=`exit 1`; echo $?}
17059 To assign default values, follow this algorithm:
17063 If the default value is a literal and does not contain any closing
17067 : "$@{var='my literal'@}"
17071 If the default value contains no closing brace, has to be expanded, and
17072 the variable being initialized is not intended to be IFS-split
17073 (i.e., it's not a list), then use:
17076 : $@{var="$default"@}
17080 If the default value contains no closing brace, has to be expanded, and
17081 the variable being initialized is intended to be IFS-split (i.e., it's a list),
17085 var=$@{var="$default"@}
17089 If the default value contains a closing brace, then use:
17092 test $@{var+y@} || var="has a '@}'"
17096 In most cases @samp{var=$@{var="$default"@}} is fine, but in case of
17097 doubt, just use the last form. @xref{Shell Substitutions}, items
17098 @samp{$@{@var{var}:-@var{value}@}} and @samp{$@{@var{var}=@var{value}@}}
17102 @section Parentheses in Shell Scripts
17103 @cindex Shell parentheses
17105 Beware of two opening parentheses in a row, as many shell
17106 implementations treat them specially, and Posix says that a portable
17107 script cannot use @samp{((} outside the @samp{$((} form used for shell
17108 arithmetic. In traditional shells, @samp{((cat))} behaves like
17109 @samp{(cat)}; but many shells, including
17110 Bash and the Korn shell, treat @samp{((cat))} as an arithmetic
17111 expression equivalent to @samp{let "cat"}, and may or may not report an
17112 error when they detect that @samp{cat} is not a number. As another
17113 example, @samp{pdksh} 5.2.14 does not treat the following code
17114 as a traditional shell would:
17117 if ((true) || false); then
17123 To work around this problem, insert a space between the two opening
17124 parentheses. There is a similar problem and workaround with
17125 @samp{$((}; see @ref{Shell Substitutions}.
17128 @section Slashes in Shell Scripts
17129 @cindex Shell slashes
17131 Unpatched Tru64 5.1 @command{sh} omits the last slash of command-line
17132 arguments that contain two trailing slashes:
17135 $ @kbd{echo / // /// //// .// //.}
17138 $ @kbd{eval "echo \$x"}
17141 $ @kbd{echo abc | tr -t ab //}
17147 Unpatched Tru64 4.0 @command{sh} adds a slash after @samp{"$var"} if the
17148 variable is empty and the second double-quote is followed by a word that
17149 begins and ends with slash:
17152 $ @kbd{sh -xc 'p=; echo "$p"/ouch/'}
17158 However, our understanding is that patches are available, so perhaps
17159 it's not worth worrying about working around these horrendous bugs.
17161 @node Special Shell Variables
17162 @section Special Shell Variables
17163 @cindex Shell variables
17164 @cindex Special shell variables
17166 Some shell variables should not be used, since they can have a deep
17167 influence on the behavior of the shell. In order to recover a sane
17168 behavior from the shell, some variables should be unset; M4sh takes
17169 care of this and provides fallback values, whenever needed, to cater
17170 for a very old @file{/bin/sh} that does not support @command{unset}.
17171 (@pxref{Portable Shell, , Portable Shell Programming}).
17173 As a general rule, shell variable names containing a lower-case letter
17174 are safe; you can define and use these variables without worrying about
17175 their effect on the underlying system, and without worrying about
17176 whether the shell changes them unexpectedly. (The exception is the
17177 shell variable @code{status}, as described below.)
17179 Here is a list of names that are known to cause trouble. This list is
17180 not exhaustive, but you should be safe if you avoid the name
17181 @code{status} and names containing only upper-case letters and
17184 @c Alphabetical order, case insensitive, 'A' before 'a'.
17187 Not all shells correctly reset @samp{$?} after conditionals (@pxref{if,
17188 , Limitations of Shell Builtins}). Not all shells manage @samp{$?}
17189 correctly in shell functions (@pxref{Shell Functions}) or in traps
17190 (@pxref{trap, , Limitations of Shell Builtins}). Not all shells reset
17191 @samp{$?} to zero after an empty command.
17194 $ @kbd{bash -c 'false; $empty; echo $?'}
17196 $ @kbd{zsh -c 'false; $empty; echo $?'}
17202 Many shells reserve @samp{$_} for various purposes, e.g., the name of
17203 the last command executed.
17207 In Tru64, if @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
17208 the standard shell conform to Posix.
17212 When this variable is set it specifies a list of directories to search
17213 when invoking @code{cd} with a relative file name that did not start
17214 with @samp{./} or @samp{../}. Posix
17215 1003.1-2001 says that if a nonempty directory name from @env{CDPATH}
17216 is used successfully, @code{cd} prints the resulting absolute
17217 file name. Unfortunately this output can break idioms like
17218 @samp{abs=`cd src && pwd`} because @code{abs} receives the name twice.
17219 Also, many shells do not conform to this part of Posix; for
17220 example, @command{zsh} prints the result only if a directory name
17221 other than @file{.} was chosen from @env{CDPATH}.
17223 In practice the shells that have this problem also support
17224 @command{unset}, so you can work around the problem as follows:
17227 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
17230 You can also avoid output by ensuring that your directory name is
17231 absolute or anchored at @samp{./}, as in @samp{abs=`cd ./src && pwd`}.
17233 Configure scripts use M4sh, which automatically unsets @env{CDPATH} if
17234 possible, so you need not worry about this problem in those scripts.
17236 @item CLICOLOR_FORCE
17237 @evindex CLICOLOR_FORCE
17238 When this variable is set, some implementations of tools like
17239 @command{ls} attempt to add color to their output via terminal escape
17240 sequences, even when the output is not directed to a terminal, and can
17241 thus cause spurious failures in scripts. Configure scripts use M4sh,
17242 which automatically unsets this variable.
17246 In the MKS shell, case statements and file name generation are
17247 case-insensitive unless @env{DUALCASE} is nonzero.
17248 Autoconf-generated scripts export this variable when they start up.
17262 These variables should not matter for shell scripts, since they are
17263 supposed to affect only interactive shells. However, at least one
17264 shell (the pre-3.0 UWIN Korn shell) gets confused about
17265 whether it is interactive, which means that (for example) a @env{PS1}
17266 with a side effect can unexpectedly modify @samp{$?}. To work around
17267 this bug, M4sh scripts (including @file{configure} scripts) do something
17271 (unset ENV) >/dev/null 2>&1 && unset ENV MAIL MAILPATH
17278 (actually, there is some complication due to bugs in @command{unset};
17279 @pxref{unset, , Limitations of Shell Builtins}).
17283 The Korn shell uses @env{FPATH} to find shell functions, so avoid
17284 @env{FPATH} in portable scripts. @env{FPATH} is consulted after
17285 @env{PATH}, but you still need to be wary of tests that use @env{PATH}
17286 to find whether a command exists, since they might report the wrong
17287 result if @env{FPATH} is also set.
17290 @evindex GREP_OPTIONS
17291 When this variable is set, some implementations of @command{grep} honor
17292 these options, even if the options include direction to enable colored
17293 output via terminal escape sequences, and the result can cause spurious
17294 failures when the output is not directed to a terminal. Configure
17295 scripts use M4sh, which automatically unsets this variable.
17299 Long ago, shell scripts inherited @env{IFS} from the environment,
17300 but this caused many problems so modern shells ignore any environment
17301 settings for @env{IFS}.
17303 Don't set the first character of @env{IFS} to backslash. Indeed,
17304 Bourne shells use the first character (backslash) when joining the
17305 components in @samp{"$@@"} and some shells then reinterpret (!)@: the
17306 backslash escapes, so you can end up with backspace and other strange
17309 The proper value for @env{IFS} (in regular code, not when performing
17310 splits) is @samp{@key{SPC}@key{TAB}@key{RET}}. The first character is
17311 especially important, as it is used to join the arguments in @samp{$*};
17312 however, note that traditional shells, but also bash-2.04, fail to adhere
17313 to this and join with a space anyway.
17315 M4sh guarantees that @env{IFS} will have the default value at the
17316 beginning of a script, and many macros within autoconf rely on this
17317 setting. It is okay to use blocks of shell code that temporarily change
17318 the value of @env{IFS} in order to split on another character, but
17319 remember to restore it before expanding further macros.
17321 Unsetting @code{IFS} instead of resetting it to the default sequence
17322 is not suggested, since code that tries to save and restore the
17323 variable's value will incorrectly reset it to an empty value, thus
17324 disabling field splitting:
17328 # default separators used for field splitting
17334 # no field splitting performed
17347 @evindex LC_COLLATE
17349 @evindex LC_MESSAGES
17350 @evindex LC_MONETARY
17351 @evindex LC_NUMERIC
17354 You should set all these variables to @samp{C} because so much
17355 configuration code assumes the C locale and Posix requires that locale
17356 environment variables be set to @samp{C} if the C locale is desired;
17357 @file{configure} scripts and M4sh do that for you.
17358 Export these variables after setting them.
17360 @c However, some older, nonstandard
17361 @c systems (notably SCO) break if locale environment variables
17362 @c are set to @samp{C}, so when running on these systems
17363 @c Autoconf-generated scripts unset the variables instead.
17368 @env{LANGUAGE} is not specified by Posix, but it is a GNU
17369 extension that overrides @env{LC_ALL} in some cases, so you (or M4sh)
17373 @itemx LC_IDENTIFICATION
17374 @itemx LC_MEASUREMENT
17377 @itemx LC_TELEPHONE
17378 @evindex LC_ADDRESS
17379 @evindex LC_IDENTIFICATION
17380 @evindex LC_MEASUREMENT
17383 @evindex LC_TELEPHONE
17385 These locale environment variables are GNU extensions. They
17386 are treated like their Posix brethren (@env{LC_COLLATE},
17387 etc.)@: as described above.
17391 Most modern shells provide the current line number in @code{LINENO}.
17392 Its value is the line number of the beginning of the current command.
17393 M4sh, and hence Autoconf, attempts to execute @command{configure} with
17394 a shell that supports @code{LINENO}. If no such shell is available, it
17395 attempts to implement @code{LINENO} with a Sed prepass that replaces each
17396 instance of the string @code{$LINENO} (not followed by an alphanumeric
17397 character) with the line's number. In M4sh scripts you should execute
17398 @code{AS_LINENO_PREPARE} so that these workarounds are included in
17399 your script; configure scripts do this automatically in @code{AC_INIT}.
17401 You should not rely on @code{LINENO} within @command{eval} or shell
17402 functions, as the behavior differs in practice. The presence of a
17403 quoted newline within simple commands can alter which line number is
17404 used as the starting point for @code{$LINENO} substitutions within that
17405 command. Also, the possibility of the Sed prepass means that you should
17406 not rely on @code{$LINENO} when quoted, when in here-documents, or when
17407 line continuations are used. Subshells should be OK, though. In the
17408 following example, lines 1, 9, and 14 are portable, but the other
17409 instances of @code{$LINENO} do not have deterministic values:
17422 ( echo 9. $LINENO )
17423 eval 'echo 10. $LINENO'
17424 eval 'echo 11. $LINENO
17429 f () @{ echo $1 $LINENO;
17436 $ @kbd{bash-3.2 ./lineno}
17455 $ @kbd{zsh-4.3.4 ./lineno}
17474 $ @kbd{pdksh-5.2.14 ./lineno}
17493 $ @kbd{sed '=' <lineno |}
17499 > @kbd{ s,^\([0-9]*\)\(.*\)[$]LINENO\([^a-zA-Z0-9_]\),\1\2\1\3,}
17502 > @kbd{ s,^[0-9]*\n,,}
17524 In particular, note that @file{config.status} (and any other subsidiary
17525 script created by @code{AS_INIT_GENERATED}) might report line numbers
17526 relative to the parent script as a result of the potential Sed pass.
17530 When executing the command @samp{>foo}, @command{zsh} executes
17531 @samp{$NULLCMD >foo} unless it is operating in Bourne shell
17532 compatibility mode and the @command{zsh} version is newer
17533 than 3.1.6-dev-18. If you are using an older @command{zsh}
17534 and forget to set @env{NULLCMD},
17535 your script might be suspended waiting for data on its standard input.
17539 For @command{zsh} 4.3.10, @env{options} is treated as an associative
17540 array even after @code{emulate sh}, so it should not be used.
17542 @item PATH_SEPARATOR
17543 @evindex PATH_SEPARATOR
17544 On DJGPP systems, the @env{PATH_SEPARATOR} environment
17545 variable can be set to either @samp{:} or @samp{;} to control the path
17546 separator Bash uses to set up certain environment variables (such as
17547 @env{PATH}). You can set this variable to @samp{;} if you want
17548 @command{configure} to use @samp{;} as a separator; this might be useful
17549 if you plan to use non-Posix shells to execute files. @xref{File System
17550 Conventions}, for more information about @code{PATH_SEPARATOR}.
17552 @item POSIXLY_CORRECT
17553 @evindex POSIXLY_CORRECT
17554 In the GNU environment, exporting @env{POSIXLY_CORRECT} with any value
17555 (even empty) causes programs to try harder to conform to Posix.
17556 Autoconf does not directly manipulate this variable, but @command{bash}
17557 ties the shell variable @env{POSIXLY_CORRECT} to whether the script is
17558 running in Posix mode. Therefore, take care when exporting or unsetting
17559 this variable, so as not to change whether @command{bash} is in Posix
17563 $ @kbd{bash --posix -c 'set -o | grep posix}
17564 > @kbd{unset POSIXLY_CORRECT}
17565 > @kbd{set -o | grep posix'}
17572 Posix 1003.1-2001 requires that @command{cd} and
17573 @command{pwd} must update the @env{PWD} environment variable to point
17574 to the logical name of the current directory, but traditional shells
17575 do not support this. This can cause confusion if one shell instance
17576 maintains @env{PWD} but a subsidiary and different shell does not know
17577 about @env{PWD} and executes @command{cd}; in this case @env{PWD}
17578 points to the wrong directory. Use @samp{`pwd`} rather than
17583 Many shells provide @code{RANDOM}, a variable that returns a different
17584 integer each time it is used. Most of the time, its value does not
17585 change when it is not used, but on IRIX 6.5 the value changes all
17586 the time. This can be observed by using @command{set}. It is common
17587 practice to use @code{$RANDOM} as part of a file name, but code
17588 shouldn't rely on @code{$RANDOM} expanding to a nonempty string.
17592 This variable is an alias to @samp{$?} for @code{zsh} (at least 3.1.6),
17593 hence read-only. Do not use it.
17596 @node Shell Functions
17597 @section Shell Functions
17598 @cindex Shell Functions
17600 Nowadays, it is difficult to find a shell that does not support
17601 shell functions at all. However, some differences should be expected.
17603 When declaring a shell function, you must include whitespace between the
17604 @samp{)} after the function name and the start of the compound
17605 expression, to avoid upsetting @command{ksh}. While it is possible to
17606 use any compound command, most scripts use @samp{@{@dots{}@}}.
17609 $ @kbd{/bin/sh -c 'a()@{ echo hi;@}; a'}
17611 $ @kbd{ksh -c 'a()@{ echo hi;@}; a'}
17612 ksh: syntax error at line 1: `@}' unexpected
17613 $ @kbd{ksh -c 'a() @{ echo hi;@}; a'}
17617 Inside a shell function, you should not rely on the error status of a
17618 subshell if the last command of that subshell was @code{exit} or
17619 @code{trap}, as this triggers bugs in zsh 4.x; while Autoconf tries to
17620 find a shell that does not exhibit the bug, zsh might be the only shell
17621 present on the user's machine.
17623 Likewise, the state of @samp{$?} is not reliable when entering a shell
17624 function. This has the effect that using a function as the first
17625 command in a @command{trap} handler can cause problems.
17628 $ @kbd{bash -c 'foo() @{ echo $?; @}; trap foo 0; (exit 2); exit 2'; echo $?}
17631 $ @kbd{ash -c 'foo() @{ echo $?; @}; trap foo 0; (exit 2); exit 2'; echo $?}
17636 DJGPP bash 2.04 has a bug in that @command{return} from a
17637 shell function which also used a command substitution causes a
17638 segmentation fault. To work around the issue, you can use
17639 @command{return} from a subshell, or @samp{AS_SET_STATUS} as last command
17640 in the execution flow of the function (@pxref{Common Shell Constructs}).
17642 Not all shells treat shell functions as simple commands impacted by
17643 @samp{set -e}, for example with Solaris 10 @command{/bin/sh}:
17646 $ @kbd{bash -c 'f() @{ return 1; @}; set -e; f; echo oops'}
17647 $ @kbd{/bin/sh -c 'f() @{ return 1; @}; set -e; f; echo oops'}
17651 Shell variables and functions may share the same namespace, for example
17652 with Solaris 10 @command{/bin/sh}:
17655 $ @kbd{f () @{ :; @}; f=; f}
17660 For this reason, Autoconf (actually M4sh, @pxref{Programming in M4sh})
17661 uses the prefix @samp{as_fn_} for its functions.
17663 Handling of positional parameters and shell options varies among shells.
17664 For example, Korn shells reset and restore trace output (@samp{set -x})
17665 and other options upon function entry and exit. Inside a function,
17666 IRIX sh sets @samp{$0} to the function name.
17668 It is not portable to pass temporary environment variables to shell
17669 functions. Solaris 10 @command{/bin/sh} does not see the variable.
17670 Meanwhile, not all shells follow the Posix rule that the assignment must
17671 affect the current environment in the same manner as special built-ins.
17674 $ @kbd{/bin/sh -c 'func() @{ echo $a;@}; a=1 func; echo $a'}
17677 $ @kbd{ash -c 'func() @{ echo $a;@}; a=1 func; echo $a'}
17680 $ @kbd{bash -c 'set -o posix; func() @{ echo $a;@}; a=1 func; echo $a'}
17685 Some ancient Bourne shell variants with function support did not reset
17686 @samp{$@var{i}, @var{i} >= 0}, upon function exit, so effectively the
17687 arguments of the script were lost after the first function invocation.
17688 It is probably not worth worrying about these shells any more.
17690 With AIX sh, a @command{trap} on 0 installed in a shell function
17691 triggers at function exit rather than at script exit. @xref{trap, ,
17692 Limitations of Shell Builtins}.
17694 @node Limitations of Builtins
17695 @section Limitations of Shell Builtins
17696 @cindex Shell builtins
17697 @cindex Limitations of shell builtins
17699 No, no, we are serious: some shells do have limitations! :)
17701 You should always keep in mind that any builtin or command may support
17702 options, and therefore differ in behavior with arguments
17703 starting with a dash. For instance, even the innocent @samp{echo "$word"}
17704 can give unexpected results when @code{word} starts with a dash. It is
17705 often possible to avoid this problem using @samp{echo "x$word"}, taking
17706 the @samp{x} into account later in the pipe. Many of these limitations
17707 can be worked around using M4sh (@pxref{Programming in M4sh}).
17709 @c This table includes things like '@command{test} (files)', so we can't
17710 @c use @table @command.
17714 @prindex @command{.}
17715 Use @command{.} only with regular files (use @samp{test -f}). Bash
17716 2.03, for instance, chokes on @samp{. /dev/null}. Remember that
17717 @command{.} uses @env{PATH} if its argument contains no slashes. Also,
17718 some shells, including bash 3.2, implicitly append the current directory
17719 to this @env{PATH} search, even though Posix forbids it. So if you want
17720 to use @command{.} on a file @file{foo} in the current directory, you
17721 must use @samp{. ./foo}.
17723 Not all shells gracefully handle syntax errors within a sourced file.
17724 On one extreme, some non-interactive shells abort the entire script. On
17725 the other, @command{zsh} 4.3.10 has a bug where it fails to react to the
17729 $ @kbd{echo 'fi' > syntax}
17730 $ @kbd{bash -c '. ./syntax; echo $?'}
17731 ./syntax: line 1: syntax error near unexpected token `fi'
17732 ./syntax: line 1: `fi'
17734 $ @kbd{ash -c '. ./syntax; echo $?'}
17735 ./syntax: 1: Syntax error: "fi" unexpected
17736 $ @kbd{zsh -c '. ./syntax; echo $?'}
17737 ./syntax:1: parse error near `fi'
17744 @prindex @command{!}
17745 The Unix version 7 shell did not support
17746 negating the exit status of commands with @command{!}, and this feature
17747 is still absent from some shells (e.g., Solaris 10 @command{/bin/sh}).
17748 Other shells, such as FreeBSD @command{/bin/sh} or @command{ash}, have
17749 bugs when using @command{!}:
17752 $ @kbd{sh -c '! : | :'; echo $?}
17754 $ @kbd{ash -c '! : | :'; echo $?}
17756 $ @kbd{sh -c '! @{ :; @}'; echo $?}
17758 $ @kbd{ash -c '! @{ :; @}'; echo $?}
17760 Syntax error: "@}" unexpected
17764 Shell code like this:
17767 if ! cmp file1 file2 >/dev/null 2>&1; then
17768 echo files differ or trouble
17772 is therefore not portable in practice. Typically it is easy to rewrite
17776 cmp file1 file2 >/dev/null 2>&1 ||
17777 echo files differ or trouble
17780 In M4sh, the @code{AS_IF} macro provides an easy way to write these kinds
17784 AS_IF([cmp -s file file.new], [],
17785 [echo files differ or trouble])
17788 This kind of rewriting is needed in code outside macro definitions that
17789 calls other macros. @xref{Common Shell Constructs}. It is also useful
17790 inside macro definitions, where the @dfn{then} and @dfn{else} branches
17791 might contain macro arguments.
17793 More generally, one can always rewrite @samp{! @var{command}} as:
17796 AS_IF([@var{command}], [(exit 1)])
17799 @item @command{&&} and @command{||}
17800 @c --------------------------------
17801 @prindex @command{&&}
17802 @prindex @command{||}
17803 If an AND-OR list is not inside @code{AC_DEFUN}, and it contains
17804 calls to Autoconf macros, it should be rewritten using @code{AS_IF}.
17805 @xref{Common Shell Constructs}. The operators @code{&&} and @code{||}
17806 have equal precedence and are left associative, so instead of:
17809 # This is dangerous outside AC_DEFUN.
17810 cmp a b >/dev/null 2>&1 &&
17811 AS_ECHO([files are same]) >$tmpfile ||
17812 AC_MSG_NOTICE([files differ, or echo failed])
17818 # This is OK outside AC_DEFUN.
17819 AS_IF([AS_IF([cmp a b >/dev/null 2>&1],
17820 [AS_ECHO([files are same]) >$tmpfile],
17822 [AC_MSG_NOTICE([files differ, or echo failed])])
17825 @item @command{@{...@}}
17826 @c --------------------
17827 @prindex @command{@{...@}}
17828 Bash 3.2 (and earlier versions) sometimes does not properly set
17829 @samp{$?} when failing to write redirected output of a compound command.
17830 This problem is most commonly observed with @samp{@{@dots{}@}}; it does
17831 not occur with @samp{(@dots{})}. For example:
17834 $ @kbd{bash -c '@{ echo foo; @} >/bad; echo $?'}
17835 bash: line 1: /bad: Permission denied
17837 $ @kbd{bash -c 'while :; do echo; done >/bad; echo $?'}
17838 bash: line 1: /bad: Permission denied
17842 To work around the bug, prepend @samp{:;}:
17845 $ @kbd{bash -c ':;@{ echo foo; @} >/bad; echo $?'}
17846 bash: line 1: /bad: Permission denied
17850 Posix requires a syntax error if a brace list has no contents. However,
17851 not all shells obey this rule; and on shells where empty lists are
17852 permitted, the effect on @samp{$?} is inconsistent. To avoid problems,
17853 ensure that a brace list is never empty.
17856 $ @kbd{bash -c 'false; @{ @}; echo $?' || echo $?}
17857 bash: line 1: syntax error near unexpected token `@}'
17858 bash: line 1: `false; @{ @}; echo $?'
17860 $ @kbd{zsh -c 'false; @{ @}; echo $?' || echo $?}
17862 $ @kbd{pdksh -c 'false; @{ @}; echo $?' || echo $?}
17867 @item @command{break}
17868 @c ------------------
17869 @prindex @command{break}
17870 The use of @samp{break 2} etc.@: is safe.
17874 @item @command{case}
17875 @c -----------------
17876 @prindex @command{case}
17877 If a @code{case} command is not inside @code{AC_DEFUN}, and it contains
17878 calls to Autoconf macros, it should be rewritten using @code{AS_CASE}.
17879 @xref{Common Shell Constructs}. Instead of:
17882 # This is dangerous outside AC_DEFUN.
17884 *.[ch]) AC_MSG_NOTICE([C source file]);;
17892 # This is OK outside AC_DEFUN.
17893 AS_CASE([$filename],
17894 [[*.[ch]]], [AC_MSG_NOTICE([C source file])])
17897 You don't need to quote the argument; no splitting is performed.
17899 You don't need the final @samp{;;}, but you should use it.
17901 Posix requires support for @code{case} patterns with opening
17902 parentheses like this:
17906 (*.c) echo "C source code";;
17911 but the @code{(} in this example is not portable to a few obsolescent Bourne
17912 shell implementations, which is a pity for those of us using tools that
17913 rely on balanced parentheses. For instance, with Solaris 10
17917 $ @kbd{case foo in (foo) echo foo;; esac}
17918 @error{}syntax error: `(' unexpected
17922 The leading @samp{(} can be omitted safely. Unfortunately, there are
17923 contexts where unbalanced parentheses cause other problems, such as when
17924 using a syntax-highlighting editor that searches for the balancing
17925 counterpart, or more importantly, when using a case statement as an
17926 underquoted argument to an Autoconf macro. @xref{Balancing
17927 Parentheses}, for trade-offs involved in various styles of dealing with
17928 unbalanced @samp{)}.
17930 Zsh handles pattern fragments derived from parameter expansions or
17931 command substitutions as though quoted:
17934 $ pat=\?; case aa in ?$pat) echo match;; esac
17935 $ pat=\?; case a? in ?$pat) echo match;; esac
17940 Because of a bug in its @code{fnmatch}, Bash fails to properly
17941 handle backslashes in character classes:
17944 bash-2.02$ @kbd{case /tmp in [/\\]*) echo OK;; esac}
17949 This is extremely unfortunate, since you are likely to use this code to
17950 handle Posix or MS-DOS absolute file names. To work around this
17951 bug, always put the backslash first:
17954 bash-2.02$ @kbd{case '\TMP' in [\\/]*) echo OK;; esac}
17956 bash-2.02$ @kbd{case /tmp in [\\/]*) echo OK;; esac}
17960 Many Bourne shells cannot handle closing brackets in character classes
17963 Some shells also have problems with backslash escaping in case you do not want
17964 to match the backslash: both a backslash and the escaped character match this
17965 pattern. To work around this, specify the character class in a variable, so
17966 that quote removal does not apply afterwards, and the special characters don't
17967 have to be backslash-escaped:
17970 $ @kbd{case '\' in [\<]) echo OK;; esac}
17972 $ @kbd{scanset='[<]'; case '\' in $scanset) echo OK;; esac}
17976 Even with this, Solaris @command{ksh} matches a backslash if the set
17978 of the characters @samp{|}, @samp{&}, @samp{(}, or @samp{)}.
17980 Conversely, Tru64 @command{ksh} (circa 2003) erroneously always matches
17981 a closing parenthesis if not specified in a character class:
17984 $ @kbd{case foo in *\)*) echo fail ;; esac}
17986 $ @kbd{case foo in *')'*) echo fail ;; esac}
17990 Some shells, such as Ash 0.3.8, are confused by an empty
17991 @code{case}/@code{esac}:
17994 ash-0.3.8 $ @kbd{case foo in esac;}
17995 @error{}Syntax error: ";" unexpected (expecting ")")
17998 Posix requires @command{case} to give an exit status of 0 if no cases
17999 match. However, @command{/bin/sh} in Solaris 10 does not obey this
18000 rule. Meanwhile, it is unclear whether a case that matches, but
18001 contains no statements, must also change the exit status to 0. The M4sh
18002 macro @code{AS_CASE} works around these inconsistencies.
18005 $ @kbd{bash -c 'case `false` in ?) ;; esac; echo $?'}
18007 $ @kbd{/bin/sh -c 'case `false` in ?) ;; esac; echo $?'}
18014 @prindex @command{cd}
18015 Posix 1003.1-2001 requires that @command{cd} must support
18016 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
18017 with @option{-L} being the default. However, traditional shells do
18018 not support these options, and their @command{cd} command has the
18019 @option{-P} behavior.
18021 Portable scripts should assume neither option is supported, and should
18022 assume neither behavior is the default. This can be a bit tricky,
18023 since the Posix default behavior means that, for example,
18024 @samp{ls ..} and @samp{cd ..} may refer to different directories if
18025 the current logical directory is a symbolic link. It is safe to use
18026 @code{cd @var{dir}} if @var{dir} contains no @file{..} components.
18027 Also, Autoconf-generated scripts check for this problem when computing
18028 variables like @code{ac_top_srcdir} (@pxref{Configuration Actions}),
18029 so it is safe to @command{cd} to these variables.
18031 Posix states that behavior is undefined if @command{cd} is given an
18032 explicit empty argument. Some shells do nothing, some change to the
18033 first entry in @env{CDPATH}, some change to @env{HOME}, and some exit
18034 the shell rather than returning an error. Unfortunately, this means
18035 that if @samp{$var} is empty, then @samp{cd "$var"} is less predictable
18036 than @samp{cd $var} (at least the latter is well-behaved in all shells
18037 at changing to @env{HOME}, although this is probably not what you wanted
18038 in a script). You should check that a directory name was supplied
18039 before trying to change locations.
18041 @xref{Special Shell Variables}, for portability problems involving
18042 @command{cd} and the @env{CDPATH} environment variable.
18043 Also please see the discussion of the @command{pwd} command.
18047 @item @command{echo}
18048 @c -----------------
18049 @prindex @command{echo}
18050 The simple @command{echo} is probably the most surprising source of
18051 portability troubles. It is not possible to use @samp{echo} portably
18052 unless both options and escape sequences are omitted. Don't expect any
18055 Do not use backslashes in the arguments, as there is no consensus on
18056 their handling. For @samp{echo '\n' | wc -l}, the @command{sh} of
18057 Solaris 10 outputs 2,
18058 but Bash and Zsh (in @command{sh} emulation mode) output 1.
18059 The problem is truly @command{echo}: all the shells
18060 understand @samp{'\n'} as the string composed of a backslash and an
18061 @samp{n}. Within a command substitution, @samp{echo 'string\c'} will
18062 mess up the internal state of ksh88 on AIX 6.1 so that it will print
18063 the first character @samp{s} only, followed by a newline, and then
18064 entirely drop the output of the next echo in a command substitution.
18066 Because of these problems, do not pass a string containing arbitrary
18067 characters to @command{echo}. For example, @samp{echo "$foo"} is safe
18068 only if you know that @var{foo}'s value cannot contain backslashes and
18069 cannot start with @samp{-}.
18071 Normally, @command{printf} is safer and easier to use than @command{echo}
18072 and @command{echo -n}. Thus, you should use @command{printf "%s\n"}
18073 instead of @command{echo}, and similarly use @command{printf %s} instead
18074 of @command{echo -n}.
18076 Older scripts, written before @command{printf} was portable,
18077 sometimes used a here-document as a safer alternative to @command{echo},
18087 @item @command{eval}
18088 @c -----------------
18089 @prindex @command{eval}
18090 The @command{eval} command is useful in limited circumstances, e.g.,
18091 using commands like @samp{eval table_$key=\$value} and @samp{eval
18092 value=table_$key} to simulate a hash table when the key is known to be
18095 You should also be wary of common bugs in @command{eval} implementations.
18096 In some shell implementations (e.g., older @command{ash}, OpenBSD 3.8
18097 @command{sh}, @command{pdksh} v5.2.14 99/07/13.2, and @command{zsh}
18098 4.2.5), the arguments of @samp{eval} are evaluated in a context where
18099 @samp{$?} is 0, so they exhibit behavior like this:
18102 $ @kbd{false; eval 'echo $?'}
18106 The correct behavior here is to output a nonzero value,
18107 but portable scripts should not rely on this.
18109 You should not rely on @code{LINENO} within @command{eval}.
18110 @xref{Special Shell Variables}.
18112 Note that, even though these bugs are easily avoided,
18113 @command{eval} is tricky to use on arbitrary arguments.
18114 It is obviously unwise to use @samp{eval $cmd} if the string value of
18115 @samp{cmd} was derived from an untrustworthy source. But even if the
18116 string value is valid, @samp{eval $cmd} might not work as intended,
18117 since it causes field splitting and file name expansion to occur twice,
18118 once for the @command{eval} and once for the command itself. It is
18119 therefore safer to use @samp{eval "$cmd"}. For example, if @var{cmd}
18120 has the value @samp{cat test?.c}, @samp{eval $cmd} might expand to the
18121 equivalent of @samp{cat test;.c} if there happens to be a file named
18122 @file{test;.c} in the current directory; and this in turn
18123 mistakenly attempts to invoke @command{cat} on the file @file{test} and
18124 then execute the command @command{.c}. To avoid this problem, use
18125 @samp{eval "$cmd"} rather than @samp{eval $cmd}.
18127 However, suppose that you want to output the text of the evaluated
18128 command just before executing it. Assuming the previous example,
18129 @samp{echo "Executing: $cmd"} outputs @samp{Executing: cat test?.c}, but
18130 this output doesn't show the user that @samp{test;.c} is the actual name
18131 of the copied file. Conversely, @samp{eval "echo Executing: $cmd"}
18132 works on this example, but it fails with @samp{cmd='cat foo >bar'},
18133 since it mistakenly replaces the contents of @file{bar} by the
18134 string @samp{cat foo}. No simple, general, and portable solution to
18135 this problem is known.
18137 @item @command{exec}
18138 @c -----------------
18139 @prindex @command{exec}
18140 Posix describes several categories of shell built-ins. Special
18141 built-ins (such as @command{exit}) must impact the environment of the
18142 current shell, and need not be available through @command{exec}. All
18143 other built-ins are regular, and must not propagate variable assignments
18144 to the environment of the current shell. However, the group of regular
18145 built-ins is further distinguished by commands that do not require a
18146 @env{PATH} search (such as @command{cd}), in contrast to built-ins that
18147 are offered as a more efficient version of something that must still be
18148 found in a @env{PATH} search (such as @command{echo}). Posix is not
18149 clear on whether @command{exec} must work with the list of 17 utilities
18150 that are invoked without a @env{PATH} search, and many platforms lack an
18151 executable for some of those built-ins:
18154 $ @kbd{sh -c 'exec cd /tmp'}
18155 sh: line 0: exec: cd: not found
18158 All other built-ins that provide utilities specified by Posix must have
18159 a counterpart executable that exists on @env{PATH}, although Posix
18160 allows @command{exec} to use the built-in instead of the executable.
18161 For example, contrast @command{bash} 3.2 and @command{pdksh} 5.2.14:
18164 $ @kbd{bash -c 'pwd --version' | head -n1}
18165 bash: line 0: pwd: --: invalid option
18166 pwd: usage: pwd [-LP]
18167 $ @kbd{bash -c 'exec pwd --version' | head -n1}
18168 pwd (GNU coreutils) 6.10
18169 $ @kbd{pdksh -c 'exec pwd --version' | head -n1}
18170 pdksh: pwd: --: unknown option
18173 When it is desired to avoid a regular shell built-in, the workaround is
18174 to use some other forwarding command, such as @command{env} or
18175 @command{nice}, that will ensure a path search:
18178 $ @kbd{pdksh -c 'exec true --version' | head -n1}
18179 $ @kbd{pdksh -c 'nice true --version' | head -n1}
18180 true (GNU coreutils) 6.10
18181 $ @kbd{pdksh -c 'env true --version' | head -n1}
18182 true (GNU coreutils) 6.10
18185 @item @command{exit}
18186 @c -----------------
18187 @prindex @command{exit}
18188 The default value of @command{exit} is supposed to be @code{$?};
18189 unfortunately, some shells, such as the DJGPP port of Bash 2.04, just
18190 perform @samp{exit 0}.
18193 bash-2.04$ @kbd{foo=`exit 1` || echo fail}
18195 bash-2.04$ @kbd{foo=`(exit 1)` || echo fail}
18197 bash-2.04$ @kbd{foo=`(exit 1); exit` || echo fail}
18201 Using @samp{exit $?} restores the expected behavior.
18203 Some shell scripts, such as those generated by @command{autoconf}, use a
18204 trap to clean up before exiting. If the last shell command exited with
18205 nonzero status, the trap also exits with nonzero status so that the
18206 invoker can tell that an error occurred.
18208 Unfortunately, in some shells, such as Solaris 10 @command{/bin/sh}, an exit
18209 trap ignores the @code{exit} command's argument. In these shells, a trap
18210 cannot determine whether it was invoked by plain @code{exit} or by
18211 @code{exit 1}. Instead of calling @code{exit} directly, use the
18212 @code{AC_MSG_ERROR} macro that has a workaround for this problem.
18216 @item @command{export}
18217 @c -------------------
18218 @prindex @command{export}
18219 The builtin @command{export} dubs a shell variable @dfn{environment
18220 variable}. Each update of exported variables corresponds to an update
18221 of the environment variables. Conversely, each environment variable
18222 received by the shell when it is launched should be imported as a shell
18223 variable marked as exported.
18225 Alas, many shells, such as Solaris 10 @command{/bin/sh},
18226 IRIX 6.3, IRIX 5.2,
18227 AIX 4.1.5, and Digital Unix 4.0, forget to
18228 @command{export} the environment variables they receive. As a result,
18229 two variables coexist: the environment variable and the shell
18230 variable. The following code demonstrates this failure:
18241 when run with @samp{FOO=foo} in the environment, these shells print
18242 alternately @samp{foo} and @samp{bar}, although they should print only
18243 @samp{foo} and then a sequence of @samp{bar}s.
18245 Therefore you should @command{export} again each environment variable
18246 that you update; the export can occur before or after the assignment.
18248 Posix is not clear on whether the @command{export} of an undefined
18249 variable causes the variable to be defined with the value of an empty
18250 string, or merely marks any future definition of a variable by that name
18251 for export. Various shells behave differently in this regard:
18254 $ @kbd{sh -c 'export foo; env | grep foo'}
18255 $ @kbd{ash -c 'export foo; env | grep foo'}
18259 Posix requires @command{export} to honor assignments made as arguments,
18260 but older shells do not support this, including @command{/bin/sh} in
18261 Solaris 10. Portable scripts should separate assignments and exports
18262 into different statements.
18265 $ @kbd{bash -c 'export foo=bar; echo $foo'}
18267 $ @kbd{/bin/sh -c 'export foo=bar; echo $foo'}
18268 /bin/sh: foo=bar: is not an identifier
18269 $ @kbd{/bin/sh -c 'export foo; foo=bar; echo $foo'}
18273 Posix requires @command{export} to work with any arbitrary value for the
18274 contents of the variable being exported, as long as the total size of
18275 the environment combined with arguments doesn't exceed @code{ARG_MAX}
18276 when executing a child process. However, some shells have extensions
18277 that involve interpreting some environment values specially, regardless
18278 of the variable name. We currently know of one case: all versions of
18279 Bash released prior to 27 September 2014 interpret an environment
18280 variable with an initial content substring of @code{() @{} as an
18281 exported function definition (this is the ``Shellshock'' remote
18282 execution bug, CVE-2014-6271 and friends, where it was possible to
18283 exploit the function parser to cause remote code execution on child bash
18284 startup; newer versions of Bash use special environment variable
18285 @emph{names} instead of values to implement the same feature).
18287 There may be entries inherited into the environment that are not valid
18288 as shell variable names; Posix states that processes should be tolerant
18289 of these names. Some shells such as @command{dash} do this by removing
18290 those names from the environment at startup, while others such as
18291 @command{bash} hide the entry from shell access but still pass it on to
18292 child processes. While you can set such names using @command{env} for a
18293 direct child process, you cannot rely on them being preserved through an
18294 intermediate pass through the shell.
18296 @item @command{false}
18297 @c ------------------
18298 @prindex @command{false}
18299 Don't expect @command{false} to exit with status 1: in native
18300 Solaris @file{/bin/false} exits with status 255.
18303 @item @command{for}
18304 @c ----------------
18305 @prindex @command{for}
18306 To loop over positional arguments, use:
18316 You may @emph{not} leave the @code{do} on the same line as @code{for},
18317 since some shells improperly grok:
18325 If you want to explicitly refer to the positional arguments, given the
18326 @samp{$@@} bug (@pxref{Shell Substitutions}), use:
18329 for arg in $@{1+"$@@"@}; do
18335 But keep in mind that Zsh, even in Bourne shell emulation mode, performs
18336 word splitting on @samp{$@{1+"$@@"@}}; see @ref{Shell Substitutions},
18337 item @samp{$@@}, for more.
18339 Posix requires support for a @command{for} loop with no list after
18340 @code{in}. However, Solaris 10 @command{/bin/sh} treats that as a syntax
18341 error. It is possible to work around this by providing any shell word
18342 that expands to nothing, or by ignoring an obvious sentinel.
18345 $ @kbd{/bin/sh -c 'for a in $empty; do echo hi; done'}
18346 $ @kbd{/bin/sh -c 'for a in ; do echo hi; done'}
18347 /bin/sh: syntax error at line 1: `;' unexpected
18350 This syntax problem is most frequently encountered in code that goes
18351 through several layers of expansion, such as an m4 macro or makefile
18352 variable used as a list body, where the first layer of expansion (m4 or
18353 make) can end up expanding to nothing in the version handed to the
18354 shell. In the makefile context, one common workaround is to use a shell
18355 variable rather than a make variable as the source of the list.
18358 $ @kbd{cat Makefile}
18361 @@for arg in $(list); do echo $$arg; done
18363 @@list='$(list)'; for arg in $$list; do echo $$arg; done
18364 $ @kbd{make bad 2&>1 | head -n1}
18365 sh: syntax error at line 1: `;' unexpected
18366 $ @kbd{make bad list='a b'}
18370 $ @kbd{make good list='a b'}
18375 In Solaris 10 @command{/bin/sh}, when the list of arguments of a
18376 @command{for} loop starts with @emph{unquoted} tokens looking like
18377 variable assignments, the loop is not executed on those tokens:
18380 $ @kbd{/bin/sh -c 'for v in a=b c=d x e=f; do echo $v; done'}
18386 Thankfully, quoting the assignment-like tokens, or starting the list
18387 with other tokens (including unquoted variable expansion that results in
18388 an assignment-like result), avoids the problem, so it is easy to work
18392 $ @kbd{/bin/sh -c 'for v in "a=b"; do echo $v; done'}
18394 $ @kbd{/bin/sh -c 'x=a=b; for v in $x c=d; do echo $v; done'}
18402 @prindex @command{if}
18403 If an @code{if} command is not inside @code{AC_DEFUN}, and it contains
18404 calls to Autoconf macros, it should be rewritten using @code{AS_IF}.
18405 @xref{Common Shell Constructs}.
18407 Using @code{if ! @dots{}} is not portable. @xref{!,,@command{!} notes}.
18409 Some very old shells did not reset the exit status from an @command{if}
18410 with no @command{else}:
18413 $ @kbd{if (exit 42); then true; fi; echo $?}
18418 whereas a proper shell should have printed @samp{0}. Although this is no
18419 longer a portability problem, as any shell that supports functions gets it
18420 correct, it explains why some makefiles have lengthy
18424 if test -f "$file"; then
18425 install "$file" "$dest"
18432 @item @command{printf}
18433 @c ------------------
18434 @prindex @command{printf}
18435 A format string starting with a @samp{-} can cause problems.
18436 Bash interprets it as an option and
18437 gives an error. And @samp{--} to mark the end of options is not good
18438 in the NetBSD Almquist shell (e.g., 0.4.6) which takes that
18439 literally as the format string. Putting the @samp{-} in a @samp{%c}
18440 or @samp{%s} is probably easiest:
18446 AIX 7.2 @command{sh} mishandles octal escapes in multi-byte locales by
18447 treating them as characters instead of bytes. For example, in a locale
18448 using the UTF-8 encoding, @samp{printf '\351'} outputs the two bytes C3,
18449 A9 (the UTF-8 encoding for U+00E9) instead of the desired single byte E9.
18450 To work around the bug, use the C locale.
18452 Bash 2.03 mishandles an escape sequence that happens to evaluate to @samp{%}:
18455 $ @kbd{printf '\045'}
18456 bash: printf: `%': missing format character
18459 Large outputs may cause trouble. On Solaris 10, for
18460 example, @file{/usr/bin/printf} is buggy, so when using
18461 @command{/bin/sh} the command @samp{printf %010000x 123} normally dumps
18464 Since @command{printf} is not always a shell builtin, there is a
18465 potential speed penalty for using @code{printf '%s\n'} as a replacement
18466 for an @command{echo} that does not interpret @samp{\} or leading
18467 @samp{-}. With Solaris @command{ksh}, it is possible to use @code{print
18468 -r --} for this role instead.
18470 @xref{echo, , Limitations of Shell Builtins}, for a discussion of
18471 portable alternatives to both @command{printf} and @command{echo}.
18474 @item @command{pwd}
18475 @c ----------------
18476 @prindex @command{pwd}
18477 With modern shells, plain @command{pwd} outputs a ``logical''
18478 directory name, some of whose components may be symbolic links. These
18479 directory names are in contrast to ``physical'' directory names, whose
18480 components are all directories.
18482 Posix 1003.1-2001 requires that @command{pwd} must support
18483 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
18484 with @option{-L} being the default. However, traditional shells do
18485 not support these options, and their @command{pwd} command has the
18486 @option{-P} behavior.
18488 Portable scripts should assume neither option is supported, and should
18489 assume neither behavior is the default. Also, on many hosts
18490 @samp{/bin/pwd} is equivalent to @samp{pwd -P}, but Posix
18491 does not require this behavior and portable scripts should not rely on
18494 Typically it's best to use plain @command{pwd}. On modern hosts this
18495 outputs logical directory names, which have the following advantages:
18499 Logical names are what the user specified.
18501 Physical names may not be portable from one installation
18502 host to another due to network file system gymnastics.
18504 On modern hosts @samp{pwd -P} may fail due to lack of permissions to
18505 some parent directory, but plain @command{pwd} cannot fail for this
18509 Also please see the discussion of the @command{cd} command.
18512 @item @command{read}
18513 @c -----------------
18514 @prindex @command{read}
18515 No options are portable, not even support @option{-r} (Solaris 10
18516 @command{/bin/sh} for example). Tru64/OSF 5.1 @command{sh} treats
18517 @command{read} as a special built-in, so it may exit if input is
18518 redirected from a non-existent or unreadable file.
18522 @item @command{set}
18523 @c ----------------
18524 @prindex @command{set}
18525 With the FreeBSD 6.0 shell, the @command{set} command (without
18526 any options) does not sort its output.
18528 The @command{set} builtin faces the usual problem with arguments
18530 dash. Modern shells such as Bash or Zsh understand @option{--} to specify
18531 the end of the options (any argument after @option{--} is a parameter,
18532 even @samp{-x} for instance), but many traditional shells (e.g., Solaris
18533 10 @command{/bin/sh}) simply stop option
18534 processing as soon as a non-option argument is found. Therefore, use
18535 @samp{dummy} or simply @samp{x} to end the option processing, and use
18536 @command{shift} to pop it out:
18539 set x $my_list; shift
18542 Avoid @samp{set -}, e.g., @samp{set - $my_list}. Posix no
18543 longer requires support for this command, and in traditional shells
18544 @samp{set - $my_list} resets the @option{-v} and @option{-x} options, which
18545 makes scripts harder to debug.
18547 Some nonstandard shells do not recognize more than one option
18548 (e.g., @samp{set -e -x} assigns @samp{-x} to the command line). It is
18549 better to combine them:
18555 @cindex @command{set -e}
18556 The @option{-e} option has historically been under-specified, with enough
18557 ambiguities to cause numerous differences across various shell
18558 implementations; see for example
18559 @uref{https://www.in-ulm.de/@/~mascheck/@/various/@/set-e/, this overview},
18560 or @uref{https://www.austingroupbugs.net/@/view.php?id=52, this link},
18561 documenting a change to Posix 2008 to match @command{ksh88} behavior.
18562 Note that mixing @code{set -e} and shell functions is asking for surprises:
18575 According to the recommendation, @samp{one} should always be output
18576 regardless of whether the @command{rm} failed, because it occurs within
18577 the body of the shell function @samp{doit} invoked on the left side of
18578 @samp{||}, where the effects of @samp{set -e} are not enforced.
18579 Likewise, @samp{two} should never be printed, since the failure of
18580 @command{rm} does not abort the function, such that the status of
18583 The BSD shell has had several problems with the @option{-e}
18584 option. Older versions of the BSD
18585 shell (circa 1990) mishandled @samp{&&}, @samp{||}, @samp{if}, and
18586 @samp{case} when @option{-e} was in effect, causing the shell to exit
18587 unexpectedly in some cases. This was particularly a problem with
18588 makefiles, and led to circumlocutions like @samp{sh -c 'test -f file ||
18589 touch file'}, where the seemingly-unnecessary @samp{sh -c '@dots{}'}
18590 wrapper works around the bug (@pxref{Failure in Make Rules}).
18592 Even relatively-recent versions of the BSD shell (e.g., OpenBSD 3.4)
18593 wrongly exit with @option{-e} if the last command within a compound
18594 statement fails and is guarded by an @samp{&&} only. For example:
18600 test -n "$foo" && exit 1
18603 test -n "$foo" && exit 1
18605 test -n "$foo" && exit 1
18611 does not print @samp{three}. One workaround is to change the last
18612 instance of @samp{test -n "$foo" && exit 1} to be @samp{if test -n
18613 "$foo"; then exit 1; fi} instead. Another possibility is to warn BSD
18614 users not to use @samp{sh -e}.
18616 When @samp{set -e} is in effect, a failed command substitution in
18617 Solaris 10 @command{/bin/sh} cannot be ignored, even with @samp{||}.
18620 $ @kbd{/bin/sh -c 'set -e; foo=`false` || echo foo; echo bar'}
18621 $ @kbd{bash -c 'set -e; foo=`false` || echo foo; echo bar'}
18627 Moreover, a command substitution, successful or not, causes this shell to
18628 exit from a failing outer command even in presence of an @samp{&&} list:
18631 $ @kbd{bash -c 'set -e; false `true` && echo notreached; echo ok'}
18633 $ @kbd{sh -c 'set -e; false `true` && echo notreached; echo ok'}
18637 Portable scripts should not use @samp{set -e} if @command{trap} is used
18638 to install an exit handler. This is because Tru64/OSF 5.1 @command{sh}
18639 sometimes enters the trap handler with the exit status of the command
18640 prior to the one that triggered the errexit handler:
18643 $ @kbd{sh -ec 'trap '\''echo $?'\'' 0; false'}
18645 $ @kbd{sh -c 'set -e; trap '\''echo $?'\'' 0; false'}
18650 Thus, when writing a script in M4sh, rather than trying to rely on
18651 @samp{set -e}, it is better to use @samp{AS_EXIT}
18652 where it is desirable to abort on failure.
18654 @cindex @command{set -b}
18655 @cindex @command{set -m}
18656 Job control is not provided by all shells, so the use of @samp{set -m}
18657 or @samp{set -b} must be done with care. When using @command{zsh} in
18658 native mode, asynchronous notification (@samp{set -b}) is enabled by
18659 default, and using @samp{emulate sh} to switch to Posix mode does not
18660 clear this setting (although asynchronous notification has no impact
18661 unless job monitoring is also enabled). Also, @command{zsh} 4.3.10 and
18662 earlier have a bug where job control can be manipulated in interactive
18663 shells, but not in subshells or scripts. Furthermore, some shells, like
18664 @command{pdksh}, fail to treat subshells as interactive, even though the
18668 $ @kbd{echo $ZSH_VERSION}
18670 $ @kbd{set -m; echo $?}
18672 $ @kbd{zsh -c 'set -m; echo $?'}
18673 set: can't change option: -m
18674 $ @kbd{(set -m); echo $?}
18675 set: can't change option: -m
18677 $ @kbd{pdksh -ci 'echo $-; (echo $-)'}
18682 @cindex @command{set -n}
18683 Use of @command{set -n} (typically via @command{sh -n script}) to
18684 validate a script is not foolproof. Modern @command{ksh93} tries to be
18685 helpful by informing you about better syntax, but switching the script
18686 to use the suggested syntax in order to silence the warnings would
18687 render the script no longer portable to older shells:
18690 $ @kbd{ksh -nc '``'}
18691 ksh: warning: line 1: `...` obsolete, use $(...)
18696 itself uses @command{sh -n} within its testsuite to check that correct
18697 scripts were generated, but only after first probing for other shell
18698 features (such as @code{test $@{BASH_VERSION+y@}}) that indicate
18699 a reasonably fast and working implementation.
18701 @item @command{shift}
18702 @c ------------------
18703 @prindex @command{shift}
18704 Not only is @command{shift}ing a bad idea when there is nothing left to
18705 shift, but in addition it is not portable: the shell of MIPS
18706 RISC/OS 4.52 refuses to do it.
18708 Don't use @samp{shift 2} etc.; while it in the SVR1 shell (1983),
18709 it is also absent in many pre-Posix shells.
18712 @item @command{source}
18713 @c -------------------
18714 @prindex @command{source}
18715 This command is not portable, as Posix does not require it; use
18716 @command{.} instead.
18719 @item @command{test}
18720 @c -----------------
18721 @prindex @command{test}
18722 The @code{test} program is the way to perform many file and string
18723 tests. It is often invoked by the alternate name @samp{[}, but using
18724 that name in Autoconf code is asking for trouble since it is an M4 quote
18727 The @option{-a}, @option{-o}, @samp{(}, and @samp{)} operands are not
18728 present in all implementations, and have been marked obsolete by Posix
18729 2008. This is because there are inherent ambiguities in using them.
18730 For example, @samp{test "$1" -a "$2"} looks like a binary operator to
18731 check whether two strings are both non-empty, but if @samp{$1} is the
18732 literal @samp{!}, then some implementations of @command{test} treat it
18733 as a negation of the unary operator @option{-a}.
18735 Thus, portable uses of @command{test} should never have more than four
18736 arguments, and scripts should use shell constructs like @samp{&&} and
18737 @samp{||} instead. If you combine @samp{&&} and @samp{||} in the same
18738 statement, keep in mind that they have equal precedence, so it is often
18739 better to parenthesize even when this is redundant. For example:
18743 test "X$a" = "X$b" -a \
18744 '(' "X$c" != "X$d" -o "X$e" = "X$f" ')'
18747 test "X$a" = "X$b" &&
18748 @{ test "X$c" != "X$d" || test "X$e" = "X$f"; @}
18751 @command{test} does not process options like most other commands do; for
18752 example, it does not recognize the @option{--} argument as marking the
18755 It is safe to use @samp{!} as a @command{test} operator. For example,
18756 @samp{if test ! -d foo; @dots{}} is portable even though @samp{if ! test
18757 -d foo; @dots{}} is not.
18760 @item @command{test} (files)
18761 @c -------------------------
18762 To enable @command{configure} scripts to support cross-compilation, they
18763 shouldn't do anything that tests features of the build system instead of
18764 the host system. But occasionally you may find it necessary to check
18765 whether some arbitrary file exists. To do so, use @samp{test -f},
18766 @samp{test -r}, or @samp{test -x}. Do not use @samp{test -e}, because
18767 Solaris 10 @command{/bin/sh}
18768 lacks it. To test for symbolic links on systems that have them, use
18769 @samp{test -h} rather than @samp{test -L}; either form conforms to
18770 Posix 1003.1-2001, but @option{-h} has been around longer.
18772 For historical reasons, Posix reluctantly allows implementations of
18773 @samp{test -x} that will succeed for the root user, even if no execute
18774 permissions are present. Furthermore, shells do not all agree on
18775 whether Access Control Lists should affect @samp{test -r}, @samp{test
18776 -w}, and @samp{test -x}; some shells base test results strictly on the
18777 current user id compared to file owner and mode, as if by
18778 @code{stat(2)}; while other shells base test results on whether the
18779 current user has the given right, even if that right is only granted by
18780 an ACL, as if by @code{faccessat(2)}. Furthermore, there is a classic
18781 time of check to time of use race between any use of @command{test}
18782 followed by operating on the just-checked file. Therefore, it is a good
18783 idea to write scripts that actually attempt an operation, and are
18784 prepared for the resulting failure if permission is denied, rather than
18785 trying to avoid an operation based solely on whether @command{test}
18786 guessed that it might not be permitted.
18788 @item @command{test} (strings)
18789 @c ---------------------------
18790 Posix says that @samp{test "@var{string}"} succeeds if @var{string} is
18791 not null, but this usage is not portable to traditional platforms like
18792 Solaris 10 @command{/bin/sh}, which mishandle strings like @samp{!} and
18793 @samp{-n}. However, it @emph{is} portable to test if a variable is set
18794 to a non-empty value, by using @samp{test $@{var+y@}}, since all known
18795 implementations properly distinguish between no arguments and a
18796 known-safe string of @samp{y}.
18798 Posix also says that @samp{test ! "@var{string}"},
18799 @samp{test -n "@var{string}"} and
18800 @samp{test -z "@var{string}"} work with any string, but many
18801 shells (such as Solaris 10, AIX 3.2, UNICOS 10.0.0.6,
18802 Digital Unix 4, etc.)@: get confused if
18803 @var{string} looks like an operator:
18807 test: argument expected
18809 test: argument expected
18810 $ @kbd{test -z ")"; echo $?}
18814 Similarly, Posix says that both @samp{test "@var{string1}" = "@var{string2"}}
18815 and @samp{test "@var{string1}" != "@var{string2"}} work for any pairs of
18816 strings, but in practice this is not true for troublesome strings that
18817 look like operators or parentheses, or that begin with @samp{-}.
18819 It is best to protect such strings with a leading @samp{X}, e.g.,
18820 @samp{test "X@var{string}" != X} rather than @samp{test -n
18821 "@var{string}"} or @samp{test ! "@var{string}"}.
18823 It is common to find variations of the following idiom:
18826 test -n "`echo $ac_feature | sed 's/[-a-zA-Z0-9_]//g'`" &&
18831 to take an action when a token matches a given pattern. Such constructs
18832 should be avoided by using:
18835 AS_CASE([$ac_feature],
18836 [[*[!-a-zA-Z0-9_]*]], [@var{action}])
18839 If the pattern is a complicated regular expression that cannot be
18840 expressed as a shell pattern, use something like this instead:
18843 expr "X$ac_feature" : 'X.*[^-a-zA-Z0-9_]' >/dev/null &&
18847 @samp{expr "X@var{foo}" : "X@var{bar}"} is more robust than @samp{echo
18848 "X@var{foo}" | grep "^X@var{bar}"}, because it avoids problems when
18849 @samp{@var{foo}} contains backslashes.
18853 @item @command{trap}
18854 @c -----------------
18855 @prindex @command{trap}
18856 It is safe to trap at least the signals 1, 2, 13, and 15. You can also
18857 trap 0, i.e., have the @command{trap} run when the script ends (either via an
18858 explicit @command{exit}, or the end of the script). The trap for 0 should be
18859 installed outside of a shell function, or AIX 5.3 @command{/bin/sh}
18860 will invoke the trap at the end of this function.
18862 Posix says that @samp{trap - 1 2 13 15} resets the traps for the
18863 specified signals to their default values, but many common shells (e.g.,
18864 Solaris 10 @command{/bin/sh}) misinterpret this and attempt to execute a
18865 ``command'' named @command{-} when the specified conditions arise.
18866 Posix 2008 also added a requirement to support @samp{trap 1 2 13 15} to
18867 reset traps, as this is supported by a larger set of shells, but there
18868 are still shells like @command{dash} that mistakenly try to execute
18869 @command{1} instead of resetting the traps. Therefore, there is no
18870 portable workaround, except for @samp{trap - 0}, for which
18871 @samp{trap '' 0} is a portable substitute.
18873 Although Posix is not absolutely clear on this point, it is widely
18874 admitted that when entering the trap @samp{$?} should be set to the exit
18875 status of the last command run before the trap. The ambiguity can be
18876 summarized as: ``when the trap is launched by an @command{exit}, what is
18877 the @emph{last} command run: that before @command{exit}, or
18878 @command{exit} itself?''
18880 Bash considers @command{exit} to be the last command, while Zsh and
18881 Solaris 10 @command{/bin/sh} consider that when the trap is run it is
18882 @emph{still} in the @command{exit}, hence it is the previous exit status
18883 that the trap receives:
18886 $ @kbd{cat trap.sh}
18889 $ @kbd{zsh trap.sh}
18891 $ @kbd{bash trap.sh}
18895 The portable solution is then simple: when you want to @samp{exit 42},
18896 run @samp{(exit 42); exit 42}, the first @command{exit} being used to
18897 set the exit status to 42 for Zsh, and the second to trigger the trap
18898 and pass 42 as exit status for Bash. In M4sh, this is covered by using
18901 The shell in FreeBSD 4.0 has the following bug: @samp{$?} is
18902 reset to 0 by empty lines if the code is inside @command{trap}.
18905 $ @kbd{trap 'false}
18913 Fortunately, this bug only affects @command{trap}.
18915 Several shells fail to execute an exit trap that is defined inside a
18916 subshell, when the last command of that subshell is not a builtin. A
18917 workaround is to use @samp{exit $?} as the shell builtin.
18920 $ @kbd{bash -c '(trap "echo hi" 0; /bin/true)'}
18922 $ @kbd{/bin/sh -c '(trap "echo hi" 0; /bin/true)'}
18923 $ @kbd{/bin/sh -c '(trap "echo hi" 0; /bin/true; exit $?)'}
18928 Likewise, older implementations of @command{bash} failed to preserve
18929 @samp{$?} across an exit trap consisting of a single cleanup command.
18932 $ @kbd{bash -c 'trap "/bin/true" 0; exit 2'; echo $?}
18934 $ @kbd{bash-2.05b -c 'trap "/bin/true" 0; exit 2'; echo $?}
18936 $ @kbd{bash-2.05b -c 'trap ":; /bin/true" 0; exit 2'; echo $?}
18940 Be aware that a trap can be called from any number of places in your
18941 script, and therefore the trap handler should not make assumptions about
18942 shell state. For some examples, if your script temporarily modifies
18943 @env{IFS}, then the trap should include an initialization back to its
18944 typical value of space-tab-newline (autoconf does this for generated
18945 @file{configure} files). Likewise, if your script changes the current
18946 working directory at some point after the trap is installed, then your
18947 trap cannot assume which directory it is in, and should begin by
18948 changing directories to an absolute path if that is important to the
18949 cleanup efforts (autotest does this for generated @file{testsuite}
18952 @item @command{true}
18953 @c -----------------
18954 @prindex @command{true}
18955 @c Info cannot handle ':' in index entries.
18956 @c @prindex @command{:}
18957 Don't worry: as far as we know @command{true} is portable.
18958 Nevertheless, it's not always a builtin (e.g., Bash 1.x), and the
18959 portable shell community tends to prefer using @command{:}. This has a
18960 funny side effect: when asked whether @command{false} is more portable
18961 than @command{true} Alexandre Oliva answered:
18964 In a sense, yes, because if it doesn't exist, the shell will produce an
18965 exit status of failure, which is correct for @command{false}, but not
18966 for @command{true}.
18969 Remember that even though @samp{:} ignores its arguments, it still takes
18970 time to compute those arguments. It is a good idea to use double quotes
18971 around any arguments to @samp{:} to avoid time spent in field splitting
18972 and file name expansion.
18976 @item @command{unset}
18977 @c ------------------
18978 @prindex @command{unset}
18979 In some nonconforming shells (e.g., Solaris 10 @command{/bin/ksh} and
18980 @command{/usr/xpg4/bin/sh}, NetBSD 5.99.43 sh, or Bash 2.05a),
18981 @code{unset FOO} fails when @code{FOO} is not set. This can interfere
18982 with @code{set -e} operation. You can use
18989 if you are not sure that @code{FOO} is set.
18991 A few ancient shells lack @command{unset} entirely. For some variables
18992 such as @code{PS1}, you can use a neutralizing value instead:
18998 Usually, shells that do not support @command{unset} need less effort to
18999 make the environment sane, so for example is not a problem if you cannot
19000 unset @command{CDPATH} on those shells. However, Bash 2.01 mishandles
19001 @code{unset MAIL} and @code{unset MAILPATH} in some cases and dumps core.
19002 So, you should do something like
19005 ( (unset MAIL) || exit 1) >/dev/null 2>&1 && unset MAIL || :
19009 @xref{Special Shell Variables}, for some neutralizing values. Also, see
19010 @ref{export, , Limitations of Builtins}, for
19011 the case of environment variables.
19013 @item @command{wait}
19014 @c -----------------
19015 @prindex @command{wait}
19016 The exit status of @command{wait} is not always reliable.
19019 @node Limitations of Usual Tools
19020 @section Limitations of Usual Tools
19021 @cindex Limitations of usual tools
19023 The small set of tools you can expect to find on any machine can still
19024 include some limitations you should be aware of.
19026 @comment Between this list and the list of builtins above, we should
19027 @comment mention all the tools in GNU Coding Standards ``Utilities in
19028 @comment Makefiles''.
19030 @c This table includes things like '@command{expr} (|)', so we can't
19031 @c use @table @command.
19034 @item @command{awk}
19035 @c ----------------
19036 @prindex @command{awk}
19037 Don't leave white space before the opening parenthesis in a user function call.
19038 Posix does not allow this and GNU Awk rejects it:
19041 $ @kbd{gawk 'function die () @{ print "Aaaaarg!" @}
19042 BEGIN @{ die () @}'}
19043 gawk: cmd. line:2: BEGIN @{ die () @}
19044 gawk: cmd. line:2: ^ parse error
19045 $ @kbd{gawk 'function die () @{ print "Aaaaarg!" @}
19046 BEGIN @{ die() @}'}
19050 Posix says that if a program contains only @samp{BEGIN} actions, and
19051 contains no instances of @code{getline}, then the program merely
19052 executes the actions without reading input. However, traditional Awk
19053 implementations (such as Solaris 10 @command{awk}) read and discard
19054 input in this case. Portable scripts can redirect input from
19055 @file{/dev/null} to work around the problem. For example:
19058 awk 'BEGIN @{print "hello world"@}' </dev/null
19061 Posix says that in an @samp{END} action, @samp{$NF} (and presumably,
19062 @samp{$1}) retain their value from the last record read, if no
19063 intervening @samp{getline} occurred. However, some implementations
19064 (such as Solaris 10 @samp{/usr/bin/awk}, @samp{nawk}, or Darwin
19065 @samp{awk}) reset these variables. A workaround is to use an
19066 intermediate variable prior to the @samp{END} block. For example:
19069 $ @kbd{cat end.awk}
19071 END @{ print "a", $1, $NF, "b", tmp @}
19072 $ @kbd{echo 1 | awk -f end.awk}
19074 $ @kbd{echo 1 | gawk -f end.awk}
19078 If you want your program to be deterministic, don't depend on @code{for}
19082 $ @kbd{cat for.awk}
19089 $ @kbd{gawk -f for.awk </dev/null}
19092 $ @kbd{nawk -f for.awk </dev/null}
19097 Some Awk implementations, such as HP-UX 11.0's native one,
19101 $ @kbd{echo xfoo | $AWK '/foo|^bar/ @{ print @}'}
19102 $ @kbd{echo bar | $AWK '/foo|^bar/ @{ print @}'}
19104 $ @kbd{echo xfoo | $AWK '/^bar|foo/ @{ print @}'}
19106 $ @kbd{echo bar | $AWK '/^bar|foo/ @{ print @}'}
19111 Either do not depend on such patterns (i.e., use @samp{/^(.*foo|bar)/},
19112 or use a simple test to reject such implementations.
19114 On @samp{ia64-hp-hpux11.23}, Awk mishandles @code{printf} conversions
19118 $ @kbd{awk 'BEGIN @{ printf "%u %d\n", 0, -1 @}'}
19122 AIX version 5.2 has an arbitrary limit of 399 on the
19123 length of regular expressions and literal strings in an Awk program.
19125 Traditional Awk implementations derived from Unix version 7, such as
19126 Solaris @command{/bin/awk}, have many limitations and do not
19127 conform to Posix. Nowadays @code{AC_PROG_AWK} (@pxref{Particular
19128 Programs}) finds you an Awk that doesn't have these problems, but if
19129 for some reason you prefer not to use @code{AC_PROG_AWK} you may need to
19130 address them. For more detailed descriptions, see @ref{Language
19131 History, , @command{awk} language history, gawk, GNU Awk User's Guide}.
19133 Traditional Awk does not support multidimensional arrays or user-defined
19136 Traditional Awk does not support the @option{-v} option. You can use
19137 assignments after the program instead, e.g., @code{$AWK '@{print v
19138 $1@}' v=x}; however, don't forget that such assignments are not
19139 evaluated until they are encountered (e.g., after any @code{BEGIN}
19142 Traditional Awk does not support the keywords @code{delete} or @code{do}.
19144 Traditional Awk does not support the expressions
19145 @code{@var{a}?@var{b}:@var{c}}, @code{!@var{a}}, @code{@var{a}^@var{b}},
19146 or @code{@var{a}^=@var{b}}.
19148 Traditional Awk does not support the predefined @code{CONVFMT} or
19149 @code{ENVIRON} variables.
19151 Traditional Awk supports only the predefined functions @code{exp}, @code{index},
19152 @code{int}, @code{length}, @code{log}, @code{split}, @code{sprintf},
19153 @code{sqrt}, and @code{substr}.
19155 Traditional Awk @code{getline} is not at all compatible with Posix;
19158 Traditional Awk has @code{for (i in a) @dots{}} but no other uses of the
19159 @code{in} keyword. For example, it lacks @code{if (i in a) @dots{}}.
19161 In code portable to both traditional and modern Awk, @code{FS} must be a
19162 string containing just one ordinary character, and similarly for the
19163 field-separator argument to @code{split}.
19165 Traditional Awk has a limit of 99 fields in a record. Since some Awk
19166 implementations, like Tru64's, split the input even if you don't refer
19167 to any field in the script, to circumvent this problem, set @samp{FS}
19168 to an unusual character and use @code{split}.
19170 Traditional Awk has a limit of at most 99 bytes in a number formatted by
19171 @code{OFMT}; for example, @code{OFMT="%.300e"; print 0.1;} typically
19174 The original version of Awk had a limit of at most 99 bytes per
19175 @code{split} field, 99 bytes per @code{substr} substring, and 99 bytes
19176 per run of non-special characters in a @code{printf} format, but these
19177 bugs have been fixed on all practical hosts that we know of.
19179 HP-UX 11.00 and IRIX 6.5 Awk require that input files have a line length
19180 of at most 3070 bytes.
19182 @item @command{basename}
19183 @c ---------------------
19184 @prindex @command{basename}
19185 Long ago some hosts lacked a working @command{basename},
19186 and portable scripts needed to use @command{expr} instead.
19187 Nowadays it is safe to use @command{basename}. For example:
19190 base=`basename -- "$file"`
19193 @c AS_BASENAME is to be replaced by a better API.
19195 Not all hosts have a working @command{basename}, and you should instead
19196 use @code{AS_BASENAME} (@pxref{Programming in M4sh}), followed by
19197 @command{expr} if you need to strip a suffix. For example:
19200 a=`basename "$aname"` # This is not portable.
19201 a=`AS_BASENAME(["$aname"])` # This is more portable.
19203 # This is not portable.
19204 c=`basename "$cname" .c`
19206 # This is more portable.
19207 c=`AS_BASENAME(["$cname"])`
19209 ?*.c) c=`expr "X$c" : 'X\(.*\)\.c'`;;
19215 @item @command{cat}
19216 @c ----------------
19217 @prindex @command{cat}
19218 Don't rely on any option.
19223 @prindex @command{cc}
19224 The command @samp{cc -c foo.c} traditionally produces an object file
19225 named @file{foo.o}. Most compilers allow @option{-c} to be combined
19226 with @option{-o} to specify a different object file name, but
19227 Posix does not require this combination and a few compilers
19228 lack support for it. @xref{C Compiler}, for how GNU Make
19229 tests for this feature with @code{AC_PROG_CC_C_O}.
19231 When a compilation such as @samp{cc -o foo foo.c} fails, some compilers
19232 (such as CDS on Reliant Unix) leave a @file{foo.o}.
19234 HP-UX @command{cc} doesn't accept @file{.S} files to preprocess and
19235 assemble. @samp{cc -c foo.S} appears to succeed, but in fact does
19238 The default executable, produced by @samp{cc foo.c}, can be
19241 @item @file{a.out} -- usual Posix convention.
19242 @item @file{b.out} -- i960 compilers (including @command{gcc}).
19243 @item @file{a.exe} -- DJGPP port of @command{gcc}.
19244 @item @file{a_out.exe} -- GNV @command{cc} wrapper for DEC C on OpenVMS.
19245 @item @file{foo.exe} -- various MS-DOS compilers.
19248 The C compiler's traditional name is @command{cc}, but other names like
19249 @command{gcc} are common. Posix 1003.1-2001 through 1003.1-2017 specify the
19250 name @command{c99}, but older Posix editions specified
19251 @command{c89}, future POSIX standards will likely specify
19252 other commands, and anyway these standard names are rarely used in
19253 practice. Typically the C compiler is invoked from makefiles that use
19254 @samp{$(CC)}, so the value of the @samp{CC} make variable selects the
19257 @item @command{chgrp}
19258 @itemx @command{chown}
19259 @c -------------------
19260 @prindex @command{chgrp}
19261 @prindex @command{chown}
19262 It is not portable to change a file's group to a group that the owner
19263 does not belong to.
19265 @item @command{chmod}
19266 @c ------------------
19267 @prindex @command{chmod}
19268 Avoid usages like @samp{chmod -w file}; use @samp{chmod a-w file}
19269 instead, for two reasons. First, plain @option{-w} does not necessarily
19270 make the file unwritable, since it does not affect mode bits that
19271 correspond to bits in the file mode creation mask. Second,
19272 Posix says that the @option{-w} might be interpreted as an
19273 implementation-specific option, not as a mode; Posix suggests
19274 using @samp{chmod -- -w file} to avoid this confusion, but unfortunately
19275 @samp{--} does not work on some older hosts.
19278 @item @command{cmp}
19279 @c ----------------
19280 @prindex @command{cmp}
19281 @command{cmp} performs a raw data comparison of two files, while
19282 @command{diff} compares two text files. Therefore, if you might compare
19283 DOS files, even if only checking whether two files are different, use
19284 @command{diff} to avoid spurious differences due to differences of
19290 @prindex @command{cp}
19291 Avoid the @option{-r} option, since Posix 1003.1-2004 marks it as
19292 obsolescent and its behavior on special files is implementation-defined.
19293 Use @option{-R} instead. On GNU hosts the two options
19294 are equivalent, but on Solaris hosts (for example) @code{cp -r}
19295 reads from pipes instead of replicating them. AIX 5.3 @code{cp -R} may
19296 corrupt its own memory with some directory hierarchies and error out or
19300 @kbd{mkdir -p 12345678/12345678/12345678/12345678}
19301 @kbd{touch 12345678/12345678/x}
19302 @kbd{cp -R 12345678 t}
19303 cp: 0653-440 12345678/12345678/: name too long.
19306 Some @command{cp} implementations (e.g., BSD/OS 4.2) do not allow
19307 trailing slashes at the end of nonexistent destination directories. To
19308 avoid this problem, omit the trailing slashes. For example, use
19309 @samp{cp -R source /tmp/newdir} rather than @samp{cp -R source
19310 /tmp/newdir/} if @file{/tmp/newdir} does not exist.
19312 The @option{-f} option is portable nowadays.
19314 @cindex timestamp resolution
19315 Traditionally, file timestamps had 1-second resolution, and @samp{cp
19316 -p} copied the timestamps exactly. However, many modern file systems
19317 have timestamps with 1-nanosecond resolution. Unfortunately, some older
19318 @samp{cp -p} implementations truncate timestamps when copying files,
19319 which can cause the destination file to appear to be older than the
19320 source. The exact amount of truncation depends on the resolution of
19321 the system calls that @command{cp} uses. Traditionally this was
19322 @code{utime}, which has 1-second resolution. Less-ancient @command{cp}
19323 implementations such as GNU Core Utilities 5.0.91 (2003) use
19324 @code{utimes}, which has 1-microsecond resolution. Modern
19325 implementations such as GNU Core Utilities 6.12 (2008) can set timestamps to
19326 the full nanosecond resolution, using the modern system calls
19327 @code{futimens} and @code{utimensat} when they are available. As of
19328 2011, though, many platforms do not yet fully support these new system
19331 Bob Proulx notes that @samp{cp -p} always @emph{tries} to copy
19332 ownerships. But whether it actually does copy ownerships or not is a
19333 system dependent policy decision implemented by the kernel. If the
19334 kernel allows it then it happens. If the kernel does not allow it then
19335 it does not happen. It is not something @command{cp} itself has control
19338 In Unix System V any user can chown files to any other user, and System
19339 V also has a non-sticky @file{/tmp}. That probably derives from the
19340 heritage of System V in a business environment without hostile users.
19342 to be a more secure model where only root can @command{chown} files and
19343 a sticky @file{/tmp} is used. That undoubtedly derives from the heritage
19344 of BSD in a campus environment.
19346 GNU/Linux and Solaris by default follow BSD, but
19347 can be configured to allow a System V style @command{chown}. On the
19348 other hand, HP-UX follows System V, but can
19349 be configured to use the modern security model and disallow
19350 @command{chown}. Since it is an administrator-configurable parameter
19351 you can't use the name of the kernel as an indicator of the behavior.
19355 @item @command{date}
19356 @c -----------------
19357 @prindex @command{date}
19358 Some versions of @command{date} do not recognize special @samp{%} directives,
19359 and unfortunately, instead of complaining, they just pass them through,
19360 and exit with success:
19364 OSF1 medusa.sis.pasteur.fr V5.1 732 alpha
19370 @item @command{diff}
19371 @c -----------------
19372 @prindex @command{diff}
19373 Option @option{-u} is nonportable.
19375 Some implementations, such as Tru64's, fail when comparing to
19376 @file{/dev/null}. Use an empty file instead.
19379 @item @command{dirname}
19380 @c --------------------
19381 @prindex @command{dirname}
19382 Long ago some hosts lacked a working @command{dirname} and portable
19383 scripts needed to use use @code{AS_DIRNAME} (@pxref{Programming in M4sh}).
19384 Nowadays @command{dirname} suffices and the following are equivalent:
19387 dir=`dirname -- "$file"`
19388 dir=`AS_DIRNAME(["$file"])`
19392 @item @command{egrep}
19393 @c ------------------
19394 @prindex @command{egrep}
19395 Although Posix stopped requiring @command{egrep} in 2001,
19396 a few traditional hosts (notably Solaris 10) do not support the Posix
19397 replacement @code{grep -E}. Also, some traditional implementations do
19398 not work on long input lines. To work around these problems, invoke
19399 @code{AC_PROG_EGREP} and then use @code{$EGREP}.
19401 Portable extended regular expressions should use @samp{\} only to escape
19402 characters in the string @samp{$()*+.?[\^@{|}. For example, @samp{\@}}
19403 is not portable, even though it typically matches @samp{@}}.
19405 The empty alternative is not portable. Use @samp{?} instead. For
19406 instance with Digital Unix v5.0:
19409 > printf "foo\n|foo\n" | $EGREP '^(|foo|bar)$'
19411 > printf "bar\nbar|\n" | $EGREP '^(foo|bar|)$'
19413 > printf "foo\nfoo|\n|bar\nbar\n" | $EGREP '^(foo||bar)$'
19418 For more information about what can appear in portable extended regular
19419 expressions, @pxref{Problematic Expressions,,,grep, GNU Grep}.
19421 @command{$EGREP} also suffers the limitations of @command{grep}
19422 (@pxref{grep, , Limitations of Usual Tools}).
19424 @item @command{expr}
19425 @c -----------------
19426 @prindex @command{expr}
19427 Not all implementations obey the Posix rule that @samp{--} separates
19428 options from arguments; likewise, not all implementations provide the
19429 extension to Posix that the first argument can be treated as part of a
19430 valid expression rather than an invalid option if it begins with
19431 @samp{-}. When performing arithmetic, use @samp{expr 0 + $var} if
19432 @samp{$var} might be a negative number, to keep @command{expr} from
19433 interpreting it as an option.
19435 No @command{expr} keyword starts with @samp{X}, so use @samp{expr
19436 X"@var{word}" : 'X@var{regex}'} to keep @command{expr} from
19437 misinterpreting @var{word}.
19439 Don't use @code{length}, @code{substr}, @code{match} and @code{index}.
19441 @item @command{expr} (@samp{|})
19442 @prindex @command{expr} (@samp{|})
19443 You can use @samp{|}. Although Posix does require that @samp{expr
19444 ''} return the empty string, it does not specify the result when you
19445 @samp{|} together the empty string (or zero) with the empty string. For
19452 Posix 1003.2-1992 returns the empty string
19453 for this case, but traditional Unix returns @samp{0} (Solaris is
19454 one such example). In Posix 1003.1-2001, the specification was
19455 changed to match traditional Unix's behavior (which is
19456 bizarre, but it's too late to fix this). Please note that the same
19457 problem does arise when the empty string results from a computation,
19461 expr bar : foo \| foo : bar
19465 Avoid this portability problem by avoiding the empty string.
19468 @item @command{expr} (@samp{:})
19469 @c ----------------------------
19470 @prindex @command{expr}
19471 Portable @command{expr} regular expressions should use @samp{\} to
19472 escape only characters in the string @samp{$()*.123456789[\^@{@}}.
19473 For example, alternation, @samp{\|}, is common but Posix does not
19474 require its support, so it should be avoided in portable scripts.
19475 Similarly, @samp{\+} and @samp{\?} should be avoided.
19477 Portable @command{expr} regular expressions should not begin with
19478 @samp{^}. Patterns are automatically anchored so leading @samp{^} is
19481 On the other hand, the behavior of the @samp{$} anchor is not portable
19482 on multi-line strings. Posix is ambiguous whether the anchor applies to
19483 each line, as was done in older versions of the GNU Core Utilities, or
19484 whether it applies only to the end of the overall string, as in
19485 Coreutils 6.0 and most other implementations.
19490 $ @kbd{expr "X$baz" : 'X\(foo\)$'}
19492 $ @kbd{expr-5.97 "X$baz" : 'X\(foo\)$'}
19496 The Posix standard is ambiguous as to whether
19497 @samp{expr 'a' : '\(b\)'} outputs @samp{0} or the empty string.
19498 In practice, it outputs the empty string on most platforms, but portable
19499 scripts should not assume this. For instance, the QNX 4.25 native
19500 @command{expr} returns @samp{0}.
19502 One might think that a way to get a uniform behavior would be to use
19503 the empty string as a default value:
19506 expr a : '\(b\)' \| ''
19510 Unfortunately this behaves exactly as the original expression; see the
19511 @command{expr} (@samp{|}) entry for more information.
19513 Some ancient @command{expr} implementations (e.g.,
19514 Solaris 10 @command{/usr/ucb/expr}) have a silly length limit that causes
19515 @command{expr} to fail if the matched substring is longer than 120
19516 bytes. In this case, you might want to fall back on @samp{echo|sed} if
19517 @command{expr} fails. Nowadays this is of practical importance only for
19518 the rare installer who mistakenly puts @file{/usr/ucb} before
19519 @file{/usr/bin} in @env{PATH} on Solaris 10.
19521 On Mac OS X 10.4, @command{expr} mishandles the pattern @samp{[^-]} in
19522 some cases. For example, the command
19524 expr Xpowerpc-apple-darwin8.1.0 : 'X[^-]*-[^-]*-\(.*\)'
19528 outputs @samp{apple-darwin8.1.0} rather than the correct @samp{darwin8.1.0}.
19529 This particular case can be worked around by substituting @samp{[^--]}
19532 Don't leave, there is some more!
19534 The QNX 4.25 @command{expr}, in addition of preferring @samp{0} to
19535 the empty string, has a funny behavior in its exit status: it's always 1
19536 when parentheses are used!
19539 $ @kbd{val=`expr 'a' : 'a'`; echo "$?: $val"}
19541 $ @kbd{val=`expr 'a' : 'b'`; echo "$?: $val"}
19544 $ @kbd{val=`expr 'a' : '\(a\)'`; echo "?: $val"}
19546 $ @kbd{val=`expr 'a' : '\(b\)'`; echo "?: $val"}
19551 In practice this can be a big problem if you are ready to catch failures
19552 of @command{expr} programs with some other method (such as using
19553 @command{sed}), since you may get twice the result. For instance
19556 $ @kbd{expr 'a' : '\(a\)' || echo 'a' | sed 's/^\(a\)$/\1/'}
19560 outputs @samp{a} on most hosts, but @samp{aa} on QNX 4.25. A
19561 simple workaround consists of testing @command{expr} and using a variable
19562 set to @command{expr} or to @command{false} according to the result.
19564 Tru64 @command{expr} incorrectly treats the result as a number, if it
19565 can be interpreted that way:
19568 $ @kbd{expr 00001 : '.*\(...\)'}
19572 On HP-UX 11, @command{expr} only supports a single
19576 $ @kbd{expr 'Xfoo' : 'X\(f\(oo\)*\)$'}
19577 expr: More than one '\(' was used.
19581 @item @command{fgrep}
19582 @c ------------------
19583 @prindex @command{fgrep}
19584 Although Posix stopped requiring @command{fgrep} in 2001,
19585 a few traditional hosts (notably Solaris 10) do not support the Posix
19586 replacement @code{grep -F}. Also, some traditional implementations do
19587 not work on long input lines. To work around these problems, invoke
19588 @code{AC_PROG_FGREP} and then use @code{$FGREP}.
19590 Tru64/OSF 5.1 @command{fgrep} does not match an empty pattern.
19593 @item @command{find}
19594 @c -----------------
19595 @prindex @command{find}
19596 Many operands of GNU @command{find} are not standardized by Posix and
19597 are missing on many platforms. These nonportable operands include
19598 @option{-follow}, @option{-maxdepth}, @option{-mindepth},
19599 @option{-printf}, and @option{,}. See the
19600 @uref{https://@/pubs.opengroup.org/@/onlinepubs/@/9699919799/@/utilities/@/find.html,
19601 Posix spec for @command{find}} for @command{find} operands that
19602 should be portable nowadays.
19604 The replacement of @samp{@{@}} is guaranteed only if the argument is
19605 exactly @emph{@{@}}, not if it's only a part of an argument. For
19606 instance, on HP-UX 11:
19610 $ @kbd{find . -name foo -exec echo "@{@}-@{@}" \;}
19615 while GNU @command{find} reports @samp{./foo-./foo}.
19616 Posix allows either behavior.
19620 @item @command{grep}
19621 @c -----------------
19622 @prindex @command{grep}
19623 Portable scripts can rely on the @command{grep} options @option{-c},
19624 @option{-l}, @option{-n}, and @option{-v}, but should avoid other
19625 options. For example, don't use @option{-w}, as Posix does not require
19626 it and Irix 6.5.16m's @command{grep} does not support it. Also,
19627 portable scripts should not combine @option{-c} with @option{-l},
19628 as Posix does not allow this.
19630 Some of the options required by Posix are not portable in practice.
19631 Don't use @samp{grep -q} to suppress output, because traditional @command{grep}
19632 implementations (e.g., Solaris 10) do not support @option{-q}.
19633 Don't use @samp{grep -s} to suppress output either, because Posix
19634 says @option{-s} does not suppress output, only some error messages;
19635 also, the @option{-s} option of traditional @command{grep} behaved
19636 like @option{-q} does in most modern implementations. Instead,
19637 redirect the standard output and standard error (in case the file
19638 doesn't exist) of @code{grep} to @file{/dev/null}. Check the exit
19639 status of @code{grep} to determine whether it found a match.
19641 The QNX4 implementation fails to count lines with @code{grep -c '$'},
19642 but works with @code{grep -c '^'}. Other alternatives for counting
19643 lines are to use @code{sed -n '$='} or @code{wc -l}.
19645 Some traditional @command{grep} implementations do not work on long
19646 input lines. On AIX the default @code{grep} silently truncates long
19647 lines on the input before matching.
19649 Also, traditional implementations do not support multiple regexps
19650 with @option{-e}: they either reject @option{-e} entirely (e.g., Solaris 10)
19651 or honor only the last pattern (e.g., IRIX 6.5 and NeXT). To
19652 work around these problems, invoke @code{AC_PROG_GREP} and then use
19655 Another possible workaround for the multiple @option{-e} problem is to
19656 separate the patterns by newlines, for example:
19664 except that this fails with traditional @command{grep}
19665 implementations and with OpenBSD 3.8 @command{grep}.
19667 Traditional @command{grep} implementations (e.g., Solaris 10) do not
19668 support the @option{-E} or @option{-F} options. To work around these
19669 problems, invoke @code{AC_PROG_EGREP} and then use @code{$EGREP}, and
19670 similarly for @code{AC_PROG_FGREP} and @code{$FGREP}. Even if you are
19671 willing to require support for Posix @command{grep}, your script should
19672 not use both @option{-E} and @option{-F}, since Posix does not allow
19675 Portable @command{grep} regular expressions should use @samp{\} only to
19676 escape characters in the string @samp{$()*.123456789[\^@{@}}. For example,
19677 alternation, @samp{\|}, is common but Posix does not require its
19678 support in basic regular expressions, so it should be avoided in
19679 portable scripts. Solaris and HP-UX @command{grep} do not support it.
19680 Similarly, the following escape sequences should also be avoided:
19681 @samp{\<}, @samp{\>}, @samp{\+}, @samp{\?}, @samp{\`}, @samp{\'},
19682 @samp{\B}, @samp{\b}, @samp{\S}, @samp{\s}, @samp{\W}, and @samp{\w}.
19683 For more information about what can appear in portable regular expressions,
19684 @pxref{Problematic Expressions,,, grep, GNU Grep}.
19686 Posix does not specify the behavior of @command{grep} on binary files.
19687 An example where this matters is using BSD @command{grep} to
19688 search text that includes embedded ANSI escape sequences for
19689 colored output to terminals (@samp{\033[m} is the sequence to restore
19690 normal output); the behavior depends on whether input is seekable:
19693 $ @kbd{printf 'esc\033[mape\n' > sample}
19694 $ @kbd{grep . sample}
19695 Binary file sample matches
19696 $ @kbd{cat sample | grep .}
19701 @item @command{join}
19702 @c -----------------
19703 @prindex @command{join}
19704 On NetBSD, @command{join -a 1 file1 file2} mistakenly behaves like
19705 @command{join -a 1 -a 2 1 file1 file2}, resulting in a usage warning;
19706 the workaround is to use @command{join -a1 file1 file2} instead.
19708 On some circa-2020 BSD-based systems @command{join} mishandles inputs
19709 with missing fields. For example, an empty line is not treated as
19710 containing an empty join field. As a workaround, input lines should
19711 always have a join field.
19713 On platforms with the BusyBox tools, the @command{join} command is
19714 entirely missing. As a workaround, you can simulate special cases of the
19715 @command{join} command using an @command{awk} script. For an example,
19716 see @url{https://lists.gnu.org/r/bug-gnulib/2021-04/msg00054.html}.
19721 @prindex @command{ln}
19722 The @option{-f} option is portable nowadays.
19724 @cindex Symbolic links
19725 Symbolic links are not available on some systems; use @samp{$(LN_S)} as
19726 a portable substitute.
19728 For versions of the DJGPP before 2.04,
19729 @command{ln} emulates symbolic links
19730 to executables by generating a stub that in turn calls the real
19731 program. This feature also works with nonexistent files like in the
19732 Posix spec. So @samp{ln -s file link} generates @file{link.exe},
19733 which attempts to call @file{file.exe} if run. But this feature only
19734 works for executables, so @samp{cp -p} is used instead for these
19735 systems. DJGPP versions 2.04 and later have full support
19736 for symbolic links.
19741 @prindex @command{ls}
19742 @cindex Listing directories
19743 The portable options are @option{-acdilrtu}. Current practice is for
19744 @option{-l} to output both owner and group, even though ancient versions
19745 of @command{ls} omitted the group.
19747 On ancient hosts, @samp{ls foo} sent the diagnostic @samp{foo not found}
19748 to standard output if @file{foo} did not exist. Hence a shell command
19749 like @samp{sources=`ls *.c 2>/dev/null`} did not always work, since it
19750 was equivalent to @samp{sources='*.c not found'} in the absence of
19751 @samp{.c} files. This is no longer a practical problem, since current
19752 @command{ls} implementations send diagnostics to standard error.
19754 The behavior of @command{ls} on a directory that is being concurrently
19755 modified is not always predictable, because of a data race where cached
19756 information returned by @code{readdir} does not match the current
19757 directory state. In fact, MacOS 10.5 has an intermittent bug where
19758 @code{readdir}, and thus @command{ls}, sometimes lists a file more than
19759 once if other files were added or removed from the directory immediately
19760 prior to the @command{ls} call. Since @command{ls} already sorts its
19761 output, the duplicate entries can be avoided by piping the results
19762 through @code{uniq}.
19765 @item @command{mkdir}
19766 @c ------------------
19767 @prindex @command{mkdir}
19768 @cindex Making directories
19769 Combining the @option{-m} and @option{-p} options, as in @samp{mkdir -m
19770 go-w -p @var{dir}}, often leads to trouble. FreeBSD
19771 @command{mkdir} incorrectly attempts to change the permissions of
19772 @var{dir} even if it already exists. HP-UX 11.23 and
19773 IRIX 6.5 @command{mkdir} often assign the wrong permissions to
19774 any newly-created parents of @var{dir}.
19776 Posix does not clearly specify whether @samp{mkdir -p foo}
19777 should succeed when @file{foo} is a symbolic link to an already-existing
19778 directory. The GNU @command{mkdir}
19779 succeeds, but Solaris 10 @command{mkdir} fails.
19781 Traditional @code{mkdir -p} implementations suffer from race conditions.
19782 For example, if you invoke @code{mkdir -p a/b} and @code{mkdir -p a/c}
19783 at the same time, both processes might detect that @file{a} is missing,
19784 one might create @file{a}, then the other might try to create @file{a}
19785 and fail with a @code{File exists} diagnostic. Solaris 10 @command{mkdir}
19786 is vulnerable, and other traditional Unix systems are
19787 probably vulnerable too. This possible race is harmful in parallel
19788 builds when several Make rules call @code{mkdir -p} to
19789 construct directories. You may use
19790 @code{install-sh -d} as a safe replacement, for example by setting
19791 @samp{MKDIR_P='/path/to/install-sh -d'} in the environment of
19792 @command{configure}, assuming the package distributes @file{install-sh}.
19794 @item @command{mkfifo}
19795 @itemx @command{mknod}
19796 @c -------------------
19797 @prindex @command{mkfifo}
19798 @prindex @command{mknod}
19799 The GNU Coding Standards state that @command{mknod} is safe to use on
19800 platforms where it has been tested to exist; but it is generally portable
19801 only for creating named FIFOs, since device numbers are
19802 platform-specific. Autotest uses @command{mkfifo} to implement parallel
19803 testsuites. Posix states that behavior is unspecified when opening a
19804 named FIFO for both reading and writing; on at least Cygwin, this
19805 results in failure on any attempt to read or write to that file
19808 @item @command{mktemp}
19809 @c -------------------
19810 @prindex @command{mktemp}
19811 @cindex Creating temporary files
19812 Shell scripts can use temporary files safely with @command{mktemp}, but
19813 it does not exist on all systems. A portable way to create a safe
19814 temporary file name is to create a temporary directory with mode 700 and
19815 use a file inside this directory. Both methods prevent attackers from
19816 gaining control, though @command{mktemp} is far less likely to fail
19817 gratuitously under attack.
19819 Here is sample code to create a new temporary directory @samp{$dir} safely:
19822 # Create a temporary directory $dir in $TMPDIR (default /tmp).
19823 # Use mktemp if possible; otherwise fall back on mkdir,
19824 # with $RANDOM to make collisions less likely.
19825 : "$@{TMPDIR:=/tmp@}"
19828 (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
19832 dir=$TMPDIR/foo$$-$RANDOM
19833 @c $$ restore font-lock
19834 (umask 077 && mkdir "$dir")
19841 @prindex @command{mv}
19842 @cindex Moving open files
19843 The only portable options are @option{-f} and @option{-i}.
19845 Moving individual files between file systems is portable (it was in Unix
19847 but it is not always atomic: when doing @samp{mv new existing}, there's
19848 a critical section where neither the old nor the new version of
19849 @file{existing} actually exists.
19851 On some systems moving files from @file{/tmp} can sometimes cause
19852 undesirable (but perfectly valid) warnings, even if you created these
19853 files. This is because @file{/tmp} belongs to a group that ordinary
19854 users are not members of, and files created in @file{/tmp} inherit
19855 the group of @file{/tmp}. When the file is copied, @command{mv} issues
19856 a diagnostic without failing:
19859 $ @kbd{touch /tmp/foo}
19860 $ @kbd{mv /tmp/foo .}
19861 @error{}mv: ./foo: set owner/group (was: 100/0): Operation not permitted
19869 This annoying behavior conforms to Posix, unfortunately.
19871 Moving directories across mount points is not portable, use @command{cp}
19874 DOS variants cannot rename or remove open files, and do not
19875 support commands like @samp{mv foo bar >foo}, even though this is
19876 perfectly portable among Posix hosts.
19881 @prindex @command{od}
19883 In MacOS X versions prior to 10.4.3, @command{od} does not support the
19884 standard Posix options @option{-A}, @option{-j}, @option{-N}, or
19885 @option{-t}, or the XSI option, @option{-s}. The only
19886 supported Posix option is @option{-v}, and the only supported
19887 XSI options are those in @option{-bcdox}. The BSD
19888 @command{hexdump} program can be used instead.
19890 In some versions of some operating systems derived from Solaris 11,
19891 @command{od} prints decimal byte values padded with zeros rather than
19895 $ @kbd{printf '#!' | od -A n -t d1 -N 2}
19903 $ @kbd{printf '#!' | od -A n -t d1 -N 2}
19907 We have observed this on both OpenIndiana and OmniOS;
19908 Illumos may also be affected.
19909 As a workaround, you can use octal output (option @code{-t o1}).
19914 @prindex @command{rm}
19915 The @option{-f} and @option{-r} options are portable.
19917 It is not portable to invoke @command{rm} without options or operands.
19918 On the other hand, Posix now requires @command{rm -f} to silently
19919 succeed when there are no operands (useful for constructs like
19920 @command{rm -rf $filelist} without first checking if @samp{$filelist}
19921 was empty). But this was not always portable; at least NetBSD
19922 @command{rm} built before 2008 would fail with a diagnostic.
19924 A file might not be removed even if its parent directory is writable
19925 and searchable. Many Posix hosts cannot remove a mount point, a named
19926 stream, a working directory, or a last link to a file that is being
19929 DOS variants cannot rename or remove open files, and do not
19930 support commands like @samp{rm foo >foo}, even though this is
19931 perfectly portable among Posix hosts.
19933 @item @command{rmdir}
19934 @c ------------------
19935 @prindex @command{rmdir}
19936 Just as with @command{rm}, some platforms refuse to remove a working
19940 @item @command{sed}
19941 @c ----------------
19942 @prindex @command{sed}
19943 Patterns should not include the separator (unless escaped), even as part
19944 of a character class. In conformance with Posix, the Cray
19945 @command{sed} rejects @samp{s/[^/]*$//}: use @samp{s%[^/]*$%%}.
19946 Even when escaped, patterns should not include separators that are also
19947 used as @command{sed} metacharacters. For example, GNU sed 4.0.9 rejects
19948 @samp{s,x\@{1\,\@},,}, while sed 4.1 strips the backslash before the comma
19949 before evaluating the basic regular expression.
19951 Avoid empty patterns within parentheses (i.e., @samp{\(\)}). Posix does
19952 not require support for empty patterns, and Unicos 9 @command{sed} rejects
19955 Unicos 9 @command{sed} loops endlessly on patterns like @samp{.*\n.*}.
19957 Sed scripts should not use branch labels longer than 7 characters and
19958 should not contain comments; AIX 5.3 @command{sed} rejects indented comments.
19959 HP-UX sed has a limit of 99 commands (not counting @samp{:} commands) and
19960 48 labels, which cannot be circumvented by using more than one script
19961 file. It can execute up to 19 reads with the @samp{r} command per cycle.
19962 Solaris @command{/usr/ucb/sed} rejects usages that exceed a limit of
19963 about 6000 bytes for the internal representation of commands.
19965 Avoid redundant @samp{;}, as some @command{sed} implementations, such as
19966 NetBSD 1.4.2's, incorrectly try to interpret the second
19967 @samp{;} as a command:
19970 $ @kbd{echo a | sed 's/x/x/;;s/x/x/'}
19971 sed: 1: "s/x/x/;;s/x/x/": invalid command code ;
19974 Some @command{sed} implementations have a buffer limited to 4000 bytes,
19975 and this limits the size of input lines, output lines, and internal
19976 buffers that can be processed portably. Likewise,
19977 not all @command{sed} implementations can handle embedded @code{NUL} or
19978 a missing trailing newline.
19980 Remember that ranges within a bracket expression of a regular expression
19981 are only well-defined in the @samp{C} (or @samp{POSIX}) locale.
19982 Meanwhile, support for character classes like @samp{[[:upper:]]} is not
19983 yet universal, so if you cannot guarantee the setting of @env{LC_ALL},
19984 it is better to spell out a range @samp{[ABCDEFGHIJKLMNOPQRSTUVWXYZ]}
19985 than to rely on @samp{[A-Z]}.
19987 Additionally, Posix states that regular expressions are only
19988 well-defined on characters. Unfortunately, there exist platforms such
19989 as MacOS X 10.5 where not all 8-bit byte values are valid characters,
19990 even though that platform has a single-byte @samp{C} locale. And Posix
19991 allows the existence of a multi-byte @samp{C} locale, although that does
19992 not yet appear to be a common implementation. At any rate, it means
19993 that not all bytes will be matched by the regular expression @samp{.}:
19996 $ @kbd{printf '\200\n' | LC_ALL=C sed -n /./p | wc -l}
19998 $ @kbd{printf '\200\n' | LC_ALL=en_US.ISO8859-1 sed -n /./p | wc -l}
20002 Portable @command{sed} regular expressions should use @samp{\} only to escape
20003 characters in the string @samp{$()*.123456789[\^n@{@}}. For example,
20004 alternation, @samp{\|}, is common but Posix does not require its
20005 support, so it should be avoided in portable scripts. Solaris
20006 @command{sed} does not support alternation; e.g., @samp{sed '/a\|b/d'}
20007 deletes only lines that contain the literal string @samp{a|b}.
20008 Similarly, @samp{\+} and @samp{\?} should be avoided.
20010 Anchors (@samp{^} and @samp{$}) inside groups are not portable.
20012 Nested parentheses in patterns (e.g., @samp{\(\(a*\)b*)\)}) are
20013 quite portable to current hosts, but was not supported by some ancient
20014 @command{sed} implementations like SVR3.
20016 Some @command{sed} implementations, e.g., Solaris, restrict the special
20017 role of the asterisk @samp{*} to one-character regular expressions and
20018 back-references, and the special role of interval expressions
20019 @samp{\@{@var{m}\@}}, @samp{\@{@var{m},\@}}, or @samp{\@{@var{m},@var{n}\@}}
20020 to one-character regular expressions. This may lead to unexpected behavior:
20023 $ @kbd{echo '1*23*4' | /usr/bin/sed 's/\(.\)*/x/g'}
20025 $ @kbd{echo '1*23*4' | /usr/xpg4/bin/sed 's/\(.\)*/x/g'}
20029 The @option{-e} option is mostly portable.
20030 However, its argument
20031 cannot start with @samp{a}, @samp{c}, or @samp{i},
20032 as this runs afoul of a Tru64 5.1 bug.
20033 Also, its argument cannot be empty, as this fails on AIX 5.3.
20034 Some people prefer to use @samp{-e}:
20037 sed -e '@var{command-1}' \
20038 -e '@var{command-2}'
20042 as opposed to the equivalent:
20052 The following usage is sometimes equivalent:
20055 sed '@var{command-1};@var{command-2}'
20058 but Posix says that this use of a semicolon has undefined effect if
20059 @var{command-1}'s verb is @samp{@{}, @samp{a}, @samp{b}, @samp{c},
20060 @samp{i}, @samp{r}, @samp{t}, @samp{w}, @samp{:}, or @samp{#}, so you
20061 should use semicolon only with simple scripts that do not use these
20064 Posix up to the 2008 revision requires the argument of the @option{-e}
20065 option to be a syntactically complete script. GNU @command{sed} allows
20066 to pass multiple script fragments, each as argument of a separate
20067 @option{-e} option, that are then combined, with newlines between the
20068 fragments, and a future Posix revision may allow this as well. This
20069 approach is not portable with script fragments ending in backslash; for
20070 example, the @command{sed} programs on Solaris 10, HP-UX 11, and AIX
20071 don't allow splitting in this case:
20074 $ @kbd{echo a | sed -n -e 'i\}
20077 $ @kbd{echo a | sed -n -e 'i\' -e 0}
20078 Unrecognized command: 0
20082 In practice, however, this technique of joining fragments
20083 through @option{-e} works for multiple @command{sed} functions within
20084 @samp{@{} and @samp{@}}, even if that is not specified by Posix:
20087 @c The quote around the closing brace silences interactive zsh.
20088 $ @kbd{echo a | sed -n -e '/a/@{' -e s/a/b/ -e p -e '@}'}
20092 Commands inside @{ @} brackets are further restricted. Posix 2008 says that
20093 they cannot be preceded by addresses, @samp{!}, or @samp{;}, and that
20094 each command must be followed immediately by a newline, without any
20095 intervening blanks or semicolons. The closing bracket must be alone on
20096 a line, other than white space preceding or following it. However, a
20097 future version of Posix may standardize the use of addresses within brackets.
20099 Contrary to yet another urban legend, you may portably use @samp{&} in
20100 the replacement part of the @code{s} command to mean ``what was
20101 matched''. All descendants of Unix version 7 @command{sed}
20103 don't have first hand experience with older @command{sed} implementations) have
20106 Posix requires that you must not have any white space between
20107 @samp{!} and the following command. It is OK to have blanks between
20108 the address and the @samp{!}. For instance, on Solaris:
20111 $ @kbd{echo "foo" | sed -n '/bar/ ! p'}
20112 @error{}Unrecognized command: /bar/ ! p
20113 $ @kbd{echo "foo" | sed -n '/bar/! p'}
20114 @error{}Unrecognized command: /bar/! p
20115 $ @kbd{echo "foo" | sed -n '/bar/ !p'}
20119 Posix also says that you should not combine @samp{!} and @samp{;}. If
20120 you use @samp{!}, it is best to put it on a command that is delimited by
20121 newlines rather than @samp{;}.
20123 Also note that Posix requires that the @samp{b}, @samp{t}, @samp{r}, and
20124 @samp{w} commands be followed by exactly one space before their argument.
20125 On the other hand, no white space is allowed between @samp{:} and the
20126 subsequent label name.
20128 If a sed script is specified on the command line and ends in an
20129 @samp{a}, @samp{c}, or @samp{i} command, the last line of inserted text
20130 should be followed by a newline. Otherwise some @command{sed}
20131 implementations (e.g., OpenBSD 3.9) do not append a newline to the
20134 Many @command{sed} implementations (e.g., MacOS X 10.4,
20135 OpenBSD 3.9, Solaris 10
20136 @command{/usr/ucb/sed}) strip leading white space from the text of
20137 @samp{a}, @samp{c}, and @samp{i} commands. Prepend a backslash to
20138 work around this incompatibility with Posix:
20141 $ @kbd{echo flushleft | sed 'a\}
20146 $ @kbd{echo foo | sed 'a\}
20153 Posix requires that with an empty regular expression, the last non-empty
20154 regular expression from either an address specification or substitution
20155 command is applied. However, busybox 1.6.1 complains when using a
20156 substitution command with a replacement containing a back-reference to
20157 an empty regular expression; the workaround is repeating the regular
20161 $ @kbd{echo abc | busybox sed '/a\(b\)c/ s//\1/'}
20162 sed: No previous regexp.
20163 $ @kbd{echo abc | busybox sed '/a\(b\)c/ s/a\(b\)c/\1/'}
20167 Portable scripts should be aware of the inconsistencies and options for
20168 handling word boundaries, as these are not specified by POSIX.
20172 Solaris 10 yes no no
20173 Solaris XPG4 yes no error
20174 NetBSD 5.1 no no yes
20175 FreeBSD 9.1 no no yes
20177 busybox yes yes error
20180 @item @command{sed} (@samp{t})
20181 @c ---------------------------
20182 @prindex @command{sed} (@samp{t})
20183 Some old systems have @command{sed} that ``forget'' to reset their
20184 @samp{t} flag when starting a new cycle. For instance on MIPS
20185 RISC/OS, and on IRIX 5.3, if you run the following @command{sed}
20186 script (the line numbers are not actual part of the texts):
20189 s/keep me/kept/g # a
20225 Why? When processing line 1, (c) matches, therefore sets the @samp{t}
20226 flag, and the output is produced. When processing
20227 line 2, the @samp{t} flag is still set (this is the bug). Command (a)
20228 fails to match, but @command{sed} is not supposed to clear the @samp{t}
20229 flag when a substitution fails. Command (b) sees that the flag is set,
20230 therefore it clears it, and jumps to (d), hence you get @samp{delete me}
20231 instead of @samp{deleted}. When processing line (3), @samp{t} is clear,
20232 (a) matches, so the flag is set, hence (b) clears the flags and jumps.
20233 Finally, since the flag is clear, line 4 is processed properly.
20235 There are two things one should remember about @samp{t} in @command{sed}.
20236 Firstly, always remember that @samp{t} jumps if @emph{some} substitution
20237 succeeded, not only the immediately preceding substitution. Therefore,
20238 always use a fake @samp{t clear} followed by a @samp{:clear} on the next
20239 line, to reset the @samp{t} flag where needed.
20241 Secondly, you cannot rely on @command{sed} to clear the flag at each new
20244 One portable implementation of the script above is:
20255 @item @command{sed} (@samp{w})
20256 @c ---------------------------
20257 @prindex @command{sed} (@samp{w})
20259 When a script contains multiple commands to write lines to the same
20260 output file, BusyBox @command{sed} mistakenly opens a separate output
20261 stream for each command. This can cause one of the commands to ``win''
20262 and the others to ``lose'', in the sense that their output is discarded.
20275 This might output only @samp{a} to @file{xxx}; the @samp{b} is lost.
20276 To avoid the problem, a portable script should contain at most one
20277 @samp{w} or @samp{s/.../.../w} command per output file.
20279 @item @command{sleep}
20280 @c ------------------
20281 @prindex @command{sleep}
20282 Using @command{sleep} is generally portable. However, remember that
20283 adding a @command{sleep} to work around timestamp issues, with a minimum
20284 granularity of one second, doesn't scale well for parallel builds on
20285 modern machines with sub-second process completion.
20287 @item @command{sort}
20288 @c -----------------
20289 @prindex @command{sort}
20290 Remember that sort order is influenced by the current locale. Inside
20291 @file{configure}, the C locale is in effect, but in Makefile snippets,
20292 you may need to specify @code{LC_ALL=C sort}.
20294 @item @command{tar}
20295 @c ----------------
20296 @prindex @command{tar}
20297 There are multiple file formats for @command{tar}; if you use Automake,
20298 the macro @code{AM_INIT_AUTOMAKE} has some options controlling which
20299 level of portability to use.
20302 @item @command{touch}
20303 @c ------------------
20304 @prindex @command{touch}
20305 @cindex timestamp resolution
20306 If you specify the desired timestamp (e.g., with the @option{-r}
20307 option), older @command{touch} implementations use the @code{utime} or
20308 @code{utimes} system call, which can result in the same kind of
20309 timestamp truncation problems that @samp{cp -p} has.
20313 @prindex @command{tr}
20314 @cindex carriage return, deleting
20315 @cindex newline, deleting
20316 @cindex deleting carriage return
20317 Not all versions of @command{tr} handle all backslash character escapes.
20318 For example, Solaris 10 @command{/usr/ucb/tr} falls over, even though
20319 Solaris contains more modern @command{tr} in other locations.
20320 Using octal escapes is more portable for carriage returns, since
20321 @samp{\015} is the same for both ASCII and EBCDIC, and since use of
20322 literal carriage returns in scripts causes a number of other problems.
20323 But for other characters, like newline, using octal escapes ties the
20324 operation to ASCII, so it is better to use literal characters.
20327 $ @kbd{@{ echo moon; echo light; @} | /usr/ucb/tr -d '\n' ; echo}
20330 $ @kbd{@{ echo moon; echo light; @} | /usr/bin/tr -d '\n' ; echo}
20332 $ @kbd{@{ echo moon; echo light; @} | /usr/ucb/tr -d '\012' ; echo}
20335 @kbd{'; @{ echo moon; echo light; @} | /usr/ucb/tr -d "$nl" ; echo}
20339 Not all versions of @command{tr} recognize direct ranges of characters: at
20340 least Solaris @command{/usr/bin/tr} still fails to do so. But you can
20341 use @command{/usr/xpg4/bin/tr} instead, or add brackets (which in Posix
20342 transliterate to themselves).
20345 $ @kbd{echo "Hazy Fantazy" | LC_ALL=C /usr/bin/tr a-z A-Z}
20347 $ @kbd{echo "Hazy Fantazy" | LC_ALL=C /usr/bin/tr '[a-z]' '[A-Z]'}
20349 $ @kbd{echo "Hazy Fantazy" | LC_ALL=C /usr/xpg4/bin/tr a-z A-Z}
20353 When providing two arguments, be sure the second string is at least as
20357 $ @kbd{echo abc | /usr/xpg4/bin/tr bc d}
20359 $ @kbd{echo abc | coreutils/tr bc d}
20363 On platforms with the BusyBox tools, @command{tr} does not support the
20364 @code{[@var{x}*@var{n}]} option syntax.
20367 $ @kbd{echo abc | tr 'abcd' '[A*4]'}
20369 $ @kbd{echo abc | coreutils/tr 'abcd' '[A*4]'}
20371 $ @kbd{echo xyz | tr 'a-z' '[A*]'}
20373 $ @kbd{echo xyz | coreutils/tr 'a-z' '[A*]'}
20377 Posix requires @command{tr} to operate on binary files. But at least
20378 Solaris @command{/usr/ucb/tr} and @command{/usr/bin/tr} silently discard
20379 @code{NUL} in the input prior to doing any translation. When using
20380 @command{tr} to process a binary file that may contain @code{NUL} bytes,
20381 it is necessary to use @command{/usr/xpg4/bin/tr} instead, or
20382 @command{/usr/xpg6/bin/tr} if that is available.
20385 $ @kbd{printf 'a\0b' | /usr/ucb/tr x x | od -An -tx1}
20387 $ @kbd{printf 'a\0b' | /usr/bin/tr x x | od -An -tx1}
20389 $ @kbd{printf 'a\0b' | /usr/xpg4/bin/tr x x | od -An -tx1}
20393 Solaris @command{/usr/ucb/tr} additionally fails to handle @samp{\0} as the
20394 octal escape for @code{NUL}.
20397 $ @kbd{printf 'abc' | /usr/ucb/tr 'bc' '\0d' | od -An -tx1}
20399 $ @kbd{printf 'abc' | /usr/bin/tr 'bc' '\0d' | od -An -tx1}
20401 $ @kbd{printf 'abc' | /usr/xpg4/bin/tr 'bc' '\0d' | od -An -tx1}
20408 @node Portable Make
20409 @chapter Portable Make Programming
20410 @prindex @command{make}
20411 @cindex Limitations of @command{make}
20413 Writing portable makefiles is an art. Since a makefile's commands are
20414 executed by the shell, you must consider the shell portability issues
20415 already mentioned. However, other issues are specific to @command{make}
20419 * $< in Ordinary Make Rules:: $< in ordinary rules
20420 * Failure in Make Rules:: Failing portably in rules
20421 * Special Chars in Names:: Special Characters in Macro Names
20422 * Backslash-Newline-Empty:: Empty lines after backslash-newline
20423 * Backslash-Newline Comments:: Spanning comments across line boundaries
20424 * Long Lines in Makefiles:: Line length limitations
20425 * Macros and Submakes:: @code{make macro=value} and submakes
20426 * The Make Macro MAKEFLAGS:: @code{$(MAKEFLAGS)} portability issues
20427 * The Make Macro SHELL:: @code{$(SHELL)} portability issues
20428 * Parallel Make:: Parallel @command{make} quirks
20429 * Comments in Make Rules:: Other problems with Make comments
20430 * Newlines in Make Rules:: Using literal newlines in rules
20431 * Comments in Make Macros:: Other problems with Make comments in macros
20432 * Trailing whitespace in Make Macros:: Macro substitution problems
20433 * Command-line Macros and whitespace:: Whitespace trimming of values
20434 * obj/ and Make:: Don't name a subdirectory @file{obj}
20435 * make -k Status:: Exit status of @samp{make -k}
20436 * VPATH and Make:: @code{VPATH} woes
20437 * Single Suffix Rules:: Single suffix rules and separated dependencies
20438 * Timestamps and Make:: Sub-second timestamp resolution
20441 @node $< in Ordinary Make Rules
20442 @section @code{$<} in Ordinary Make Rules
20444 Posix says that the @samp{$<} construct in makefiles can be
20445 used only in inference rules and in the @samp{.DEFAULT} rule; its
20446 meaning in ordinary rules is unspecified. Solaris @command{make}
20447 for instance replaces it with the empty string. OpenBSD (3.0 and
20448 later) @command{make} diagnoses these uses and errors out.
20450 @node Failure in Make Rules
20451 @section Failure in Make Rules
20453 Posix 2008 requires that @command{make} must invoke each command with
20454 the equivalent of a @samp{sh -e -c} subshell, which causes the
20455 subshell to exit immediately if a subsidiary simple-command fails,
20456 although not all @command{make} implementations have historically
20457 followed this rule. For
20458 example, the command @samp{touch T; rm -f U} may attempt to
20459 remove @file{U} even if the @command{touch} fails, although this is not
20460 permitted with Posix make. One way to work around failures in simple
20461 commands is to reword them so that they always succeed, e.g., @samp{touch
20463 However, even this approach can run into common bugs in BSD
20464 implementations of the @option{-e} option of @command{sh} and
20465 @command{set} (@pxref{set, , Limitations of Shell Builtins}), so if you
20467 about porting to buggy BSD shells it may be simpler to migrate
20468 complicated @command{make} actions into separate scripts.
20470 @node Special Chars in Names
20471 @section Special Characters in Make Macro Names
20473 Posix limits macro names to nonempty strings containing only
20474 ASCII letters and digits, @samp{.}, and @samp{_}. Many
20475 @command{make} implementations allow a wider variety of characters, but
20476 portable makefiles should avoid them. It is portable to start a name
20477 with a special character, e.g., @samp{$(.FOO)}.
20479 Some ancient @command{make} implementations don't support leading
20480 underscores in macro names. An example is NEWS-OS 4.2R.
20483 $ @kbd{cat Makefile}
20486 all:; @@echo this is test
20488 Make: Must be a separator on rules line 2. Stop.
20489 $ @kbd{cat Makefile2}
20492 all:; @@echo this is test
20493 $ @kbd{make -f Makefile2}
20498 However, this problem is no longer of practical concern.
20500 @node Backslash-Newline-Empty
20501 @section Backslash-Newline Before Empty Lines
20503 @c This has been seen on ia64 hpux 11.20, and on one hppa hpux 10.20,
20504 @c but another hppa hpux 10.20 didn't have it. Bob Proulx
20505 @c <bob@proulx.com> thinks it was in hpux 8.0 too.
20506 On some versions of HP-UX, @command{make} reads multiple newlines
20507 following a backslash, continuing to the next non-empty line. For
20521 shows @code{FOO} equal to @code{one BAR = two}. Other implementations
20522 sensibly let a backslash continue only to the immediately following
20525 @node Backslash-Newline Comments
20526 @section Backslash-Newline in Make Comments
20528 According to Posix, Make comments start with @code{#}
20529 and continue until an unescaped newline is reached.
20532 $ @kbd{cat Makefile}
20539 $ @kbd{make} # GNU make
20544 However this is not always the case. Some implementations
20545 discard everything from @code{#} through the end of the line, ignoring any
20546 trailing backslash.
20549 $ @kbd{pmake} # BSD make
20550 "Makefile", line 3: Need an operator
20551 Fatal errors encountered -- cannot continue
20555 Therefore, if you want to comment out a multi-line definition, prefix each
20556 line with @code{#}, not only the first.
20564 @node Long Lines in Makefiles
20565 @section Long Lines in Makefiles
20567 Tru64 5.1's @command{make} has been reported to crash when given a
20568 makefile with lines longer than around 20 kB. Earlier versions are
20569 reported to exit with @code{Line too long} diagnostics.
20571 @node Macros and Submakes
20572 @section @code{make macro=value} and Submakes
20574 A command-line variable definition such as @code{foo=bar} overrides any
20575 definition of @code{foo} in a makefile. Some @command{make}
20576 implementations (such as GNU @command{make}) propagate this
20577 override to subsidiary invocations of @command{make}. Some other
20578 implementations do not pass the substitution along to submakes.
20581 $ @kbd{cat Makefile}
20588 $ @kbd{make foo=bar} # GNU make 3.79.1
20591 make[1]: Entering directory `/home/adl'
20593 make[1]: Leaving directory `/home/adl'
20594 $ @kbd{pmake foo=bar} # BSD make
20600 You have a few possibilities if you do want the @code{foo=bar} override
20601 to propagate to submakes. One is to use the @option{-e}
20602 option, which causes all environment variables to have precedence over
20603 the makefile macro definitions, and declare foo as an environment
20607 $ @kbd{env foo=bar make -e}
20610 The @option{-e} option is propagated to submakes automatically,
20611 and since the environment is inherited between @command{make}
20612 invocations, the @code{foo} macro is overridden in
20613 submakes as expected.
20615 This syntax (@code{foo=bar make -e}) is portable only when used
20616 outside of a makefile, for instance from a script or from the
20617 command line. When run inside a @command{make} rule, GNU
20618 @command{make} 3.80 and prior versions forget to propagate the
20619 @option{-e} option to submakes.
20621 Moreover, using @option{-e} could have unexpected side effects if your
20622 environment contains some other macros usually defined by the
20623 makefile. (See also the note about @code{make -e} and @code{SHELL}
20626 If you can foresee all macros that a user might want to override, then
20627 you can propagate them to submakes manually, from your makefile:
20633 $(MAKE) foo=$(foo) two
20638 Another way to propagate a variable to submakes in a portable way is to
20639 expand an extra variable in every invocation of @samp{$(MAKE)} within
20646 $(MAKE) $(SUBMAKEFLAGS) two
20651 Users must be aware that this technique is in use to take advantage of
20652 it, e.g.@: with @code{make foo=bar SUBMAKEFLAGS='foo=bar'}, but it
20653 allows any macro to be overridden. Makefiles generated by
20654 @command{automake} use this technique, expanding @code{$(AM_MAKEFLAGS)}
20655 on the command lines of submakes (@pxref{Subdirectories, , Automake,
20656 automake, GNU Automake}).
20658 @node The Make Macro MAKEFLAGS
20659 @section The Make Macro MAKEFLAGS
20660 @cindex @code{MAKEFLAGS} and @command{make}
20661 @cindex @command{make} and @code{MAKEFLAGS}
20663 Posix requires @command{make} to use @code{MAKEFLAGS} to affect the
20664 current and recursive invocations of make, but allows implementations
20665 several formats for the variable. It is tricky to parse
20666 @code{$MAKEFLAGS} to determine whether @option{-s} for silent execution
20667 or @option{-k} for continued execution are in effect. For example, you
20668 cannot assume that the first space-separated word in @code{$MAKEFLAGS}
20669 contains single-letter options, since in the Cygwin version of
20670 GNU @command{make} it is either @option{--unix} or
20671 @option{--win32} with the second word containing single-letter options.
20674 $ @kbd{cat Makefile}
20676 @@echo MAKEFLAGS = $(MAKEFLAGS)
20680 MAKEFLAGS = --unix -k
20683 @node The Make Macro SHELL
20684 @section The Make Macro @code{SHELL}
20685 @cindex @code{SHELL} and @command{make}
20686 @cindex @command{make} and @code{SHELL}
20688 Posix-compliant @command{make} internally uses the @code{$(SHELL)}
20689 macro to spawn shell processes and execute Make rules. This
20690 is a builtin macro supplied by @command{make}, but it can be modified
20691 by a makefile or by a command-line argument.
20693 Not all @command{make} implementations define this @code{SHELL} macro.
20695 @command{make} is an example; this implementation always uses
20696 @code{/bin/sh}. So it's a good idea to always define @code{SHELL} in
20697 your makefiles. If you use Autoconf, do
20704 If you use Automake, this is done for you.
20706 Do not force @code{SHELL = /bin/sh} because that is not correct
20707 everywhere. Remember, @file{/bin/sh} is not Posix compliant on many
20708 systems, such as FreeBSD 4, NetBSD 3, AIX 3, Solaris 10, or Tru64.
20709 Additionally, DJGPP lacks @code{/bin/sh}, and when its
20710 GNU @command{make} port sees such a setting it enters a
20711 special emulation mode where features like pipes and redirections are
20712 emulated on top of DOS's @command{command.com}. Unfortunately this
20713 emulation is incomplete; for instance it does not handle command
20714 substitutions. Using @code{@@SHELL@@} means that your makefile will
20715 benefit from the same improved shell, such as @command{bash} or
20716 @command{ksh}, that was discovered during @command{configure}, so that
20717 you aren't fighting two different sets of shell bugs between the two
20720 Posix-compliant @command{make} should never acquire the value of
20721 $(SHELL) from the environment, even when @code{make -e} is used
20722 (otherwise, think about what would happen to your rules if
20723 @code{SHELL=/bin/tcsh}).
20725 However not all @command{make} implementations have this exception.
20726 For instance it's not surprising that Tru64 @command{make} doesn't
20727 protect @code{SHELL}, since it doesn't use it.
20730 $ @kbd{cat Makefile}
20736 $ @kbd{env SHELL=/bin/tcsh FOO=bar make -e} # Tru64 Make
20739 $ @kbd{env SHELL=/bin/tcsh FOO=bar gmake -e} # GNU make
20744 Conversely, @command{make} is not supposed to export any changes to the
20745 macro @code{SHELL} to child processes. Again, many implementations
20749 $ @kbd{cat Makefile}
20753 $ @kbd{env SHELL=sh make -e SHELL=/bin/ksh} # BSD Make, GNU make 3.80
20756 $ @kbd{env SHELL=sh gmake -e SHELL=/bin/ksh} # GNU make 3.81
20761 @node Parallel Make
20762 @section Parallel Make
20763 @cindex Parallel @command{make}
20765 Support for parallel execution in @command{make} implementation varies.
20766 Generally, using GNU make is your best bet.
20768 When NetBSD or FreeBSD @command{make} are run in parallel mode, they will
20769 reuse the same shell for multiple commands within one recipe. This can
20770 have various unexpected consequences. For example, changes of directories
20771 or variables persist between recipes, so that:
20775 @@var=value; cd /; pwd; echo $$var; echo $$$$
20776 @@pwd; echo $$var; echo $$$$
20780 may output the following with @code{make -j1}, at least on NetBSD up to
20781 5.1 and FreeBSD up to 8.2:
20793 while without @option{-j1}, or with @option{-B}, the output looks less
20806 Another consequence is that, if one command in a recipe uses @code{exit 0}
20807 to indicate a successful exit, the shell will be gone and the remaining
20808 commands of this recipe will not be executed.
20810 The BSD @command{make} implementations, when run in parallel mode,
20811 will also pass the @command{Makefile} recipes to the shell through
20812 its standard input, thus making it unusable from the recipes:
20815 $ @kbd{cat Makefile}
20817 @@read line; echo LINE: $$line
20818 @c $$ @c restore font-lock
20819 $ @kbd{echo foo | make read}
20821 $ @kbd{echo foo | make -j1 read} # NetBSD 5.1 and FreeBSD 8.2
20826 Moreover, when FreeBSD @command{make} (up at least to 8.2) is run in
20827 parallel mode, it implements the @code{@@} and @code{-} ``recipe
20828 modifiers'' by dynamically modifying the active shell flags. This
20829 behavior has the effects of potentially clobbering the exit status
20830 of recipes silenced with the @code{@@} modifier if they also unset
20831 the @option{errexit} shell flag, and of mangling the output in
20835 $ @kbd{cat Makefile}
20837 @@echo $$-; set +e; false
20839 -echo $$-; false; echo set -
20840 $ @kbd{make a; echo status: $?}
20844 $ @kbd{make -j1 a; echo status: $?}
20848 echo $-; echo set -
20856 You can avoid all these issues by using the @option{-B} option to enable
20857 compatibility semantics. However, that will effectively also disable
20858 all parallelism as that will cause prerequisites to be updated in the
20859 order they are listed in a rule.
20861 Some make implementations (among them, FreeBSD @command{make}, NetBSD
20862 @command{make}, and Solaris @command{dmake}), when invoked with a
20863 @option{-j@var{N}} option, connect the standard output and standard
20864 error of all their child processes to pipes or temporary regular
20865 files. This can lead to subtly different semantics in the behavior
20866 of the spawned processes. For example, even if the @command{make}
20867 standard output is connected to a tty, the recipe command will not be:
20870 $ @kbd{cat Makefile}
20872 @@test -t 1 && echo "Is a tty" || echo "Is not a tty"
20873 $ @kbd{make -j 2} # FreeBSD 8.2 make
20875 $ @kbd{make -j 2} # NetBSD 5.1 make
20878 $ @kbd{dmake -j 2} # Solaris 10 dmake
20879 @var{hostname} --> 1 job
20880 @var{hostname} --> Job output
20888 $ @kbd{make -j 2} # GNU make, Heirloom make
20893 The above examples also show additional status output produced in parallel
20894 mode for targets being updated by Solaris @command{dmake} and NetBSD
20895 @command{make} (but @emph{not} by FreeBSD @command{make}).
20897 Furthermore, parallel runs of those @command{make} implementations will
20898 route standard error from commands that they spawn into their own
20899 standard output, and may remove leading whitespace from output lines.
20902 @node Comments in Make Rules
20903 @section Comments in Make Rules
20904 @cindex Comments in @file{Makefile} rules
20905 @cindex @file{Makefile} rules and comments
20907 Never put comments in a rule.
20909 Some @command{make} treat anything starting with a tab as a command for
20910 the current rule, even if the tab is immediately followed by a @code{#}.
20911 The @command{make} from Tru64 Unix V5.1 is one of them. The following
20912 makefile runs @code{# foo} through the shell.
20919 As a workaround, you can use the @command{:} no-op command with a string
20920 argument that gets ignored:
20927 Conversely, if you want to use the @samp{#} character in some command,
20928 you can only do so by expanding it inside a rule (@pxref{Comments in
20929 Make Macros}). So for example, if @samp{COMMENT_CHAR} is substituted by
20930 @command{config.status} as @samp{#}, then the following substitutes
20931 @samp{@@COMMENT_CHAR@@} in a generated header:
20935 sed -e 's|@@''COMMENT_CHAR''@@|@@COMMENT_CHAR@@|g' \
20936 $(srcdir)/foo.h.in > $@@
20939 The funny shell quoting avoids a substitution at @command{config.status}
20940 run time of the left-hand side of the @command{sed} @samp{s} command.
20942 @node Newlines in Make Rules
20943 @section Newlines in Make Rules
20944 @cindex Newlines in @file{Makefile} rules
20945 @cindex @file{Makefile} rules and newlines
20947 In shell scripts, newlines can be used inside string literals. But in
20948 the shell statements of @file{Makefile} rules, this is not possible:
20949 A newline not preceded by a backslash is a separator between shell
20950 statements. Whereas a newline that is preceded by a backslash becomes
20951 part of the shell statement according to POSIX, but gets replaced,
20952 together with the backslash that precedes it, by a space in GNU
20953 @command{make} 3.80 and older. So, how can a newline be used in a string
20956 The trick is to set up a shell variable that contains a newline:
20959 nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit"
20962 For example, in order to create a multi-line @samp{sed} expression that
20963 inserts a blank line after every line of a file, this code can be used:
20966 nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit"; \
20967 sed -e "s/\$$/\\$$@{nl@}/" < input > output
20970 @node Comments in Make Macros
20971 @section Comments in Make Macros
20972 @cindex Comments in @file{Makefile} macros
20973 @cindex @file{Makefile} macros and comments
20975 Avoid putting comments in macro values as far as possible. Posix
20976 specifies that the text starting from the @samp{#} sign until the end of
20977 the line is to be ignored, which has the unfortunate effect of
20978 disallowing them even within quotes. Thus, the following might lead to
20979 a syntax error at compile time:
20982 CPPFLAGS = "-DCOMMENT_CHAR='#'"
20986 as @samp{CPPFLAGS} may be expanded to @samp{"-DCOMMENT_CHAR='}.
20988 Most @command{make} implementations disregard this and treat single and
20989 double quotes specially here. Also, GNU @command{make} lets you put
20990 @samp{#} into a macro value by escaping it with a backslash, i.e.,
20991 @samp{\#}. However, neither of these usages are portable.
20992 @xref{Comments in Make Rules}, for a portable alternative.
20994 Even without quoting involved, comments can have surprising effects,
20995 because the whitespace before them is part of the variable value:
20998 foo = bar # trailing comment
20999 print: ; @@echo "$(foo)."
21003 prints @samp{bar .}, which is usually not intended, and can expose
21004 @command{make} bugs as described below.
21006 @node Trailing whitespace in Make Macros
21007 @section Trailing whitespace in Make Macros
21008 @cindex whitespace in @file{Makefile} macros
21009 @cindex @file{Makefile} macros and whitespace
21011 GNU @command{make} 3.80 mistreats trailing whitespace in macro
21012 substitutions and appends another spurious suffix:
21017 print: ; @@echo $(foo:=.test)
21021 prints @samp{bar.test .test}.
21023 BSD and Solaris @command{make} implementations do not honor trailing
21024 whitespace in macro definitions as Posix requires:
21027 foo = bar # Note the space after "bar".
21028 print: ; @@echo $(foo)t
21032 prints @samp{bart} instead of @samp{bar t}. To work around this, you
21033 can use a helper macro as in the previous example.
21036 @node Command-line Macros and whitespace
21037 @section Command-line Macros and whitespace
21038 @cindex whitespace in command-line macros
21039 @cindex command-line, macros set on
21040 @cindex environment, macros set from
21042 Some @command{make} implementations may strip trailing whitespace off
21043 of macros set on the command line in addition to leading whitespace.
21044 Further, some may strip leading whitespace off of macros set from
21045 environment variables:
21048 $ @kbd{echo 'print: ; @@echo "x$(foo)x$(bar)x"' |
21049 foo=' f f ' make -f - bar=' b b '}
21050 x f f xb b x # AIX, BSD, GNU make
21051 xf f xb b x # HP-UX, IRIX, Tru64/OSF make
21052 x f f xb bx # Solaris make
21056 @node obj/ and Make
21057 @section The @file{obj/} Subdirectory and Make
21058 @cindex @file{obj/}, subdirectory
21059 @cindex BSD @command{make} and @file{obj/}
21061 Never name one of your subdirectories @file{obj/} if you don't like
21064 If an @file{obj/} directory exists, BSD @command{make} enters it
21065 before reading the makefile. Hence the makefile in the
21066 current directory is not read.
21069 $ @kbd{cat Makefile}
21072 $ @kbd{cat obj/Makefile}
21075 $ @kbd{make} # GNU make
21078 $ @kbd{pmake} # BSD make
21083 @node make -k Status
21084 @section Exit Status of @code{make -k}
21085 @cindex @code{make -k}
21087 Do not rely on the exit status of @code{make -k}. Some implementations
21088 reflect whether they encountered an error in their exit status; other
21089 implementations always succeed.
21092 $ @kbd{cat Makefile}
21095 $ @kbd{make -k; echo exit status: $?} # GNU make
21097 make: *** [all] Error 1
21099 $ @kbd{pmake -k; echo exit status: $?} # BSD make
21101 *** Error code 1 (continuing)
21105 @node VPATH and Make
21106 @section @code{VPATH} and Make
21107 @cindex @code{VPATH}
21109 Posix does not specify the semantics of @code{VPATH}. Typically,
21110 @command{make} supports @code{VPATH}, but its implementation is not
21113 Autoconf and Automake support makefiles whose usages of @code{VPATH} are
21114 portable to recent-enough popular implementations of @command{make}, but
21115 to keep the resulting makefiles portable, a package's makefile
21116 prototypes must take the following issues into account. These issues
21117 are complicated and are often poorly understood, and installers who use
21118 @code{VPATH} should expect to find many bugs in this area. If you use
21119 @code{VPATH}, the simplest way to avoid these portability bugs is to
21120 stick with GNU @command{make}, since it is the most
21121 commonly-used @command{make} among Autoconf users.
21123 Here are some known issues with some @code{VPATH}
21127 * Variables listed in VPATH:: @code{VPATH} must be literal on ancient hosts
21128 * VPATH and Double-colon:: Problems with @samp{::} on ancient hosts
21129 * $< in Explicit Rules:: @code{$<} does not work in ordinary rules
21130 * Automatic Rule Rewriting:: @code{VPATH} goes wild on Solaris
21131 * Tru64 Directory Magic:: @command{mkdir} goes wild on Tru64
21132 * Make Target Lookup:: More details about @code{VPATH} lookup
21135 @node Variables listed in VPATH
21136 @subsection Variables listed in @code{VPATH}
21137 @cindex @code{VPATH} and variables
21138 @cindex variables and @code{VPATH}
21140 Do not set @code{VPATH} to the value of another variable, for example
21141 @samp{VPATH = $(srcdir)}, because some ancient versions of
21142 @command{make} do not do variable substitutions on the value of
21143 @code{VPATH}. For example, use this
21146 srcdir = @@srcdir@@
21151 rather than @samp{VPATH = $(srcdir)}. Note that with GNU
21152 Automake, there is no need to set this yourself.
21154 @node VPATH and Double-colon
21155 @subsection @code{VPATH} and Double-colon Rules
21156 @cindex @code{VPATH} and double-colon rules
21157 @cindex double-colon rules and @code{VPATH}
21159 With ancient versions of Sun @command{make},
21160 any assignment to @code{VPATH} causes @command{make} to execute only
21161 the first set of double-colon rules.
21162 However, this problem is no longer of practical concern.
21164 @node $< in Explicit Rules
21165 @subsection @code{$<} Not Supported in Explicit Rules
21166 @cindex explicit rules, @code{$<}, and @code{VPATH}
21167 @cindex @code{$<}, explicit rules, and @code{VPATH}
21168 @cindex @code{VPATH}, explicit rules, and @code{$<}
21170 Using @code{$<} in explicit rules is not portable.
21171 The prerequisite file must be named explicitly in the rule. If you want
21172 to find the prerequisite via a @code{VPATH} search, you have to code the
21173 whole thing manually. @xref{Build Directories}.
21175 @node Automatic Rule Rewriting
21176 @subsection Automatic Rule Rewriting
21177 @cindex @code{VPATH} and automatic rule rewriting
21178 @cindex automatic rule rewriting and @code{VPATH}
21180 Some @command{make} implementations, such as Solaris and Tru64,
21181 search for prerequisites in @code{VPATH} and
21182 then rewrite each occurrence as a plain word in the rule.
21186 # This isn't portable to GNU make.
21193 executes @code{cp ../pkg/src/if.c f.c} if @file{if.c} is
21194 found in @file{../pkg/src}.
21196 However, this rule leads to real problems in practice. For example, if
21197 the source directory contains an ordinary file named @file{test} that is
21198 used in a dependency, Solaris @command{make} rewrites commands like
21199 @samp{if test -r foo; @dots{}} to @samp{if ../pkg/src/test -r foo;
21200 @dots{}}, which is typically undesirable. In fact, @command{make} is
21201 completely unaware of shell syntax used in the rules, so the VPATH
21202 rewrite can potentially apply to @emph{any} whitespace-separated word
21203 in a rule, including shell variables, functions, and keywords.
21206 $ @kbd{mkdir build}
21208 $ @kbd{cat > Makefile <<'END'}
21210 all: arg func for echo
21211 func () @{ for arg in "$$@@"; do echo $$arg; done; @}; \
21214 $ @kbd{touch ../arg ../func ../for ../echo}
21216 ../func () @{ ../for ../arg in "$@@"; do ../echo $arg; done; @}; \
21217 ../func "hello world"
21218 sh: syntax error at line 1: `do' unexpected
21223 To avoid this problem, portable makefiles should never mention a source
21224 file or dependency whose name is that of a shell keyword like @file{for}
21225 or @file{until}, a shell command like @command{cat} or @command{gcc} or
21226 @command{test}, or a shell function or variable used in the corresponding
21227 @command{Makefile} recipe.
21229 Because of these problems GNU @command{make} and many other @command{make}
21230 implementations do not rewrite commands, so portable makefiles should
21231 search @code{VPATH} manually. It is tempting to write this:
21234 # This isn't portable to Solaris make.
21237 cp `test -f if.c || echo $(VPATH)/`if.c f.c
21241 However, the ``prerequisite rewriting'' still applies here. So if
21242 @file{if.c} is in @file{../pkg/src}, Solaris and Tru64 @command{make}
21246 cp `test -f ../pkg/src/if.c || echo ../pkg/src/`if.c f.c
21257 and thus fails. Oops.
21259 A simple workaround, and good practice anyway, is to use @samp{$?} and
21260 @samp{$@@} when possible:
21269 but this does not generalize well to commands with multiple
21270 prerequisites. A more general workaround is to rewrite the rule so that
21271 the prerequisite @file{if.c} never appears as a plain word. For
21272 example, these three rules would be safe, assuming @file{if.c} is in
21273 @file{../pkg/src} and the other files are in the working directory:
21278 cat `test -f ./if.c || echo $(VPATH)/`if.c f1.c >$@@
21280 cat `test -f 'if.c' || echo $(VPATH)/`if.c g1.c >$@@
21282 cat `test -f "if.c" || echo $(VPATH)/`if.c h1.c >$@@
21285 Things get worse when your prerequisites are in a macro.
21289 HEADERS = f.h g.h h.h
21290 install-HEADERS: $(HEADERS)
21291 for i in $(HEADERS); do \
21292 $(INSTALL) -m 644 \
21293 `test -f $$i || echo $(VPATH)/`$$i \
21294 $(DESTDIR)$(includedir)/$$i; \
21295 @c $$ restore font-lock
21299 The above @code{install-HEADERS} rule is not Solaris-proof because @code{for
21300 i in $(HEADERS);} is expanded to @code{for i in f.h g.h h.h;}
21301 where @code{f.h} and @code{g.h} are plain words and are hence
21302 subject to @code{VPATH} adjustments.
21304 If the three files are in @file{../pkg/src}, the rule is run as:
21307 for i in ../pkg/src/f.h ../pkg/src/g.h h.h; do \
21309 `test -f $i || echo ../pkg/src/`$i \
21310 /usr/local/include/$i; \
21314 where the two first @command{install} calls fail. For instance,
21315 consider the @code{f.h} installation:
21319 `test -f ../pkg/src/f.h || \
21322 /usr/local/include/../pkg/src/f.h;
21331 /usr/local/include/../pkg/src/f.h;
21334 Note that the manual @code{VPATH} search did not cause any problems here;
21335 however this command installs @file{f.h} in an incorrect directory.
21337 Trying to quote @code{$(HEADERS)} in some way, as we did for
21338 @code{foo.c} a few makefiles ago, does not help:
21341 install-HEADERS: $(HEADERS)
21342 headers='$(HEADERS)'; \
21343 for i in $$headers; do \
21344 $(INSTALL) -m 644 \
21345 `test -f $$i || echo $(VPATH)/`$$i \
21346 $(DESTDIR)$(includedir)/$$i; \
21350 Now, @code{headers='$(HEADERS)'} macro-expands to:
21353 headers='f.h g.h h.h'
21357 but @code{g.h} is still a plain word. (As an aside, the idiom
21358 @code{headers='$(HEADERS)'; for i in $$headers;} is a good
21359 idea if @code{$(HEADERS)} can be empty, because some shells diagnose a
21360 syntax error on @code{for i in;}.)
21362 One workaround is to strip this unwanted @file{../pkg/src/} prefix manually:
21366 HEADERS = f.h g.h h.h
21367 install-HEADERS: $(HEADERS)
21368 headers='$(HEADERS)'; \
21369 for i in $$headers; do \
21370 i=`expr "$$i" : '$(VPATH)/\(.*\)'`;
21371 $(INSTALL) -m 644 \
21372 `test -f $$i || echo $(VPATH)/`$$i \
21373 $(DESTDIR)$(includedir)/$$i; \
21374 @c $$ restore font-lock
21378 Automake does something similar. However the above hack works only if
21379 the files listed in @code{HEADERS} are in the current directory or a
21380 subdirectory; they should not be in an enclosing directory. If we had
21381 @code{HEADERS = ../f.h}, the above fragment would fail in a VPATH
21382 build with Tru64 @command{make}. The reason is that not only does
21383 Tru64 @command{make} rewrite dependencies, but it also simplifies
21384 them. Hence @code{../f.h} becomes @code{../pkg/f.h} instead of
21385 @code{../pkg/src/../f.h}. This obviously defeats any attempt to strip
21386 a leading @file{../pkg/src/} component.
21388 The following example makes the behavior of Tru64 @command{make}
21392 $ @kbd{cat Makefile}
21404 Dependency @file{../foo} was found in @file{sub/../foo}, but Tru64
21405 @command{make} simplified it as @file{foo}. (Note that the @file{sub/}
21406 directory does not even exist, this just means that the simplification
21407 occurred before the file was checked for.)
21410 @node Tru64 Directory Magic
21411 @subsection Tru64 @command{make} Creates Prerequisite Directories Magically
21412 @cindex @code{VPATH} and prerequisite directories
21413 @cindex prerequisite directories and @code{VPATH}
21415 When a prerequisite is a subdirectory of @code{VPATH}, Tru64
21416 @command{make} creates it in the current directory.
21419 $ @kbd{mkdir -p foo/bar build}
21421 $ @kbd{cat >Makefile <<END
21430 This can yield unexpected results if a rule uses a manual @code{VPATH}
21431 search as presented before.
21436 command `test -d foo/bar || echo ../`foo/bar
21439 The above @command{command} is run on the empty @file{foo/bar}
21440 directory that was created in the current directory.
21442 @node Make Target Lookup
21443 @subsection Make Target Lookup
21444 @cindex @code{VPATH}, resolving target pathnames
21446 GNU @command{make} uses a complex algorithm to decide when it
21447 should use files found via a @code{VPATH} search. @xref{Search
21448 Algorithm, , How Directory Searches are Performed, make, The GNU Make
21451 If a target needs to be rebuilt, GNU @command{make} discards the
21452 file name found during the @code{VPATH} search for this target, and
21453 builds the file locally using the file name given in the makefile.
21454 If a target does not need to be rebuilt, GNU @command{make} uses the
21455 file name found during the @code{VPATH} search.
21457 Other @command{make} implementations, like NetBSD @command{make}, are
21458 easier to describe: the file name found during the @code{VPATH} search
21459 is used whether the target needs to be rebuilt or not. Therefore
21460 new files are created locally, but existing files are updated at their
21461 @code{VPATH} location.
21463 OpenBSD and FreeBSD @command{make}, however,
21465 @code{VPATH} search for a dependency that has an explicit rule.
21466 This is extremely annoying.
21468 When attempting a @code{VPATH} build for an autoconfiscated package
21469 (e.g., @code{mkdir build && cd build && ../configure}), this means
21471 @command{make} builds everything locally in the @file{build}
21472 directory, while BSD @command{make} builds new files locally and
21473 updates existing files in the source directory.
21476 $ @kbd{cat Makefile}
21479 foo.x bar.x: newer.x
21480 @@echo Building $@@
21481 $ @kbd{touch ../bar.x}
21482 $ @kbd{touch ../newer.x}
21483 $ @kbd{make} # GNU make
21486 $ @kbd{pmake} # NetBSD make
21489 $ @kbd{fmake} # FreeBSD make, OpenBSD make
21492 $ @kbd{tmake} # Tru64 make
21495 $ @kbd{touch ../bar.x}
21496 $ @kbd{make} # GNU make
21498 $ @kbd{pmake} # NetBSD make
21500 $ @kbd{fmake} # FreeBSD make, OpenBSD make
21503 $ @kbd{tmake} # Tru64 make
21508 Note how NetBSD @command{make} updates @file{../bar.x} in its
21509 VPATH location, and how FreeBSD, OpenBSD, and Tru64
21510 @command{make} always
21511 update @file{bar.x}, even when @file{../bar.x} is up to date.
21513 Another point worth mentioning is that once GNU @command{make} has
21514 decided to ignore a @code{VPATH} file name (e.g., it ignored
21515 @file{../bar.x} in the above example) it continues to ignore it when
21516 the target occurs as a prerequisite of another rule.
21518 The following example shows that GNU @command{make} does not look up
21519 @file{bar.x} in @code{VPATH} before performing the @code{.x.y} rule,
21520 because it ignored the @code{VPATH} result of @file{bar.x} while running
21521 the @code{bar.x: newer.x} rule.
21524 $ @kbd{cat Makefile}
21528 @@echo Building $@@
21532 $ @kbd{touch ../bar.x}
21533 $ @kbd{touch ../newer.x}
21534 $ @kbd{make} # GNU make
21537 cp: cannot stat 'bar.x': No such file or directory
21538 make: *** [bar.y] Error 1
21539 $ @kbd{pmake} # NetBSD make
21543 $ @kbd{fmake} # FreeBSD make, OpenBSD make
21544 echo Building bar.x
21546 cp: cannot stat 'bar.x': No such file or directory
21548 $ @kbd{tmake} # Tru64 make
21550 cp: bar.x: No such file or directory
21554 Note that if you drop away the command from the @code{bar.x: newer.x}
21555 rule, GNU @command{make} magically starts to work: it
21556 knows that @code{bar.x} hasn't been updated, therefore it doesn't
21557 discard the result from @code{VPATH} (@file{../bar.x}) in succeeding
21558 uses. Tru64 also works, but FreeBSD and OpenBSD
21562 $ @kbd{cat Makefile}
21569 $ @kbd{touch ../bar.x}
21570 $ @kbd{touch ../newer.x}
21571 $ @kbd{make} # GNU make
21574 $ @kbd{pmake} # NetBSD make
21577 $ @kbd{fmake} # FreeBSD make, OpenBSD make
21579 cp: cannot stat 'bar.x': No such file or directory
21581 $ @kbd{tmake} # Tru64 make
21585 It seems the sole solution that would please every @command{make}
21586 implementation is to never rely on @code{VPATH} searches for targets.
21587 In other words, @code{VPATH} should be reserved to sources that are not built.
21590 @node Single Suffix Rules
21591 @section Single Suffix Rules and Separated Dependencies
21592 @cindex Single Suffix Inference Rule
21593 @cindex Rule, Single Suffix Inference
21594 A @dfn{Single Suffix Rule} is basically a usual suffix (inference) rule
21595 (@samp{.from.to:}), but which @emph{destination} suffix is empty
21598 @cindex Separated Dependencies
21599 @dfn{Separated dependencies} simply refers to listing the prerequisite
21600 of a target, without defining a rule. Usually one can list on the one
21601 hand side, the rules, and on the other hand side, the dependencies.
21603 Solaris @command{make} does not support separated dependencies for
21604 targets defined by single suffix rules:
21607 $ @kbd{cat Makefile}
21612 $ @kbd{touch foo.in}
21619 while GNU Make does:
21625 Makefile foo foo.in
21628 Note it works without the @samp{foo: foo.in} dependency.
21631 $ @kbd{cat Makefile}
21640 and it works with double suffix inference rules:
21643 $ @kbd{cat Makefile}
21645 .SUFFIXES: .in .out
21652 As a result, in such a case, you have to write target rules.
21654 @node Timestamps and Make
21655 @section Timestamp Resolution and Make
21656 @cindex timestamp resolution
21657 Traditionally, file timestamps had 1-second resolution, and
21658 @command{make} used those timestamps to determine whether one file was
21659 newer than the other. However, many modern file systems have
21660 timestamps with 1-nanosecond resolution. Some @command{make}
21661 implementations look at the entire timestamp; others ignore the
21662 fractional part, which can lead to incorrect results. Normally this
21663 is not a problem, but in some extreme cases you may need to use tricks
21664 like @samp{sleep 1} to work around timestamp truncation bugs.
21666 Commands like @samp{cp -p} and @samp{touch -r} typically do not copy
21667 file timestamps to their full resolutions (@pxref{touch, , Limitations of Usual
21668 Tools}). Hence you should be wary of rules like this:
21675 as @file{dest} often appears to be older than @file{src} after the
21676 timestamp is truncated, and this can cause @command{make} to do
21677 needless rework the next time it is invoked. To work around this
21678 problem, you can use a timestamp file, e.g.:
21686 Apart from timestamp resolution, there are also differences in handling
21687 equal timestamps. HP-UX @command{make} updates targets if it has the
21688 same timestamp as one of its prerequisites, in violation of Posix rules.
21690 This can cause spurious rebuilds for repeated runs of @command{make}.
21691 This in turn can cause @command{make} to fail if it tries to rebuild
21692 generated files in a possibly read-only source tree with tools not
21693 present on the end-user machine. Use GNU @command{make} instead.
21697 @c ======================================== Portable C and C++ Programming
21699 @node Portable C and C++
21700 @chapter Portable C and C++ Programming
21701 @cindex Portable C and C++ programming
21703 C and C++ programs often use low-level features of the underlying
21704 system, and therefore are often more difficult to make portable to other
21707 Several standards have been developed to help make your programs more
21708 portable. If you write programs with these standards in mind, you can
21709 have greater confidence that your programs work on a wide variety
21712 @uref{https://@/gcc.gnu.org/@/onlinedocs/@/gcc/@/Standards.html, Language
21713 Standards Supported by GCC}
21716 @xref{Standards, , Language Standards Supported by
21717 GCC, gcc, Using the GNU Compiler Collection
21720 for a list of C-related standards. Many programs also assume the
21721 @uref{https://@/en.wikipedia.org/@/wiki/@/POSIX, Posix standard}.
21724 @cindex C89, C99, C11, C17, and C23
21725 The first widely used C variant was K&R C, which predates any C
21726 standard. K&R C compilers are no longer of practical interest, though,
21727 and Autoconf assumes at least C89, the first C standard,
21728 which is sometimes called ``C90'' due to a delay in standardization.
21729 C has since gone through the standards C99, C11, C17, and C23, and
21730 Autoconf is compatible with all these standards.
21732 Program portability is a huge topic, and this section can only briefly
21733 introduce common pitfalls. @xref{System Portability, , Portability
21734 between System Types, standards, The GNU Coding Standards}, for
21738 * Varieties of Unportability:: How to make your programs unportable
21739 * Integer Overflow:: When integers get too large
21740 * Preprocessor Arithmetic:: @code{#if} expression problems
21741 * Null Pointers:: Properties of null pointers
21742 * Buffer Overruns:: Subscript errors and the like
21743 * Volatile Objects:: @code{volatile} and signals
21744 * Floating Point Portability:: Portable floating-point arithmetic
21745 * Exiting Portably:: Exiting and the exit status
21748 @node Varieties of Unportability
21749 @section Varieties of Unportability
21750 @cindex portability
21752 Autoconf tests and ordinary programs often need to test what is allowed
21753 on a system, and therefore they may need to deliberately exceed the
21754 boundaries of what the standards allow, if only to see whether an
21755 optional feature is present. When you write such a program, you should
21756 keep in mind the difference between constraints, unspecified behavior,
21757 and undefined behavior.
21759 In C, a @dfn{constraint} is a rule that the compiler must enforce. An
21760 example constraint is that C programs must not declare a bit-field with
21761 negative width. Tests can therefore reliably assume that programs with
21762 negative-width bit-fields are rejected by a compiler that conforms
21765 @dfn{Unspecified behavior} is valid behavior, where the standard allows
21766 multiple possibilities. For example, the order of evaluation of
21767 function arguments is unspecified. Some unspecified behavior is
21768 @dfn{implementation-defined}, i.e., documented by the implementation,
21769 but since Autoconf tests cannot read the documentation they cannot
21770 distinguish between implementation-defined and other unspecified
21771 behavior. It is common for Autoconf tests to probe implementations to
21772 determine otherwise-unspecified behavior.
21774 @dfn{Undefined behavior} is invalid behavior, where the standard allows
21775 the implementation to do anything it pleases. For example,
21776 dereferencing a null pointer leads to undefined behavior. If possible,
21777 test programs should avoid undefined behavior, since a program with
21778 undefined behavior might succeed on a test that should fail.
21780 The above rules apply to programs that are intended to conform to the
21781 standard. However, strictly-conforming programs are quite rare, since
21782 the standards are so limiting. A major goal of Autoconf is to support
21783 programs that use implementation features not described by the standard,
21784 and it is fairly common for test programs to violate the above rules, if
21785 the programs work well enough in practice.
21787 @node Integer Overflow
21788 @section Integer Overflow
21789 @cindex integer overflow
21790 @cindex overflow, signed integer
21791 @cindex signed integer overflow
21792 @cindex wraparound arithmetic
21794 Although some traditional C programs assume that signed integer overflow
21795 wraps around reliably using two's complement arithmetic, the C standard
21796 says that program behavior is undefined on overflow, and these C
21797 programs may not work on many modern implementations.
21800 * Integer Overflow Basics:: Why integer overflow is a problem
21801 * Signed Overflow Examples:: Examples of code assuming wraparound
21802 * Optimization and Wraparound:: Optimizations that break uses of wraparound
21803 * Signed Overflow Advice:: Practical advice for signed overflow issues
21804 * Signed Integer Division:: @code{INT_MIN / -1} and @code{INT_MIN % -1}
21807 @node Integer Overflow Basics
21808 @subsection Basics of Integer Overflow
21809 @cindex integer overflow
21810 @cindex overflow, signed integer
21811 @cindex signed integer overflow
21812 @cindex wraparound arithmetic
21814 In languages like C, integer overflow wraps around for unsigned
21815 integer types that are at least as wide as @code{unsigned int};
21816 e.g., @code{UINT_MAX + 1} yields zero.
21817 This is guaranteed by the C standard and is
21818 portable in practice, unless you specify aggressive,
21819 nonstandard optimization options
21820 suitable only for special applications.
21822 In contrast, the C standard says that signed integer overflow leads to
21823 undefined behavior where a program can do anything, including dumping
21824 core or overrunning a buffer. The misbehavior can even precede the
21825 overflow. Such an overflow can occur during addition, subtraction,
21826 multiplication, division, and left shift. It can even occur for
21827 unsigned types like @code{unsigned short int} that are narrower
21828 than @code{int}, as values of these types are widened to @code{int}
21829 before computation.
21831 Despite this requirement of the standard, some C programs assume that
21832 signed integer overflow silently wraps around modulo a power of two,
21833 using two's complement arithmetic, so long as you convert the resulting
21834 value to a signed integer type. These programs can have problems,
21835 especially when optimization is enabled. If you assume a GCC-like
21836 compiler, you can work around the problems by compiling with GCC's
21837 @code{-fwrapv} option; however, this is not portable.
21839 For historical reasons C17 and earlier also allowed implementations with
21840 ones' complement or signed magnitude arithmetic, but C23 requires
21841 two's complement and it is safe to assume two's complement nowadays.
21843 Also, overflow can occur when converting an out-of-range value to a
21844 signed integer type. Here a standard implementation must define what
21845 happens, and this can include raising an exception. Although practical
21846 implementations typically wrap around silently in this case, a few
21847 debugging implementations trap instead.
21849 @node Signed Overflow Examples
21850 @subsection Examples of Code Assuming Wraparound Overflow
21851 @cindex integer overflow
21852 @cindex overflow, signed integer
21853 @cindex signed integer overflow
21854 @cindex wraparound arithmetic
21856 There was long a tension between what the C standard requires for signed
21857 integer overflow, and what traditional C programs commonly assumed. The
21858 standard allows aggressive optimizations based on assumptions that
21859 overflow never occurs, but traditionally many C programs relied on overflow
21860 wrapping around. Although these programs did not conform to the standard,
21861 they formerly worked in practice because traditionally compilers did not
21862 optimize in such a way that would break the programs. Nowadays, though,
21863 compilers do perform these optimizations, so portable programs can no
21864 longer assume reliable wraparound on signed integer overflow.
21866 The C Standard says that if a program has signed integer overflow its
21867 behavior is undefined, and the undefined behavior can even precede the
21868 overflow. To take an extreme example:
21870 @c Inspired by Robert Dewar's example in
21871 @c <https://gcc.gnu.org/ml/gcc/2007-01/msg00038.html> (2007-01-01).
21873 if (password == expected_password)
21874 allow_superuser_privileges ();
21875 else if (counter++ == INT_MAX)
21878 printf ("%d password mismatches\n", counter);
21882 If the @code{int} variable @code{counter} equals @code{INT_MAX},
21883 @code{counter++} must overflow and the behavior is undefined, so the C
21884 standard allows the compiler to optimize away the test against
21885 @code{INT_MAX} and the @code{abort} call.
21886 Worse, if an earlier bug in the program lets the compiler deduce that
21887 @code{counter == INT_MAX} or that @code{counter} previously overflowed,
21888 the C standard allows the compiler to optimize away the password test
21889 and generate code that allows superuser privileges unconditionally.
21891 Here is an example derived from the 7th Edition Unix implementation of
21892 @code{atoi} (1979-01-10):
21898 while (*p >= '0' && *p <= '9')
21899 n = n * 10 + *p++ - '0';
21900 return (f ? -n : n);
21904 Even if the input string is in range, on most modern machines this has
21905 signed overflow when computing the most negative integer (the @code{-n}
21906 overflows) or a value near an extreme integer (the @code{+}
21909 Here is another example, derived from the 7th Edition implementation of
21910 @code{rand} (1979-01-10). Here the programmer expects both
21911 multiplication and addition to wrap on overflow:
21914 static long int randx = 1;
21916 randx = randx * 1103515245 + 12345;
21917 return (randx >> 16) & 077777;
21920 In the following example, derived from the GNU C Library 2.15
21921 implementation of @code{mktime} (2012-03-21), the code assumes
21922 wraparound arithmetic in @code{+} to detect signed overflow:
21926 int sec_requested, sec_adjustment;
21928 t1 = t + sec_requested;
21929 t2 = t1 + sec_adjustment;
21930 if (((t1 < t) != (sec_requested < 0))
21931 | ((t2 < t1) != (sec_adjustment < 0)))
21935 Although some of these examples will likely behave as if signed integer
21936 overflow wraps around reliably, other examples are likely to misbehave
21937 when optimization is enabled. All these examples should be avoided in
21938 portable code because signed integer overflow is not reliable on modern
21939 systems, and it's not worth worrying about which of these examples
21940 happen to work on most platforms and which do not.
21942 @node Optimization and Wraparound
21943 @subsection Optimizations That Break Wraparound Arithmetic
21944 @cindex loop induction
21946 Compilers sometimes generate code that is incompatible with wraparound
21947 integer arithmetic. A simple example is an algebraic simplification: a
21948 compiler might translate @code{(i * 2000) / 1000} to @code{i * 2}
21949 because it assumes that @code{i * 2000} does not overflow. The
21950 translation is not equivalent to the original when overflow occurs:
21951 e.g., in the typical case of 32-bit signed two's complement wraparound
21952 @code{int}, if @code{i} has type @code{int} and value @code{1073742},
21953 the original expression returns @minus{}2147483 but the optimized
21954 version returns the mathematically correct value 2147484.
21956 More subtly, loop induction optimizations often exploit the undefined
21957 behavior of signed overflow. Consider the following contrived function
21962 sumc (int lo, int hi)
21965 for (int i = lo; i <= hi; i++)
21972 To avoid multiplying by 53 each time through the loop, an optimizing
21973 compiler might internally transform @code{sumc} to the equivalent of the
21978 transformed_sumc (int lo, int hi)
21982 for (int ic = lo * 53; ic <= hic; ic += 53)
21989 This transformation is allowed by the C standard, but it is invalid for
21990 wraparound arithmetic when @code{INT_MAX / 53 < hi}, because then the
21991 overflow in computing expressions like @code{hi * 53} can cause the
21992 expression @code{i <= hi} to yield a different value from the
21993 transformed expression @code{ic <= hic}.
21995 For this reason, compilers that use loop induction and similar
21996 techniques often do not support reliable wraparound arithmetic when a
21997 loop induction variable like @code{ic} is involved. Since loop
21998 induction variables are generated by the compiler, and are not visible
21999 in the source code, it is not always trivial to say whether the problem
22002 Hardly any code actually depends on wraparound arithmetic in cases like
22003 these, so in practice these loop induction optimizations are almost
22004 always useful. However, edge cases in this area can cause problems.
22008 for (int j = 1; 0 < j; j *= 2)
22013 Here, the loop attempts to iterate through all powers of 2 that
22014 @code{int} can represent, but the C standard allows a compiler to
22015 optimize away the comparison and generate an infinite loop,
22016 under the argument that behavior is undefined on overflow. As of this
22017 writing this optimization is done on some platforms by
22018 GCC with @option{-O2}, so this code is not portable in practice.
22020 @node Signed Overflow Advice
22021 @subsection Practical Advice for Signed Overflow Issues
22022 @cindex integer overflow
22023 @cindex overflow, signed integer
22024 @cindex signed integer overflow
22025 @cindex wraparound arithmetic
22027 Ideally the safest approach is to avoid signed integer overflow
22028 entirely. For example, instead of multiplying two signed integers, you
22029 can convert them to double-width integers, multiply the wider values,
22030 then test whether the result is in the narrower range. Or you can use
22031 more-complicated code employing unsigned integers of the same width.
22033 Rewriting code in this way will be inconvenient, though, especially if
22034 the signed values might be negative and no wider type is available.
22035 Using unsigned arithmetic to check for overflow is
22036 particularly painful to do portably and efficiently when dealing with an
22037 integer type like @code{uid_t} whose width and signedness vary from
22038 platform to platform. Also, this approach may hurt performance.
22040 Hence it is often useful to maintain code that needs
22041 wraparound on overflow, instead of rewriting the code. The rest of this
22042 section attempts to give practical advice for this situation.
22044 To detect integer overflow portably when attempting operations like
22045 @code{sum = a + b}, you can use the C23 @code{<stdckdint.h>} macros
22046 @code{ckd_add}, @code{ckd_sub}, and @code{ckd_mul}.
22047 The following code adds two integers with overflow wrapping around
22048 reliably in the sum:
22051 #include <stdckdint.h>
22053 /* Set sum = a + b, with wraparound. */
22054 if (ckd_add (&sum, a, b))
22055 /* 'sum' has just the low order bits. */;
22057 /* 'sum' is the correct answer. */;
22060 To be portable to pre-C23 platforms you can use Gnulib's
22061 @code{stdckdint} module, which emulates this part of C23 (@pxref{Gnulib}).
22062 Invoking the @code{stdckdint} macros typically costs just one machine
22063 instruction for the arithmetic and another instruction for the rare
22064 branch on overflow.
22066 If your code uses a signed loop index, make sure that the index cannot
22067 overflow, along with all signed expressions derived from the index.
22068 Here is a contrived example of problematic code with two instances of
22072 for (int i = INT_MAX - 10; i <= INT_MAX; i++)
22075 report_overflow ();
22081 Because of the two overflows, a compiler might optimize away or
22082 transform the two comparisons in a way that is incompatible with the
22083 wraparound assumption.
22085 If your code is intended to be compiled only by GCC and
22086 assumes wraparound behavior, and you want to insulate it
22087 against any GCC optimizations that would fail to support that
22088 behavior, you should use GCC's @option{-fwrapv} option, which
22089 causes signed overflow to wrap around reliably (except for division and
22090 remainder, as discussed in the next section).
22092 If you need to write portable code and therefore cannot assume that
22093 signed integer overflow wraps around reliably, you should consider
22094 debugging with a GCC option that causes signed overflow to raise an
22095 exception. These options include @option{-fsanitize=undefined} and
22098 @node Signed Integer Division
22099 @subsection Signed Integer Division and Integer Overflow
22100 @cindex division, integer
22103 integer division is not always harmless: for example, on CPUs of the
22104 i386 family, dividing @code{INT_MIN} by @code{-1} yields a SIGFPE signal
22105 which by default terminates the program. Worse, taking the remainder
22106 of these two values typically yields the same signal on these CPUs,
22107 behavior that the C standard allows.
22109 @node Preprocessor Arithmetic
22110 @section Preprocessor Arithmetic
22111 @cindex preprocessor arithmetic
22113 In C99 and later, preprocessor arithmetic, used for @code{#if}
22115 be evaluated as if all signed values are of type @code{intmax_t} and all
22116 unsigned values of type @code{uintmax_t}. Many compilers are buggy in
22117 this area, though. For example, as of 2007, Sun C mishandles @code{#if
22118 LLONG_MIN < 0} on a platform with 32-bit @code{long int} and 64-bit
22119 @code{long long int}. Also, some older preprocessors mishandle
22120 constants ending in @code{LL}. To work around these problems, you can
22121 compute the value of expressions like @code{LONG_MAX < LLONG_MAX} at
22122 @code{configure}-time rather than at @code{#if}-time.
22124 @node Null Pointers
22125 @section Properties of Null Pointers
22126 @cindex null pointers
22128 Most modern hosts reliably fail when you attempt to dereference a null
22131 On almost all modern hosts, null pointers use an all-bits-zero internal
22132 representation, so you can reliably use @code{memset} with 0 to set all
22133 the pointers in an array to null values.
22135 If @code{p} is a null pointer to an object type, the C expression
22136 @code{p + 0} always evaluates to @code{p} on modern hosts, even though
22137 the standard says that it has undefined behavior.
22139 @node Buffer Overruns
22140 @section Buffer Overruns and Subscript Errors
22141 @cindex buffer overruns
22143 Buffer overruns and subscript errors are the most common dangerous
22144 errors in C programs. They result in undefined behavior because storing
22145 outside an array typically modifies storage that is used by some other
22146 object, and most modern systems lack runtime checks to catch these
22147 errors. Programs should not rely on buffer overruns being caught.
22149 There is one exception to the usual rule that a portable program cannot
22150 address outside an array. In C, it is valid to compute the address just
22151 past an object, e.g., @code{&a[N]} where @code{a} has @code{N} elements,
22152 so long as you do not dereference the resulting pointer. But it is not
22153 valid to compute the address just before an object, e.g., @code{&a[-1]};
22154 nor is it valid to compute two past the end, e.g., @code{&a[N+1]}. On
22155 most platforms @code{&a[-1] < &a[0] && &a[N] < &a[N+1]}, but this is not
22156 reliable in general, and it is usually easy enough to avoid the
22157 potential portability problem, e.g., by allocating an extra unused array
22158 element at the start or end.
22160 @uref{https://@/www.valgrind.org/, Valgrind} can catch many overruns.
22161 GCC users might also consider using the @option{-fsanitize=} options
22163 @xref{Instrumentation Options, , Program Instrumentation Options,
22164 gcc, Using the GNU Compiler Collection (GCC)}.
22166 Buffer overruns are usually caused by off-by-one errors, but there are
22167 more subtle ways to get them.
22169 Using @code{int} values to index into an array or compute array sizes
22170 causes problems on typical 64-bit hosts where an array index might
22171 be @math{2^{31}} or larger. Index values of type @code{size_t} avoid this
22172 problem, but cannot be negative. Index values of type @code{ptrdiff_t}
22173 are signed, and are wide enough in practice.
22175 If you add or multiply two numbers to calculate an array size, e.g.,
22176 @code{malloc (x * sizeof y + z)}, havoc ensues if the addition or
22177 multiplication overflows.
22179 Many implementations of the @code{alloca} function silently misbehave
22180 and can generate buffer overflows if given sizes that are too large.
22181 The size limits are implementation dependent, but are at least 4000
22182 bytes on all platforms that we know about.
22184 The standard functions @code{asctime}, @code{asctime_r}, @code{ctime},
22185 @code{ctime_r}, and @code{gets} are prone to buffer overflows, and
22186 portable code should not use them unless the inputs are known to be
22187 within certain limits. The time-related functions can overflow their
22188 buffers if given timestamps out of range (e.g., a year less than -999
22189 or greater than 9999). Time-related buffer overflows cannot happen with
22190 recent-enough versions of the GNU C library, but are possible
22192 implementations. The @code{gets} function is the worst, since it almost
22193 invariably overflows its buffer when presented with an input line larger
22196 @node Volatile Objects
22197 @section Volatile Objects
22198 @cindex volatile objects
22200 The keyword @code{volatile} is often misunderstood in portable code.
22201 Its use inhibits some memory-access optimizations, but programmers often
22202 wish that it had a different meaning than it actually does.
22204 @code{volatile} was designed for code that accesses special objects like
22205 memory-mapped device registers whose contents spontaneously change.
22206 Such code is inherently low-level, and it is difficult to specify
22207 portably what @code{volatile} means in these cases. The C standard
22208 says, ``What constitutes an access to an object that has
22209 volatile-qualified type is implementation-defined,'' so in theory each
22210 implementation is supposed to fill in the gap by documenting what
22211 @code{volatile} means for that implementation. In practice, though,
22212 this documentation is usually absent or incomplete.
22214 One area of confusion is the distinction between objects defined with
22215 volatile types, and volatile lvalues. From the C standard's point of
22216 view, an object defined with a volatile type has externally visible
22217 behavior. You can think of such objects as having little oscilloscope
22218 probes attached to them, so that the user can observe some properties of
22219 accesses to them, just as the user can observe data written to output
22220 files. However, the standard does not make it clear whether users can
22221 observe accesses by volatile lvalues to ordinary objects. For example:
22224 /* Declare and access a volatile object.
22225 Accesses to X are "visible" to users. */
22226 static int volatile x;
22229 /* Access two ordinary objects via a volatile lvalue.
22230 It's not clear whether accesses to *P are "visible". */
22232 int *z = malloc (sizeof (int));
22240 Programmers often wish that @code{volatile} meant ``Perform the memory
22241 access here and now, without merging several memory accesses, without
22242 changing the memory word size, and without reordering.'' But the C
22243 standard does not require this. For objects defined with a volatile
22244 type, accesses must be done before the next sequence point; but
22245 otherwise merging, reordering, and word-size change is allowed. Worse,
22246 it is not clear from the standard whether volatile lvalues provide more
22247 guarantees in general than nonvolatile lvalues, if the underlying
22248 objects are ordinary.
22250 Even when accessing objects defined with a volatile type,
22251 the C standard allows only
22252 extremely limited signal handlers: in C99 the behavior is undefined if a signal
22253 handler reads any non-local object, or writes to any non-local object
22254 whose type is not @code{sig_atomic_t volatile}, or calls any standard
22255 library function other than @code{abort}, @code{signal}, and
22256 @code{_Exit}. Hence C compilers need not worry about a signal handler
22257 disturbing ordinary computation. C11 and Posix allow some additional
22258 behavior in a portable signal handler, but are still quite restrictive.
22260 Some C implementations allow memory-access optimizations within each
22261 translation unit, such that actual behavior agrees with the behavior
22262 required by the standard only when calling a function in some other
22263 translation unit, and a signal handler acts like it was called from a
22264 different translation unit. The C99 standard hints that in these
22265 implementations, objects referred to by signal handlers ``would require
22266 explicit specification of @code{volatile} storage, as well as other
22267 implementation-defined restrictions.'' But unfortunately even for this
22268 special case these other restrictions are often not documented well.
22269 This area was significantly changed in C11, and eventually implementations
22270 will probably head in the C11 direction, but this will take some time.
22271 @xref{Volatiles, , When is a Volatile Object Accessed?, gcc, Using the
22272 GNU Compiler Collection (GCC)}, for some
22273 restrictions imposed by GCC. @xref{Defining Handlers, ,
22274 Defining Signal Handlers, libc, The GNU C Library}, for some
22275 restrictions imposed by the GNU C library. Restrictions
22276 differ on other platforms.
22278 If possible, it is best to use a signal handler that fits within the
22279 limits imposed by the C and Posix standards.
22281 If this is not practical, you can try the following rules of thumb. A
22282 signal handler should access only volatile lvalues, preferably lvalues
22283 that refer to objects defined with a volatile type, and should not
22284 assume that the accessed objects have an internally consistent state
22285 if they are larger than a machine word. Furthermore, installers
22286 should employ compilers and compiler options that are commonly used
22287 for building operating system kernels, because kernels often need more
22288 from @code{volatile} than the C Standard requires, and installers who
22289 compile an application in a similar environment can sometimes benefit
22290 from the extra constraints imposed by kernels on compilers.
22291 Admittedly we are hand-waving somewhat here, as there are few
22292 guarantees in this area; the rules of thumb may help to fix some bugs
22293 but there is a good chance that they will not fix them all.
22295 For @code{volatile}, C++ has the same problems that C does.
22296 Multithreaded applications have even more problems with @code{volatile},
22297 but they are beyond the scope of this section.
22299 The bottom line is that using @code{volatile} typically hurts
22300 performance but should not hurt correctness. In some cases its use
22301 does help correctness, but these cases are often so poorly understood
22302 that all too often adding @code{volatile} to a data structure merely
22303 alleviates some symptoms of a bug while not fixing the bug in general.
22305 @node Floating Point Portability
22306 @section Floating Point Portability
22307 @cindex floating point
22309 Almost all modern systems use IEEE-754 floating point, and it is safe to
22310 assume IEEE-754 in most portable code these days. For more information,
22311 please see David Goldberg's classic paper
22312 @uref{http://@/www.validlab.com/@/goldberg/@/paper.pdf, What Every Computer
22313 Scientist Should Know About Floating-Point Arithmetic}.
22315 @node Exiting Portably
22316 @section Exiting Portably
22317 @cindex exiting portably
22319 A C or C++ program can exit with status @var{N} by returning
22320 @var{N} from the @code{main} function. Portable programs are supposed
22321 to exit either with status 0 or @code{EXIT_SUCCESS} to succeed, or with
22322 status @code{EXIT_FAILURE} to fail, but in practice it is portable to
22323 fail by exiting with status 1, and test programs that assume Posix can
22324 fail by exiting with status values from 1 through 255.
22326 A program can also exit with status @var{N} by passing @var{N} to the
22327 @code{exit} function, and a program can fail by calling the @code{abort}
22328 function. If a program is specialized to just some platforms, it can fail
22329 by calling functions specific to those platforms, e.g., @code{_exit}
22330 (Posix). However, like other functions, an exit
22331 function should be declared, typically by including a header. For
22332 example, if a C program calls @code{exit}, it should include @file{stdlib.h}
22333 either directly or via the default includes (@pxref{Default Includes}).
22335 A program can fail due to undefined behavior such as dereferencing a null
22336 pointer, but this is not recommended as undefined behavior allows an
22337 implementation to do whatever it pleases and this includes exiting
22341 @c ================================================== Manual Configuration
22343 @node Manual Configuration
22344 @chapter Manual Configuration
22346 A few kinds of features can't be guessed automatically by running test
22347 programs. For example, the details of the object-file format, or
22348 special options that need to be passed to the compiler or linker.
22349 Autoconf provides a uniform method for handling unguessable features,
22350 by giving each operating system a @dfn{canonical system type}, also
22351 known as a @dfn{canonical name} or @dfn{target triplet}.
22353 @prindex @command{config.guess}
22354 @prindex @command{config.sub}
22356 If you use any of the macros described in this chapter, you must
22357 distribute the helper scripts @command{config.guess} and
22358 @command{config.sub} along with your source code. Some Autoconf macros
22359 use these macros internally, so you may need to distribute these scripts
22360 even if you do not use any of these macros yourself. @xref{Input}, for
22361 information about the @code{AC_CONFIG_AUX_DIR} macro which you can use
22362 to control in which directory @command{configure} looks for helper
22363 scripts, and where to get the scripts from.
22366 * Specifying Target Triplets:: Specifying target triplets
22367 * Canonicalizing:: Getting the canonical system type
22368 * Using System Type:: What to do with the system type
22371 @node Specifying Target Triplets
22372 @section Specifying target triplets
22373 @cindex System type
22374 @cindex Target triplet
22375 @c This node used to be named Specifying Names. The @anchor allows old
22376 @c links to still work.
22377 @anchor{Specifying Names}
22380 @command{configure} scripts can make decisions based on a canonical name
22381 for the system type, or @dfn{target triplet}, which has the form:
22382 @samp{@var{cpu}-@var{vendor}-@var{os}}, where @var{os} can be
22383 @samp{@var{system}} or @samp{@var{kernel}-@var{system}}
22385 @command{configure} can usually guess the canonical name for the type of
22386 system it's running on. To do so it runs a script called
22387 @command{config.guess}, which infers the name using the @code{uname}
22388 command or symbols predefined by the C preprocessor.
22390 Alternately, the user can specify the system type with command line
22391 arguments to @command{configure} (@pxref{System Types}. Doing so is
22393 cross-compiling. In the most complex case of cross-compiling, three
22394 system types are involved. The options to specify them are:
22397 @item --build=@var{build-type}
22398 the type of system on which the package is being configured and
22399 compiled. It defaults to the result of running @command{config.guess}.
22400 Specifying a @var{build-type} that differs from @var{host-type} enables
22401 cross-compilation mode.
22403 @item --host=@var{host-type}
22404 the type of system on which the package runs. By default it is the
22405 same as the build machine. The tools that get used to build and
22406 manipulate binaries will, by default, all be prefixed with
22407 @code{@var{host-type}-}, such as @code{@var{host-type}-gcc},
22408 @code{@var{host-type}-g++}, @code{@var{host-type}-ar}, and
22409 @code{@var{host-type}-nm}. If the binaries produced by these tools can
22410 be executed by the build system, the configure script will make use of
22411 it in @code{AC_RUN_IFELSE} invocations; otherwise, cross-compilation
22412 mode is enabled. Specifying a @var{host-type} that differs
22413 from @var{build-type}, when @var{build-type} was also explicitly
22414 specified, equally enables cross-compilation mode.
22416 @item --target=@var{target-type}
22417 the type of system for which any compiler tools in the package
22418 produce code (rarely needed). By default, it is the same as host.
22421 If you mean to override the result of @command{config.guess} but
22422 still produce binaries for the build machine, use @option{--build},
22423 not @option{--host}.
22425 So, for example, to produce binaries for 64-bit MinGW, use a command
22429 ./configure --host=x86_64-w64-mingw64
22432 If your system has the ability to execute MinGW binaries but you don't
22433 want to make use of this feature and instead prefer cross-compilation
22434 guesses, use a command like this:
22437 ./configure --build=x86_64-pc-linux-gnu --host=x86_64-w64-mingw64
22441 Note that if you do not specify @option{--host}, @command{configure}
22442 fails if it can't run the code generated by the specified compiler. For
22443 example, configuring as follows fails:
22446 ./configure CC=x86_64-w64-mingw64-gcc
22449 When cross-compiling, @command{configure} will warn about any tools
22450 (compilers, linkers, assemblers) whose name is not prefixed with the
22451 host type. This is an aid to users performing cross-compilation.
22452 Continuing the example above, if a cross-compiler named @command{cc} is
22453 used with a native @command{pkg-config}, then libraries found by
22454 @command{pkg-config} will likely cause subtle build failures; but using
22455 the names @command{x86_64-w64-mingw64-gcc} and
22456 @command{x86_64-w64-mingw64-pkg-config}
22457 avoids any confusion. Avoiding the warning is as simple as creating the
22458 correct symlinks naming the cross tools.
22460 @cindex @command{config.sub}
22461 @command{configure} recognizes short aliases for many system types; for
22462 example, @samp{decstation} can be used instead of
22463 @samp{mips-dec-ultrix4.2}. @command{configure} runs a script called
22464 @command{config.sub} to canonicalize system type aliases.
22466 This section deliberately omits the description of the obsolete
22467 interface; see @ref{Hosts and Cross-Compilation}.
22470 @node Canonicalizing
22471 @section Getting the Canonical System Type
22472 @cindex System type
22473 @cindex Canonical system type
22475 The following macros make the system type available to @command{configure}
22478 @ovindex build_alias
22479 @ovindex host_alias
22480 @ovindex target_alias
22482 The variables @samp{build_alias}, @samp{host_alias}, and
22483 @samp{target_alias} are always exactly the arguments of @option{--build},
22484 @option{--host}, and @option{--target}; in particular, they are left empty
22485 if the user did not use them, even if the corresponding
22486 @code{AC_CANONICAL} macro was run. Any configure script may use these
22487 variables anywhere. These are the variables that should be used when in
22488 interaction with the user.
22490 If you need to recognize some special environments based on their system
22491 type, run the following macros to get canonical system names. These
22492 variables are not set before the macro call.
22494 @defmac AC_CANONICAL_BUILD
22495 @acindex{CANONICAL_BUILD}
22498 @ovindex build_vendor
22500 Compute the canonical build-system type variable, @code{build}, and its
22501 three individual parts @code{build_cpu}, @code{build_vendor}, and
22504 If @option{--build} was specified, then @code{build} is the
22505 canonicalization of @code{build_alias} by @command{config.sub},
22506 otherwise it is determined by the shell script @command{config.guess}.
22509 @defmac AC_CANONICAL_HOST
22510 @acindex{CANONICAL_HOST}
22513 @ovindex host_vendor
22515 Compute the canonical host-system type variable, @code{host}, and its
22516 three individual parts @code{host_cpu}, @code{host_vendor}, and
22519 If @option{--host} was specified, then @code{host} is the
22520 canonicalization of @code{host_alias} by @command{config.sub},
22521 otherwise it defaults to @code{build}.
22524 @defmac AC_CANONICAL_TARGET
22525 @acindex{CANONICAL_TARGET}
22527 @ovindex target_cpu
22528 @ovindex target_vendor
22530 Compute the canonical target-system type variable, @code{target}, and its
22531 three individual parts @code{target_cpu}, @code{target_vendor}, and
22534 If @option{--target} was specified, then @code{target} is the
22535 canonicalization of @code{target_alias} by @command{config.sub},
22536 otherwise it defaults to @code{host}.
22539 Note that there can be artifacts due to the backward compatibility
22540 code. @xref{Hosts and Cross-Compilation}, for more.
22542 @node Using System Type
22543 @section Using the System Type
22545 In @file{configure.ac} the system type is generally used by one or more
22546 @code{case} statements to select system-specifics. Shell wildcards can
22547 be used to match a group of system types.
22549 For example, an extra assembler code object file could be chosen, giving
22550 access to a CPU cycle counter register. @code{$(CYCLE_OBJ)} in the
22551 following would be used in a makefile to add the object to a
22552 program or library.
22556 [alpha*-*-*], [CYCLE_OBJ=rpcc.o],
22557 [i?86-*-*], [CYCLE_OBJ=rdtsc.o],
22559 AC_SUBST([CYCLE_OBJ])
22562 @code{AC_CONFIG_LINKS} (@pxref{Configuration Links}) is another good way
22563 to select variant source files, for example optimized code for some
22564 CPUs. The configured CPU type doesn't always indicate exact CPU types,
22565 so some runtime capability checks may be necessary too.
22569 [alpha*-*-*], [AC_CONFIG_LINKS([dither.c:alpha/dither.c])],
22570 [powerpc*-*-*], [AC_CONFIG_LINKS([dither.c:powerpc/dither.c])],
22571 [AC_CONFIG_LINKS([dither.c:generic/dither.c])])
22574 The host system type can also be used to find cross-compilation tools
22575 with @code{AC_CHECK_TOOL} (@pxref{Generic Programs}).
22577 The above examples all show @samp{$host}, since this is where the code
22578 is going to run. Only rarely is it necessary to test @samp{$build}
22579 (which is where the build is being done).
22581 Whenever you're tempted to use @samp{$host} it's worth considering
22582 whether some sort of probe would be better. New system types come along
22583 periodically or previously missing features are added. Well-written
22584 probes can adapt themselves to such things, but hard-coded lists of
22585 names can't. Here are some guidelines,
22589 Availability of libraries and library functions should always be checked
22592 Variant behavior of system calls is best identified with runtime tests
22593 if possible, but bug workarounds or obscure difficulties might have to
22594 be driven from @samp{$host}.
22596 Assembler code is inevitably highly CPU-specific and is best selected
22597 according to @samp{$host_cpu}.
22599 Assembler variations like underscore prefix on globals or ELF versus
22600 COFF type directives are however best determined by probing, perhaps
22601 even examining the compiler output.
22604 @samp{$target} is for use by a package creating a compiler or similar.
22605 For ordinary packages it's meaningless and should not be used. It
22606 indicates what the created compiler should generate code for, if it can
22607 cross-compile. @samp{$target} generally selects various hard-coded CPU
22608 and system conventions, since usually the compiler or tools under
22609 construction themselves determine how the target works.
22612 @c ===================================================== Site Configuration.
22614 @node Site Configuration
22615 @chapter Site Configuration
22617 @command{configure} scripts support several kinds of local configuration
22618 decisions. There are ways for users to specify where external software
22619 packages are, include or exclude optional features, install programs
22620 under modified names, and set default values for @command{configure}
22624 * Help Formatting:: Customizing @samp{configure --help}
22625 * External Software:: Working with other optional software
22626 * Package Options:: Selecting optional features
22627 * Pretty Help Strings:: Formatting help string
22628 * Option Checking:: Controlling checking of @command{configure} options
22629 * Site Details:: Configuring site details
22630 * Transforming Names:: Changing program names when installing
22631 * Site Defaults:: Giving @command{configure} local defaults
22634 @node Help Formatting
22635 @section Controlling Help Output
22637 Users consult @samp{configure --help} to learn of configuration
22638 decisions specific to your package. By default, @command{configure}
22639 breaks this output into sections for each type of option; within each
22640 section, help strings appear in the order @file{configure.ac} defines
22646 --enable-bar include bar
22653 @defmac AC_PRESERVE_HELP_ORDER
22654 @acindex{PRESERVE_HELP_ORDER}
22656 Request an alternate @option{--help} format, in which options of all
22657 types appear together, in the order defined. Call this macro before any
22658 @code{AC_ARG_ENABLE} or @code{AC_ARG_WITH}.
22661 Optional Features and Packages:
22663 --enable-bar include bar
22669 @node External Software
22670 @section Working With External Software
22671 @cindex External software
22673 Some packages require, or can optionally use, other software packages
22674 that are already installed. The user can give @command{configure}
22675 command line options to specify which such external software to use.
22676 The options have one of these forms:
22678 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
22681 --with-@var{package}@r{[}=@var{arg}@r{]}
22682 --without-@var{package}
22685 For example, @option{--with-gnu-ld} means work with the GNU linker
22686 instead of some other linker. @option{--with-x} means work with The X
22689 The user can give an argument by following the package name with
22690 @samp{=} and the argument. Giving an argument of @samp{no} is for
22691 packages that are used by default; it says to @emph{not} use the
22692 package. An argument that is neither @samp{yes} nor @samp{no} could
22693 include a name or number of a version of the other package, to specify
22694 more precisely which other package this program is supposed to work
22695 with. If no argument is given, it defaults to @samp{yes}.
22696 @option{--without-@var{package}} is equivalent to
22697 @option{--with-@var{package}=no}.
22699 Normally @command{configure} scripts complain about
22700 @option{--with-@var{package}} options that they do not support.
22701 @xref{Option Checking}, for details, and for how to override the
22704 For each external software package that may be used, @file{configure.ac}
22705 should call @code{AC_ARG_WITH} to detect whether the @command{configure}
22706 user asked to use it. Whether each package is used or not by default,
22707 and which arguments are valid, is up to you.
22709 @anchor{AC_ARG_WITH}
22710 @defmac AC_ARG_WITH (@var{package}, @var{help-string}, @
22711 @ovar{action-if-given}, @ovar{action-if-not-given})
22713 If the user gave @command{configure} the option @option{--with-@var{package}}
22714 or @option{--without-@var{package}}, run shell commands
22715 @var{action-if-given}. If neither option was given, run shell commands
22716 @var{action-if-not-given}. The name @var{package} indicates another
22717 software package that this program should work with. It should consist
22718 only of alphanumeric characters, dashes, plus signs, and dots.
22720 The option's argument is available to the shell commands
22721 @var{action-if-given} in the shell variable @code{withval}, which is
22722 actually just the value of the shell variable named
22723 @code{with_@var{package}}, with any non-alphanumeric characters in
22724 @var{package} changed into @samp{_}. You may use that variable instead,
22727 Note that @var{action-if-not-given} is not expanded until the point that
22728 @code{AC_ARG_WITH} was expanded. If you need the value of
22729 @code{with_@var{package}} set to a default value by the time argument
22730 parsing is completed, use @code{m4_divert_text} to the @code{DEFAULTS}
22731 diversion (@pxref{m4_divert_text}) (if done as an argument to
22732 @code{AC_ARG_WITH}, also provide non-diverted text to avoid a shell
22735 The argument @var{help-string} is a description of the option that
22738 --with-readline support fancy command line editing
22742 @var{help-string} may be more than one line long, if more detail is
22743 needed. Just make sure the columns line up in @samp{configure
22744 --help}. Avoid tabs in the help string. The easiest way to provide the
22745 proper leading whitespace is to format your @var{help-string} with the macro
22746 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
22748 The following example shows how to use the @code{AC_ARG_WITH} macro in
22749 a common situation. You want to let the user decide whether to enable
22750 support for an external library (e.g., the readline library); if the user
22751 specified neither @option{--with-readline} nor @option{--without-readline},
22752 you want to enable support for readline only if the library is available
22755 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within 'if' is solved.
22757 AC_ARG_WITH([readline],
22758 [AS_HELP_STRING([--with-readline],
22759 [support fancy command line editing @@<:@@default=check@@:>@@])],
22761 [: m4_divert_text([DEFAULTS], [with_readline=check])])
22764 AS_IF([test "x$with_readline" != xno],
22765 [AC_CHECK_LIB([readline], [main],
22766 [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
22767 AC_DEFINE([HAVE_LIBREADLINE], [1],
22768 [Define if you have libreadline])
22770 [if test "x$with_readline" != xcheck; then
22772 [--with-readline was given, but test for readline failed])
22777 The next example shows how to use @code{AC_ARG_WITH} to give the user the
22778 possibility to enable support for the readline library, in case it is still
22779 experimental and not well tested, and is therefore disabled by default.
22781 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within 'if' is solved.
22783 AC_ARG_WITH([readline],
22784 [AS_HELP_STRING([--with-readline],
22785 [enable experimental support for readline])],
22787 [with_readline=no])
22790 AS_IF([test "x$with_readline" != xno],
22791 [AC_CHECK_LIB([readline], [main],
22792 [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
22793 AC_DEFINE([HAVE_LIBREADLINE], [1],
22794 [Define if you have libreadline])
22797 [--with-readline was given, but test for readline failed])],
22801 The last example shows how to use @code{AC_ARG_WITH} to give the user the
22802 possibility to disable support for the readline library, given that it is
22803 an important feature and that it should be enabled by default.
22805 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within 'if' is solved.
22807 AC_ARG_WITH([readline],
22808 [AS_HELP_STRING([--without-readline],
22809 [disable support for readline])],
22811 [with_readline=yes])
22814 AS_IF([test "x$with_readline" != xno],
22815 [AC_CHECK_LIB([readline], [main],
22816 [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
22817 AC_DEFINE([HAVE_LIBREADLINE], [1],
22818 [Define if you have libreadline])
22821 [readline test failed (--without-readline to disable)])],
22825 These three examples can be easily adapted to the case where
22826 @code{AC_ARG_ENABLE} should be preferred to @code{AC_ARG_WITH} (see
22827 @ref{Package Options}).
22830 @node Package Options
22831 @section Choosing Package Options
22832 @cindex Package options
22833 @cindex Options, package
22835 If a software package has optional compile-time features, the user can
22836 give @command{configure} command line options to specify whether to
22837 compile them. The options have one of these forms:
22839 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
22842 --enable-@var{feature}@r{[}=@var{arg}@r{]}
22843 --disable-@var{feature}
22846 These options allow users to choose which optional features to build and
22847 install. @option{--enable-@var{feature}} options should never make a
22848 feature behave differently or cause one feature to replace another.
22849 They should only cause parts of the program to be built rather than left
22852 The user can give an argument by following the feature name with
22853 @samp{=} and the argument. Giving an argument of @samp{no} requests
22854 that the feature @emph{not} be made available. A feature with an
22855 argument looks like @option{--enable-debug=stabs}. If no argument is
22856 given, it defaults to @samp{yes}. @option{--disable-@var{feature}} is
22857 equivalent to @option{--enable-@var{feature}=no}.
22859 Normally @command{configure} scripts complain about
22860 @option{--enable-@var{package}} options that they do not support.
22861 @xref{Option Checking}, for details, and for how to override the
22864 For each optional feature, @file{configure.ac} should call
22865 @code{AC_ARG_ENABLE} to detect whether the @command{configure} user asked
22866 to include it. Whether each feature is included or not by default, and
22867 which arguments are valid, is up to you.
22869 @anchor{AC_ARG_ENABLE}
22870 @defmac AC_ARG_ENABLE (@var{feature}, @var{help-string}, @
22871 @ovar{action-if-given}, @ovar{action-if-not-given})
22872 @acindex{ARG_ENABLE}
22873 If the user gave @command{configure} the option
22874 @option{--enable-@var{feature}} or @option{--disable-@var{feature}}, run
22875 shell commands @var{action-if-given}. If neither option was given, run
22876 shell commands @var{action-if-not-given}. The name @var{feature}
22877 indicates an optional user-level facility. It should consist only of
22878 alphanumeric characters, dashes, plus signs, and dots.
22880 The option's argument is available to the shell commands
22881 @var{action-if-given} in the shell variable @code{enableval}, which is
22882 actually just the value of the shell variable named
22883 @code{enable_@var{feature}}, with any non-alphanumeric characters in
22884 @var{feature} changed into @samp{_}. You may use that variable instead,
22885 if you wish. The @var{help-string} argument is like that of
22886 @code{AC_ARG_WITH} (@pxref{External Software}).
22888 Note that @var{action-if-not-given} is not expanded until the point that
22889 @code{AC_ARG_ENABLE} was expanded. If you need the value of
22890 @code{enable_@var{feature}} set to a default value by the time argument
22891 parsing is completed, use @code{m4_divert_text} to the @code{DEFAULTS}
22892 diversion (@pxref{m4_divert_text}) (if done as an argument to
22893 @code{AC_ARG_ENABLE}, also provide non-diverted text to avoid a shell
22896 You should format your @var{help-string} with the macro
22897 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
22899 See the examples suggested with the definition of @code{AC_ARG_WITH}
22900 (@pxref{External Software}) to get an idea of possible applications of
22901 @code{AC_ARG_ENABLE}.
22904 @node Pretty Help Strings
22905 @section Making Your Help Strings Look Pretty
22906 @cindex Help strings
22908 Properly formatting the @samp{help strings} which are used in
22909 @code{AC_ARG_WITH} (@pxref{External Software}) and @code{AC_ARG_ENABLE}
22910 (@pxref{Package Options}) can be challenging. Specifically, you want
22911 your own @samp{help strings} to line up in the appropriate columns of
22912 @samp{configure --help} just like the standard Autoconf @samp{help
22913 strings} do. This is the purpose of the @code{AS_HELP_STRING} macro.
22915 @anchor{AS_HELP_STRING}
22916 @defmac AS_HELP_STRING (@var{left-hand-side}, @var{right-hand-side} @
22917 @dvar{indent-column, 26}, @dvar{wrap-column, 79})
22918 @asindex{HELP_STRING}
22920 Expands into a help string that looks pretty when the user executes
22921 @samp{configure --help}. It is typically used in @code{AC_ARG_WITH}
22922 (@pxref{External Software}) or @code{AC_ARG_ENABLE} (@pxref{Package
22923 Options}). The following example makes this clearer.
22927 [AS_HELP_STRING([--with-foo],
22928 [use foo (default is no)])],
22929 [use_foo=$withval],
22933 Then the last few lines of @samp{configure --help} appear like
22937 --enable and --with options recognized:
22938 --with-foo use foo (default is no)
22941 Macro expansion is performed on the first argument. However, the second
22942 argument of @code{AS_HELP_STRING} is treated as a whitespace separated
22943 list of text to be reformatted, and is not subject to macro expansion.
22944 Since it is not expanded, it should not be double quoted.
22945 @xref{Autoconf Language}, for a more detailed explanation.
22947 The @code{AS_HELP_STRING} macro is particularly helpful when the
22948 @var{left-hand-side} and/or @var{right-hand-side} are composed of macro
22949 arguments, as shown in the following example. Be aware that
22950 @var{left-hand-side} may not expand to unbalanced quotes,
22951 although quadrigraphs can be used.
22954 AC_DEFUN([MY_ARG_WITH],
22955 [AC_ARG_WITH(m4_translit([[$1]], [_], [-]),
22956 [AS_HELP_STRING([--with-m4_translit([$1], [_], [-])],
22957 [use $1 (default is $2)])],
22958 [use_[]$1=$withval],
22960 MY_ARG_WITH([a_b], [no])
22963 Here, the last few lines of @samp{configure --help} will include:
22966 --enable and --with options recognized:
22967 --with-a-b use a_b (default is no)
22970 The parameters @var{indent-column} and @var{wrap-column} were introduced
22971 in Autoconf 2.62. Generally, they should not be specified; they exist
22972 for fine-tuning of the wrapping.
22974 AS_HELP_STRING([--option], [description of option])
22975 @result{} --option description of option
22976 AS_HELP_STRING([--option], [description of option], [15], [30])
22977 @result{} --option description of
22983 @node Option Checking
22984 @section Controlling Checking of @command{configure} Options
22985 @cindex Options, Package
22987 The @command{configure} script checks its command-line options against a
22988 list of known options, like @option{--help} or @option{--config-cache}.
22989 An unknown option ordinarily indicates a mistake by the user and
22990 @command{configure} halts with an error. However, by default unknown
22991 @option{--with-@var{package}} and @option{--enable-@var{feature}}
22992 options elicit only a warning, to support configuring entire source
22995 Source trees often contain multiple packages with a top-level
22996 @command{configure} script that uses the @code{AC_CONFIG_SUBDIRS} macro
22997 (@pxref{Subdirectories}). Because the packages generally support
22998 different @option{--with-@var{package}} and
22999 @option{--enable-@var{feature}} options, the GNU Coding
23000 Standards say they must accept unrecognized options without halting.
23001 Even a warning message is undesirable here, so @code{AC_CONFIG_SUBDIRS}
23002 automatically disables the warnings.
23004 This default behavior may be modified in two ways. First, the installer
23005 can invoke @code{configure --disable-option-checking} to disable
23006 these warnings, or invoke @code{configure --enable-option-checking=fatal}
23007 options to turn them into fatal errors, respectively. Second, the
23008 maintainer can use @code{AC_DISABLE_OPTION_CHECKING}.
23010 @defmac AC_DISABLE_OPTION_CHECKING
23011 @acindex{DISABLE_OPTION_CHECKING}
23013 By default, disable warnings related to any unrecognized
23014 @option{--with-@var{package}} or @option{--enable-@var{feature}}
23015 options. This is implied by @code{AC_CONFIG_SUBDIRS}.
23017 The installer can override this behavior by passing
23018 @option{--enable-option-checking} (enable warnings) or
23019 @option{--enable-option-checking=fatal} (enable errors) to
23020 @command{configure}.
23025 @section Configuring Site Details
23026 @cindex Site details
23028 Some software packages require complex site-specific information. Some
23029 examples are host names to use for certain services, company names, and
23030 email addresses to contact. Since some configuration scripts generated
23031 by Metaconfig ask for such information interactively, people sometimes
23032 wonder how to get that information in Autoconf-generated configuration
23033 scripts, which aren't interactive.
23035 Such site configuration information should be put in a file that is
23036 edited @emph{only by users}, not by programs. The location of the file
23037 can either be based on the @code{prefix} variable, or be a standard
23038 location such as the user's home directory. It could even be specified
23039 by an environment variable. The programs should examine that file at
23040 runtime, rather than at compile time. Runtime configuration is more
23041 convenient for users and makes the configuration process simpler than
23042 getting the information while configuring. @xref{Directory Variables, ,
23043 Variables for Installation Directories, standards, The GNU Coding
23044 Standards}, for more information on where to put data files.
23046 @node Transforming Names
23047 @section Transforming Program Names When Installing
23048 @cindex Transforming program names
23049 @cindex Program names, transforming
23051 Autoconf supports changing the names of programs when installing them.
23052 In order to use these transformations, @file{configure.ac} must call the
23053 macro @code{AC_ARG_PROGRAM}.
23055 @defmac AC_ARG_PROGRAM
23056 @acindex{ARG_PROGRAM}
23057 @ovindex program_transform_name
23058 Place in output variable @code{program_transform_name} a sequence of
23059 @code{sed} commands for changing the names of installed programs.
23061 If any of the options described below are given to @command{configure},
23062 program names are transformed accordingly. Otherwise, if
23063 @code{AC_CANONICAL_TARGET} has been called and a @option{--target} value
23064 is given, the target type followed by a dash is used as a prefix.
23065 Otherwise, no program name transformation is done.
23069 * Transformation Options:: @command{configure} options to transform names
23070 * Transformation Examples:: Sample uses of transforming names
23071 * Transformation Rules:: Makefile uses of transforming names
23074 @node Transformation Options
23075 @subsection Transformation Options
23077 You can specify name transformations by giving @command{configure} these
23078 command line options:
23081 @item --program-prefix=@var{prefix}
23082 prepend @var{prefix} to the names;
23084 @item --program-suffix=@var{suffix}
23085 append @var{suffix} to the names;
23087 @item --program-transform-name=@var{expression}
23088 perform @code{sed} substitution @var{expression} on the names.
23091 @node Transformation Examples
23092 @subsection Transformation Examples
23094 These transformations are useful with programs that can be part of a
23095 cross-compilation development environment. For example, a
23096 cross-assembler running on x86-64 configured with
23097 @option{--target=aarch64-linux-gnu} is normally installed as
23098 @file{aarch64-linux-gnu-as}, rather than @file{as}, which could be confused
23099 with a native x86-64 assembler.
23101 You can force a program name to begin with @file{g}, if you don't want
23102 GNU programs installed on your system to shadow other programs with
23103 the same name. For example, if you configure GNU @code{diff} with
23104 @option{--program-prefix=g}, then when you run @samp{make install} it is
23105 installed as @file{/usr/local/bin/gdiff}.
23107 As a more sophisticated example, you could use
23110 --program-transform-name='s/^/g/; s/^gg/g/; s/^gless/less/'
23114 to prepend @samp{g} to most of the program names in a source tree,
23115 excepting those like @code{gdb} that already have one and those like
23116 @code{less} and @code{lesskey} that aren't GNU programs. (That is
23117 assuming that you have a source tree containing those programs that is
23118 set up to use this feature.)
23120 One way to install multiple versions of some programs simultaneously is
23121 to append a version number to the name of one or both. For example, if
23122 you want to keep Autoconf version 1 around for awhile, you can configure
23123 Autoconf version 2 using @option{--program-suffix=2} to install the
23124 programs as @file{/usr/local/bin/autoconf2},
23125 @file{/usr/local/bin/autoheader2}, etc. Nevertheless, pay attention
23126 that only the binaries are renamed, therefore you'd have problems with
23127 the library files which might overlap.
23129 @node Transformation Rules
23130 @subsection Transformation Rules
23132 Here is how to use the variable @code{program_transform_name} in a
23133 @file{Makefile.in}:
23136 PROGRAMS = cp ls rm
23137 transform = @@program_transform_name@@
23139 for p in $(PROGRAMS); do \
23140 $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p | \
23141 sed '$(transform)'`; \
23145 for p in $(PROGRAMS); do \
23146 rm -f $(DESTDIR)$(bindir)/`echo $$p | sed '$(transform)'`; \
23147 @c $$ restore font-lock
23151 It is guaranteed that @code{program_transform_name} is never empty, and
23152 that there are no useless separators. Therefore you may safely embed
23153 @code{program_transform_name} within a sed program using @samp{;}:
23156 transform = @@program_transform_name@@
23157 transform_exe = s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/
23160 Whether to do the transformations on documentation files (Texinfo or
23161 @code{man}) is a tricky question; there seems to be no perfect answer,
23162 due to the several reasons for name transforming. Documentation is not
23163 usually particular to a specific architecture, and Texinfo files do not
23164 conflict with system documentation. But they might conflict with
23165 earlier versions of the same files, and @code{man} pages sometimes do
23166 conflict with system documentation. As a compromise, it is probably
23167 best to do name transformations on @code{man} pages but not on Texinfo
23170 @node Site Defaults
23171 @section Setting Site Defaults
23172 @cindex Site defaults
23173 @cindex config.site
23175 Autoconf-generated @command{configure} scripts allow your site to provide
23176 default values for some configuration values. You do this by creating
23177 site- and system-wide initialization files.
23179 @evindex CONFIG_SITE
23180 If the environment variable @code{CONFIG_SITE} is set, @command{configure}
23181 uses its value as a space-separated list of shell scripts to read;
23182 it is recommended that these be absolute file names. Otherwise, it
23183 reads the shell script @file{@var{prefix}/share/config.site} if it exists,
23184 then @file{@var{prefix}/etc/config.site} if it exists. Thus,
23185 settings in machine-specific files override those in machine-independent
23186 ones in case of conflict.
23188 Site files can be arbitrary shell scripts, but only certain kinds of
23189 code are really appropriate to be in them. Because @command{configure}
23190 reads any cache file after it has read any site files, a site file can
23191 define a default cache file to be shared between all Autoconf-generated
23192 @command{configure} scripts run on that system (@pxref{Cache Files}). If
23193 you set a default cache file in a site file, it is a good idea to also
23194 set the output variable @code{CC} in that site file, because the cache
23195 file is only valid for a particular compiler, but many systems have
23198 You can examine or override the value set by a command line option to
23199 @command{configure} in a site file; options set shell variables that have
23200 the same names as the options, with any dashes turned into underscores.
23201 The exceptions are that @option{--without-} and @option{--disable-} options
23202 are like giving the corresponding @option{--with-} or @option{--enable-}
23203 option and the value @samp{no}. Thus, @option{--cache-file=localcache}
23204 sets the variable @code{cache_file} to the value @samp{localcache};
23205 @option{--enable-warnings=no} or @option{--disable-warnings} sets the variable
23206 @code{enable_warnings} to the value @samp{no}; @option{--prefix=/usr} sets the
23207 variable @code{prefix} to the value @samp{/usr}; etc.
23209 Site files are also good places to set default values for other output
23210 variables, such as @code{CFLAGS}, if you need to give them non-default
23211 values: anything you would normally do, repetitively, on the command
23212 line. If you use non-default values for @var{prefix} or
23213 @var{exec_prefix} (wherever you locate the site file), you can set them
23214 in the site file if you specify it with the @code{CONFIG_SITE}
23215 environment variable.
23217 You can set some cache values in the site file itself. Doing this is
23218 useful if you are cross-compiling, where it is impossible to check features
23219 that require running a test program. You could ``prime the cache'' by
23220 setting those values correctly for that system in
23221 @file{@var{prefix}/etc/config.site}. To find out the names of the cache
23222 variables you need to set, see the documentation of the respective
23223 Autoconf macro. If the variables or their semantics are undocumented,
23224 you may need to look for shell variables with @samp{_cv_} in their names
23225 in the affected @command{configure} scripts, or in the Autoconf M4
23226 source code for those macros; but in that case, their name or semantics
23227 may change in a future Autoconf version.
23229 The cache file is careful to not override any variables set in the site
23230 files. Similarly, you should not override command-line options in the
23231 site files. Your code should check that variables such as @code{prefix}
23232 and @code{cache_file} have their default values (as set near the top of
23233 @command{configure}) before changing them.
23235 Here is a sample file @file{/usr/share/local/@/gnu/share/@/config.site}. The
23236 command @samp{configure --prefix=/usr/share/local/gnu} would read this
23237 file (if @code{CONFIG_SITE} is not set to a different file).
23240 # /usr/share/local/gnu/share/config.site for configure
23242 # Change some defaults.
23243 test "$prefix" = NONE && prefix=/usr/share/local/gnu
23244 test "$exec_prefix" = NONE && exec_prefix=/usr/local/gnu
23245 test "$sharedstatedir" = '$@{prefix@}/com' && sharedstatedir=/var
23246 test "$localstatedir" = '$@{prefix@}/var' && localstatedir=/var
23247 test "$runstatedir" = '$@{localstatedir@}/run' && runstatedir=/run
23249 # Give Autoconf 2.x generated configure scripts a shared default
23250 # cache file for feature test results, architecture-specific.
23251 if test "$cache_file" = /dev/null; then
23252 cache_file="$prefix/var/config.cache"
23253 # A cache file is only valid for one C compiler.
23258 @c Leave this use of "File system" rendered as one word, but
23259 @c slightly obfuscated so as not to trigger the syntax-check prohibition.
23260 @cindex File@/system Hierarchy Standard
23263 Another use of @file{config.site} is for priming the directory variables
23264 @c "File system", but slightly obfuscated, as above.
23265 in a manner consistent with the File@/system Hierarchy Standard
23266 (FHS). Once the following file is installed at
23267 @file{/usr/share/config.site}, a user can execute simply
23268 @code{./configure --prefix=/usr} to get all the directories chosen in
23269 the locations recommended by FHS.
23272 # /usr/share/config.site for FHS defaults when installing below /usr,
23273 # and the respective settings were not changed on the command line.
23274 if test "$prefix" = /usr; then
23275 test "$sysconfdir" = '$@{prefix@}/etc' && sysconfdir=/etc
23276 test "$sharedstatedir" = '$@{prefix@}/com' && sharedstatedir=/var
23277 test "$localstatedir" = '$@{prefix@}/var' && localstatedir=/var
23281 @cindex @file{lib64}
23282 @cindex 64-bit libraries
23283 Likewise, on platforms where 64-bit libraries are built by default, then
23284 installed in @file{/usr/local/@/lib64} instead of @file{/usr/local/@/lib},
23285 it is appropriate to install @file{/usr/local/@/share/config.site}:
23288 # /usr/local/share/config.site for platforms that prefer
23289 # the directory /usr/local/lib64 over /usr/local/lib.
23290 test "$libdir" = '$@{exec_prefix@}/lib' && libdir='$@{exec_prefix@}/lib64'
23294 @c ============================================== Running configure Scripts.
23296 @node Running configure Scripts
23297 @chapter Running @command{configure} Scripts
23298 @cindex @command{configure}
23300 Below are instructions on how to configure a package that uses a
23301 @command{configure} script, suitable for inclusion as an @file{INSTALL}
23302 file in the package. A plain-text version of @file{INSTALL} which you
23303 may use comes with Autoconf.
23306 * Basic Installation:: Instructions for typical cases
23307 * Compilers and Options:: Selecting compilers and optimization
23308 * Multiple Architectures:: Compiling for multiple architectures at once
23309 * Installation Names:: Installing in different directories
23310 * Optional Features:: Selecting optional features
23311 * System Types:: Specifying a system type
23312 * Sharing Defaults:: Setting site-wide defaults for @command{configure}
23313 * Defining Variables:: Specifying the compiler etc.
23314 * configure Invocation:: Changing how @command{configure} runs
23318 @include install.texi
23321 @c ============================================== config.status Invocation
23323 @node config.status Invocation
23324 @chapter config.status Invocation
23325 @cindex @command{config.status}
23327 The @command{configure} script creates a file named @file{config.status},
23328 which actually configures, @dfn{instantiates}, the template files. It
23329 also records the configuration options that were specified when the
23330 package was last configured in case reconfiguring is needed.
23334 ./config.status @ovar{option}@dots{} @ovar{tag}@dots{}
23337 It configures each @var{tag}; if none are specified, all the templates
23338 are instantiated. A @var{tag} refers to a file or other tag associated
23339 with a configuration action, as specified by an @code{AC_CONFIG_@var{ITEMS}}
23340 macro (@pxref{Configuration Actions}). The files must be specified
23341 without their dependencies, as in
23344 ./config.status foobar
23351 ./config.status foobar:foo.in:bar.in
23354 The supported options are:
23359 Print a summary of the command line options, the list of the template
23364 Print the version number of Autoconf and the configuration settings,
23368 Print the configuration settings in reusable way, quoted for the shell,
23369 and exit. For example, for a debugging build that otherwise reuses the
23370 configuration from a different build directory @var{build-dir} of a
23371 package in @var{src-dir}, you could use the following:
23374 args=`@var{build-dir}/config.status --config`
23375 eval @var{src-dir}/configure "$args" CFLAGS=-g --srcdir=@var{src-dir}
23379 Note that it may be necessary to override a @option{--srcdir} setting
23380 that was saved in the configuration, if the arguments are used in a
23381 different build directory.
23386 Do not print progress messages.
23390 Don't remove the temporary files.
23392 @item --file=@var{file}[:@var{template}]
23393 Require that @var{file} be instantiated as if
23394 @samp{AC_CONFIG_FILES(@var{file}:@var{template})} was used. Both
23395 @var{file} and @var{template} may be @samp{-} in which case the standard
23396 output and/or standard input, respectively, is used. If a
23397 @var{template} file name is relative, it is first looked for in the build
23398 tree, and then in the source tree. @xref{Configuration Actions}, for
23401 This option and the following ones provide one way for separately
23402 distributed packages to share the values computed by @command{configure}.
23403 Doing so can be useful if some of the packages need a superset of the
23404 features that one of them, perhaps a common library, does. These
23405 options allow a @file{config.status} file to create files other than the
23406 ones that its @file{configure.ac} specifies, so it can be used for a
23407 different package, or for extracting a subset of values. For example,
23410 echo '@@CC@@' | ./config.status --file=-
23414 provides the value of @code{@@CC@@} on standard output.
23416 @item --header=@var{file}[:@var{template}]
23417 Same as @option{--file} above, but with @samp{AC_CONFIG_HEADERS}.
23420 Ask @file{config.status} to update itself and exit (no instantiation).
23421 This option is useful if you change @command{configure}, so that the
23422 results of some tests might be different from the previous run. The
23423 @option{--recheck} option reruns @command{configure} with the same arguments
23424 you used before, plus the @option{--no-create} option, which prevents
23425 @command{configure} from running @file{config.status} and creating
23426 @file{Makefile} and other files, and the @option{--no-recursion} option,
23427 which prevents @command{configure} from running other @command{configure}
23428 scripts in subdirectories. (This is so other Make rules can
23429 run @file{config.status} when it changes; @pxref{Automatic Remaking},
23433 @file{config.status} checks several optional environment variables that
23434 can alter its behavior:
23436 @anchor{CONFIG_SHELL}
23437 @defvar CONFIG_SHELL
23438 @evindex CONFIG_SHELL
23439 The shell with which to run @command{configure}. It must be
23440 Bourne-compatible, and the absolute name of the shell should be passed.
23441 The default is a shell that supports @code{LINENO} if available, and
23442 @file{/bin/sh} otherwise.
23445 @defvar CONFIG_STATUS
23446 @evindex CONFIG_STATUS
23447 The file name to use for the shell script that records the
23448 configuration. The default is @file{./config.status}. This variable is
23449 useful when one package uses parts of another and the @command{configure}
23450 scripts shouldn't be merged because they are maintained separately.
23453 You can use @file{./config.status} in your makefiles. For example, in
23454 the dependencies given above (@pxref{Automatic Remaking}),
23455 @file{config.status} is run twice when @file{configure.ac} has changed.
23456 If that bothers you, you can make each run only regenerate the files for
23461 stamp-h: config.h.in config.status
23462 ./config.status config.h
23465 Makefile: Makefile.in config.status
23466 ./config.status Makefile
23470 The calling convention of @file{config.status} has changed; see
23471 @ref{Obsolete config.status Use}, for details.
23474 @c =================================================== Obsolete Constructs
23476 @node Obsolete Constructs
23477 @chapter Obsolete Constructs
23478 @cindex Obsolete constructs
23480 Autoconf changes, and throughout the years some constructs have been
23481 obsoleted. Most of the changes involve the macros, but in some cases
23482 the tools themselves, or even some concepts, are now considered
23485 You may completely skip this chapter if you are new to Autoconf. Its
23486 intention is mainly to help maintainers updating their packages by
23487 understanding how to move to more modern constructs.
23490 * Obsolete config.status Use:: Obsolete convention for @command{config.status}
23491 * acconfig Header:: Additional entries in @file{config.h.in}
23492 * autoupdate Invocation:: Automatic update of @file{configure.ac}
23493 * Obsolete Macros:: Backward compatibility macros
23494 * Autoconf 1:: Tips for upgrading your files
23495 * Autoconf 2.13:: Some fresher tips
23498 @node Obsolete config.status Use
23499 @section Obsolete @file{config.status} Invocation
23501 @file{config.status} now supports arguments to specify the files to
23502 instantiate; see @ref{config.status Invocation}, for more details.
23503 Before, environment variables had to be used.
23505 @defvar CONFIG_COMMANDS
23506 @evindex CONFIG_COMMANDS
23507 The tags of the commands to execute. The default is the arguments given
23508 to @code{AC_OUTPUT} and @code{AC_CONFIG_COMMANDS} in
23509 @file{configure.ac}.
23512 @defvar CONFIG_FILES
23513 @evindex CONFIG_FILES
23514 The files in which to perform @samp{@@@var{variable}@@} substitutions.
23515 The default is the arguments given to @code{AC_OUTPUT} and
23516 @code{AC_CONFIG_FILES} in @file{configure.ac}.
23519 @defvar CONFIG_HEADERS
23520 @evindex CONFIG_HEADERS
23521 The files in which to substitute C @code{#define} statements. The
23522 default is the arguments given to @code{AC_CONFIG_HEADERS}; if that
23523 macro was not called, @file{config.status} ignores this variable.
23526 @defvar CONFIG_LINKS
23527 @evindex CONFIG_LINKS
23528 The symbolic links to establish. The default is the arguments given to
23529 @code{AC_CONFIG_LINKS}; if that macro was not called,
23530 @file{config.status} ignores this variable.
23533 In @ref{config.status Invocation}, using this old interface, the example
23539 stamp-h: config.h.in config.status
23540 CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_FILES= \
23541 CONFIG_HEADERS=config.h ./config.status
23544 Makefile: Makefile.in config.status
23545 CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_HEADERS= \
23546 CONFIG_FILES=Makefile ./config.status
23551 (If @file{configure.ac} does not call @code{AC_CONFIG_HEADERS}, there is
23552 no need to set @code{CONFIG_HEADERS} in the @command{make} rules. Equally
23553 for @code{CONFIG_COMMANDS}, etc.)
23556 @node acconfig Header
23557 @section @file{acconfig.h}
23559 @cindex @file{acconfig.h}
23560 @cindex @file{config.h.top}
23561 @cindex @file{config.h.bot}
23563 In order to produce @file{config.h.in}, @command{autoheader} needs to
23564 build or to find templates for each symbol. Modern releases of Autoconf
23565 use @code{AH_VERBATIM} and @code{AH_TEMPLATE} (@pxref{Autoheader
23566 Macros}), but in older releases a file, @file{acconfig.h}, contained the
23567 list of needed templates. @command{autoheader} copied comments and
23568 @code{#define} and @code{#undef} statements from @file{acconfig.h} in
23569 the current directory, if present. This file used to be mandatory if
23570 you @code{AC_DEFINE} any additional symbols.
23572 Modern releases of Autoconf also provide @code{AH_TOP} and
23573 @code{AH_BOTTOM} if you need to prepend/append some information to
23574 @file{config.h.in}. Ancient versions of Autoconf had a similar feature:
23575 if @file{./acconfig.h} contains the string @samp{@@TOP@@},
23576 @command{autoheader} copies the lines before the line containing
23577 @samp{@@TOP@@} into the top of the file that it generates. Similarly,
23578 if @file{./acconfig.h} contains the string @samp{@@BOTTOM@@},
23579 @command{autoheader} copies the lines after that line to the end of the
23580 file it generates. Either or both of those strings may be omitted. An
23581 even older alternate way to produce the same effect in ancient versions
23582 of Autoconf is to create the files @file{@var{file}.top} (typically
23583 @file{config.h.top}) and/or @file{@var{file}.bot} in the current
23584 directory. If they exist, @command{autoheader} copies them to the
23585 beginning and end, respectively, of its output.
23587 In former versions of Autoconf, the files used in preparing a software
23588 package for distribution were:
23591 configure.ac --. .------> autoconf* -----> configure
23593 [aclocal.m4] --+ `---.
23595 +--> [autoheader*] -> [config.h.in]
23596 [acconfig.h] ----. |
23603 Using only the @code{AH_} macros, @file{configure.ac} should be
23604 self-contained, and should not depend upon @file{acconfig.h} etc.
23607 @node autoupdate Invocation
23608 @section Using @command{autoupdate} to Modernize @file{configure.ac}
23609 @cindex @command{autoupdate}
23611 The @command{autoupdate} program updates a @file{configure.ac} file that
23612 calls Autoconf macros by their old names to use the current macro names.
23613 In version 2 of Autoconf, most of the macros were renamed to use a more
23614 uniform and descriptive naming scheme. @xref{Macro Names}, for a
23615 description of the new scheme. Although the old names still work
23616 (@pxref{Obsolete Macros}, for a list of the old macros and the corresponding
23617 new names), you can make your @file{configure.ac} files more readable
23618 and make it easier to use the current Autoconf documentation if you
23619 update them to use the new macro names.
23621 @evindex SIMPLE_BACKUP_SUFFIX
23622 If given no arguments, @command{autoupdate} updates @file{configure.ac},
23623 backing up the original version with the suffix @file{~} (or the value
23624 of the environment variable @code{SIMPLE_BACKUP_SUFFIX}, if that is
23625 set). If you give @command{autoupdate} an argument, it reads that file
23626 instead of @file{configure.ac} and writes the updated file to the
23630 @command{autoupdate} accepts the following options:
23635 Print a summary of the command line options and exit.
23639 Print the version number of Autoconf and exit.
23643 Report processing steps.
23647 Don't remove the temporary files.
23651 Force the update even if the file has not changed. Disregard the cache.
23653 @item --include=@var{dir}
23654 @itemx -I @var{dir}
23655 Also look for input files in @var{dir}. Multiple invocations accumulate.
23656 Directories are browsed from last to first.
23658 @item --prepend-include=@var{dir}
23659 @itemx -B @var{dir}
23660 Prepend directory @var{dir} to the search path. This is used to include
23661 the language-specific files before any third-party macros.
23664 @node Obsolete Macros
23665 @section Obsolete Macros
23667 Several macros are obsoleted in Autoconf, for various reasons (typically
23668 they failed to quote properly, couldn't be extended for more recent
23669 issues, etc.). They are still supported, but deprecated: their use
23672 During the jump from Autoconf version 1 to version 2, most of the
23673 macros were renamed to use a more uniform and descriptive naming scheme,
23674 but their signature did not change. @xref{Macro Names}, for a
23675 description of the new naming scheme. Below, if there is just the mapping
23676 from old names to new names for these macros, the reader is invited to
23677 refer to the definition of the new macro for the signature and the
23682 @cvindex _ALL_SOURCE
23683 This macro is a platform-specific subset of
23684 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
23689 Replaced by @code{AC_FUNC_ALLOCA} (@pxref{AC_FUNC_ALLOCA}).
23692 @defmac AC_ARG_ARRAY
23693 @acindex{ARG_ARRAY}
23694 Removed because of limited usefulness.
23699 This macro is obsolete; it does nothing.
23702 @defmac AC_C_LONG_DOUBLE
23703 @acindex{C_LONG_DOUBLE}
23704 @cvindex HAVE_LONG_DOUBLE
23705 If the C compiler supports a working @code{long double} type with more
23706 range or precision than the @code{double} type, define
23707 @code{HAVE_LONG_DOUBLE}.
23709 You should use @code{AC_TYPE_LONG_DOUBLE} or
23710 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead. @xref{Particular Types}.
23713 @defmac AC_CANONICAL_SYSTEM
23714 @acindex{CANONICAL_SYSTEM}
23715 Determine the system type and set output variables to the names of the
23716 canonical system types. @xref{Canonicalizing}, for details about the
23717 variables this macro sets.
23719 The user is encouraged to use either @code{AC_CANONICAL_BUILD}, or
23720 @code{AC_CANONICAL_HOST}, or @code{AC_CANONICAL_TARGET}, depending on
23721 the needs. Using @code{AC_CANONICAL_TARGET} is enough to run the two
23722 other macros (@pxref{Canonicalizing}).
23725 @defmac AC_CHAR_UNSIGNED
23726 @acindex{CHAR_UNSIGNED}
23727 Replaced by @code{AC_C_CHAR_UNSIGNED} (@pxref{AC_C_CHAR_UNSIGNED}).
23730 @defmac AC_CHECK_TYPE (@var{type}, @var{default})
23731 @acindex{CHECK_TYPE}
23732 Autoconf, up to 2.13, used to provide this version of
23733 @code{AC_CHECK_TYPE}, deprecated because of its flaws. First, although
23734 it is a member of the @code{CHECK} clan, it does
23735 more than just checking. Secondly, missing types are defined
23736 using @code{#define}, not @code{typedef}, and this can lead to
23737 problems in the case of pointer types.
23739 This use of @code{AC_CHECK_TYPE} is obsolete and discouraged; see
23740 @ref{Generic Types}, for the description of the current macro.
23742 If the type @var{type} is not defined, define it to be the C (or C++)
23743 builtin type @var{default}, e.g., @samp{short int} or @samp{unsigned int}.
23745 This macro is equivalent to:
23748 AC_CHECK_TYPE([@var{type}], [],
23749 [AC_DEFINE_UNQUOTED([@var{type}], [@var{default}],
23750 [Define to '@var{default}'
23751 if <sys/types.h> does not define.])])
23754 In order to keep backward compatibility, the two versions of
23755 @code{AC_CHECK_TYPE} are implemented, selected using these heuristics:
23759 If there are three or four arguments, the modern version is used.
23762 If the second argument appears to be a C or C++ type, then the
23763 obsolete version is used. This happens if the argument is a C or C++
23764 @emph{builtin} type or a C identifier ending in @samp{_t}, optionally
23765 followed by one of @samp{[(* } and then by a string of zero or more
23766 characters taken from the set @samp{[]()* _a-zA-Z0-9}.
23769 If the second argument is spelled with the alphabet of valid C and C++
23770 types, the user is warned and the modern version is used.
23773 Otherwise, the modern version is used.
23777 You are encouraged either to use a valid builtin type, or to use the
23778 equivalent modern code (see above), or better yet, to use
23779 @code{AC_CHECK_TYPES} together with
23782 #ifndef HAVE_LOFF_T
23783 typedef loff_t off_t;
23787 @c end of AC_CHECK_TYPE
23789 @defmac AC_CHECKING (@var{feature-description})
23794 AC_MSG_NOTICE([checking @var{feature-description}@dots{}]
23798 @xref{AC_MSG_NOTICE}.
23801 @defmac AC_COMPILE_CHECK (@var{echo-text}, @var{includes}, @
23802 @var{function-body}, @var{action-if-true}, @ovar{action-if-false})
23803 @acindex{COMPILE_CHECK}
23804 This is an obsolete version of @code{AC_TRY_COMPILE} itself replaced by
23805 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}), with the
23806 addition that it prints @samp{checking for @var{echo-text}} to the
23807 standard output first, if @var{echo-text} is non-empty. Use
23808 @code{AC_MSG_CHECKING} and @code{AC_MSG_RESULT} instead to print
23809 messages (@pxref{Printing Messages}).
23814 Replaced by @code{AC_C_CONST} (@pxref{AC_C_CONST}).
23817 @defmac AC_CROSS_CHECK
23818 @acindex{CROSS_CHECK}
23819 Same as @code{AC_C_CROSS}, which is obsolete too, and does nothing
23826 Check for the Cygwin environment in which case the shell variable
23827 @code{CYGWIN} is set to @samp{yes}. Don't use this macro, the dignified
23828 means to check the nature of the host is using @code{AC_CANONICAL_HOST}
23829 (@pxref{Canonicalizing}). As a matter of fact this macro is defined as:
23832 AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
23834 *cygwin* ) CYGWIN=yes;;
23839 Beware that the variable @env{CYGWIN} has a special meaning when
23840 running Cygwin, and should not be changed. That's yet another reason
23841 not to use this macro.
23844 @defmac AC_DECL_SYS_SIGLIST
23845 @acindex{DECL_SYS_SIGLIST}
23846 @cvindex SYS_SIGLIST_DECLARED
23850 AC_CHECK_DECLS([sys_siglist], [], [],
23851 [#include <signal.h>
23852 /* NetBSD declares sys_siglist in unistd.h. */
23853 #ifdef HAVE_UNISTD_H
23854 # include <unistd.h>
23860 @xref{AC_CHECK_DECLS}.
23863 @defmac AC_DECL_YYTEXT
23864 @acindex{DECL_YYTEXT}
23865 Does nothing, now integrated in @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
23868 @defmac AC_DIAGNOSE (@var{category}, @var{message})
23870 Replaced by @code{m4_warn} (@pxref{m4_warn}).
23873 @defmac AC_DIR_HEADER
23874 @acindex{DIR_HEADER}
23879 Like calling @code{AC_FUNC_CLOSEDIR_VOID}
23880 (@pxref{AC_FUNC_CLOSEDIR_VOID}) and @code{AC_HEADER_DIRENT}
23881 (@pxref{AC_HEADER_DIRENT}),
23882 but defines a different set of C preprocessor macros to indicate which
23883 header file is found:
23885 @multitable {@file{sys/ndir.h}} {Old Symbol} {@code{HAVE_SYS_NDIR_H}}
23886 @item Header @tab Old Symbol @tab New Symbol
23887 @item @file{dirent.h} @tab @code{DIRENT} @tab @code{HAVE_DIRENT_H}
23888 @item @file{sys/ndir.h} @tab @code{SYSNDIR} @tab @code{HAVE_SYS_NDIR_H}
23889 @item @file{sys/dir.h} @tab @code{SYSDIR} @tab @code{HAVE_SYS_DIR_H}
23890 @item @file{ndir.h} @tab @code{NDIR} @tab @code{HAVE_NDIR_H}
23894 @defmac AC_DYNIX_SEQ
23895 @acindex{DYNIX_SEQ}
23896 If on DYNIX/ptx, add @option{-lseq} to output variable
23897 @code{LIBS}. This macro used to be defined as
23900 AC_CHECK_LIB([seq], [getmntent], [LIBS="-lseq $LIBS"])
23904 now it is just @code{AC_FUNC_GETMNTENT} (@pxref{AC_FUNC_GETMNTENT}).
23910 Defined the output variable @code{EXEEXT} based on the output of the
23911 compiler, which is now done automatically. Typically set to empty
23912 string if Posix and @samp{.exe} if a DOS variant.
23917 Similar to @code{AC_CYGWIN} but checks for the EMX environment on OS/2
23918 and sets @code{EMXOS2}. Don't use this macro, the dignified means to
23919 check the nature of the host is using @code{AC_CANONICAL_HOST}
23920 (@pxref{Canonicalizing}).
23923 @defmac AC_ENABLE (@var{feature}, @var{action-if-given}, @
23924 @ovar{action-if-not-given})
23926 This is an obsolete version of @code{AC_ARG_ENABLE} that does not
23927 support providing a help string (@pxref{AC_ARG_ENABLE}).
23932 Replaced by @code{AC_MSG_ERROR} (@pxref{AC_MSG_ERROR}).
23935 @defmac AC_FATAL (@var{message})
23937 Replaced by @code{m4_fatal} (@pxref{m4_fatal}).
23942 Replaced by @code{AC_PATH_X} (@pxref{AC_PATH_X}).
23945 @defmac AC_FIND_XTRA
23946 @acindex{FIND_XTRA}
23947 Replaced by @code{AC_PATH_XTRA} (@pxref{AC_PATH_XTRA}).
23952 Replaced by @code{m4_foreach_w} (@pxref{m4_foreach_w}).
23955 @defmac AC_FUNC_CHECK
23956 @acindex{FUNC_CHECK}
23957 Replaced by @code{AC_CHECK_FUNC} (@pxref{AC_CHECK_FUNC}).
23960 @anchor{AC_FUNC_SETVBUF_REVERSED}
23961 @defmac AC_FUNC_SETVBUF_REVERSED
23962 @acindex{FUNC_SETVBUF_REVERSED}
23963 @cvindex SETVBUF_REVERSED
23964 @c @fuindex setvbuf
23965 @prindex @code{setvbuf}
23966 Do nothing. Formerly, this macro checked whether @code{setvbuf} takes
23967 the buffering type as its second argument and the buffer pointer as the
23968 third, instead of the other way around, and defined
23969 @code{SETVBUF_REVERSED}. However, the last systems to have the problem
23970 were those based on SVR2, which became obsolete in 1987, and the macro
23971 is no longer needed.
23974 @defmac AC_FUNC_WAIT3
23975 @acindex{FUNC_WAIT3}
23976 @cvindex HAVE_WAIT3
23978 @prindex @code{wait3}
23979 If @code{wait3} is found and fills in the contents of its third argument
23980 (a @samp{struct rusage *}), which HP-UX does not do, define
23983 These days portable programs should use @code{waitpid}, not
23984 @code{wait3}, as @code{wait3} has been removed from Posix.
23987 @defmac AC_GCC_TRADITIONAL
23988 @acindex{GCC_TRADITIONAL}
23989 Replaced by @code{AC_PROG_GCC_TRADITIONAL} (@pxref{AC_PROG_GCC_TRADITIONAL}),
23990 which is itself obsolete.
23993 @defmac AC_GETGROUPS_T
23994 @acindex{GETGROUPS_T}
23995 Replaced by @code{AC_TYPE_GETGROUPS} (@pxref{AC_TYPE_GETGROUPS}).
23998 @defmac AC_GETLOADAVG
23999 @acindex{GETLOADAVG}
24000 Replaced by @code{AC_FUNC_GETLOADAVG} (@pxref{AC_FUNC_GETLOADAVG}).
24003 @defmac AC_GNU_SOURCE
24004 @acindex{GNU_SOURCE}
24005 @cvindex _GNU_SOURCE
24006 This macro is a platform-specific subset of
24007 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
24010 @defmac AC_HAVE_FUNCS
24011 @acindex{HAVE_FUNCS}
24012 Replaced by @code{AC_CHECK_FUNCS} (@pxref{AC_CHECK_FUNCS}).
24015 @defmac AC_HAVE_HEADERS
24016 @acindex{HAVE_HEADERS}
24017 Replaced by @code{AC_CHECK_HEADERS} (@pxref{AC_CHECK_HEADERS}).
24020 @defmac AC_HAVE_LIBRARY (@var{library}, @ovar{action-if-found}, @
24021 @ovar{action-if-not-found}, @ovar{other-libraries})
24022 @acindex{HAVE_LIBRARY}
24023 This macro is equivalent to calling @code{AC_CHECK_LIB} with a
24024 @var{function} argument of @code{main}. In addition, @var{library} can
24025 be written as any of @samp{foo}, @option{-lfoo}, or @samp{libfoo.a}. In
24026 all of those cases, the compiler is passed @option{-lfoo}. However,
24027 @var{library} cannot be a shell variable; it must be a literal name.
24028 @xref{AC_CHECK_LIB}.
24031 @defmac AC_HAVE_POUNDBANG
24032 @acindex{HAVE_POUNDBANG}
24033 Replaced by @code{AC_SYS_INTERPRETER} (@pxref{AC_SYS_INTERPRETER}).
24036 @defmac AC_HEADER_CHECK
24037 @acindex{HEADER_CHECK}
24038 Replaced by @code{AC_CHECK_HEADER} (@pxref{AC_CHECK_HEADER}).
24041 @defmac AC_HEADER_EGREP
24042 @acindex{HEADER_EGREP}
24043 Replaced by @code{AC_EGREP_HEADER} (@pxref{AC_EGREP_HEADER}).
24046 @anchor{AC_HEADER_TIME}
24047 @defmac AC_HEADER_TIME
24048 @acindex{HEADER_TIME}
24049 @cvindex TIME_WITH_SYS_TIME
24051 @hdrindex{sys/time.h}
24052 @caindex header_time
24053 This macro used to check whether it was possible to include
24054 @file{time.h} and @file{sys/time.h} in the same source file,
24055 defining @code{TIME_WITH_SYS_TIME} if so.
24057 Nowadays, it is equivalent to @samp{AC_CHECK_HEADERS([sys/time.h])},
24058 although it does still define @code{TIME_WITH_SYS_TIME} for
24059 compatibility's sake. @file{time.h} is universally present, and the
24060 systems on which @file{sys/time.h} conflicted with @file{time.h} are
24064 @defmac AC_HELP_STRING
24065 @acindex{HELP_STRING}
24066 Replaced by @code{AS_HELP_STRING} (@pxref{AS_HELP_STRING}).
24069 @defmac AC_INIT (@var{unique-file-in-source-dir})
24071 Formerly @code{AC_INIT} used to have a single argument, and was
24076 AC_CONFIG_SRCDIR(@var{unique-file-in-source-dir})
24078 See @ref{AC_INIT} and @ref{AC_CONFIG_SRCDIR}.
24083 Replaced by @code{AC_C_INLINE} (@pxref{AC_C_INLINE}).
24086 @defmac AC_INT_16_BITS
24087 @acindex{INT_16_BITS}
24088 @cvindex INT_16_BITS
24089 If the C type @code{int} is 16 bits wide, define @code{INT_16_BITS}.
24090 Use @samp{AC_CHECK_SIZEOF(int)} instead (@pxref{AC_CHECK_SIZEOF}).
24093 @defmac AC_IRIX_SUN
24095 If on IRIX (Silicon Graphics Unix), add @option{-lsun} to output
24096 @code{LIBS}. If you were using it to get @code{getmntent}, use
24097 @code{AC_FUNC_GETMNTENT} instead. If you used it for the NIS versions
24098 of the password and group functions, use @samp{AC_CHECK_LIB(sun,
24099 getpwnam)}. Up to Autoconf 2.13, it used to be
24102 AC_CHECK_LIB([sun], [getmntent], [LIBS="-lsun $LIBS"])
24106 now it is defined as
24110 AC_CHECK_LIB([sun], [getpwnam])
24114 See @ref{AC_FUNC_GETMNTENT} and @ref{AC_CHECK_LIB}.
24117 @defmac AC_ISC_POSIX
24118 @acindex{ISC_POSIX}
24120 This macro adds @option{-lcposix} to output variable @code{LIBS} if
24121 necessary for Posix facilities. Sun dropped support for the obsolete
24122 INTERACTIVE Systems Corporation Unix on 2006-07-23. New programs
24123 need not use this macro. It is implemented as
24124 @code{AC_SEARCH_LIBS([strerror], [cposix])} (@pxref{AC_SEARCH_LIBS}).
24129 Same as @samp{AC_LANG([C])} (@pxref{AC_LANG}).
24132 @defmac AC_LANG_CPLUSPLUS
24133 @acindex{LANG_CPLUSPLUS}
24134 Same as @samp{AC_LANG([C++])} (@pxref{AC_LANG}).
24137 @defmac AC_LANG_FORTRAN77
24138 @acindex{LANG_FORTRAN77}
24139 Same as @samp{AC_LANG([Fortran 77])} (@pxref{AC_LANG}).
24142 @defmac AC_LANG_RESTORE
24143 @acindex{LANG_RESTORE}
24144 Select the @var{language} that is saved on the top of the stack, as set
24145 by @code{AC_LANG_SAVE}, remove it from the stack, and call
24146 @code{AC_LANG(@var{language})}. @xref{Language Choice}, for the
24147 preferred way to change languages.
24150 @defmac AC_LANG_SAVE
24151 @acindex{LANG_SAVE}
24152 Remember the current language (as set by @code{AC_LANG}) on a stack.
24153 The current language does not change. @code{AC_LANG_PUSH} is preferred
24154 (@pxref{AC_LANG_PUSH}).
24157 @defmac AC_LINK_FILES (@var{source}@dots{}, @var{dest}@dots{})
24158 @acindex{LINK_FILES}
24159 This is an obsolete version of @code{AC_CONFIG_LINKS}
24160 (@pxref{AC_CONFIG_LINKS}. An updated version of:
24163 AC_LINK_FILES(config/$machine.h config/$obj_format.h,
24171 AC_CONFIG_LINKS([host.h:config/$machine.h
24172 object.h:config/$obj_format.h])
24178 Replaced by @code{AC_PROG_LN_S} (@pxref{AC_PROG_LN_S}).
24181 @defmac AC_LONG_64_BITS
24182 @acindex{LONG_64_BITS}
24183 @cvindex LONG_64_BITS
24184 Define @code{LONG_64_BITS} if the C type @code{long int} is 64 bits wide.
24185 Use the generic macro @samp{AC_CHECK_SIZEOF([long int])} instead
24186 (@pxref{AC_CHECK_SIZEOF}).
24189 @defmac AC_LONG_DOUBLE
24190 @acindex{LONG_DOUBLE}
24191 If the C compiler supports a working @code{long double} type with more
24192 range or precision than the @code{double} type, define
24193 @code{HAVE_LONG_DOUBLE}.
24195 You should use @code{AC_TYPE_LONG_DOUBLE} or
24196 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead. @xref{Particular Types}.
24199 @defmac AC_LONG_FILE_NAMES
24200 @acindex{LONG_FILE_NAMES}
24203 AC_SYS_LONG_FILE_NAMES
24206 @xref{AC_SYS_LONG_FILE_NAMES}.
24209 @defmac AC_MAJOR_HEADER
24210 @acindex{MAJOR_HEADER}
24211 Replaced by @code{AC_HEADER_MAJOR} (@pxref{AC_HEADER_MAJOR}).
24214 @defmac AC_MEMORY_H
24216 @cvindex NEED_MEMORY_H
24217 Used to define @code{NEED_MEMORY_H} if the @code{mem} functions were
24218 defined in @file{memory.h}. Today it is equivalent to
24219 @samp{AC_CHECK_HEADERS([memory.h])} (@pxref{AC_CHECK_HEADERS}). Adjust
24220 your code to get the @code{mem} functions from @file{string.h} instead.
24225 Similar to @code{AC_CYGWIN} but checks for the MinGW compiler
24226 environment and sets @code{MINGW32}. Don't use this macro, the
24227 dignified means to check the nature of the host is using
24228 @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
24234 @cvindex _POSIX_SOURCE
24235 @cvindex _POSIX_1_SOURCE
24236 This macro is a platform-specific subset of
24237 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
24240 @defmac AC_MINUS_C_MINUS_O
24241 @acindex{MINUS_C_MINUS_O}
24242 Replaced by @code{AC_PROG_CC_C_O} (@pxref{AC_PROG_CC_C_O}).
24247 Replaced by @code{AC_FUNC_MMAP} (@pxref{AC_FUNC_MMAP}).
24252 Replaced by @code{AC_TYPE_MODE_T} (@pxref{AC_TYPE_MODE_T}).
24258 Defined the output variable @code{OBJEXT} based on the output of the
24259 compiler, after .c files have been excluded. Typically set to @samp{o}
24260 if Posix, @samp{obj} if a DOS variant.
24261 Now the compiler checking macros handle
24262 this automatically.
24265 @defmac AC_OBSOLETE (@var{this-macro-name}, @ovar{suggestion})
24267 Make M4 print a message to the standard error output warning that
24268 @var{this-macro-name} is obsolete, and giving the file and line number
24269 where it was called. @var{this-macro-name} should be the name of the
24270 macro that is calling @code{AC_OBSOLETE}. If @var{suggestion} is given,
24271 it is printed at the end of the warning message; for example, it can be
24272 a suggestion for what to use instead of @var{this-macro-name}.
24277 AC_OBSOLETE([$0], [; use AC_CHECK_HEADERS(unistd.h) instead])dnl
24281 You are encouraged to use @code{AU_DEFUN} instead, since it gives better
24282 services to the user (@pxref{AU_DEFUN}).
24287 Replaced by @code{AC_TYPE_OFF_T} (@pxref{AC_TYPE_OFF_T}).
24290 @defmac AC_OUTPUT (@ovar{file}@dots{}, @ovar{extra-cmds}, @ovar{init-cmds})
24292 The use of @code{AC_OUTPUT} with arguments is deprecated. This obsoleted
24293 interface is equivalent to:
24297 AC_CONFIG_FILES(@var{file}@dots{})
24298 AC_CONFIG_COMMANDS([default],
24299 @var{extra-cmds}, @var{init-cmds})
24305 See @ref{AC_CONFIG_FILES}, @ref{AC_CONFIG_COMMANDS}, and @ref{AC_OUTPUT}.
24308 @defmac AC_OUTPUT_COMMANDS (@var{extra-cmds}, @ovar{init-cmds})
24309 @acindex{OUTPUT_COMMANDS}
24310 Specify additional shell commands to run at the end of
24311 @file{config.status}, and shell commands to initialize any variables
24312 from @command{configure}. This macro may be called multiple times. It is
24313 obsolete, replaced by @code{AC_CONFIG_COMMANDS} (@pxref{AC_CONFIG_COMMANDS}).
24315 Here is an unrealistic example:
24319 AC_OUTPUT_COMMANDS([echo this is extra $fubar, and so on.],
24321 AC_OUTPUT_COMMANDS([echo this is another, extra, bit],
24325 Aside from the fact that @code{AC_CONFIG_COMMANDS} requires an
24326 additional key, an important difference is that
24327 @code{AC_OUTPUT_COMMANDS} is quoting its arguments twice, unlike
24328 @code{AC_CONFIG_COMMANDS}. This means that @code{AC_CONFIG_COMMANDS}
24329 can safely be given macro calls as arguments:
24332 AC_CONFIG_COMMANDS(foo, [my_FOO()])
24336 Conversely, where one level of quoting was enough for literal strings
24337 with @code{AC_OUTPUT_COMMANDS}, you need two with
24338 @code{AC_CONFIG_COMMANDS}. The following lines are equivalent:
24342 AC_OUTPUT_COMMANDS([echo "Square brackets: []"])
24343 AC_CONFIG_COMMANDS([default], [[echo "Square brackets: []"]])
24350 Replaced by @code{AC_TYPE_PID_T} (@pxref{AC_TYPE_PID_T}).
24355 Replaced by @code{AC_PREFIX_PROGRAM} (@pxref{AC_PREFIX_PROGRAM}).
24358 @defmac AC_PROG_CC_C89
24359 @acindex{PROG_CC_C89}
24360 Now done by @code{AC_PROG_CC} (@pxref{AC_PROG_CC}).
24363 @defmac AC_PROG_CC_C99
24364 @acindex{PROG_CC_C99}
24365 Now done by @code{AC_PROG_CC} (@pxref{AC_PROG_CC}).
24368 @defmac AC_PROG_CC_STDC
24369 @acindex{PROG_CC_STDC}
24370 Now done by @code{AC_PROG_CC} (@pxref{AC_PROG_CC}).
24373 @anchor{AC_PROG_GCC_TRADITIONAL}
24374 @defmac AC_PROG_GCC_TRADITIONAL
24375 @acindex{PROG_GCC_TRADITIONAL}
24376 Used to put GCC into ``traditional'' (pre-ISO C) compilation mode,
24377 on systems with headers that did not work correctly with a
24378 standard-compliant compiler. GCC has not supported traditional
24379 compilation in many years, and all of the systems that required this are
24380 long obsolete themselves. This macro is now a compatibility synonym for
24381 @code{AC_PROG_CC} (@pxref{AC_PROG_CC}).
24385 @defmac AC_PROGRAMS_CHECK
24386 @acindex{PROGRAMS_CHECK}
24387 Replaced by @code{AC_CHECK_PROGS} (@pxref{AC_CHECK_PROGS}).
24390 @defmac AC_PROGRAMS_PATH
24391 @acindex{PROGRAMS_PATH}
24392 Replaced by @code{AC_PATH_PROGS} (@pxref{AC_PATH_PROGS}).
24395 @defmac AC_PROGRAM_CHECK
24396 @acindex{PROGRAM_CHECK}
24397 Replaced by @code{AC_CHECK_PROG} (@pxref{AC_CHECK_PROG}).
24400 @defmac AC_PROGRAM_EGREP
24401 @acindex{PROGRAM_EGREP}
24402 Replaced by @code{AC_EGREP_CPP} (@pxref{AC_EGREP_CPP}).
24405 @defmac AC_PROGRAM_PATH
24406 @acindex{PROGRAM_PATH}
24407 Replaced by @code{AC_PATH_PROG} (@pxref{AC_PATH_PROG}).
24410 @defmac AC_REMOTE_TAPE
24411 @acindex{REMOTE_TAPE}
24412 Removed because of limited usefulness.
24415 @defmac AC_RESTARTABLE_SYSCALLS
24416 @acindex{RESTARTABLE_SYSCALLS}
24417 This macro was renamed @code{AC_SYS_RESTARTABLE_SYSCALLS}. However,
24418 these days portable programs should use @code{sigaction} with
24419 @code{SA_RESTART} if they want restartable system calls. They should
24420 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
24421 system call is restartable is a dynamic issue, not a configuration-time
24425 @defmac AC_RETSIGTYPE
24426 @acindex{RETSIGTYPE}
24427 Replaced by @code{AC_TYPE_SIGNAL} (@pxref{AC_TYPE_SIGNAL}), which itself
24433 Removed because of limited usefulness.
24436 @defmac AC_SCO_INTL
24439 If on SCO Unix, add @option{-lintl} to output variable @code{LIBS}. This
24440 macro used to do this:
24443 AC_CHECK_LIB([intl], [strftime], [LIBS="-lintl $LIBS"])
24447 Now it just calls @code{AC_FUNC_STRFTIME} instead (@pxref{AC_FUNC_STRFTIME}).
24450 @defmac AC_SETVBUF_REVERSED
24451 @acindex{SETVBUF_REVERSED}
24454 AC_FUNC_SETVBUF_REVERSED
24457 @xref{AC_FUNC_SETVBUF_REVERSED}.
24460 @defmac AC_SET_MAKE
24462 Replaced by @code{AC_PROG_MAKE_SET} (@pxref{AC_PROG_MAKE_SET}).
24465 @defmac AC_SIZEOF_TYPE
24466 @acindex{SIZEOF_TYPE}
24467 Replaced by @code{AC_CHECK_SIZEOF} (@pxref{AC_CHECK_SIZEOF}).
24472 Replaced by @code{AC_TYPE_SIZE_T} (@pxref{AC_TYPE_SIZE_T}).
24475 @defmac AC_STAT_MACROS_BROKEN
24476 @acindex{STAT_MACROS_BROKEN}
24477 Replaced by @code{AC_HEADER_STAT} (@pxref{AC_HEADER_STAT}).
24480 @defmac AC_STDC_HEADERS
24481 @acindex{STDC_HEADERS}
24482 Replaced by @code{AC_HEADER_STDC} (@pxref{AC_HEADER_STDC}), which
24483 is itself obsolete. Nowadays it is safe to assume the facilities of C89
24489 Replaced by @code{AC_FUNC_STRCOLL} (@pxref{AC_FUNC_STRCOLL}).
24492 @defmac AC_STRUCT_ST_BLKSIZE
24493 @acindex{STRUCT_ST_BLKSIZE}
24494 @cvindex HAVE_STRUCT_STAT_ST_BLKSIZE
24495 @cvindex HAVE_ST_BLKSIZE
24496 If @code{struct stat} contains an @code{st_blksize} member, define
24497 @code{HAVE_STRUCT_STAT_ST_BLKSIZE}. The former name,
24498 @code{HAVE_ST_BLKSIZE} is to be avoided, as its support will cease in
24499 the future. This macro is obsoleted, and should be replaced by
24502 AC_CHECK_MEMBERS([struct stat.st_blksize])
24505 @xref{AC_CHECK_MEMBERS}.
24508 @defmac AC_STRUCT_ST_RDEV
24509 @acindex{STRUCT_ST_RDEV}
24510 @cvindex HAVE_ST_RDEV
24511 @cvindex HAVE_STRUCT_STAT_ST_RDEV
24512 If @code{struct stat} contains an @code{st_rdev} member, define
24513 @code{HAVE_STRUCT_STAT_ST_RDEV}. The former name for this macro,
24514 @code{HAVE_ST_RDEV}, is to be avoided as it will cease to be supported
24515 in the future. Actually, even the new macro is obsolete and should be
24518 AC_CHECK_MEMBERS([struct stat.st_rdev])
24521 @xref{AC_CHECK_MEMBERS}.
24524 @defmac AC_ST_BLKSIZE
24525 @acindex{ST_BLKSIZE}
24526 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
24529 @defmac AC_ST_BLOCKS
24530 @acindex{ST_BLOCKS}
24531 Replaced by @code{AC_STRUCT_ST_BLOCKS} (@pxref{AC_STRUCT_ST_BLOCKS}).
24536 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
24539 @defmac AC_SYS_RESTARTABLE_SYSCALLS
24540 @acindex{SYS_RESTARTABLE_SYSCALLS}
24541 @cvindex HAVE_RESTARTABLE_SYSCALLS
24542 If the system automatically restarts a system call that is interrupted
24543 by a signal, define @code{HAVE_RESTARTABLE_SYSCALLS}. This macro does
24544 not check whether system calls are restarted in general---it checks whether a
24545 signal handler installed with @code{signal} (but not @code{sigaction})
24546 causes system calls to be restarted. It does not check whether system calls
24547 can be restarted when interrupted by signals that have no handler.
24549 These days portable programs should use @code{sigaction} with
24550 @code{SA_RESTART} if they want restartable system calls. They should
24551 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
24552 system call is restartable is a dynamic issue, not a configuration-time
24556 @defmac AC_SYS_SIGLIST_DECLARED
24557 @acindex{SYS_SIGLIST_DECLARED}
24558 This macro was renamed @code{AC_DECL_SYS_SIGLIST}. However, even that
24559 name is obsolete, as the same functionality is now achieved via
24560 @code{AC_CHECK_DECLS} (@pxref{AC_CHECK_DECLS}).
24563 @defmac AC_TEST_CPP
24565 This macro was renamed @code{AC_TRY_CPP}, which in turn was replaced by
24566 @code{AC_PREPROC_IFELSE} (@pxref{AC_PREPROC_IFELSE}).
24569 @defmac AC_TEST_PROGRAM
24570 @acindex{TEST_PROGRAM}
24571 This macro was renamed @code{AC_TRY_RUN}, which in turn was replaced by
24572 @code{AC_RUN_IFELSE} (@pxref{AC_RUN_IFELSE}).
24575 @defmac AC_TIMEZONE
24577 Replaced by @code{AC_STRUCT_TIMEZONE} (@pxref{AC_STRUCT_TIMEZONE}).
24580 @defmac AC_TIME_WITH_SYS_TIME
24581 @acindex{TIME_WITH_SYS_TIME}
24582 Replaced by @code{AC_HEADER_TIME} (@pxref{AC_HEADER_TIME}), which is
24583 itself obsolete; nowadays one need only do
24584 @samp{AC_CHECK_HEADERS([sys/time.h])}.
24587 @defmac AC_TRY_COMPILE (@var{includes}, @var{function-body}, @
24588 @ovar{action-if-true}, @ovar{action-if-false})
24589 @acindex{TRY_COMPILE}
24594 [AC_LANG_PROGRAM([[@var{includes}]],
24595 [[@var{function-body}]])],
24596 [@var{action-if-true}],
24597 [@var{action-if-false}])
24601 @xref{Running the Compiler}.
24603 This macro double quotes both @var{includes} and @var{function-body}.
24605 For C and C++, @var{includes} is any @code{#include} statements needed
24606 by the code in @var{function-body} (@var{includes} is ignored if
24607 the currently selected language is Fortran or Fortran 77). The compiler
24608 and compilation flags are determined by the current language
24609 (@pxref{Language Choice}).
24612 @defmac AC_TRY_CPP (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
24618 [AC_LANG_SOURCE([[@var{input}]])],
24619 [@var{action-if-true}],
24620 [@var{action-if-false}])
24624 @xref{Running the Preprocessor}.
24626 This macro double quotes the @var{input}.
24629 @defmac AC_TRY_LINK (@var{includes}, @var{function-body}, @
24630 @ovar{action-if-true}, @ovar{action-if-false})
24636 [AC_LANG_PROGRAM([[@var{includes}]],
24637 [[@var{function-body}]])],
24638 [@var{action-if-true}],
24639 [@var{action-if-false}])
24643 @xref{Running the Linker}.
24645 This macro double quotes both @var{includes} and @var{function-body}.
24647 Depending on the current language (@pxref{Language Choice}), create a
24648 test program to see whether a function whose body consists of
24649 @var{function-body} can be compiled and linked. If the file compiles
24650 and links successfully, run shell commands @var{action-if-found},
24651 otherwise run @var{action-if-not-found}.
24653 This macro double quotes both @var{includes} and @var{function-body}.
24655 For C and C++, @var{includes} is any @code{#include} statements needed
24656 by the code in @var{function-body} (@var{includes} is ignored if
24657 the currently selected language is Fortran or Fortran 77). The compiler
24658 and compilation flags are determined by the current language
24659 (@pxref{Language Choice}), and in addition @code{LDFLAGS} and
24660 @code{LIBS} are used for linking.
24663 @defmac AC_TRY_LINK_FUNC (@var{function}, @ovar{action-if-found}, @
24664 @ovar{action-if-not-found})
24665 @acindex{TRY_LINK_FUNC}
24666 This macro is equivalent to
24668 AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])],
24669 [@var{action-if-found}], [@var{action-if-not-found}])
24672 @xref{Running the Linker}.
24675 @defmac AC_TRY_RUN (@var{program}, @ovar{action-if-true}, @
24676 @ovar{action-if-false}, @dvar{action-if-cross-compiling, AC_MSG_FAILURE})
24682 [AC_LANG_SOURCE([[@var{program}]])],
24683 [@var{action-if-true}],
24684 [@var{action-if-false}],
24685 [@var{action-if-cross-compiling}])
24692 @anchor{AC_TYPE_SIGNAL}
24693 @defmac AC_TYPE_SIGNAL
24694 @acindex{TYPE_SIGNAL}
24695 @cvindex RETSIGTYPE
24696 @hdrindex{signal.h}
24697 If @file{signal.h} declares @code{signal} as returning a pointer to a
24698 function returning @code{void}, define @code{RETSIGTYPE} to be
24699 @code{void}; otherwise, define it to be @code{int}. These days, it is
24700 portable to assume C89, and that signal handlers return @code{void},
24701 without needing to use this macro or @code{RETSIGTYPE}.
24706 Replaced by @code{AC_TYPE_UID_T} (@pxref{AC_TYPE_UID_T}).
24709 @defmac AC_UNISTD_H
24711 Same as @samp{AC_CHECK_HEADERS([unistd.h])} (@pxref{AC_CHECK_HEADERS}),
24712 which is one of the tests done as a side effect by
24713 @code{AC_INCLUDES_DEFAULT} (@pxref{Default Includes}), so usually
24714 unnecessary to write explicitly.
24720 Define @code{USG} if the BSD string functions (@code{bcopy},
24721 @code{bzero}, @code{index}, @code{rindex}, etc) are @emph{not} defined
24722 in @file{strings.h}. Modern code should assume @file{string.h} exists
24723 and should use the standard C string functions (@code{memmove}, @code{memset},
24724 @code{strchr}, @code{strrchr}, etc) unconditionally.
24726 @file{strings.h} may be the only header that declares @code{strcasecmp},
24727 @code{strncasecmp}, and @code{ffs}. @code{AC_INCLUDES_DEFAULT} checks
24728 for it (@pxref{Default Includes}); test @code{HAVE_STRINGS_H}.
24731 @defmac AC_UTIME_NULL
24732 @acindex{UTIME_NULL}
24733 Replaced by @code{AC_FUNC_UTIME_NULL} (@pxref{AC_FUNC_UTIME_NULL}).
24736 @defmac AC_VALIDATE_CACHED_SYSTEM_TUPLE (@ovar{cmd})
24737 @acindex{VALIDATE_CACHED_SYSTEM_TUPLE}
24738 If the cache file is inconsistent with the current host, target and
24739 build system types, it used to execute @var{cmd} or print a default
24740 error message. This is now handled by default.
24743 @defmac AC_VERBOSE (@var{result-description})
24745 Replaced by @code{AC_MSG_RESULT} (@pxref{AC_MSG_RESULT}).
24750 Replaced by @code{AC_FUNC_FORK} (@pxref{AC_FUNC_FORK}).
24755 Replaced by @code{AC_FUNC_VPRINTF} (@pxref{AC_FUNC_VPRINTF}).
24760 This macro was renamed @code{AC_FUNC_WAIT3}. However, these days
24761 portable programs should use @code{waitpid}, not @code{wait3}, as
24762 @code{wait3} has been removed from Posix.
24767 Replaced by @code{AC_MSG_WARN} (@pxref{AC_MSG_WARN}).
24770 @defmac AC_WARNING (@var{message})
24772 Replaced by @code{m4_warn} (@pxref{m4_warn}).
24775 @defmac AC_WITH (@var{package}, @var{action-if-given}, @
24776 @ovar{action-if-not-given})
24778 This is an obsolete version of @code{AC_ARG_WITH} that does not
24779 support providing a help string (@pxref{AC_ARG_WITH}).
24782 @defmac AC_WORDS_BIGENDIAN
24783 @acindex{WORDS_BIGENDIAN}
24784 Replaced by @code{AC_C_BIGENDIAN} (@pxref{AC_C_BIGENDIAN}).
24787 @defmac AC_XENIX_DIR
24788 @acindex{XENIX_DIR}
24790 This macro used to add @option{-lx} to output variable @code{LIBS} if on
24791 Xenix. Also, if @file{dirent.h} is being checked for, added
24792 @option{-ldir} to @code{LIBS}. Now it is merely an alias of
24793 @code{AC_HEADER_DIRENT} instead, plus some code to detect whether
24794 running XENIX on which you should not depend:
24797 AC_MSG_CHECKING([for Xenix])
24798 AC_EGREP_CPP([yes],
24799 [#if defined M_XENIX && !defined M_UNIX
24802 [AC_MSG_RESULT([yes]); XENIX=yes],
24803 [AC_MSG_RESULT([no]); XENIX=])
24806 Don't use this macro, the dignified means to check the nature of the
24807 host is using @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
24810 @defmac AC_YYTEXT_POINTER
24811 @acindex{YYTEXT_POINTER}
24812 This macro was renamed @code{AC_DECL_YYTEXT}, which in turn was
24813 integrated into @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
24817 @section Upgrading From Version 1
24818 @cindex Upgrading autoconf
24819 @cindex Autoconf upgrading
24821 Autoconf version 2 is mostly backward compatible with version 1.
24822 However, it introduces better ways to do some things, and doesn't
24823 support some of the ugly things in version 1. So, depending on how
24824 sophisticated your @file{configure.ac} files are, you might have to do
24825 some manual work in order to upgrade to version 2. This chapter points
24826 out some problems to watch for when upgrading. Also, perhaps your
24827 @command{configure} scripts could benefit from some of the new features in
24828 version 2; the changes are summarized in the file @file{NEWS} in the
24829 Autoconf distribution.
24832 * Changed File Names:: Files you might rename
24833 * Changed Makefiles:: New things to put in @file{Makefile.in}
24834 * Changed Macros:: Macro calls you might replace
24835 * Changed Results:: Changes in how to check test results
24836 * Changed Macro Writing:: Better ways to write your own macros
24839 @node Changed File Names
24840 @subsection Changed File Names
24842 If you have an @file{aclocal.m4} installed with Autoconf (as opposed to
24843 in a particular package's source directory), you must rename it to
24844 @file{acsite.m4}. @xref{autoconf Invocation}.
24846 If you distribute @file{install.sh} with your package, rename it to
24847 @file{install-sh} so @command{make} builtin rules don't inadvertently
24848 create a file called @file{install} from it. @code{AC_PROG_INSTALL}
24849 looks for the script under both names, but it is best to use the new name.
24851 If you were using @file{config.h.top}, @file{config.h.bot}, or
24852 @file{acconfig.h}, you still can, but you have less clutter if you
24853 use the @code{AH_} macros. @xref{Autoheader Macros}.
24855 @node Changed Makefiles
24856 @subsection Changed Makefiles
24858 Add @samp{@@CFLAGS@@}, @samp{@@CPPFLAGS@@}, and @samp{@@LDFLAGS@@} in
24859 your @file{Makefile.in} files, so they can take advantage of the values
24860 of those variables in the environment when @command{configure} is run.
24861 Doing this isn't necessary, but it's a convenience for users.
24863 Also add @samp{@@configure_input@@} in a comment to each input file for
24864 @code{AC_OUTPUT}, so that the output files contain a comment saying
24865 they were produced by @command{configure}. Automatically selecting the
24866 right comment syntax for all the kinds of files that people call
24867 @code{AC_OUTPUT} on became too much work.
24869 Add @file{config.log} and @file{config.cache} to the list of files you
24870 remove in @code{distclean} targets.
24872 If you have the following in @file{Makefile.in}:
24875 prefix = /usr/local
24876 exec_prefix = $(prefix)
24880 you must change it to:
24883 prefix = @@prefix@@
24884 exec_prefix = @@exec_prefix@@
24888 The old behavior of replacing those variables without @samp{@@}
24889 characters around them has been removed.
24891 @node Changed Macros
24892 @subsection Changed Macros
24894 Many of the macros were renamed in Autoconf version 2. You can still
24895 use the old names, but the new ones are clearer, and it's easier to find
24896 the documentation for them. @xref{Obsolete Macros}, for a table showing the
24897 new names for the old macros. Use the @command{autoupdate} program to
24898 convert your @file{configure.ac} to using the new macro names.
24899 @xref{autoupdate Invocation}.
24901 Some macros have been superseded by similar ones that do the job better,
24902 but are not call-compatible. If you get warnings about calling obsolete
24903 macros while running @command{autoconf}, you may safely ignore them, but
24904 your @command{configure} script generally works better if you follow
24905 the advice that is printed about what to replace the obsolete macros with. In
24906 particular, the mechanism for reporting the results of tests has
24907 changed. If you were using @command{echo} or @code{AC_VERBOSE} (perhaps
24908 via @code{AC_COMPILE_CHECK}), your @command{configure} script's output
24909 looks better if you switch to @code{AC_MSG_CHECKING} and
24910 @code{AC_MSG_RESULT}. @xref{Printing Messages}. Those macros work best
24911 in conjunction with cache variables. @xref{Caching Results}.
24915 @node Changed Results
24916 @subsection Changed Results
24918 If you were checking the results of previous tests by examining the
24919 shell variable @code{DEFS}, you need to switch to checking the values of
24920 the cache variables for those tests. @code{DEFS} no longer exists while
24921 @command{configure} is running; it is only created when generating output
24922 files. This difference from version 1 is because properly quoting the
24923 contents of that variable turned out to be too cumbersome and
24924 inefficient to do every time @code{AC_DEFINE} is called. @xref{Cache
24927 For example, here is a @file{configure.ac} fragment written for Autoconf
24931 AC_HAVE_FUNCS(syslog)
24933 *-DHAVE_SYSLOG*) ;;
24934 *) # syslog is not in the default libraries. See if it's in some other.
24936 for lib in bsd socket inet; do
24937 AC_CHECKING(for syslog in -l$lib)
24938 LIBS="-l$lib $saved_LIBS"
24939 AC_HAVE_FUNCS(syslog)
24941 *-DHAVE_SYSLOG*) break ;;
24949 Here is a way to write it for version 2:
24952 AC_CHECK_FUNCS([syslog])
24953 AS_IF([test "x$ac_cv_func_syslog" = xno],
24954 [# syslog is not in the default libraries. See if it's in some other.
24955 for lib in bsd socket inet; do
24956 AC_CHECK_LIB([$lib], [syslog],
24957 [AC_DEFINE([HAVE_SYSLOG])
24958 LIBS="-l$lib $LIBS"; break])
24962 If you were working around bugs in @code{AC_DEFINE_UNQUOTED} by adding
24963 backslashes before quotes, you need to remove them. It now works
24964 predictably, and does not treat quotes (except back quotes) specially.
24965 @xref{Setting Output Variables}.
24967 All of the Boolean shell variables set by Autoconf macros now use
24968 @samp{yes} for the true value. Most of them use @samp{no} for false,
24969 though for backward compatibility some use the empty string instead. If
24970 you were relying on a shell variable being set to something like 1 or
24971 @samp{t} for true, you need to change your tests.
24973 @node Changed Macro Writing
24974 @subsection Changed Macro Writing
24976 When defining your own macros, you should now use @code{AC_DEFUN}
24977 instead of @code{define}. @code{AC_DEFUN} automatically calls
24978 @code{AC_PROVIDE} and ensures that macros called via @code{AC_REQUIRE}
24979 do not interrupt other macros, to prevent nested @samp{checking@dots{}}
24980 messages on the screen. There's no actual harm in continuing to use the
24981 older way, but it's less convenient and attractive. @xref{Macro
24984 You probably looked at the macros that came with Autoconf as a guide for
24985 how to do things. It would be a good idea to take a look at the new
24986 versions of them, as the style is somewhat improved and they take
24987 advantage of some new features.
24989 If you were doing tricky things with undocumented Autoconf internals
24990 (macros, variables, diversions), check whether you need to change
24991 anything to account for changes that have been made. Perhaps you can
24992 even use an officially supported technique in version 2 instead of
24993 kludging. Or perhaps not.
24995 To speed up your locally written feature tests, add caching to them.
24996 See whether any of your tests are of general enough usefulness to
24997 encapsulate them into macros that you can share.
25000 @node Autoconf 2.13
25001 @section Upgrading From Version 2.13
25002 @cindex Upgrading autoconf
25003 @cindex Autoconf upgrading
25005 The introduction of the previous section (@pxref{Autoconf 1}) perfectly
25006 suits this section@enddots{}
25009 Autoconf version 2.50 is mostly backward compatible with version 2.13.
25010 However, it introduces better ways to do some things, and doesn't
25011 support some of the ugly things in version 2.13. So, depending on how
25012 sophisticated your @file{configure.ac} files are, you might have to do
25013 some manual work in order to upgrade to version 2.50. This chapter
25014 points out some problems to watch for when upgrading. Also, perhaps
25015 your @command{configure} scripts could benefit from some of the new
25016 features in version 2.50; the changes are summarized in the file
25017 @file{NEWS} in the Autoconf distribution.
25021 * Changed Quotation:: Broken code which used to work
25022 * New Macros:: Interaction with foreign macros
25023 * Hosts and Cross-Compilation:: Bugward compatibility kludges
25024 * AC_LIBOBJ vs LIBOBJS:: LIBOBJS is a forbidden token
25025 * AC_ACT_IFELSE vs AC_TRY_ACT:: A more generic scheme for testing sources
25028 @node Changed Quotation
25029 @subsection Changed Quotation
25031 The most important changes are invisible to you: the implementation of
25032 most macros have completely changed. This allowed more factorization of
25033 the code, better error messages, a higher uniformity of the user's
25034 interface etc. Unfortunately, as a side effect, some construct which
25035 used to (miraculously) work might break starting with Autoconf 2.50.
25036 The most common culprit is bad quotation.
25038 For instance, in the following example, the message is not properly
25043 AC_CHECK_HEADERS(foo.h, ,
25044 AC_MSG_ERROR(cannot find foo.h, bailing out))
25049 Autoconf 2.13 simply ignores it:
25052 $ @kbd{autoconf-2.13; ./configure --silent}
25053 creating cache ./config.cache
25054 configure: error: cannot find foo.h
25059 while Autoconf 2.50 produces a broken @file{configure}:
25062 $ @kbd{autoconf-2.50; ./configure --silent}
25063 configure: error: cannot find foo.h
25064 ./configure: exit: bad non-numeric arg `bailing'
25065 ./configure: exit: bad non-numeric arg `bailing'
25069 The message needs to be quoted, and the @code{AC_MSG_ERROR} invocation
25073 AC_INIT([Example], [1.0], [bug-example@@example.org])
25074 AC_CHECK_HEADERS([foo.h], [],
25075 [AC_MSG_ERROR([cannot find foo.h, bailing out])])
25079 Many many (and many more) Autoconf macros were lacking proper quotation,
25080 including no less than@dots{} @code{AC_DEFUN} itself!
25083 $ @kbd{cat configure.in}
25084 AC_DEFUN([AC_PROG_INSTALL],
25085 [# My own much better version
25090 $ @kbd{autoconf-2.13}
25091 autoconf: Undefined macros:
25092 ***BUG in Autoconf--please report*** AC_FD_MSG
25093 ***BUG in Autoconf--please report*** AC_EPI
25094 configure.in:1:AC_DEFUN([AC_PROG_INSTALL],
25095 configure.in:5:AC_PROG_INSTALL
25096 $ @kbd{autoconf-2.50}
25102 @subsection New Macros
25104 @cindex undefined macro
25105 @cindex @code{_m4_divert_diversion}
25107 While Autoconf was relatively dormant in the late 1990s, Automake
25108 provided Autoconf-like macros for a while. Starting with Autoconf 2.50
25109 in 2001, Autoconf provided
25110 versions of these macros, integrated in the @code{AC_} namespace,
25111 instead of @code{AM_}. But in order to ease the upgrading via
25112 @command{autoupdate}, bindings to such @code{AM_} macros are provided.
25114 Unfortunately older versions of Automake (e.g., Automake 1.4)
25115 did not quote the names of these macros.
25116 Therefore, when @command{m4} finds something like
25117 @samp{AC_DEFUN(AM_TYPE_PTRDIFF_T, @dots{})} in @file{aclocal.m4},
25118 @code{AM_TYPE_PTRDIFF_T} is
25119 expanded, replaced with its Autoconf definition.
25121 Fortunately Autoconf catches pre-@code{AC_INIT} expansions, and
25122 complains, in its own words:
25125 $ @kbd{cat configure.ac}
25126 AC_INIT([Example], [1.0], [bug-example@@example.org])
25128 $ @kbd{aclocal-1.4}
25130 aclocal.m4:17: error: m4_defn: undefined macro: _m4_divert_diversion
25131 aclocal.m4:17: the top level
25132 autom4te: m4 failed with exit status: 1
25136 Modern versions of Automake no longer define most of these
25137 macros, and properly quote the names of the remaining macros.
25138 If you must use an old Automake, do not depend upon macros from Automake
25139 as it is simply not its job
25140 to provide macros (but the one it requires itself):
25143 $ @kbd{cat configure.ac}
25144 AC_INIT([Example], [1.0], [bug-example@@example.org])
25146 $ @kbd{rm aclocal.m4}
25148 autoupdate: 'configure.ac' is updated
25149 $ @kbd{cat configure.ac}
25150 AC_INIT([Example], [1.0], [bug-example@@example.org])
25151 AC_CHECK_TYPES([ptrdiff_t])
25152 $ @kbd{aclocal-1.4}
25158 @node Hosts and Cross-Compilation
25159 @subsection Hosts and Cross-Compilation
25160 @cindex Cross compilation
25162 Based on the experience of compiler writers, and after long public
25163 debates, many aspects of the cross-compilation chain have changed:
25167 the relationship between the build, host, and target architecture types,
25170 the command line interface for specifying them to @command{configure},
25173 the variables defined in @command{configure},
25176 the enabling of cross-compilation mode.
25181 The relationship between build, host, and target have been cleaned up:
25182 the chain of default is now simply: target defaults to host, host to
25183 build, and build to the result of @command{config.guess}. Nevertheless,
25184 in order to ease the transition from 2.13 to 2.50, the following
25185 transition scheme is implemented. @emph{Do not rely on it}, as it will
25186 be completely disabled in a couple of releases (we cannot keep it, as it
25187 proves to cause more problems than it cures).
25189 They all default to the result of running @command{config.guess}, unless
25190 you specify either @option{--build} or @option{--host}. In this case,
25191 the default becomes the system type you specified. If you specify both,
25192 and they're different, @command{configure} enters cross compilation
25193 mode, so it doesn't run any tests that require execution.
25195 Hint: if you mean to override the result of @command{config.guess},
25196 prefer @option{--build} over @option{--host}.
25200 For backward compatibility, @command{configure} accepts a system
25201 type as an option by itself. Such an option overrides the
25202 defaults for build, host, and target system types. The following
25203 configure statement configures a cross toolchain that runs on
25204 NetBSD/alpha but generates code for GNU Hurd/sparc,
25205 which is also the build platform.
25208 ./configure --host=alpha-netbsd sparc-gnu
25213 In Autoconf 2.13 and before, the variables @code{build}, @code{host},
25214 and @code{target} had a different semantics before and after the
25215 invocation of @code{AC_CANONICAL_BUILD} etc. Now, the argument of
25216 @option{--build} is strictly copied into @code{build_alias}, and is left
25217 empty otherwise. After the @code{AC_CANONICAL_BUILD}, @code{build} is
25218 set to the canonicalized build type. To ease the transition, before,
25219 its contents is the same as that of @code{build_alias}. Do @emph{not}
25220 rely on this broken feature.
25222 For consistency with the backward compatibility scheme exposed above,
25223 when @option{--host} is specified but @option{--build} isn't, the build
25224 system is assumed to be the same as @option{--host}, and
25225 @samp{build_alias} is set to that value. Eventually, this
25226 historically incorrect behavior will go away.
25230 The former scheme to enable cross-compilation proved to cause more harm
25231 than good, in particular, it used to be triggered too easily, leaving
25232 regular end users puzzled in front of cryptic error messages.
25233 @command{configure} could even enter cross-compilation mode only
25234 because the compiler was not functional. This is mainly because
25235 @command{configure} used to try to detect cross-compilation, instead of
25236 waiting for an explicit flag from the user.
25238 Now, @command{configure} enters cross-compilation mode if and only if
25239 @option{--host} is passed.
25241 That's the short documentation. To ease the transition between 2.13 and
25242 its successors, a more complicated scheme is implemented. @emph{Do not
25243 rely on the following}, as it will be removed in the near future.
25245 If you specify @option{--host}, but not @option{--build}, when
25246 @command{configure} performs the first compiler test it tries to run
25247 an executable produced by the compiler. If the execution fails, it
25248 enters cross-compilation mode. This is fragile. Moreover, by the time
25249 the compiler test is performed, it may be too late to modify the
25250 build-system type: other tests may have already been performed.
25251 Therefore, whenever you specify @option{--host}, be sure to specify
25252 @option{--build} too.
25255 ./configure --build=x86_64-pc-linux-gnu --host=x86_64-w64-mingw64
25259 enters cross-compilation mode. The former interface, which
25260 consisted in setting the compiler to a cross-compiler without informing
25261 @command{configure} is obsolete. For instance, @command{configure}
25262 fails if it can't run the code generated by the specified compiler if you
25263 configure as follows:
25266 ./configure CC=x86_64-w64-mingw64-gcc
25270 @node AC_LIBOBJ vs LIBOBJS
25271 @subsection @code{AC_LIBOBJ} vs.@: @code{LIBOBJS}
25273 Up to Autoconf 2.13, the replacement of functions was triggered via the
25274 variable @code{LIBOBJS}. Since Autoconf 2.50, the macro
25275 @code{AC_LIBOBJ} should be used instead (@pxref{Generic Functions}).
25276 Starting at Autoconf 2.53, the use of @code{LIBOBJS} is an error.
25278 This change is mandated by the unification of the GNU Build System
25279 components. In particular, the various fragile techniques used to parse
25280 a @file{configure.ac} are all replaced with the use of traces. As a
25281 consequence, any action must be traceable, which obsoletes critical
25282 variable assignments. Fortunately, @code{LIBOBJS} was the only problem,
25283 and it can even be handled gracefully (read, ``without your having to
25284 change something'').
25286 There were two typical uses of @code{LIBOBJS}: asking for a replacement
25287 function, and adjusting @code{LIBOBJS} for Automake and/or Libtool.
25291 As for function replacement, the fix is immediate: use
25292 @code{AC_LIBOBJ}. For instance:
25295 LIBOBJS="$LIBOBJS fnmatch.o"
25296 LIBOBJS="$LIBOBJS malloc.$ac_objext"
25300 should be replaced with:
25303 AC_LIBOBJ([fnmatch])
25304 AC_LIBOBJ([malloc])
25310 When used with Automake 1.10 or newer, a suitable value for
25311 @code{LIBOBJDIR} is set so that the @code{LIBOBJS} and @code{LTLIBOBJS}
25312 can be referenced from any @file{Makefile.am}. Even without Automake,
25313 arranging for @code{LIBOBJDIR} to be set correctly enables
25314 referencing @code{LIBOBJS} and @code{LTLIBOBJS} in another directory.
25315 The @code{LIBOBJDIR} feature is experimental.
25318 @node AC_ACT_IFELSE vs AC_TRY_ACT
25319 @subsection @code{AC_@var{ACT}_IFELSE} vs.@: @code{AC_TRY_@var{ACT}}
25320 @c the anchor keeps the old node name, to try to avoid breaking links
25321 @anchor{AC_FOO_IFELSE vs AC_TRY_FOO}
25323 @acindex{@var{ACT}_IFELSE}
25324 @acindex{TRY_@var{ACT}}
25325 Since Autoconf 2.50, internal codes uses @code{AC_PREPROC_IFELSE},
25326 @code{AC_COMPILE_IFELSE}, @code{AC_LINK_IFELSE}, and
25327 @code{AC_RUN_IFELSE} on one hand and @code{AC_LANG_SOURCE},
25328 and @code{AC_LANG_PROGRAM} on the other hand instead of the deprecated
25329 @code{AC_TRY_CPP}, @code{AC_TRY_COMPILE}, @code{AC_TRY_LINK}, and
25330 @code{AC_TRY_RUN}. The motivations where:
25333 a more consistent interface: @code{AC_TRY_COMPILE} etc.@: were double
25334 quoting their arguments;
25337 the combinatorial explosion is solved by decomposing on the one hand the
25338 generation of sources, and on the other hand executing the program;
25341 this scheme helps supporting more languages than plain C and C++.
25344 In addition to the change of syntax, the philosophy has changed too:
25345 while emphasis was put on speed at the expense of accuracy, today's
25346 Autoconf promotes accuracy of the testing framework at, ahem@dots{}, the
25350 As a perfect example of what is @emph{not} to be done, here is how to
25351 find out whether a header file contains a particular declaration, such
25352 as a typedef, a structure, a structure member, or a function. Use
25353 @code{AC_EGREP_HEADER} instead of running @code{grep} directly on the
25354 header file; on some systems the symbol might be defined in another
25355 header file that the file you are checking includes.
25357 As a (bad) example, here is how you should not check for C preprocessor
25358 symbols, either defined by header files or predefined by the C
25359 preprocessor: using @code{AC_EGREP_CPP}:
25367 ], is_aix=yes, is_aix=no)
25371 The above example, properly written would (i) use
25372 @code{AC_LANG_PROGRAM}, and (ii) run the compiler:
25376 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
25378 error: This isn't AIX!
25387 @c ============================= Generating Test Suites with Autotest
25389 @node Using Autotest
25390 @chapter Generating Test Suites with Autotest
25395 @strong{N.B.: This section describes a feature which is still
25396 stabilizing. Although we believe that Autotest is useful as-is, this
25397 documentation describes an interface which might change in the future:
25398 do not depend upon Autotest without subscribing to the Autoconf mailing
25402 It is paradoxical that portable projects depend on nonportable tools
25403 to run their test suite. Autoconf by itself is the paragon of this
25404 problem: although it aims at perfectly portability, up to 2.13 its
25405 test suite was using DejaGNU, a rich and complex testing
25406 framework, but which is far from being standard on Posix systems.
25407 Worse yet, it was likely to be missing on the most fragile platforms,
25408 the very platforms that are most likely to torture Autoconf and
25409 exhibit deficiencies.
25411 To circumvent this problem, many package maintainers have developed their
25412 own testing framework, based on simple shell scripts whose sole outputs
25413 are exit status values describing whether the test succeeded. Most of
25414 these tests share common patterns, and this can result in lots of
25415 duplicated code and tedious maintenance.
25417 Following exactly the same reasoning that yielded to the inception of
25418 Autoconf, Autotest provides a test suite generation framework, based on
25419 M4 macros building a portable shell script. The suite itself is
25420 equipped with automatic logging and tracing facilities which greatly
25421 diminish the interaction with bug reporters, and simple timing reports.
25423 Autoconf itself has been using Autotest for years, and we do attest that
25424 it has considerably improved the strength of the test suite and the
25425 quality of bug reports. Other projects are known to use some generation
25426 of Autotest, such as Bison, GNU Wdiff, GNU Tar, each of
25427 them with different needs, and this usage has validated Autotest as a general
25430 Nonetheless, compared to DejaGNU, Autotest is inadequate for
25431 interactive tool testing, which is probably its main limitation.
25434 * Using an Autotest Test Suite:: Autotest and the user
25435 * Writing Testsuites:: Autotest macros
25436 * testsuite Invocation:: Running @command{testsuite} scripts
25437 * Making testsuite Scripts:: Using autom4te to create @command{testsuite}
25440 @node Using an Autotest Test Suite
25441 @section Using an Autotest Test Suite
25444 * testsuite Scripts:: The concepts of Autotest
25445 * Autotest Logs:: Their contents
25448 @node testsuite Scripts
25449 @subsection @command{testsuite} Scripts
25451 @cindex @command{testsuite}
25453 Generating testing or validation suites using Autotest is rather easy.
25454 The whole validation suite is held in a file to be processed through
25455 @command{autom4te}, itself using GNU M4 under the hood, to
25456 produce a stand-alone Bourne shell script which then gets distributed.
25457 Neither @command{autom4te} nor GNU M4 are needed at
25458 the installer's end.
25461 Each test of the validation suite should be part of some test group. A
25462 @dfn{test group} is a sequence of interwoven tests that ought to be
25463 executed together, usually because one test in the group creates data
25464 files that a later test in the same group needs to read. Complex test
25465 groups make later debugging more tedious. It is much better to
25466 keep only a few tests per test group. Ideally there is only one test
25469 For all but the simplest packages, some file such as @file{testsuite.at}
25470 does not fully hold all test sources, as these are often easier to
25471 maintain in separate files. Each of these separate files holds a single
25472 test group, or a sequence of test groups all addressing some common
25473 functionality in the package. In such cases, @file{testsuite.at}
25474 merely initializes the validation suite, and sometimes does elementary
25475 health checking, before listing include statements for all other test
25476 files. The special file @file{package.m4}, containing the
25477 identification of the package, is automatically included if found.
25479 A convenient alternative consists in moving all the global issues
25480 (local Autotest macros, elementary health checking, and @code{AT_INIT}
25481 invocation) into the file @code{local.at}, and making
25482 @file{testsuite.at} be a simple list of @code{m4_include}s of sub test
25483 suites. In such case, generating the whole test suite or pieces of it
25484 is only a matter of choosing the @command{autom4te} command line
25487 The validation scripts that Autotest produces are by convention called
25488 @command{testsuite}. When run, @command{testsuite} executes each test
25489 group in turn, producing only one summary line per test to say if that
25490 particular test succeeded or failed. At end of all tests, summarizing
25491 counters get printed. One debugging directory is left for each test
25492 group which failed, if any: such directories are named
25493 @file{testsuite.dir/@var{nn}}, where @var{nn} is the sequence number of
25494 the test group, and they include:
25497 @item a debugging script named @file{run} which reruns the test in
25498 @dfn{debug mode} (@pxref{testsuite Invocation}). The automatic generation
25499 of debugging scripts has the purpose of easing the chase for bugs.
25501 @item all the files created with @code{AT_DATA}
25503 @item all the Erlang source code files created with @code{AT_CHECK_EUNIT}
25505 @item a log of the run, named @file{testsuite.log}
25508 In the ideal situation, none of the tests fail, and consequently no
25509 debugging directory is left behind for validation.
25511 It often happens in practice that individual tests in the validation
25512 suite need to get information coming out of the configuration process.
25513 Some of this information, common for all validation suites, is provided
25514 through the file @file{atconfig}, automatically created by
25515 @code{AC_CONFIG_TESTDIR}. For configuration information which your
25516 testing environment specifically needs, you might prepare an optional
25517 file named @file{atlocal.in}, instantiated by @code{AC_CONFIG_FILES}.
25518 The configuration process produces @file{atconfig} and @file{atlocal}
25519 out of these two input files, and these two produced files are
25520 automatically read by the @file{testsuite} script.
25522 Here is a diagram showing the relationship between files.
25525 Files used in preparing a software package for distribution:
25530 subfile-1.at ->. [local.at] ---->+
25532 subfile-i.at ---->-- testsuite.at -->-- autom4te* -->testsuite
25538 Files used in configuring a software package:
25543 [atlocal.in] --> config.status* --<
25549 Files created during test suite execution:
25552 atconfig -->. .--> testsuite.log
25556 [atlocal] ->' `--> [testsuite.dir]
25560 @node Autotest Logs
25561 @subsection Autotest Logs
25563 When run, the test suite creates a log file named after itself, e.g., a
25564 test suite named @command{testsuite} creates @file{testsuite.log}. It
25565 contains a lot of information, usually more than maintainers actually
25566 need, but therefore most of the time it contains all that is needed:
25569 @item command line arguments
25570 A bad but unfortunately widespread habit consists of
25571 setting environment variables before the command, such as in
25572 @samp{CC=my-home-grown-cc ./testsuite}. The test suite does not
25573 know this change, hence (i) it cannot report it to you, and (ii)
25574 it cannot preserve the value of @code{CC} for subsequent runs.
25575 Autoconf faced exactly the same problem, and solved it by asking
25576 users to pass the variable definitions as command line arguments.
25577 Autotest requires this rule, too, but has no means to enforce it; the log
25578 then contains a trace of the variables that were changed by the user.
25580 @item @file{ChangeLog} excerpts
25581 The topmost lines of all the @file{ChangeLog} files found in the source
25582 hierarchy. This is especially useful when bugs are reported against
25583 development versions of the package, since the version string does not
25584 provide sufficient information to know the exact state of the sources
25585 the user compiled. Of course, this relies on the use of a
25588 @item build machine
25589 Running a test suite in a cross-compile environment is not an easy task,
25590 since it would mean having the test suite run on a machine @var{build},
25591 while running programs on a machine @var{host}. It is much simpler to
25592 run both the test suite and the programs on @var{host}, but then, from
25593 the point of view of the test suite, there remains a single environment,
25594 @var{host} = @var{build}. The log contains relevant information on the
25595 state of the @var{build} machine, including some important environment
25597 @c FIXME: How about having an M4sh macro to say "hey, log the value
25598 @c of '@dots{}'"? This would help both Autoconf and Autotest.
25600 @item tested programs
25601 The absolute file name and answers to @option{--version} of the tested
25602 programs (see @ref{Writing Testsuites}, @code{AT_TESTED}).
25604 @item configuration log
25605 The contents of @file{config.log}, as created by @command{configure},
25606 are appended. It contains the configuration flags and a detailed report
25607 on the configuration itself.
25611 @node Writing Testsuites
25612 @section Writing @file{testsuite.at}
25614 The @file{testsuite.at} is a Bourne shell script making use of special
25615 Autotest M4 macros. It often contains a call to @code{AT_INIT} near
25616 its beginning followed by one call to @code{m4_include} per source file
25617 for tests. Each such included file, or the remainder of
25618 @file{testsuite.at} if include files are not used, contain a sequence of
25619 test groups. Each test group begins with a call to @code{AT_SETUP},
25620 then an arbitrary number of shell commands or calls to @code{AT_CHECK},
25621 and then completes with a call to @code{AT_CLEANUP}. Multiple test
25622 groups can be categorized by a call to @code{AT_BANNER}.
25624 All of the public Autotest macros have all-uppercase names in the
25625 namespace @samp{^AT_} to prevent them from accidentally conflicting with
25626 other text; Autoconf also reserves the namespace @samp{^_AT_} for
25627 internal macros. All shell variables used in the testsuite for internal
25628 purposes have mostly-lowercase names starting with @samp{at_}. Autotest
25629 also uses here-document delimiters in the namespace @samp{^_AT[A-Z]}, and
25630 makes use of the file system namespace @samp{^at-}.
25632 Since Autoconf is built on top of M4sugar (@pxref{Programming in
25633 M4sugar}) and M4sh (@pxref{Programming in M4sh}), you must also be aware
25634 of those namespaces (@samp{^_?\(m4\|AS\)_}). In general, you
25635 @emph{should not use} the namespace of a package that does not own the
25636 macro or shell code you are writing.
25638 @defmac AT_INIT (@ovar{name})
25640 @c FIXME: Not clear, plus duplication of the information.
25641 Initialize Autotest. Giving a @var{name} to the test suite is
25642 encouraged if your package includes several test suites. Before this
25643 macro is called, @code{AT_PACKAGE_STRING} and
25644 @code{AT_PACKAGE_BUGREPORT} must be defined, which are used to display
25645 information about the testsuite to the user. Typically, these macros
25646 are provided by a file @file{package.m4} built by @command{make}
25647 (@pxref{Making testsuite Scripts}), in order to inherit the package
25648 name, version, and bug reporting address from @file{configure.ac}.
25651 @defmac AT_COPYRIGHT (@var{copyright-notice})
25652 @atindex{COPYRIGHT}
25653 @cindex Copyright Notice
25654 State that, in addition to the Free Software Foundation's copyright on
25655 the Autotest macros, parts of your test suite are covered by
25656 @var{copyright-notice}.
25658 The @var{copyright-notice} shows up in both the head of
25659 @command{testsuite} and in @samp{testsuite --version}.
25662 @defmac AT_ARG_OPTION (@var{options}, @var{help-text}, @
25663 @ovar{action-if-given}, @ovar{action-if-not-given})
25664 @atindex{ARG_OPTION}
25665 @vrindex at_arg_@var{option}
25666 Accept options from the space-separated list @var{options}, a list that
25667 has leading dashes removed from the options. Long options will be
25668 prefixed with @samp{--}, single-character options with @samp{-}. The
25669 first word in this list is the primary @var{option}, any others are
25670 assumed to be short-hand aliases. The variable associated with it
25671 is @code{at_arg_@var{option}}, with any dashes in @var{option} replaced
25674 If the user passes @option{--@var{option}} to the @command{testsuite},
25675 the variable will be set to @samp{:}. If the user does not pass the
25676 option, or passes @option{--no-@var{option}}, then the variable will be
25677 set to @samp{false}.
25680 @vrindex at_optarg_@var{option}
25681 @var{action-if-given} is run each time the option is encountered; here,
25682 the variable @code{at_optarg} will be set to @samp{:} or @samp{false} as
25683 appropriate. @code{at_optarg} is actually just a copy of
25684 @code{at_arg_@var{option}}.
25686 @var{action-if-not-given} will be run once after option parsing is
25687 complete and if no option from @var{options} was used.
25689 @var{help-text} is added to the end of the list of options shown in
25690 @command{testsuite --help} (@pxref{AS_HELP_STRING}).
25692 It is recommended that you use a package-specific prefix to @var{options}
25693 names in order to avoid clashes with future Autotest built-in options.
25696 @defmac AT_ARG_OPTION_ARG (@var{options}, @var{help-text}, @
25697 @ovar{action-if-given}, @ovar{action-if-not-given})
25698 @atindex{ARG_OPTION_ARG}
25699 @vrindex at_arg_@var{option}
25700 Accept options with arguments from the space-separated list
25701 @var{options}, a list that has leading dashes removed from the options.
25702 Long options will be prefixed with @samp{--}, single-character options
25703 with @samp{-}. The first word in this list is the primary @var{option},
25704 any others are assumed to be short-hand aliases. The variable associated
25705 with it is @code{at_arg_@var{option}}, with any dashes in @var{option}
25706 replaced with underscores.
25708 If the user passes @option{--@var{option}=@var{arg}} or
25709 @option{--@var{option} @var{arg}} to the @command{testsuite}, the
25710 variable will be set to @samp{@var{arg}}.
25713 @var{action-if-given} is run each time the option is encountered; here,
25714 the variable @code{at_optarg} will be set to @samp{@var{arg}}.
25715 @code{at_optarg} is actually just a copy of @code{at_arg_@var{option}}.
25717 @var{action-if-not-given} will be run once after option parsing is
25718 complete and if no option from @var{options} was used.
25720 @var{help-text} is added to the end of the list of options shown in
25721 @command{testsuite --help} (@pxref{AS_HELP_STRING}).
25723 It is recommended that you use a package-specific prefix to @var{options}
25724 names in order to avoid clashes with future Autotest built-in options.
25727 @defmac AT_COLOR_TESTS
25728 @atindex{COLOR_TESTS}
25729 Enable colored test results by default when the output is connected to
25733 @defmac AT_TESTED (@var{executables})
25735 Log the file name and answer to @option{--version} of each program in
25736 space-separated list @var{executables}. Several invocations register
25737 new executables, in other words, don't fear registering one program
25740 Autotest test suites rely on @env{PATH} to find the tested program.
25741 This avoids the need to generate absolute names of the various tools, and
25742 makes it possible to test installed programs. Therefore, knowing which
25743 programs are being exercised is crucial to understanding problems in
25744 the test suite itself, or its occasional misuses. It is a good idea to
25745 also subscribe foreign programs you depend upon, to avoid incompatible
25748 @var{executables} is implicitly wrapped in shell double quotes, but it
25749 will still use shell variable expansion (@samp{$}), command substitution
25750 (@samp{`}), and backslash escaping (@samp{\}). In particular, the
25751 @env{EXEEXT} variable is available if it is passed to the testsuite
25752 via @file{atlocal} or @file{atconfig}.
25755 @defmac AT_PREPARE_TESTS (@var{shell-code})
25756 @atindex{PREPARE_TESTS}
25757 Execute @var{shell-code} in the main testsuite process,
25758 after initializing the test suite and processing command-line options,
25759 but before running any tests. If this macro is used several times,
25760 all of the @var{shell-code}s will be executed,
25761 in the order they appeared in @file{testsuite.at}.
25763 One reason to use @code{AT_PREPARE_TESTS} is when the programs under
25764 test are sensitive to environment variables: you can unset all these
25765 variables or reset them to safe values in @var{shell-code}.
25767 @var{shell-code} is only executed if at least one test is going to be
25768 run. In particular, it will not be executed if any of the @option{--help},
25769 @option{--version}, @option{--list}, or @option{--clean} options are
25770 given to @command{testsuite} (@pxref{testsuite Invocation}).
25773 @defmac AT_PREPARE_EACH_TEST (@var{shell-code})
25774 @atindex{AT_PREPARE_EACH_TEST}
25775 Execute @var{shell-code} in each test group's subshell, at the point of
25776 the @code{AT_SETUP} that starts the test group.
25779 @defmac AT_TEST_HELPER_FN (@var{name}, @var{args}, @var{description}, @var{code})
25780 Define a shell function that will be available to the code for each test
25781 group. Its name will be @code{ath_fn_@var{name}}, and its body will be
25782 @var{code}. (The prefix prevents name conflicts with shell functions
25783 defined by M4sh and Autotest.)
25785 @var{args} should describe the function's arguments and @var{description}
25786 what it does; these are used only for documentation comments in the
25787 generated testsuite script.
25792 @defmac AT_BANNER (@var{test-category-name})
25794 This macro identifies the start of a category of related test groups.
25795 When the resulting @file{testsuite} is invoked with more than one test
25796 group to run, its output will include a banner containing
25797 @var{test-category-name} prior to any tests run from that category. The
25798 banner should be no more than about 40 or 50 characters. A blank banner
25799 indicates uncategorized tests; an empty line will be inserted after
25800 tests from an earlier category, effectively ending that category.
25803 @defmac AT_SETUP (@var{test-group-name})
25805 This macro starts a group of related tests, all to be executed in the
25806 same subshell. It accepts a single argument, which holds a few words
25807 (no more than about 30 or 40 characters) quickly describing the purpose
25808 of the test group being started. @var{test-group-name} must not expand
25809 to unbalanced quotes, although quadrigraphs can be used.
25812 @defmac AT_KEYWORDS (@var{keywords})
25814 Associate the space-separated list of @var{keywords} to the enclosing
25815 test group. This makes it possible to run ``slices'' of the test suite.
25816 For instance, if some of your test groups exercise some @samp{foo}
25817 feature, then using @samp{AT_KEYWORDS(foo)} lets you run
25818 @samp{./testsuite -k foo} to run exclusively these test groups. The
25819 @var{test-group-name} of the test group is automatically recorded to
25820 @code{AT_KEYWORDS}.
25822 Several invocations within a test group accumulate new keywords. In
25823 other words, don't fear registering the same keyword several times in a
25827 @defmac AT_CAPTURE_FILE (@var{file})
25828 @atindex{CAPTURE_FILE}
25829 If the current test group fails, log the contents of @var{file}.
25830 Several identical calls within one test group have no additional effect.
25833 @defmac AT_FAIL_IF (@var{shell-condition})
25835 Make the test group fail and skip the rest of its execution, if
25836 @var{shell-condition} is true. @var{shell-condition} is a shell expression
25837 such as a @code{test} command. Tests before @command{AT_FAIL_IF}
25838 will be executed and may still cause the test group to be skipped.
25839 You can instantiate this macro many times from within the same test group.
25841 You should use this macro only for very simple failure conditions. If the
25842 @var{shell-condition} could emit any kind of output you should instead
25843 use @command{AT_CHECK} like
25845 AT_CHECK([if @var{shell-condition}; then exit 99; fi])
25848 so that such output is properly recorded in the @file{testsuite.log}
25852 @defmac AT_SKIP_IF (@var{shell-condition})
25854 Determine whether the test should be skipped because it requires
25855 features that are unsupported on the machine under test.
25856 @var{shell-condition} is a shell expression such as a @code{test}
25857 command. Tests before @command{AT_SKIP_IF} will be executed
25858 and may still cause the test group to fail. You can instantiate this
25859 macro many times from within the same test group.
25861 You should use this macro only for very simple skip conditions. If the
25862 @var{shell-condition} could emit any kind of output you should instead
25863 use @command{AT_CHECK} like
25865 AT_CHECK([if @var{shell-condition}; then exit 77; fi])
25868 so that such output is properly recorded in the @file{testsuite.log}
25872 @defmac AT_XFAIL_IF (@var{shell-condition})
25874 Determine whether the test is expected to fail because it is a known
25875 bug (for unsupported features, you should skip the test).
25876 @var{shell-condition} is a shell expression such as a @code{test}
25877 command; you can instantiate this macro many times from within the
25878 same test group, and one of the conditions is enough to turn
25879 the test into an expected failure.
25884 End the current test group.
25889 @defmac AT_DATA (@var{file}, @var{contents})
25890 @defmacx AT_DATA_UNQUOTED (@var{file}, @var{contents})
25892 Initialize an input data @var{file} with given @var{contents}. Of
25893 course, the @var{contents} have to be properly quoted between square
25894 brackets to protect against included commas or spurious M4
25895 expansion. @var{contents} must be empty or end with a newline.
25897 be a single shell word that expands into a single file name.
25899 The difference between @code{AT_DATA} and @code{AT_DATA_UNQUOTED} is
25900 that only the latter performs shell variable expansion (@samp{$}),
25901 command substitution (@samp{`}), and backslash escaping (@samp{\})
25905 @defmac AT_CHECK (@var{commands}, @dvar{status, 0}, @ovar{stdout}, @
25906 @ovar{stderr}, @ovar{run-if-fail}, @ovar{run-if-pass})
25907 @defmacx AT_CHECK_UNQUOTED (@var{commands}, @dvar{status, 0}, @ovar{stdout}, @
25908 @ovar{stderr}, @ovar{run-if-fail}, @ovar{run-if-pass})
25910 @atindex{CHECK_UNQUOTED}
25912 Perform a test, by running the shell @var{commands} in a subshell.
25913 @var{commands} is output as-is, so shell expansions are honored.
25914 These commands are expected to have a final exit status of @var{status},
25915 and to produce output as described by @var{stdout} and @var{stderr}
25918 This macro must be invoked in between @code{AT_SETUP} and @code{AT_CLEANUP}.
25920 If @var{commands} exit with unexpected status 77, then the rest of the
25921 test group is skipped. If @var{commands} exit with unexpected status
25922 99, then the test group is immediately failed; this is called a
25923 @emph{hard failure}. Otherwise, the test is considered to have
25924 succeeded if all of the status, stdout, and stderr expectations were
25927 If @var{run-if-fail} is nonempty, it provides extra shell commands to
25928 run when the test fails; if @var{run-if-pass} is nonempty, it provides
25929 extra shell commands to run when the test succeeds. These commands are
25930 @emph{not} run in a subshell, and they are not run when the test group
25931 is skipped (exit code 77) or hard-failed (exit code 99). They may
25932 change whether the test group is considered to have succeeded, by
25933 modifying the shell variable @code{at_failed}; set it to @code{:} to
25934 indicate that the test group has failed, or @code{false} to indicate
25935 that it has succeeded.
25937 The exit status of @var{commands} is available to @var{run-if-fail} and
25938 @var{run-if-pass} commands in the @code{at_status} shell variable. The
25939 output from @var{commands} is also available, in the files named by the
25940 @code{at_stdout} and @code{at_stderr} variables.
25942 If @var{status} is the literal @samp{ignore}, then the exit status of
25943 @var{commands} is not checked, except for the special cases of 77 (skip)
25944 and 99 (hard failure). The existence of hard failures allows one to
25945 mark a test as an expected failure with @code{AT_XFAIL_IF} because a
25946 feature has not yet been implemented, but to still distinguish between
25947 gracefully handling the missing feature and dumping core.
25949 If the value of the @var{stdout} or @var{stderr} parameter is one of the
25950 literals in the following table, then the test treats the output
25951 according to the rules of that literal.
25955 The content of the output is ignored, but still captured in the test
25956 group log (if the testsuite is run with the @option{-v} option, the test
25957 group log is displayed as the test is run; if the test group later
25958 fails, the test group log is also copied into the overall testsuite
25959 log). This action is valid for both @var{stdout} and @var{stderr}.
25962 The content of the output is ignored, and nothing is captured in the log
25963 files. If @var{commands} are likely to produce binary output (including
25964 long lines) or large amounts of output, then logging the output can make
25965 it harder to locate details related to subsequent tests within the
25966 group, and could potentially corrupt terminal display of a user running
25967 @command{testsuite -v}. This action is valid for both @var{stdout} and
25971 Only valid as the @var{stdout} parameter. Capture the content of
25972 standard output in both a file named @file{stdout} and the test group log.
25973 Subsequent commands in the test group can then post-process the file.
25974 This action is often used when it is desired to use @command{grep} to
25975 look for a substring in the output, or when the output must be
25976 post-processed to normalize error messages into a common form.
25979 Only valid as the @var{stderr} parameter. Capture the content of
25980 standard error in both a file named @file{stderr} and the test group log.
25983 @itemx stderr-nolog
25984 Like @samp{stdout} or @samp{stderr}, except that the captured output is
25985 not duplicated into the test group log. This action is particularly
25986 useful for an intermediate check that produces large amounts of data,
25987 which will be followed by another check that filters down to the
25988 relevant data, as it makes it easier to locate details in the log.
25991 Only valid as the @var{stdout} parameter. Compare standard output with
25992 the previously created file @file{expout}, and list any differences in
25996 Only valid as the @var{stderr} parameter. Compare standard error with
25997 the previously created file @file{experr}, and list any differences in
26001 Otherwise, the values of the @var{stdout} and @var{stderr} parameters
26002 are treated as text that must exactly match the output given by
26003 @var{commands} on standard output and standard error (including an empty
26004 parameter for no output); any differences are captured in the testsuite
26005 log and the test is failed (unless an unexpected exit status of 77
26006 skipped the test instead).
26008 @code{AT_CHECK_UNQUOTED} performs shell variable expansion (@samp{$}),
26009 command substitution (@samp{`}), and backslash escaping (@samp{\}) on
26010 comparison text given in the @var{stdout} and @var{stderr} parameters;
26011 @code{AT_CHECK} does not. There is no difference in the interpretation
26015 @defmac AT_CHECK_EUNIT (@var{module}, @var{test-spec}, @ovar{erlflags}, @
26016 @ovar{run-if-fail}, @ovar{run-if-pass})
26017 @atindex{CHECK_EUNIT}
26018 Initialize and execute an Erlang module named @var{module} that performs
26019 tests following the @var{test-spec} EUnit test specification.
26020 @var{test-spec} must be a valid EUnit test specification, as defined in
26021 the @uref{https://@/erlang.org/@/doc/@/apps/@/eunit/@/index.html, EUnit
26022 Reference Manual}. @var{erlflags} are optional command-line options
26023 passed to the Erlang interpreter to execute the test Erlang module.
26024 Typically, @var{erlflags} defines at least the paths to directories
26025 containing the compiled Erlang modules under test, as @samp{-pa path1
26028 For example, the unit tests associated with Erlang module @samp{testme},
26029 which compiled code is in subdirectory @file{src}, can be performed
26033 AT_CHECK_EUNIT([testme_testsuite], [@{module, testme@}],
26034 [-pa "$@{abs_top_builddir@}/src"])
26037 This macro must be invoked in between @code{AT_SETUP} and @code{AT_CLEANUP}.
26039 Variables @code{ERL}, @code{ERLC}, and (optionally) @code{ERLCFLAGS}
26040 must be defined as the path of the Erlang interpreter, the path of the
26041 Erlang compiler, and the command-line flags to pass to the compiler,
26042 respectively. Those variables should be configured in
26043 @file{configure.ac} using the @command{AC_ERLANG_PATH_ERL} and
26044 @command{AC_ERLANG_PATH_ERLC} macros, and the configured values of those
26045 variables are automatically defined in the testsuite. If @code{ERL} or
26046 @code{ERLC} is not defined, the test group is skipped.
26048 If the EUnit library cannot be found, i.e. if module @code{eunit} cannot
26049 be loaded, the test group is skipped. Otherwise, if @var{test-spec} is
26050 an invalid EUnit test specification, the test group fails. Otherwise,
26051 if the EUnit test passes, shell commands @var{run-if-pass} are executed
26052 or, if the EUnit test fails, shell commands @var{run-if-fail} are
26053 executed and the test group fails.
26055 Only the generated test Erlang module is automatically compiled and
26056 executed. If @var{test-spec} involves testing other Erlang modules,
26057 e.g. module @samp{testme} in the example above, those modules must be
26060 If the testsuite is run in verbose mode and with the @option{--verbose} option,
26061 EUnit is also run in verbose mode to output more details about
26062 individual unit tests.
26066 @node testsuite Invocation
26067 @section Running @command{testsuite} Scripts
26068 @cindex @command{testsuite}
26070 Autotest test suites support the following options:
26075 Display the list of options and exit successfully.
26079 Display the version of the test suite and exit successfully.
26081 @item --directory=@var{dir}
26082 @itemx -C @var{dir}
26083 Change the current directory to @var{dir} before creating any files.
26084 Useful for running the testsuite in a subdirectory from a top-level
26087 @item --jobs@r{[}=@var{n}@r{]}
26089 Run @var{n} tests in parallel, if possible. If @var{n} is not given,
26090 run all given tests in parallel. Note that there should be no space
26091 before the argument to @option{-j}, as @option{-j @var{number}} denotes
26092 the separate arguments @option{-j} and @option{@var{number}}, see below.
26094 In parallel mode, the standard input device of the testsuite script is
26095 not available to commands inside a test group. Furthermore, banner
26096 lines are not printed, and the summary line for each test group is
26097 output after the test group completes. Summary lines may appear
26098 unordered. If verbose and trace output are enabled (see below), they
26099 may appear intermixed from concurrently running tests.
26101 Parallel mode requires the @command{mkfifo} command to work, and will be
26102 silently disabled otherwise.
26106 Remove all the files the test suite might have created and exit. Meant
26107 for @code{clean} Make targets.
26111 List all the tests (or only the selection), including their possible
26117 By default all tests are performed (or described with @option{--list})
26118 silently in the default environment, but the environment, set of tests,
26119 and verbosity level can be tuned:
26122 @item @var{variable}=@var{value}
26123 Set the environment @var{variable} to @var{value}. Use this rather
26124 than @samp{FOO=foo ./testsuite} as debugging scripts would then run in a
26125 different environment.
26127 @cindex @code{AUTOTEST_PATH}
26128 The variable @code{AUTOTEST_PATH} specifies the testing path to prepend
26129 to @env{PATH}. Relative directory names (not starting with
26130 @samp{/}) are considered to be relative to the top level of the
26131 package being built. All directories are made absolute, first
26132 starting from the top level @emph{build} tree, then from the
26133 @emph{source} tree. For instance @samp{./testsuite
26134 AUTOTEST_PATH=tests:bin} for a @file{/src/foo-1.0} source package built
26135 in @file{/tmp/foo} results in @samp{/tmp/foo/tests:/tmp/foo/bin} and
26136 then @samp{/src/foo-1.0/tests:/src/foo-1.0/bin} being prepended to
26140 @itemx @var{number}-@var{number}
26141 @itemx @var{number}-
26142 @itemx -@var{number}
26143 Add the corresponding test groups, with obvious semantics, to the
26146 @item --keywords=@var{keywords}
26147 @itemx -k @var{keywords}
26148 Add to the selection the test groups with title or keywords (arguments
26149 to @code{AT_SETUP} or @code{AT_KEYWORDS}) that match @emph{all} keywords
26150 of the comma separated list @var{keywords}, case-insensitively. Use
26151 @samp{!} immediately before the keyword to invert the selection for this
26152 keyword. By default, the keywords match whole words; enclose them in
26153 @samp{.*} to also match parts of words.
26155 For example, running
26158 @kbd{./testsuite -k 'autoupdate,.*FUNC.*'}
26162 selects all tests tagged @samp{autoupdate} @emph{and} with tags
26163 containing @samp{FUNC} (as in @samp{AC_CHECK_FUNC}, @samp{AC_FUNC_ALLOCA},
26167 @kbd{./testsuite -k '!autoupdate' -k '.*FUNC.*'}
26171 selects all tests not tagged @samp{autoupdate} @emph{or} with tags
26172 containing @samp{FUNC}.
26176 If any test fails, immediately abort testing. This implies
26177 @option{--debug}: post test group clean up, and top-level logging
26178 are inhibited. This option is meant for the full test
26179 suite, it is not really useful for generated debugging scripts.
26180 If the testsuite is run in parallel mode using @option{--jobs},
26181 then concurrently running tests will finish before exiting.
26185 Force more verbosity in the detailed output of what is being done. This
26186 is the default for debugging scripts.
26189 @itemx --color@r{[}=never@r{|}auto@r{|}always@r{]}
26190 Enable colored test results. Without an argument, or with @samp{always},
26191 test results will be colored. With @samp{never}, color mode is turned
26192 off. Otherwise, if either the macro @code{AT_COLOR_TESTS} is used by
26193 the testsuite author, or the argument @samp{auto} is given, then test
26194 results are colored if standard output is connected to a terminal.
26198 Do not remove the files after a test group was performed---but they are
26199 still removed @emph{before}, therefore using this option is sane when
26200 running several test groups. Create debugging scripts. Do not
26201 overwrite the top-level
26202 log (in order to preserve a supposedly existing full log file). This is
26203 the default for debugging scripts, but it can also be useful to debug
26204 the testsuite itself.
26207 Add to the selection all test groups that failed or passed unexpectedly
26208 during the last non-debugging test run.
26212 Trigger shell tracing of the test groups.
26215 Besides these options accepted by every Autotest testsuite, the
26216 testsuite author might have added package-specific options
26217 via the @code{AT_ARG_OPTION} and @code{AT_ARG_OPTION_ARG} macros
26218 (@pxref{Writing Testsuites}); refer to @command{testsuite --help} and
26219 the package documentation for details.
26222 @node Making testsuite Scripts
26223 @section Making @command{testsuite} Scripts
26225 For putting Autotest into movement, you need some configuration and
26226 makefile machinery. We recommend, at least if your package uses deep or
26227 shallow hierarchies, that you use @file{tests/} as the name of the
26228 directory holding all your tests and their makefile. Here is a
26229 check list of things to do, followed by an example, taking into
26230 consideration whether you are also using Automake.
26235 @cindex @file{package.m4}
26236 @atindex{PACKAGE_STRING}
26237 @atindex{PACKAGE_BUGREPORT}
26238 @atindex{PACKAGE_NAME}
26239 @atindex{PACKAGE_TARNAME}
26240 @atindex{PACKAGE_VERSION}
26241 @atindex{PACKAGE_URL}
26242 Make sure to create the file @file{package.m4}, which defines the
26243 identity of the package. It must define @code{AT_PACKAGE_STRING}, the
26244 full signature of the package, and @code{AT_PACKAGE_BUGREPORT}, the
26245 address to which bug reports should be sent. For sake of completeness,
26246 we suggest that you also define @code{AT_PACKAGE_NAME},
26247 @code{AT_PACKAGE_TARNAME}, @code{AT_PACKAGE_VERSION}, and
26248 @code{AT_PACKAGE_URL}.
26249 @xref{Initializing configure}, for a description of these variables.
26250 Be sure to distribute @file{package.m4} and to put it into the source
26251 hierarchy: the test suite ought to be shipped! See below for an example.
26254 Invoke @code{AC_CONFIG_TESTDIR} in your @file{configure.ac}.
26256 @defmac AC_CONFIG_TESTDIR (@var{directory}, @dvarv{test-path, directory})
26257 @acindex{CONFIG_TESTDIR}
26258 An Autotest test suite is to be configured in @var{directory}. This
26259 macro causes @file{@var{directory}/atconfig} to be created by
26260 @command{config.status} and sets the default @code{AUTOTEST_PATH} to
26261 @var{test-path} (@pxref{testsuite Invocation}).
26265 Still within @file{configure.ac}, as appropriate, ensure that some
26266 @code{AC_CONFIG_FILES} command includes substitution for
26267 @file{tests/atlocal}.
26270 Also within your @file{configure.ac}, arrange for the @code{AUTOM4TE}
26271 variable to be set.
26274 The appropriate @file{Makefile} should be modified so the validation in
26275 your package is triggered by @samp{make check}.
26278 The following example demonstrates the above checklist, first by
26279 assuming that you are using Automake (see below for tweaks to make to
26280 get the same results without Automake). Begin by adding the following
26281 lines to your @file{configure.ac}:
26284 # Initialize the test suite.
26285 AC_CONFIG_TESTDIR([tests])
26286 AC_CONFIG_FILES([tests/Makefile tests/atlocal])
26287 AM_MISSING_PROG([AUTOM4TE], [autom4te])
26290 Next, add the following lines to your @file{tests/Makefile.am}, in order
26291 to link @samp{make check} with a validation suite.
26294 # The ':;' works around a Bash 3.2 bug when the output is not writable.
26295 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
26297 echo '# Signature of the current package.' && \
26298 echo 'm4_define([AT_PACKAGE_NAME],' && \
26299 echo ' [$(PACKAGE_NAME)])' && \
26300 echo 'm4_define([AT_PACKAGE_TARNAME],' && \
26301 echo ' [$(PACKAGE_TARNAME)])' && \
26302 echo 'm4_define([AT_PACKAGE_VERSION],' && \
26303 echo ' [$(PACKAGE_VERSION)])' && \
26304 echo 'm4_define([AT_PACKAGE_STRING],' && \
26305 echo ' [$(PACKAGE_STRING)])' && \
26306 echo 'm4_define([AT_PACKAGE_BUGREPORT],' && \
26307 echo ' [$(PACKAGE_BUGREPORT)])'; \
26308 echo 'm4_define([AT_PACKAGE_URL],' && \
26309 echo ' [$(PACKAGE_URL)])'; \
26310 @} >'$(srcdir)/package.m4'
26312 EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) atlocal.in
26313 TESTSUITE = $(srcdir)/testsuite
26315 check-local: atconfig atlocal $(TESTSUITE)
26316 $(SHELL) '$(TESTSUITE)' $(TESTSUITEFLAGS)
26318 installcheck-local: atconfig atlocal $(TESTSUITE)
26319 $(SHELL) '$(TESTSUITE)' AUTOTEST_PATH='$(bindir)' \
26323 test ! -f '$(TESTSUITE)' || \
26324 $(SHELL) '$(TESTSUITE)' --clean
26326 AUTOTEST = $(AUTOM4TE) --language=autotest
26327 $(TESTSUITE): $(srcdir)/testsuite.at $(srcdir)/package.m4
26328 $(AUTOTEST) -I '$(srcdir)' -o $@@.tmp $@@.at
26332 Note that the built testsuite is distributed; this is necessary because
26333 users might not have Autoconf installed, and thus would not be able to
26334 rebuild it. Likewise, the use of Automake's @code{AM_MISSING_PROG} will
26335 arrange for the definition of @code{$AUTOM4TE} within the Makefile to
26336 provide the user with
26337 a nicer error message if they modify a source file to the testsuite, and
26338 accidentally trigger the rebuild rules.
26340 You might want to list explicitly the dependencies, i.e., the list of
26341 the files @file{testsuite.at} includes.
26343 If you don't use Automake, you should make the following tweaks. In
26344 your @file{configure.ac}, replace the @code{AM_MISSING_PROG} line above
26345 with @code{AC_PATH_PROG([AUTOM4TE], [autom4te], [false])}. You are
26346 welcome to also try using the @command{missing} script from the Automake
26347 project instead of @command{false}, to try to get a nicer error message
26348 when the user modifies prerequisites but did not have Autoconf
26349 installed, but at that point you may be better off using Automake.
26350 Then, take the code suggested above for @file{tests/@/Makefile.am} and
26351 place it in your @file{tests/@/Makefile.in} instead. Add code to your
26352 @file{tests/@/Makefile.in} to ensure that @code{$(EXTRA_DIST)} files are
26353 distributed, as well as adding the following additional lines to prepare
26354 the set of needed Makefile variables:
26358 PACKAGE_NAME = @@PACKAGE_NAME@@
26359 PACKAGE_TARNAME = @@PACKAGE_TARNAME@@
26360 PACKAGE_VERSION = @@PACKAGE_VERSION@@
26361 PACKAGE_STRING = @@PACKAGE_STRING@@
26362 PACKAGE_BUGREPORT = @@PACKAGE_BUGREPORT@@
26363 PACKAGE_URL = @@PACKAGE_URL@@
26364 AUTOM4TE = @@AUTOM4TE@@
26366 atconfig: $(top_builddir)/config.status
26367 cd $(top_builddir) && \
26368 $(SHELL) ./config.status $(subdir)/$@@
26370 atlocal: $(srcdir)/atlocal.in $(top_builddir)/config.status
26371 cd $(top_builddir) && \
26372 $(SHELL) ./config.status $(subdir)/$@@
26375 Using the above example (with or without Automake), and assuming you
26376 were careful to not initialize @samp{TESTSUITEFLAGS} within your
26377 makefile, you can now fine-tune test suite execution at runtime by
26378 altering this variable, for example:
26381 make check TESTSUITEFLAGS='-v -d -x 75 -k AC_PROG_CC CFLAGS=-g'
26386 @c =============================== Frequent Autoconf Questions, with answers
26389 @chapter Frequent Autoconf Questions, with answers
26391 Several questions about Autoconf come up occasionally. Here some of them
26395 * Distributing:: Distributing @command{configure} scripts
26396 * Why GNU M4:: Why not use the standard M4?
26397 * Bootstrapping:: Autoconf and GNU M4 require each other?
26398 * Why Not Imake:: Why GNU uses @command{configure} instead of Imake
26399 * Defining Directories:: Passing @code{datadir} to program
26400 * Autom4te Cache:: What is it? Can I remove it?
26401 * Present But Cannot Be Compiled:: Compiler and Preprocessor Disagree
26402 * Expanded Before Required:: Expanded Before Required
26403 * Debugging:: Debugging @command{configure} scripts
26407 @section Distributing @command{configure} Scripts
26411 What are the restrictions on distributing @command{configure}
26412 scripts that Autoconf generates? How does that affect my
26413 programs that use them?
26416 There are no restrictions on how the configuration scripts that Autoconf
26417 produces may be distributed or used. In Autoconf version 1, they were
26418 covered by the GNU General Public License. We still encourage
26419 software authors to distribute their work under terms like those of the
26420 GPL, but doing so is not required to use Autoconf.
26422 Of the other files that might be used with @command{configure},
26423 @file{config.h.in} is under whatever copyright you use for your
26424 @file{configure.ac}. @file{config.sub} and @file{config.guess} have an
26425 exception to the GPL when they are used with an Autoconf-generated
26426 @command{configure} script, which permits you to distribute them under the
26427 same terms as the rest of your package. @file{install-sh} is from the X
26428 Consortium and is not copyrighted.
26431 @section Why Require GNU M4?
26434 Why does Autoconf require GNU M4?
26437 Many M4 implementations have hard-coded limitations on the size and
26438 number of macros that Autoconf exceeds. They also lack several
26439 builtin macros that it would be difficult to get along without in a
26440 sophisticated application like Autoconf, including:
26450 Autoconf requires version 1.4.8 or later of GNU M4.
26451 It works better with version 1.4.16 or later.
26453 Since only software maintainers need to use Autoconf, and since GNU
26454 M4 is simple to configure and install, it seems reasonable to require
26455 GNU M4 to be installed also. Many maintainers of GNU and
26456 other free software already have most of the GNU utilities
26457 installed, since they prefer them.
26459 @node Bootstrapping
26460 @section How Can I Bootstrap?
26464 If Autoconf requires GNU M4 and GNU M4 has an Autoconf
26465 @command{configure} script, how do I bootstrap? It seems like a chicken
26469 This is a misunderstanding. Although GNU M4 does come with a
26470 @command{configure} script produced by Autoconf, Autoconf is not required
26471 in order to run the script and install GNU M4. Autoconf is only
26472 required if you want to change the M4 @command{configure} script, which few
26473 people have to do (mainly its maintainer).
26475 @node Why Not Imake
26476 @section Why Not Imake?
26480 Why not use Imake instead of @command{configure} scripts?
26483 Several people have written addressing this question, so
26484 adaptations of their explanations are included here.
26486 The following answer is based on one written by Richard Pixley:
26489 Autoconf generated scripts frequently work on machines that it has
26490 never been set up to handle before. That is, it does a good job of
26491 inferring a configuration for a new system. Imake cannot do this.
26493 Imake uses a common database of host specific data. For X11, this makes
26494 sense because the distribution is made as a collection of tools, by one
26495 central authority who has control over the database.
26497 GNU tools are not released this way. Each GNU tool has a
26498 maintainer; these maintainers are scattered across the world. Using a
26499 common database would be a maintenance nightmare. Autoconf may appear
26500 to be this kind of database, but in fact it is not. Instead of listing
26501 host dependencies, it lists program requirements.
26503 If you view the GNU suite as a collection of native tools, then the
26504 problems are similar. But the GNU development tools can be
26505 configured as cross tools in almost any host+target permutation. All of
26506 these configurations can be installed concurrently. They can even be
26507 configured to share host independent files across hosts. Imake doesn't
26508 address these issues.
26510 Imake templates are a form of standardization. The GNU coding
26511 standards address the same issues without necessarily imposing the same
26516 Here is some further explanation, written by Per Bothner:
26519 One of the advantages of Imake is that it is easy to generate large
26520 makefiles using the @samp{#include} and macro mechanisms of @command{cpp}.
26521 However, @code{cpp} is not programmable: it has limited conditional
26522 facilities, and no looping. And @code{cpp} cannot inspect its
26525 All of these problems are solved by using @code{sh} instead of
26526 @code{cpp}. The shell is fully programmable, has macro substitution,
26527 can execute (or source) other shell scripts, and can inspect its
26532 Paul Eggert elaborates more:
26535 With Autoconf, installers need not assume that Imake itself is already
26536 installed and working well. This may not seem like much of an advantage
26537 to people who are accustomed to Imake. But on many hosts Imake is not
26538 installed or the default installation is not working well, and requiring
26539 Imake to install a package hinders the acceptance of that package on
26540 those hosts. For example, the Imake template and configuration files
26541 might not be installed properly on a host, or the Imake build procedure
26542 might wrongly assume that all source files are in one big directory
26543 tree, or the Imake configuration might assume one compiler whereas the
26544 package or the installer needs to use another, or there might be a
26545 version mismatch between the Imake expected by the package and the Imake
26546 supported by the host. These problems are much rarer with Autoconf,
26547 where each package comes with its own independent configuration
26550 Also, Imake often suffers from unexpected interactions between
26551 @command{make} and the installer's C preprocessor. The fundamental problem
26552 here is that the C preprocessor was designed to preprocess C programs,
26553 not makefiles. This is much less of a problem with Autoconf,
26554 which uses the general-purpose preprocessor M4, and where the
26555 package's author (rather than the installer) does the preprocessing in a
26560 Finally, Mark Eichin notes:
26563 Imake isn't all that extensible, either. In order to add new features to
26564 Imake, you need to provide your own project template, and duplicate most
26565 of the features of the existing one. This means that for a sophisticated
26566 project, using the vendor-provided Imake templates fails to provide any
26567 leverage---since they don't cover anything that your own project needs
26568 (unless it is an X11 program).
26570 On the other side, though:
26572 The one advantage that Imake has over @command{configure}:
26573 @file{Imakefile} files tend to be much shorter (likewise, less redundant)
26574 than @file{Makefile.in} files. There is a fix to this, however---at least
26575 for the Kerberos V5 tree, we've modified things to call in common
26576 @file{post.in} and @file{pre.in} makefile fragments for the
26577 entire tree. This means that a lot of common things don't have to be
26578 duplicated, even though they normally are in @command{configure} setups.
26582 @node Defining Directories
26583 @section How Do I @code{#define} Installation Directories?
26586 My program needs library files, installed in @code{datadir} and
26590 AC_DEFINE_UNQUOTED([DATADIR], [$datadir],
26591 [Define to the read-only architecture-independent
26599 #define DATADIR "$@{prefix@}/share"
26603 As already explained, this behavior is on purpose, mandated by the
26604 GNU Coding Standards, see @ref{Installation Directory
26605 Variables}. There are several means to achieve a similar goal:
26609 Do not use @code{AC_DEFINE} but use your makefile to pass the
26610 actual value of @code{datadir} via compilation flags.
26611 @xref{Installation Directory Variables}, for the details.
26614 This solution can be simplified when compiling a program: you may either
26615 extend the @code{CPPFLAGS}:
26618 CPPFLAGS = -DDATADIR='"$(datadir)"' @@CPPFLAGS@@
26622 If you are using Automake, you should use @code{AM_CPPFLAGS} instead:
26625 AM_CPPFLAGS = -DDATADIR='"$(datadir)"'
26629 Alternatively, create a dedicated header file:
26632 DISTCLEANFILES = myprog-paths.h
26633 myprog-paths.h: Makefile
26634 echo '#define DATADIR "$(datadir)"' >$@@
26638 The Gnulib module @samp{configmake} provides such a header with all the
26639 standard directory variables defined, @pxref{configmake,,, gnulib, GNU
26643 Use @code{AC_DEFINE} but have @command{configure} compute the literal
26644 value of @code{datadir} and others. Many people have wrapped macros to
26645 automate this task; for an example, see the macro @code{AC_DEFINE_DIR} from
26646 the @uref{https://@/www.gnu.org/@/software/@/autoconf-archive/, Autoconf Macro
26649 This solution does not conform to the GNU Coding Standards.
26652 Note that all the previous solutions hard wire the absolute name of
26653 these directories in the executables, which is not a good property. You
26654 may try to compute the names relative to @code{prefix}, and try to
26655 find @code{prefix} at runtime, this way your package is relocatable.
26659 @node Autom4te Cache
26660 @section What is @file{autom4te.cache}?
26663 What is this directory @file{autom4te.cache}? Can I safely remove it?
26666 In the GNU Build System, @file{configure.ac} plays a central
26667 role and is read by many tools: @command{autoconf} to create
26668 @file{configure}, @command{autoheader} to create @file{config.h.in},
26669 @command{automake} to create @file{Makefile.in}, @command{autoscan} to
26670 check the completeness of @file{configure.ac}, @command{autoreconf} to
26671 check the GNU Build System components that are used. To
26672 ``read @file{configure.ac}'' actually means to compile it with M4,
26673 which can be a long process for complex @file{configure.ac}.
26675 This is why all these tools, instead of running directly M4, invoke
26676 @command{autom4te} (@pxref{autom4te Invocation}) which, while answering to
26677 a specific demand, stores additional information in
26678 @file{autom4te.cache} for future runs. For instance, if you run
26679 @command{autoconf}, behind the scenes, @command{autom4te} also
26680 stores information for the other tools, so that when you invoke
26681 @command{autoheader} or @command{automake} etc., reprocessing
26682 @file{configure.ac} is not needed. The speed up is frequently 30%,
26683 and is increasing with the size of @file{configure.ac}.
26685 But it is and remains being simply a cache: you can safely remove it.
26690 Can I permanently get rid of it?
26693 The creation of this cache can be disabled from
26694 @file{~/.autom4te.cfg}, see @ref{Customizing autom4te}, for more
26695 details. You should be aware that disabling the cache slows down the
26696 Autoconf test suite by 40%. The more GNU Build System
26697 components are used, the more the cache is useful; for instance
26698 running @samp{autoreconf -f} on the Core Utilities is twice slower without
26699 the cache @emph{although @option{--force} implies that the cache is
26700 not fully exploited}, and eight times slower than without
26704 @node Present But Cannot Be Compiled
26705 @section Header Present But Cannot Be Compiled
26707 The most important guideline to bear in mind when checking for
26708 features is to mimic as much as possible the intended use.
26709 Unfortunately, old versions of @code{AC_CHECK_HEADER} and
26710 @code{AC_CHECK_HEADERS} failed to follow this idea, and called
26711 the preprocessor, instead of the compiler, to check for headers. As a
26712 result, incompatibilities between headers went unnoticed during
26713 configuration, and maintainers finally had to deal with this issue
26716 The transition began with Autoconf 2.56. As of Autoconf 2.64 both
26717 checks are performed, and @command{configure} complains loudly if the
26718 compiler and the preprocessor do not agree. However, only the compiler
26719 result is considered. As of Autoconf 2.70, only the compiler check is
26722 Consider the following example:
26725 $ @kbd{cat number.h}
26726 typedef int number;
26728 const number pi = 3;
26729 $ @kbd{cat configure.ac}
26730 AC_INIT([Example], [1.0], [bug-example@@example.org])
26731 AC_CHECK_HEADERS([pi.h])
26732 $ @kbd{autoconf -Wall}
26733 $ @kbd{./configure CPPFLAGS='-I.'}
26734 checking for gcc... gcc
26735 checking whether the C compiler works... yes
26736 checking for C compiler default output file name... a.out
26737 checking for suffix of executables...
26738 checking whether we are cross compiling... no
26739 checking for suffix of object files... o
26740 checking whether the compiler supports GNU C... yes
26741 checking whether gcc accepts -g... yes
26742 checking for gcc option to enable C11 features... -std=gnu11
26743 checking for sys/types.h... yes
26744 checking for sys/stat.h... yes
26745 checking for strings.h... yes
26746 checking for inttypes.h... yes
26747 checking for stdint.h... yes
26748 checking for unistd.h... yes
26749 checking for pi.h... no
26753 The proper way to handle this case is using the fourth argument
26754 (@pxref{Generic Headers}):
26757 $ @kbd{cat configure.ac}
26758 AC_INIT([Example], [1.0], [bug-example@@example.org])
26759 AC_CHECK_HEADERS([number.h pi.h], [], [],
26760 [[#ifdef HAVE_NUMBER_H
26761 # include <number.h>
26764 $ @kbd{autoconf -Wall}
26765 $ @kbd{./configure CPPFLAGS='-I.'}
26766 checking for gcc... gcc
26767 checking whether the C compiler works... yes
26768 checking for C compiler default output file name... a.out
26769 checking for suffix of executables...
26770 checking whether we are cross compiling... no
26771 checking for suffix of object files... o
26772 checking whether the compiler supports GNU C... yes
26773 checking whether gcc accepts -g... yes
26774 checking for gcc option to enable C11 features... -std=gnu11
26775 checking for number.h... yes
26776 checking for pi.h... yes
26779 See @ref{Particular Headers}, for a list of headers with their
26782 @node Expanded Before Required
26783 @section Expanded Before Required
26785 @cindex expanded before required
26786 Older versions of Autoconf silently built files with incorrect ordering
26787 between dependent macros if an outer macro first expanded, then later
26788 indirectly required, an inner macro. Starting with Autoconf 2.64, this
26789 situation no longer generates out-of-order code, but results in
26790 duplicate output and a syntax warning:
26793 $ @kbd{cat configure.ac}
26794 @result{}AC_DEFUN([TESTA], [[echo in A
26795 @result{}if test -n "$SEEN_A" ; then echo duplicate ; fi
26796 @result{}SEEN_A=:]])
26797 @result{}AC_DEFUN([TESTB], [AC_REQUIRE([TESTA])[echo in B
26798 @result{}if test -z "$SEEN_A" ; then echo bug ; fi]])
26799 @result{}AC_DEFUN([TESTC], [AC_REQUIRE([TESTB])[echo in C]])
26800 @result{}AC_DEFUN([OUTER], [[echo in OUTER]
26807 @result{}configure.ac:11: warning: AC_REQUIRE:
26808 @result{} 'TESTA' was expanded before it was required
26809 @result{}configure.ac:4: TESTB is expanded from...
26810 @result{}configure.ac:6: TESTC is expanded from...
26811 @result{}configure.ac:7: OUTER is expanded from...
26812 @result{}configure.ac:11: the top level
26816 To avoid this warning, decide what purpose the macro in question serves.
26817 If it only needs to be expanded once (for example, if it provides
26818 initialization text used by later macros), then the simplest fix is to
26819 change the macro to be declared with @code{AC_DEFUN_ONCE}
26820 (@pxref{One-Shot Macros}), although this only works in Autoconf 2.64 and
26821 newer. A more portable fix is to change all
26822 instances of direct calls to instead go through @code{AC_REQUIRE}
26823 (@pxref{Prerequisite Macros}). If, instead, the macro is parameterized
26824 by arguments or by the current definition of other macros in the m4
26825 environment, then the macro should always be directly expanded instead
26828 For another case study, consider this example trimmed down from an
26829 actual package. Originally, the package contained shell code and
26830 multiple macro invocations at the top level of @file{configure.ac}:
26833 AC_DEFUN([FOO], [AC_COMPILE_IFELSE([@dots{}])])
26840 but that was getting complex, so the author wanted to offload some of
26841 the text into a new macro in another file included via
26842 @file{aclocal.m4}. The naïve approach merely wraps the text in a new
26846 AC_DEFUN([FOO], [AC_COMPILE_IFELSE([@dots{}])])
26856 With older versions of Autoconf, the setting of @samp{foobar=} occurs
26857 before the single compiler check, as the author intended. But with
26858 Autoconf 2.64, this issues the ``expanded before it was required''
26859 warning for @code{AC_PROG_CC}, and outputs two copies of the compiler
26860 check, one before @samp{foobar=}, and one after. To understand why this
26861 is happening, remember that the use of @code{AC_COMPILE_IFELSE} includes
26862 a call to @code{AC_REQUIRE([AC_PROG_CC])} under the hood. According to
26863 the documented semantics of @code{AC_REQUIRE}, this means that
26864 @code{AC_PROG_CC} @emph{must} occur before the body of the outermost
26865 @code{AC_DEFUN}, which in this case is @code{BAR}, thus preceding the
26866 use of @samp{foobar=}. The older versions of Autoconf were broken with
26867 regards to the rules of @code{AC_REQUIRE}, which explains why the code
26868 changed from one over to two copies of @code{AC_PROG_CC} when upgrading
26869 autoconf. In other words, the author was unknowingly relying on a bug
26870 exploit to get the desired results, and that exploit broke once the bug
26873 So, what recourse does the author have, to restore their intended
26874 semantics of setting @samp{foobar=} prior to a single compiler check,
26875 regardless of whether Autoconf 2.63 or 2.64 is used? One idea is to
26876 remember that only @code{AC_DEFUN} is impacted by @code{AC_REQUIRE};
26877 there is always the possibility of using the lower-level
26881 AC_DEFUN([FOO], [AC_COMPILE_IFELSE([@dots{}])])
26891 This works great if everything is in the same file. However, it does
26892 not help in the case where the author wants to have @command{aclocal}
26893 find the definition of @code{BAR} from its own file, since
26894 @command{aclocal} requires the use of @code{AC_DEFUN}. In this case, a
26895 better fix is to recognize that if @code{BAR} also uses
26896 @code{AC_REQUIRE}, then there will no longer be direct expansion prior
26897 to a subsequent require. Then, by creating yet another helper macro,
26898 the author can once again guarantee a single invocation of
26899 @code{AC_PROG_CC}, which will still occur after @code{foobar=}. The
26900 author can also use @code{AC_BEFORE} to make sure no other macro
26901 appearing before @code{BAR} has triggered an unwanted expansion of
26905 AC_DEFUN([FOO], [AC_COMPILE_IFELSE([@dots{}])])
26906 AC_DEFUN([BEFORE_CC], [
26910 AC_BEFORE([$0], [AC_PROG_CC])dnl
26911 AC_REQUIRE([BEFORE_CC])dnl
26912 AC_REQUIRE([AC_PROG_CC])dnl
26920 @section Debugging @command{configure} scripts
26922 While in general, @command{configure} scripts generated by Autoconf
26923 strive to be fairly portable to various systems, compilers, shells, and
26924 other tools, it may still be necessary to debug a failing test, broken
26925 script or makefile, or fix or override an incomplete, faulty, or erroneous
26926 test, especially during macro development. Failures can occur at all levels,
26927 in M4 syntax or semantics, shell script issues, or due to bugs in the
26928 test or the tools invoked by @command{configure}. Together with the
26929 rather arcane error message that @command{m4} and @command{make} may
26930 produce when their input contains syntax errors, this can make debugging
26933 Nevertheless, here is a list of hints and strategies that may help:
26937 When @command{autoconf} fails, common causes for error include:
26941 mismatched or unbalanced parentheses or braces (@pxref{Balancing
26944 @item under- or over-quoted macro arguments (@pxref{Autoconf
26945 Language}, @pxref{Quoting and Parameters}, @pxref{Quotation and Nested
26948 @item spaces between macro name and opening parenthesis (@pxref{Autoconf
26952 Typically, it helps to go back to the last working version of the input
26953 and compare the differences for each of these errors. Another
26954 possibility is to sprinkle pairs of @code{m4_traceon} and
26955 @code{m4_traceoff} judiciously in the code, either without a parameter
26956 or listing some macro names and watch @command{m4} expand its input
26957 verbosely (@pxref{Debugging via autom4te}).
26960 Sometimes @command{autoconf} succeeds but the generated
26961 @command{configure} script has invalid shell syntax. You can detect this
26962 case by running @samp{bash -n configure} or @samp{sh -n configure}.
26963 If this command fails, the same tips apply, as if @command{autoconf} had
26967 Debugging @command{configure} script execution may be done by sprinkling
26968 pairs of @code{set -x} and @code{set +x} into the shell script before
26969 and after the region that contains a bug. Running the whole script with
26970 @samp{@var{shell} -vx ./configure 2>&1 | tee @var{log-file}} with a decent
26971 @var{shell} may work, but produces lots of output. Here, it can help to
26972 search for markers like @samp{checking for} a particular test in the
26976 Alternatively, you might use a shell with debugging capabilities like
26977 @uref{http://bashdb.sourceforge.net/, bashdb}.
26980 When @command{configure} tests produce invalid results for your system,
26981 it may be necessary to override them:
26985 For programs, tools or libraries variables, preprocessor, compiler, or
26986 linker flags, it is often sufficient to override them at @command{make}
26987 run time with some care (@pxref{Macros and Submakes}). Since this
26988 normally won't cause @command{configure} to be run again with these
26989 changed settings, it may fail if the changed variable would have caused
26990 different test results from @command{configure}, so this may work only
26991 for simple differences.
26994 Most tests which produce their result in a substituted variable allow to
26995 override the test by setting the variable on the @command{configure}
26996 command line (@pxref{Compilers and Options}, @pxref{Defining Variables}).
26999 Many tests store their result in a cache variable (@pxref{Caching
27000 Results}). This lets you override them either on the
27001 @command{configure} command line as above, or through a primed cache or
27002 site file (@pxref{Cache Files}, @pxref{Site Defaults}). The name of a
27003 cache variable is documented with a test macro or may be inferred from
27004 @ref{Cache Variable Names}; the precise semantics of undocumented
27005 variables are often internal details, subject to change.
27009 Alternatively, @command{configure} may produce invalid results because
27010 of uncaught programming errors, in your package or in an upstream
27011 library package. For example, when @code{AC_CHECK_LIB} fails to find a
27012 library with a specified function, always check @file{config.log}. This
27013 will reveal the exact error that produced the failing result: the
27014 library linked by @code{AC_CHECK_LIB} probably has a fatal bug.
27017 Conversely, as macro author, you can make it easier for users of your
27022 by minimizing dependencies between tests and between test results as far
27026 by using @command{make} variables to factorize and allow
27027 override of settings at @command{make} run time,
27030 by honoring the GNU Coding Standards and not overriding flags
27031 reserved for the user except temporarily during @command{configure}
27035 by not requiring users of your macro to use the cache variables.
27036 Instead, expose the result of the test via @var{run-if-true} and
27037 @var{run-if-false} parameters. If the result is not a boolean,
27038 then provide it through documented shell variables.
27042 @c ===================================================== History of Autoconf.
27045 @chapter History of Autoconf
27046 @cindex History of autoconf
27048 @emph{This chapter was written by the original author, David MacKenzie.}
27050 You may be wondering, Why was Autoconf originally written? How did it
27051 get into its present form? (Why does it look like gorilla spit?) If
27052 you're not wondering, then this chapter contains no information useful
27053 to you, and you might as well skip it. If you @emph{are} wondering,
27054 then let there be light@enddots{}
27057 * Genesis:: Prehistory and naming of @command{configure}
27058 * Exodus:: The plagues of M4 and Perl
27059 * Leviticus:: The priestly code of portability arrives
27060 * Numbers:: Growth and contributors
27061 * Deuteronomy:: Approaching the promises of easy configuration
27067 In June 1991 I was maintaining many of the GNU utilities for the
27068 Free Software Foundation. As they were ported to more platforms and
27069 more programs were added, the number of @option{-D} options that users
27070 had to select in the makefile (around 20) became burdensome.
27071 Especially for me---I had to test each new release on a bunch of
27072 different systems. So I wrote a little shell script to guess some of
27073 the correct settings for the fileutils package, and released it as part
27074 of fileutils 2.0. That @command{configure} script worked well enough that
27075 the next month I adapted it (by hand) to create similar @command{configure}
27076 scripts for several other GNU utilities packages. Brian Berliner
27077 also adapted one of my scripts for his CVS revision control system.
27079 Later that summer, I learned that Richard Stallman and Richard Pixley
27080 were developing similar scripts to use in the GNU compiler tools;
27081 so I adapted my @command{configure} scripts to support their evolving
27082 interface: using the file name @file{Makefile.in} as the templates;
27083 adding @samp{+srcdir}, the first option (of many); and creating
27084 @file{config.status} files.
27089 As I got feedback from users, I incorporated many improvements, using
27090 Emacs to search and replace, cut and paste, similar changes in each of
27091 the scripts. As I adapted more GNU utilities packages to use
27092 @command{configure} scripts, updating them all by hand became impractical.
27093 Rich Murphey, the maintainer of the GNU graphics utilities, sent me
27094 mail saying that the @command{configure} scripts were great, and asking if
27095 I had a tool for generating them that I could send him. No, I thought,
27096 but I should! So I started to work out how to generate them. And the
27097 journey from the slavery of hand-written @command{configure} scripts to the
27098 abundance and ease of Autoconf began.
27100 Cygnus @command{configure}, which was being developed at around that time,
27101 is table driven; it is meant to deal mainly with a discrete number of
27102 system types with a small number of mainly unguessable features (such as
27103 details of the object file format). The automatic configuration system
27104 that Brian Fox had developed for Bash takes a similar approach. For
27105 general use, it seems to me a hopeless cause to try to maintain an
27106 up-to-date database of which features each variant of each operating
27107 system has. It's easier and more reliable to check for most features on
27108 the fly---especially on hybrid systems that people have hacked on
27109 locally or that have patches from vendors installed.
27111 I considered using an architecture similar to that of Cygnus
27112 @command{configure}, where there is a single @command{configure} script that
27113 reads pieces of @file{configure.in} when run. But I didn't want to have
27114 to distribute all of the feature tests with every package, so I settled
27115 on having a different @command{configure} made from each
27116 @file{configure.in} by a preprocessor. That approach also offered more
27117 control and flexibility.
27119 I looked briefly into using the Metaconfig package, by Larry Wall,
27120 Harlan Stenn, and Raphael Manfredi, but I decided not to for several
27121 reasons. The @command{Configure} scripts it produces are interactive,
27122 which I find quite inconvenient; I didn't like the ways it checked for
27123 some features (such as library functions); I didn't know that it was
27124 still being maintained, and the @command{Configure} scripts I had
27125 seen didn't work on many modern systems (such as System V R4 and NeXT);
27126 it wasn't flexible in what it could do in response to a feature's
27127 presence or absence; I found it confusing to learn; and it was too big
27128 and complex for my needs (I didn't realize then how much Autoconf would
27129 eventually have to grow).
27131 I considered using Perl to generate my style of @command{configure}
27132 scripts, but decided that M4 was better suited to the job of simple
27133 textual substitutions: it gets in the way less, because output is
27134 implicit. Plus, everyone already has it. (Initially I didn't rely on
27135 the GNU extensions to M4.) Also, some of my friends at the
27136 University of Maryland had recently been putting M4 front ends on
27137 several programs, including @code{tvtwm}, and I was interested in trying
27138 out a new language.
27143 Since my @command{configure} scripts determine the system's capabilities
27144 automatically, with no interactive user intervention, I decided to call
27145 the program that generates them Autoconfig. But with a version number
27146 tacked on, that name would be too long for old Unix file systems,
27147 so I shortened it to Autoconf.
27149 In the fall of 1991 I called together a group of fellow questers after
27150 the Holy Grail of portability (er, that is, alpha testers) to give me
27151 feedback as I encapsulated pieces of my handwritten scripts in M4 macros
27152 and continued to add features and improve the techniques used in the
27153 checks. Prominent among the testers were François Pinard, who came up
27154 with the idea of making an Autoconf shell script to run M4
27155 and check for unresolved macro calls; Richard Pixley, who suggested
27156 running the compiler instead of searching the file system to find
27157 include files and symbols, for more accurate results; Karl Berry, who
27158 got Autoconf to configure @TeX{} and added the macro index to the
27159 documentation; and Ian Lance Taylor, who added support for creating a C
27160 header file as an alternative to putting @option{-D} options in a
27161 makefile, so he could use Autoconf for his UUCP package.
27162 The alpha testers cheerfully adjusted their files again and again as the
27163 names and calling conventions of the Autoconf macros changed from
27164 release to release. They all contributed many specific checks, great
27165 ideas, and bug fixes.
27170 In July 1992, after months of alpha testing, I released Autoconf 1.0,
27171 and converted many GNU packages to use it. I was surprised by how
27172 positive the reaction to it was. More people started using it than I
27173 could keep track of, including people working on software that wasn't
27174 part of the GNU Project (such as TCL, FSP, and Kerberos V5).
27175 Autoconf continued to improve rapidly, as many people using the
27176 @command{configure} scripts reported problems they encountered.
27178 Autoconf turned out to be a good torture test for M4 implementations.
27179 Unix M4 started to dump core because of the length of the
27180 macros that Autoconf defined, and several bugs showed up in GNU
27181 M4 as well. Eventually, we realized that we needed to use some
27182 features that only GNU M4 has. 4.3BSD M4, in
27183 particular, has an impoverished set of builtin macros; the System V
27184 version is better, but still doesn't provide everything we need.
27186 More development occurred as people put Autoconf under more stresses
27187 (and to uses I hadn't anticipated). Karl Berry added checks for X11.
27188 david zuhn contributed C++ support. François Pinard made it diagnose
27189 invalid arguments. Jim Blandy bravely coerced it into configuring
27190 GNU Emacs, laying the groundwork for several later improvements.
27191 Roland McGrath got it to configure the GNU C Library, wrote the
27192 @command{autoheader} script to automate the creation of C header file
27193 templates, and added a @option{--verbose} option to @command{configure}.
27194 Noah Friedman added the @option{--autoconf-dir} option and
27195 @code{AC_MACRODIR} environment variable. (He also coined the term
27196 @dfn{autoconfiscate} to mean ``adapt a software package to use
27197 Autoconf''.) Roland and Noah improved the quoting protection in
27198 @code{AC_DEFINE} and fixed many bugs, especially when I got sick of
27199 dealing with portability problems from February through June, 1993.
27202 @section Deuteronomy
27204 A long wish list for major features had accumulated, and the effect of
27205 several years of patching by various people had left some residual
27206 cruft. In April 1994, while working for Cygnus Support, I began a major
27207 revision of Autoconf. I added most of the features of the Cygnus
27208 @command{configure} that Autoconf had lacked, largely by adapting the
27209 relevant parts of Cygnus @command{configure} with the help of david zuhn
27210 and Ken Raeburn. These features include support for using
27211 @file{config.sub}, @file{config.guess}, @option{--host}, and
27212 @option{--target}; making links to files; and running @command{configure}
27213 scripts in subdirectories. Adding these features enabled Ken to convert
27214 GNU @code{as}, and Rob Savoye to convert DejaGNU, to using
27217 I added more features in response to other peoples' requests. Many
27218 people had asked for @command{configure} scripts to share the results of
27219 the checks between runs, because (particularly when configuring a large
27220 source tree, like Cygnus does) they were frustratingly slow. Mike
27221 Haertel suggested adding site-specific initialization scripts. People
27222 distributing software that had to unpack on MS-DOS asked for a way to
27223 override the @file{.in} extension on the file names, which produced file
27224 names like @file{config.h.in} containing two dots. Jim Avera did an
27225 extensive examination of the problems with quoting in @code{AC_DEFINE}
27226 and @code{AC_SUBST}; his insights led to significant improvements.
27227 Richard Stallman asked that compiler output be sent to @file{config.log}
27228 instead of @file{/dev/null}, to help people debug the Emacs
27229 @command{configure} script.
27231 I made some other changes because of my dissatisfaction with the quality
27232 of the program. I made the messages showing results of the checks less
27233 ambiguous, always printing a result. I regularized the names of the
27234 macros and cleaned up coding style inconsistencies. I added some
27235 auxiliary utilities that I had developed to help convert source code
27236 packages to use Autoconf. With the help of François Pinard, I made
27237 the macros not interrupt each others' messages. (That feature revealed
27238 some performance bottlenecks in GNU M4, which he hastily
27239 corrected!) I reorganized the documentation around problems people want
27240 to solve. And I began a test suite, because experience had shown that
27241 Autoconf has a pronounced tendency to regress when we change it.
27243 Again, several alpha testers gave invaluable feedback, especially
27244 François Pinard, Jim Meyering, Karl Berry, Rob Savoye, Ken Raeburn,
27247 Finally, version 2.0 was ready. And there was much rejoicing. (And I
27248 have free time again. I think. Yeah, right.)
27251 @c ========================================================== Appendices
27254 @node GNU Free Documentation License
27255 @appendix GNU Free Documentation License
27263 * Environment Variable Index:: Index of environment variables used
27264 * Output Variable Index:: Index of variables set in output files
27265 * Preprocessor Symbol Index:: Index of C preprocessor symbols defined
27266 * Cache Variable Index:: Index of documented cache variables
27267 * Autoconf Macro Index:: Index of Autoconf macros
27268 * M4 Macro Index:: Index of M4, M4sugar, and M4sh macros
27269 * Autotest Macro Index:: Index of Autotest macros
27270 * Program & Function Index:: Index of those with portability problems
27271 * Concept Index:: General index
27274 @node Environment Variable Index
27275 @appendixsec Environment Variable Index
27277 This is an alphabetical list of the environment variables that might
27278 influence Autoconf checks.
27282 @node Output Variable Index
27283 @appendixsec Output Variable Index
27285 This is an alphabetical list of the variables that Autoconf can
27286 substitute into files that it creates, typically one or more
27287 makefiles. @xref{Setting Output Variables}, for more information
27288 on how this is done.
27292 @node Preprocessor Symbol Index
27293 @appendixsec Preprocessor Symbol Index
27295 This is an alphabetical list of the C preprocessor symbols that the
27296 Autoconf macros define. To work with Autoconf, C source code needs to
27297 use these names in @code{#if} or @code{#ifdef} directives.
27301 @node Cache Variable Index
27302 @appendixsec Cache Variable Index
27304 This is an alphabetical list of documented cache variables used
27305 by macros defined in Autoconf. Autoconf macros may use additional cache
27306 variables internally.
27307 @ifset shortindexflag
27308 To make the list easier to use, the variables are listed without their
27309 preceding @samp{ac_cv_}.
27314 @node Autoconf Macro Index
27315 @appendixsec Autoconf Macro Index
27317 This is an alphabetical list of the Autoconf macros.
27318 @ifset shortindexflag
27319 To make the list easier to use, the macros are listed without their
27320 preceding @samp{AC_}.
27325 @node M4 Macro Index
27326 @appendixsec M4 Macro Index
27328 This is an alphabetical list of the M4, M4sugar, and M4sh macros.
27329 @ifset shortindexflag
27330 To make the list easier to use, the macros are listed without their
27331 preceding @samp{m4_} or @samp{AS_}. The prefix is @samp{m4_} for
27332 all-lowercase macro names and @samp{AS_} for all-uppercase macro
27338 @node Autotest Macro Index
27339 @appendixsec Autotest Macro Index
27341 This is an alphabetical list of the Autotest macros.
27342 @ifset shortindexflag
27343 To make the list easier to use, the macros are listed without their
27344 preceding @samp{AT_}.
27349 @node Program & Function Index
27350 @appendixsec Program and Function Index
27352 This is an alphabetical list of the programs and functions whose
27353 portability is discussed in this document.
27357 @node Concept Index
27358 @appendixsec Concept Index
27360 This is an alphabetical list of the files, tools, and concepts
27361 introduced in this document.
27367 @c LocalWords: texinfo setfilename autoconf texi settitle setchapternewpage
27368 @c LocalWords: setcontentsaftertitlepage finalout ARG ovar varname dvar acx
27369 @c LocalWords: makeinfo dvi defcodeindex ev ov CPP cv Autotest mv defindex fn
27370 @c LocalWords: shortindexflag iftex ifset acindex ACindex ifclear ahindex fu
27371 @c LocalWords: asindex MSindex atindex ATindex auindex hdrindex prindex FIXME
27372 @c LocalWords: msindex alloca fnindex Aaarg indices FSF's dircategory ifnames
27373 @c LocalWords: direntry autoscan autoreconf autoheader autoupdate config FDs
27374 @c LocalWords: testsuite titlepage Elliston Demaille vskip filll ifnottex hmm
27375 @c LocalWords: insertcopying Autoconf's detailmenu Automake Libtool Posix ois
27376 @c LocalWords: Systemology Checkpointing Changequote INTERCAL changequote dfn
27377 @c LocalWords: Quadrigraphs builtins Shellology acconfig Bugward LIBOBJ Imake
27378 @c LocalWords: LIBOBJS IFELSE cindex flushright Pinard Metaconfig uref Simons
27379 @c LocalWords: distclean uninstall noindent versioning Tromey dir vr
27380 @c LocalWords: SAMS samp aclocal acsite underquoted emph itemx prepend SUBST
27381 @c LocalWords: evindex automake Gettext autopoint gettext symlink libtoolize
27382 @c LocalWords: defmac INIT tarname ovindex cvindex BUGREPORT PREREQ asis PROG
27383 @c LocalWords: SRCDIR srcdir globbing afterwards cmds foos fooo foooo init cd
27384 @c LocalWords: builddir timestamp src Imakefile chmod defvar CFLAGS CPPFLAGS
27385 @c LocalWords: CXXFLAGS DEFS DHAVE defvarx FCFLAGS FFLAGS LDFLAGS bindir GCC
27386 @c LocalWords: datadir datarootdir docdir dvidir htmldir libdir ifnothtml kbd
27387 @c LocalWords: includedir infodir libexecdir localedir localstatedir mandir
27388 @c LocalWords: oldincludedir pdfdir PDF psdir PostScript sbindir sysconfdir
27389 @c LocalWords: sharedstatedir DDATADIR sed tmp pkgdatadir VPATH conf unistd
27390 @c LocalWords: undef endif builtin FUNCS ifndef STACKSEG getb GETB YMP fubar
27391 @c LocalWords: PRE dest SUBDIRS subdirs fi struct STDC stdlib stddef INTTYPES
27392 @c LocalWords: inttypes STDINT stdint AWK AIX Solaris NeXT env EGREP FGREP yy
27393 @c LocalWords: LEXLIB YYTEXT lfl nonportable Automake's LN RANLIB byacc INETD
27394 @c LocalWords: inetd prog PROGS progs ranlib lmp lXt lX nsl gethostbyname UX
27395 @c LocalWords: isinf isnan glibc IRIX sunmath lm lsunmath pre sizeof
27396 @c LocalWords: ld inline malloc putenv setenv FreeBSD realloc SunOS MinGW
27397 @c LocalWords: snprintf vsnprintf sprintf vsprintf sscanf gcc strerror ifdef
27398 @c LocalWords: strnlen sysconf PAGESIZE unsetenv va fallback memcpy dst FUNC
27399 @c LocalWords: PowerPC GNUC libPW pragma Olibcalls CHOWN chown CLOSEDIR VFORK
27400 @c LocalWords: closedir FNMATCH fnmatch vfork FSEEKO LARGEFILE fseeko SVR sc
27401 @c LocalWords: largefile GETGROUPS getgroups GETLOADAVG DGUX UMAX NLIST KMEM
27402 @c LocalWords: SETGID getloadavg nlist GETMNTENT irix acxindex autom
27403 @c LocalWords: getmntent UnixWare GETPGRP getpgid getpgrp Posix's pid LSTAT
27404 @c LocalWords: lstat rpl MEMCMP memcmp OpenStep MBRTOWC mbrtowc MKTIME mktime
27405 @c LocalWords: localtime MMAP mmap OBSTACK obstack obstacks ARGTYPES timeval
27406 @c LocalWords: SETPGRP setpgrp defmacx Hurd SETVBUF setvbuf STRCOLL strcoll
27407 @c LocalWords: STRTOD strtod DECL STRFTIME strftime SCO UTIME utime VPRINTF
27408 @c LocalWords: DOPRNT vprintf doprnt sp unfixable LIBSOURCE LIBSOURCES Eggert
27409 @c LocalWords: linux netinet ia Tru XFree DIRENT NDIR dirent ndir multitable
27410 @c LocalWords: NAMLEN strlen namlen MKDEV SYSMACROS makedev RESOLV resolv DNS
27411 @c LocalWords: inet structs NAMESER arpa NETDB netdb UTekV UTS GCC's kB
27412 @c LocalWords: STDBOOL BOOL stdbool cplusplus bool Bool stdarg tm te
27413 @c LocalWords: ctype strchr strrchr rindex bcopy memmove memchr WEXITSTATUS
27414 @c LocalWords: WIFEXITED TIOCGWINSZ GWINSZ termios preprocess preprocessable
27415 @c LocalWords: DECLS strdup calloc BLKSIZE blksize RDEV rdev TZNAME tzname pw
27416 @c LocalWords: passwd gecos pwd MBSTATE mbstate wchar RETSIGTYPE hup UID uid
27417 @c LocalWords: gid ptrdiff uintmax EXEEXT OBJEXT Ae conftest AXP str
27418 @c LocalWords: ALIGNOF WERROR Werror cpp HP's WorkShop egcs un fied stdc CXX
27419 @c LocalWords: varargs BIGENDIAN Endianness SPARC endianness grep'ed CONST FC
27420 @c LocalWords: const STRINGIZE stringizing PARAMS unprotoize protos KCC cxx
27421 @c LocalWords: xlC aCC CXXCPP FREEFORM xlf FLIBS FCLIBS ish SRCEXT XTRA LFS
27422 @c LocalWords: ISC lcposix MINIX Minix conditionalized inlines hw dD confdefs
27423 @c LocalWords: fputs stdout PREPROC ar UFS HFS QNX realtime fstype STATVFS se
27424 @c LocalWords: statvfs STATFS statfs func machfile hdr lelf raboof DEFUN GTK
27425 @c LocalWords: GTKMM Grmph ified ine defn baz EOF qar Ahhh changecom algol io
27426 @c LocalWords: changeword quadrigraphs quadrigraph dnl SGI atoi overquoting
27427 @c LocalWords: Aas Wcross sep args namespace undefine bpatsubst popdef dquote
27428 @c LocalWords: bregexp Overquote overquotation meisch maisch meische maische
27429 @c LocalWords: miscian DIRNAME dirname MKDIR CATFILE XMKMF TRAVOLTA celsius
27430 @c LocalWords: EMX emxos Emacsen Korn DYNIX subshell posix Ksh ksh Pdksh Zsh
27431 @c LocalWords: pdksh zsh Allbery Lipe Kubota UWS zorglub stderr eval esac lfn
27432 @c LocalWords: drivespec Posixy DJGPP doschk prettybird LPT pfew Zsh's yu yaa
27433 @c LocalWords: yM uM aM firebird IP subdir misparses ok Unpatched abc bc zA
27434 @c LocalWords: CDPATH DUALCASE LINENO prepass Subshells lineno NULLCMD cmp wc
27435 @c LocalWords: MAILPATH scanset arg NetBSD Almquist printf expr cp pR
27436 @c LocalWords: Oliva awk Aaaaarg cmd regex xfoo GNV OpenVMS VM url fc
27437 @c LocalWords: sparc Proulx nbar nfoo maxdepth acdilrtu TWG mc ing FP
27438 @c LocalWords: mkdir exe uname OpenBSD Fileutils mktemp umask TMPDIR guid os
27439 @c LocalWords: fooXXXXXX Unicos utimes hpux hppa unescaped SUBST'ed
27440 @c LocalWords: pmake DOS's gmake ifoo DESTDIR autoconfiscated pc coff mips gg
27441 @c LocalWords: dec ultrix cpu wildcards rpcc rdtsc powerpc readline
27442 @c LocalWords: withval vxworks gless localcache usr LOFF loff CYGWIN Cygwin
27443 @c LocalWords: cygwin SIGLIST siglist SYSNDIR SYSDIR ptx lseq rusage elif MSC
27444 @c LocalWords: lfoo POUNDBANG lsun NIS getpwnam SYSCALLS RSH INTL lintl aix
27445 @c LocalWords: intl lx ldir syslog bsd EPI toolchain netbsd objext de KNR nn
27446 @c LocalWords: fication LTLIBOBJS Wdiff TESTDIR atconfig atlocal akim XFAIL
27447 @c LocalWords: ChangeLog prepended errexit smallexample TESTSUITEFLAGS GPL er
27448 @c LocalWords: installcheck autotest indir Pixley Bothner Eichin Kerberos adl
27449 @c LocalWords: DISTCLEANFILES preprocessor's fileutils Stallman Murphey Stenn
27450 @c LocalWords: Manfredi Autoconfig TCL FSP david zuhn Blandy MACRODIR Raeburn
27451 @c LocalWords: autoconfiscate Savoye Haertel Avera Meyering fdl appendixsec
27452 @c LocalWords: printindex american LIBOBJDIR LibdirTest ERLCFLAGS OBJCFLAGS
27453 @c LocalWords: VER Gnulib online xyes strcpy TYPEOF typeof OBJC objcc objc ln
27454 @c LocalWords: GOBJC OTP ERLC erl valloc decr dumpdef errprint incr
27455 @c LocalWords: esyscmd len maketemp pushdef substr syscmd sysval translit txt
27456 @c LocalWords: sinclude foreach myvar tolower toupper uniq BASENAME STDIN
27457 @c LocalWords: Dynix basename aname cname macroexpands xno xcheck iso
27458 @c LocalWords: LIBREADLINE lreadline lncurses libreadline vrindex SYS
27459 @c LocalWords: syncodeindex define'd caindex CAindex MacKenzie DIRS
27460 @c LocalWords: Runtime runtime Submakes submakes MAKEFLAGS whitespace
27461 @c LocalWords: Timestamps Unportability Canonicalizing stdckdint dirN
27462 @c LocalWords: acinclude AMFLAGS LIBS OBJCXXFLAGS GOFLAGS runstatedir
27463 @c LocalWords: metacharacter EXPENSIVEP errno setjmp wctype sys mawk
27464 @c LocalWords: nawk ggrep egrep gegrep fgrep gfgrep LEX lex yytext nm
27465 @c LocalWords: yywrap xflex lexyy YFLAGS yacc divnum libs fuindex ffs
27466 @c LocalWords: environ sigaction extern ftello nonnull STRTOLD LLONG
27467 @c LocalWords: strtold vfprintf ULLONG strcasecmp strncasecmp MSVC th
27468 @c LocalWords: NDEBUG Xenix INO libc ISDIR ISREG Tektronix Amdahl ino
27469 @c LocalWords: typedef pxref fileblocks submembers INTMAX intmax UINT
27470 @c LocalWords: INTPTR intptr SSIZE ssize uint UINTPTR uintptr OPENMP
27471 @c LocalWords: openmp OpenMP omp Alignas Alignof Noreturn UTF vals gl
27472 @c LocalWords: offsetof VARARRAYS VLA CCC stdcxx nullptr
27473 @c LocalWords: constexpr decltype unicode fstreams iostreams iomanip
27474 @c LocalWords: stringstreams GXX OBJCPP OBJCXX objcxx GOBJCXX erlc tx
27475 @c LocalWords: OBJCXXCPP FIXEDFORM GFC argc argv shellvar fpp MODEXT
27476 @c LocalWords: freeform fixedform MODINC MODOUT gccgo GOC xmkmf fseek
27477 @c LocalWords: interpval ftell Interix macOS PTHREAD NonStop XOPEN xc
27478 @c LocalWords: IEC ATTRIBS BFP DFP O'Donell Sebor ERTS Erlang's erts
27479 @c LocalWords: erlang Wundef scalable USG NOTMAKE DOUCH
27480 @c LocalWords: IVE changesyntax ifnotinfo oline num cfg debugfile cdr
27481 @c LocalWords: debugmode traceoff traceon patsubst dumpdefs ifelse aa
27482 @c LocalWords: mkstemp undivert lifo errprintn BINSH sanitization bcd
27483 @c LocalWords: cleardivert bmatch bpatsubsts subst cond nblank ifval
27484 @c LocalWords: ifblank ifnblank ifvaln fputc fgetc argn mapall dvarv
27485 @c LocalWords: shiftn abcd elt noquote mkargs joinall SHA prereq dup
27486 @c LocalWords: listc setb seta ARITH HNUM xcurly xoccupied
27487 @c LocalWords: TESTA TESTB TESTC hoc xpg xxyzzyz dtksh nosuch fifos
27488 @c LocalWords: fifo Stardent sig WIF WIFSIGNALED SIGQUIT tty perl ret
27489 @c LocalWords: SIGINT NUL SFN PRN aeiou MSYS SIGTERM xhi arith UWIN
27490 @c LocalWords: CLICOLOR FPATH POSIXLY Shellshock CVE OSF doit ec ci
27491 @c LocalWords: notreached cim nc ACL faccessat Alexandre getline sqrt
27492 @c LocalWords: CONVFMT FS OFMT CDS chgrp futimens utimensat oo esc od
27493 @c LocalWords: ownerships mape readdir mkfifo mknod testsuites XSI rf
27494 @c LocalWords: bcdox hexdump filelist rmdir flushleft busybox nl HAZy
27495 @c LocalWords: ABCDEFGHIJKLMNOPQRSTUVWXYZ Fantazy FAntAZy adc unix xb
27496 @c LocalWords: SUBMAKEFLAGS ehBc ehB hBc hvB dmake hostname nlinit xf
27497 @c LocalWords: DCOMMENT bart pathnames ifhtml randx
27498 @c LocalWords: sumc hic ic fwrapv ftrapv SIGFPE memset fmudflap ctime
27499 @c LocalWords: asctime lvalues lvalue Multithreaded decstation gdb na
27500 @c LocalWords: enableval lesskey FHS superset waitpid libfoo cposix
27501 @c LocalWords: mem RESTARTABLE bzero DejaGNU EUNIT subfile optarg ive
27502 @c LocalWords: nolog expout experr erlflags EUnit testme eunit myprog
27503 @c LocalWords: configmake vx bashdb tvtwm questers UUCP McGrath
27504 @c LocalWords: ispell
27505 @c Local Variables:
27508 @c ispell-local-dictionary: "american"
27509 @c indent-tabs-mode: nil
27510 @c whitespace-check-buffer-indent: nil