]> git.ipfire.org Git - thirdparty/autoconf.git/commitdiff
* doc/autoconf.texi (Writing configure.in): Explain what Autoconf
authorAkim Demaille <akim@epita.fr>
Mon, 3 Apr 2000 11:59:27 +0000 (11:59 +0000)
committerAkim Demaille <akim@epita.fr>
Mon, 3 Apr 2000 11:59:27 +0000 (11:59 +0000)
is.  Explain how to use the quotation scheme used in Autoconf.
(Quoting): Update.  Do not advocate changequote.

ChangeLog
doc/autoconf.texi

index 4680cfa7ae8d4cc2bb9360d4ffa2a2f640d151ed..63111196a5299a4c62ec50e01381623a856270c1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2000-04-03  Akim Demaille  <akim@epita.fr>
+
+       * doc/autoconf.texi (Writing configure.in): Explain what Autoconf
+       is.  Explain how to use the quotation scheme used in Autoconf.
+       (Quoting): Update.  Do not advocate changequote.
+
 2000-03-30  Steven G. Johnson  <stevenj@alum.mit.edu>
 
        Fix F77 name-mangling macros to work with cached values (so
index ce2e99a7b9e73cfc276d432363aa4b43c93a9b9e..d833219790d31737526d8977641813cd50da7091 100644 (file)
@@ -142,8 +142,7 @@ package.  This is edition @value{EDITION}, for Autoconf version
 * Macro Index::                 Index of Autoconf macros
 * Concept Index::               General index
 
-@detailmenu
- --- The Detailed Node Listing ---
+@detailmenu --- The Detailed Node Listing ---
 
 Making @code{configure} Scripts
 
@@ -153,6 +152,12 @@ Making @code{configure} Scripts
 * Invoking autoconf::           How to create configuration scripts
 * Invoking autoreconf::         Remaking multiple @code{configure} scripts
 
+Writing @file{configure.in}
+
+* Shell Script Compiler::       Autoconf as solution of a problem
+* Autoconf Language::           Programming in Autoconf
+* configure.in Layout::         Standard organization of configure.in
+
 Initialization and Output Files
 
 * Notices::                     Copyright, version numbers in @code{configure}
@@ -336,6 +341,8 @@ History of Autoconf
 @end detailmenu
 @end menu
 
+@c ============================================================= Introduction.
+
 @node Introduction, Making configure Scripts, Top, Top
 @chapter Introduction
 
@@ -412,6 +419,9 @@ Mail suggestions and bug reports for Autoconf to
 @code{autoconf@@gnu.org}.  Please include the Autoconf version number,
 which you can get by running @samp{autoconf --version}.
 
+
+@c ================================================= Making configure Scripts.
+
 @node Making configure Scripts, Setup, Introduction, Top
 @chapter Making @code{configure} Scripts
 @cindex @file{aclocal.m4}
@@ -515,6 +525,192 @@ hand-crafted shell commands.  The @code{autoscan} program can give you a
 good start in writing @file{configure.in} (@pxref{Invoking autoscan},
 for more information).
 
+
+
+@menu
+* Shell Script Compiler::       Autoconf as solution of a problem
+* Autoconf Language::           Programming in Autoconf
+* configure.in Layout::         Standard organization of configure.in
+@end menu
+
+@node Shell Script Compiler, Autoconf Language, Writing configure.in, Writing configure.in
+@subsection A Shell Script Compiler
+
+Like for any other language, to properly program in Autoconf, i.e. the
+language in which you write @file{configure.in}, you must understand
+@emph{what} is the problem it tries to answer, and @emph{how} it does.
+
+The problem Autoconf addresses is that the world is a mess: after all,
+you are using Autoconf in order to have your package compile easily on
+all sort of different systems, some of them being extremely hostile.
+But Autoconf itself suffers from these differences: @code{configure}
+must run on all those systems, hence, @code{configure} must use the
+least common divisor between all supported system.
+
+Naturally, you orient yourself towards shell scripts.  And in fact,
+there is not even the need for a tool like @code{autoconf}: a set of
+properly written shell functions is way enough to make it easy to write
+@code{configure} scripts by hand.  Sigh!  Unfortunately, shell functions
+do not belong to the least common divisor, therefore, where you'd define
+a function and use it ten times, you need to write ten times its body.
+
+Therefore, what is really needed is some kind of compiler,
+@code{autoconf}, which takes an Autoconf program, @file{configure.in},
+and transform it in a portable shell script, @code{configure}.
+
+How does @code{autoconf} perform this task?
+
+Two obvious solutions: creating a brand new language, or extending an
+existing one.  The former option is very attractive: all sort of
+optimizations could easily be implemented in the compiler, many rigorous
+checks could be performed on the Autoconf program, and in particular, it
+would be extremely easy to reject any non portable construct etc.
+Alternatively, you can extend an existing language, of course, the
+@code{sh} language.
+
+Autoconf does the latter: it is an layer on top of @code{sh}.  Quite
+naturally then, it has been chosen to implement @code{autoconf} as a
+macro expander, i.e., a program which takes a text in input and
+repeatedly performs @dfn{macro expansions}, repeatedly replaces macro
+uses with macro bodies.  Instead of implementing a dedicated Autoconf
+macro expander, it is natural to use an existing general purpose macro
+expander, such as @code{m4}, and implement the extensions as a set of
+@code{m4} macros.
+
+
+@node Autoconf Language, configure.in Layout, Shell Script Compiler, Writing configure.in
+@subsection The Autoconf Language
+@cindex quotation
+
+The Autoconf language is very different from usual languages because you
+handle actual code just as plain text.  Where in C for instance, data
+and instructions have very different syntactic status, in Autoconf their
+status is rigorously the same.  Therefore we need a means to distinguish
+literal strings from text to be expanded: quotation.
+
+When calling macros that take arguments, there must not be any blank
+space between the macro name and the open parenthesis.  Arguments should
+be enclosed within the @code{m4} quote characters @samp{[} and @samp{]},
+and are separated by a comma.  Any leading space in arguments are
+ignored, unless they are quoted.  You may safely leave out the quotes
+when the argument is simple text, but @emph{always} quote complex
+arguments such as other macro calls.  This rule recursively applies for
+each macro call, including macro called from other macros.
+
+For instance:
+
+@example
+AC_CHECK_HEADER([stdio.h],
+                [AC_DEFINE([HAVE_STDIO_H])],
+                [AC_MSG_ERROR([Sorry, can't do anything for you])])
+@end example
+
+@noindent
+is perfectly quoted.  You may safely simplify quotation to
+
+@example
+AC_CHECK_HEADER(stdio.h,
+                [AC_DEFINE(HAVE_STDIO_H)],
+                [AC_MSG_ERROR([Sorry, can't do anything for you])])
+@end example
+
+@noindent
+Notice that the argument of @code{AC_MSG_ERROR} is still quoted,
+otherwise its comma would have been understood as an argument separator.
+
+The following example is wrong and dangerous, as it is underquoted:
+
+@example
+AC_CHECK_HEADER(stdio.h,
+                AC_DEFINE(HAVE_STDIO_H),
+                AC_MSG_ERROR([Sorry, can't do anything for you]))
+@end example
+
+You may have to use text which also resembles a macro call.  In this
+case, you must quote this text at top level:
+
+@example
+echo "Hard rock was here!  --[AC_DC]"
+@end example
+
+@noindent
+which will result in
+
+@example
+echo "Hard rock was here!  --AC_DC"
+@end example
+
+@noindent
+in @code{configure}. Since there is an additional quoting level for each
+macro invocation, this results in @emph{double quoting all the literal
+strings}:
+
+@example
+AC_MSG_WARN([[AC_DC stinks  --Iron Maiden]])
+@end example
+
+You are now able to understand one of the constructs of Autoconf which
+has continuously been misunderstood...  The thumb rule is that
+@emph{whenever you expect macro expansion, expect quote expansion},
+i.e., expect one level of quotes to be lost.  For instance
+
+@example
+AC_COMPILE_IFELSE([char b[10];],, exit 1)
+@end example
+
+@noindent
+is incorrect: here the first argument of @code{AC_COMPILE_IFELSE}, is
+@samp{char b[10];}, and it will be expanded once, which results in
+@samp{char b10;}.  There was a idiom developed in the Autoconf world to
+address this issue, based on the @code{m4} @code{changequote} primitive,
+but do not use it!  Let's take a closer look: the author meant the first
+argument to be understood as a literal, and therefore it must be quoted
+twice:
+
+@example
+AC_COMPILE_IFELSE([[char b[10];]],, exit 1)
+@end example
+
+@noindent
+and voil@`a!  You really produced @samp{char b[10];}.
+
+The careful reader will note that the so-called perfectly quoted
+@code{AC_CHECK_HEADER} example above is actually lacking three pairs of
+quotes!  Nevertheless, for sake of readability, double quotation of
+literals is used only where needed.
+
+
+Some macros take optional arguments, which this documentation represents
+@ovar{arg} (not to be confounded with the quote characters).  You may
+just leave them empty, or use @samp{[]} to make explicit the emptiness
+of the argument.  Finally you may simply leave out the trailing commas.
+The three lines below are equivalent:
+
+@example
+AC_CHECK_HEADERS(stdio.h, [], [])
+AC_CHECK_HEADERS(stdio.h,,)
+AC_CHECK_HEADERS(stdio.h)
+@end example
+
+It is best to put each macro call on its own line in
+@file{configure.in}.  Most of the macros don't add extra newlines; they
+rely on the newline after the macro call to terminate the commands.
+This approach makes the generated @code{configure} script a little
+easier to read by not inserting lots of blank lines.  It is generally
+safe to set shell variables on the same line as a macro call, because
+the shell allows assignments without intervening newlines.
+
+You can include comments in @file{configure.in} files by starting them
+with the @samp{#}.  For example, it is helpful to begin
+@file{configure.in} files with a line like this:
+
+@example
+# Process this file with autoconf to produce a configure script.
+@end example
+
+@node configure.in Layout,  , Autoconf Language, Writing configure.in
+@subsection Standard @file{configure.in} Layout
+
 The order in which @file{configure.in} calls the Autoconf macros is not
 important, with a few exceptions.  Every @file{configure.in} must
 contain a call to @code{AC_INIT} before the checks, and a call to
@@ -548,39 +744,6 @@ checks for system services
 @end group
 @end display
 
-It is best to put each macro call on its own line in
-@file{configure.in}.  Most of the macros don't add extra newlines; they
-rely on the newline after the macro call to terminate the commands.
-This approach makes the generated @code{configure} script a little
-easier to read by not inserting lots of blank lines.  It is generally
-safe to set shell variables on the same line as a macro call, because
-the shell allows assignments without intervening newlines.
-
-When calling macros that take arguments, there must not be any blank
-space between the macro name and the open parenthesis.  Arguments can be
-more than one line long if they are enclosed within the @code{m4} quote
-characters @samp{[} and @samp{]}.  If you have a long line such as a
-list of file names, you can generally use a backslash at the end of a
-line to continue it logically on the next line (this is implemented by
-the shell, not by anything special that Autoconf does).
-
-Some macros handle two cases: what to do if the given condition is met,
-and what to do if the condition is not met.  In some places you might
-want to do something if a condition is true but do nothing if it's
-false, or vice versa.  To omit the true case, pass an empty value for
-the @var{action-if-found} argument to the macro.  To omit the false
-case, omit the @var{action-if-not-found} argument to the macro,
-including the comma before it.
-
-You can include comments in @file{configure.in} files by starting them
-with the @code{m4} builtin macro @code{dnl}, which discards text up
-through the next newline.  These comments do not appear in the generated
-@code{configure} scripts.  For example, it is helpful to begin
-@file{configure.in} files with a line like this:
-
-@example
-dnl Process this file with autoconf to produce a configure script.
-@end example
 
 @node Invoking autoscan, Invoking ifnames, Writing configure.in, Making configure Scripts
 @section Using @code{autoscan} to Create @file{configure.in}
@@ -902,6 +1065,9 @@ Print the name of each directory where @code{autoreconf} runs
 Print the version number of Autoconf and exit.
 @end table
 
+
+@c ========================================= Initialization and Output Files.
+
 @node Setup, Existing Tests, Making configure Scripts, Top
 @chapter Initialization and Output Files
 
@@ -3944,6 +4110,9 @@ If on Xenix, add @samp{-lx} to output variable @code{LIBS}.  Also, if
 macro is obsolete; use @code{AC_HEADER_DIRENT} instead.
 @end defmac
 
+
+@c ========================================================= Writing Tests
+
 @node Writing Tests, Results, Existing Tests, Top
 @chapter Writing Tests
 
@@ -4498,6 +4667,9 @@ argument of either @code{AC_PROG_CPP} or @code{AC_PROG_CXXCPP},
 depending on which language is current.
 @end defmac
 
+
+@c ====================================================== Results of Tests.
+
 @node Results, Writing Macros, Writing Tests, Top
 @chapter Results of Tests
 
@@ -4920,6 +5092,9 @@ to follow a call to @code{AC_CHECKING} instead of
 is considered obsolete.
 @end defmac
 
+
+@c ========================================================== Writing Macros.
+
 @node Writing Macros, Manual Configuration, Results, Top
 @chapter Writing Macros
 
@@ -5033,6 +5208,11 @@ macro does.  For example, @code{AC_PATH_X} has internal macros
 
 @node Quoting, Dependencies Between Macros, Macro Names, Writing Macros
 @section Quoting
+@cindex quotation
+
+@c FIXME: Grmph, yet another quoting myth: quotation has *never*
+@c prevented `expansion' of $1.  Unless it refers to the expansion
+@c of the value of $1?  Anyway, we need a rewrite here...
 
 Macros that are called by other macros are evaluated by @code{m4}
 several times; each evaluation might require another layer of quotes to
@@ -5046,14 +5226,11 @@ Autoconf changes the @code{m4} quote characters from the default
 @samp{`} and @samp{'} to @samp{[} and @samp{]}, because many of the
 macros use @samp{`} and @samp{'}, mismatched.  However, in a few places
 the macros need to use brackets (usually in C program text or regular
-expressions).  In those places, they use the @code{m4} builtin command
-@code{changequote} to temporarily change the quote characters to
-@samp{<<} and @samp{>>}.  (Sometimes, if they don't need to quote
-anything, they disable quoting entirely instead by setting the quote
-characters to empty strings.)  Here is an example:
+expressions).  In those places, just quote properly!  See @ref{Autoconf
+Language}, for some information on quotation.
+
+It is frequent to read Autoconf programs with snippets like:
 
-@c FIXME: Oh no, please, no, not that crap again.  Well, a new section
-@c on quoting is needed since below the bad thing is advocated.
 @example
 AC_TRY_LINK(
 changequote(<<, >>)dnl
@@ -5062,9 +5239,58 @@ changequote(<<, >>)dnl
 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
 #endif>>,
 changequote([, ])dnl
-[atoi(*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
+[atoi (*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
+@end example
+
+@noindent
+which is incredibly useless since @code{AC_TRY_LINK} is @emph{already}
+double quoting, so you just need:
+
+@example
+AC_TRY_LINK(
+[#include <time.h>
+#ifndef tzname /* For SGI.  */
+extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
+#endif],
+[atoi (*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
+@end example
+
+@noindent
+The @code{m4} fluent reader noted that these two writings are rigorously
+equivalent, since @code{m4} swallows both the @samp{changequote(<<, >>)}
+and @samp{<<} @samp{>>} when it @dfn{collects} the arguments: these
+quotes are not part of the arguments!
+
+Simplified, the example above is just doing this:
+
+@example
+changequote(<<, >>)dnl
+<<[]>>
+changequote([, ])dnl
 @end example
 
+@noindent
+instead of simply
+
+@example
+[[]]
+@end example
+
+
+With macros which do not double quote their arguments (which is the
+rule), double quote the (risky) literals:
+
+@example
+AC_LINK_IFELSE([AC_LANG_PROGRAM(
+[[#include <time.h>
+#ifndef tzname /* For SGI.  */
+extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
+#endif]],
+[atoi (*tzname);])], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
+@end example
+
+@c FIXME: Quadrigraphs and hopeless cases.
+
 When you create a @code{configure} script using newly written macros,
 examine it carefully to check whether you need to add more quotes in
 your macros.  If one or more words have disappeared in the @code{m4}
@@ -5204,6 +5430,8 @@ have been left obsolete for a long time.
 @end defmac
 
 
+@c ================================================== Manual Configuration
+
 @node Manual Configuration, Site Configuration, Writing Macros, Top
 @chapter Manual Configuration
 
@@ -5375,6 +5603,9 @@ You can also use the host system type to find cross-compilation tools.
 @xref{Generic Programs}, for information about the @code{AC_CHECK_TOOL}
 macro which does that.
 
+
+@c ===================================================== Site Configuration.
+
 @node Site Configuration, Invoking configure, Manual Configuration, Top
 @chapter Site Configuration
 
@@ -5807,6 +6038,9 @@ if test "$cache_file" = ./config.cache; then
 fi
 @end example
 
+
+@c ============================================== Running configure Scripts.
+
 @node Invoking configure, Invoking config.status, Site Configuration, Top
 @chapter Running @code{configure} Scripts
 @cindex @code{configure}
@@ -5830,8 +6064,8 @@ may use comes with Autoconf.
 
 @include install.texi
 
-@c Parts of the following section should obviously be part of INSTALL.
-@c But how to split that?
+
+@c ============================================== Recreating a Configuration
 
 @node Invoking config.status, Questions, Invoking configure, Top
 @chapter Recreating a Configuration
@@ -5990,6 +6224,8 @@ no need to set @code{CONFIG_HEADERS} in the @code{make} rules, equally
 for @code{CONFIG_COMMANDS} etc.)
 
 
+@c ================================================ Questions About Autoconf.
+
 @node Questions, Upgrading, Invoking config.status, Top
 @chapter Questions About Autoconf
 
@@ -6165,6 +6401,9 @@ for the Kerberos V5 tree, we've modified things to call in common
 entire tree.  This means that a lot of common things don't have to be
 duplicated, even though they normally are in @code{configure} setups.
 
+
+@c ================================================= Upgrading From Version 1.
+
 @node Upgrading, History, Questions, Top
 @chapter Upgrading From Version 1
 
@@ -6397,6 +6636,9 @@ To speed up your locally written feature tests, add caching to them.
 See whether any of your tests are of general enough usefulness to
 encapsulate into macros that you can share.
 
+
+@c ===================================================== History of Autoconf.
+
 @node History, Old Macro Names, Upgrading, Top
 @chapter History of Autoconf
 
@@ -6624,6 +6866,9 @@ Eichin.
 Finally, version 2.0 was ready.  And there was much rejoicing.  (And I
 have free time again.  I think.  Yeah, right.)
 
+
+@c ========================================================= Old Macro Names.
+
 @node Old Macro Names, Environment Variable Index, History, Top
 @chapter Old Macro Names