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.
79 # Map macro names to file names.
82 # Map file names to file contents.
88 # Matches a macro definition.
89 # AC_DEFUN([macroname], ...)
91 # AC_DEFUN(macroname, ...)
92 # When macroname is `['-quoted , we accept any character in the name,
93 # except `]'. Otherwise macroname stops on the first `]', `,', `)',
94 # or `\n' encountered.
95 $ac_defun_rx = "A[CU]_DEFUN\\((?:\\[([^]]+)\\]|([^],)\n]+))";
97 # Matches an AC_REQUIRE line.
98 $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
100 # Matches an m4_include line
101 $m4_include_rx = "(?:m4_)?s?include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
104 ################################################################
106 # Check macros in acinclude.m4. If one is not used, warn.
107 sub check_acinclude ()
111 foreach $key (keys %map)
113 next unless $map{$key} eq 'acinclude.m4';
114 if (! $macro_seen{$key})
116 # FIXME: should print line number of acinclude.m4.
117 warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
122 ################################################################
124 # Scan all the installed m4 files and construct a map.
125 sub scan_m4_files (@)
127 local (@dirlist) = @_;
129 # First, scan acinclude.m4 if it exists.
130 if (-f 'acinclude.m4')
132 $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
136 foreach $m4dir (@dirlist)
138 if (! opendir (DIR, $m4dir))
140 print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
144 local ($file, $fullfile);
145 # We reverse the directory contents so that foo2.m4 gets
146 # used in preference to foo1.m4.
147 foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
149 # Only examine .m4 files.
150 next unless $file =~ /\.m4$/;
152 # Skip some files when running out of srcdir.
153 next if $file eq 'aclocal.m4';
155 $fullfile = $m4dir . '/' . $file;
156 $file_contents{$fullfile} = &scan_file ($fullfile);
161 # Construct a new function that does the searching. We use a
162 # function (instead of just evaluating $search in the loop) so that
163 # "die" is correctly and easily propagated if run.
164 my $search = "sub search {\nmy \$found = 0;\n";
165 foreach my $key (reverse sort keys %map)
167 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
168 . '"); $found = 1; }' . "\n");
170 $search .= "return \$found;\n};\n";
172 die "internal error: $@\n search is $search" if $@;
175 ################################################################
177 # Add a macro to the output.
182 # We want to ignore AC_ macros. However, if an AC_ macro is
183 # defined in (eg) acinclude.m4, then we want to make sure we mark
185 return if $macro =~ /^AC_/ && ! defined $map{$macro};
187 if (! defined $map{$macro})
189 warn "aclocal: macro `$macro' required but not defined\n";
194 print STDERR "aclocal: saw macro $macro\n" if $verbose;
195 $macro_seen{$macro} = 1;
196 &add_file ($map{$macro});
199 # scan_contents ($file)
200 # --------------------------------
201 my %scanned_configure_dep = ();
202 sub scan_configure_dep ($)
205 # Do not scan a file twice.
207 if exists $scanned_configure_dep{$file};
208 $scanned_configure_dep{$file} = 1;
210 my $mtime = mtime $file;
211 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
213 my $contents = exists $file_contents{$file} ?
214 $file_contents{$file} : contents $file;
219 foreach (split ("\n", $contents))
222 # Remove comments from current line.
226 while (/$m4_include_rx/g)
228 push (@ilist, $1 || $2);
231 while (/$ac_require_rx/g)
233 push (@rlist, $1 || $2);
236 # The search function is constructed dynamically by
237 # scan_m4_files. The last parenthetical match makes sure we
238 # don't match things that look like macro assignments or
240 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
242 # Macro not found, but AM_ prefix found.
243 warn "aclocal: $file: $line: macro `$2' not found in library\n";
248 add_macro ($_) foreach (@rlist);
249 my $dirname = dirname $file;
250 &scan_configure_dep (File::Spec->rel2abs ($_, $dirname)) foreach (@ilist);
253 # Add a file to output.
258 # Only add a file once.
259 return if ($file_seen{$file});
260 $file_seen{$file} = 1;
262 scan_configure_dep $file;
265 # Point to the documentation for underquoted AC_DEFUN only once.
266 my $underquoted_manual_once = 0;
268 # Scan a single M4 file. Return contents.
273 unshift @file_order, $file;
275 my $fh = new Automake::XFile $file;
277 while ($_ = $fh->getline)
288 print STDERR "$file:$.: underquoted definition of $2\n";
289 print STDERR "$file:$.: run info '(automake)Extending aclocal'\n"
290 . "or see http://sources.redhat.com/automake/"
291 . "automake.html#Extending%20aclocal\n"
292 unless $underquoted_manual_once;
293 $underquoted_manual_once = 1;
295 if (! defined $map{$1 || $2})
297 print STDERR "aclocal: found macro $1 in $file: $.\n"
299 $map{$1 || $2} = $file;
303 # Note: we used to give an error here if we saw a
304 # duplicated macro. However, this turns out to be
305 # extremely unpopular. It causes actual problems which
306 # are hard to work around, especially when you must
307 # mix-and-match tool versions.
308 print STDERR "aclocal: ignoring macro $1 in $file: $.\n"
317 sub trace_used_macros ()
319 my %files = map { $map{$_} => 1 } keys %macro_seen;
321 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
322 $traces .= " --language Autoconf-without-aclocal-m4 ";
323 # All candidate files.
324 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
325 # All candidate macros.
326 $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
328 print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
330 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
334 while ($_ = $tracefh->getline)
337 $traced{$_} = 1 if $macro_seen{$_};
345 sub scan_configure ()
347 # Make sure we include acinclude.m4 if it exists.
348 if (-f 'acinclude.m4')
350 add_file ('acinclude.m4');
352 scan_configure_dep ($configure_ac);
355 ################################################################
358 sub write_aclocal ($@)
360 my ($output_file, @macros) = @_;
363 my %files = map { $map{$_} => 1 } @macros;
364 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
366 for $file (grep { exists $files{$_} } @file_order)
368 my $mtime = mtime $file;
369 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
371 # If the file to add looks like outside the project, copy it
372 # to the output. The regex catches filenames starting with
373 # things like `/', `\', or `c:\'.
374 if ($file =~ m,^(?:\w:)?[\\/],)
376 $output .= $file_contents{$file} . "\n";
380 # Otherwise, simply include the file.
381 $output .= "m4_include([$file])\n";
385 # Nothing to output?!
386 # FIXME: Shouldn't we diagnose this?
387 return if ! length ($output);
389 # We used to print `# $output_file generated automatically etc.' But
390 # this creates spurious differences when using autoreconf. Autoreconf
391 # creates aclocal.m4t and then rename it to aclocal.m4, but the
392 # rebuild rules generated by Automake create aclocal.m4 directly --
393 # this would gives two ways to get the same file, with a different
394 # name in the header.
395 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
397 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
398 # Free Software Foundation, Inc.
399 # This file is free software; the Free Software Foundation
400 # gives unlimited permission to copy and/or distribute it,
401 # with or without modifications, as long as this notice is preserved.
403 # This program is distributed in the hope that it will be useful,
404 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
405 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
406 # PARTICULAR PURPOSE.
410 # We try not to update $output_file unless necessary, because
411 # doing so invalidate Autom4te's cache and therefore slows down
412 # tools called after aclocal.
414 # We need to overwrite $output_file in the following situations.
415 # * The --force option is in use.
416 # * One of the dependencies is younger.
417 # (Not updating $output_file in this situation would cause
418 # make to call aclocal in loop.)
419 # * The contents of the current file are different from what
422 && $greatest_mtime < mtime ($output_file)
423 && $output eq contents ($output_file))
425 print STDERR "aclocal: $output_file unchanged\n" if $verbose;
429 print STDERR "aclocal: writing $output_file\n" if $verbose;
431 my $out = new Automake::XFile "> $output_file";
436 ################################################################
438 # Print usage and exit.
441 local ($status) = @_;
443 print "Usage: aclocal [OPTIONS] ...\n\n";
445 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
447 --acdir=DIR directory holding config files
448 --help print this help, then exit
449 -I DIR add directory to search list for .m4 files
450 --force always update output file
451 --output=FILE put output in FILE (default aclocal.m4)
452 --print-ac-dir print name of directory holding m4 files
453 --verbose don't be silent
454 --version print version number, then exit
456 Report bugs to <bug-automake\@gnu.org>.\n";
461 # Parse command line.
462 sub parse_arguments (@)
464 local (@arglist) = @_;
466 local ($print_and_exit) = 0;
470 if ($arglist[0] =~ /^--acdir=(.+)$/)
474 elsif ($arglist[0] =~/^--output=(.+)$/)
478 elsif ($arglist[0] eq '-I')
481 push (@dirlist, $arglist[0]);
483 elsif ($arglist[0] eq '--print-ac-dir')
487 elsif ($arglist[0] eq '--force')
491 elsif ($arglist[0] eq '--verbose')
495 elsif ($arglist[0] eq '--version')
497 print "aclocal (GNU $PACKAGE) $VERSION\n\n";
498 print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
499 print "This is free software; see the source for copying conditions. There is NO\n";
500 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
501 print "Written by Tom Tromey <tromey\@redhat.com>\n";
504 elsif ($arglist[0] eq '--help')
510 print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
523 $default_dirlist="$acdir/dirlist"
524 if $acdir ne $default_acdir;
526 # Search the versioned directory near the end, and then the
527 # unversioned directory last. Only do this if the user didn't
529 push (@dirlist, "$acdir-$APIVERSION")
530 if $acdir eq $default_acdir;
532 # By default $(datadir)/aclocal doesn't exist. We don't want to
533 # get an error in the case where we are searching the default
534 # directory and it hasn't been created.
535 push (@dirlist, $acdir)
536 unless $acdir eq $default_acdir && ! -d $acdir;
538 # Finally, adds any directory listed in the `dirlist' file.
539 if (open (DEFAULT_DIRLIST, $default_dirlist))
541 while (<DEFAULT_DIRLIST>)
545 # strip off newlines and end-of-line comments
547 chomp ($contents=$_);
550 push (@dirlist, $contents);
553 close (DEFAULT_DIRLIST);
559 ################################################################
561 local (@dirlist) = parse_arguments (@ARGV);
562 $configure_ac = require_configure_ac;
563 scan_m4_files (@dirlist);
567 my %macro_traced = trace_used_macros;
568 write_aclocal ($output_file, keys %macro_traced);
574 ### Setup "GNU" style for perl-mode and cperl-mode.
576 ## perl-indent-level: 2
577 ## perl-continued-statement-offset: 2
578 ## perl-continued-brace-offset: 0
579 ## perl-brace-offset: 0
580 ## perl-brace-imaginary-offset: 0
581 ## perl-label-offset: -2
582 ## cperl-indent-level: 2
583 ## cperl-brace-offset: 0
584 ## cperl-continued-brace-offset: 0
585 ## cperl-label-offset: -2
586 ## cperl-extra-newline-before-brace: t
587 ## cperl-merge-trailing-else: nil
588 ## cperl-continued-statement-offset: 2