ylwrap patch from ian
[automake.git] / aclocal.in
blobb6bc6846d1991f21cc30367d786ca2311bcae0e9
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 # aclocal - create aclocal.m4 by scanning configure.in
6 # Copyright (C) 1996, 1997 Free Software Foundation, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 # 02111-1307, USA.
23 # Written by Tom Tromey <tromey@cygnus.com>.
25 eval 'exec @PERL@ -S $0 ${1+"$@"}'
26     if 0;
28 # aclocal - scan configure.in and generate aclocal.m4.
30 # Some constants.
31 $VERSION = "@VERSION@";
32 $PACKAGE = "@PACKAGE@";
33 $prefix = "@prefix@";
34 # Note that this isn't pkgdatadir, but a separate directory.
35 $acdir = "@datadir@/aclocal";
37 # Some globals.
39 # Exit status.
40 $exit_status = 0;
42 # Text to output.
43 $output = '';
45 # Output file name.
46 $output_file = 'aclocal.m4';
48 # Which macros have been seen.
49 %macro_seen = ();
51 # Which files have been seen.
52 %file_seen = ();
54 # Map macro names to file names.
55 %map = ();
57 # Map file names to file contents.
58 %file_contents = ();
60 # How much to say.
61 $verbosity = 0;
63 @obsolete_macros =
64     (
65      'AC_FEATURE_CTYPE',
66      'AC_FEATURE_ERRNO',
67      'AC_FEATURE_EXIT',
68      'AC_SYSTEM_HEADER',
69      'fp_C_PROTOTYPES',
70      'fp_FUNC_FNMATCH',
71      'fp_PROG_CC_STDC',
72      'fp_PROG_INSTALL',
73      'fp_WITH_DMALLOC',
74      'fp_WITH_REGEX',
75      'gm_PROG_LIBTOOL',
76      'jm_MAINTAINER_MODE',
77      'md_TYPE_PTRDIFF_T',
78      'ud_PATH_LISPDIR',
79      'ud_GNU_GETTEXT',
81      # Now part of autoconf proper, under a different name.
82      'AM_FUNC_FNMATCH',
83      'AM_SANITY_CHECK_CC',
85 # These aren't quite obsolete.
86 #      'md_PATH_PROG',
87 #      'ud_GNU_GETTEXT',
88 #      'ud_LC_MESSAGES',
89 #      'ud_WITH_NLS'
90      );
92 $obsolete_rx = '(' . join ('|', @obsolete_macros) . ')';
94 # Matches a macro definition.
95 $ac_defun_rx = "AC_DEFUN\\(\\[?([^],)\n]+)\\]?";
97 # Matches an AC_REQUIRE line.
98 $ac_require_rx = "AC_REQUIRE\\(\\[?([^])]*)\\]?\\)";
102 local (@dirlist) = &parse_arguments (@ARGV);
103 &scan_m4_files ($acdir, @dirlist);
104 &scan_configure;
105 if (! $exit_status)
107     &write_aclocal;
109 &check_acinclude;
111 exit $exit_status;
113 ################################################################
115 # Print usage and exit.
116 sub usage
118     local ($status) = @_;
120     print "Usage: aclocal [OPTIONS] ...\n\n";
121     print "Generate aclocal.m4 by scanning configure.in\n
122   --acdir=DIR           directory holding config files
123   --help                print this help, then exit
124   -I DIR                add directory to search list for .m4 files
125   --output=FILE         put output in FILE (default aclocal.m4)
126   --verbose             don't be silent
127   --version             print version number, then exit
129 Report bugs to <automake-bugs\@prep.ai.mit.edu>.\n";
131     exit $status;
134 # Parse command line.
135 sub parse_arguments
137     local (@arglist) = @_;
138     local (@dirlist);
140     while (@arglist)
141     {
142         if ($arglist[0] =~ /^--acdir=(.+)$/)
143         {
144             $acdir = $1;
145         }
146         elsif ($arglist[0] =~/^--output=(.+)$/)
147         {
148             $output_file = $1;
149         }
150         elsif ($arglist[0] eq '-I')
151         {
152             shift (@arglist);
153             push (@dirlist, $arglist[0]);
154         }
155         elsif ($arglist[0] eq '--verbose')
156         {
157             ++$verbosity;
158         }
159         elsif ($arglist[0] eq '--version')
160         {
161             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
162             print "Copyright (C) 1996, 1997 Free Software Foundation, Inc.\n";
163             print "This is free software; see the source for copying conditions.  There is NO\n";
164             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
165             print "Written by Tom Tromey <tromey\@cygnus.com>\n";
166             exit 0;
167         }
168         elsif ($arglist[0] eq '--help')
169         {
170             &usage (0);
171         }
172         else
173         {
174             die "aclocal: unrecognized option -- \`$arglist[0]'\nTry \`aclocal --help' for more information.\n";
175         }
177         shift (@arglist);
178     }
180     return @dirlist;
183 ################################################################
185 sub scan_configure
187     open (CONFIGURE, "configure.in")
188         || die "aclocal: couldn't open \`configure.in': $!\n";
190     # Make sure we include acinclude.m4 if it exists.
191     if (-f 'acinclude.m4')
192     {
193         &add_file ('acinclude.m4');
194     }
196     while (<CONFIGURE>)
197     {
198         # Remove comments from current line.
199         s/\bdnl\b.*$//;
200         s/\#.*$//;
202         if (/$obsolete_rx/o)
203         {
204             chop;
205             warn "aclocal: configure.in: $.: obsolete macro \`$_'\n";
206             $exit_status = 1;
207             next;
208         }
210         # Search for things we know about.  The "search" sub is
211         # constructed dynamically, above.
212         &search;
213     }
215     close (CONFIGURE);
218 ################################################################
220 # Check macros in acinclude.m4.  If one is not used, warn.
221 sub check_acinclude
223     local ($key);
225     foreach $key (keys %map)
226     {
227         next unless $map{$key} eq 'acinclude.m4';
228         if (! $macro_seen{$key})
229         {
230             # FIXME: should print line number of acinclude.m4.
231             warn "aclocal: macro \`$key' defined in acinclude.m4 but never used\n";
232         }
233     }
236 ################################################################
238 # Scan all the installed m4 files and construct a map.
239 sub scan_m4_files
241     local (@dirlist) = @_;
243     # First, scan acinclude.m4 if it exists.
244     if (-f 'acinclude.m4')
245     {
246         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
247     }
249     local ($m4dir);
250     foreach $m4dir (@dirlist)
251     {
252         opendir (DIR, $m4dir)
253             || die "aclocal: couldn't open directory \`$m4dir': $!\n";
254         local ($file, $fullfile, $expr);
255         foreach $file (sort grep (! /^\./, readdir (DIR)))
256         {
257             # Only examine .m4 files.
258             next unless $file =~ /\.m4$/;
260             # Skip some files when running out of srcdir.
261             next if $file eq 'aclocal.m4';
263             $fullfile = $m4dir . '/' . $file;
264             $file_contents{$fullfile} = &scan_file ($fullfile);
265         }
266         closedir (DIR);
267     }
269     # Construct a new function that does the searching.  We use a
270     # function (instead of just evalling $search in the loop) so that
271     # "die" is correctly and easily propagated if run.
272     local ($search, $expr, $key) = '';
273     foreach $key (keys %map)
274     {
275         # EXPR is a regexp matching the name of the macro.
276         ($expr = $key) =~ s/(\W)/\\$1/g;
277         $search .= "&add_macro ('" . $key . "') if /" . $expr . "/;\n";
278     }
279     eval 'sub search { ' . $search . '};';
280     die "internal error: $@\n search is $search " if $@;
283 ################################################################
285 # Add a macro to the output.
286 sub add_macro
288     local ($macro) = @_;
290     # We want to ignore AC_ macros.  However, if an AC_ macro is
291     # defined in (eg) acinclude.m4, then we want to make sure we mark
292     # it as seen.
293     return if $macro =~ /^AC_/ && ! defined $map{$macro};
295     if (! defined $map{$macro})
296     {
297         warn "aclocal: macro \`$macro' required but not defined\n";
298         $exit_status = 1;
299         return;
300     }
302     print STDERR "saw macro $macro\n" if $verbosity;
303     $macro_seen{$macro} = 1;
304     &add_file ($map{$macro});
307 # Add a file to output.
308 sub add_file
310     local ($file) = @_;
312     # Only add a file once.
313     return if ($file_seen{$file});
314     $file_seen{$file} = 1;
316     $output .= $file_contents{$file} . "\n";
317     local (@rlist);
318     foreach (split ("\n", $file_contents{$file}))
319     {
320         if (/$ac_require_rx/g)
321         {
322             push (@rlist, $1);
323         }
325         # This function constructed dynamically.
326         &search;
327     }
329     local ($macro);
330     foreach $macro (@rlist)
331     {
332         &add_macro ($macro);
333     }
336 # Scan a single M4 file.  Return contents.
337 sub scan_file
339     local ($file) = @_;
341     open (FILE, $file)
342         || die "aclocal: couldn't open \`$file': $!\n";
343     local ($contents) = '';
344     while (<FILE>)
345     {
346         # Ignore `##' lines.
347         next if /^##/;
349         $contents .= $_;
351         if (/$ac_defun_rx/)
352         {
353             if (!defined $map{$1})
354             {
355                 $map{$1} = $file;
356             }
357             # Allow acinclude.m4 to override other macro files.
358             elsif ($map{$1} ne 'acinclude.m4' || $file eq 'acinclude.m4')
359             {
360                 warn "aclocal: $file: $.: duplicated macro \`$1'\n";
361                 $exit_status = 1;
362             }
363             print STDERR "Found macro $1 in $file: $.\n" if $verbosity;
364         }
365     }
366     close (FILE);
368     return $contents;
371 ################################################################
373 # Write output.
374 sub write_aclocal
376     return if ! length ($output);
378     print STDERR "Writing aclocal.m4\n" if $verbosity;
380     open (ACLOCAL, "> aclocal.m4")
381         || die "aclocal: couldn't open \`aclocal.m4' for writing: $!\n";
382     print ACLOCAL "dnl aclocal.m4 generated automatically by aclocal $VERSION\n\n";
383     print ACLOCAL $output;
384     close (ACLOCAL);