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;
40 use Automake::FileUtils;
43 # Note that this isn't pkgdatadir, but a separate directory.
44 # Note also that the versioned directory is handled later.
45 $acdir = '@datadir@/aclocal';
46 $default_acdir = $acdir;
47 # contains a list of directories, one per line, to be added
48 # to the dirlist in addition to $acdir, as if -I had been
49 # added to the command line. If acdir has been redirected,
50 # we will also check the specified acdir (this is done later).
51 $default_dirlist = "$default_acdir/dirlist";
55 # configure.ac or configure.in.
56 my $configure_ac = require_configure_ac;
62 $output_file = 'aclocal.m4';
64 # Modification time of the youngest dependency.
70 # Which macros have been seen.
73 # Which files have been seen.
76 # Map macro names to file names.
79 # Map file names to file contents.
85 # Matches a macro definition.
86 # AC_DEFUN([macroname], ...)
88 # AC_DEFUN(macroname, ...)
89 # When macroname is `['-quoted , we accept any character in the name,
90 # except `]'. Otherwise macroname stops on the first `]', `,', `)',
91 # or `\n' encountered.
92 $ac_defun_rx = "A[CU]_DEFUN\\((?:\\[([^]]+)\\]|([^],)\n]+))";
94 # Matches an AC_REQUIRE line.
95 $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
99 local (@dirlist) = &parse_arguments (@ARGV);
100 &scan_m4_files (@dirlist);
104 my %macro_traced = &trace_used_macros;
105 &write_aclocal ($output_file, keys %macro_traced);
111 ################################################################
113 # Print usage and exit.
116 local ($status) = @_;
118 print "Usage: aclocal [OPTIONS] ...\n\n";
120 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
122 --acdir=DIR directory holding config files
123 --help print this help, then exit
124 -I DIR add directory to search list for .m4 files
125 --force always update output file
126 --output=FILE put output in FILE (default aclocal.m4)
127 --print-ac-dir print name of directory holding m4 files
128 --verbose don't be silent
129 --version print version number, then exit
131 Report bugs to <bug-automake\@gnu.org>.\n";
136 # Parse command line.
137 sub parse_arguments (@)
139 local (@arglist) = @_;
141 local ($print_and_exit) = 0;
145 if ($arglist[0] =~ /^--acdir=(.+)$/)
149 elsif ($arglist[0] =~/^--output=(.+)$/)
153 elsif ($arglist[0] eq '-I')
156 push (@dirlist, $arglist[0]);
158 elsif ($arglist[0] eq '--print-ac-dir')
162 elsif ($arglist[0] eq '--force')
166 elsif ($arglist[0] eq '--verbose')
170 elsif ($arglist[0] eq '--version')
172 print "aclocal (GNU $PACKAGE) $VERSION\n\n";
173 print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
174 print "This is free software; see the source for copying conditions. There is NO\n";
175 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
176 print "Written by Tom Tromey <tromey\@redhat.com>\n";
179 elsif ($arglist[0] eq '--help')
185 print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
198 $default_dirlist="$acdir/dirlist"
199 if $acdir ne $default_acdir;
201 # Search the versioned directory near the end, and then the
202 # unversioned directory last. Only do this if the user didn't
204 push (@dirlist, "$acdir-$APIVERSION")
205 if $acdir eq $default_acdir;
207 # By default $(datadir)/aclocal doesn't exist. We don't want to
208 # get an error in the case where we are searching the default
209 # directory and it hasn't been created.
210 push (@dirlist, $acdir)
211 unless $acdir eq $default_acdir && ! -d $acdir;
213 # Finally, adds any directory listed in the `dirlist' file.
214 if (open (DEFAULT_DIRLIST, $default_dirlist))
216 while (<DEFAULT_DIRLIST>)
220 # strip off newlines and end-of-line comments
222 chomp ($contents=$_);
225 push (@dirlist, $contents);
228 close (DEFAULT_DIRLIST);
235 ################################################################
237 sub scan_configure ()
239 if (! open (CONFIGURE, $configure_ac))
241 print STDERR "aclocal: couldn't open `$configure_ac': $!\n";
245 my $mtime = mtime $configure_ac;
246 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
248 # Make sure we include acinclude.m4 if it exists.
249 if (-f 'acinclude.m4')
251 &add_file ('acinclude.m4');
256 # Remove comments from current line.
260 # Search for things we know about. The "search" sub is
261 # constructed dynamically by scan_m4_files. The last
262 # parenthethical match makes sure we don't match things that
263 # look like macro assignments or AC_SUBSTs.
264 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
266 # Macro not found, but AM_ prefix found.
267 warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
275 ################################################################
277 # Check macros in acinclude.m4. If one is not used, warn.
278 sub check_acinclude ()
282 foreach $key (keys %map)
284 next unless $map{$key} eq 'acinclude.m4';
285 if (! $macro_seen{$key})
287 # FIXME: should print line number of acinclude.m4.
288 warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
293 ################################################################
295 # Scan all the installed m4 files and construct a map.
296 sub scan_m4_files (@)
298 local (@dirlist) = @_;
300 # First, scan acinclude.m4 if it exists.
301 if (-f 'acinclude.m4')
303 $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
307 foreach $m4dir (@dirlist)
309 if (! opendir (DIR, $m4dir))
311 print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
315 local ($file, $fullfile);
316 foreach $file (sort grep (! /^\./, readdir (DIR)))
318 # Only examine .m4 files.
319 next unless $file =~ /\.m4$/;
321 # Skip some files when running out of srcdir.
322 next if $file eq 'aclocal.m4';
324 $fullfile = $m4dir . '/' . $file;
325 $file_contents{$fullfile} = &scan_file ($fullfile);
330 # Construct a new function that does the searching. We use a
331 # function (instead of just evalling $search in the loop) so that
332 # "die" is correctly and easily propagated if run.
333 my $search = "sub search {\nmy \$found = 0;\n";
334 foreach my $key (reverse sort keys %map)
336 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
337 . '"); $found = 1; }' . "\n");
339 $search .= "return \$found;\n};\n";
341 die "internal error: $@\n search is $search" if $@;
344 ################################################################
346 # Add a macro to the output.
351 # We want to ignore AC_ macros. However, if an AC_ macro is
352 # defined in (eg) acinclude.m4, then we want to make sure we mark
354 return if $macro =~ /^AC_/ && ! defined $map{$macro};
356 if (! defined $map{$macro})
358 warn "aclocal: macro `$macro' required but not defined\n";
363 print STDERR "aclocal: saw macro $macro\n" if $verbose;
364 $macro_seen{$macro} = 1;
365 &add_file ($map{$macro});
368 # Add a file to output.
373 # Only add a file once.
374 return if ($file_seen{$file});
375 $file_seen{$file} = 1;
377 my $mtime = mtime $file;
378 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
380 # If the file to add looks like path outside the project,
381 # copy it to the output.
382 # The regex catches filenames starting with things like
383 # / \ c:\ ../ ./../ etc.
384 if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
386 $output .= $file_contents{$file} . "\n";
390 # Otherwise, simply include the file.
391 $output .= "m4_include([$file])\n";
394 foreach (split ("\n", $file_contents{$file}))
396 # Remove comments from current line.
400 if (/$ac_require_rx/g)
402 push (@rlist, $1 || $2);
405 # The search function is constructed dynamically by
406 # scan_m4_files. The last parenthethical match makes sure we
407 # don't match things that look like macro assignments or
409 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
411 # Macro not found, but AM_ prefix found.
412 warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
418 foreach $macro (@rlist)
424 # Scan a single M4 file. Return contents.
429 my $fh = new Automake::XFile $file;
431 while ($_ = $fh->getline)
440 if (! defined $map{$1 || $2})
442 $map{$1 || $2} = $file;
445 # Note: we used to give an error here if we saw a
446 # duplicated macro. However, this turns out to be
447 # extremely unpopular. It causes actual problems which
448 # are hard to work around, especially when you must
449 # mix-and-match tool versions.
451 print STDERR "aclocal: found macro $1 in $file: $.\n" if $verbose;
458 sub trace_used_macros ($)
462 my %files = map { $map{$_} => 1 } keys %macro_seen;
464 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
465 $traces .= " --language Autoconf-without-aclocal-m4 $filename ";
466 # All candidate files.
467 $traces .= join (' ', sort keys %files) . " ";
468 # All candidate macros.
469 $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
471 print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
473 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
477 while ($_ = $tracefh->getline)
480 $traced{$_} = 1 if $macro_seen{$_};
485 ################################################################
488 sub write_aclocal ($@)
490 my ($output_file, @macros) = @_;
493 my %files = map { $map{$_} => 1 } @macros;
494 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
496 for $file (sort keys %files)
498 my $mtime = mtime $file;
499 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
501 # If the file to add looks like path outside the project,
502 # copy it to the output.
503 # The regex catches filenames starting with things like
504 # / \ c:\ ../ ./../ etc.
505 if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
507 $output .= $file_contents{$file} . "\n";
511 # Otherwise, simply include the file.
512 $output .= "m4_include([$file])\n";
516 # Nothing to output?!
517 # FIXME: Shouldn't we diagnose this?
518 return if ! length ($output);
520 # We used to print `# $output_file generated automatically etc.' But
521 # this creates spurious differences when using autoreconf. Autoreconf
522 # creates aclocal.m4t and then rename it to aclocal.m4, but the
523 # rebuild rules generated by Automake create aclocal.m4 directly --
524 # this would gives two ways to get the same file, with a different
525 # name in the header.
526 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
528 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
529 # Free Software Foundation, Inc.
530 # This file is free software; the Free Software Foundation
531 # gives unlimited permission to copy and/or distribute it,
532 # with or without modifications, as long as this notice is preserved.
534 # This program is distributed in the hope that it will be useful,
535 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
536 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
537 # PARTICULAR PURPOSE.
541 # We try not to update $output_file unless necessary, because
542 # doing so invalidate Autom4te's cache and therefore slows down
543 # tools called after aclocal.
545 # We need to overwrite $output_file in the following situations.
546 # * The --force option is in use.
547 # * One of the dependencies is younger.
548 # (Not updating $output_file in this situation would cause
549 # make to call aclocal in loop.)
550 # * The contents of the current file are different from what
553 && $greatest_mtime < mtime ($output_file)
554 && $output eq contents ($output_file))
556 print STDERR "aclocal: $output_file unchanged\n" if $verbose;
560 print STDERR "aclocal: writing $output_file\n" if $verbose;
562 my $out = new Automake::XFile "> $output_file";
567 ### Setup "GNU" style for perl-mode and cperl-mode.
569 ## perl-indent-level: 2
570 ## perl-continued-statement-offset: 2
571 ## perl-continued-brace-offset: 0
572 ## perl-brace-offset: 0
573 ## perl-brace-imaginary-offset: 0
574 ## perl-label-offset: -2
575 ## cperl-indent-level: 2
576 ## cperl-brace-offset: 0
577 ## cperl-continued-brace-offset: 0
578 ## cperl-label-offset: -2
579 ## cperl-extra-newline-before-brace: t
580 ## cperl-merge-trailing-else: nil
581 ## cperl-continued-statement-offset: 2