2002-07-31 Charles Wilson <cwilson@ece.gatech.edu>
[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 # contains a list of directories, one per line, to be added
50 # to the dirlist in addition to $acdir, as if -I had been
51 # added to the command line.  If acdir has been redirected,
52 # we will also check the specified acdir (this is done later).
53 $default_dirlist = "$default_acdir/dirlist";
54
55 # Some globals.
56
57 # Exit status.
58 $exit_status = 0;
59
60 # Name of the top autoconf input: `configure.ac' or `configure.in'.
61 $configure_ac = find_configure_ac;
62
63 # Text to output.
64 $output = '';
65
66 # Output file name.
67 $output_file = 'aclocal.m4';
68
69 # Which macros have been seen.
70 %macro_seen = ();
71
72 # Which files have been seen.
73 %file_seen = ();
74
75 # Map macro names to file names.
76 %map = ();
77
78 # Map file names to file contents.
79 %file_contents = ();
80
81 # How much to say.
82 $verbose = 0;
83
84 # Matches a macro definition.
85 $ac_defun_rx = "A[CU]_DEFUN\\(\\[?([^],)\n]+)\\]?";
86
87 # Matches an AC_REQUIRE line.
88 $ac_require_rx = "AC_REQUIRE\\(\\[?([^])]*)\\]?\\)";
89
90 \f
91
92 local (@dirlist) = &parse_arguments (@ARGV);
93 &scan_m4_files (@dirlist);
94 &scan_configure;
95 if (! $exit_status)
96 {
97     &write_aclocal;
98 }
99 &check_acinclude;
100
101 exit $exit_status;
102
103 ################################################################
104
105 # Print usage and exit.
106 sub usage
107 {
108     local ($status) = @_;
109
110     print "Usage: aclocal [OPTIONS] ...\n\n";
111     print "\
112 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
113
114   --acdir=DIR           directory holding config files
115   --help                print this help, then exit
116   -I DIR                add directory to search list for .m4 files
117   --output=FILE         put output in FILE (default aclocal.m4)
118   --print-ac-dir        print name of directory holding m4 files
119   --verbose             don't be silent
120   --version             print version number, then exit
121
122 Report bugs to <bug-automake\@gnu.org>.\n";
123
124     exit $status;
125 }
126
127 # Parse command line.
128 sub parse_arguments
129 {
130     local (@arglist) = @_;
131     local (@dirlist);
132     local ($print_and_exit) = 0;
133
134     while (@arglist)
135     {
136         if ($arglist[0] =~ /^--acdir=(.+)$/)
137         {
138             $acdir = $1;
139         }
140         elsif ($arglist[0] =~/^--output=(.+)$/)
141         {
142             $output_file = $1;
143         }
144         elsif ($arglist[0] eq '-I')
145         {
146             shift (@arglist);
147             push (@dirlist, $arglist[0]);
148         }
149         elsif ($arglist[0] eq '--print-ac-dir')
150         {
151             $print_and_exit = 1;
152         }
153         elsif ($arglist[0] eq '--verbose')
154         {
155             ++$verbose;
156         }
157         elsif ($arglist[0] eq '--version')
158         {
159             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
160             print "Copyright 2002 Free Software Foundation, Inc.\n";
161             print "This is free software; see the source for copying conditions.  There is NO\n";
162             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
163             print "Written by Tom Tromey <tromey\@redhat.com>\n";
164             exit 0;
165         }
166         elsif ($arglist[0] eq '--help')
167         {
168             &usage (0);
169         }
170         else
171         {
172             die "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
173         }
174
175         shift (@arglist);
176     }
177
178     if ($print_and_exit)
179     {
180         print $acdir, "\n";
181         exit 0;
182     }
183
184     $default_dirlist="$acdir/dirlist"
185         if $acdir ne $default_acdir;
186
187     if (open (DEFAULT_DIRLIST, $default_dirlist))
188     {
189         while (<DEFAULT_DIRLIST>)
190         {
191             # Ignore '#' lines.
192             next if /^#/;
193             # strip off newlines and end-of-line comments
194             s/\s*\#.*$//;
195             chomp ($contents=$_);
196             if (-d $contents )
197             {
198                 push (@dirlist, $contents);
199             }   
200         }
201         close (DEFAULT_DIRLIST);
202     }
203
204     # Search the versioned directory near the end, and then the
205     # unversioned directory last.  Only do this if the user didn't
206     # override acdir.
207     push (@dirlist, "$acdir-$APIVERSION")
208         if $acdir eq $default_acdir;
209
210     # By default $(datadir)/aclocal doesn't exist.  We don't want to
211     # get an error in the case where we are searching the default
212     # directory and it hasn't been created.
213     push (@dirlist, $acdir)
214         unless $acdir eq $default_acdir && ! -d $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         # Search for things we know about.  The "search" sub is
242         # constructed dynamically by scan_m4_files.  The last
243         # parenthethical match makes sure we don't match things that
244         # look like macro assignments or AC_SUBSTs.
245         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
246         {
247             # Macro not found, but AM_ prefix found.
248             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
249             $exit_status = 1;
250         }
251     }
252
253     close (CONFIGURE);
254 }
255
256 ################################################################
257
258 # Check macros in acinclude.m4.  If one is not used, warn.
259 sub check_acinclude
260 {
261     local ($key);
262
263     foreach $key (keys %map)
264     {
265         next unless $map{$key} eq 'acinclude.m4';
266         if (! $macro_seen{$key})
267         {
268             # FIXME: should print line number of acinclude.m4.
269             warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
270         }
271     }
272 }
273
274 ################################################################
275
276 # Scan all the installed m4 files and construct a map.
277 sub scan_m4_files
278 {
279     local (@dirlist) = @_;
280
281     # First, scan acinclude.m4 if it exists.
282     if (-f 'acinclude.m4')
283     {
284         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
285     }
286
287     local ($m4dir);
288     foreach $m4dir (@dirlist)
289     {
290         opendir (DIR, $m4dir)
291             || die "aclocal: couldn't open directory `$m4dir': $!\n";
292         local ($file, $fullfile);
293         foreach $file (sort grep (! /^\./, readdir (DIR)))
294         {
295             # Only examine .m4 files.
296             next unless $file =~ /\.m4$/;
297
298             # Skip some files when running out of srcdir.
299             next if $file eq 'aclocal.m4';
300
301             $fullfile = $m4dir . '/' . $file;
302             $file_contents{$fullfile} = &scan_file ($fullfile);
303         }
304         closedir (DIR);
305     }
306
307     # Construct a new function that does the searching.  We use a
308     # function (instead of just evalling $search in the loop) so that
309     # "die" is correctly and easily propagated if run.
310     my $search = "sub search {\nmy \$found = 0;\n";
311     foreach my $key (reverse sort keys %map)
312     {
313         # EXPR is a regexp matching the name of the macro.
314         (my $expr = $key) =~ s/(\W)/\\$1/g;
315         $search .= ('if (/\b' . $key . '\b/) { & add_macro (' . $key
316                     . '); $found = 1; }' . "\n");
317     }
318     $search .= "return \$found;\n};\n";
319     eval $search;
320     die "internal error: $@\n search is $search" if $@;
321 }
322
323 ################################################################
324
325 # Add a macro to the output.
326 sub add_macro
327 {
328     local ($macro) = @_;
329
330     # We want to ignore AC_ macros.  However, if an AC_ macro is
331     # defined in (eg) acinclude.m4, then we want to make sure we mark
332     # it as seen.
333     return if $macro =~ /^AC_/ && ! defined $map{$macro};
334
335     if (! defined $map{$macro})
336     {
337         warn "aclocal: macro `$macro' required but not defined\n";
338         $exit_status = 1;
339         return;
340     }
341
342     print STDERR "aclocal: saw macro $macro\n" if $verbose;
343     $macro_seen{$macro} = 1;
344     &add_file ($map{$macro});
345 }
346
347 # Add a file to output.
348 sub add_file
349 {
350     local ($file) = @_;
351
352     # Only add a file once.
353     return if ($file_seen{$file});
354     $file_seen{$file} = 1;
355
356     $output .= $file_contents{$file} . "\n";
357     local ($a, @rlist);
358     foreach (split ("\n", $file_contents{$file}))
359     {
360         # This is a hack for Perl 4.
361         $a = $_;
362         if ($a =~ /$ac_require_rx/g)
363         {
364             push (@rlist, $1);
365         }
366
367         # Remove comments from current line.
368         s/\bdnl\b.*$//;
369         s/\#.*$//;
370
371         # The search function is constructed dynamically by
372         # scan_m4_files.  The last parenthethical match makes sure we
373         # don't match things that look like macro assignments or
374         # AC_SUBSTs.
375         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
376         {
377             # Macro not found, but AM_ prefix found.
378             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
379             $exit_status = 1;
380         }
381     }
382
383     local ($macro);
384     foreach $macro (@rlist)
385     {
386         &add_macro ($macro);
387     }
388 }
389
390 # Scan a single M4 file.  Return contents.
391 sub scan_file
392 {
393     local ($file) = @_;
394
395     my $fh = new Automake::XFile $file;
396     my $contents = '';
397     while ($_ = $fh->getline)
398     {
399         # Ignore `##' lines.
400         next if /^##/;
401
402         $contents .= $_;
403
404         if (/$ac_defun_rx/)
405         {
406             if (! defined $map{$1})
407             {
408                 $map{$1} = $file;
409             }
410
411             # Note: we used to give an error here if we saw a
412             # duplicated macro.  However, this turns out to be
413             # extremely unpopular.  It causes actual problems which
414             # are hard to work around, especially when you must
415             # mix-and-match tool versions.
416
417             print STDERR "aclocal: found macro $1 in $file: $.\n" if $verbose;
418         }
419     }
420
421     return $contents;
422 }
423
424 ################################################################
425
426 # Write output.
427 sub write_aclocal
428 {
429     return if ! length ($output);
430
431     print STDERR "aclocal: writing $output_file\n" if $verbose;
432
433     my $out = new Automake::XFile "> $output_file";
434
435 # We used to print `# $output_file generated automatically etc.'  But
436 # this creates spurious differences when using autoreconf.  Autoreconf
437 # creates aclocal.m4t and then rename it to aclocal.m4, but the
438 # rebuild rules generated by Automake create aclocal.m4 directly --
439 # this would gives two ways to get the same file, with a different
440 # name in the header.
441     print $out
442 "# generated automatically by aclocal $VERSION -*- Autoconf -*-
443
444 # Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
445 # Free Software Foundation, Inc.
446 # This file is free software; the Free Software Foundation
447 # gives unlimited permission to copy and/or distribute it,
448 # with or without modifications, as long as this notice is preserved.
449
450 # This program is distributed in the hope that it will be useful,
451 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
452 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
453 # PARTICULAR PURPOSE.
454
455 $output";
456 }
457
458 ### Setup "GNU" style for perl-mode and cperl-mode.
459 ## Local Variables:
460 ## perl-indent-level: 2
461 ## perl-continued-statement-offset: 2
462 ## perl-continued-brace-offset: 0
463 ## perl-brace-offset: 0
464 ## perl-brace-imaginary-offset: 0
465 ## perl-label-offset: -2
466 ## cperl-indent-level: 2
467 ## cperl-brace-offset: 0
468 ## cperl-continued-brace-offset: 0
469 ## cperl-label-offset: -2
470 ## cperl-extra-newline-before-brace: t
471 ## cperl-merge-trailing-else: nil
472 ## cperl-continued-statement-offset: 2
473 ## End: