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, 2004
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>, and
29 # Alexandre Duret-Lutz <adl@gnu.org>.
33 my $perllibdir = $ENV{'perllibdir'} || '@datadir@/@PACKAGE@-@APIVERSION@';
34 unshift @INC, (split '@PATH_SEPARATOR@', $perllibdir);
40 use Automake::General;
41 use Automake::Configure_ac;
42 use Automake::Channels;
43 use Automake::ChannelDefs;
45 use Automake::FileUtils;
50 # Note that this isn't pkgdatadir, but a separate directory.
51 # Note also that the versioned directory is handled later.
52 my $acdir = '@datadir@/aclocal';
53 my $default_acdir = $acdir;
54 # contains a list of directories, one per line, to be added
55 # to the dirlist in addition to $acdir, as if -I had been
56 # added to the command line. If acdir has been redirected,
57 # we will also check the specified acdir (this is done later).
58 my $default_dirlist = "$default_acdir/dirlist";
62 # configure.ac or configure.in.
66 my $output_file = 'aclocal.m4';
68 # Modification time of the youngest dependency.
69 my $greatest_mtime = 0;
74 # Which macros have been seen.
77 # Which files have been seen.
80 # Remember the order into which we scanned the files.
81 # It's important to output the contents of aclocal.m4 in the opposite order.
82 # (Definitions in first files we have scanned should override those from
83 # later files. So they must appear last in the output.)
86 # Map macro names to file names.
89 # Ditto, but records the last definition of each macro as returned by --trace.
90 my %map_traced_defs = ();
92 # Map file names to file contents.
93 my %file_contents = ();
95 # Map file names to included files (transitively closed).
96 my %file_includes = ();
98 # Matches a macro definition.
99 # AC_DEFUN([macroname], ...)
101 # AC_DEFUN(macroname, ...)
102 # When macroname is `['-quoted , we accept any character in the name,
103 # except `]'. Otherwise macroname stops on the first `]', `,', `)',
104 # or `\n' encountered.
106 "(?:A[CU]_DEFUN|AC_DEFUN_ONCE)\\((?:\\[([^]]+)\\]|([^],)\n]+))";
108 # Matches an AC_REQUIRE line.
109 my $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
111 # Matches an m4_include line
112 my $m4_include_rx = "(?:m4_)?s?include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
115 ################################################################
117 # Check macros in acinclude.m4. If one is not used, warn.
118 sub check_acinclude ()
120 foreach my $key (keys %map)
122 # FIXME: should print line number of acinclude.m4.
123 msg ('syntax', "warning: macro `$key' defined in "
124 . "acinclude.m4 but never used")
125 if $map{$key} eq 'acinclude.m4' && ! exists $macro_seen{$key};
129 ################################################################
131 # Scan all the installed m4 files and construct a map.
132 sub scan_m4_files (@)
136 # First, scan configure.ac. It may contain macro definitions,
137 # or may include other files that define macros.
138 &scan_file ($configure_ac, 'aclocal');
140 # Then, scan acinclude.m4 if it exists.
141 if (-f 'acinclude.m4')
143 &scan_file ('acinclude.m4', 'aclocal');
146 # Finally, scan all files in our search path.
147 foreach my $m4dir (@dirlist)
149 if (! opendir (DIR, $m4dir))
151 fatal "couldn't open directory `$m4dir': $!";
154 # We reverse the directory contents so that foo2.m4 gets
155 # used in preference to foo1.m4.
156 foreach my $file (reverse sort grep (! /^\./, readdir (DIR)))
158 # Only examine .m4 files.
159 next unless $file =~ /\.m4$/;
161 # Skip some files when running out of srcdir.
162 next if $file eq 'aclocal.m4';
164 my $fullfile = File::Spec->canonpath ("$m4dir/$file");
165 &scan_file ($fullfile, 'aclocal');
170 # Construct a new function that does the searching. We use a
171 # function (instead of just evaluating $search in the loop) so that
172 # "die" is correctly and easily propagated if run.
173 my $search = "sub search {\nmy \$found = 0;\n";
174 foreach my $key (reverse sort keys %map)
176 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
177 . '"); $found = 1; }' . "\n");
179 $search .= "return \$found;\n};\n";
181 prog_error "$@\n search is $search" if $@;
184 ################################################################
186 # Add a macro to the output.
191 # Ignore unknown required macros. Either they are not really
192 # needed (e.g., a conditional AC_REQUIRE), in which case aclocal
193 # should be quiet, or they are needed and Autoconf itself will
194 # complain when we trace for macro usage later.
195 return unless defined $map{$macro};
197 verb "saw macro $macro";
198 $macro_seen{$macro} = 1;
199 &add_file ($map{$macro});
202 # scan_configure_dep ($file)
203 # --------------------------
204 # Scan a configure dependency (configure.ac, or separate m4 files)
205 # for uses of know macros and AC_REQUIREs of possibly unknown macros.
206 # Recursively scan m4_included files.
207 my %scanned_configure_dep = ();
208 sub scan_configure_dep ($)
211 # Do not scan a file twice.
213 if exists $scanned_configure_dep{$file};
214 $scanned_configure_dep{$file} = 1;
216 my $mtime = mtime $file;
217 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
219 my $contents = exists $file_contents{$file} ?
220 $file_contents{$file} : contents $file;
225 foreach (split ("\n", $contents))
228 # Remove comments from current line.
232 while (/$m4_include_rx/go)
234 push (@ilist, $1 || $2);
237 while (/$ac_require_rx/go)
239 push (@rlist, $1 || $2);
242 # The search function is constructed dynamically by
243 # scan_m4_files. The last parenthetical match makes sure we
244 # don't match things that look like macro assignments or
246 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
248 # Macro not found, but AM_ prefix found.
249 # Make this just a warning, because we do not know whether
250 # the macro is actually used (it could be called conditionally).
251 msg ('unsupported', "$file:$line",
252 "warning: macro `$2' not found in library");
256 add_macro ($_) foreach (@rlist);
257 my $dirname = dirname $file;
258 &scan_configure_dep (File::Spec->rel2abs ($_, $dirname)) foreach (@ilist);
261 # Add a file to output.
266 # Only add a file once.
267 return if ($file_seen{$file});
268 $file_seen{$file} = 1;
270 scan_configure_dep $file;
273 # Point to the documentation for underquoted AC_DEFUN only once.
274 my $underquoted_manual_once = 0;
276 # scan_file ($FILE, $WHERE)
277 # -------------------------
278 # Scan a single M4 file ($FILE), and all files it includes.
279 # Return the list of included files.
280 # $WHERE is the location to use in the diagnostic if the file
284 my ($file, $where) = @_;
285 my $base = dirname $file;
287 # Do not scan the same file twice.
288 return @{$file_includes{$file}} if exists $file_includes{$file};
289 # Prevent potential infinite recursion (if two files include each other).
290 return () if exists $file_contents{$file};
292 unshift @file_order, $file;
294 fatal "$where: file `$file' does not exist" if ! -e $file;
296 my $fh = new Automake::XFile $file;
300 while ($_ = $fh->getline)
308 while ($line =~ /$ac_defun_rx/go)
312 msg ('syntax', "$file:$.", "warning: underquoted definition of $2"
313 . "\n run info '(automake)Extending aclocal'\n"
314 . " or see http://sources.redhat.com/automake/"
315 . "automake.html#Extending-aclocal")
316 unless $underquoted_manual_once;
317 $underquoted_manual_once = 1;
319 my $macro = $1 || $2;
320 if (! defined $map{$macro})
322 verb "found macro $macro in $file: $.";
323 $map{$macro} = $file;
327 # Note: we used to give an error here if we saw a
328 # duplicated macro. However, this turns out to be
329 # extremely unpopular. It causes actual problems which
330 # are hard to work around, especially when you must
331 # mix-and-match tool versions.
332 verb "ignoring macro $macro in $file: $.";
336 while ($line =~ /$m4_include_rx/go)
338 my $ifile = $1 || $2;
339 # m4_include is relative to the directory of the file which
340 # perform the include, but we want paths relative to the
341 # directory where aclocal is run. Do not use
342 # File::Spec->rel2abs, because we want to store relative
343 # paths (they might be used later of aclocal outputs an
344 # m4_include for this file, or if the user itself includes
346 $ifile = "$base/$ifile"
347 unless $base eq '.' || File::Spec->file_name_is_absolute ($ifile);
348 push (@inc_files, $ifile);
349 $inc_lines{$ifile} = $.;
352 $file_contents{$file} = $contents;
354 # For some reason I don't understand, it does not work
355 # to do `map { scan_file ($_, ...) } @inc_files' below.
356 # With Perl 5.8.2 it undefines @inc_files.
357 my @copy = @inc_files;
358 my @all_inc_files = (@inc_files,
359 map { scan_file ($_, "$file:$inc_lines{$_}") } @copy);
360 $file_includes{$file} = \@all_inc_files;
361 return @all_inc_files;
364 # strip_redundant_includes (%FILES)
365 # ---------------------------------
366 # Each key in %FILES is a file that must be present in the output.
367 # However some of these files might already include other files in %FILES,
368 # so there is no point in including them another time.
369 # This removes items of %FILES which are already included by another file.
370 sub strip_redundant_includes (%)
373 # Files at the end of @file_order should override those at the beginning,
374 # so it is important to preserve these trailing files. We can remove
375 # a file A if it is going to be output before a file B that includes
376 # file A, not the converse.
377 foreach my $file (reverse @file_order)
379 next unless exists $files{$file};
380 foreach my $ifile (@{$file_includes{$file}})
382 next unless exists $files{$ifile};
383 delete $files{$ifile};
384 verb "$ifile is already included by $file";
390 sub trace_used_macros ()
392 my %files = map { $map{$_} => 1 } keys %macro_seen;
393 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
394 %files = strip_redundant_includes %files;
395 # configure.ac is implicitly included.
396 delete $files{$configure_ac};
398 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
399 $traces .= " --language Autoconf-without-aclocal-m4 ";
400 # All candidate files.
401 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
402 # All candidate macros.
403 $traces .= join (' ',
404 (map { "--trace='$_:\$f::\$n::\$1'" } ('AC_DEFUN',
407 # Do not trace $1 for all other macros as we do
408 # not need it and it might contains harmful
409 # characters (like newlines).
410 (map { "--trace='$_:\$f::\$n'" } (keys %macro_seen)));
412 verb "running $traces $configure_ac";
414 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
418 while ($_ = $tracefh->getline)
421 my ($file, $macro, $arg1) = split (/::/);
423 $traced{$macro} = 1 if exists $macro_seen{$macro};
425 $map_traced_defs{$arg1} = $file
426 if ($macro eq 'AC_DEFUN'
427 || $macro eq 'AC_DEFUN_ONCE'
428 || $macro eq 'AU_DEFUN');
436 sub scan_configure ()
438 # Make sure we include acinclude.m4 if it exists.
439 if (-f 'acinclude.m4')
441 add_file ('acinclude.m4');
443 scan_configure_dep ($configure_ac);
446 ################################################################
449 sub write_aclocal ($@)
451 my ($output_file, @macros) = @_;
455 # Get the list of files containing definitions for the macros used.
456 # (Filter out unused macro definitions with $map_traced_defs. This
457 # can happen when an Autoconf macro is conditionally defined:
458 # aclocal sees the potential definition, but this definition is
459 # actually never processed and the Autoconf implementation is used
464 if (exists $map_traced_defs{$m}
465 && $map{$m} eq $map_traced_defs{$m});
467 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
468 %files = strip_redundant_includes %files;
469 delete $files{$configure_ac};
471 for my $file (grep { exists $files{$_} } @file_order)
473 # Check the time stamp of this file, and all files it includes.
474 for my $ifile ($file, @{$file_includes{$file}})
476 my $mtime = mtime $ifile;
477 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
480 # If the file to add looks like outside the project, copy it
481 # to the output. The regex catches filenames starting with
482 # things like `/', `\', or `c:\'.
483 if ($file =~ m,^(?:\w:)?[\\/],)
485 $output .= $file_contents{$file} . "\n";
489 # Otherwise, simply include the file.
490 $output .= "m4_include([$file])\n";
494 # Nothing to output?!
495 # FIXME: Shouldn't we diagnose this?
496 return if ! length ($output);
498 # We used to print `# $output_file generated automatically etc.' But
499 # this creates spurious differences when using autoreconf. Autoreconf
500 # creates aclocal.m4t and then rename it to aclocal.m4, but the
501 # rebuild rules generated by Automake create aclocal.m4 directly --
502 # this would gives two ways to get the same file, with a different
503 # name in the header.
504 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
506 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
507 # Free Software Foundation, Inc.
508 # This file is free software; the Free Software Foundation
509 # gives unlimited permission to copy and/or distribute it,
510 # with or without modifications, as long as this notice is preserved.
512 # This program is distributed in the hope that it will be useful,
513 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
514 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
515 # PARTICULAR PURPOSE.
519 # We try not to update $output_file unless necessary, because
520 # doing so invalidate Autom4te's cache and therefore slows down
521 # tools called after aclocal.
523 # We need to overwrite $output_file in the following situations.
524 # * The --force option is in use.
525 # * One of the dependencies is younger.
526 # (Not updating $output_file in this situation would cause
527 # make to call aclocal in loop.)
528 # * The contents of the current file are different from what
531 && $greatest_mtime < mtime ($output_file)
532 && $output eq contents ($output_file))
534 verb "$output_file unchanged";
538 verb "writing $output_file";
540 my $out = new Automake::XFile "> $output_file";
545 ################################################################
547 # Print usage and exit.
552 print "Usage: aclocal [OPTIONS] ...
554 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
557 --acdir=DIR directory holding config files (for debugging)
558 --force always update output file
559 --help print this help, then exit
560 -I DIR add directory to search list for .m4 files
561 --output=FILE put output in FILE (default aclocal.m4)
562 --print-ac-dir print name of directory holding m4 files, then exit
563 --verbose don't be silent
564 --version print version number, then exit
565 -W, --warnings=CATEGORY report the warnings falling in CATEGORY
567 Warning categories include:
568 `syntax' dubious syntactic constructs (default)
569 `unsupported' unknown macros (default)
570 `all' all the warnings (default)
571 `no-CATEGORY' turn off warnings in CATEGORY
572 `none' turn off all the warnings
573 `error' treat warnings as errors
575 Report bugs to <bug-automake\@gnu.org>.\n";
580 # Print version and exit.
584 aclocal (GNU $PACKAGE) $VERSION
585 Written by Tom Tromey <tromey\@redhat.com>
586 and Alexandre Duret-Lutz <adl\@gnu.org>
587 Copyright (C) 2004 Free Software Foundation, Inc.
588 This is free software; see the source for copying conditions. There is NO
589 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.";
593 # Parse command line.
594 sub parse_arguments ()
597 my $print_and_exit = 0;
601 'acdir=s' => \$acdir,
602 'force' => \$force_output,
604 'output=s' => \$output_file,
605 'print_ac_dir' => \$print_and_exit,
606 'verbose' => sub { setup_channel 'verb', silent => 0; },
607 'W|warnings:s' => \&parse_warnings,
610 Getopt::Long::config ("bundling", "pass_through");
612 # See if --version or --help is used. We want to process these before
613 # anything else because the GNU Coding Standards require us to
614 # `exit 0' after processing these options, and we can't guarantee this
615 # if we treat other options first. (Handling other options first
616 # could produce error diagnostics, and in this condition it is
617 # confusing if Automake does `exit 0'.)
618 my %cli_options_1st_pass =
620 'version' => \&version,
621 'help' => sub { usage(0); },
622 # Recognize all other options (and their arguments) but do nothing.
623 map { $_ => sub {} } (keys %cli_options)
625 my @ARGV_backup = @ARGV;
626 Getopt::Long::GetOptions %cli_options_1st_pass
628 @ARGV = @ARGV_backup;
630 # Now *really* process the options. This time we know
631 # that --help and --version are not present.
632 Getopt::Long::GetOptions %cli_options
637 fatal ("unrecognized option `$ARGV[0]'\n"
638 . "Try `$0 --help' for more information.");
647 $default_dirlist="$acdir/dirlist"
648 if $acdir ne $default_acdir;
650 # Search the versioned directory near the end, and then the
651 # unversioned directory last. Only do this if the user didn't
653 push (@dirlist, "$acdir-$APIVERSION")
654 if $acdir eq $default_acdir;
656 # By default $(datadir)/aclocal doesn't exist. We don't want to
657 # get an error in the case where we are searching the default
658 # directory and it hasn't been created.
659 push (@dirlist, $acdir)
660 unless $acdir eq $default_acdir && ! -d $acdir;
662 # Finally, adds any directory listed in the `dirlist' file.
663 if (open (DEFAULT_DIRLIST, $default_dirlist))
665 while (<DEFAULT_DIRLIST>)
669 # strip off newlines and end-of-line comments
672 push (@dirlist, $_) if -d $_;
674 close (DEFAULT_DIRLIST);
680 ################################################################
682 parse_WARNINGS; # Parse the WARNINGS environment variable.
683 my @dirlist = parse_arguments;
684 $configure_ac = require_configure_ac;
685 scan_m4_files (@dirlist);
689 my %macro_traced = trace_used_macros;
690 write_aclocal ($output_file, keys %macro_traced);
696 ### Setup "GNU" style for perl-mode and cperl-mode.
698 ## perl-indent-level: 2
699 ## perl-continued-statement-offset: 2
700 ## perl-continued-brace-offset: 0
701 ## perl-brace-offset: 0
702 ## perl-brace-imaginary-offset: 0
703 ## perl-label-offset: -2
704 ## cperl-indent-level: 2
705 ## cperl-brace-offset: 0
706 ## cperl-continued-brace-offset: 0
707 ## cperl-label-offset: -2
708 ## cperl-extra-newline-before-brace: t
709 ## cperl-merge-trailing-else: nil
710 ## cperl-continued-statement-offset: 2