3 # autoconf -- create `configure' using m4 macros
4 # Copyright 1992, 1993, 1994, 1996, 1999, 2000, 2001
5 # Free Software Foundation, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 me=`echo "$0" | sed -e 's,.*[\\/],,'`
25 Usage: $0 [OPTION] ... [TEMPLATE-FILE]
27 Generate a configuration script from a TEMPLATE-FILE if given, or
28 \`configure.ac' if present, or else \`configure.in'. Output is sent
29 to the standard output if TEMPLATE-FILE is given, else into
33 -h, --help print this help, then exit
34 -V, --version print version number, then exit
35 -v, --verbose verbosely report processing
36 -d, --debug don't remove temporary files
37 -o, --output=FILE save output in FILE (stdout is the default)
38 -W, --warnings=CATEGORY report the warnings falling in CATEGORY [syntax]
40 Warning categories include:
41 \`cross' cross compilation issues
42 \`obsolete' obsolete constructs
43 \`syntax' dubious syntactic constructs
44 \`all' all the warnings
45 \`no-CATEGORY' turn off the warnings on CATEGORY
46 \`none' turn off all the warnings
47 \`error' warnings are error
49 The environment variable \`WARNINGS' is honored.
52 -A, --autoconf-dir=ACDIR Autoconf's macro files location (rarely needed)
53 -l, --localdir=DIR location of the \`aclocal.m4' file
56 -t, --trace=MACRO report the list of calls to MACRO
57 -i, --initialization also trace Autoconf's initialization process
59 In tracing mode, no configuration script is created.
61 Report bugs to <bug-autoconf@gnu.org>."
64 autoconf (@PACKAGE_NAME@) @VERSION@
65 Written by David J. MacKenzie.
67 Copyright 1992, 1993, 1994, 1996, 1999, 2000, 2001
68 Free Software Foundation, Inc.
69 This is free software; see the source for copying conditions. There is NO
70 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
73 Try \`$me --help' for more information."
76 echo \"$me: option \\\`\$1' requires an argument\" >&2
81 if test "${LANG+set}" = set; then LANG=C; export LANG; fi
82 if test "${LC_ALL+set}" = set; then LC_ALL=C; export LC_ALL; fi
83 if test "${LC_CTYPE+set}" = set; then LC_CTYPE=C; export LC_CTYPE; fi
84 if test "${LC_MESSAGES+set}" = set; then LC_MESSAGES=C; export LC_MESSAGES; fi
87 # We use echo to avoid assuming a particular line-breaking character.
88 # The extra dot is to prevent the shell from consuming trailing
89 # line-breaks from the sub-command output. A line-break within
90 # single-quotes doesn't work because, if this script is created in a
91 # platform that uses two characters for line-breaks (e.g., DOS), tr
93 ac_LF_and_DOT=`echo; echo .`
101 if echo xfoo | $AWK '/foo|^bar$/ { print }' | grep xfoo >/dev/null; then :; else
102 echo "$me: the regex engine of $AWK is too broken to be used" >&2
103 echo "$me: you might want to install GNU AWK" >&2
109 : ${autoconf_dir=${AC_MACRODIR=@datadir@}}
110 test -z "$AC_ACLOCALDIR" &&
111 AC_ACLOCALDIR=`(aclocal --print-ac-dir) 2>/dev/null`
113 # Trace Autoconf's initialization?
121 # Trace the first arguments of some macros
123 # Produce the configure script (default)
128 # Parse command line.
129 while test $# -gt 0 ; do
130 optarg=`expr "x$1" : 'x--[^=]*=\(.*\)' \| \
134 echo "$version" ; exit 0 ;;
136 echo "$usage"; exit 0 ;;
144 --localdir=* | -l?* )
148 test $# = 1 && eval "$exit_missing_arg"
153 --autoconf-dir=* | -A?* )
156 --autoconf-dir | -A )
157 test $# = 1 && eval "$exit_missing_arg"
161 --macrodir=* | -m?* )
162 echo "$me: warning: --macrodir is obsolete, use --autoconf-dir" >&2
166 echo "$me: warning: --macrodir is obsolete, use --autoconf-dir" >&2
167 test $# = 1 && eval "$exit_missing_arg"
174 traces="$traces '"`echo "$optarg" | sed "s/'/'\\\\\\\\''/g"`"'"
177 test $# = 1 && eval "$exit_missing_arg"
180 traces="$traces '"`echo "$1" | sed "s/'/'\\\\\\\\''/g"`"'"
182 --initialization | -i )
190 test $# = 1 && eval "$exit_missing_arg"
195 --warnings=* | -W?* )
196 warnings=$warnings,$optarg
199 test $# = 1 && eval "$exit_missing_arg"
201 warnings=$warnings,$1
204 -- ) # Stop option processing
206 - ) # Use stdin as input.
210 echo "$me: invalid option $1"
218 # The warnings are the concatenation of 1. application's defaults,
219 # 2. $WARNINGS, $3 command line options, in that order.
220 # Set them in the order expected by the M4 macros: the converse.
221 alphabet='abcdefghijklmnopqrstuvwxyz'
222 ALPHABET='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
224 WORDCHAR=_$alphabet$ALPHABET$NUMBERS
227 for warning in `IFS=,; echo syntax $WARNINGS $warnings |
228 tr $ALPHABET $alphabet`
230 test -n $warning || continue
231 m4_warnings="$warning"`test -n "$m4_warnings" && echo ",$m4_warnings"`
235 # Trap on 0 to stop playing with `rm'.
238 trap 'status=$?; rm -rf $tmp && exit $status' 0
239 trap '(exit 1); exit 1' 1 2 13 15
242 # Create a (secure) tmp directory for tmp files.
245 tmp=`(umask 077 && mktemp -d -q "$TMPDIR/acXXXXXX") 2>/dev/null` &&
246 test -n "$tmp" && test -d "$tmp"
250 (umask 077 && mkdir $tmp)
253 echo "$me: cannot create a temporary directory in $TMPDIR" >&2
258 test -f "$autoconf_dir/acsite.m4" && acsite_m4="$autoconf_dir/acsite.m4"
259 test -f "$localdir/aclocal.m4" && aclocal_m4="$localdir/aclocal.m4"
260 m4_common="-I $autoconf_dir -I $localdir -Dm4_tmpdir=$tmp"
261 m4_prefiles=" $autoconf_dir/autoconf.m4 $acsite_m4 $aclocal_m4"
262 m4f_prefiles=$m4_prefiles
263 run_m4="$M4 $m4_common"
265 # Find the input file.
268 case `ls configure.ac configure.in 2>/dev/null` in
270 echo "$me: warning: both \`configure.ac' and \`configure.in' are present." >&2
271 echo "$me: warning: proceeding with \`configure.ac'." >&2
272 infile=configure.ac;;
273 *ac ) infile=configure.ac;;
274 *in ) infile=configure.in;;
276 echo "$me: no input file" >&2
279 test $task = script && test -z "$outfile" && outfile=configure;;
282 echo "$me: invalid number of arguments."
287 # Unless specified, the output is stdout.
288 test -z "$outfile" && outfile=-
290 # We need an actual file.
291 if test z$infile = z-; then
294 elif test ! -r "$infile"; then
295 echo "$me: $infile: No such file or directory" >&2
299 # Output is produced into FD 4. Prepare it.
301 -) # Output to stdout
307 # Initializations are performed. Proceed to the main task.
310 ## --------------------------------- ##
311 ## Generate the `configure' script. ##
312 ## --------------------------------- ##
317 $verbose "$me: running $run_m4 -Dm4_warnings=$m4_warnings $m4f_prefiles $infile" >&2
318 $run_m4 -Dm4_warnings=$m4_warnings $m4f_prefiles $infile >$tmp/configure ||
319 { (exit 1); exit 1; }
321 if test "x$outfile" != x-; then
325 # Put the real line numbers into configure to make config.log more
326 # helpful. Because quoting can sometimes get really painful in m4,
327 # there are special @tokens@ to substitute.
328 sed 's/^ //' >"$tmp/finalize.awk" <<EOF
329 # Load the list of tokens which escape the forbidden patterns.
331 # Be sure the read GAWK documentation to understand the parens
332 # around \`tmp "/forbidden.rx"'.
333 while ((getline pattern < (tmp "/forbidden.rx")) > 0)
334 forbidden = (forbidden ? forbidden "|" : "") pattern
335 close (tmp "/forbidden.rx")
337 errprint("$me: forbidden: " forbidden)
339 while ((getline pattern < (tmp "/allowed.rx")) > 0)
340 allowed = (allowed ? allowed "|" : "") pattern
343 close (tmp "/allowed.rx")
345 errprint("$me: allowed: " allowed)
348 function errprint (message)
350 # BAD! the pipe to 'cat >&2' doesn't work for DJGPP.
351 # print message | "cat >&2"
352 # Use normal redirection instead:
353 print message > "$tmp/finalize.err"
356 function undefined (file, line, macro)
358 errprint(file ":" line ": error: possibly undefined macro: " macro)
376 if (\$0 ~ /__oline__/)
377 while (sub (/__oline__/, oline))
379 while (sub (/@<:@/, "["))
381 while (sub (/@:>@/, "]"))
383 while (sub (/@S\|@/, "$"))
385 while (sub (/@%:@/, "#"))
390 # Dubious feature: we tolerate macro names when commented.
394 split (\$0, tokens, /[^$WORDCHAR]*/)
396 for (token in tokens)
397 if (match (tokens[token], forbidden) &&
398 !match (tokens[token], allowed))
400 macros [tokens [token]] = oline
401 some_macros_were_not_expanded = 1
405 # If there are some macros which are left unexpanded in the output,
406 # try to find the input which is responsible. Otherwise, try to help.
408 if (some_macros_were_not_expanded)
411 while (getline < "$infile")
414 for (macro in macros)
415 if (index (\$0, macro))
417 delete macros [macro]
418 undefined("$infile", line, macro)
422 for (macro in macros)
423 undefined("$outfile", macros [macro], macro)
429 `$verbose "-v verbose=1"` \
430 -f "$tmp/finalize.awk" <$tmp/configure >&4 ||
431 { test -f "$tmp/finalize.err" && cat "$tmp/finalize.err" >&2
433 test -f "$tmp/finalize.err" && cat "$tmp/finalize.err" >&2
434 ;; # End of the task script.
445 # Routines to process formatted m4 traces.
446 sed 's/^ //' >$tmp/trace.m4 <<\EOF
449 # _at_MODE(SEPARATOR, ELT1, ELT2...)
450 # ----------------------------------
451 # List the elements, separating then with SEPARATOR.
453 # `at' -- the elements are enclosed in brackets.
454 # `star' -- the elements are listed as are.
455 # `percent' -- the elements are `flattened': spaces are singled out,
456 # and no new line remains.
458 [at_ifelse([$#], [1], [],
460 [[[$2]][$1]$0([$1], at_shift(at_shift($@)))])])
461 define([_at_percent],
462 [at_ifelse([$#], [1], [],
463 [$#], [2], [at_flatten([$2])],
464 [at_flatten([$2])[$1]$0([$1], at_shift(at_shift($@)))])])
466 [at_ifelse([$#], [1], [],
468 [[$2][$1]$0([$1], at_shift(at_shift($@)))])])
470 # FLATTEN quotes its result.
472 [at_patsubst(at_patsubst(at_patsubst([[[$1]]],
478 [^ *\(.*\) *$], [[\1]])])
480 define([at_args], [at_shift(at_shift(at_shift(at_shift(at_shift($@)))))])
481 define([at_at], [_$0([$1], at_args($@))])
482 define([at_percent], [_$0([$1], at_args($@))])
483 define([at_star], [_$0([$1], at_args($@))])
487 # If you trace `define', then on `define([m4_exit], defn([m4exit])' you
490 # AT_define([m4sugar.m4], [115], [1], [define], [m4_exit], <m4exit>)
492 # Since `<m4exit>' is not quoted, the outter m4, when processing
493 # `trace.m4' will exit prematurely. Hence, move all the builtins to
494 # the `at_' name space.
495 echo '# Copy the builtins.' >>$tmp/trace.m4
497 $M4 2>&1 >/dev/null |
498 sed 's/^\([^:]*\):.*/define([at_\1], defn([\1]))/' >>$tmp/trace.m4
501 echo '# Disable the builtins.' >>$tmp/trace.m4
503 $M4 2>&1 >/dev/null |
504 sed 's/^\([^:]*\):.*/at_undefine([\1])/' >>$tmp/trace.m4
510 # Transform the traces from m4 into an m4 input file.
511 # Typically, transform:
513 # | m4trace:configure.ac:3: -1- AC_SUBST([exec_prefix], [NONE])
517 # | AT_AC_SUBST([configure.ac], [3], [1], [AC_SUBST], [exec_prefix], [NONE])
519 # Pay attention that the file name might include colons, if under DOS
520 # for instance, so we don't use `[^:][^:]*'.
521 # The first s/// catches multiline traces, the second, traces as above.
522 preamble='m4trace:\(..*\):\([0-9][0-9]*\): -\([0-9][0-9]*\)-'
523 cat >$tmp/trace2m4.sed <<EOF
524 s/^$preamble \([^(][^(]*\)(\(.*\)$/AT_\4([\1], [\2], [\3], [\4], \5/
525 s/^$preamble \(.*\)$/AT_\4([\1], [\2], [\3], [\4])/
530 # Translate user tracing requests into m4 macros.
531 cat >$tmp/translate.awk <<\EOF
532 function trans (arg, sep)
543 # Name (also available as $0).
550 # $@, list of quoted effective arguments.
552 return "]at_at([" (separator ? separator : ",") "], $@)["
553 # $*, list of unquoted effective arguments.
555 return "]at_star([" (separator ? separator : ",") "], $@)["
556 # $%, list of flattened unquoted effective arguments.
558 return "]at_percent([" (separator ? separator : ":") "], $@)["
561 function error (message)
563 print message | "cat >&2"
568 # Accumulate the whole input.
569 request = request $0 "\n"
574 request = substr (request, 1, length (request) - 1)
575 # The default request is `$f:$l:$n:$*'.
576 colon = index (request, ":")
577 macro = colon ? substr (request, 1, colon - 1) : request
578 request = colon ? substr (request, colon + 1) : "$f:$l:$n:$%"
582 for (cp = request; cp; cp = substr (cp, 2))
584 char = substr (cp, 1, 1)
587 if (match (cp, /^\$[0-9]+/))
590 res = res "$" (substr (cp, 2, RLENGTH - 1) + 4)
591 cp = substr (cp, RLENGTH)
593 else if (substr (cp, 2, 1) ~ /[fldn$@%*]/)
595 # $x, no separator given.
596 res = res trans(substr (cp, 2, 1))
599 else if (substr (cp, 2, 1) == "{")
601 # ${sep}x, long separator.
602 end = index (cp, "}")
604 error("invalid escape: " cp)
605 separator = substr (cp, 3, end - 3)
606 if (substr (cp, end + 1, 1) ~ /[*@%]/)
607 res = res trans(substr (cp, end + 1, 1), separator)
609 error("invalid escape: " cp)
610 cp = substr (cp, end + 1)
612 else if (substr (cp, 3, 1) ~ /[*@%]/)
614 # $sx, short separator `s'.
615 res = res trans(substr (cp, 3, 1), substr (cp, 2, 1))
620 error("invalid escape: " substr (cp, 1, 2))
627 # Produce the definition of AT_<MACRO> = the translation of the request.
628 print "at_define([AT_" macro "],"
636 # Extract both the m4 program and the m4 options from TRACES.
637 echo "## ------------------------- ##" >>$tmp/trace.m4
638 echo "## Trace processing macros. ##" >>$tmp/trace.m4
639 echo "## ------------------------- ##" >>$tmp/trace.m4
642 eval set dummy "$traces"
646 echo "# $trace" >>$tmp/trace.m4
648 # The request may be several lines long, hence sed has to quit.
649 macro_name=`echo "$trace" | sed 's/:.*//;q'`
650 # If for instance TRACE is `define', be sure to have an empty
654 trace_format=`echo "$trace" | sed "1s/^$macro_name:/:/"`;;
659 # GNU M4 1.4's tracing of builtins is buggy. When run on this input:
662 # | changequote([, ])
663 # | define([m4_eval], defn([eval]))
669 # it behaves this way:
671 # | % m4 input.m4 -da -t eval
672 # | m4trace: -1- eval(1)
673 # | m4trace: -1- m4_eval(2)
674 # | m4trace: -1- m4_eval(3)
679 # | % m4 input.m4 -da -t m4_eval
682 # So we will merge them, i.e. tracing `BUILTIN' or tracing
683 # `m4_BUILTIN' will be the same: tracing both, but honoring the
684 # *last* trace specification.
685 # FIXME: This is not enough: in the output `$0' will be `BUILTIN'
686 # sometimes and `m4_BUILTIN' at others. We should render a unique name,
687 # the one specified by the user.
688 base_name=`echo "$macro_name" | sed 's/^m4_//'`
689 if echo "ifdef(\`$base_name', \`', \`m4exit(-1)')" | $M4; then
690 # BASE_NAME is a builtin.
691 trace_opt="$trace_opt -t $base_name -t m4_$base_name"
692 echo "$base_name$trace_format" |
693 $AWK -f $tmp/translate.awk >>$tmp/trace.m4 ||
694 { (exit 1); exit 1; }
695 echo "m4_$base_name$trace_format" |
696 $AWK -f $tmp/translate.awk >>$tmp/trace.m4 ||
697 { (exit 1); exit 1; }
699 # MACRO_NAME is not a builtin.
700 trace_opt="$trace_opt -t $macro_name"
702 $AWK -f $tmp/translate.awk >>$tmp/trace.m4 ||
703 { (exit 1); exit 1; }
708 echo "## ------------------- ##" >>$tmp/trace.m4
709 echo "## Traces to process. ##" >>$tmp/trace.m4
710 echo "## ------------------- ##" >>$tmp/trace.m4
712 echo "at_divert(0)at_dnl" >>$tmp/trace.m4
714 # Do we trace the initialization?
715 # `errprint' must be silent, otherwise there can be warnings mixed
716 # with traces in m4's stderr.
717 run_m4_trace="$run_m4 $trace_opt -daflq -Derrprint"
718 if $initialization; then
719 trace_prefiles="$m4_prefiles"
721 trace_prefiles="$m4f_prefiles"
724 # Run m4 on the input file to get traces.
726 # We used to have a simple pipe, which was very convenient as it
727 # allows to use traces on never ending expansions (i.e., when
728 # debugging :) but it is requires to keep error messages *and*
729 # traces in stderr. This is too fragile, as it results in
730 # unexpected data in the output. autoheader has been fragile to
732 $verbose "$me: running $run_m4_trace -o $tmp/traces $trace_prefiles $infile" >&2
733 $run_m4_trace -o $tmp/traces $trace_prefiles $infile >/dev/null ||
735 echo "$me: tracing failed" >&2
739 $verbose "$me: running $M4 $tmp/trace.m4" >&2
740 sed -f $tmp/trace2m4.sed $tmp/traces |
741 # Now we are ready to run m4 to process the trace file.
748 # It makes no sense to try to transform __oline__.
756 echo "$me: traces formatting failed" >&2
766 *) echo "$me: internal error: unknown task: $task" >&2
770 (exit $status); exit $status