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