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, 2005
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;
52 # Include paths for searching macros. We search macros in this order:
53 # user-supplied directories first, then the directory containing the
54 # automake macros, and finally the system-wide directories for
55 # third-party macro. @user_includes can be augmented with -I.
56 # @system_includes can be augmented with the `dirlist' file. Also
57 # --acdir will reset both @automake_includes and @system_includes.
58 my @user_includes = ();
59 my @automake_includes = ("@datadir@/aclocal-$APIVERSION");
60 my @system_includes = ('@datadir@/aclocal');
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 # Remember the order into which we scanned the files.
78 # It's important to output the contents of aclocal.m4 in the opposite order.
79 # (Definitions in first files we have scanned should override those from
80 # later files. So they must appear last in the output.)
83 # Map macro names to file names.
86 # Ditto, but records the last definition of each macro as returned by --trace.
87 my %map_traced_defs = ();
89 # Map file names to file contents.
90 my %file_contents = ();
92 # Map file names to file types.
94 use constant FT_USER => 1;
95 use constant FT_AUTOMAKE => 2;
96 use constant FT_SYSTEM => 3;
98 # Map file names to included files (transitively closed).
99 my %file_includes = ();
101 # Matches a macro definition.
102 # AC_DEFUN([macroname], ...)
104 # AC_DEFUN(macroname, ...)
105 # When macroname is `['-quoted , we accept any character in the name,
106 # except `]'. Otherwise macroname stops on the first `]', `,', `)',
107 # or `\n' encountered.
109 "(?:A[CU]_DEFUN|AC_DEFUN_ONCE)\\((?:\\[([^]]+)\\]|([^],)\n]+))";
111 # Matches an AC_REQUIRE line.
112 my $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
114 # Matches an m4_include line
115 my $m4_include_rx = "(?:m4_)?s?include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
118 ################################################################
120 # Check macros in acinclude.m4. If one is not used, warn.
121 sub check_acinclude ()
123 foreach my $key (keys %map)
125 # FIXME: should print line number of acinclude.m4.
126 msg ('syntax', "warning: macro `$key' defined in "
127 . "acinclude.m4 but never used")
128 if $map{$key} eq 'acinclude.m4' && ! exists $macro_seen{$key};
132 ################################################################
134 # scan_m4_dirs($TYPE, @DIRS)
135 # --------------------------
136 # Scan all M4 files installed in @DIRS for new macro definitions.
137 # Register each file as of type $TYPE (one of the FT_* constants).
138 sub scan_m4_dirs ($@)
140 my ($type, @dirlist) = @_;
142 foreach my $m4dir (@dirlist)
144 if (! opendir (DIR, $m4dir))
146 fatal "couldn't open directory `$m4dir': $!";
149 # We reverse the directory contents so that foo2.m4 gets
150 # used in preference to foo1.m4.
151 foreach my $file (reverse sort grep (! /^\./, readdir (DIR)))
153 # Only examine .m4 files.
154 next unless $file =~ /\.m4$/;
156 # Skip some files when running out of srcdir.
157 next if $file eq 'aclocal.m4';
159 my $fullfile = File::Spec->canonpath ("$m4dir/$file");
160 &scan_file ($type, $fullfile, 'aclocal');
166 # Scan all the installed m4 files and construct a map.
169 # First, scan configure.ac. It may contain macro definitions,
170 # or may include other files that define macros.
171 &scan_file (FT_USER, $configure_ac, 'aclocal');
173 # Then, scan acinclude.m4 if it exists.
174 if (-f 'acinclude.m4')
176 &scan_file (FT_USER, 'acinclude.m4', 'aclocal');
179 # Finally, scan all files in our search paths.
180 scan_m4_dirs (FT_USER, @user_includes);
181 scan_m4_dirs (FT_AUTOMAKE, @automake_includes);
182 scan_m4_dirs (FT_SYSTEM, @system_includes);
184 # Construct a new function that does the searching. We use a
185 # function (instead of just evaluating $search in the loop) so that
186 # "die" is correctly and easily propagated if run.
187 my $search = "sub search {\nmy \$found = 0;\n";
188 foreach my $key (reverse sort keys %map)
190 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
191 . '"); $found = 1; }' . "\n");
193 $search .= "return \$found;\n};\n";
195 prog_error "$@\n search is $search" if $@;
198 ################################################################
200 # Add a macro to the output.
205 # Ignore unknown required macros. Either they are not really
206 # needed (e.g., a conditional AC_REQUIRE), in which case aclocal
207 # should be quiet, or they are needed and Autoconf itself will
208 # complain when we trace for macro usage later.
209 return unless defined $map{$macro};
211 verb "saw macro $macro";
212 $macro_seen{$macro} = 1;
213 &add_file ($map{$macro});
216 # scan_configure_dep ($file)
217 # --------------------------
218 # Scan a configure dependency (configure.ac, or separate m4 files)
219 # for uses of know macros and AC_REQUIREs of possibly unknown macros.
220 # Recursively scan m4_included files.
221 my %scanned_configure_dep = ();
222 sub scan_configure_dep ($)
225 # Do not scan a file twice.
227 if exists $scanned_configure_dep{$file};
228 $scanned_configure_dep{$file} = 1;
230 my $mtime = mtime $file;
231 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
233 my $contents = exists $file_contents{$file} ?
234 $file_contents{$file} : contents $file;
239 foreach (split ("\n", $contents))
242 # Remove comments from current line.
246 while (/$m4_include_rx/go)
248 push (@ilist, $1 || $2);
251 while (/$ac_require_rx/go)
253 push (@rlist, $1 || $2);
256 # The search function is constructed dynamically by
257 # scan_m4_files. The last parenthetical match makes sure we
258 # don't match things that look like macro assignments or
260 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
262 # Macro not found, but AM_ prefix found.
263 # Make this just a warning, because we do not know whether
264 # the macro is actually used (it could be called conditionally).
265 msg ('unsupported', "$file:$line",
266 "warning: macro `$2' not found in library");
270 add_macro ($_) foreach (@rlist);
271 my $dirname = dirname $file;
272 &scan_configure_dep (File::Spec->rel2abs ($_, $dirname)) foreach (@ilist);
277 # Add $FILE to output.
278 my %file_added = (); # files which have already been added.
283 # Only add a file once.
284 return if ($file_added{$file});
285 $file_added{$file} = 1;
287 scan_configure_dep $file;
290 # Point to the documentation for underquoted AC_DEFUN only once.
291 my $underquoted_manual_once = 0;
293 # scan_file ($TYPE, $FILE, $WHERE)
294 # -------------------------
295 # Scan a single M4 file ($FILE), and all files it includes.
296 # Return the list of included files.
297 # $TYPE is one of FT_USER, FT_AUTOMAKE, or FT_SYSTEM, depending
298 # on where the file comes from.
299 # $WHERE is the location to use in the diagnostic if the file
303 my ($type, $file, $where) = @_;
304 my $base = dirname $file;
306 # Do not scan the same file twice.
307 return @{$file_includes{$file}} if exists $file_includes{$file};
308 # Prevent potential infinite recursion (if two files include each other).
309 return () if exists $file_contents{$file};
311 unshift @file_order, $file;
313 $file_type{$file} = $type;
315 fatal "$where: file `$file' does not exist" if ! -e $file;
317 my $fh = new Automake::XFile $file;
321 while ($_ = $fh->getline)
329 while ($line =~ /$ac_defun_rx/go)
333 msg ('syntax', "$file:$.", "warning: underquoted definition of $2"
334 . "\n run info '(automake)Extending aclocal'\n"
335 . " or see http://sources.redhat.com/automake/"
336 . "automake.html#Extending-aclocal")
337 unless $underquoted_manual_once;
338 $underquoted_manual_once = 1;
340 my $macro = $1 || $2;
341 if (! defined $map{$macro})
343 verb "found macro $macro in $file: $.";
344 $map{$macro} = $file;
348 # Note: we used to give an error here if we saw a
349 # duplicated macro. However, this turns out to be
350 # extremely unpopular. It causes actual problems which
351 # are hard to work around, especially when you must
352 # mix-and-match tool versions.
353 verb "ignoring macro $macro in $file: $.";
357 while ($line =~ /$m4_include_rx/go)
359 my $ifile = $1 || $2;
360 # m4_include is relative to the directory of the file which
361 # perform the include, but we want paths relative to the
362 # directory where aclocal is run. Do not use
363 # File::Spec->rel2abs, because we want to store relative
364 # paths (they might be used later of aclocal outputs an
365 # m4_include for this file, or if the user itself includes
367 $ifile = "$base/$ifile"
368 unless $base eq '.' || File::Spec->file_name_is_absolute ($ifile);
369 push (@inc_files, $ifile);
370 $inc_lines{$ifile} = $.;
373 $file_contents{$file} = $contents;
375 # For some reason I don't understand, it does not work
376 # to do `map { scan_file ($_, ...) } @inc_files' below.
377 # With Perl 5.8.2 it undefines @inc_files.
378 my @copy = @inc_files;
379 my @all_inc_files = (@inc_files,
380 map { scan_file ($type, $_,
381 "$file:$inc_lines{$_}") } @copy);
382 $file_includes{$file} = \@all_inc_files;
383 return @all_inc_files;
386 # strip_redundant_includes (%FILES)
387 # ---------------------------------
388 # Each key in %FILES is a file that must be present in the output.
389 # However some of these files might already include other files in %FILES,
390 # so there is no point in including them another time.
391 # This removes items of %FILES which are already included by another file.
392 sub strip_redundant_includes (%)
395 # Files at the end of @file_order should override those at the beginning,
396 # so it is important to preserve these trailing files. We can remove
397 # a file A if it is going to be output before a file B that includes
398 # file A, not the converse.
399 foreach my $file (reverse @file_order)
401 next unless exists $files{$file};
402 foreach my $ifile (@{$file_includes{$file}})
404 next unless exists $files{$ifile};
405 delete $files{$ifile};
406 verb "$ifile is already included by $file";
412 sub trace_used_macros ()
414 my %files = map { $map{$_} => 1 } keys %macro_seen;
415 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
416 %files = strip_redundant_includes %files;
417 # configure.ac is implicitly included.
418 delete $files{$configure_ac};
420 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
421 $traces .= " --language Autoconf-without-aclocal-m4 ";
422 # All candidate files.
423 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
424 # All candidate macros.
425 $traces .= join (' ',
426 (map { "--trace='$_:\$f::\$n::\$1'" } ('AC_DEFUN',
429 # Do not trace $1 for all other macros as we do
430 # not need it and it might contains harmful
431 # characters (like newlines).
432 (map { "--trace='$_:\$f::\$n'" } (keys %macro_seen)));
434 verb "running $traces $configure_ac";
436 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
440 while ($_ = $tracefh->getline)
443 my ($file, $macro, $arg1) = split (/::/);
445 $traced{$macro} = 1 if exists $macro_seen{$macro};
447 $map_traced_defs{$arg1} = $file
448 if ($macro eq 'AC_DEFUN'
449 || $macro eq 'AC_DEFUN_ONCE'
450 || $macro eq 'AU_DEFUN');
458 sub scan_configure ()
460 # Make sure we include acinclude.m4 if it exists.
461 if (-f 'acinclude.m4')
463 add_file ('acinclude.m4');
465 scan_configure_dep ($configure_ac);
468 ################################################################
471 sub write_aclocal ($@)
473 my ($output_file, @macros) = @_;
477 # Get the list of files containing definitions for the macros used.
478 # (Filter out unused macro definitions with $map_traced_defs. This
479 # can happen when an Autoconf macro is conditionally defined:
480 # aclocal sees the potential definition, but this definition is
481 # actually never processed and the Autoconf implementation is used
486 if (exists $map_traced_defs{$m}
487 && $map{$m} eq $map_traced_defs{$m});
489 # Always include acinclude.m4, even if it does not appear to be used.
490 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
491 # Do not explicitly include a file that is already indirectly included.
492 %files = strip_redundant_includes %files;
493 # Never include configure.ac :)
494 delete $files{$configure_ac};
496 for my $file (grep { exists $files{$_} } @file_order)
498 # Check the time stamp of this file, and of all files it includes.
499 for my $ifile ($file, @{$file_includes{$file}})
501 my $mtime = mtime $ifile;
502 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
505 # If the file to add looks like outside the project, copy it
506 # to the output. The regex catches filenames starting with
507 # things like `/', `\', or `c:\'.
508 if ($file_type{$file} != FT_USER
509 || $file =~ m,^(?:\w:)?[\\/],)
511 $output .= $file_contents{$file} . "\n";
515 # Otherwise, simply include the file.
516 $output .= "m4_include([$file])\n";
520 # Nothing to output?!
521 # FIXME: Shouldn't we diagnose this?
522 return if ! length ($output);
524 # We used to print `# $output_file generated automatically etc.' But
525 # this creates spurious differences when using autoreconf. Autoreconf
526 # creates aclocal.m4t and then rename it to aclocal.m4, but the
527 # rebuild rules generated by Automake create aclocal.m4 directly --
528 # this would gives two ways to get the same file, with a different
529 # name in the header.
530 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
532 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
533 # 2005 Free Software Foundation, Inc.
534 # This file is free software; the Free Software Foundation
535 # gives unlimited permission to copy and/or distribute it,
536 # with or without modifications, as long as this notice is preserved.
538 # This program is distributed in the hope that it will be useful,
539 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
540 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
541 # PARTICULAR PURPOSE.
545 # We try not to update $output_file unless necessary, because
546 # doing so invalidate Autom4te's cache and therefore slows down
547 # tools called after aclocal.
549 # We need to overwrite $output_file in the following situations.
550 # * The --force option is in use.
551 # * One of the dependencies is younger.
552 # (Not updating $output_file in this situation would cause
553 # make to call aclocal in loop.)
554 # * The contents of the current file are different from what
557 && $greatest_mtime < mtime ($output_file)
558 && $output eq contents ($output_file))
560 verb "$output_file unchanged";
564 verb "writing $output_file";
566 my $out = new Automake::XFile "> $output_file";
571 ################################################################
573 # Print usage and exit.
578 print "Usage: aclocal [OPTIONS] ...
580 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
583 --acdir=DIR directory holding config files (for debugging)
584 --force always update output file
585 --help print this help, then exit
586 -I DIR add directory to search list for .m4 files
587 --output=FILE put output in FILE (default aclocal.m4)
588 --print-ac-dir print name of directory holding m4 files, then exit
589 --verbose don't be silent
590 --version print version number, then exit
591 -W, --warnings=CATEGORY report the warnings falling in CATEGORY
593 Warning categories include:
594 `syntax' dubious syntactic constructs (default)
595 `unsupported' unknown macros (default)
596 `all' all the warnings (default)
597 `no-CATEGORY' turn off warnings in CATEGORY
598 `none' turn off all the warnings
599 `error' treat warnings as errors
601 Report bugs to <bug-automake\@gnu.org>.\n";
606 # Print version and exit.
610 aclocal (GNU $PACKAGE) $VERSION
611 Written by Tom Tromey <tromey\@redhat.com>
612 and Alexandre Duret-Lutz <adl\@gnu.org>.
614 Copyright (C) 2005 Free Software Foundation, Inc.
615 This is free software; see the source for copying conditions. There is NO
616 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
621 # Parse command line.
622 sub parse_arguments ()
624 my $print_and_exit = 0;
628 'acdir=s' => sub # Setting --acdir overrides both the
629 { # automake (versioned) directory and the
630 # public (unversioned) system directory.
631 @automake_includes = ();
632 @system_includes = ($_[1])
634 'force' => \$force_output,
635 'I=s' => \@user_includes,
636 'output=s' => \$output_file,
637 'print-ac-dir' => \$print_and_exit,
638 'verbose' => sub { setup_channel 'verb', silent => 0; },
639 'W|warnings=s' => \&parse_warnings,
642 Getopt::Long::config ("bundling", "pass_through");
644 # See if --version or --help is used. We want to process these before
645 # anything else because the GNU Coding Standards require us to
646 # `exit 0' after processing these options, and we can't guarantee this
647 # if we treat other options first. (Handling other options first
648 # could produce error diagnostics, and in this condition it is
649 # confusing if aclocal does `exit 0'.)
650 my %cli_options_1st_pass =
652 'version' => \&version,
653 'help' => sub { usage(0); },
654 # Recognize all other options (and their arguments) but do nothing.
655 map { $_ => sub {} } (keys %cli_options)
657 my @ARGV_backup = @ARGV;
658 Getopt::Long::GetOptions %cli_options_1st_pass
660 @ARGV = @ARGV_backup;
662 # Now *really* process the options. This time we know that --help
663 # and --version are not present, but we specify them nonetheless so
664 # that ambiguous abbreviation are diagnosed.
665 Getopt::Long::GetOptions %cli_options, 'version' => sub {}, 'help' => sub {}
671 for my $k (keys %cli_options)
675 map { $argopts{(length ($_) == 1)
676 ? "-$_" : "--$_" } = 1; } (split (/\|/, $1));
679 if (exists $argopts{$ARGV[0]})
681 fatal ("option `$ARGV[0]' requires an argument\n"
682 . "Try `$0 --help' for more information.");
686 fatal ("unrecognized option `$ARGV[0]'\n"
687 . "Try `$0 --help' for more information.");
693 print "@system_includes\n";
697 if (! -d $system_includes[0])
699 # By default $(datadir)/aclocal doesn't exist. We don't want to
700 # get an error in the case where we are searching the default
701 # directory and it hasn't been created. (We know
702 # @system_includes has its default value if @automake_includes
703 # is not empty, because --acdir is the only way to change this.)
704 @system_includes = () if @automake_includes;
708 # Finally, adds any directory listed in the `dirlist' file.
709 if (open (DIRLIST, "$system_includes[0]/dirlist"))
715 # strip off newlines and end-of-line comments
718 push (@system_includes, $_) if -d $_;
725 ################################################################
727 parse_WARNINGS; # Parse the WARNINGS environment variable.
729 $configure_ac = require_configure_ac;
734 my %macro_traced = trace_used_macros;
735 write_aclocal ($output_file, keys %macro_traced);
741 ### Setup "GNU" style for perl-mode and cperl-mode.
743 ## perl-indent-level: 2
744 ## perl-continued-statement-offset: 2
745 ## perl-continued-brace-offset: 0
746 ## perl-brace-offset: 0
747 ## perl-brace-imaginary-offset: 0
748 ## perl-label-offset: -2
749 ## cperl-indent-level: 2
750 ## cperl-brace-offset: 0
751 ## cperl-continued-brace-offset: 0
752 ## cperl-label-offset: -2
753 ## cperl-extra-newline-before-brace: t
754 ## cperl-merge-trailing-else: nil
755 ## cperl-continued-statement-offset: 2