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 # Whether we should copy M4 file in $user_includes[0].
65 # configure.ac or configure.in.
69 my $output_file = 'aclocal.m4';
74 # Modification time of the youngest dependency.
75 my $greatest_mtime = 0;
77 # Which macros 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 file types.
97 use constant FT_USER => 1;
98 use constant FT_AUTOMAKE => 2;
99 use constant FT_SYSTEM => 3;
101 # Map file names to included files (transitively closed).
102 my %file_includes = ();
104 # Files which have already been added.
107 # Files that have already been scanned.
108 my %scanned_configure_dep = ();
110 # Matches a macro definition.
111 # AC_DEFUN([macroname], ...)
113 # AC_DEFUN(macroname, ...)
114 # When macroname is `['-quoted , we accept any character in the name,
115 # except `]'. Otherwise macroname stops on the first `]', `,', `)',
116 # or `\n' encountered.
118 "(?:AU_ALIAS|A[CU]_DEFUN|AC_DEFUN_ONCE)\\((?:\\[([^]]+)\\]|([^],)\n]+))";
120 # Matches an AC_REQUIRE line.
121 my $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
123 # Matches an m4_include line
124 my $m4_include_rx = "(?:m4_)?s?include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
127 ################################################################
129 # Check macros in acinclude.m4. If one is not used, warn.
130 sub check_acinclude ()
132 foreach my $key (keys %map)
134 # FIXME: should print line number of acinclude.m4.
135 msg ('syntax', "warning: macro `$key' defined in "
136 . "acinclude.m4 but never used")
137 if $map{$key} eq 'acinclude.m4' && ! exists $macro_seen{$key};
147 %map_traced_defs = ();
152 %scanned_configure_dep = ();
156 # install_file ($SRC, $DEST)
157 sub install_file ($$)
159 my ($src, $dest) = @_;
162 || !exists $file_contents{$dest}
163 || $file_contents{$src} ne $file_contents{$dest})
165 if (system ('cp', $src, $dest))
167 error ("error while copying `$src' to `$dest'");
171 msg 'note', "installing `$dest'";
176 ################################################################
178 # scan_m4_dirs($TYPE, @DIRS)
179 # --------------------------
180 # Scan all M4 files installed in @DIRS for new macro definitions.
181 # Register each file as of type $TYPE (one of the FT_* constants).
182 sub scan_m4_dirs ($@)
184 my ($type, @dirlist) = @_;
186 foreach my $m4dir (@dirlist)
188 if (! opendir (DIR, $m4dir))
190 fatal "couldn't open directory `$m4dir': $!";
193 # We reverse the directory contents so that foo2.m4 gets
194 # used in preference to foo1.m4.
195 foreach my $file (reverse sort grep (! /^\./, readdir (DIR)))
197 # Only examine .m4 files.
198 next unless $file =~ /\.m4$/;
200 # Skip some files when running out of srcdir.
201 next if $file eq 'aclocal.m4';
203 my $fullfile = File::Spec->canonpath ("$m4dir/$file");
204 &scan_file ($type, $fullfile, 'aclocal');
210 # Scan all the installed m4 files and construct a map.
213 # First, scan configure.ac. It may contain macro definitions,
214 # or may include other files that define macros.
215 &scan_file (FT_USER, $configure_ac, 'aclocal');
217 # Then, scan acinclude.m4 if it exists.
218 if (-f 'acinclude.m4')
220 &scan_file (FT_USER, 'acinclude.m4', 'aclocal');
223 # Finally, scan all files in our search paths.
224 scan_m4_dirs (FT_USER, @user_includes);
225 scan_m4_dirs (FT_AUTOMAKE, @automake_includes);
226 scan_m4_dirs (FT_SYSTEM, @system_includes);
228 # Construct a new function that does the searching. We use a
229 # function (instead of just evaluating $search in the loop) so that
230 # "die" is correctly and easily propagated if run.
231 my $search = "sub search {\nmy \$found = 0;\n";
232 foreach my $key (reverse sort keys %map)
234 $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
235 . '"); $found = 1; }' . "\n");
237 $search .= "return \$found;\n};\n";
239 prog_error "$@\n search is $search" if $@;
242 ################################################################
244 # Add a macro to the output.
249 # Ignore unknown required macros. Either they are not really
250 # needed (e.g., a conditional AC_REQUIRE), in which case aclocal
251 # should be quiet, or they are needed and Autoconf itself will
252 # complain when we trace for macro usage later.
253 return unless defined $map{$macro};
255 verb "saw macro $macro";
256 $macro_seen{$macro} = 1;
257 &add_file ($map{$macro});
260 # scan_configure_dep ($file)
261 # --------------------------
262 # Scan a configure dependency (configure.ac, or separate m4 files)
263 # for uses of know macros and AC_REQUIREs of possibly unknown macros.
264 # Recursively scan m4_included files.
265 sub scan_configure_dep ($)
268 # Do not scan a file twice.
270 if exists $scanned_configure_dep{$file};
271 $scanned_configure_dep{$file} = 1;
273 my $mtime = mtime $file;
274 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
276 my $contents = exists $file_contents{$file} ?
277 $file_contents{$file} : contents $file;
282 foreach (split ("\n", $contents))
285 # Remove comments from current line.
289 while (/$m4_include_rx/go)
291 push (@ilist, $1 || $2);
294 while (/$ac_require_rx/go)
296 push (@rlist, $1 || $2);
299 # The search function is constructed dynamically by
300 # scan_m4_files. The last parenthetical match makes sure we
301 # don't match things that look like macro assignments or
303 if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
305 # Macro not found, but AM_ prefix found.
306 # Make this just a warning, because we do not know whether
307 # the macro is actually used (it could be called conditionally).
308 msg ('unsupported', "$file:$line",
309 "warning: macro `$2' not found in library");
313 add_macro ($_) foreach (@rlist);
314 my $dirname = dirname $file;
315 &scan_configure_dep (File::Spec->rel2abs ($_, $dirname)) foreach (@ilist);
320 # Add $FILE to output.
325 # Only add a file once.
326 return if ($file_added{$file});
327 $file_added{$file} = 1;
329 scan_configure_dep $file;
332 # Point to the documentation for underquoted AC_DEFUN only once.
333 my $underquoted_manual_once = 0;
335 # scan_file ($TYPE, $FILE, $WHERE)
336 # -------------------------
337 # Scan a single M4 file ($FILE), and all files it includes.
338 # Return the list of included files.
339 # $TYPE is one of FT_USER, FT_AUTOMAKE, or FT_SYSTEM, depending
340 # on where the file comes from.
341 # $WHERE is the location to use in the diagnostic if the file
345 my ($type, $file, $where) = @_;
346 my $base = dirname $file;
348 # Do not scan the same file twice.
349 return @{$file_includes{$file}} if exists $file_includes{$file};
350 # Prevent potential infinite recursion (if two files include each other).
351 return () if exists $file_contents{$file};
353 unshift @file_order, $file;
355 $file_type{$file} = $type;
357 fatal "$where: file `$file' does not exist" if ! -e $file;
359 my $fh = new Automake::XFile $file;
363 while ($_ = $fh->getline)
371 while ($line =~ /$ac_defun_rx/go)
375 msg ('syntax', "$file:$.", "warning: underquoted definition of $2"
376 . "\n run info '(automake)Extending aclocal'\n"
377 . " or see http://sources.redhat.com/automake/"
378 . "automake.html#Extending-aclocal")
379 unless $underquoted_manual_once;
380 $underquoted_manual_once = 1;
382 my $macro = $1 || $2;
383 if (! defined $map{$macro})
385 verb "found macro $macro in $file: $.";
386 $map{$macro} = $file;
390 # Note: we used to give an error here if we saw a
391 # duplicated macro. However, this turns out to be
392 # extremely unpopular. It causes actual problems which
393 # are hard to work around, especially when you must
394 # mix-and-match tool versions.
395 verb "ignoring macro $macro in $file: $.";
399 while ($line =~ /$m4_include_rx/go)
401 my $ifile = $1 || $2;
402 # m4_include is relative to the directory of the file which
403 # perform the include, but we want paths relative to the
404 # directory where aclocal is run. Do not use
405 # File::Spec->rel2abs, because we want to store relative
406 # paths (they might be used later of aclocal outputs an
407 # m4_include for this file, or if the user itself includes
409 $ifile = "$base/$ifile"
410 unless $base eq '.' || File::Spec->file_name_is_absolute ($ifile);
411 push (@inc_files, $ifile);
412 $inc_lines{$ifile} = $.;
415 $file_contents{$file} = $contents;
417 # For some reason I don't understand, it does not work
418 # to do `map { scan_file ($_, ...) } @inc_files' below.
419 # With Perl 5.8.2 it undefines @inc_files.
420 my @copy = @inc_files;
421 my @all_inc_files = (@inc_files,
422 map { scan_file ($type, $_,
423 "$file:$inc_lines{$_}") } @copy);
424 $file_includes{$file} = \@all_inc_files;
425 return @all_inc_files;
428 # strip_redundant_includes (%FILES)
429 # ---------------------------------
430 # Each key in %FILES is a file that must be present in the output.
431 # However some of these files might already include other files in %FILES,
432 # so there is no point in including them another time.
433 # This removes items of %FILES which are already included by another file.
434 sub strip_redundant_includes (%)
437 # Files at the end of @file_order should override those at the beginning,
438 # so it is important to preserve these trailing files. We can remove
439 # a file A if it is going to be output before a file B that includes
440 # file A, not the converse.
441 foreach my $file (reverse @file_order)
443 next unless exists $files{$file};
444 foreach my $ifile (@{$file_includes{$file}})
446 next unless exists $files{$ifile};
447 delete $files{$ifile};
448 verb "$ifile is already included by $file";
454 sub trace_used_macros ()
456 my %files = map { $map{$_} => 1 } keys %macro_seen;
457 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
458 %files = strip_redundant_includes %files;
459 # configure.ac is implicitly included.
460 delete $files{$configure_ac};
462 my $traces = ($ENV{AUTOM4TE} || 'autom4te');
463 $traces .= " --language Autoconf-without-aclocal-m4 ";
464 # All candidate files.
465 $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
466 # All candidate macros.
467 $traces .= join (' ',
468 (map { "--trace='$_:\$f::\$n::\$1'" } ('AC_DEFUN',
471 # Do not trace $1 for all other macros as we do
472 # not need it and it might contains harmful
473 # characters (like newlines).
474 (map { "--trace='$_:\$f::\$n'" } (keys %macro_seen)));
476 verb "running $traces $configure_ac";
478 my $tracefh = new Automake::XFile ("$traces $configure_ac |");
482 while ($_ = $tracefh->getline)
485 my ($file, $macro, $arg1) = split (/::/);
487 $traced{$macro} = 1 if exists $macro_seen{$macro};
489 $map_traced_defs{$arg1} = $file
490 if ($macro eq 'AC_DEFUN'
491 || $macro eq 'AC_DEFUN_ONCE'
492 || $macro eq 'AU_DEFUN');
500 sub scan_configure ()
502 # Make sure we include acinclude.m4 if it exists.
503 if (-f 'acinclude.m4')
505 add_file ('acinclude.m4');
507 scan_configure_dep ($configure_ac);
510 ################################################################
513 # Return 0 iff some files were installed locally.
514 sub write_aclocal ($@)
516 my ($output_file, @macros) = @_;
520 # Get the list of files containing definitions for the macros used.
521 # (Filter out unused macro definitions with $map_traced_defs. This
522 # can happen when an Autoconf macro is conditionally defined:
523 # aclocal sees the potential definition, but this definition is
524 # actually never processed and the Autoconf implementation is used
529 if (exists $map_traced_defs{$m}
530 && $map{$m} eq $map_traced_defs{$m});
532 # Always include acinclude.m4, even if it does not appear to be used.
533 $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
534 # Do not explicitly include a file that is already indirectly included.
535 %files = strip_redundant_includes %files;
536 # Never include configure.ac :)
537 delete $files{$configure_ac};
541 for my $file (grep { exists $files{$_} } @file_order)
543 # Check the time stamp of this file, and of all files it includes.
544 for my $ifile ($file, @{$file_includes{$file}})
546 my $mtime = mtime $ifile;
547 $greatest_mtime = $mtime if $greatest_mtime < $mtime;
550 # If the file to add looks like outside the project, copy it
551 # to the output. The regex catches filenames starting with
552 # things like `/', `\', or `c:\'.
553 if ($file_type{$file} != FT_USER
554 || $file =~ m,^(?:\w:)?[\\/],)
556 if (!$install || $file_type{$file} != FT_SYSTEM)
558 # Copy the file into aclocal.m4.
559 $output .= $file_contents{$file} . "\n";
563 # Install the file (and any file it includes).
565 for my $ifile (@{$file_includes{$file}}, $file)
567 $dest = "$user_includes[0]/" . basename $ifile;
568 verb "installing $ifile to $dest";
569 install_file ($ifile, $dest);
576 # Otherwise, simply include the file.
577 $output .= "m4_include([$file])\n";
583 verb "running aclocal anew, because some files were installed locally";
587 # Nothing to output?!
588 # FIXME: Shouldn't we diagnose this?
589 return 1 if ! length ($output);
591 # We used to print `# $output_file generated automatically etc.' But
592 # this creates spurious differences when using autoreconf. Autoreconf
593 # creates aclocal.m4t and then rename it to aclocal.m4, but the
594 # rebuild rules generated by Automake create aclocal.m4 directly --
595 # this would gives two ways to get the same file, with a different
596 # name in the header.
597 $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
599 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
600 # 2005 Free Software Foundation, Inc.
601 # This file is free software; the Free Software Foundation
602 # gives unlimited permission to copy and/or distribute it,
603 # with or without modifications, as long as this notice is preserved.
605 # This program is distributed in the hope that it will be useful,
606 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
607 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
608 # PARTICULAR PURPOSE.
612 # We try not to update $output_file unless necessary, because
613 # doing so invalidate Autom4te's cache and therefore slows down
614 # tools called after aclocal.
616 # We need to overwrite $output_file in the following situations.
617 # * The --force option is in use.
618 # * One of the dependencies is younger.
619 # (Not updating $output_file in this situation would cause
620 # make to call aclocal in loop.)
621 # * The contents of the current file are different from what
624 && $greatest_mtime < mtime ($output_file)
625 && $output eq contents ($output_file))
627 verb "$output_file unchanged";
631 verb "writing $output_file";
633 my $out = new Automake::XFile "> $output_file";
638 ################################################################
640 # Print usage and exit.
645 print "Usage: aclocal [OPTIONS] ...
647 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
650 --acdir=DIR directory holding config files (for debugging)
651 --force always update output file
652 --help print this help, then exit
653 -I DIR add directory to search list for .m4 files
654 --install copy third-party files to the first -I directory
655 --output=FILE put output in FILE (default aclocal.m4)
656 --print-ac-dir print name of directory holding m4 files, then exit
657 --verbose don't be silent
658 --version print version number, then exit
659 -W, --warnings=CATEGORY report the warnings falling in CATEGORY
661 Warning categories include:
662 `syntax' dubious syntactic constructs (default)
663 `unsupported' unknown macros (default)
664 `all' all the warnings (default)
665 `no-CATEGORY' turn off warnings in CATEGORY
666 `none' turn off all the warnings
667 `error' treat warnings as errors
669 Report bugs to <bug-automake\@gnu.org>.\n";
674 # Print version and exit.
678 aclocal (GNU $PACKAGE) $VERSION
679 Written by Tom Tromey <tromey\@redhat.com>
680 and Alexandre Duret-Lutz <adl\@gnu.org>.
682 Copyright (C) 2005 Free Software Foundation, Inc.
683 This is free software; see the source for copying conditions. There is NO
684 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
689 # Parse command line.
690 sub parse_arguments ()
692 my $print_and_exit = 0;
696 'acdir=s' => sub # Setting --acdir overrides both the
697 { # automake (versioned) directory and the
698 # public (unversioned) system directory.
699 @automake_includes = ();
700 @system_includes = ($_[1])
702 'force' => \$force_output,
703 'I=s' => \@user_includes,
704 'install' => \$install,
705 'output=s' => \$output_file,
706 'print-ac-dir' => \$print_and_exit,
707 'verbose' => sub { setup_channel 'verb', silent => 0; },
708 'W|warnings=s' => \&parse_warnings,
711 Getopt::Long::config ("bundling", "pass_through");
713 # See if --version or --help is used. We want to process these before
714 # anything else because the GNU Coding Standards require us to
715 # `exit 0' after processing these options, and we can't guarantee this
716 # if we treat other options first. (Handling other options first
717 # could produce error diagnostics, and in this condition it is
718 # confusing if aclocal does `exit 0'.)
719 my %cli_options_1st_pass =
721 'version' => \&version,
722 'help' => sub { usage(0); },
723 # Recognize all other options (and their arguments) but do nothing.
724 map { $_ => sub {} } (keys %cli_options)
726 my @ARGV_backup = @ARGV;
727 Getopt::Long::GetOptions %cli_options_1st_pass
729 @ARGV = @ARGV_backup;
731 # Now *really* process the options. This time we know that --help
732 # and --version are not present, but we specify them nonetheless so
733 # that ambiguous abbreviation are diagnosed.
734 Getopt::Long::GetOptions %cli_options, 'version' => sub {}, 'help' => sub {}
740 for my $k (keys %cli_options)
744 map { $argopts{(length ($_) == 1)
745 ? "-$_" : "--$_" } = 1; } (split (/\|/, $1));
748 if (exists $argopts{$ARGV[0]})
750 fatal ("option `$ARGV[0]' requires an argument\n"
751 . "Try `$0 --help' for more information.");
755 fatal ("unrecognized option `$ARGV[0]'\n"
756 . "Try `$0 --help' for more information.");
762 print "@system_includes\n";
766 if ($install && !@user_includes)
768 fatal ("--install should copy macros in the directory indicated by the"
769 . "\nfirst -I option, but no -I was supplied.");
772 if (! -d $system_includes[0])
774 # By default $(datadir)/aclocal doesn't exist. We don't want to
775 # get an error in the case where we are searching the default
776 # directory and it hasn't been created. (We know
777 # @system_includes has its default value if @automake_includes
778 # is not empty, because --acdir is the only way to change this.)
779 @system_includes = () if @automake_includes;
783 # Finally, adds any directory listed in the `dirlist' file.
784 if (open (DIRLIST, "$system_includes[0]/dirlist"))
790 # strip off newlines and end-of-line comments
793 push (@system_includes, $_) if -d $_;
800 ################################################################
802 parse_WARNINGS; # Parse the WARNINGS environment variable.
804 $configure_ac = require_configure_ac;
806 # We may have to rerun aclocal if some file have been installed, but
807 # it should not happen more than once. The reason we must run again
808 # is that once the file has been moved from /usr/share/aclocal/ to the
809 # local m4/ directory it appears at a new place in the search path,
810 # hence it should be output at a different position in aclocal.m4. If
811 # we did not rerun aclocal, the next run of aclocal would produce a
812 # different aclocal.m4.
817 prog_error "Too many loops." if $loop > 2;
823 my %macro_traced = trace_used_macros;
824 last if write_aclocal ($output_file, keys %macro_traced);
830 ### Setup "GNU" style for perl-mode and cperl-mode.
832 ## perl-indent-level: 2
833 ## perl-continued-statement-offset: 2
834 ## perl-continued-brace-offset: 0
835 ## perl-brace-offset: 0
836 ## perl-brace-imaginary-offset: 0
837 ## perl-label-offset: -2
838 ## cperl-indent-level: 2
839 ## cperl-brace-offset: 0
840 ## cperl-continued-brace-offset: 0
841 ## cperl-label-offset: -2
842 ## cperl-extra-newline-before-brace: t
843 ## cperl-merge-trailing-else: nil
844 ## cperl-continued-statement-offset: 2