Changed bug-reporting address.
[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   --print-ac-dir        print name of directory holding m4 files
126   --verbose             don't be silent
127   --version             print version number, then exit
128
129 Report bugs to <bug-automake\@gnu.org>.\n";
130
131     exit $status;
132 }
133
134 # Parse command line.
135 sub parse_arguments
136 {
137     local (@arglist) = @_;
138     local (@dirlist);
139     local ($print_and_exit) = 0;
140
141     while (@arglist)
142     {
143         if ($arglist[0] =~ /^--acdir=(.+)$/)
144         {
145             $acdir = $1;
146         }
147         elsif ($arglist[0] =~/^--output=(.+)$/)
148         {
149             $output_file = $1;
150         }
151         elsif ($arglist[0] eq '-I')
152         {
153             shift (@arglist);
154             push (@dirlist, $arglist[0]);
155         }
156         elsif ($arglist[0] eq '--print-ac-dir')
157         {
158             $print_and_exit = 1;
159         }
160         elsif ($arglist[0] eq '--verbose')
161         {
162             ++$verbosity;
163         }
164         elsif ($arglist[0] eq '--version')
165         {
166             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
167             print "Copyright (C) 1998 Free Software Foundation, Inc.\n";
168             print "This is free software; see the source for copying conditions.  There is NO\n";
169             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
170             print "Written by Tom Tromey <tromey\@cygnus.com>\n";
171             exit 0;
172         }
173         elsif ($arglist[0] eq '--help')
174         {
175             &usage (0);
176         }
177         else
178         {
179             die "aclocal: unrecognized option -- \`$arglist[0]'\nTry \`aclocal --help' for more information.\n";
180         }
181
182         shift (@arglist);
183     }
184
185     if ($print_and_exit)
186     {
187         print $acdir, "\n";
188         exit 0;
189     }
190
191     return @dirlist;
192 }
193
194 ################################################################
195
196 sub scan_configure
197 {
198     open (CONFIGURE, "configure.in")
199         || die "aclocal: couldn't open \`configure.in': $!\n";
200
201     # Make sure we include acinclude.m4 if it exists.
202     if (-f 'acinclude.m4')
203     {
204         &add_file ('acinclude.m4');
205     }
206
207     while (<CONFIGURE>)
208     {
209         # Remove comments from current line.
210         s/\bdnl\b.*$//;
211         s/\#.*$//;
212
213         if (/$obsolete_rx/o)
214         {
215             chop;
216             warn "aclocal: configure.in: $.: obsolete macro \`$_'\n";
217             $exit_status = 1;
218             next;
219         }
220
221         # Search for things we know about.  The "search" sub is
222         # constructed dynamically by scan_m4_files.
223         if (! &search && /(^|\s+)(AM_[A-Z_]+)/)
224         {
225             # Macro not found, but AM_ prefix found.
226             warn "aclocal: configure.in: $.: macro \`$2' not found in library\n";
227             $exit_status = 1;
228         }
229     }
230
231     close (CONFIGURE);
232 }
233
234 ################################################################
235
236 # Check macros in acinclude.m4.  If one is not used, warn.
237 sub check_acinclude
238 {
239     local ($key);
240
241     foreach $key (keys %map)
242     {
243         next unless $map{$key} eq 'acinclude.m4';
244         if (! $macro_seen{$key})
245         {
246             # FIXME: should print line number of acinclude.m4.
247             warn "aclocal: macro \`$key' defined in acinclude.m4 but never used\n";
248         }
249     }
250 }
251
252 ################################################################
253
254 # Scan all the installed m4 files and construct a map.
255 sub scan_m4_files
256 {
257     local (@dirlist) = @_;
258
259     # First, scan acinclude.m4 if it exists.
260     if (-f 'acinclude.m4')
261     {
262         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
263     }
264
265     local ($m4dir);
266     foreach $m4dir (@dirlist)
267     {
268         opendir (DIR, $m4dir)
269             || die "aclocal: couldn't open directory \`$m4dir': $!\n";
270         local ($file, $fullfile, $expr);
271         foreach $file (sort grep (! /^\./, readdir (DIR)))
272         {
273             # Only examine .m4 files.
274             next unless $file =~ /\.m4$/;
275
276             # Skip some files when running out of srcdir.
277             next if $file eq 'aclocal.m4';
278
279             $fullfile = $m4dir . '/' . $file;
280             $file_contents{$fullfile} = &scan_file ($fullfile);
281         }
282         closedir (DIR);
283     }
284
285     # Construct a new function that does the searching.  We use a
286     # function (instead of just evalling $search in the loop) so that
287     # "die" is correctly and easily propagated if run.
288     local ($search, $expr, $key) = '';
289     foreach $key (reverse sort keys %map)
290     {
291         # EXPR is a regexp matching the name of the macro.
292         ($expr = $key) =~ s/(\W)/\\$1/g;
293         $search .= ("if (/" . $expr . "/) { & add_macro (" . $key
294                     . "); return 1; }\n");
295     }
296     $search .= "return 0;\n";
297     eval 'sub search { ' . $search . '};';
298     die "internal error: $@\n search is $search " if $@;
299 }
300
301 ################################################################
302
303 # Add a macro to the output.
304 sub add_macro
305 {
306     local ($macro) = @_;
307
308     # We want to ignore AC_ macros.  However, if an AC_ macro is
309     # defined in (eg) acinclude.m4, then we want to make sure we mark
310     # it as seen.
311     return if $macro =~ /^AC_/ && ! defined $map{$macro};
312
313     if (! defined $map{$macro})
314     {
315         warn "aclocal: macro \`$macro' required but not defined\n";
316         $exit_status = 1;
317         return;
318     }
319
320     print STDERR "saw macro $macro\n" if $verbosity;
321     $macro_seen{$macro} = 1;
322     &add_file ($map{$macro});
323 }
324
325 # Add a file to output.
326 sub add_file
327 {
328     local ($file) = @_;
329
330     # Only add a file once.
331     return if ($file_seen{$file});
332     $file_seen{$file} = 1;
333
334     $output .= $file_contents{$file} . "\n";
335     local ($a, @rlist);
336     foreach (split ("\n", $file_contents{$file}))
337     {
338         # This is a hack for Perl 4.
339         $a = $_;
340         if ($a =~ /$ac_require_rx/g)
341         {
342             push (@rlist, $1);
343         }
344
345         # This function constructed dynamically.
346         if (! &search && /(^|\s+)(AM_[A-Z_]+)/)
347         {
348             # Macro not found, but AM_ prefix found.
349             warn "aclocal: configure.in: $.: macro \`$2' not found in library\n";
350             $exit_status = 1;
351         }
352     }
353
354     local ($macro);
355     foreach $macro (@rlist)
356     {
357         &add_macro ($macro);
358     }
359 }
360
361 # Scan a single M4 file.  Return contents.
362 sub scan_file
363 {
364     local ($file) = @_;
365
366     open (FILE, $file)
367         || die "aclocal: couldn't open \`$file': $!\n";
368     local ($contents) = '';
369     while (<FILE>)
370     {
371         # Ignore `##' lines.
372         next if /^##/;
373
374         $contents .= $_;
375
376         if (/$ac_defun_rx/)
377         {
378             if (!defined $map{$1})
379             {
380                 $map{$1} = $file;
381             }
382             # Allow acinclude.m4 to override other macro files.
383             elsif ($map{$1} ne 'acinclude.m4' || $file eq 'acinclude.m4')
384             {
385                 warn "aclocal: $file: $.: duplicated macro \`$1'\n";
386                 $exit_status = 1;
387             }
388             print STDERR "Found macro $1 in $file: $.\n" if $verbosity;
389         }
390     }
391     close (FILE);
392
393     return $contents;
394 }
395
396 ################################################################
397
398 # Write output.
399 sub write_aclocal
400 {
401     return if ! length ($output);
402
403     print STDERR "Writing $output_file\n" if $verbosity;
404
405     open (ACLOCAL, "> " . $output_file)
406         || die "aclocal: couldn't open \`$output_file' for writing: $!\n";
407     print ACLOCAL "dnl $output_file generated automatically by aclocal $VERSION\n";
408     print ACLOCAL "\
409 dnl Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
410 dnl This file is free software; the Free Software Foundation
411 dnl gives unlimited permission to copy and/or distribute it,
412 dnl with or without modifications, as long as this notice is preserved.
413
414 dnl This program is distributed in the hope that it will be useful,
415 dnl but WITHOUT ANY WARRANTY, to the extent permitted by law; without
416 dnl even the implied warranty of MERCHANTABILITY or FITNESS FOR A
417 dnl PARTICULAR PURPOSE.
418
419 ";
420     print ACLOCAL $output;
421     close (ACLOCAL);
422 }