.
[autoconf.git] / autoconf.sh
blob4e010b14a614ad6d575ab8e1860dab66c48b632d
1 #! @SHELL@
2 # autoconf -- create `configure' using m4 macros
3 # Copyright (C) 1992, 93, 94, 96, 99, 2000 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 # 02111-1307, USA.
20 # If given no args, create `configure' from template file `configure.in'.
21 # With one arg, create a configure script on standard output from
22 # the given template file.
24 me=`echo "$0" | sed -e 's,.*/,,'`
26 usage="\
27 Usage: autoconf [OPTION] ... [TEMPLATE-FILE]
29 Generate a configuration script from a TEMPLATE-FILE if given, or
30 \`configure.in' by default. Output is sent to the standard output if
31 TEMPLATE-FILE is given, else into \`configure'.
33 If the option \`--trace' is used, no configuration script is created.
35 -h, --help print this help, then exit
36 -V, --version print version number, then exit
37 -v, --verbose verbosely report processing
38 -d, --debug don't remove temporary files
39 -m, --macrodir=DIR directory storing Autoconf's macro files
40 -l, --localdir=DIR directory storing the \`aclocal.m4' file
41 -t, --trace=MACRO report the list of calls to MACRO
42 -i, --initialization also trace Autoconf's initialization process
43 -o, --output=FILE save output in FILE (stdout is the default)
45 Report bugs to <bug-autoconf@gnu.org>."
47 version="\
48 autoconf (GNU @PACKAGE@) @VERSION@
49 Written by David J. MacKenzie.
51 Copyright (C) 1992, 93, 94, 96, 99, 2000 Free Software Foundation, Inc.
52 This is free software; see the source for copying conditions. There is NO
53 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
55 help="\
56 Try \`$me --help' for more information."
58 # NLS nuisances.
59 # Only set these to C if already set. These must not be set unconditionally
60 # because not all systems understand e.g. LANG=C (notably SCO).
61 # Fixing LC_MESSAGES prevents Solaris sh from translating var values in `set'!
62 # Non-C LC_CTYPE values break the ctype check.
63 if test "${LANG+set}" = set; then LANG=C; export LANG; fi
64 if test "${LC_ALL+set}" = set; then LC_ALL=C; export LC_ALL; fi
65 if test "${LC_MESSAGES+set}" = set; then LC_MESSAGES=C; export LC_MESSAGES; fi
66 if test "${LC_CTYPE+set}" = set; then LC_CTYPE=C; export LC_CTYPE; fi
68 # ac_LF_and_DOT
69 # We use echo to avoid assuming a particular line-breaking character.
70 # The extra dot is to prevent the shell from consuming trailing
71 # line-breaks from the sub-command output. A line-break within
72 # single-quotes doesn't work because, if this script is created in a
73 # platform that uses two characters for line-breaks (e.g., DOS), tr
74 # would break.
75 ac_LF_and_DOT=`echo; echo .`
77 # Find GNU m4.
78 # Handle the case that m4 has moved since we were configured.
79 # It may have been found originally in a build directory.
80 : ${M4=@M4@}
81 case "$M4" in
82 /*) test -f "$M4" || M4=m4 ;;
83 esac
84 # Some non-GNU m4's don't reject the --help option, so give them /dev/null.
85 case `$M4 --help </dev/null 2>&1` in
86 *reload-state*);;
87 *) echo "$me: Autoconf requires GNU m4 1.4 or later" >&2; exit 1 ;;
88 esac
90 # Variables.
91 : ${AC_MACRODIR=@datadir@}
92 : ${AC_ACLOCALDIR=`(aclocal --print-ac-dir) 2>/dev/null`}
93 : ${AWK=@AWK@}
94 debug=false
95 # Trace Autoconf's initialization?
96 initialization=false
97 localdir=
98 outfile=
99 # Exit status.
100 status=0
101 # Tasks:
102 # - trace
103 # Trace the first arguments of some macros
104 # - script
105 # Produce the configure script (default)
106 task=script
107 : ${TMPDIR=/tmp}
108 tmpbase=$TMPDIR/ac$$
109 tmpin=${tmpbase}.in
110 tmpout=${tmpbase}.out
111 silent_m4=${tmpbase}silent.m4
112 trace_m4=${tmpbase}trace.m4
113 translate_awk=${tmpbase}trans.awk
114 verbose=:
116 # Parse command line
117 while test $# -gt 0 ; do
118 case "$1" in
119 --version | --vers* | -V )
120 echo "$version" ; exit 0 ;;
121 --help | --h* | -h )
122 echo "$usage"; exit 0 ;;
124 --debug | --d* | -d )
125 debug=:; shift ;;
126 --verbose | --verb* | -v )
127 verbose=echo
128 shift;;
130 --localdir=* | --l*=* )
131 localdir=`echo "$1" | sed -e 's/^[^=]*=//'`
132 shift ;;
133 --localdir | --l* | -l )
134 shift
135 test $# = 0 && { echo "$help" >&2; exit 1; }
136 localdir=$1
137 shift ;;
139 --macrodir=* | --m*=* )
140 AC_MACRODIR=`echo "$1" | sed -e 's/^[^=]*=//'`
141 shift ;;
142 --macrodir | --m* | -m )
143 shift
144 test $# = 0 && { echo "$help" >&2; exit 1; }
145 AC_MACRODIR=$1
146 shift ;;
148 --install )
149 task=install
150 shift;;
152 --trace | -t )
153 task=trace
154 shift
155 test $# = 0 && { echo "$help" >&2; exit 1; }
156 traces="$traces '$1'"
157 shift ;;
158 --trace=* )
159 task=trace
160 traces="$traces '"`echo "$1" | sed -e 's/^[^=]*=//;s/:.*//'`"'"
161 shift ;;
162 --initialization | -i )
163 initialization=:
164 shift;;
166 --output | -o )
167 shift
168 test $# = 0 && { echo "$help" >&2; exit 1; }
169 outfile=$1
170 shift ;;
171 --output=* )
172 outfile=`echo "$1" | sed -e 's/^[^=]*=//'`
173 shift ;;
175 -- ) # Stop option processing
176 shift; break ;;
177 - ) # Use stdin as input.
178 break ;;
179 -* )
180 exec >&2
181 echo "$me: invalid option $1"
182 echo "$help"
183 exit 1 ;;
185 break ;;
186 esac
187 done
189 # Running m4.
190 test -n "$localdir" && use_localdir="-I$localdir"
191 run_m4="$M4 $use_localdir -I $AC_MACRODIR autoconf.m4"
192 run_m4f="$M4 $use_localdir --reload $AC_MACRODIR/autoconf.m4f"
194 # Find the input file.
195 case $# in
196 0) infile=configure.in
197 test $task = script && test "x$outfile" = x && outfile=configure;;
198 1) infile=$1 ;;
199 *) exec >&2
200 echo "$me: invalid number of arguments."
201 echo "$help"
202 exit 1 ;;
203 esac
205 # Trap on 0 to stop playing with `rm'.
206 $debug ||
208 trap 'status=$?; rm -f $tmpbase* && exit $status' 0
209 trap 'exit $?' 1 2 13 15
212 if test z$infile = z-; then
213 infile=$tmpin
214 cat >$infile
215 elif test ! -r "$infile"; then
216 echo "$me: $infile: No such file or directory" >&2
217 exit 1
220 # Output is produced into FD 4. Prepare it.
221 case "x$outfile" in
222 x- | x ) # Output to stdout
223 exec 4>&1 ;;
225 exec 4>$outfile;;
226 esac
228 # Initializations are performed. Proceed to the main task.
229 case $task in
231 ## --------------------- ##
232 ## Generate the script. ##
233 ## --------------------- ##
234 script)
235 $run_m4f $infile >$tmpout || exit 2
237 # You could add your own prefixes to pattern if you wanted to check for
238 # them too, e.g. pattern='\(AC_\|ILT_\)', except that UNIX sed doesn't do
239 # alternation.
240 pattern="A[CHM]_"
242 if grep "^[^#]*$pattern" $tmpout >/dev/null 2>&1; then
243 echo "$me: undefined macros:" >&2
244 sed -n "s/^[^#]*\\($pattern[_A-Za-z0-9]*\\).*/\\1/p" $tmpout |
245 while read macro; do
246 grep -n "^[^#]*$macro" $infile /dev/null
247 test $? = 1 && echo "***BUG in Autoconf--please report*** $macro"
248 done | sort -u >&2
249 status=1
252 if test -n "$outfile"; then
253 chmod +x $outfile
256 # Put the real line numbers into configure to make config.log more
257 # helpful. Because quoting can sometimes get really painful in m4,
258 # there are special @tokens@ to substitute.
259 $AWK '
261 sub(/[ ]*$/, "")
262 if ($0 == "")
264 if (!duplicate)
265 print
266 duplicate = 1
267 next
269 duplicate = 0
270 oline++
271 while (sub(/__oline__/, oline))
272 continue
273 while (sub(/@<:@/, "["))
274 continue
275 while (sub(/@:>@/, "]"))
276 continue
277 while (sub(/@S\|@/, "$"))
278 continue
279 while (sub(/@%:@/, "#"))
280 continue
281 print
282 }' <$tmpout >&4
283 ;; # End of the task script.
287 ## -------------- ##
288 ## Trace macros. ##
289 ## -------------- ##
290 trace)
291 # `errprint' must be silent when we run `m4 --trace', otherwise there
292 # can be warnings mixed with traces in m4's stderr.
293 cat >$silent_m4 <<\EOF
294 define(`errprint')dnl
296 # A program to trace m4 macros.
297 cat >$trace_m4 <<\EOF
298 divert(-1)
299 changequote([, ])
300 define([_at],
301 [ifelse([$#], [1], [],
302 [$#], [2], [[[$2]]],
303 [[[$2]][$1]_at([$1], shift(shift($@)))])])
304 define([_star],
305 [ifelse([$#], [1], [],
306 [$#], [2], [smash([$2])],
307 [smash([$2])[$1]_star([$1], shift(shift($@)))])])
309 # Smash quotes its result.
310 define([smash],
311 [patsubst(patsubst(patsubst([[[$1]]],
315 ]+],
316 [ ]),
317 [^ *\(.*\) *$], [[\1]])])
318 define([args],
319 [shift(shift(shift(shift(shift($@)))))])
320 define([at],
321 [_at([$1], args($@))])
322 define([star],
323 [_star([$1], args($@))])
325 # A program to translate user tracing requests into m4 macros.
326 cat >$translate_awk <<\EOF
327 function trans (arg, sep)
329 # File name.
330 if (arg == "f")
331 return "$1"
332 # Line number.
333 if (arg == "l")
334 return "$2"
335 # Depth.
336 if (arg == "d")
337 return "$3"
338 # Name (also available as $0).
339 if (arg == "n")
340 return "$4"
341 # Escaped dollar.
342 if (arg == "$")
343 return "$"
345 # $@, list of quoted effective arguments.
346 if (arg == "@")
347 return "]at([" (separator ? separator : ",") "], $@)["
348 # $*, list of unquoted effective arguments.
349 if (arg == "*")
350 return "]star([" (separator ? separator : ":") "], $@)["
353 function error (message)
355 print message >"/dev/stderr"
356 exit 1
360 # Accumulate the whole input.
361 request = request $0 "\n"
364 END {
365 res = ""
367 for (cp = request; cp; cp = substr (cp, 2))
369 char = substr (cp, 1, 1)
370 if (char == "$")
372 if (match (cp, /^\$[0-9]+/))
374 # $n -> $(n + 4)
375 res = res "$" (substr (cp, 2, RLENGTH - 1) + 4)
376 cp = substr (cp, RLENGTH)
378 else if (substr (cp, 2, 1) ~ /[fldn$@*]/)
380 # $x, no separator given.
381 res = res trans(substr (cp, 2, 1))
382 cp = substr (cp, 2)
384 else if (substr (cp, 2, 1) == "{")
386 # ${sep}x, long separator.
387 end = index (cp, "}")
388 if (!end)
389 error("invalid escape: " cp)
390 separator = substr (cp, 3, end - 3)
391 if (substr (cp, end + 1, 1) ~ /[*@]/)
392 res = res trans(substr (cp, end + 1, 1), separator)
393 else
394 error("invalid escape: " cp)
395 cp = substr (cp, end + 1)
397 else if (substr (cp, 3, 1) ~ /[$@]/)
399 # $sx, short separator `s'.
400 res = res trans(substr (cp, 3, 1), substr (cp, 2, 1))
401 cp = substr(cp, 3)
403 else
405 error("invalid escape: " substr (cp, 1, 2))
408 else
409 res = res char
411 print res
414 # Extract both the m4 program and the m4 options from TRACES.
415 eval set dummy "$traces"
416 shift
417 for trace
419 # The request may be several lines long, hence sed has to quit.
420 trace_opt="$trace_opt -t "`echo "$trace" | sed -e 's/:.*//;q'`
421 case "$trace" in
422 *:*)
423 # The `]])' are closing the `define' which is started by sed.
424 # This is to cope with multiple line requests.
425 echo "$trace]])" |
426 sed -e '1s/^\([^:]*\):\(.*\)$/define([AT_\1], [[\2/';;
428 # Default request.
429 echo "define([AT_$trace], [[\$f:\$l:\$n:\$*]])";;
430 esac |
431 $AWK -f $translate_awk >>$trace_m4 || exit 1
432 done
433 echo "divert(0)dnl" >>$trace_m4
435 # Do we trace the initialization?
436 if $initialization; then
437 run_m4_trace="$run_m4 $trace_opt -daflq $silent"
438 else
439 run_m4_trace="$run_m4f $trace_opt -daflq $silent"
442 # Run m4 on the input file to get traces.
443 $verbose "Running $run_m4_trace $infile" >&2
444 $run_m4_trace $infile 2>&1 >/dev/null |
445 sed -e 's/^m4trace:\([^:][^:]*\):\([0-9][0-9]*\): -\([0-9][0-9]*\)- \([^(][^(]*\)(\(.*\)$/AT_\4([\1], [\2], [\3], [\4], \5/' \
446 -e 's/^m4trace:\([^:][^:]*\):\([0-9][0-9]*\): -\([0-9][0-9]*\)- \(.*\)$/AT_\4([\1], [\2], [\3], [\4])/' >>$trace_m4
448 # Now we are ready to run m4 to process the trace file.
449 # It makes no sense to try to transform __oline__.
450 $verbose "Running $M4 $trace_m4" >&2
451 $M4 $trace_m4 |
452 sed '
453 s/@<:@/[/g
454 s/@:>@/]/g
455 s/@S|@/$/g
456 s/@%:@/#/g
457 ' >&4
463 ## -------------------------------------------------------- ##
464 ## Task --install. Install links to the library m4 files. ##
465 ## -------------------------------------------------------- ##
466 install)
467 # An m4 program that reports what macros are requested, and where
468 # they were defined.
469 cat >$tmpin <<\EOF
470 dnl Keep the definition of the old AC_DEFUN
471 define([AC_DEFUN_OLD], defn([AC_DEFUN]))
473 dnl Define the macro so that the first time it is expanded, it reports
474 dnl on stderr its name, and where it was defined.
475 define([AC_DEFUN],
476 [AC_DEFUN_OLD([$1],
477 [ifdef([AC_DECLARED{$1}],,
478 [define([AC_DECLARED{$1}])errprint(]]__file__:__line__:[[ [$1]
479 )])dnl
480 ][$2])])
482 dnl All the includes must be disabled. If they are not, since people don't
483 dnl protect the first argument of AC_DEFUN, then, if read a second time
484 dnl this argument will be expanded, and we'll get pure junk out of m4.
485 define([AC_INCLUDE])
487 # Run m4 with all the library files, discard stdout, save stderr.
488 $verbose Running $run_m4f -dipa -t m4_include -t m4_sinclude $tmpin $localdir/*.m4 $AC_ACLOCALDIR/*.m4 $infile >&2
489 $run_m4f -dipa -t m4_include -t m4_sinclude $tmpin $localdir/*.m4 $AC_ACLOCALDIR/*.m4 $infile >/dev/null 2>$tmpout
490 # Keep only the good lines, there may be other outputs
491 grep '^[^: ]*:[0-9][0-9]*:[^:]*$' $tmpout >$tmpin
492 # Extract the files that are not in the local dir, and install the links.
493 # Save in $tmpout the list of installed links.
494 >$tmpout
495 $verbose "Required macros:" >&2
496 $verbose "`sed -e 's/^/| /' $tmpin`" >&2
497 cat $tmpin |
498 while read line
500 file=`echo "$line" | sed -e 's/:.*//'`
501 filename=`echo "$file" | sed -e 's,.*/,,'`
502 macro=`echo "$line" | sed -e 's/.*:[ ]*//'`
503 if test -f "$file" && test "x$file" != "x$infile"; then
504 if test -f $localdir/$filename; then
505 $verbose "$filename already installed" >&2
506 else
507 $verbose "installing $file which provides $macro" >&2
508 ln -s "$file" "$localdir/$filename" ||
509 cp "$file" "$localdir/$filename" ||
511 echo "$me: cannot link from $file to $localdir/$filename" >&2
512 exit 1
515 echo "$localdir/$filename" >>$tmpout
517 done
518 # Now that we have installed the links, and that we know that the
519 # user needs the FILES, check that there is an exact correspondence.
520 # Use yourself to get the list of the included files.
521 export AC_ACLOCALDIR
522 export AC_MACRODIR
523 # Not m4_s?include, because it would catch acsite and aclocal, which
524 # we don't care about.
525 $0 -l "$localdir" -t AC_INCLUDE $infile |
526 sed -e 's/^[^:]*:[^:]*:[^:]*://g' |
527 sort |
528 uniq >$tmpin
529 # All the included files are needed.
530 for file in `cat $tmpin`;
532 if fgrep "$file" $tmpout >/dev/null 2>&1; then :; else
533 echo "\`$file' is uselessly included" >&2
535 done
536 # All the needed files are included.
537 for file in `sort $tmpout | uniq`;
539 if fgrep "$file" $tmpin >/dev/null 2>&1; then :; else
540 echo "\`$file' is not included" >&2
542 done
547 ## ------------ ##
548 ## Unknown task ##
549 ## ------------ ##
551 *)echo "$me: internal error: unknown task: $task" >&2
552 exit 1
553 esac
555 exit $status