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 ()
109 foreach my $key (keys %map)
111 # FIXME: should print line number of acinclude.m4.
112 warn ("aclocal: warning: macro `$key' defined in "
113 . "acinclude.m4 but never used\n")
114 if $map{$key} eq 'acinclude.m4' && ! $macro_seen{$key};
118 ################################################################
120 # Scan all the installed m4 files and construct a map.
121 sub scan_m4_files (@)
123 local (@dirlist) = @_;
125 # First, scan acinclude.m4 if it exists.
126 if (-f 'acinclude.m4')
128 $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
132 foreach $m4dir (@dirlist)
134 if (! opendir (DIR, $m4dir))
136 print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
140 local ($file, $fullfile);
141 # We reverse the directory contents so that foo2.m4 gets
142 # used in preference to foo1.m4.
143 foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
145 # Only examine .m4 files.
146 next unless $file =~ /\.m4$/;
148 # Skip some files when running out of srcdir.
149 next if $file eq 'aclocal.m4';
151 $fullfile = $m4dir . '/' . $file;
152 $file_contents{$fullfile} = &scan_file ($fullfile);
157 # Construct a new function that does the searching. We use a
158 # function (instead of just evaluating $search in the loop) so that
159 # "die" is correctly and easily propagated if run.
160 my $search = "sub search {\nmy \$found = 0;\n";
161 foreach my $key (reverse sort keys %map)
163 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
164 . '"); $found = 1; }' . "\n");
166 $search .= "return \$found;\n};\n";
168 die "internal error: $@\n search is $search" if $@;
171 ################################################################
173 # Add a macro to the output.
178 # We want to ignore AC_ macros. However, if an AC_ macro is
179 # defined in (eg) acinclude.m4, then we want to make sure we mark
181 return if $macro =~ /^AC_/ && ! defined $map{$macro};
183 if (! defined $map{$macro})
185 warn "aclocal: macro `$macro' required but not defined\n";
190 print STDERR "aclocal: saw macro $macro\n" if $verbose;
191 $macro_seen{$macro} = 1;
192 &add_file ($map{$macro});
195 # scan_contents ($file)
196 # --------------------------------
197 my %scanned_configure_dep = ();
198 sub scan_configure_dep ($)
201 # Do not scan a file twice.
203 if exists $scanned_configure_dep{$file};
204 $scanned_configure_dep{$file} = 1;
206 my $mtime = mtime $file;
207 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
209 my $contents = exists $file_contents{$file} ?
210 $file_contents{$file} : contents $file;
215 foreach (split ("\n", $contents))
218 # Remove comments from current line.
222 while (/$m4_include_rx/g)
224 push (@ilist, $1 || $2);
227 while (/$ac_require_rx/g)
229 push (@rlist, $1 || $2);
232 # The search function is constructed dynamically by
233 # scan_m4_files. The last parenthetical match makes sure we
234 # don't match things that look like macro assignments or
236 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
238 # Macro not found, but AM_ prefix found.
239 warn "aclocal: $file: $line: macro `$2' not found in library\n";
244 add_macro ($_) foreach (@rlist);
245 my $dirname = dirname $file;
246 &scan_configure_dep (File::Spec->rel2abs ($_, $dirname)) foreach (@ilist);
249 # Add a file to output.
254 # Only add a file once.
255 return if ($file_seen{$file});
256 $file_seen{$file} = 1;
258 scan_configure_dep $file;
261 # Point to the documentation for underquoted AC_DEFUN only once.
262 my $underquoted_manual_once = 0;
264 # Scan a single M4 file. Return contents.
269 unshift @file_order, $file;
271 my $fh = new Automake::XFile $file;
273 while ($_ = $fh->getline)
284 print STDERR "$file:$.: warning: underquoted definition of $2\n";
285 print STDERR " run info '(automake)Extending aclocal'\n"
286 . " or see http://sources.redhat.com/automake/"
287 . "automake.html#Extending%20aclocal\n"
288 unless $underquoted_manual_once;
289 $underquoted_manual_once = 1;
291 if (! defined $map{$1 || $2})
293 print STDERR "aclocal: found macro $1 in $file: $.\n"
295 $map{$1 || $2} = $file;
299 # Note: we used to give an error here if we saw a
300 # duplicated macro. However, this turns out to be
301 # extremely unpopular. It causes actual problems which
302 # are hard to work around, especially when you must
303 # mix-and-match tool versions.
304 print STDERR "aclocal: ignoring macro $1 in $file: $.\n"
313 sub trace_used_macros ()
315 my %files = map { $map{$_} => 1 } keys %macro_seen;
317 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
318 $traces .= " --language Autoconf-without-aclocal-m4 ";
319 # All candidate files.
320 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
321 # All candidate macros.
322 $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
324 print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
326 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
330 while ($_ = $tracefh->getline)
333 $traced{$_} = 1 if $macro_seen{$_};
341 sub scan_configure ()
343 # Make sure we include acinclude.m4 if it exists.
344 if (-f 'acinclude.m4')
346 add_file ('acinclude.m4');
348 scan_configure_dep ($configure_ac);
351 ################################################################
354 sub write_aclocal ($@)
356 my ($output_file, @macros) = @_;
359 my %files = map { $map{$_} => 1 } @macros;
360 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
362 for $file (grep { exists $files{$_} } @file_order)
364 my $mtime = mtime $file;
365 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
367 # If the file to add looks like outside the project, copy it
368 # to the output. The regex catches filenames starting with
369 # things like `/', `\', or `c:\'.
370 if ($file =~ m,^(?:\w:)?[\\/],)
372 $output .= $file_contents{$file} . "\n";
376 # Otherwise, simply include the file.
377 $output .= "m4_include([$file])\n";
381 # Nothing to output?!
382 # FIXME: Shouldn't we diagnose this?
383 return if ! length ($output);
385 # We used to print `# $output_file generated automatically etc.' But
386 # this creates spurious differences when using autoreconf. Autoreconf
387 # creates aclocal.m4t and then rename it to aclocal.m4, but the
388 # rebuild rules generated by Automake create aclocal.m4 directly --
389 # this would gives two ways to get the same file, with a different
390 # name in the header.
391 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
393 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
394 # Free Software Foundation, Inc.
395 # This file is free software; the Free Software Foundation
396 # gives unlimited permission to copy and/or distribute it,
397 # with or without modifications, as long as this notice is preserved.
399 # This program is distributed in the hope that it will be useful,
400 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
401 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
402 # PARTICULAR PURPOSE.
406 # We try not to update $output_file unless necessary, because
407 # doing so invalidate Autom4te's cache and therefore slows down
408 # tools called after aclocal.
410 # We need to overwrite $output_file in the following situations.
411 # * The --force option is in use.
412 # * One of the dependencies is younger.
413 # (Not updating $output_file in this situation would cause
414 # make to call aclocal in loop.)
415 # * The contents of the current file are different from what
418 && $greatest_mtime < mtime ($output_file)
419 && $output eq contents ($output_file))
421 print STDERR "aclocal: $output_file unchanged\n" if $verbose;
425 print STDERR "aclocal: writing $output_file\n" if $verbose;
427 my $out = new Automake::XFile "> $output_file";
432 ################################################################
434 # Print usage and exit.
437 local ($status) = @_;
439 print "Usage: aclocal [OPTIONS] ...\n\n";
441 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
443 --acdir=DIR directory holding config files
444 --help print this help, then exit
445 -I DIR add directory to search list for .m4 files
446 --force always update output file
447 --output=FILE put output in FILE (default aclocal.m4)
448 --print-ac-dir print name of directory holding m4 files
449 --verbose don't be silent
450 --version print version number, then exit
452 Report bugs to <bug-automake\@gnu.org>.\n";
457 # Parse command line.
458 sub parse_arguments (@)
460 local (@arglist) = @_;
462 local ($print_and_exit) = 0;
466 if ($arglist[0] =~ /^--acdir=(.+)$/)
470 elsif ($arglist[0] =~/^--output=(.+)$/)
474 elsif ($arglist[0] eq '-I')
477 push (@dirlist, $arglist[0]);
479 elsif ($arglist[0] eq '--print-ac-dir')
483 elsif ($arglist[0] eq '--force')
487 elsif ($arglist[0] eq '--verbose')
491 elsif ($arglist[0] eq '--version')
493 print "aclocal (GNU $PACKAGE) $VERSION\n\n";
494 print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
495 print "This is free software; see the source for copying conditions. There is NO\n";
496 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
497 print "Written by Tom Tromey <tromey\@redhat.com>\n";
500 elsif ($arglist[0] eq '--help')
506 print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
519 $default_dirlist="$acdir/dirlist"
520 if $acdir ne $default_acdir;
522 # Search the versioned directory near the end, and then the
523 # unversioned directory last. Only do this if the user didn't
525 push (@dirlist, "$acdir-$APIVERSION")
526 if $acdir eq $default_acdir;
528 # By default $(datadir)/aclocal doesn't exist. We don't want to
529 # get an error in the case where we are searching the default
530 # directory and it hasn't been created.
531 push (@dirlist, $acdir)
532 unless $acdir eq $default_acdir && ! -d $acdir;
534 # Finally, adds any directory listed in the `dirlist' file.
535 if (open (DEFAULT_DIRLIST, $default_dirlist))
537 while (<DEFAULT_DIRLIST>)
541 # strip off newlines and end-of-line comments
543 chomp ($contents=$_);
546 push (@dirlist, $contents);
549 close (DEFAULT_DIRLIST);
555 ################################################################
557 local (@dirlist) = parse_arguments (@ARGV);
558 $configure_ac = require_configure_ac;
559 scan_m4_files (@dirlist);
563 my %macro_traced = trace_used_macros;
564 write_aclocal ($output_file, keys %macro_traced);
570 ### Setup "GNU" style for perl-mode and cperl-mode.
572 ## perl-indent-level: 2
573 ## perl-continued-statement-offset: 2
574 ## perl-continued-brace-offset: 0
575 ## perl-brace-offset: 0
576 ## perl-brace-imaginary-offset: 0
577 ## perl-label-offset: -2
578 ## cperl-indent-level: 2
579 ## cperl-brace-offset: 0
580 ## cperl-continued-brace-offset: 0
581 ## cperl-label-offset: -2
582 ## cperl-extra-newline-before-brace: t
583 ## cperl-merge-trailing-else: nil
584 ## cperl-continued-statement-offset: 2