* automake.in (dist_header): Avoid changing permissions of files
[automake.git] / aclocal.in
blobce7a1a23d3b9e4ccca3a12e3c7d8c3b8468c4b4d
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'",
96      'AM_FUNC_MKTIME', "use \`AC_FUNC_MKTIME'",
98 # These aren't quite obsolete.
99 #      'md_PATH_PROG',
100      );
102 $obsolete_rx = '(' . join ('|', keys %obsolete_macros) . ')';
104 # Matches a macro definition.
105 $ac_defun_rx = "AC_DEFUN\\(\\[?([^],)\n]+)\\]?";
107 # Matches an AC_REQUIRE line.
108 $ac_require_rx = "AC_REQUIRE\\(\\[?([^])]*)\\]?\\)";
112 local (@dirlist) = &parse_arguments (@ARGV);
113 &scan_m4_files ($acdir, @dirlist);
114 &scan_configure;
115 if (! $exit_status)
117     &write_aclocal;
119 &check_acinclude;
121 exit $exit_status;
123 ################################################################
125 # Print usage and exit.
126 sub usage
128     local ($status) = @_;
130     print "Usage: aclocal [OPTIONS] ...\n\n";
131     print "Generate aclocal.m4 by scanning configure.in\n
132   --acdir=DIR           directory holding config files
133   --help                print this help, then exit
134   -I DIR                add directory to search list for .m4 files
135   --output=FILE         put output in FILE (default aclocal.m4)
136   --print-ac-dir        print name of directory holding m4 files
137   --verbose             don't be silent
138   --version             print version number, then exit
140 Report bugs to <bug-automake\@gnu.org>.\n";
142     exit $status;
145 # Parse command line.
146 sub parse_arguments
148     local (@arglist) = @_;
149     local (@dirlist);
150     local ($print_and_exit) = 0;
152     while (@arglist)
153     {
154         if ($arglist[0] =~ /^--acdir=(.+)$/)
155         {
156             $acdir = $1;
157         }
158         elsif ($arglist[0] =~/^--output=(.+)$/)
159         {
160             $output_file = $1;
161         }
162         elsif ($arglist[0] eq '-I')
163         {
164             shift (@arglist);
165             push (@dirlist, $arglist[0]);
166         }
167         elsif ($arglist[0] eq '--print-ac-dir')
168         {
169             $print_and_exit = 1;
170         }
171         elsif ($arglist[0] eq '--verbose')
172         {
173             ++$verbosity;
174         }
175         elsif ($arglist[0] eq '--version')
176         {
177             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
178             print "Copyright (C) 1999 Free Software Foundation, Inc.\n";
179             print "This is free software; see the source for copying conditions.  There is NO\n";
180             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
181             print "Written by Tom Tromey <tromey\@cygnus.com>\n";
182             exit 0;
183         }
184         elsif ($arglist[0] eq '--help')
185         {
186             &usage (0);
187         }
188         else
189         {
190             die "aclocal: unrecognized option -- \`$arglist[0]'\nTry \`aclocal --help' for more information.\n";
191         }
193         shift (@arglist);
194     }
196     if ($print_and_exit)
197     {
198         print $acdir, "\n";
199         exit 0;
200     }
202     return @dirlist;
205 ################################################################
207 sub scan_configure
209     open (CONFIGURE, "configure.in")
210         || die "aclocal: couldn't open \`configure.in': $!\n";
212     # Make sure we include acinclude.m4 if it exists.
213     if (-f 'acinclude.m4')
214     {
215         &add_file ('acinclude.m4');
216     }
218     while (<CONFIGURE>)
219     {
220         # Remove comments from current line.
221         s/\bdnl\b.*$//;
222         s/\#.*$//;
224         if (/$obsolete_rx/o)
225         {
226             local ($hint) = '';
227             if ($obsolete_macros{$1} ne '')
228             {
229                 $hint = '; ' . $obsolete_macros{$1};
230             }
231             warn "aclocal: configure.in: $.: \`$1' is obsolete$hint\n";
232             $exit_status = 1;
233             next;
234         }
236         # Search for things we know about.  The "search" sub is
237         # constructed dynamically by scan_m4_files.
238         if (! &search && /(^|\s+)(AM_[A-Z_]+)/)
239         {
240             # Macro not found, but AM_ prefix found.
241             warn "aclocal: configure.in: $.: macro \`$2' not found in library\n";
242             $exit_status = 1;
243         }
244     }
246     close (CONFIGURE);
249 ################################################################
251 # Check macros in acinclude.m4.  If one is not used, warn.
252 sub check_acinclude
254     local ($key);
256     foreach $key (keys %map)
257     {
258         next unless $map{$key} eq 'acinclude.m4';
259         if (! $macro_seen{$key})
260         {
261             # FIXME: should print line number of acinclude.m4.
262             warn "aclocal: macro \`$key' defined in acinclude.m4 but never used\n";
263         }
264     }
267 ################################################################
269 # Scan all the installed m4 files and construct a map.
270 sub scan_m4_files
272     local (@dirlist) = @_;
274     # First, scan acinclude.m4 if it exists.
275     if (-f 'acinclude.m4')
276     {
277         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
278     }
280     local ($m4dir);
281     foreach $m4dir (@dirlist)
282     {
283         opendir (DIR, $m4dir)
284             || die "aclocal: couldn't open directory \`$m4dir': $!\n";
285         local ($file, $fullfile, $expr);
286         foreach $file (sort grep (! /^\./, readdir (DIR)))
287         {
288             # Only examine .m4 files.
289             next unless $file =~ /\.m4$/;
291             # Skip some files when running out of srcdir.
292             next if $file eq 'aclocal.m4';
294             $fullfile = $m4dir . '/' . $file;
295             $file_contents{$fullfile} = &scan_file ($fullfile);
296         }
297         closedir (DIR);
298     }
300     # Construct a new function that does the searching.  We use a
301     # function (instead of just evalling $search in the loop) so that
302     # "die" is correctly and easily propagated if run.
303     local ($search, $expr, $key) = '';
304     foreach $key (reverse sort keys %map)
305     {
306         # EXPR is a regexp matching the name of the macro.
307         ($expr = $key) =~ s/(\W)/\\$1/g;
308         $search .= ("if (/" . $expr . "/) { & add_macro (" . $key
309                     . "); return 1; }\n");
310     }
311     $search .= "return 0;\n";
312     eval 'sub search { ' . $search . '};';
313     die "internal error: $@\n search is $search " if $@;
316 ################################################################
318 # Add a macro to the output.
319 sub add_macro
321     local ($macro) = @_;
323     # We want to ignore AC_ macros.  However, if an AC_ macro is
324     # defined in (eg) acinclude.m4, then we want to make sure we mark
325     # it as seen.
326     return if $macro =~ /^AC_/ && ! defined $map{$macro};
328     if (! defined $map{$macro})
329     {
330         warn "aclocal: macro \`$macro' required but not defined\n";
331         $exit_status = 1;
332         return;
333     }
335     print STDERR "saw macro $macro\n" if $verbosity;
336     $macro_seen{$macro} = 1;
337     &add_file ($map{$macro});
340 # Add a file to output.
341 sub add_file
343     local ($file) = @_;
345     # Only add a file once.
346     return if ($file_seen{$file});
347     $file_seen{$file} = 1;
349     $output .= $file_contents{$file} . "\n";
350     local ($a, @rlist);
351     foreach (split ("\n", $file_contents{$file}))
352     {
353         # This is a hack for Perl 4.
354         $a = $_;
355         if ($a =~ /$ac_require_rx/g)
356         {
357             push (@rlist, $1);
358         }
360         # This function constructed dynamically.
361         if (! &search && /(^|\s+)(AM_[A-Z_]+)/)
362         {
363             # Macro not found, but AM_ prefix found.
364             warn "aclocal: configure.in: $.: macro \`$2' not found in library\n";
365             $exit_status = 1;
366         }
367     }
369     local ($macro);
370     foreach $macro (@rlist)
371     {
372         &add_macro ($macro);
373     }
376 # Scan a single M4 file.  Return contents.
377 sub scan_file
379     local ($file) = @_;
381     open (FILE, $file)
382         || die "aclocal: couldn't open \`$file': $!\n";
383     local ($contents) = '';
384     while (<FILE>)
385     {
386         # Ignore `##' lines.
387         next if /^##/;
389         $contents .= $_;
391         if (/$ac_defun_rx/)
392         {
393             if (!defined $map{$1})
394             {
395                 $map{$1} = $file;
396             }
397             # Allow acinclude.m4 to override other macro files.
398             elsif ($map{$1} ne 'acinclude.m4' || $file eq 'acinclude.m4')
399             {
400                 warn "aclocal: $file: $.: duplicated macro \`$1'\n";
401                 $exit_status = 1;
402             }
403             print STDERR "Found macro $1 in $file: $.\n" if $verbosity;
404         }
405     }
406     close (FILE);
408     return $contents;
411 ################################################################
413 # Write output.
414 sub write_aclocal
416     return if ! length ($output);
418     print STDERR "Writing $output_file\n" if $verbosity;
420     open (ACLOCAL, "> " . $output_file)
421         || die "aclocal: couldn't open \`$output_file' for writing: $!\n";
422     print ACLOCAL "dnl $output_file generated automatically by aclocal $VERSION\n";
423     print ACLOCAL "\
424 dnl Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
425 dnl This file is free software; the Free Software Foundation
426 dnl gives unlimited permission to copy and/or distribute it,
427 dnl with or without modifications, as long as this notice is preserved.
429 dnl This program is distributed in the hope that it will be useful,
430 dnl but WITHOUT ANY WARRANTY, to the extent permitted by law; without
431 dnl even the implied warranty of MERCHANTABILITY or FITNESS FOR A
432 dnl PARTICULAR PURPOSE.
435     print ACLOCAL $output;
436     close (ACLOCAL);