* aclocal.in (parse_arguments): Added 2002.
[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 1996, 1997, 1998, 1999, 2000,\n";
195             print "          2001, 2002 Free Software Foundation, Inc.\n";
196             print "This is free software; see the source for copying conditions.  There is NO\n";
197             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
198             print "Written by Tom Tromey <tromey\@redhat.com>\n";
199             exit 0;
200         }
201         elsif ($arglist[0] eq '--help')
202         {
203             &usage (0);
204         }
205         else
206         {
207             die "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
208         }
209
210         shift (@arglist);
211     }
212
213     if ($print_and_exit)
214     {
215         print $acdir, "\n";
216         exit 0;
217     }
218
219     # Search the versioned directory near the end, and then the
220     # unversioned directory last.  Only do this if the user didn't
221     # override acdir.
222     push (@dirlist, "$acdir-$APIVERSION")
223         if $acdir eq $default_acdir;
224
225     # By default $(datadir)/aclocal doesn't exist.  We don't want to
226     # get an error in the case where we are searching the default
227     # directory and it hasn't been created.
228     push (@dirlist, $acdir)
229         unless $acdir eq $default_acdir && ! -d $acdir;
230
231     return @dirlist;
232 }
233
234 ################################################################
235
236 sub scan_configure
237 {
238     die "aclocal: `configure.ac' or `configure.in' is required\n"
239         if !$configure_ac;
240
241     open (CONFIGURE, $configure_ac)
242         || die "aclocal: couldn't open `$configure_ac': $!\n";
243
244     # Make sure we include acinclude.m4 if it exists.
245     if (-f 'acinclude.m4')
246     {
247         &add_file ('acinclude.m4');
248     }
249
250     while (<CONFIGURE>)
251     {
252         # Remove comments from current line.
253         s/\bdnl\b.*$//;
254         s/\#.*$//;
255
256         if (/$obsolete_rx/o)
257         {
258             local ($hint) = '';
259             if ($obsolete_macros{$1} ne '')
260             {
261                 $hint = '; ' . $obsolete_macros{$1};
262             }
263             warn "aclocal: $configure_ac: $.: `$1' is obsolete$hint\n";
264             $exit_status = 1;
265             next;
266         }
267
268         # Search for things we know about.  The "search" sub is
269         # constructed dynamically by scan_m4_files.  The last
270         # parenthethical match makes sure we don't match things that
271         # look like macro assignments or AC_SUBSTs.
272         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
273         {
274             # Macro not found, but AM_ prefix found.
275             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
276             $exit_status = 1;
277         }
278     }
279
280     close (CONFIGURE);
281 }
282
283 ################################################################
284
285 # Check macros in acinclude.m4.  If one is not used, warn.
286 sub check_acinclude
287 {
288     local ($key);
289
290     foreach $key (keys %map)
291     {
292         next unless $map{$key} eq 'acinclude.m4';
293         if (! $macro_seen{$key})
294         {
295             # FIXME: should print line number of acinclude.m4.
296             warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
297         }
298     }
299 }
300
301 ################################################################
302
303 # Scan all the installed m4 files and construct a map.
304 sub scan_m4_files
305 {
306     local (@dirlist) = @_;
307
308     # First, scan acinclude.m4 if it exists.
309     if (-f 'acinclude.m4')
310     {
311         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
312     }
313
314     local ($m4dir);
315     foreach $m4dir (@dirlist)
316     {
317         opendir (DIR, $m4dir)
318             || die "aclocal: couldn't open directory `$m4dir': $!\n";
319         local ($file, $fullfile);
320         foreach $file (sort grep (! /^\./, readdir (DIR)))
321         {
322             # Only examine .m4 files.
323             next unless $file =~ /\.m4$/;
324
325             # Skip some files when running out of srcdir.
326             next if $file eq 'aclocal.m4';
327
328             $fullfile = $m4dir . '/' . $file;
329             $file_contents{$fullfile} = &scan_file ($fullfile);
330         }
331         closedir (DIR);
332     }
333
334     # Construct a new function that does the searching.  We use a
335     # function (instead of just evalling $search in the loop) so that
336     # "die" is correctly and easily propagated if run.
337     my $search = "sub search {\nmy \$found = 0;\n";
338     foreach my $key (reverse sort keys %map)
339     {
340         # EXPR is a regexp matching the name of the macro.
341         (my $expr = $key) =~ s/(\W)/\\$1/g;
342         $search .= ('if (/\b' . $key . '\b/) { & add_macro (' . $key
343                     . '); $found = 1; }' . "\n");
344     }
345     $search .= "return \$found;\n};\n";
346     eval $search;
347     die "internal error: $@\n search is $search" if $@;
348 }
349
350 ################################################################
351
352 # Add a macro to the output.
353 sub add_macro
354 {
355     local ($macro) = @_;
356
357     # We want to ignore AC_ macros.  However, if an AC_ macro is
358     # defined in (eg) acinclude.m4, then we want to make sure we mark
359     # it as seen.
360     return if $macro =~ /^AC_/ && ! defined $map{$macro};
361
362     if (! defined $map{$macro})
363     {
364         warn "aclocal: macro `$macro' required but not defined\n";
365         $exit_status = 1;
366         return;
367     }
368
369     print STDERR "aclocal: saw macro $macro\n" if $verbose;
370     $macro_seen{$macro} = 1;
371     &add_file ($map{$macro});
372 }
373
374 # Add a file to output.
375 sub add_file
376 {
377     local ($file) = @_;
378
379     # Only add a file once.
380     return if ($file_seen{$file});
381     $file_seen{$file} = 1;
382
383     $output .= $file_contents{$file} . "\n";
384     local ($a, @rlist);
385     foreach (split ("\n", $file_contents{$file}))
386     {
387         # This is a hack for Perl 4.
388         $a = $_;
389         if ($a =~ /$ac_require_rx/g)
390         {
391             push (@rlist, $1);
392         }
393
394         # Remove comments from current line.
395         s/\bdnl\b.*$//;
396         s/\#.*$//;
397
398         # The search function is constructed dynamically by
399         # scan_m4_files.  The last parenthethical match makes sure we
400         # don't match things that look like macro assignments or
401         # AC_SUBSTs.
402         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
403         {
404             # Macro not found, but AM_ prefix found.
405             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
406             $exit_status = 1;
407         }
408     }
409
410     local ($macro);
411     foreach $macro (@rlist)
412     {
413         &add_macro ($macro);
414     }
415 }
416
417 # Scan a single M4 file.  Return contents.
418 sub scan_file
419 {
420     local ($file) = @_;
421
422     my $fh = new Automake::XFile $file;
423     my $contents = '';
424     while ($_ = $fh->getline)
425     {
426         # Ignore `##' lines.
427         next if /^##/;
428
429         $contents .= $_;
430
431         if (/$ac_defun_rx/)
432         {
433             if (! defined $map{$1})
434             {
435                 $map{$1} = $file;
436             }
437
438             # Note: we used to give an error here if we saw a
439             # duplicated macro.  However, this turns out to be
440             # extremely unpopular.  It causes actual problems which
441             # are hard to work around, especially when you must
442             # mix-and-match tool versions.
443
444             print STDERR "aclocal: found macro $1 in $file: $.\n" if $verbose;
445         }
446     }
447
448     return $contents;
449 }
450
451 ################################################################
452
453 # Write output.
454 sub write_aclocal
455 {
456     return if ! length ($output);
457
458     print STDERR "aclocal: writing $output_file\n" if $verbose;
459
460     my $out = new Automake::XFile "> $output_file";
461     print $out
462 "# $output_file generated automatically by aclocal $VERSION -*- Autoconf -*-
463
464 # Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
465 # Free Software Foundation, Inc.
466 # This file is free software; the Free Software Foundation
467 # gives unlimited permission to copy and/or distribute it,
468 # with or without modifications, as long as this notice is preserved.
469
470 # This program is distributed in the hope that it will be useful,
471 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
472 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
473 # PARTICULAR PURPOSE.
474
475 $output";
476 }