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