source: trunk/essentials/sys-devel/automake-1.8/aclocal.in@ 3132

Last change on this file since 3132 was 3118, checked in by bird, 18 years ago

automake 1.8.5

File size: 18.9 KB
Line 
1#!@PERL@
2# -*- perl -*-
3# @configure_input@
4
5eval '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, 2003, 2004
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
30BEGIN
31{
32 my $perllibdir = $ENV{'perllibdir'} || '@datadir@/@PACKAGE@-@APIVERSION@';
33 unshift @INC, (split '@PATH_SEPARATOR@', $perllibdir);
34}
35
36use Automake::Config;
37use Automake::General;
38use Automake::Configure_ac;
39use Automake::Channels;
40use Automake::XFile;
41use Automake::FileUtils;
42use File::Basename;
43use File::stat;
44use Cwd;
45
46# Note that this isn't pkgdatadir, but a separate directory.
47# Note also that the versioned directory is handled later.
48$acdir = '@datadir@/aclocal';
49$default_acdir = $acdir;
50# contains a list of directories, one per line, to be added
51# to the dirlist in addition to $acdir, as if -I had been
52# added to the command line. If acdir has been redirected,
53# we will also check the specified acdir (this is done later).
54$default_dirlist = "$default_acdir/dirlist";
55
56# Some globals.
57
58# configure.ac or configure.in.
59my $configure_ac;
60
61# Output file name.
62$output_file = 'aclocal.m4';
63
64# Modification time of the youngest dependency.
65$greatest_mtime = 0;
66
67# Option --force.
68$force_output = 0;
69
70# Which macros have been seen.
71%macro_seen = ();
72
73# Which files have been seen.
74%file_seen = ();
75
76# Remember the order into which we scanned the files.
77# It's important to output the contents of aclocal.m4 in the opposite order.
78# (Definitions in first files we have scanned should override those from
79# later files. So they must appear last in the output.)
80@file_order = ();
81
82# Map macro names to file names.
83%map = ();
84
85# Map file names to file contents.
86%file_contents = ();
87
88# Map file names to included files (transitively closed).
89%file_includes = ();
90
91# How much to say.
92$verbose = 0;
93
94# Matches a macro definition.
95# AC_DEFUN([macroname], ...)
96# or
97# AC_DEFUN(macroname, ...)
98# When macroname is `['-quoted , we accept any character in the name,
99# except `]'. Otherwise macroname stops on the first `]', `,', `)',
100# or `\n' encountered.
101$ac_defun_rx = "A[CU]_DEFUN\\((?:\\[([^]]+)\\]|([^],)\n]+))";
102
103# Matches an AC_REQUIRE line.
104$ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
105
106# Matches an m4_include line
107$m4_include_rx = "(?:m4_)?s?include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
108
109
110
111################################################################
112
113# Check macros in acinclude.m4. If one is not used, warn.
114sub check_acinclude ()
115{
116 foreach my $key (keys %map)
117 {
118 # FIXME: should print line number of acinclude.m4.
119 warn ("aclocal: warning: macro `$key' defined in "
120 . "acinclude.m4 but never used\n")
121 if $map{$key} eq 'acinclude.m4' && ! $macro_seen{$key};
122 }
123}
124
125################################################################
126
127# Scan all the installed m4 files and construct a map.
128sub scan_m4_files (@)
129{
130 local (@dirlist) = @_;
131
132 # First, scan configure.ac. It may contain macro definitions,
133 # or may include other files that define macros.
134 &scan_file ($configure_ac);
135
136 # Then, scan acinclude.m4 if it exists.
137 if (-f 'acinclude.m4')
138 {
139 &scan_file ('acinclude.m4');
140 }
141
142 # Finally, scan all files in our search path.
143 local ($m4dir);
144 foreach $m4dir (@dirlist)
145 {
146 if (! opendir (DIR, $m4dir))
147 {
148 print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
149 exit 1;
150 }
151
152 local ($file, $fullfile);
153 # We reverse the directory contents so that foo2.m4 gets
154 # used in preference to foo1.m4.
155 foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
156 {
157 # Only examine .m4 files.
158 next unless $file =~ /\.m4$/;
159
160 # Skip some files when running out of srcdir.
161 next if $file eq 'aclocal.m4';
162
163 $fullfile = $m4dir . '/' . $file;
164 &scan_file ($fullfile);
165 }
166 closedir (DIR);
167 }
168
169 # Construct a new function that does the searching. We use a
170 # function (instead of just evaluating $search in the loop) so that
171 # "die" is correctly and easily propagated if run.
172 my $search = "sub search {\nmy \$found = 0;\n";
173 foreach my $key (reverse sort keys %map)
174 {
175 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
176 . '"); $found = 1; }' . "\n");
177 }
178 $search .= "return \$found;\n};\n";
179 eval $search;
180 die "internal error: $@\n search is $search" if $@;
181}
182
183################################################################
184
185# Add a macro to the output.
186sub add_macro ($)
187{
188 local ($macro) = @_;
189
190 # We want to ignore AC_ macros. However, if an AC_ macro is
191 # defined in (eg) acinclude.m4, then we want to make sure we mark
192 # it as seen.
193 return if $macro =~ /^AC_/ && ! defined $map{$macro};
194
195 if (! defined $map{$macro})
196 {
197 warn "aclocal: macro `$macro' required but not defined\n";
198 $exit_code = 1;
199 return;
200 }
201
202 print STDERR "aclocal: saw macro $macro\n" if $verbose;
203 $macro_seen{$macro} = 1;
204 &add_file ($map{$macro});
205}
206
207# rel2abs ($file, $directory)
208# ---------------------------
209# Similar to File::Spec->rel2abs ($file, $directory), but
210# work with Perl 5.005. (File::Spec->rel2abs is available
211# only in Perl 5.6.)
212# Remove this once we require 5.6.
213sub rel2abs ($$)
214{
215 my ($file, $dir) = @_;
216 if (! File::Spec->file_name_is_absolute ($file))
217 {
218 $dir = cwd () . "/$dir"
219 unless File::Spec->file_name_is_absolute ($dir);
220 $file = "$dir/$file";
221 }
222 $file = File::Spec->canonpath ($file);
223 return $file;
224}
225
226# scan_configure_dep ($file)
227# --------------------------
228# Scan a configure dependency (configure.ac, or separate m4 files)
229# for uses of know macros and AC_REQUIREs of possibly unknown macros.
230# Recursively scan m4_included files.
231my %scanned_configure_dep = ();
232sub scan_configure_dep ($)
233{
234 my ($file) = @_;
235 # Do not scan a file twice.
236 return ()
237 if exists $scanned_configure_dep{$file};
238 $scanned_configure_dep{$file} = 1;
239
240 my $mtime = mtime $file;
241 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
242
243 my $contents = exists $file_contents{$file} ?
244 $file_contents{$file} : contents $file;
245
246 my $line = 0;
247 my @rlist = ();
248 my @ilist = ();
249 foreach (split ("\n", $contents))
250 {
251 ++$line;
252 # Remove comments from current line.
253 s/\bdnl\b.*$//;
254 s/\#.*$//;
255
256 while (/$m4_include_rx/go)
257 {
258 push (@ilist, $1 || $2);
259 }
260
261 while (/$ac_require_rx/go)
262 {
263 push (@rlist, $1 || $2);
264 }
265
266 # The search function is constructed dynamically by
267 # scan_m4_files. The last parenthetical match makes sure we
268 # don't match things that look like macro assignments or
269 # AC_SUBSTs.
270 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
271 {
272 # Macro not found, but AM_ prefix found.
273 warn "aclocal: $file: $line: macro `$2' not found in library\n";
274 $exit_code = 1;
275 }
276 }
277
278 add_macro ($_) foreach (@rlist);
279 my $dirname = dirname $file;
280 &scan_configure_dep (rel2abs ($_, $dirname)) foreach (@ilist);
281}
282
283# Add a file to output.
284sub add_file ($)
285{
286 local ($file) = @_;
287
288 # Only add a file once.
289 return if ($file_seen{$file});
290 $file_seen{$file} = 1;
291
292 scan_configure_dep $file;
293}
294
295# Point to the documentation for underquoted AC_DEFUN only once.
296my $underquoted_manual_once = 0;
297
298# Scan a single M4 file, and all files it includes.
299# Return the list of included files.
300sub scan_file ($)
301{
302 my ($file) = @_;
303 my $base = dirname $file;
304
305 # Do not scan the same file twice.
306 return @$file_includes{$file} if exists $file_includes{$file};
307 # Prevent potential infinite recursion (if two files include each other).
308 return () if exists $file_contents{$file};
309
310 unshift @file_order, $file;
311
312 my $fh = new Automake::XFile $file;
313 my $contents = '';
314 my @inc_files = ();
315 while ($_ = $fh->getline)
316 {
317 # Ignore `##' lines.
318 next if /^##/;
319
320 $contents .= $_;
321
322 while (/$ac_defun_rx/go)
323 {
324 if (! defined $1)
325 {
326 print STDERR "$file:$.: warning: underquoted definition of $2\n";
327 print STDERR " run info '(automake)Extending aclocal'\n"
328 . " or see http://sources.redhat.com/automake/"
329 . "automake.html#Extending%20aclocal\n"
330 unless $underquoted_manual_once;
331 $underquoted_manual_once = 1;
332 }
333 my $macro = $1 || $2;
334 if (! defined $map{$macro})
335 {
336 print STDERR "aclocal: found macro $macro in $file: $.\n"
337 if $verbose;
338 $map{$macro} = $file;
339 }
340 else
341 {
342 # Note: we used to give an error here if we saw a
343 # duplicated macro. However, this turns out to be
344 # extremely unpopular. It causes actual problems which
345 # are hard to work around, especially when you must
346 # mix-and-match tool versions.
347 print STDERR "aclocal: ignoring macro $macro in $file: $.\n"
348 if $verbose;
349 }
350 }
351
352 while (/$m4_include_rx/go)
353 {
354 my $ifile = $1 || $2;
355 # m4_include is relative to the directory of the file which
356 # perform the include, but we want paths relative to the
357 # directory where aclocal is run. Do not use
358 # File::Spec->rel2abs, because we want to store relative
359 # paths (they might be used later of aclocal outputs an
360 # m4_include for this file, or if the user itself includes
361 # this file).
362 $ifile = "$base/$ifile"
363 unless $base eq '.' || File::Spec->file_name_is_absolute ($ifile);
364 push (@inc_files, $ifile);
365 }
366 }
367 $file_contents{$file} = $contents;
368
369 # For some reason I don't understand, it does not work
370 # to do `map { scan_file ($_) } @inc_files' below.
371 # With Perl 5.8.2 it undefines @inc_files.
372 my @copy = @inc_files;
373 my @all_inc_files = (@inc_files, map { scan_file ($_) } @copy);
374 $file_includes{$file} = \@all_inc_files;
375 return @all_inc_files;
376}
377
378# strip_redundant_includes (%FILES)
379# ---------------------------------
380# Each key in %FILES is a file that must be present in the output.
381# However some of these files might already include other files in %FILES,
382# so there is no point in including them another time.
383# This removes items of %FILES which are already included by another file.
384sub strip_redundant_includes (%)
385{
386 my %files = @_;
387 # Files at the end of @file_order should override those at the beginning,
388 # so it is important to preserve these trailing files. We can remove
389 # a file A if it is going to be output before a file B that includes
390 # file A, not the converse.
391 foreach my $file (reverse @file_order)
392 {
393 next unless exists $files{$file};
394 foreach my $ifile (@{$file_includes{$file}})
395 {
396 next unless exists $files{$ifile};
397 delete $files{$ifile};
398 print STDERR "$ifile is already included by $file\n"
399 if $verbose;
400 }
401 }
402 return %files;
403}
404
405sub trace_used_macros ()
406{
407 my %files = map { $map{$_} => 1 } keys %macro_seen;
408 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
409 %files = strip_redundant_includes %files;
410 # configure.ac is implicitly included.
411 delete $files{$configure_ac};
412
413 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
414 $traces .= " --language Autoconf-without-aclocal-m4 ";
415 # All candidate files.
416 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
417 # All candidate macros.
418 $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
419
420 print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
421
422 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
423
424 my %traced = ();
425
426 while ($_ = $tracefh->getline)
427 {
428 chomp;
429 $traced{$_} = 1 if $macro_seen{$_};
430 }
431
432 $tracefh->close;
433
434 return %traced;
435}
436
437sub scan_configure ()
438{
439 # Make sure we include acinclude.m4 if it exists.
440 if (-f 'acinclude.m4')
441 {
442 add_file ('acinclude.m4');
443 }
444 scan_configure_dep ($configure_ac);
445}
446
447################################################################
448
449# Write output.
450sub write_aclocal ($@)
451{
452 my ($output_file, @macros) = @_;
453 my $output = '';
454
455 my %files = map { $map{$_} => 1 } @macros;
456 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
457 %files = strip_redundant_includes %files;
458 delete $files{$configure_ac};
459
460 for $file (grep { exists $files{$_} } @file_order)
461 {
462 # Check the time stamp of this file, and all files it includes.
463 for my $ifile ($file, @{$file_includes{$file}})
464 {
465 my $mtime = mtime $ifile;
466 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
467 }
468
469 # If the file to add looks like outside the project, copy it
470 # to the output. The regex catches filenames starting with
471 # things like `/', `\', or `c:\'.
472 if ($file =~ m,^(?:\w:)?[\\/],)
473 {
474 $output .= $file_contents{$file} . "\n";
475 }
476 else
477 {
478 # Otherwise, simply include the file.
479 $output .= "m4_include([$file])\n";
480 }
481 }
482
483 # Nothing to output?!
484 # FIXME: Shouldn't we diagnose this?
485 return if ! length ($output);
486
487 # We used to print `# $output_file generated automatically etc.' But
488 # this creates spurious differences when using autoreconf. Autoreconf
489 # creates aclocal.m4t and then rename it to aclocal.m4, but the
490 # rebuild rules generated by Automake create aclocal.m4 directly --
491 # this would gives two ways to get the same file, with a different
492 # name in the header.
493 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
494
495# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
496# Free Software Foundation, Inc.
497# This file is free software; the Free Software Foundation
498# gives unlimited permission to copy and/or distribute it,
499# with or without modifications, as long as this notice is preserved.
500
501# This program is distributed in the hope that it will be useful,
502# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
503# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
504# PARTICULAR PURPOSE.
505
506$output";
507
508 # We try not to update $output_file unless necessary, because
509 # doing so invalidate Autom4te's cache and therefore slows down
510 # tools called after aclocal.
511 #
512 # We need to overwrite $output_file in the following situations.
513 # * The --force option is in use.
514 # * One of the dependencies is younger.
515 # (Not updating $output_file in this situation would cause
516 # make to call aclocal in loop.)
517 # * The contents of the current file are different from what
518 # we have computed.
519 if (!$force_output
520 && $greatest_mtime < mtime ($output_file)
521 && $output eq contents ($output_file))
522 {
523 print STDERR "aclocal: $output_file unchanged\n" if $verbose;
524 return;
525 }
526
527 print STDERR "aclocal: writing $output_file\n" if $verbose;
528
529 my $out = new Automake::XFile "> $output_file";
530 print $out $output;
531 return;
532}
533
534################################################################
535
536# Print usage and exit.
537sub usage ($)
538{
539 local ($status) = @_;
540
541 print "Usage: aclocal [OPTIONS] ...\n\n";
542 print "\
543Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
544
545 --acdir=DIR directory holding config files
546 --help print this help, then exit
547 -I DIR add directory to search list for .m4 files
548 --force always update output file
549 --output=FILE put output in FILE (default aclocal.m4)
550 --print-ac-dir print name of directory holding m4 files
551 --verbose don't be silent
552 --version print version number, then exit
553
554Report bugs to <bug-automake\@gnu.org>.\n";
555
556 exit $status;
557}
558
559# Parse command line.
560sub parse_arguments (@)
561{
562 local (@arglist) = @_;
563 local (@dirlist);
564 local ($print_and_exit) = 0;
565
566 while (@arglist)
567 {
568 if ($arglist[0] =~ /^--acdir=(.+)$/)
569 {
570 $acdir = $1;
571 }
572 elsif ($arglist[0] =~/^--output=(.+)$/)
573 {
574 $output_file = $1;
575 }
576 elsif ($arglist[0] eq '-I')
577 {
578 shift (@arglist);
579 push (@dirlist, $arglist[0]);
580 }
581 elsif ($arglist[0] eq '--print-ac-dir')
582 {
583 $print_and_exit = 1;
584 }
585 elsif ($arglist[0] eq '--force')
586 {
587 $force_output = 1;
588 }
589 elsif ($arglist[0] eq '--verbose')
590 {
591 ++$verbose;
592 }
593 elsif ($arglist[0] eq '--version')
594 {
595 print "aclocal (GNU $PACKAGE) $VERSION\n";
596 print "Written by Tom Tromey <tromey\@redhat.com>\n\n";
597 print "Copyright (C) 2004 Free Software Foundation, Inc.\n";
598 print "This is free software; see the source for copying conditions. There is NO\n";
599 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
600 exit 0;
601 }
602 elsif ($arglist[0] eq '--help')
603 {
604 &usage (0);
605 }
606 else
607 {
608 print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
609 exit 1;
610 }
611
612 shift (@arglist);
613 }
614
615 if ($print_and_exit)
616 {
617 print $acdir, "\n";
618 exit 0;
619 }
620
621 $default_dirlist="$acdir/dirlist"
622 if $acdir ne $default_acdir;
623
624 # Search the versioned directory near the end, and then the
625 # unversioned directory last. Only do this if the user didn't
626 # override acdir.
627 push (@dirlist, "$acdir-$APIVERSION")
628 if $acdir eq $default_acdir;
629
630 # By default $(datadir)/aclocal doesn't exist. We don't want to
631 # get an error in the case where we are searching the default
632 # directory and it hasn't been created.
633 push (@dirlist, $acdir)
634 unless $acdir eq $default_acdir && ! -d $acdir;
635
636 # Finally, adds any directory listed in the `dirlist' file.
637 if (open (DEFAULT_DIRLIST, $default_dirlist))
638 {
639 while (<DEFAULT_DIRLIST>)
640 {
641 # Ignore '#' lines.
642 next if /^#/;
643 # strip off newlines and end-of-line comments
644 s/\s*\#.*$//;
645 chomp ($contents=$_);
646 if (-d $contents )
647 {
648 push (@dirlist, $contents);
649 }
650 }
651 close (DEFAULT_DIRLIST);
652 }
653
654 return @dirlist;
655}
656
657################################################################
658
659local (@dirlist) = parse_arguments (@ARGV);
660$configure_ac = require_configure_ac;
661scan_m4_files (@dirlist);
662scan_configure;
663if (! $exit_code)
664 {
665 my %macro_traced = trace_used_macros;
666 write_aclocal ($output_file, keys %macro_traced);
667 }
668check_acinclude;
669
670exit $exit_code;
671
672### Setup "GNU" style for perl-mode and cperl-mode.
673## Local Variables:
674## perl-indent-level: 2
675## perl-continued-statement-offset: 2
676## perl-continued-brace-offset: 0
677## perl-brace-offset: 0
678## perl-brace-imaginary-offset: 0
679## perl-label-offset: -2
680## cperl-indent-level: 2
681## cperl-brace-offset: 0
682## cperl-continued-brace-offset: 0
683## cperl-label-offset: -2
684## cperl-extra-newline-before-brace: t
685## cperl-merge-trailing-else: nil
686## cperl-continued-statement-offset: 2
687## End:
Note: See TracBrowser for help on using the repository browser.