* configure.in (AM_INIT_AUTOMAKE): Set the fork identifier to
[automake.git] / aclocal.in
blobbe0fa47ea51db3102b0346d207aec2f6d9138c12
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 # aclocal - create aclocal.m4 by scanning configure.ac
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.ac 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 # Name of the top autoconf input: `configure.ac' or `configure.in'
64 $configure_ac = '';
66 @obsolete_macros =
67     (
68      'AC_FEATURE_CTYPE',
69      'AC_FEATURE_ERRNO',
70      'AC_FEATURE_EXIT',
71      'AC_SYSTEM_HEADER',
72      'fp_C_PROTOTYPES',
73      'fp_FUNC_FNMATCH',
74      'fp_PROG_CC_STDC',
75      'fp_PROG_INSTALL',
76      'fp_WITH_DMALLOC',
77      'fp_WITH_REGEX',
78      'gm_PROG_LIBTOOL',
79      'jm_MAINTAINER_MODE',
80      'md_TYPE_PTRDIFF_T',
81      'ud_PATH_LISPDIR',
82      'ud_GNU_GETTEXT',
84      # Now part of autoconf proper, under a different name.
85      'AM_FUNC_FNMATCH',
86      'AM_SANITY_CHECK_CC',
87      'AM_PROG_INSTALL',
88      'AM_EXEEXT',
89      'AM_CYGWIN32',
90      'AM_MINGW32',
92 # These aren't quite obsolete.
93 #      'md_PATH_PROG',
94 #      'ud_LC_MESSAGES',
95 #      'ud_WITH_NLS'
96      );
98 $obsolete_rx = '(' . join ('|', @obsolete_macros) . ')';
100 # Matches a macro definition.
101 $ac_defun_rx = "AC_DEFUN\\(\\[?([^],)\n]+)\\]?";
103 # Matches an AC_REQUIRE line.
104 $ac_require_rx = "AC_REQUIRE\\(\\[?([^])]*)\\]?\\)";
108 local (@dirlist) = &parse_arguments (@ARGV);
109 &scan_m4_files ($acdir, @dirlist);
110 &scan_configure;
111 if (! $exit_status)
113     &write_aclocal;
115 &check_acinclude;
117 exit $exit_status;
119 ################################################################
121 # Print usage and exit.
122 sub usage
124     local ($status) = @_;
126     print "Usage: aclocal [OPTIONS] ...\n\n";
127     print "Generate aclocal.m4 by scanning configure.ac\n
128   --acdir=DIR           directory holding config files
129   --help                print this help, then exit
130   -I DIR                add directory to search list for .m4 files
131   --output=FILE         put output in FILE (default aclocal.m4)
132   --print-ac-dir        print name of directory holding m4 files
133   --verbose             don't be silent
134   --version             print version number, then exit
136 Report bugs to <bug-automake\@gnu.org>.\n";
138     exit $status;
141 # Parse command line.
142 sub parse_arguments
144     local (@arglist) = @_;
145     local (@dirlist);
146     local ($print_and_exit) = 0;
148     while (@arglist)
149     {
150         if ($arglist[0] =~ /^--acdir=(.+)$/)
151         {
152             $acdir = $1;
153         }
154         elsif ($arglist[0] =~/^--output=(.+)$/)
155         {
156             $output_file = $1;
157         }
158         elsif ($arglist[0] eq '-I')
159         {
160             shift (@arglist);
161             push (@dirlist, $arglist[0]);
162         }
163         elsif ($arglist[0] eq '--print-ac-dir')
164         {
165             $print_and_exit = 1;
166         }
167         elsif ($arglist[0] eq '--verbose')
168         {
169             ++$verbosity;
170         }
171         elsif ($arglist[0] eq '--version')
172         {
173             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
174             print "Copyright (C) 1999 Free Software Foundation, Inc.\n";
175             print "This is free software; see the source for copying conditions.  There is NO\n";
176             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
177             print "Written by Tom Tromey <tromey\@cygnus.com>\n";
178             exit 0;
179         }
180         elsif ($arglist[0] eq '--help')
181         {
182             &usage (0);
183         }
184         else
185         {
186             die "aclocal: unrecognized option -- \`$arglist[0]'\nTry \`aclocal --help' for more information.\n";
187         }
189         shift (@arglist);
190     }
192     if ($print_and_exit)
193     {
194         print $acdir, "\n";
195         exit 0;
196     }
198     return @dirlist;
201 ################################################################
203 sub scan_configure
205     warn "aclocal: both `configure.ac' and `configure.in' present:"
206          . " ignoring `configure.in'\n"
207          if -f 'configure.ac' && -f 'configure.in';
208     $configure_ac = 'configure.in'
209          if -f 'configure.in';
210     $configure_ac = 'configure.ac'
211          if -f 'configure.ac';
212     die "aclocal: `configure.ac' or `configure.in' is required\n"
213          if !$configure_ac;
215     open (CONFIGURE, $configure_ac)
216         || die "aclocal: couldn't open \`$configure_ac': $!\n";
218     # Make sure we include acinclude.m4 if it exists.
219     if (-f 'acinclude.m4')
220     {
221         &add_file ('acinclude.m4');
222     }
224     while (<CONFIGURE>)
225     {
226         # Remove comments from current line.
227         s/\bdnl\b.*$//;
228         s/\#.*$//;
230         if (/$obsolete_rx/o)
231         {
232             chop;
233             warn "aclocal: $configure_ac: $.: obsolete macro \`$_'\n";
234             $exit_status = 1;
235             next;
236         }
238         # Search for things we know about.  The "search" sub is
239         # constructed dynamically by scan_m4_files.
240         if (! &search && /(^|\s+)(AM_[A-Z_]+)/)
241         {
242             # Macro not found, but AM_ prefix found.
243             warn "aclocal: $configure_ac: $.: macro \`$2' not found in library\n";
244             $exit_status = 1;
245         }
246     }
248     close (CONFIGURE);
251 ################################################################
253 # Check macros in acinclude.m4.  If one is not used, warn.
254 sub check_acinclude
256     local ($key);
258     foreach $key (keys %map)
259     {
260         next unless $map{$key} eq 'acinclude.m4';
261         if (! $macro_seen{$key})
262         {
263             # FIXME: should print line number of acinclude.m4.
264             warn "aclocal: macro \`$key' defined in acinclude.m4 but never used\n";
265         }
266     }
269 ################################################################
271 # Scan all the installed m4 files and construct a map.
272 sub scan_m4_files
274     local (@dirlist) = @_;
276     # First, scan acinclude.m4 if it exists.
277     if (-f 'acinclude.m4')
278     {
279         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
280     }
282     local ($m4dir);
283     foreach $m4dir (@dirlist)
284     {
285         opendir (DIR, $m4dir)
286             || die "aclocal: couldn't open directory \`$m4dir': $!\n";
287         local ($file, $fullfile, $expr);
288         foreach $file (sort grep (! /^\./, readdir (DIR)))
289         {
290             # Only examine .m4 files.
291             next unless $file =~ /\.m4$/;
293             # Skip some files when running out of srcdir.
294             next if $file eq 'aclocal.m4';
296             $fullfile = $m4dir . '/' . $file;
297             $file_contents{$fullfile} = &scan_file ($fullfile);
298         }
299         closedir (DIR);
300     }
302     # Construct a new function that does the searching.  We use a
303     # function (instead of just evalling $search in the loop) so that
304     # "die" is correctly and easily propagated if run.
305     local ($search, $expr, $key) = '';
306     foreach $key (reverse sort keys %map)
307     {
308         # EXPR is a regexp matching the name of the macro.
309         ($expr = $key) =~ s/(\W)/\\$1/g;
310         $search .= ("if (/" . $expr . "/) { & add_macro (" . $key
311                     . "); return 1; }\n");
312     }
313     $search .= "return 0;\n";
314     eval 'sub search { ' . $search . '};';
315     die "internal error: $@\n search is $search " if $@;
318 ################################################################
320 # Add a macro to the output.
321 sub add_macro
323     local ($macro) = @_;
325     # We want to ignore AC_ macros.  However, if an AC_ macro is
326     # defined in (eg) acinclude.m4, then we want to make sure we mark
327     # it as seen.
328     return if $macro =~ /^AC_/ && ! defined $map{$macro};
330     if (! defined $map{$macro})
331     {
332         warn "aclocal: macro \`$macro' required but not defined\n";
333         $exit_status = 1;
334         return;
335     }
337     print STDERR "saw macro $macro\n" if $verbosity;
338     $macro_seen{$macro} = 1;
339     &add_file ($map{$macro});
342 # Add a file to output.
343 sub add_file
345     local ($file) = @_;
347     # Only add a file once.
348     return if ($file_seen{$file});
349     $file_seen{$file} = 1;
351     $output .= $file_contents{$file} . "\n";
352     local ($a, @rlist);
353     foreach (split ("\n", $file_contents{$file}))
354     {
355         # This is a hack for Perl 4.
356         $a = $_;
357         if ($a =~ /$ac_require_rx/g)
358         {
359             push (@rlist, $1);
360         }
362         # This function constructed dynamically.
363         if (! &search && /(^|\s+)(AM_[A-Z_]+)/)
364         {
365             # Macro not found, but AM_ prefix found.
366             warn "aclocal: $configure_ac: $.: macro \`$2' not found in library\n";
367             $exit_status = 1;
368         }
369     }
371     local ($macro);
372     foreach $macro (@rlist)
373     {
374         &add_macro ($macro);
375     }
378 # Scan a single M4 file.  Return contents.
379 sub scan_file
381     local ($file) = @_;
383     open (FILE, $file)
384         || die "aclocal: couldn't open \`$file': $!\n";
385     local ($contents) = '';
386     while (<FILE>)
387     {
388         # Ignore `##' lines.
389         next if /^##/;
391         $contents .= $_;
393         if (/$ac_defun_rx/)
394         {
395             if (!defined $map{$1})
396             {
397                 $map{$1} = $file;
398             }
399             # Allow acinclude.m4 to override other macro files.
400             elsif ($map{$1} ne 'acinclude.m4' || $file eq 'acinclude.m4')
401             {
402                 warn "aclocal: $file: $.: duplicated macro \`$1'\n";
403                 $exit_status = 1;
404             }
405             print STDERR "Found macro $1 in $file: $.\n" if $verbosity;
406         }
407     }
408     close (FILE);
410     return $contents;
413 ################################################################
415 # Write output.
416 sub write_aclocal
418     return if ! length ($output);
420     print STDERR "Writing $output_file\n" if $verbosity;
422     open (ACLOCAL, "> " . $output_file)
423         || die "aclocal: couldn't open \`$output_file' for writing: $!\n";
424     print ACLOCAL "dnl $output_file generated automatically by aclocal $VERSION\n";
425     print ACLOCAL "\
426 dnl Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
427 dnl This file is free software; the Free Software Foundation
428 dnl gives unlimited permission to copy and/or distribute it,
429 dnl with or without modifications, as long as this notice is preserved.
431 dnl This program is distributed in the hope that it will be useful,
432 dnl but WITHOUT ANY WARRANTY, to the extent permitted by law; without
433 dnl even the implied warranty of MERCHANTABILITY or FITNESS FOR A
434 dnl PARTICULAR PURPOSE.
437     print ACLOCAL $output;
438     close (ACLOCAL);