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