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