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
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 configure.ac. It may contain macro definitions,
131 # or may include other files that define macros.
132 &scan_file ($configure_ac);
134 # Then, scan acinclude.m4 if it exists.
135 if (-f 'acinclude.m4')
137 &scan_file ('acinclude.m4');
140 # Finally, scan all files in our search path.
142 foreach $m4dir (@dirlist)
144 if (! opendir (DIR, $m4dir))
146 print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
150 local ($file, $fullfile);
151 # We reverse the directory contents so that foo2.m4 gets
152 # used in preference to foo1.m4.
153 foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
155 # Only examine .m4 files.
156 next unless $file =~ /\.m4$/;
158 # Skip some files when running out of srcdir.
159 next if $file eq 'aclocal.m4';
161 $fullfile = $m4dir . '/' . $file;
162 &scan_file ($fullfile);
167 # Construct a new function that does the searching. We use a
168 # function (instead of just evaluating $search in the loop) so that
169 # "die" is correctly and easily propagated if run.
170 my $search = "sub search {\nmy \$found = 0;\n";
171 foreach my $key (reverse sort keys %map)
173 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
174 . '"); $found = 1; }' . "\n");
176 $search .= "return \$found;\n};\n";
178 die "internal error: $@\n search is $search" if $@;
181 ################################################################
183 # Add a macro to the output.
188 # We want to ignore AC_ macros. However, if an AC_ macro is
189 # defined in (eg) acinclude.m4, then we want to make sure we mark
191 return if $macro =~ /^AC_/ && ! defined $map{$macro};
193 if (! defined $map{$macro})
195 warn "aclocal: macro `$macro' required but not defined\n";
200 print STDERR "aclocal: saw macro $macro\n" if $verbose;
201 $macro_seen{$macro} = 1;
202 &add_file ($map{$macro});
205 # scan_contents ($file)
206 # --------------------------------
207 my %scanned_configure_dep = ();
208 sub scan_configure_dep ($)
211 # Do not scan a file twice.
213 if exists $scanned_configure_dep{$file};
214 $scanned_configure_dep{$file} = 1;
216 my $mtime = mtime $file;
217 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
219 my $contents = exists $file_contents{$file} ?
220 $file_contents{$file} : contents $file;
225 foreach (split ("\n", $contents))
228 # Remove comments from current line.
232 while (/$m4_include_rx/go)
234 push (@ilist, $1 || $2);
237 while (/$ac_require_rx/go)
239 push (@rlist, $1 || $2);
242 # The search function is constructed dynamically by
243 # scan_m4_files. The last parenthetical match makes sure we
244 # don't match things that look like macro assignments or
246 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
248 # Macro not found, but AM_ prefix found.
249 warn "aclocal: $file: $line: macro `$2' not found in library\n";
254 add_macro ($_) foreach (@rlist);
255 my $dirname = dirname $file;
256 &scan_configure_dep (File::Spec->rel2abs ($_, $dirname)) foreach (@ilist);
259 # Add a file to output.
264 # Only add a file once.
265 return if ($file_seen{$file});
266 $file_seen{$file} = 1;
268 scan_configure_dep $file;
271 # Point to the documentation for underquoted AC_DEFUN only once.
272 my $underquoted_manual_once = 0;
274 # Scan a single M4 file, and all files it includes.
275 # Return the list of included files.
279 my $base = dirname $file;
281 # Do not scan the same file twice.
282 return @$file_includes{$file} if exists $file_includes{$file};
283 # Prevent potential infinite recursion (if two files include each other).
284 return () if exists $file_contents{$file};
286 unshift @file_order, $file;
288 my $fh = new Automake::XFile $file;
291 while ($_ = $fh->getline)
298 while (/$ac_defun_rx/go)
302 print STDERR "$file:$.: warning: underquoted definition of $2\n";
303 print STDERR " run info '(automake)Extending aclocal'\n"
304 . " or see http://sources.redhat.com/automake/"
305 . "automake.html#Extending%20aclocal\n"
306 unless $underquoted_manual_once;
307 $underquoted_manual_once = 1;
309 my $macro = $1 || $2;
310 if (! defined $map{$macro})
312 print STDERR "aclocal: found macro $macro in $file: $.\n"
314 $map{$macro} = $file;
318 # Note: we used to give an error here if we saw a
319 # duplicated macro. However, this turns out to be
320 # extremely unpopular. It causes actual problems which
321 # are hard to work around, especially when you must
322 # mix-and-match tool versions.
323 print STDERR "aclocal: ignoring macro $macro in $file: $.\n"
328 while (/$m4_include_rx/go)
330 my $ifile = $1 || $2;
331 # m4_include is relative to the directory of the file which
332 # perform the include, but we want paths relative to the
333 # directory where aclocal is run. Do not use
334 # File::Spec->rel2abs, because we want to store relative
335 # paths (they might be used later of aclocal outputs an
336 # m4_include for this file, or if the user itself includes
338 $ifile = "$base/$ifile"
339 unless $base eq '.' || File::Spec->file_name_is_absolute ($ifile);
340 push (@inc_files, $ifile);
343 $file_contents{$file} = $contents;
345 # For some reason I don't understand, it does not work
346 # to do `map { scan_file ($_) } @inc_files' below.
347 # With Perl 5.8.2 it undefines @inc_files.
348 my @copy = @inc_files;
349 my @all_inc_files = (@inc_files, map { scan_file ($_) } @copy);
350 $file_includes{$file} = \@all_inc_files;
351 return @all_inc_files;
354 # strip_redundant_includes (%FILES)
355 # ---------------------------------
356 # Each key in %FILES is a file that must be present in the output.
357 # However some of these files might already include other files in %FILES,
358 # so there is no point in including them another time.
359 # This removes items of %FILES which are already included by another file.
360 sub strip_redundant_includes (%)
363 # Files at the end of @file_order should override those at the beginning,
364 # so it is important to preserve these trailing files. We can remove
365 # a file A if it is going to be output before a file B that includes
366 # file A, not the converse.
367 foreach my $file (reverse @file_order)
369 next unless exists $files{$file};
370 foreach my $ifile (@{$file_includes{$file}})
372 next unless exists $files{$ifile};
373 delete $files{$ifile};
374 print STDERR "$ifile is already included by $file\n"
381 sub trace_used_macros ()
383 my %files = map { $map{$_} => 1 } keys %macro_seen;
384 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
385 %files = strip_redundant_includes %files;
386 # configure.ac is implicitly included.
387 delete $files{$configure_ac};
389 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
390 $traces .= " --language Autoconf-without-aclocal-m4 ";
391 # All candidate files.
392 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
393 # All candidate macros.
394 $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
396 print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
398 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
402 while ($_ = $tracefh->getline)
405 $traced{$_} = 1 if $macro_seen{$_};
413 sub scan_configure ()
415 # Make sure we include acinclude.m4 if it exists.
416 if (-f 'acinclude.m4')
418 add_file ('acinclude.m4');
420 scan_configure_dep ($configure_ac);
423 ################################################################
426 sub write_aclocal ($@)
428 my ($output_file, @macros) = @_;
431 my %files = map { $map{$_} => 1 } @macros;
432 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
433 %files = strip_redundant_includes %files;
434 delete $files{$configure_ac};
436 for $file (grep { exists $files{$_} } @file_order)
438 # Check the time stamp of this file, and all files it includes.
439 for my $ifile ($file, @{$file_includes{$file}})
441 my $mtime = mtime $ifile;
442 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
445 # If the file to add looks like outside the project, copy it
446 # to the output. The regex catches filenames starting with
447 # things like `/', `\', or `c:\'.
448 if ($file =~ m,^(?:\w:)?[\\/],)
450 $output .= $file_contents{$file} . "\n";
454 # Otherwise, simply include the file.
455 $output .= "m4_include([$file])\n";
459 # Nothing to output?!
460 # FIXME: Shouldn't we diagnose this?
461 return if ! length ($output);
463 # We used to print `# $output_file generated automatically etc.' But
464 # this creates spurious differences when using autoreconf. Autoreconf
465 # creates aclocal.m4t and then rename it to aclocal.m4, but the
466 # rebuild rules generated by Automake create aclocal.m4 directly --
467 # this would gives two ways to get the same file, with a different
468 # name in the header.
469 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
471 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
472 # Free Software Foundation, Inc.
473 # This file is free software; the Free Software Foundation
474 # gives unlimited permission to copy and/or distribute it,
475 # with or without modifications, as long as this notice is preserved.
477 # This program is distributed in the hope that it will be useful,
478 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
479 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
480 # PARTICULAR PURPOSE.
484 # We try not to update $output_file unless necessary, because
485 # doing so invalidate Autom4te's cache and therefore slows down
486 # tools called after aclocal.
488 # We need to overwrite $output_file in the following situations.
489 # * The --force option is in use.
490 # * One of the dependencies is younger.
491 # (Not updating $output_file in this situation would cause
492 # make to call aclocal in loop.)
493 # * The contents of the current file are different from what
496 && $greatest_mtime < mtime ($output_file)
497 && $output eq contents ($output_file))
499 print STDERR "aclocal: $output_file unchanged\n" if $verbose;
503 print STDERR "aclocal: writing $output_file\n" if $verbose;
505 my $out = new Automake::XFile "> $output_file";
510 ################################################################
512 # Print usage and exit.
515 local ($status) = @_;
517 print "Usage: aclocal [OPTIONS] ...\n\n";
519 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
521 --acdir=DIR directory holding config files
522 --help print this help, then exit
523 -I DIR add directory to search list for .m4 files
524 --force always update output file
525 --output=FILE put output in FILE (default aclocal.m4)
526 --print-ac-dir print name of directory holding m4 files
527 --verbose don't be silent
528 --version print version number, then exit
530 Report bugs to <bug-automake\@gnu.org>.\n";
535 # Parse command line.
536 sub parse_arguments (@)
538 local (@arglist) = @_;
540 local ($print_and_exit) = 0;
544 if ($arglist[0] =~ /^--acdir=(.+)$/)
548 elsif ($arglist[0] =~/^--output=(.+)$/)
552 elsif ($arglist[0] eq '-I')
555 push (@dirlist, $arglist[0]);
557 elsif ($arglist[0] eq '--print-ac-dir')
561 elsif ($arglist[0] eq '--force')
565 elsif ($arglist[0] eq '--verbose')
569 elsif ($arglist[0] eq '--version')
571 print "aclocal (GNU $PACKAGE) $VERSION\n\n";
572 print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
573 print "This is free software; see the source for copying conditions. There is NO\n";
574 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
575 print "Written by Tom Tromey <tromey\@redhat.com>\n";
578 elsif ($arglist[0] eq '--help')
584 print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
597 $default_dirlist="$acdir/dirlist"
598 if $acdir ne $default_acdir;
600 # Search the versioned directory near the end, and then the
601 # unversioned directory last. Only do this if the user didn't
603 push (@dirlist, "$acdir-$APIVERSION")
604 if $acdir eq $default_acdir;
606 # By default $(datadir)/aclocal doesn't exist. We don't want to
607 # get an error in the case where we are searching the default
608 # directory and it hasn't been created.
609 push (@dirlist, $acdir)
610 unless $acdir eq $default_acdir && ! -d $acdir;
612 # Finally, adds any directory listed in the `dirlist' file.
613 if (open (DEFAULT_DIRLIST, $default_dirlist))
615 while (<DEFAULT_DIRLIST>)
619 # strip off newlines and end-of-line comments
621 chomp ($contents=$_);
624 push (@dirlist, $contents);
627 close (DEFAULT_DIRLIST);
633 ################################################################
635 local (@dirlist) = parse_arguments (@ARGV);
636 $configure_ac = require_configure_ac;
637 scan_m4_files (@dirlist);
641 my %macro_traced = trace_used_macros;
642 write_aclocal ($output_file, keys %macro_traced);
648 ### Setup "GNU" style for perl-mode and cperl-mode.
650 ## perl-indent-level: 2
651 ## perl-continued-statement-offset: 2
652 ## perl-continued-brace-offset: 0
653 ## perl-brace-offset: 0
654 ## perl-brace-imaginary-offset: 0
655 ## perl-label-offset: -2
656 ## cperl-indent-level: 2
657 ## cperl-brace-offset: 0
658 ## cperl-continued-brace-offset: 0
659 ## cperl-label-offset: -2
660 ## cperl-extra-newline-before-brace: t
661 ## cperl-merge-trailing-else: nil
662 ## cperl-continued-statement-offset: 2