For PR automake/358:
[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 $perllibdir = $ENV{'perllibdir'} || '@datadir@/@PACKAGE@-@APIVERSION@';
33   unshift @INC, $perllibdir;
34 }
35
36 use Automake::General;
37 use Automake::XFile;
38
39 # Some constants.
40 $VERSION = '@VERSION@';
41 $APIVERSION = '@APIVERSION@';
42 $PACKAGE = '@PACKAGE@';
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 # contains a list of directories, one per line, to be added
48 # to the dirlist in addition to $acdir, as if -I had been
49 # added to the command line.  If acdir has been redirected,
50 # we will also check the specified acdir (this is done later).
51 $default_dirlist = "$default_acdir/dirlist";
52
53 # Some globals.
54
55 # Exit status.
56 $exit_status = 0;
57
58 # Name of the top autoconf input: `configure.ac' or `configure.in'.
59 $configure_ac = find_configure_ac;
60
61 # Text to output.
62 $output = '';
63
64 # Output file name.
65 $output_file = 'aclocal.m4';
66
67 # Which macros have been seen.
68 %macro_seen = ();
69
70 # Which files have been seen.
71 %file_seen = ();
72
73 # Map macro names to file names.
74 %map = ();
75
76 # Map file names to file contents.
77 %file_contents = ();
78
79 # How much to say.
80 $verbose = 0;
81
82 # Matches a macro definition.
83 $ac_defun_rx = "A[CU]_DEFUN\\(\\[?([^],)\n]+)\\]?";
84
85 # Matches an AC_REQUIRE line.
86 $ac_require_rx = "AC_REQUIRE\\(\\[?([^])]*)\\]?\\)";
87
88 \f
89
90 local (@dirlist) = &parse_arguments (@ARGV);
91 &scan_m4_files (@dirlist);
92 &scan_configure;
93 if (! $exit_status)
94 {
95     &write_aclocal;
96 }
97 &check_acinclude;
98
99 exit $exit_status;
100
101 ################################################################
102
103 # Print usage and exit.
104 sub usage ($)
105 {
106     local ($status) = @_;
107
108     print "Usage: aclocal [OPTIONS] ...\n\n";
109     print "\
110 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
111
112   --acdir=DIR           directory holding config files
113   --help                print this help, then exit
114   -I DIR                add directory to search list for .m4 files
115   --output=FILE         put output in FILE (default aclocal.m4)
116   --print-ac-dir        print name of directory holding m4 files
117   --verbose             don't be silent
118   --version             print version number, then exit
119
120 Report bugs to <bug-automake\@gnu.org>.\n";
121
122     exit $status;
123 }
124
125 # Parse command line.
126 sub parse_arguments (@)
127 {
128     local (@arglist) = @_;
129     local (@dirlist);
130     local ($print_and_exit) = 0;
131
132     while (@arglist)
133     {
134         if ($arglist[0] =~ /^--acdir=(.+)$/)
135         {
136             $acdir = $1;
137         }
138         elsif ($arglist[0] =~/^--output=(.+)$/)
139         {
140             $output_file = $1;
141         }
142         elsif ($arglist[0] eq '-I')
143         {
144             shift (@arglist);
145             push (@dirlist, $arglist[0]);
146         }
147         elsif ($arglist[0] eq '--print-ac-dir')
148         {
149             $print_and_exit = 1;
150         }
151         elsif ($arglist[0] eq '--verbose')
152         {
153             ++$verbose;
154         }
155         elsif ($arglist[0] eq '--version')
156         {
157             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
158             print "Copyright (C) 2002 Free Software Foundation, Inc.\n";
159             print "This is free software; see the source for copying conditions.  There is NO\n";
160             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
161             print "Written by Tom Tromey <tromey\@redhat.com>\n";
162             exit 0;
163         }
164         elsif ($arglist[0] eq '--help')
165         {
166             &usage (0);
167         }
168         else
169         {
170             die "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
171         }
172
173         shift (@arglist);
174     }
175
176     if ($print_and_exit)
177     {
178         print $acdir, "\n";
179         exit 0;
180     }
181
182     $default_dirlist="$acdir/dirlist"
183         if $acdir ne $default_acdir;
184
185     # Search the versioned directory near the end, and then the
186     # unversioned directory last.  Only do this if the user didn't
187     # override acdir.
188     push (@dirlist, "$acdir-$APIVERSION")
189         if $acdir eq $default_acdir;
190
191     # By default $(datadir)/aclocal doesn't exist.  We don't want to
192     # get an error in the case where we are searching the default
193     # directory and it hasn't been created.
194     push (@dirlist, $acdir)
195         unless $acdir eq $default_acdir && ! -d $acdir;
196
197     # Finally, adds any directory listed in the `dirlist' file.
198     if (open (DEFAULT_DIRLIST, $default_dirlist))
199     {
200         while (<DEFAULT_DIRLIST>)
201         {
202             # Ignore '#' lines.
203             next if /^#/;
204             # strip off newlines and end-of-line comments
205             s/\s*\#.*$//;
206             chomp ($contents=$_);
207             if (-d $contents )
208             {
209                 push (@dirlist, $contents);
210             }
211         }
212         close (DEFAULT_DIRLIST);
213     }
214
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     my (@rlist);
358     foreach (split ("\n", $file_contents{$file}))
359     {
360         # Remove comments from current line.
361         s/\bdnl\b.*$//;
362         s/\#.*$//;
363
364         if (/$ac_require_rx/g)
365         {
366             push (@rlist, $1);
367         }
368
369         # The search function is constructed dynamically by
370         # scan_m4_files.  The last parenthethical match makes sure we
371         # don't match things that look like macro assignments or
372         # AC_SUBSTs.
373         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
374         {
375             # Macro not found, but AM_ prefix found.
376             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
377             $exit_status = 1;
378         }
379     }
380
381     local ($macro);
382     foreach $macro (@rlist)
383     {
384         &add_macro ($macro);
385     }
386 }
387
388 # Scan a single M4 file.  Return contents.
389 sub scan_file ($)
390 {
391     local ($file) = @_;
392
393     my $fh = new Automake::XFile $file;
394     my $contents = '';
395     while ($_ = $fh->getline)
396     {
397         # Ignore `##' lines.
398         next if /^##/;
399
400         $contents .= $_;
401
402         if (/$ac_defun_rx/)
403         {
404             if (! defined $map{$1})
405             {
406                 $map{$1} = $file;
407             }
408
409             # Note: we used to give an error here if we saw a
410             # duplicated macro.  However, this turns out to be
411             # extremely unpopular.  It causes actual problems which
412             # are hard to work around, especially when you must
413             # mix-and-match tool versions.
414
415             print STDERR "aclocal: found macro $1 in $file: $.\n" if $verbose;
416         }
417     }
418
419     return $contents;
420 }
421
422 ################################################################
423
424 # Write output.
425 sub write_aclocal ()
426 {
427     return if ! length ($output);
428
429     print STDERR "aclocal: writing $output_file\n" if $verbose;
430
431     my $out = new Automake::XFile "> $output_file";
432
433 # We used to print `# $output_file generated automatically etc.'  But
434 # this creates spurious differences when using autoreconf.  Autoreconf
435 # creates aclocal.m4t and then rename it to aclocal.m4, but the
436 # rebuild rules generated by Automake create aclocal.m4 directly --
437 # this would gives two ways to get the same file, with a different
438 # name in the header.
439     print $out
440 "# generated automatically by aclocal $VERSION -*- Autoconf -*-
441
442 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
443 # Free Software Foundation, Inc.
444 # This file is free software; the Free Software Foundation
445 # gives unlimited permission to copy and/or distribute it,
446 # with or without modifications, as long as this notice is preserved.
447
448 # This program is distributed in the hope that it will be useful,
449 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
450 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
451 # PARTICULAR PURPOSE.
452
453 $output";
454 }
455
456 ### Setup "GNU" style for perl-mode and cperl-mode.
457 ## Local Variables:
458 ## perl-indent-level: 2
459 ## perl-continued-statement-offset: 2
460 ## perl-continued-brace-offset: 0
461 ## perl-brace-offset: 0
462 ## perl-brace-imaginary-offset: 0
463 ## perl-label-offset: -2
464 ## cperl-indent-level: 2
465 ## cperl-brace-offset: 0
466 ## cperl-continued-brace-offset: 0
467 ## cperl-label-offset: -2
468 ## cperl-extra-newline-before-brace: t
469 ## cperl-merge-trailing-else: nil
470 ## cperl-continued-statement-offset: 2
471 ## End: