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;
44 # Note that this isn't pkgdatadir, but a separate directory.
45 # Note also that the versioned directory is handled later.
46 $acdir = '@datadir@/aclocal';
47 $default_acdir = $acdir;
48 # contains a list of directories, one per line, to be added
49 # to the dirlist in addition to $acdir, as if -I had been
50 # added to the command line. If acdir has been redirected,
51 # we will also check the specified acdir (this is done later).
52 $default_dirlist = "$default_acdir/dirlist";
56 # configure.ac or configure.in.
57 my $configure_ac = require_configure_ac;
60 $output_file = 'aclocal.m4';
62 # Modification time of the youngest dependency.
68 # Which macros have been seen.
71 # Which files have been seen.
74 # Remember the order into which we scanned the files.
75 # It's important to output the contents of aclocal.m4 in the opposite order.
78 # Map macro names to file names.
81 # Map file names to file contents.
87 # Matches a macro definition.
88 # AC_DEFUN([macroname], ...)
90 # AC_DEFUN(macroname, ...)
91 # When macroname is `['-quoted , we accept any character in the name,
92 # except `]'. Otherwise macroname stops on the first `]', `,', `)',
93 # or `\n' encountered.
94 $ac_defun_rx = "A[CU]_DEFUN\\((?:\\[([^]]+)\\]|([^],)\n]+))";
96 # Matches an AC_REQUIRE line.
97 $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
101 local (@dirlist) = &parse_arguments (@ARGV);
102 &scan_m4_files (@dirlist);
106 my %macro_traced = &trace_used_macros;
107 &write_aclocal ($output_file, keys %macro_traced);
113 ################################################################
115 # Print usage and exit.
118 local ($status) = @_;
120 print "Usage: aclocal [OPTIONS] ...\n\n";
122 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
124 --acdir=DIR directory holding config files
125 --help print this help, then exit
126 -I DIR add directory to search list for .m4 files
127 --force always update output file
128 --output=FILE put output in FILE (default aclocal.m4)
129 --print-ac-dir print name of directory holding m4 files
130 --verbose don't be silent
131 --version print version number, then exit
133 Report bugs to <bug-automake\@gnu.org>.\n";
138 # Parse command line.
139 sub parse_arguments (@)
141 local (@arglist) = @_;
143 local ($print_and_exit) = 0;
147 if ($arglist[0] =~ /^--acdir=(.+)$/)
151 elsif ($arglist[0] =~/^--output=(.+)$/)
155 elsif ($arglist[0] eq '-I')
158 push (@dirlist, $arglist[0]);
160 elsif ($arglist[0] eq '--print-ac-dir')
164 elsif ($arglist[0] eq '--force')
168 elsif ($arglist[0] eq '--verbose')
172 elsif ($arglist[0] eq '--version')
174 print "aclocal (GNU $PACKAGE) $VERSION\n\n";
175 print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
176 print "This is free software; see the source for copying conditions. There is NO\n";
177 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
178 print "Written by Tom Tromey <tromey\@redhat.com>\n";
181 elsif ($arglist[0] eq '--help')
187 print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
200 $default_dirlist="$acdir/dirlist"
201 if $acdir ne $default_acdir;
203 # Search the versioned directory near the end, and then the
204 # unversioned directory last. Only do this if the user didn't
206 push (@dirlist, "$acdir-$APIVERSION")
207 if $acdir eq $default_acdir;
209 # By default $(datadir)/aclocal doesn't exist. We don't want to
210 # get an error in the case where we are searching the default
211 # directory and it hasn't been created.
212 push (@dirlist, $acdir)
213 unless $acdir eq $default_acdir && ! -d $acdir;
215 # Finally, adds any directory listed in the `dirlist' file.
216 if (open (DEFAULT_DIRLIST, $default_dirlist))
218 while (<DEFAULT_DIRLIST>)
222 # strip off newlines and end-of-line comments
224 chomp ($contents=$_);
227 push (@dirlist, $contents);
230 close (DEFAULT_DIRLIST);
236 ################################################################
238 sub scan_configure ()
240 if (! open (CONFIGURE, $configure_ac))
242 print STDERR "aclocal: couldn't open `$configure_ac': $!\n";
246 my $mtime = mtime $configure_ac;
247 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
249 # Make sure we include acinclude.m4 if it exists.
250 if (-f 'acinclude.m4')
252 &add_file ('acinclude.m4');
257 # Remove comments from current line.
261 # Search for things we know about. The "search" sub is
262 # constructed dynamically by scan_m4_files. The last
263 # parenthetical match makes sure we don't match things that
264 # look like macro assignments or AC_SUBSTs.
265 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
267 # Macro not found, but AM_ prefix found.
268 warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
276 ################################################################
278 # Check macros in acinclude.m4. If one is not used, warn.
279 sub check_acinclude ()
283 foreach $key (keys %map)
285 next unless $map{$key} eq 'acinclude.m4';
286 if (! $macro_seen{$key})
288 # FIXME: should print line number of acinclude.m4.
289 warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
294 ################################################################
296 # Scan all the installed m4 files and construct a map.
297 sub scan_m4_files (@)
299 local (@dirlist) = @_;
301 # First, scan acinclude.m4 if it exists.
302 if (-f 'acinclude.m4')
304 $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
308 foreach $m4dir (@dirlist)
310 if (! opendir (DIR, $m4dir))
312 print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
316 local ($file, $fullfile);
317 # We reverse the directory contents so that foo2.m4 gets
318 # used in preference to foo1.m4.
319 foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
321 # Only examine .m4 files.
322 next unless $file =~ /\.m4$/;
324 # Skip some files when running out of srcdir.
325 next if $file eq 'aclocal.m4';
327 $fullfile = $m4dir . '/' . $file;
328 $file_contents{$fullfile} = &scan_file ($fullfile);
333 # Construct a new function that does the searching. We use a
334 # function (instead of just evaluating $search in the loop) so that
335 # "die" is correctly and easily propagated if run.
336 my $search = "sub search {\nmy \$found = 0;\n";
337 foreach my $key (reverse sort keys %map)
339 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
340 . '"); $found = 1; }' . "\n");
342 $search .= "return \$found;\n};\n";
344 die "internal error: $@\n search is $search" if $@;
347 ################################################################
349 # Add a macro to the output.
354 # We want to ignore AC_ macros. However, if an AC_ macro is
355 # defined in (eg) acinclude.m4, then we want to make sure we mark
357 return if $macro =~ /^AC_/ && ! defined $map{$macro};
359 if (! defined $map{$macro})
361 warn "aclocal: macro `$macro' required but not defined\n";
366 print STDERR "aclocal: saw macro $macro\n" if $verbose;
367 $macro_seen{$macro} = 1;
368 &add_file ($map{$macro});
371 # Add a file to output.
376 # Only add a file once.
377 return if ($file_seen{$file});
378 $file_seen{$file} = 1;
380 my $mtime = mtime $file;
381 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
383 # If the file to add looks like path outside the project,
384 # copy it to the output.
385 # The regex catches filenames starting with things like
386 # / \ c:\ ../ ./../ etc.
387 if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
389 $output .= $file_contents{$file} . "\n";
393 # Otherwise, simply include the file.
394 $output .= "m4_include([$file])\n";
397 foreach (split ("\n", $file_contents{$file}))
399 # Remove comments from current line.
403 if (/$ac_require_rx/g)
405 push (@rlist, $1 || $2);
408 # The search function is constructed dynamically by
409 # scan_m4_files. The last parenthetical match makes sure we
410 # don't match things that look like macro assignments or
412 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
414 # Macro not found, but AM_ prefix found.
415 warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
421 foreach $macro (@rlist)
427 # Point to the documentation for underquoted AC_DEFUN only once.
428 my $underquoted_manual_once = 0;
430 # Scan a single M4 file. Return contents.
435 unshift @file_order, $file;
437 my $fh = new Automake::XFile $file;
439 while ($_ = $fh->getline)
450 print STDERR "$file:$.: underquoted definition of $2\n";
451 print STDERR "$file:$.: run info '(automake)Extending aclocal'\n"
452 . "or see http://sources.redhat.com/automake/"
453 . "automake.html#Extending%20aclocal\n"
454 unless $underquoted_manual_once;
455 $underquoted_manual_once = 1;
457 if (! defined $map{$1 || $2})
459 print STDERR "aclocal: found macro $1 in $file: $.\n"
461 $map{$1 || $2} = $file;
465 # Note: we used to give an error here if we saw a
466 # duplicated macro. However, this turns out to be
467 # extremely unpopular. It causes actual problems which
468 # are hard to work around, especially when you must
469 # mix-and-match tool versions.
470 print STDERR "aclocal: ignoring macro $1 in $file: $.\n"
479 sub trace_used_macros ($)
483 my %files = map { $map{$_} => 1 } keys %macro_seen;
485 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
486 $traces .= " --language Autoconf-without-aclocal-m4 $filename ";
487 # All candidate files.
488 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
489 # All candidate macros.
490 $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
492 print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
494 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
498 while ($_ = $tracefh->getline)
501 $traced{$_} = 1 if $macro_seen{$_};
509 ################################################################
512 sub write_aclocal ($@)
514 my ($output_file, @macros) = @_;
517 my %files = map { $map{$_} => 1 } @macros;
518 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
520 for $file (grep { exists $files{$_} } @file_order)
522 my $mtime = mtime $file;
523 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
525 # If the file to add looks like path outside the project,
526 # copy it to the output.
527 # The regex catches filenames starting with things like
528 # / \ c:\ ../ ./../ etc.
529 if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
531 $output .= $file_contents{$file} . "\n";
535 # Otherwise, simply include the file.
536 $output .= "m4_include([$file])\n";
540 # Nothing to output?!
541 # FIXME: Shouldn't we diagnose this?
542 return if ! length ($output);
544 # We used to print `# $output_file generated automatically etc.' But
545 # this creates spurious differences when using autoreconf. Autoreconf
546 # creates aclocal.m4t and then rename it to aclocal.m4, but the
547 # rebuild rules generated by Automake create aclocal.m4 directly --
548 # this would gives two ways to get the same file, with a different
549 # name in the header.
550 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
552 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
553 # Free Software Foundation, Inc.
554 # This file is free software; the Free Software Foundation
555 # gives unlimited permission to copy and/or distribute it,
556 # with or without modifications, as long as this notice is preserved.
558 # This program is distributed in the hope that it will be useful,
559 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
560 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
561 # PARTICULAR PURPOSE.
565 # We try not to update $output_file unless necessary, because
566 # doing so invalidate Autom4te's cache and therefore slows down
567 # tools called after aclocal.
569 # We need to overwrite $output_file in the following situations.
570 # * The --force option is in use.
571 # * One of the dependencies is younger.
572 # (Not updating $output_file in this situation would cause
573 # make to call aclocal in loop.)
574 # * The contents of the current file are different from what
577 && $greatest_mtime < mtime ($output_file)
578 && $output eq contents ($output_file))
580 print STDERR "aclocal: $output_file unchanged\n" if $verbose;
584 print STDERR "aclocal: writing $output_file\n" if $verbose;
586 my $out = new Automake::XFile "> $output_file";
591 ### Setup "GNU" style for perl-mode and cperl-mode.
593 ## perl-indent-level: 2
594 ## perl-continued-statement-offset: 2
595 ## perl-continued-brace-offset: 0
596 ## perl-brace-offset: 0
597 ## perl-brace-imaginary-offset: 0
598 ## perl-label-offset: -2
599 ## cperl-indent-level: 2
600 ## cperl-brace-offset: 0
601 ## cperl-continued-brace-offset: 0
602 ## cperl-label-offset: -2
603 ## cperl-extra-newline-before-brace: t
604 ## cperl-merge-trailing-else: nil
605 ## cperl-continued-statement-offset: 2