* aclocal.in: Use Automake::General and Automake::XFile.
[platform/upstream/automake.git] / aclocal.in
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
4
5 eval 'exec @PERL@ -S $0 ${1+"$@"}'
6     if 0;
7
8 # aclocal - create aclocal.m4 by scanning configure.ac
9 # Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2, or (at your option)
14 # any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 # 02111-1307, USA.
25
26 # Written by Tom Tromey <tromey@cygnus.com>.
27
28 BEGIN
29 {
30   my $prefix = "@prefix@";
31   my $perllibdir = $ENV{'perllibdir'} || "@datadir@/@PACKAGE@";
32   unshift @INC, "$perllibdir";
33 }
34
35 use Automake::General;
36 use Automake::XFile;
37
38 # Some constants.
39 $VERSION = "@VERSION@";
40 $PACKAGE = "@PACKAGE@";
41 $prefix = "@prefix@";
42 # Note that this isn't pkgdatadir, but a separate directory.
43 $acdir = "@datadir@/aclocal";
44
45 # Some globals.
46
47 # Exit status.
48 $exit_status = 0;
49
50 # Name of the top autoconf input: `configure.ac' or `configure.in'.
51 $configure_ac = find_configure_ac;
52
53 # Text to output.
54 $output = '';
55
56 # Output file name.
57 $output_file = 'aclocal.m4';
58
59 # Which macros have been seen.
60 %macro_seen = ();
61
62 # Which files have been seen.
63 %file_seen = ();
64
65 # Map macro names to file names.
66 %map = ();
67
68 # Map file names to file contents.
69 %file_contents = ();
70
71 # How much to say.
72 $verbose = 0;
73
74 # Map from obsolete macros to hints for new macros.
75 # If you change this, change the corresponding list in automake.in.
76 # FIXME: should just put this into a single file.
77 my %obsolete_macros =
78     (
79      'AC_FEATURE_CTYPE'         => "use `AC_HEADER_STDC'",
80      'AC_FEATURE_ERRNO'         => "add `strerror' to `AC_REPLACE_FUNCS(...)'",
81      'AC_FEATURE_EXIT'          => '',
82      'AC_SYSTEM_HEADER'         => '',
83
84      # Note that we do not handle this one, because it is still run
85      # from AM_CONFIG_HEADER.  So we deal with it specially in
86      # &scan_autoconf_files.
87      # 'AC_CONFIG_HEADER'       => "use `AM_CONFIG_HEADER'",
88
89      'fp_C_PROTOTYPES'          => "use `AM_C_PROTOTYPES'",
90      'fp_PROG_CC_STDC'          => "use `AM_PROG_CC_STDC'",
91      'fp_PROG_INSTALL'          => "use `AC_PROG_INSTALL'",
92      'fp_WITH_DMALLOC'          => "use `AM_WITH_DMALLOC'",
93      'fp_WITH_REGEX'            => "use `AM_WITH_REGEX'",
94      'gm_PROG_LIBTOOL'          => "use `AM_PROG_LIBTOOL'",
95      'jm_MAINTAINER_MODE'       => "use `AM_MAINTAINER_MODE'",
96      'md_TYPE_PTRDIFF_T'        => "use `AM_TYPE_PTRDIFF_T'",
97      'ud_PATH_LISPDIR'          => "use `AM_PATH_LISPDIR'",
98      'ud_GNU_GETTEXT'           => "use `AM_GNU_GETTEXT'",
99
100      # Now part of autoconf proper, under a different name.
101      'AM_FUNC_FNMATCH'          => "use `AC_FUNC_FNMATCH'",
102      'fp_FUNC_FNMATCH'          => "use `AC_FUNC_FNMATCH'",
103      'AM_SANITY_CHECK_CC'       => "automatically done by `AC_PROG_CC'",
104      'AM_PROG_INSTALL'          => "use `AC_PROG_INSTALL'",
105      'AM_EXEEXT'                => "automatically done by `AC_PROG_(CC|CXX|F77)'",
106      'AM_CYGWIN32'              => "use `AC_CYGWIN'",
107      'AM_MINGW32'               => "use `AC_MINGW32'",
108      'AM_FUNC_MKTIME'           => "use `AC_FUNC_MKTIME'",
109
110 # These aren't quite obsolete.
111 #      'md_PATH_PROG',
112      );
113
114 # Regexp to match the above macros.
115 $obsolete_rx = '\b(' . join ('|', keys %obsolete_macros) . ')\b';
116
117 # Matches a macro definition.
118 $ac_defun_rx = "AC_DEFUN\\(\\[?([^],)\n]+)\\]?";
119
120 # Matches an AC_REQUIRE line.
121 $ac_require_rx = "AC_REQUIRE\\(\\[?([^])]*)\\]?\\)";
122
123 \f
124
125 local (@dirlist) = &parse_arguments (@ARGV);
126 &scan_m4_files (@dirlist);
127 &scan_configure;
128 if (! $exit_status)
129 {
130     &write_aclocal;
131 }
132 &check_acinclude;
133
134 exit $exit_status;
135
136 ################################################################
137
138 # Print usage and exit.
139 sub usage
140 {
141     local ($status) = @_;
142
143     print "Usage: aclocal [OPTIONS] ...\n\n";
144     print "\
145 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
146
147   --acdir=DIR           directory holding config files
148   --help                print this help, then exit
149   -I DIR                add directory to search list for .m4 files
150   --output=FILE         put output in FILE (default aclocal.m4)
151   --print-ac-dir        print name of directory holding m4 files
152   --verbose             don't be silent
153   --version             print version number, then exit
154
155 Report bugs to <bug-automake\@gnu.org>.\n";
156
157     exit $status;
158 }
159
160 # Parse command line.
161 sub parse_arguments
162 {
163     local (@arglist) = @_;
164     local (@dirlist);
165     local ($print_and_exit) = 0;
166
167     while (@arglist)
168     {
169         if ($arglist[0] =~ /^--acdir=(.+)$/)
170         {
171             $acdir = $1;
172         }
173         elsif ($arglist[0] =~/^--output=(.+)$/)
174         {
175             $output_file = $1;
176         }
177         elsif ($arglist[0] eq '-I')
178         {
179             shift (@arglist);
180             push (@dirlist, $arglist[0]);
181         }
182         elsif ($arglist[0] eq '--print-ac-dir')
183         {
184             $print_and_exit = 1;
185         }
186         elsif ($arglist[0] eq '--verbose')
187         {
188             ++$verbose;
189         }
190         elsif ($arglist[0] eq '--version')
191         {
192             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
193             print "Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.\n";
194             print "This is free software; see the source for copying conditions.  There is NO\n";
195             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
196             print "Written by Tom Tromey <tromey\@cygnus.com>\n";
197             exit 0;
198         }
199         elsif ($arglist[0] eq '--help')
200         {
201             &usage (0);
202         }
203         else
204         {
205             die "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
206         }
207
208         shift (@arglist);
209     }
210
211     if ($print_and_exit)
212     {
213         print $acdir, "\n";
214         exit 0;
215     }
216
217     # Search our install directory last.
218     push (@dirlist, $acdir);
219
220     return @dirlist;
221 }
222
223 ################################################################
224
225 sub scan_configure
226 {
227     die "aclocal: `configure.ac' or `configure.in' is required\n"
228         if !$configure_ac;
229
230     open (CONFIGURE, $configure_ac)
231         || die "aclocal: couldn't open `$configure_ac': $!\n";
232
233     # Make sure we include acinclude.m4 if it exists.
234     if (-f 'acinclude.m4')
235     {
236         &add_file ('acinclude.m4');
237     }
238
239     while (<CONFIGURE>)
240     {
241         # Remove comments from current line.
242         s/\bdnl\b.*$//;
243         s/\#.*$//;
244
245         if (/$obsolete_rx/o)
246         {
247             local ($hint) = '';
248             if ($obsolete_macros{$1} ne '')
249             {
250                 $hint = '; ' . $obsolete_macros{$1};
251             }
252             warn "aclocal: $configure_ac: $.: `$1' is obsolete$hint\n";
253             $exit_status = 1;
254             next;
255         }
256
257         # Search for things we know about.  The "search" sub is
258         # constructed dynamically by scan_m4_files.  The last
259         # parenthethical match makes sure we don't match things that
260         # look like macro assignments or AC_SUBSTs.
261         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
262         {
263             # Macro not found, but AM_ prefix found.
264             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
265             $exit_status = 1;
266         }
267     }
268
269     close (CONFIGURE);
270 }
271
272 ################################################################
273
274 # Check macros in acinclude.m4.  If one is not used, warn.
275 sub check_acinclude
276 {
277     local ($key);
278
279     foreach $key (keys %map)
280     {
281         next unless $map{$key} eq 'acinclude.m4';
282         if (! $macro_seen{$key})
283         {
284             # FIXME: should print line number of acinclude.m4.
285             warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
286         }
287     }
288 }
289
290 ################################################################
291
292 # Scan all the installed m4 files and construct a map.
293 sub scan_m4_files
294 {
295     local (@dirlist) = @_;
296
297     # First, scan acinclude.m4 if it exists.
298     if (-f 'acinclude.m4')
299     {
300         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
301     }
302
303     local ($m4dir);
304     foreach $m4dir (@dirlist)
305     {
306         opendir (DIR, $m4dir)
307             || die "aclocal: couldn't open directory `$m4dir': $!\n";
308         local ($file, $fullfile);
309         foreach $file (sort grep (! /^\./, readdir (DIR)))
310         {
311             # Only examine .m4 files.
312             next unless $file =~ /\.m4$/;
313
314             # Skip some files when running out of srcdir.
315             next if $file eq 'aclocal.m4';
316
317             $fullfile = $m4dir . '/' . $file;
318             $file_contents{$fullfile} = &scan_file ($fullfile);
319         }
320         closedir (DIR);
321     }
322
323     # Construct a new function that does the searching.  We use a
324     # function (instead of just evalling $search in the loop) so that
325     # "die" is correctly and easily propagated if run.
326     my $search = "sub search {\nmy \$found = 0;\n";
327     foreach my $key (reverse sort keys %map)
328     {
329         # EXPR is a regexp matching the name of the macro.
330         (my $expr = $key) =~ s/(\W)/\\$1/g;
331         $search .= ('if (/\b' . $key . '\b/) { & add_macro (' . $key
332                     . '); $found = 1; }' . "\n");
333     }
334     $search .= "return \$found;\n};\n";
335     eval $search;
336     die "internal error: $@\n search is $search" if $@;
337 }
338
339 ################################################################
340
341 # Add a macro to the output.
342 sub add_macro
343 {
344     local ($macro) = @_;
345
346     # We want to ignore AC_ macros.  However, if an AC_ macro is
347     # defined in (eg) acinclude.m4, then we want to make sure we mark
348     # it as seen.
349     return if $macro =~ /^AC_/ && ! defined $map{$macro};
350
351     if (! defined $map{$macro})
352     {
353         warn "aclocal: macro `$macro' required but not defined\n";
354         $exit_status = 1;
355         return;
356     }
357
358     print STDERR "aclocal: saw macro $macro\n" if $verbose;
359     $macro_seen{$macro} = 1;
360     &add_file ($map{$macro});
361 }
362
363 # Add a file to output.
364 sub add_file
365 {
366     local ($file) = @_;
367
368     # Only add a file once.
369     return if ($file_seen{$file});
370     $file_seen{$file} = 1;
371
372     $output .= $file_contents{$file} . "\n";
373     local ($a, @rlist);
374     foreach (split ("\n", $file_contents{$file}))
375     {
376         # This is a hack for Perl 4.
377         $a = $_;
378         if ($a =~ /$ac_require_rx/g)
379         {
380             push (@rlist, $1);
381         }
382
383         # Remove comments from current line.
384         s/\bdnl\b.*$//;
385         s/\#.*$//;
386
387         # The search function is constructed dynamically by
388         # scan_m4_files.  The last parenthethical match makes sure we
389         # don't match things that look like macro assignments or
390         # AC_SUBSTs.
391         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
392         {
393             # Macro not found, but AM_ prefix found.
394             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
395             $exit_status = 1;
396         }
397     }
398
399     local ($macro);
400     foreach $macro (@rlist)
401     {
402         &add_macro ($macro);
403     }
404 }
405
406 # Scan a single M4 file.  Return contents.
407 sub scan_file
408 {
409     local ($file) = @_;
410
411     my $fh = new Automake::XFile $file;
412     my $contents = '';
413     while ($_ = $fh->getline)
414     {
415         # Ignore `##' lines.
416         next if /^##/;
417
418         $contents .= $_;
419
420         if (/$ac_defun_rx/)
421         {
422             if (! defined $map{$1})
423             {
424                 $map{$1} = $file;
425             }
426
427             # Note: we used to give an error here if we saw a
428             # duplicated macro.  However, this turns out to be
429             # extremely unpopular.  It causes actual problems which
430             # are hard to work around, especially when you must
431             # mix-and-match tool versions.
432
433             print STDERR "aclocal: found macro $1 in $file: $.\n" if $verbose;
434         }
435     }
436
437     return $contents;
438 }
439
440 ################################################################
441
442 # Write output.
443 sub write_aclocal
444 {
445     return if ! length ($output);
446
447     print STDERR "aclocal: writing $output_file\n" if $verbose;
448
449     my $out = new Automake::XFile "> $output_file";
450     print $out
451 "# $output_file generated automatically by aclocal $VERSION -*- Autoconf -*-
452
453 # Copyright 1996, 1997, 1998, 1999, 2000, 2001
454 # Free Software Foundation, Inc.
455 # This file is free software; the Free Software Foundation
456 # gives unlimited permission to copy and/or distribute it,
457 # with or without modifications, as long as this notice is preserved.
458
459 # This program is distributed in the hope that it will be useful,
460 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
461 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
462 # PARTICULAR PURPOSE.
463
464 $output";
465 }