use shell functions to speed up autotest and produce slimmer testsuites
[autoconf.git] / bin / autoheader.in
blob9c19ea3a263d43fe50d1c764c6b29ff8691b3c12
1 #! @PERL@
2 # -*- Perl -*-
3 # @configure_input@
5 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
6     if 0;
8 # autoheader -- create `config.h.in' from `configure.ac'
10 # Copyright (C) 1992, 1993, 1994, 1996, 1998, 1999, 2000, 2001, 2002,
11 # 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
26 # Written by Roland McGrath.
27 # Rewritten in Perl by Akim Demaille.
29 BEGIN
31   my $datadir = $ENV{'autom4te_perllibdir'} || '@datadir@';
32   unshift @INC, "$datadir";
34   # Override SHELL.  On DJGPP SHELL may not be set to a shell
35   # that can handle redirection and quote arguments correctly,
36   # e.g.: COMMAND.COM.  For DJGPP always use the shell that configure
37   # has detected.
38   $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
41 use Autom4te::ChannelDefs;
42 use Autom4te::Channels;
43 use Autom4te::Configure_ac;
44 use Autom4te::FileUtils;
45 use Autom4te::General;
46 use Autom4te::XFile;
47 use strict;
49 # Using `do FILE', we need `local' vars.
50 use vars qw ($config_h %verbatim %symbol);
52 # Lib files.
53 my $autom4te = $ENV{'AUTOM4TE'} || '@bindir@/@autom4te-name@';
54 local $config_h;
55 my $config_h_in;
56 my @prepend_include;
57 my @include;
60 # $HELP
61 # -----
62 $help = "Usage: $0 [OPTION] ... [TEMPLATE-FILE]
64 Create a template file of C \`\#define\' statements for \`configure\' to
65 use.  To this end, scan TEMPLATE-FILE, or \`configure.ac\' if present,
66 or else \`configure.in\'.
68   -h, --help               print this help, then exit
69   -V, --version            print version number, then exit
70   -v, --verbose            verbosely report processing
71   -d, --debug              don\'t remove temporary files
72   -f, --force              consider all files obsolete
73   -W, --warnings=CATEGORY  report the warnings falling in CATEGORY
75 " . Autom4te::ChannelDefs::usage () . "
77 Library directories:
78   -B, --prepend-include=DIR  prepend directory DIR to search path
79   -I, --include=DIR          append directory DIR to search path
81 Report bugs to <bug-autoconf\@gnu.org>.
85 # $VERSION
86 # --------
87 $version = "autoheader (@PACKAGE_NAME@) @VERSION@
88 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
89 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
90 This is free software: you are free to change and redistribute it.
91 There is NO WARRANTY, to the extent permitted by law.
93 Written by Roland McGrath and Akim Demaille.
97 ## ---------- ##
98 ## Routines.  ##
99 ## ---------- ##
102 # parse_args ()
103 # -------------
104 # Process any command line arguments.
105 sub parse_args ()
107   my $srcdir;
109   parse_WARNINGS;
110   getopt ('I|include=s'         => \@include,
111           'B|prepend-include=s' => \@prepend_include,
112           'W|warnings=s'        => \&parse_warnings);
114   if (! @ARGV)
115     {
116       my $configure_ac = require_configure_ac;
117       push @ARGV, $configure_ac;
118     }
122 ## -------------- ##
123 ## Main program.  ##
124 ## -------------- ##
126 mktmpdir ('ah');
127 switch_warning 'obsolete';
128 parse_args;
130 # Preach.
131 my $config_h_top = find_file ("config.h.top?",
132                               reverse (@prepend_include), @include);
133 my $config_h_bot = find_file ("config.h.bot?",
134                               reverse (@prepend_include), @include);
135 my $acconfig_h = find_file ("acconfig.h?",
136                             reverse (@prepend_include), @include);
137 if ($config_h_top || $config_h_bot || $acconfig_h)
138   {
139     my $msg = << "END";
140     Using auxiliary files such as \`acconfig.h\', \`config.h.bot\'
141     and \`config.h.top\', to define templates for \`config.h.in\'
142     is deprecated and discouraged.
144     Using the third argument of \`AC_DEFINE\' and
145     \`AC_DEFINE_UNQUOTED\' allows one to define a template without
146     \`acconfig.h\':
148       AC_DEFINE([NEED_FUNC_MAIN], 1,
149                 [Define if a function \`main\' is needed.])
151     More sophisticated templates can also be produced, see the
152     documentation.
154     $msg =~ s/^    /WARNING: /gm;
155     msg 'obsolete', $msg;
156   }
158 # Set up autoconf.
159 my $autoconf = "'$autom4te' --language=autoconf ";
160 $autoconf .= join (' ', map { "--include='$_'" } @include);
161 $autoconf .= join (' ', map { "--prepend-include='$_'" } @prepend_include);
162 $autoconf .= ' --debug' if $debug;
163 $autoconf .= ' --force' if $force;
164 $autoconf .= ' --verbose' if $verbose;
166 # ----------------------- #
167 # Real work starts here.  #
168 # ----------------------- #
170 # Source what the traces are trying to tell us.
171 verb "$me: running $autoconf to trace from $ARGV[0]";
172 xsystem ("$autoconf"
173          # If you change this list, update the
174          # `Autoheader-preselections' section of autom4te.in.
175          . ' --trace AC_CONFIG_HEADERS:\'$$config_h ||= \'"\'"\'$1\'"\'"\';\''
176          . ' --trace AH_OUTPUT:\'$$verbatim{\'"\'"\'$1\'"\'"\'} = \'"\'"\'$2\'"\'"\';\''
177          . ' --trace AC_DEFINE_TRACE_LITERAL:\'$$symbol{\'"\'"\'$1\'"\'"\'} = 1;\''
178          . " $ARGV[0] >$tmp/traces.pl");
180 local (%verbatim, %symbol);
181 debug "$me: \`do'ing $tmp/traces.pl:\n" . `sed 's/^/| /' $tmp/traces.pl`;
182 do "$tmp/traces.pl";
183 warn "couldn't parse $tmp/traces.pl: $@" if $@;
184 unless ($config_h)
185   {
186     error "error: AC_CONFIG_HEADERS not found in $ARGV[0]";
187     exit 1;
188   }
190 # We template only the first CONFIG_HEADER.
191 $config_h =~ s/ .*//;
192 # Support "outfile[:infile]", defaulting infile="outfile.in".
193 ($config_h, $config_h_in) = split (':', $config_h, 2);
194 $config_h_in ||= "$config_h.in";
196 # %SYMBOL might contain things like `F77_FUNC(name,NAME)', but we keep
197 # only the name of the macro.
198 %symbol = map { s/\(.*//; $_ => 1 } keys %symbol;
200 my $out = new Autom4te::XFile (">$tmp/config.hin");
202 # Don't write "do not edit" -- it will get copied into the
203 # config.h, which it's ok to edit.
204 print $out "/* $config_h_in.  Generated from $ARGV[0] by autoheader.  */\n";
206 # Dump the top.
207 if ($config_h_top)
208   {
209     my $in = new Autom4te::XFile ($config_h_top);
210     while ($_ = $in->getline)
211       {
212         print $out $_;
213       }
214   }
216 # Dump `acconfig.h', except for its bottom portion.
217 if ($acconfig_h)
218   {
219     my $in = new Autom4te::XFile ($acconfig_h);
220     while ($_ = $in->getline)
221       {
222         last if /\@BOTTOM\@/;
223         next if /\@TOP\@/;
224         print $out $_;
225       }
226   }
228 # Dump the templates from `configure.ac'.
229 foreach (sort keys %verbatim)
230   {
231     print $out "\n$verbatim{$_}\n";
232   }
234 # Dump bottom portion of `acconfig.h'.
235 if ($acconfig_h)
236   {
237     my $in = new Autom4te::XFile ($acconfig_h);
238     my $dump = 0;
239     while ($_ = $in->getline)
240       {
241         print $out $_ if $dump;
242         $dump = 1  if /\@BOTTOM\@/;
243       }
244   }
246 # Dump the bottom.
247 if ($config_h_bot)
248   {
249     my $in = new Autom4te::XFile ($config_h_bot);
250     while ($_ = $in->getline)
251       {
252         print $out $_;
253       }
254   }
256 $out->close;
258 # Check that all the symbols have a template.
260   my $in = new Autom4te::XFile ("$tmp/config.hin");
261   my $suggest_ac_define = 1;
262   while ($_ = $in->getline)
263     {
264       my ($symbol) = /^\#\s*\w+\s+(\w+)/
265         or next;
266       delete $symbol{$symbol};
267     }
268   foreach (sort keys %symbol)
269     {
270       msg 'syntax', "warning: missing template: $_";
271       if ($suggest_ac_define)
272         {
273           msg 'syntax',  "Use AC_DEFINE([$_], [], [Description])";
274           $suggest_ac_define = 0;
275         }
277     }
278   exit 1
279     if keys %symbol;
282 update_file ("$tmp/config.hin", "$config_h_in", $force);
284 ### Setup "GNU" style for perl-mode and cperl-mode.
285 ## Local Variables:
286 ## perl-indent-level: 2
287 ## perl-continued-statement-offset: 2
288 ## perl-continued-brace-offset: 0
289 ## perl-brace-offset: 0
290 ## perl-brace-imaginary-offset: 0
291 ## perl-label-offset: -2
292 ## cperl-indent-level: 2
293 ## cperl-brace-offset: 0
294 ## cperl-continued-brace-offset: 0
295 ## cperl-label-offset: -2
296 ## cperl-extra-newline-before-brace: t
297 ## cperl-merge-trailing-else: nil
298 ## cperl-continued-statement-offset: 2
299 ## End: