5 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
8 # aclocal - create aclocal.m4 by scanning configure.ac
10 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
11 # Free Software Foundation, Inc.
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)
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.
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
28 # Written by Tom Tromey <tromey@redhat.com>.
32 my $perllibdir = $ENV{'perllibdir'} || '@datadir@/@PACKAGE@-@APIVERSION@';
33 unshift @INC, (split ':', $perllibdir);
37 use Automake::General;
38 use Automake::Configure_ac;
39 use Automake::Channels;
41 use Automake::FileUtils;
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";
57 # configure.ac or configure.in.
61 $output_file = 'aclocal.m4';
63 # Modification time of the youngest dependency.
69 # Which macros have been seen.
72 # Which files have been seen.
75 # Remember the order into which we scanned the files.
76 # It's important to output the contents of aclocal.m4 in the opposite order.
77 # (Definitions in first files we have scanned should override those from
78 # later files. So they must appear last in the output.)
81 # Map macro names to file names.
84 # Map file names to file contents.
87 # Map file names to included files (transitively closed).
93 # Matches a macro definition.
94 # AC_DEFUN([macroname], ...)
96 # AC_DEFUN(macroname, ...)
97 # When macroname is `['-quoted , we accept any character in the name,
98 # except `]'. Otherwise macroname stops on the first `]', `,', `)',
99 # or `\n' encountered.
100 $ac_defun_rx = "A[CU]_DEFUN\\((?:\\[([^]]+)\\]|([^],)\n]+))";
102 # Matches an AC_REQUIRE line.
103 $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
105 # Matches an m4_include line
106 $m4_include_rx = "(?:m4_)?s?include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
109 ################################################################
111 # Check macros in acinclude.m4. If one is not used, warn.
112 sub check_acinclude ()
114 foreach my $key (keys %map)
116 # FIXME: should print line number of acinclude.m4.
117 warn ("aclocal: warning: macro `$key' defined in "
118 . "acinclude.m4 but never used\n")
119 if $map{$key} eq 'acinclude.m4' && ! $macro_seen{$key};
123 ################################################################
125 # Scan all the installed m4 files and construct a map.
126 sub scan_m4_files (@)
128 local (@dirlist) = @_;
130 # First, scan acinclude.m4 if it exists.
131 if (-f 'acinclude.m4')
133 &scan_file ('acinclude.m4');
137 foreach $m4dir (@dirlist)
139 if (! opendir (DIR, $m4dir))
141 print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
145 local ($file, $fullfile);
146 # We reverse the directory contents so that foo2.m4 gets
147 # used in preference to foo1.m4.
148 foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
150 # Only examine .m4 files.
151 next unless $file =~ /\.m4$/;
153 # Skip some files when running out of srcdir.
154 next if $file eq 'aclocal.m4';
156 $fullfile = $m4dir . '/' . $file;
157 &scan_file ($fullfile);
162 # Construct a new function that does the searching. We use a
163 # function (instead of just evaluating $search in the loop) so that
164 # "die" is correctly and easily propagated if run.
165 my $search = "sub search {\nmy \$found = 0;\n";
166 foreach my $key (reverse sort keys %map)
168 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
169 . '"); $found = 1; }' . "\n");
171 $search .= "return \$found;\n};\n";
173 die "internal error: $@\n search is $search" if $@;
176 ################################################################
178 # Add a macro to the output.
183 # We want to ignore AC_ macros. However, if an AC_ macro is
184 # defined in (eg) acinclude.m4, then we want to make sure we mark
186 return if $macro =~ /^AC_/ && ! defined $map{$macro};
188 if (! defined $map{$macro})
190 warn "aclocal: macro `$macro' required but not defined\n";
195 print STDERR "aclocal: saw macro $macro\n" if $verbose;
196 $macro_seen{$macro} = 1;
197 &add_file ($map{$macro});
200 # scan_contents ($file)
201 # --------------------------------
202 my %scanned_configure_dep = ();
203 sub scan_configure_dep ($)
206 # Do not scan a file twice.
208 if exists $scanned_configure_dep{$file};
209 $scanned_configure_dep{$file} = 1;
211 my $mtime = mtime $file;
212 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
214 my $contents = exists $file_contents{$file} ?
215 $file_contents{$file} : contents $file;
220 foreach (split ("\n", $contents))
223 # Remove comments from current line.
227 while (/$m4_include_rx/go)
229 push (@ilist, $1 || $2);
232 while (/$ac_require_rx/go)
234 push (@rlist, $1 || $2);
237 # The search function is constructed dynamically by
238 # scan_m4_files. The last parenthetical match makes sure we
239 # don't match things that look like macro assignments or
241 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
243 # Macro not found, but AM_ prefix found.
244 warn "aclocal: $file: $line: macro `$2' not found in library\n";
249 add_macro ($_) foreach (@rlist);
250 my $dirname = dirname $file;
251 &scan_configure_dep (File::Spec->rel2abs ($_, $dirname)) foreach (@ilist);
254 # Add a file to output.
259 # Only add a file once.
260 return if ($file_seen{$file});
261 $file_seen{$file} = 1;
263 scan_configure_dep $file;
266 # Point to the documentation for underquoted AC_DEFUN only once.
267 my $underquoted_manual_once = 0;
269 # Scan a single M4 file, and all files it includes.
270 # Return the list of included files.
274 my $base = dirname $file;
276 # Do not scan the same file twice.
277 return @$file_includes{$file} if exists $file_includes{$file};
278 # Prevent potential infinite recursion (if two files include each other).
279 return () if exists $file_contents{$file};
281 unshift @file_order, $file;
283 my $fh = new Automake::XFile $file;
286 while ($_ = $fh->getline)
293 while (/$ac_defun_rx/go)
297 print STDERR "$file:$.: warning: underquoted definition of $2\n";
298 print STDERR " run info '(automake)Extending aclocal'\n"
299 . " or see http://sources.redhat.com/automake/"
300 . "automake.html#Extending%20aclocal\n"
301 unless $underquoted_manual_once;
302 $underquoted_manual_once = 1;
304 my $macro = $1 || $2;
305 if (! defined $map{$macro})
307 print STDERR "aclocal: found macro $macro in $file: $.\n"
309 $map{$macro} = $file;
313 # Note: we used to give an error here if we saw a
314 # duplicated macro. However, this turns out to be
315 # extremely unpopular. It causes actual problems which
316 # are hard to work around, especially when you must
317 # mix-and-match tool versions.
318 print STDERR "aclocal: ignoring macro $macro in $file: $.\n"
323 while (/$m4_include_rx/go)
325 my $ifile = $1 || $2;
326 # m4_include is relative to the directory of the file which
327 # perform the include, but we want paths relative to the
328 # directory where aclocal is run. Do not use
329 # File::Spec->rel2abs, because we want to store relative
330 # paths (they might be used later of aclocal outputs an
331 # m4_include for this file, or if the user itself includes
333 $ifile = "$base/$ifile"
334 unless $base eq '.' || File::Spec->file_name_is_absolute ($ifile);
335 push (@inc_files, $ifile);
338 $file_contents{$file} = $contents;
340 # For some reason I don't understand, it does not work
341 # to do `map { scan_file ($_) } @inc_files' below.
342 # With Perl 5.8.2 it undefines @inc_files.
343 my @copy = @inc_files;
344 my @all_inc_files = (@inc_files, map { scan_file ($_) } @copy);
345 $file_includes{$file} = \@all_inc_files;
346 return @all_inc_files;
349 # strip_redundant_includes (%FILES)
350 # ---------------------------------
351 # Each key in %FILES is a file that must be present in the output.
352 # However some of these files might already include other files in %FILES,
353 # so there is no point in including them another time.
354 # This removes items of %FILES which are already included by another file.
355 sub strip_redundant_includes (%)
358 # Files at the end of @file_order should override those at the beginning,
359 # so it is important to preserve these trailing files. We can remove
360 # a file A if it is going to be output before a file B that includes
361 # file A, not the converse.
362 foreach my $file (reverse @file_order)
364 next unless exists $files{$file};
365 foreach my $ifile (@{$file_includes{$file}})
367 next unless exists $files{$ifile};
368 delete $files{$ifile};
369 print STDERR "$ifile is already included by $file\n"
376 sub trace_used_macros ()
378 my %files = map { $map{$_} => 1 } keys %macro_seen;
379 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
380 %files = strip_redundant_includes %files;
382 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
383 $traces .= " --language Autoconf-without-aclocal-m4 ";
384 # All candidate files.
385 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
386 # All candidate macros.
387 $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
389 print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
391 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
395 while ($_ = $tracefh->getline)
398 $traced{$_} = 1 if $macro_seen{$_};
406 sub scan_configure ()
408 # Make sure we include acinclude.m4 if it exists.
409 if (-f 'acinclude.m4')
411 add_file ('acinclude.m4');
413 scan_configure_dep ($configure_ac);
416 ################################################################
419 sub write_aclocal ($@)
421 my ($output_file, @macros) = @_;
424 my %files = map { $map{$_} => 1 } @macros;
425 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
426 %files = strip_redundant_includes %files;
428 for $file (grep { exists $files{$_} } @file_order)
430 # Check the time stamp of this file, and all files it includes.
431 for my $ifile ($file, @{$file_includes{$file}})
433 my $mtime = mtime $ifile;
434 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
437 # If the file to add looks like outside the project, copy it
438 # to the output. The regex catches filenames starting with
439 # things like `/', `\', or `c:\'.
440 if ($file =~ m,^(?:\w:)?[\\/],)
442 $output .= $file_contents{$file} . "\n";
446 # Otherwise, simply include the file.
447 $output .= "m4_include([$file])\n";
451 # Nothing to output?!
452 # FIXME: Shouldn't we diagnose this?
453 return if ! length ($output);
455 # We used to print `# $output_file generated automatically etc.' But
456 # this creates spurious differences when using autoreconf. Autoreconf
457 # creates aclocal.m4t and then rename it to aclocal.m4, but the
458 # rebuild rules generated by Automake create aclocal.m4 directly --
459 # this would gives two ways to get the same file, with a different
460 # name in the header.
461 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
463 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
464 # Free Software Foundation, Inc.
465 # This file is free software; the Free Software Foundation
466 # gives unlimited permission to copy and/or distribute it,
467 # with or without modifications, as long as this notice is preserved.
469 # This program is distributed in the hope that it will be useful,
470 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
471 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
472 # PARTICULAR PURPOSE.
476 # We try not to update $output_file unless necessary, because
477 # doing so invalidate Autom4te's cache and therefore slows down
478 # tools called after aclocal.
480 # We need to overwrite $output_file in the following situations.
481 # * The --force option is in use.
482 # * One of the dependencies is younger.
483 # (Not updating $output_file in this situation would cause
484 # make to call aclocal in loop.)
485 # * The contents of the current file are different from what
488 && $greatest_mtime < mtime ($output_file)
489 && $output eq contents ($output_file))
491 print STDERR "aclocal: $output_file unchanged\n" if $verbose;
495 print STDERR "aclocal: writing $output_file\n" if $verbose;
497 my $out = new Automake::XFile "> $output_file";
502 ################################################################
504 # Print usage and exit.
507 local ($status) = @_;
509 print "Usage: aclocal [OPTIONS] ...\n\n";
511 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
513 --acdir=DIR directory holding config files
514 --help print this help, then exit
515 -I DIR add directory to search list for .m4 files
516 --force always update output file
517 --output=FILE put output in FILE (default aclocal.m4)
518 --print-ac-dir print name of directory holding m4 files
519 --verbose don't be silent
520 --version print version number, then exit
522 Report bugs to <bug-automake\@gnu.org>.\n";
527 # Parse command line.
528 sub parse_arguments (@)
530 local (@arglist) = @_;
532 local ($print_and_exit) = 0;
536 if ($arglist[0] =~ /^--acdir=(.+)$/)
540 elsif ($arglist[0] =~/^--output=(.+)$/)
544 elsif ($arglist[0] eq '-I')
547 push (@dirlist, $arglist[0]);
549 elsif ($arglist[0] eq '--print-ac-dir')
553 elsif ($arglist[0] eq '--force')
557 elsif ($arglist[0] eq '--verbose')
561 elsif ($arglist[0] eq '--version')
563 print "aclocal (GNU $PACKAGE) $VERSION\n";
564 print "Written by Tom Tromey <tromey\@redhat.com>\n\n";
565 print "Copyright (C) 2004 Free Software Foundation, Inc.\n";
566 print "This is free software; see the source for copying conditions. There is NO\n";
567 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
570 elsif ($arglist[0] eq '--help')
576 print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
589 $default_dirlist="$acdir/dirlist"
590 if $acdir ne $default_acdir;
592 # Search the versioned directory near the end, and then the
593 # unversioned directory last. Only do this if the user didn't
595 push (@dirlist, "$acdir-$APIVERSION")
596 if $acdir eq $default_acdir;
598 # By default $(datadir)/aclocal doesn't exist. We don't want to
599 # get an error in the case where we are searching the default
600 # directory and it hasn't been created.
601 push (@dirlist, $acdir)
602 unless $acdir eq $default_acdir && ! -d $acdir;
604 # Finally, adds any directory listed in the `dirlist' file.
605 if (open (DEFAULT_DIRLIST, $default_dirlist))
607 while (<DEFAULT_DIRLIST>)
611 # strip off newlines and end-of-line comments
613 chomp ($contents=$_);
616 push (@dirlist, $contents);
619 close (DEFAULT_DIRLIST);
625 ################################################################
627 local (@dirlist) = parse_arguments (@ARGV);
628 $configure_ac = require_configure_ac;
629 scan_m4_files (@dirlist);
633 my %macro_traced = trace_used_macros;
634 write_aclocal ($output_file, keys %macro_traced);
640 ### Setup "GNU" style for perl-mode and cperl-mode.
642 ## perl-indent-level: 2
643 ## perl-continued-statement-offset: 2
644 ## perl-continued-brace-offset: 0
645 ## perl-brace-offset: 0
646 ## perl-brace-imaginary-offset: 0
647 ## perl-label-offset: -2
648 ## cperl-indent-level: 2
649 ## cperl-brace-offset: 0
650 ## cperl-continued-brace-offset: 0
651 ## cperl-label-offset: -2
652 ## cperl-extra-newline-before-brace: t
653 ## cperl-merge-trailing-else: nil
654 ## cperl-continued-statement-offset: 2