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.
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 $configure_ac = require_configure_ac;
103 &scan_m4_files (@dirlist);
107 my %macro_traced = &trace_used_macros;
108 &write_aclocal ($output_file, keys %macro_traced);
114 ################################################################
116 # Print usage and exit.
119 local ($status) = @_;
121 print "Usage: aclocal [OPTIONS] ...\n\n";
123 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
125 --acdir=DIR directory holding config files
126 --help print this help, then exit
127 -I DIR add directory to search list for .m4 files
128 --force always update output file
129 --output=FILE put output in FILE (default aclocal.m4)
130 --print-ac-dir print name of directory holding m4 files
131 --verbose don't be silent
132 --version print version number, then exit
134 Report bugs to <bug-automake\@gnu.org>.\n";
139 # Parse command line.
140 sub parse_arguments (@)
142 local (@arglist) = @_;
144 local ($print_and_exit) = 0;
148 if ($arglist[0] =~ /^--acdir=(.+)$/)
152 elsif ($arglist[0] =~/^--output=(.+)$/)
156 elsif ($arglist[0] eq '-I')
159 push (@dirlist, $arglist[0]);
161 elsif ($arglist[0] eq '--print-ac-dir')
165 elsif ($arglist[0] eq '--force')
169 elsif ($arglist[0] eq '--verbose')
173 elsif ($arglist[0] eq '--version')
175 print "aclocal (GNU $PACKAGE) $VERSION\n\n";
176 print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
177 print "This is free software; see the source for copying conditions. There is NO\n";
178 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
179 print "Written by Tom Tromey <tromey\@redhat.com>\n";
182 elsif ($arglist[0] eq '--help')
188 print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
201 $default_dirlist="$acdir/dirlist"
202 if $acdir ne $default_acdir;
204 # Search the versioned directory near the end, and then the
205 # unversioned directory last. Only do this if the user didn't
207 push (@dirlist, "$acdir-$APIVERSION")
208 if $acdir eq $default_acdir;
210 # By default $(datadir)/aclocal doesn't exist. We don't want to
211 # get an error in the case where we are searching the default
212 # directory and it hasn't been created.
213 push (@dirlist, $acdir)
214 unless $acdir eq $default_acdir && ! -d $acdir;
216 # Finally, adds any directory listed in the `dirlist' file.
217 if (open (DEFAULT_DIRLIST, $default_dirlist))
219 while (<DEFAULT_DIRLIST>)
223 # strip off newlines and end-of-line comments
225 chomp ($contents=$_);
228 push (@dirlist, $contents);
231 close (DEFAULT_DIRLIST);
237 ################################################################
239 sub scan_configure ()
241 if (! open (CONFIGURE, $configure_ac))
243 print STDERR "aclocal: couldn't open `$configure_ac': $!\n";
247 my $mtime = mtime $configure_ac;
248 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
250 # Make sure we include acinclude.m4 if it exists.
251 if (-f 'acinclude.m4')
253 &add_file ('acinclude.m4');
258 # Remove comments from current line.
262 # Search for things we know about. The "search" sub is
263 # constructed dynamically by scan_m4_files. The last
264 # parenthetical match makes sure we don't match things that
265 # look like macro assignments or AC_SUBSTs.
266 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
268 # Macro not found, but AM_ prefix found.
269 warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
277 ################################################################
279 # Check macros in acinclude.m4. If one is not used, warn.
280 sub check_acinclude ()
284 foreach $key (keys %map)
286 next unless $map{$key} eq 'acinclude.m4';
287 if (! $macro_seen{$key})
289 # FIXME: should print line number of acinclude.m4.
290 warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
295 ################################################################
297 # Scan all the installed m4 files and construct a map.
298 sub scan_m4_files (@)
300 local (@dirlist) = @_;
302 # First, scan acinclude.m4 if it exists.
303 if (-f 'acinclude.m4')
305 $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
309 foreach $m4dir (@dirlist)
311 if (! opendir (DIR, $m4dir))
313 print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
317 local ($file, $fullfile);
318 # We reverse the directory contents so that foo2.m4 gets
319 # used in preference to foo1.m4.
320 foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
322 # Only examine .m4 files.
323 next unless $file =~ /\.m4$/;
325 # Skip some files when running out of srcdir.
326 next if $file eq 'aclocal.m4';
328 $fullfile = $m4dir . '/' . $file;
329 $file_contents{$fullfile} = &scan_file ($fullfile);
334 # Construct a new function that does the searching. We use a
335 # function (instead of just evaluating $search in the loop) so that
336 # "die" is correctly and easily propagated if run.
337 my $search = "sub search {\nmy \$found = 0;\n";
338 foreach my $key (reverse sort keys %map)
340 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
341 . '"); $found = 1; }' . "\n");
343 $search .= "return \$found;\n};\n";
345 die "internal error: $@\n search is $search" if $@;
348 ################################################################
350 # Add a macro to the output.
355 # We want to ignore AC_ macros. However, if an AC_ macro is
356 # defined in (eg) acinclude.m4, then we want to make sure we mark
358 return if $macro =~ /^AC_/ && ! defined $map{$macro};
360 if (! defined $map{$macro})
362 warn "aclocal: macro `$macro' required but not defined\n";
367 print STDERR "aclocal: saw macro $macro\n" if $verbose;
368 $macro_seen{$macro} = 1;
369 &add_file ($map{$macro});
372 # Add a file to output.
377 # Only add a file once.
378 return if ($file_seen{$file});
379 $file_seen{$file} = 1;
381 my $mtime = mtime $file;
382 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
385 foreach (split ("\n", $file_contents{$file}))
387 # Remove comments from current line.
391 if (/$ac_require_rx/g)
393 push (@rlist, $1 || $2);
396 # The search function is constructed dynamically by
397 # scan_m4_files. The last parenthetical match makes sure we
398 # don't match things that look like macro assignments or
400 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
402 # Macro not found, but AM_ prefix found.
403 warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
409 foreach $macro (@rlist)
415 # Point to the documentation for underquoted AC_DEFUN only once.
416 my $underquoted_manual_once = 0;
418 # Scan a single M4 file. Return contents.
423 unshift @file_order, $file;
425 my $fh = new Automake::XFile $file;
427 while ($_ = $fh->getline)
438 print STDERR "$file:$.: underquoted definition of $2\n";
439 print STDERR "$file:$.: run info '(automake)Extending aclocal'\n"
440 . "or see http://sources.redhat.com/automake/"
441 . "automake.html#Extending%20aclocal\n"
442 unless $underquoted_manual_once;
443 $underquoted_manual_once = 1;
445 if (! defined $map{$1 || $2})
447 print STDERR "aclocal: found macro $1 in $file: $.\n"
449 $map{$1 || $2} = $file;
453 # Note: we used to give an error here if we saw a
454 # duplicated macro. However, this turns out to be
455 # extremely unpopular. It causes actual problems which
456 # are hard to work around, especially when you must
457 # mix-and-match tool versions.
458 print STDERR "aclocal: ignoring macro $1 in $file: $.\n"
467 sub trace_used_macros ($)
471 my %files = map { $map{$_} => 1 } keys %macro_seen;
473 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
474 $traces .= " --language Autoconf-without-aclocal-m4 $filename ";
475 # All candidate files.
476 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
477 # All candidate macros.
478 $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
480 print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
482 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
486 while ($_ = $tracefh->getline)
489 $traced{$_} = 1 if $macro_seen{$_};
497 ################################################################
500 sub write_aclocal ($@)
502 my ($output_file, @macros) = @_;
505 my %files = map { $map{$_} => 1 } @macros;
506 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
508 for $file (grep { exists $files{$_} } @file_order)
510 my $mtime = mtime $file;
511 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
513 # If the file to add looks like path outside the project,
514 # copy it to the output.
515 # The regex catches filenames starting with things like
516 # / \ c:\ ../ ./../ etc.
517 if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
519 $output .= $file_contents{$file} . "\n";
523 # Otherwise, simply include the file.
524 $output .= "m4_include([$file])\n";
528 # Nothing to output?!
529 # FIXME: Shouldn't we diagnose this?
530 return if ! length ($output);
532 # We used to print `# $output_file generated automatically etc.' But
533 # this creates spurious differences when using autoreconf. Autoreconf
534 # creates aclocal.m4t and then rename it to aclocal.m4, but the
535 # rebuild rules generated by Automake create aclocal.m4 directly --
536 # this would gives two ways to get the same file, with a different
537 # name in the header.
538 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
540 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
541 # Free Software Foundation, Inc.
542 # This file is free software; the Free Software Foundation
543 # gives unlimited permission to copy and/or distribute it,
544 # with or without modifications, as long as this notice is preserved.
546 # This program is distributed in the hope that it will be useful,
547 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
548 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
549 # PARTICULAR PURPOSE.
553 # We try not to update $output_file unless necessary, because
554 # doing so invalidate Autom4te's cache and therefore slows down
555 # tools called after aclocal.
557 # We need to overwrite $output_file in the following situations.
558 # * The --force option is in use.
559 # * One of the dependencies is younger.
560 # (Not updating $output_file in this situation would cause
561 # make to call aclocal in loop.)
562 # * The contents of the current file are different from what
565 && $greatest_mtime < mtime ($output_file)
566 && $output eq contents ($output_file))
568 print STDERR "aclocal: $output_file unchanged\n" if $verbose;
572 print STDERR "aclocal: writing $output_file\n" if $verbose;
574 my $out = new Automake::XFile "> $output_file";
579 ### Setup "GNU" style for perl-mode and cperl-mode.
581 ## perl-indent-level: 2
582 ## perl-continued-statement-offset: 2
583 ## perl-continued-brace-offset: 0
584 ## perl-brace-offset: 0
585 ## perl-brace-imaginary-offset: 0
586 ## perl-label-offset: -2
587 ## cperl-indent-level: 2
588 ## cperl-brace-offset: 0
589 ## cperl-continued-brace-offset: 0
590 ## cperl-label-offset: -2
591 ## cperl-extra-newline-before-brace: t
592 ## cperl-merge-trailing-else: nil
593 ## cperl-continued-statement-offset: 2