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