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