minor date changes; last fixes to aclocal --output
[platform/upstream/automake.git] / aclocal.in
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
4
5 # aclocal - create aclocal.m4 by scanning configure.in
6 # Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
7
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.
12
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.
17
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.
22
23 # Written by Tom Tromey <tromey@cygnus.com>.
24
25 eval 'exec @PERL@ -S $0 ${1+"$@"}'
26     if 0;
27
28 # aclocal - scan configure.in and generate aclocal.m4.
29
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";
36
37 # Some globals.
38
39 # Exit status.
40 $exit_status = 0;
41
42 # Text to output.
43 $output = '';
44
45 # Output file name.
46 $output_file = 'aclocal.m4';
47
48 # Which macros have been seen.
49 %macro_seen = ();
50
51 # Which files have been seen.
52 %file_seen = ();
53
54 # Map macro names to file names.
55 %map = ();
56
57 # Map file names to file contents.
58 %file_contents = ();
59
60 # How much to say.
61 $verbosity = 0;
62
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',
80
81      # Now part of autoconf proper, under a different name.
82      'AM_FUNC_FNMATCH',
83      'AM_SANITY_CHECK_CC',
84
85 # These aren't quite obsolete.
86 #      'md_PATH_PROG',
87 #      'ud_LC_MESSAGES',
88 #      'ud_WITH_NLS'
89      );
90
91 $obsolete_rx = '(' . join ('|', @obsolete_macros) . ')';
92
93 # Matches a macro definition.
94 $ac_defun_rx = "AC_DEFUN\\(\\[?([^],)\n]+)\\]?";
95
96 # Matches an AC_REQUIRE line.
97 $ac_require_rx = "AC_REQUIRE\\(\\[?([^])]*)\\]?\\)";
98
99 \f
100
101 local (@dirlist) = &parse_arguments (@ARGV);
102 &scan_m4_files ($acdir, @dirlist);
103 &scan_configure;
104 if (! $exit_status)
105 {
106     &write_aclocal;
107 }
108 &check_acinclude;
109
110 exit $exit_status;
111
112 ################################################################
113
114 # Print usage and exit.
115 sub usage
116 {
117     local ($status) = @_;
118
119     print "Usage: aclocal [OPTIONS] ...\n\n";
120     print "Generate aclocal.m4 by scanning configure.in\n
121   --acdir=DIR           directory holding config files
122   --help                print this help, then exit
123   -I DIR                add directory to search list for .m4 files
124   --output=FILE         put output in FILE (default aclocal.m4)
125   --verbose             don't be silent
126   --version             print version number, then exit
127
128 Report bugs to <automake-bugs\@gnu.org>.\n";
129
130     exit $status;
131 }
132
133 # Parse command line.
134 sub parse_arguments
135 {
136     local (@arglist) = @_;
137     local (@dirlist);
138
139     while (@arglist)
140     {
141         if ($arglist[0] =~ /^--acdir=(.+)$/)
142         {
143             $acdir = $1;
144         }
145         elsif ($arglist[0] =~/^--output=(.+)$/)
146         {
147             $output_file = $1;
148         }
149         elsif ($arglist[0] eq '-I')
150         {
151             shift (@arglist);
152             push (@dirlist, $arglist[0]);
153         }
154         elsif ($arglist[0] eq '--verbose')
155         {
156             ++$verbosity;
157         }
158         elsif ($arglist[0] eq '--version')
159         {
160             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
161             print "Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.\n";
162             print "This is free software; see the source for copying conditions.  There is NO\n";
163             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
164             print "Written by Tom Tromey <tromey\@cygnus.com>\n";
165             exit 0;
166         }
167         elsif ($arglist[0] eq '--help')
168         {
169             &usage (0);
170         }
171         else
172         {
173             die "aclocal: unrecognized option -- \`$arglist[0]'\nTry \`aclocal --help' for more information.\n";
174         }
175
176         shift (@arglist);
177     }
178
179     return @dirlist;
180 }
181
182 ################################################################
183
184 sub scan_configure
185 {
186     open (CONFIGURE, "configure.in")
187         || die "aclocal: couldn't open \`configure.in': $!\n";
188
189     # Make sure we include acinclude.m4 if it exists.
190     if (-f 'acinclude.m4')
191     {
192         &add_file ('acinclude.m4');
193     }
194
195     while (<CONFIGURE>)
196     {
197         # Remove comments from current line.
198         s/\bdnl\b.*$//;
199         s/\#.*$//;
200
201         if (/$obsolete_rx/o)
202         {
203             chop;
204             warn "aclocal: configure.in: $.: obsolete macro \`$_'\n";
205             $exit_status = 1;
206             next;
207         }
208
209         # Search for things we know about.  The "search" sub is
210         # constructed dynamically by scan_m4_files.
211         if (! &search && /(AM_[A-Z_]+)/)
212         {
213             # Macro not found, but AM_ prefix found.
214             warn "aclocal: configure.in: $.: macro \`$1' not found in library\n";
215             $exit_status = 1;
216         }
217     }
218
219     close (CONFIGURE);
220 }
221
222 ################################################################
223
224 # Check macros in acinclude.m4.  If one is not used, warn.
225 sub check_acinclude
226 {
227     local ($key);
228
229     foreach $key (keys %map)
230     {
231         next unless $map{$key} eq 'acinclude.m4';
232         if (! $macro_seen{$key})
233         {
234             # FIXME: should print line number of acinclude.m4.
235             warn "aclocal: macro \`$key' defined in acinclude.m4 but never used\n";
236         }
237     }
238 }
239
240 ################################################################
241
242 # Scan all the installed m4 files and construct a map.
243 sub scan_m4_files
244 {
245     local (@dirlist) = @_;
246
247     # First, scan acinclude.m4 if it exists.
248     if (-f 'acinclude.m4')
249     {
250         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
251     }
252
253     local ($m4dir);
254     foreach $m4dir (@dirlist)
255     {
256         opendir (DIR, $m4dir)
257             || die "aclocal: couldn't open directory \`$m4dir': $!\n";
258         local ($file, $fullfile, $expr);
259         foreach $file (sort grep (! /^\./, readdir (DIR)))
260         {
261             # Only examine .m4 files.
262             next unless $file =~ /\.m4$/;
263
264             # Skip some files when running out of srcdir.
265             next if $file eq 'aclocal.m4';
266
267             $fullfile = $m4dir . '/' . $file;
268             $file_contents{$fullfile} = &scan_file ($fullfile);
269         }
270         closedir (DIR);
271     }
272
273     # Construct a new function that does the searching.  We use a
274     # function (instead of just evalling $search in the loop) so that
275     # "die" is correctly and easily propagated if run.
276     local ($search, $expr, $key) = '';
277     foreach $key (keys %map)
278     {
279         # EXPR is a regexp matching the name of the macro.
280         ($expr = $key) =~ s/(\W)/\\$1/g;
281         $search .= ("if (/" . $expr . "/) { & add_macro (" . $key
282                     . "); return 1; }\n");
283     }
284     $search .= "return 0;\n";
285     eval 'sub search { ' . $search . '};';
286     die "internal error: $@\n search is $search " if $@;
287 }
288
289 ################################################################
290
291 # Add a macro to the output.
292 sub add_macro
293 {
294     local ($macro) = @_;
295
296     # We want to ignore AC_ macros.  However, if an AC_ macro is
297     # defined in (eg) acinclude.m4, then we want to make sure we mark
298     # it as seen.
299     return if $macro =~ /^AC_/ && ! defined $map{$macro};
300
301     if (! defined $map{$macro})
302     {
303         warn "aclocal: macro \`$macro' required but not defined\n";
304         $exit_status = 1;
305         return;
306     }
307
308     print STDERR "saw macro $macro\n" if $verbosity;
309     $macro_seen{$macro} = 1;
310     &add_file ($map{$macro});
311 }
312
313 # Add a file to output.
314 sub add_file
315 {
316     local ($file) = @_;
317
318     # Only add a file once.
319     return if ($file_seen{$file});
320     $file_seen{$file} = 1;
321
322     $output .= $file_contents{$file} . "\n";
323     local ($a, @rlist);
324     foreach (split ("\n", $file_contents{$file}))
325     {
326         # This is a hack for Perl 4.
327         $a = $_;
328         if ($a =~ /$ac_require_rx/g)
329         {
330             push (@rlist, $1);
331         }
332
333         # This function constructed dynamically.
334         &search;
335     }
336
337     local ($macro);
338     foreach $macro (@rlist)
339     {
340         &add_macro ($macro);
341     }
342 }
343
344 # Scan a single M4 file.  Return contents.
345 sub scan_file
346 {
347     local ($file) = @_;
348
349     open (FILE, $file)
350         || die "aclocal: couldn't open \`$file': $!\n";
351     local ($contents) = '';
352     while (<FILE>)
353     {
354         # Ignore `##' lines.
355         next if /^##/;
356
357         $contents .= $_;
358
359         if (/$ac_defun_rx/)
360         {
361             if (!defined $map{$1})
362             {
363                 $map{$1} = $file;
364             }
365             # Allow acinclude.m4 to override other macro files.
366             elsif ($map{$1} ne 'acinclude.m4' || $file eq 'acinclude.m4')
367             {
368                 warn "aclocal: $file: $.: duplicated macro \`$1'\n";
369                 $exit_status = 1;
370             }
371             print STDERR "Found macro $1 in $file: $.\n" if $verbosity;
372         }
373     }
374     close (FILE);
375
376     return $contents;
377 }
378
379 ################################################################
380
381 # Write output.
382 sub write_aclocal
383 {
384     return if ! length ($output);
385
386     print STDERR "Writing $output_file\n" if $verbosity;
387
388     open (ACLOCAL, "> " . $output_file)
389         || die "aclocal: couldn't open \`$output_file' for writing: $!\n";
390     print ACLOCAL "dnl $output_file generated automatically by aclocal $VERSION\n";
391     print ACLOCAL "\
392 dnl Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
393 dnl This Makefile.in is free software; the Free Software Foundation
394 dnl gives unlimited permission to copy and/or distribute it,
395 dnl with or without modifications, as long as this notice is preserved.
396
397 dnl This program is distributed in the hope that it will be useful,
398 dnl but WITHOUT ANY WARRANTY, to the extent permitted by law; without
399 dnl even the implied warranty of MERCHANTABILITY or FITNESS FOR A
400 dnl PARTICULAR PURPOSE.
401
402 ";
403     print ACLOCAL $output;
404     close (ACLOCAL);
405 }