* automake.in (handle_tests): Test `dir=./' before `dir=', so that
[automake.git] / aclocal.in
blob715ccb2c5d20ad106c918491645fbfda61bddb67
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 # aclocal - create aclocal.m4 by scanning configure.in
6 # Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
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.
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.
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.
23 # Written by Tom Tromey <tromey@cygnus.com>.
25 eval 'exec @PERL@ -S $0 ${1+"$@"}'
26     if 0;
28 # aclocal - scan configure.in and generate aclocal.m4.
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";
37 # Some globals.
39 # Exit status.
40 $exit_status = 0;
42 # Text to output.
43 $output = '';
45 # Output file name.
46 $output_file = 'aclocal.m4';
48 # Which macros have been seen.
49 %macro_seen = ();
51 # Which files have been seen.
52 %file_seen = ();
54 # Map macro names to file names.
55 %map = ();
57 # Map file names to file contents.
58 %file_contents = ();
60 # How much to say.
61 $verbosity = 0;
63 # Map from obsolete macros to hints for new macros.
64 # If you change this, change the corresponding list in automake.in.
65 # FIXME: should just put this into a single file.
66 %obsolete_macros =
67     (
68      'AC_FEATURE_CTYPE', "use \`AC_HEADER_STDC'",
69      'AC_FEATURE_ERRNO', "add \`strerror' to \`AC_REPLACE_FUNCS(...)'",
70      'AC_FEATURE_EXIT', '',
71      'AC_SYSTEM_HEADER', '',
73      # Note that we do not handle this one, because it is still run
74      # from AM_CONFIG_HEADER.  So we deal with it specially in
75      # scan_configure.
76      # 'AC_CONFIG_HEADER', "use \`AM_CONFIG_HEADER'",
78      'fp_C_PROTOTYPES', "use \`AM_C_PROTOTYPES'",
79      'fp_PROG_CC_STDC', "use \`AM_PROG_CC_STDC'",
80      'fp_PROG_INSTALL', "use \`AC_PROG_INSTALL'",
81      'fp_WITH_DMALLOC', "use \`AM_WITH_DMALLOC'",
82      'fp_WITH_REGEX', "use \`AM_WITH_REGEX'",
83      'gm_PROG_LIBTOOL', "use \`AM_PROG_LIBTOOL'",
84      'jm_MAINTAINER_MODE', "use \`AM_MAINTAINER_MODE'",
85      'md_TYPE_PTRDIFF_T', "use \`AM_TYPE_PTRDIFF_T'",
86      'ud_PATH_LISPDIR', "use \`AM_PATH_LISPDIR'",
88      # Now part of autoconf proper, under a different name.
89      'AM_FUNC_FNMATCH', "use \`AC_FUNC_FNMATCH'",
90      'fp_FUNC_FNMATCH', "use \`AC_FUNC_FNMATCH'",
91      'AM_SANITY_CHECK_CC', "automatically done by \`AC_PROG_CC'",
92      'AM_PROG_INSTALL', "use \`AC_PROG_INSTALL'",
93      'AM_EXEEXT', "use \`AC_EXEEXT'",
94      'AM_CYGWIN32', "use \`AC_CYGWIN32'",
95      'AM_MINGW32', "use \`AC_MINGW32'",
97 # These aren't quite obsolete.
98 #      'md_PATH_PROG',
99      );
101 $obsolete_rx = '(' . join ('|', keys %obsolete_macros) . ')';
103 # Matches a macro definition.
104 $ac_defun_rx = "AC_DEFUN\\(\\[?([^],)\n]+)\\]?";
106 # Matches an AC_REQUIRE line.
107 $ac_require_rx = "AC_REQUIRE\\(\\[?([^])]*)\\]?\\)";
111 local (@dirlist) = &parse_arguments (@ARGV);
112 &scan_m4_files ($acdir, @dirlist);
113 &scan_configure;
114 if (! $exit_status)
116     &write_aclocal;
118 &check_acinclude;
120 exit $exit_status;
122 ################################################################
124 # Print usage and exit.
125 sub usage
127     local ($status) = @_;
129     print "Usage: aclocal [OPTIONS] ...\n\n";
130     print "Generate aclocal.m4 by scanning configure.in\n
131   --acdir=DIR           directory holding config files
132   --help                print this help, then exit
133   -I DIR                add directory to search list for .m4 files
134   --output=FILE         put output in FILE (default aclocal.m4)
135   --print-ac-dir        print name of directory holding m4 files
136   --verbose             don't be silent
137   --version             print version number, then exit
139 Report bugs to <bug-automake\@gnu.org>.\n";
141     exit $status;
144 # Parse command line.
145 sub parse_arguments
147     local (@arglist) = @_;
148     local (@dirlist);
149     local ($print_and_exit) = 0;
151     while (@arglist)
152     {
153         if ($arglist[0] =~ /^--acdir=(.+)$/)
154         {
155             $acdir = $1;
156         }
157         elsif ($arglist[0] =~/^--output=(.+)$/)
158         {
159             $output_file = $1;
160         }
161         elsif ($arglist[0] eq '-I')
162         {
163             shift (@arglist);
164             push (@dirlist, $arglist[0]);
165         }
166         elsif ($arglist[0] eq '--print-ac-dir')
167         {
168             $print_and_exit = 1;
169         }
170         elsif ($arglist[0] eq '--verbose')
171         {
172             ++$verbosity;
173         }
174         elsif ($arglist[0] eq '--version')
175         {
176             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
177             print "Copyright (C) 1999 Free Software Foundation, Inc.\n";
178             print "This is free software; see the source for copying conditions.  There is NO\n";
179             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
180             print "Written by Tom Tromey <tromey\@cygnus.com>\n";
181             exit 0;
182         }
183         elsif ($arglist[0] eq '--help')
184         {
185             &usage (0);
186         }
187         else
188         {
189             die "aclocal: unrecognized option -- \`$arglist[0]'\nTry \`aclocal --help' for more information.\n";
190         }
192         shift (@arglist);
193     }
195     if ($print_and_exit)
196     {
197         print $acdir, "\n";
198         exit 0;
199     }
201     return @dirlist;
204 ################################################################
206 sub scan_configure
208     open (CONFIGURE, "configure.in")
209         || die "aclocal: couldn't open \`configure.in': $!\n";
211     # Make sure we include acinclude.m4 if it exists.
212     if (-f 'acinclude.m4')
213     {
214         &add_file ('acinclude.m4');
215     }
217     while (<CONFIGURE>)
218     {
219         # Remove comments from current line.
220         s/\bdnl\b.*$//;
221         s/\#.*$//;
223         if (/$obsolete_rx/o)
224         {
225             local ($hint) = '';
226             if ($obsolete_macros{$1} ne '')
227             {
228                 $hint = '; ' . $obsolete_macros{$1};
229             }
230             warn "aclocal: configure.in: $.: \`$1' is obsolete$hint\n";
231             $exit_status = 1;
232             next;
233         }
235         # Search for things we know about.  The "search" sub is
236         # constructed dynamically by scan_m4_files.
237         if (! &search && /(^|\s+)(AM_[A-Z_]+)/)
238         {
239             # Macro not found, but AM_ prefix found.
240             warn "aclocal: configure.in: $.: macro \`$2' not found in library\n";
241             $exit_status = 1;
242         }
243     }
245     close (CONFIGURE);
248 ################################################################
250 # Check macros in acinclude.m4.  If one is not used, warn.
251 sub check_acinclude
253     local ($key);
255     foreach $key (keys %map)
256     {
257         next unless $map{$key} eq 'acinclude.m4';
258         if (! $macro_seen{$key})
259         {
260             # FIXME: should print line number of acinclude.m4.
261             warn "aclocal: macro \`$key' defined in acinclude.m4 but never used\n";
262         }
263     }
266 ################################################################
268 # Scan all the installed m4 files and construct a map.
269 sub scan_m4_files
271     local (@dirlist) = @_;
273     # First, scan acinclude.m4 if it exists.
274     if (-f 'acinclude.m4')
275     {
276         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
277     }
279     local ($m4dir);
280     foreach $m4dir (@dirlist)
281     {
282         opendir (DIR, $m4dir)
283             || die "aclocal: couldn't open directory \`$m4dir': $!\n";
284         local ($file, $fullfile, $expr);
285         foreach $file (sort grep (! /^\./, readdir (DIR)))
286         {
287             # Only examine .m4 files.
288             next unless $file =~ /\.m4$/;
290             # Skip some files when running out of srcdir.
291             next if $file eq 'aclocal.m4';
293             $fullfile = $m4dir . '/' . $file;
294             $file_contents{$fullfile} = &scan_file ($fullfile);
295         }
296         closedir (DIR);
297     }
299     # Construct a new function that does the searching.  We use a
300     # function (instead of just evalling $search in the loop) so that
301     # "die" is correctly and easily propagated if run.
302     local ($search, $expr, $key) = '';
303     foreach $key (reverse sort keys %map)
304     {
305         # EXPR is a regexp matching the name of the macro.
306         ($expr = $key) =~ s/(\W)/\\$1/g;
307         $search .= ("if (/" . $expr . "/) { & add_macro (" . $key
308                     . "); return 1; }\n");
309     }
310     $search .= "return 0;\n";
311     eval 'sub search { ' . $search . '};';
312     die "internal error: $@\n search is $search " if $@;
315 ################################################################
317 # Add a macro to the output.
318 sub add_macro
320     local ($macro) = @_;
322     # We want to ignore AC_ macros.  However, if an AC_ macro is
323     # defined in (eg) acinclude.m4, then we want to make sure we mark
324     # it as seen.
325     return if $macro =~ /^AC_/ && ! defined $map{$macro};
327     if (! defined $map{$macro})
328     {
329         warn "aclocal: macro \`$macro' required but not defined\n";
330         $exit_status = 1;
331         return;
332     }
334     print STDERR "saw macro $macro\n" if $verbosity;
335     $macro_seen{$macro} = 1;
336     &add_file ($map{$macro});
339 # Add a file to output.
340 sub add_file
342     local ($file) = @_;
344     # Only add a file once.
345     return if ($file_seen{$file});
346     $file_seen{$file} = 1;
348     $output .= $file_contents{$file} . "\n";
349     local ($a, @rlist);
350     foreach (split ("\n", $file_contents{$file}))
351     {
352         # This is a hack for Perl 4.
353         $a = $_;
354         if ($a =~ /$ac_require_rx/g)
355         {
356             push (@rlist, $1);
357         }
359         # This function constructed dynamically.
360         if (! &search && /(^|\s+)(AM_[A-Z_]+)/)
361         {
362             # Macro not found, but AM_ prefix found.
363             warn "aclocal: configure.in: $.: macro \`$2' not found in library\n";
364             $exit_status = 1;
365         }
366     }
368     local ($macro);
369     foreach $macro (@rlist)
370     {
371         &add_macro ($macro);
372     }
375 # Scan a single M4 file.  Return contents.
376 sub scan_file
378     local ($file) = @_;
380     open (FILE, $file)
381         || die "aclocal: couldn't open \`$file': $!\n";
382     local ($contents) = '';
383     while (<FILE>)
384     {
385         # Ignore `##' lines.
386         next if /^##/;
388         $contents .= $_;
390         if (/$ac_defun_rx/)
391         {
392             if (!defined $map{$1})
393             {
394                 $map{$1} = $file;
395             }
396             # Allow acinclude.m4 to override other macro files.
397             elsif ($map{$1} ne 'acinclude.m4' || $file eq 'acinclude.m4')
398             {
399                 warn "aclocal: $file: $.: duplicated macro \`$1'\n";
400                 $exit_status = 1;
401             }
402             print STDERR "Found macro $1 in $file: $.\n" if $verbosity;
403         }
404     }
405     close (FILE);
407     return $contents;
410 ################################################################
412 # Write output.
413 sub write_aclocal
415     return if ! length ($output);
417     print STDERR "Writing $output_file\n" if $verbosity;
419     open (ACLOCAL, "> " . $output_file)
420         || die "aclocal: couldn't open \`$output_file' for writing: $!\n";
421     print ACLOCAL "dnl $output_file generated automatically by aclocal $VERSION\n";
422     print ACLOCAL "\
423 dnl Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
424 dnl This file is free software; the Free Software Foundation
425 dnl gives unlimited permission to copy and/or distribute it,
426 dnl with or without modifications, as long as this notice is preserved.
428 dnl This program is distributed in the hope that it will be useful,
429 dnl but WITHOUT ANY WARRANTY, to the extent permitted by law; without
430 dnl even the implied warranty of MERCHANTABILITY or FITNESS FOR A
431 dnl PARTICULAR PURPOSE.
434     print ACLOCAL $output;
435     close (ACLOCAL);