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 # Remember the order into which we scanned the files.
77 # It's important to output the contents of aclocal.m4 in the opposite order.
80 # Map macro names to file names.
83 # Map file names to file contents.
89 # Matches a macro definition.
90 # AC_DEFUN([macroname], ...)
92 # AC_DEFUN(macroname, ...)
93 # When macroname is `['-quoted , we accept any character in the name,
94 # except `]'. Otherwise macroname stops on the first `]', `,', `)',
95 # or `\n' encountered.
96 $ac_defun_rx = "A[CU]_DEFUN\\((?:\\[([^]]+)\\]|([^],)\n]+))";
98 # Matches an AC_REQUIRE line.
99 $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
103 local (@dirlist) = &parse_arguments (@ARGV);
104 &scan_m4_files (@dirlist);
108 my %macro_traced = &trace_used_macros;
109 &write_aclocal ($output_file, keys %macro_traced);
115 ################################################################
117 # Print usage and exit.
120 local ($status) = @_;
122 print "Usage: aclocal [OPTIONS] ...\n\n";
124 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
126 --acdir=DIR directory holding config files
127 --help print this help, then exit
128 -I DIR add directory to search list for .m4 files
129 --force always update output file
130 --output=FILE put output in FILE (default aclocal.m4)
131 --print-ac-dir print name of directory holding m4 files
132 --verbose don't be silent
133 --version print version number, then exit
135 Report bugs to <bug-automake\@gnu.org>.\n";
140 # Parse command line.
141 sub parse_arguments (@)
143 local (@arglist) = @_;
145 local ($print_and_exit) = 0;
149 if ($arglist[0] =~ /^--acdir=(.+)$/)
153 elsif ($arglist[0] =~/^--output=(.+)$/)
157 elsif ($arglist[0] eq '-I')
160 push (@dirlist, $arglist[0]);
162 elsif ($arglist[0] eq '--print-ac-dir')
166 elsif ($arglist[0] eq '--force')
170 elsif ($arglist[0] eq '--verbose')
174 elsif ($arglist[0] eq '--version')
176 print "aclocal (GNU $PACKAGE) $VERSION\n\n";
177 print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
178 print "This is free software; see the source for copying conditions. There is NO\n";
179 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
180 print "Written by Tom Tromey <tromey\@redhat.com>\n";
183 elsif ($arglist[0] eq '--help')
189 print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
202 $default_dirlist="$acdir/dirlist"
203 if $acdir ne $default_acdir;
205 # Search the versioned directory near the end, and then the
206 # unversioned directory last. Only do this if the user didn't
208 push (@dirlist, "$acdir-$APIVERSION")
209 if $acdir eq $default_acdir;
211 # By default $(datadir)/aclocal doesn't exist. We don't want to
212 # get an error in the case where we are searching the default
213 # directory and it hasn't been created.
214 push (@dirlist, $acdir)
215 unless $acdir eq $default_acdir && ! -d $acdir;
217 # Finally, adds any directory listed in the `dirlist' file.
218 if (open (DEFAULT_DIRLIST, $default_dirlist))
220 while (<DEFAULT_DIRLIST>)
224 # strip off newlines and end-of-line comments
226 chomp ($contents=$_);
229 push (@dirlist, $contents);
232 close (DEFAULT_DIRLIST);
238 ################################################################
240 sub scan_configure ()
242 if (! open (CONFIGURE, $configure_ac))
244 print STDERR "aclocal: couldn't open `$configure_ac': $!\n";
248 my $mtime = mtime $configure_ac;
249 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
251 # Make sure we include acinclude.m4 if it exists.
252 if (-f 'acinclude.m4')
254 &add_file ('acinclude.m4');
259 # Remove comments from current line.
263 # Search for things we know about. The "search" sub is
264 # constructed dynamically by scan_m4_files. The last
265 # parenthethical match makes sure we don't match things that
266 # look like macro assignments or AC_SUBSTs.
267 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
269 # Macro not found, but AM_ prefix found.
270 warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
278 ################################################################
280 # Check macros in acinclude.m4. If one is not used, warn.
281 sub check_acinclude ()
285 foreach $key (keys %map)
287 next unless $map{$key} eq 'acinclude.m4';
288 if (! $macro_seen{$key})
290 # FIXME: should print line number of acinclude.m4.
291 warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
296 ################################################################
298 # Scan all the installed m4 files and construct a map.
299 sub scan_m4_files (@)
301 local (@dirlist) = @_;
303 # First, scan acinclude.m4 if it exists.
304 if (-f 'acinclude.m4')
306 $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
310 foreach $m4dir (@dirlist)
312 if (! opendir (DIR, $m4dir))
314 print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
318 local ($file, $fullfile);
319 # We reverse the directory contents so that foo2.m4 gets
320 # used in preference to foo1.m4.
321 foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
323 # Only examine .m4 files.
324 next unless $file =~ /\.m4$/;
326 # Skip some files when running out of srcdir.
327 next if $file eq 'aclocal.m4';
329 $fullfile = $m4dir . '/' . $file;
330 $file_contents{$fullfile} = &scan_file ($fullfile);
335 # Construct a new function that does the searching. We use a
336 # function (instead of just evalling $search in the loop) so that
337 # "die" is correctly and easily propagated if run.
338 my $search = "sub search {\nmy \$found = 0;\n";
339 foreach my $key (reverse sort keys %map)
341 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
342 . '"); $found = 1; }' . "\n");
344 $search .= "return \$found;\n};\n";
346 die "internal error: $@\n search is $search" if $@;
349 ################################################################
351 # Add a macro to the output.
356 # We want to ignore AC_ macros. However, if an AC_ macro is
357 # defined in (eg) acinclude.m4, then we want to make sure we mark
359 return if $macro =~ /^AC_/ && ! defined $map{$macro};
361 if (! defined $map{$macro})
363 warn "aclocal: macro `$macro' required but not defined\n";
368 print STDERR "aclocal: saw macro $macro\n" if $verbose;
369 $macro_seen{$macro} = 1;
370 &add_file ($map{$macro});
373 # Add a file to output.
378 # Only add a file once.
379 return if ($file_seen{$file});
380 $file_seen{$file} = 1;
382 my $mtime = mtime $file;
383 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
385 # If the file to add looks like path outside the project,
386 # copy it to the output.
387 # The regex catches filenames starting with things like
388 # / \ c:\ ../ ./../ etc.
389 if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
391 $output .= $file_contents{$file} . "\n";
395 # Otherwise, simply include the file.
396 $output .= "m4_include([$file])\n";
399 foreach (split ("\n", $file_contents{$file}))
401 # Remove comments from current line.
405 if (/$ac_require_rx/g)
407 push (@rlist, $1 || $2);
410 # The search function is constructed dynamically by
411 # scan_m4_files. The last parenthethical match makes sure we
412 # don't match things that look like macro assignments or
414 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
416 # Macro not found, but AM_ prefix found.
417 warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
423 foreach $macro (@rlist)
429 # Scan a single M4 file. Return contents.
434 unshift @file_order, $file;
436 my $fh = new Automake::XFile $file;
438 while ($_ = $fh->getline)
447 if (! defined $map{$1 || $2})
449 print STDERR "aclocal: found macro $1 in $file: $.\n"
451 $map{$1 || $2} = $file;
455 # Note: we used to give an error here if we saw a
456 # duplicated macro. However, this turns out to be
457 # extremely unpopular. It causes actual problems which
458 # are hard to work around, especially when you must
459 # mix-and-match tool versions.
460 print STDERR "aclocal: ignoring macro $1 in $file: $.\n"
469 sub trace_used_macros ($)
473 my %files = map { $map{$_} => 1 } keys %macro_seen;
475 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
476 $traces .= " --language Autoconf-without-aclocal-m4 $filename ";
477 # All candidate files.
478 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
479 # All candidate macros.
480 $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
482 print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
484 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
488 while ($_ = $tracefh->getline)
491 $traced{$_} = 1 if $macro_seen{$_};
496 ################################################################
499 sub write_aclocal ($@)
501 my ($output_file, @macros) = @_;
504 my %files = map { $map{$_} => 1 } @macros;
505 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
507 for $file (grep { exists $files{$_} } @file_order)
509 my $mtime = mtime $file;
510 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
512 # If the file to add looks like path outside the project,
513 # copy it to the output.
514 # The regex catches filenames starting with things like
515 # / \ c:\ ../ ./../ etc.
516 if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
518 $output .= $file_contents{$file} . "\n";
522 # Otherwise, simply include the file.
523 $output .= "m4_include([$file])\n";
527 # Nothing to output?!
528 # FIXME: Shouldn't we diagnose this?
529 return if ! length ($output);
531 # We used to print `# $output_file generated automatically etc.' But
532 # this creates spurious differences when using autoreconf. Autoreconf
533 # creates aclocal.m4t and then rename it to aclocal.m4, but the
534 # rebuild rules generated by Automake create aclocal.m4 directly --
535 # this would gives two ways to get the same file, with a different
536 # name in the header.
537 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
539 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
540 # Free Software Foundation, Inc.
541 # This file is free software; the Free Software Foundation
542 # gives unlimited permission to copy and/or distribute it,
543 # with or without modifications, as long as this notice is preserved.
545 # This program is distributed in the hope that it will be useful,
546 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
547 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
548 # PARTICULAR PURPOSE.
552 # We try not to update $output_file unless necessary, because
553 # doing so invalidate Autom4te's cache and therefore slows down
554 # tools called after aclocal.
556 # We need to overwrite $output_file in the following situations.
557 # * The --force option is in use.
558 # * One of the dependencies is younger.
559 # (Not updating $output_file in this situation would cause
560 # make to call aclocal in loop.)
561 # * The contents of the current file are different from what
564 && $greatest_mtime < mtime ($output_file)
565 && $output eq contents ($output_file))
567 print STDERR "aclocal: $output_file unchanged\n" if $verbose;
571 print STDERR "aclocal: writing $output_file\n" if $verbose;
573 my $out = new Automake::XFile "> $output_file";
578 ### Setup "GNU" style for perl-mode and cperl-mode.
580 ## perl-indent-level: 2
581 ## perl-continued-statement-offset: 2
582 ## perl-continued-brace-offset: 0
583 ## perl-brace-offset: 0
584 ## perl-brace-imaginary-offset: 0
585 ## perl-label-offset: -2
586 ## cperl-indent-level: 2
587 ## cperl-brace-offset: 0
588 ## cperl-continued-brace-offset: 0
589 ## cperl-label-offset: -2
590 ## cperl-extra-newline-before-brace: t
591 ## cperl-merge-trailing-else: nil
592 ## cperl-continued-statement-offset: 2