release: beta release 1.13.1b (will become 1.13.2)
[automake.git] / aclocal.in
blobb51c09df7755ed424533fe5c654f351ccb7dcad3
1 #!@PERL@ -w
2 # -*- perl -*-
3 # @configure_input@
5 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
6     if 0;
8 # aclocal - create aclocal.m4 by scanning configure.ac
10 # Copyright (C) 1996-2013 Free Software Foundation, Inc.
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2, or (at your option)
15 # any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 # Written by Tom Tromey <tromey@redhat.com>, and
26 # Alexandre Duret-Lutz <adl@gnu.org>.
28 BEGIN
30   @Aclocal::perl_libdirs = ('@datadir@/@PACKAGE@-@APIVERSION@')
31     unless @Aclocal::perl_libdirs;
32   unshift @INC, @Aclocal::perl_libdirs;
35 use strict;
37 use Automake::Config;
38 use Automake::General;
39 use Automake::Configure_ac;
40 use Automake::Channels;
41 use Automake::ChannelDefs;
42 use Automake::XFile;
43 use Automake::FileUtils;
44 use File::Basename;
45 use File::Path ();
47 # Some globals.
49 # Support AC_CONFIG_MACRO_DIRS also with older autoconf.
50 # FIXME: To be removed in Automake 1.14, once we can assume autoconf
51 #        2.70 or later.
52 # FIXME: keep in sync with 'internal/ac-config-macro-dirs.m4'.
53 my $ac_config_macro_dirs_fallback =
54   'm4_ifndef([AC_CONFIG_MACRO_DIRS], [' .
55     'm4_defun([_AM_CONFIG_MACRO_DIRS], [])' .
56     'm4_defun([AC_CONFIG_MACRO_DIRS], [_AM_CONFIG_MACRO_DIRS($@)])' .
57   '])';
59 # We do not operate in threaded mode.
60 $perl_threads = 0;
62 # Include paths for searching macros.  We search macros in this order:
63 # user-supplied directories first, then the directory containing the
64 # automake macros, and finally the system-wide directories for
65 # third-party macros.
66 # @user_includes can be augmented with -I or AC_CONFIG_MACRO_DIRS.
67 # @automake_includes can be reset with the '--automake-acdir' option.
68 # @system_includes can be augmented with the 'dirlist' file or the
69 # ACLOCAL_PATH environment variable, and reset with the '--system-acdir'
70 # option.
71 my @user_includes = ();
72 my @automake_includes = ("@datadir@/aclocal-$APIVERSION");
73 my @system_includes = ('@datadir@/aclocal');
75 # Whether we should copy M4 file in $user_includes[0].
76 my $install = 0;
78 # --diff
79 my @diff_command;
81 # --dry-run
82 my $dry_run = 0;
84 # configure.ac or configure.in.
85 my $configure_ac;
87 # Output file name.
88 my $output_file = 'aclocal.m4';
90 # Option --force.
91 my $force_output = 0;
93 # Modification time of the youngest dependency.
94 my $greatest_mtime = 0;
96 # Which macros have been seen.
97 my %macro_seen = ();
99 # Remember the order into which we scanned the files.
100 # It's important to output the contents of aclocal.m4 in the opposite order.
101 # (Definitions in first files we have scanned should override those from
102 # later files.  So they must appear last in the output.)
103 my @file_order = ();
105 # Map macro names to file names.
106 my %map = ();
108 # Ditto, but records the last definition of each macro as returned by --trace.
109 my %map_traced_defs = ();
111 # Map basenames to macro names.
112 my %invmap = ();
114 # Map file names to file contents.
115 my %file_contents = ();
117 # Map file names to file types.
118 my %file_type = ();
119 use constant FT_USER => 1;
120 use constant FT_AUTOMAKE => 2;
121 use constant FT_SYSTEM => 3;
123 # Map file names to included files (transitively closed).
124 my %file_includes = ();
126 # Files which have already been added.
127 my %file_added = ();
129 # Files that have already been scanned.
130 my %scanned_configure_dep = ();
132 # Serial numbers, for files that have one.
133 # The key is the basename of the file,
134 # the value is the serial number represented as a list.
135 my %serial = ();
137 # Matches a macro definition.
138 #   AC_DEFUN([macroname], ...)
139 # or
140 #   AC_DEFUN(macroname, ...)
141 # When macroname is '['-quoted , we accept any character in the name,
142 # except ']'.  Otherwise macroname stops on the first ']', ',', ')',
143 # or '\n' encountered.
144 my $ac_defun_rx =
145   "(?:AU_ALIAS|A[CU]_DEFUN|AC_DEFUN_ONCE)\\((?:\\[([^]]+)\\]|([^],)\n]+))";
147 # Matches an AC_REQUIRE line.
148 my $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
150 # Matches an m4_include line.
151 my $m4_include_rx = "(m4_|m4_s|s)include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
153 # Match a serial number.
154 my $serial_line_rx = '^#\s*serial\s+(\S*)';
155 my $serial_number_rx = '^\d+(?:\.\d+)*$';
157 # Autoconf version.  This variable is set by 'trace_used_macros'.
158 my $ac_version;
160 # User directory containing extra m4 files for macros definition,
161 # as extracted from calls to the macro AC_CONFIG_MACRO_DIRS.
162 # This variable is updated by 'trace_used_macros'.
163 my @ac_config_macro_dirs;
165 # If set, names a temporary file that must be erased on abnormal exit.
166 my $erase_me;
168 ################################################################
170 # Prototypes for all subroutines.
172 sub unlink_tmp (;$);
173 sub xmkdir_p ($);
174 sub check_acinclude ();
175 sub reset_maps ();
176 sub install_file ($$);
177 sub list_compare (\@\@);
178 sub scan_m4_dirs ($$@);
179 sub scan_m4_files ();
180 sub add_macro ($);
181 sub scan_configure_dep ($);
182 sub add_file ($);
183 sub scan_file ($$$);
184 sub strip_redundant_includes (%);
185 sub trace_used_macros ();
186 sub scan_configure ();
187 sub write_aclocal ($@);
188 sub usage ($);
189 sub version ();
190 sub handle_acdir_option ($$);
191 sub parse_arguments ();
192 sub parse_ACLOCAL_PATH ();
194 ################################################################
196 # Erase temporary file ERASE_ME.  Handle signals.
197 sub unlink_tmp (;$)
199   my ($sig) = @_;
201   if ($sig)
202     {
203       verb "caught SIG$sig, bailing out";
204     }
205   if (defined $erase_me && -e $erase_me && !unlink ($erase_me))
206     {
207       fatal "could not remove '$erase_me': $!";
208     }
209   undef $erase_me;
211   # reraise default handler.
212   if ($sig)
213     {
214       $SIG{$sig} = 'DEFAULT';
215       kill $sig => $$;
216     }
219 $SIG{'INT'} = $SIG{'TERM'} = $SIG{'QUIT'} = $SIG{'HUP'} = 'unlink_tmp';
220 END { unlink_tmp }
222 sub xmkdir_p ($)
224   my $dir = shift;
225   local $@ = undef;
226   return
227     if -d $dir or eval { File::Path::mkpath $dir };
228   chomp $@;
229   $@ =~ s/\s+at\s.*\bline\s\d+.*$//;
230   fatal "could not create directory '$dir': $@";
233 # Check macros in acinclude.m4.  If one is not used, warn.
234 sub check_acinclude ()
236   foreach my $key (keys %map)
237     {
238       # FIXME: should print line number of acinclude.m4.
239       msg ('syntax', "macro '$key' defined in acinclude.m4 but never used")
240         if $map{$key} eq 'acinclude.m4' && ! exists $macro_seen{$key};
241     }
244 sub reset_maps ()
246   $greatest_mtime = 0;
247   %macro_seen = ();
248   @file_order = ();
249   %map = ();
250   %map_traced_defs = ();
251   %file_contents = ();
252   %file_type = ();
253   %file_includes = ();
254   %file_added = ();
255   %scanned_configure_dep = ();
256   %invmap = ();
257   %serial = ();
258   undef &search;
261 # install_file ($SRC, $DESTDIR)
262 sub install_file ($$)
264   my ($src, $destdir) = @_;
265   my $dest = $destdir . "/" . basename ($src);
266   my $diff_dest;
268   verb "installing $src to $dest";
270   if ($force_output
271       || !exists $file_contents{$dest}
272       || $file_contents{$src} ne $file_contents{$dest})
273     {
274       if (-e $dest)
275         {
276           msg 'note', "overwriting '$dest' with '$src'";
277           $diff_dest = $dest;
278         }
279       else
280         {
281           msg 'note', "installing '$dest' from '$src'";
282         }
284       if (@diff_command)
285         {
286           if (! defined $diff_dest)
287             {
288               # $dest does not exist.  We create an empty one just to
289               # run diff, and we erase it afterward.  Using the real
290               # the destination file (rather than a temporary file) is
291               # good when diff is run with options that display the
292               # file name.
293               #
294               # If creating $dest fails, fall back to /dev/null.  At
295               # least one diff implementation (Tru64's) cannot deal
296               # with /dev/null.  However working around this is not
297               # worth the trouble since nobody run aclocal on a
298               # read-only tree anyway.
299               $erase_me = $dest;
300               my $f = new IO::File "> $dest";
301               if (! defined $f)
302                 {
303                   undef $erase_me;
304                   $diff_dest = '/dev/null';
305                 }
306               else
307                 {
308                   $diff_dest = $dest;
309                   $f->close;
310                 }
311             }
312           my @cmd = (@diff_command, $diff_dest, $src);
313           $! = 0;
314           verb "running: @cmd";
315           my $res = system (@cmd);
316           Automake::FileUtils::handle_exec_errors "@cmd", 1
317             if $res;
318           unlink_tmp;
319         }
320       elsif (!$dry_run)
321         {
322           xmkdir_p ($destdir);
323           xsystem ('cp', $src, $dest);
324         }
325     }
328 # Compare two lists of numbers.
329 sub list_compare (\@\@)
331   my @l = @{$_[0]};
332   my @r = @{$_[1]};
333   while (1)
334     {
335       if (0 == @l)
336         {
337           return (0 == @r) ? 0 : -1;
338         }
339       elsif (0 == @r)
340         {
341           return 1;
342         }
343       elsif ($l[0] < $r[0])
344         {
345           return -1;
346         }
347       elsif ($l[0] > $r[0])
348         {
349           return 1;
350         }
351       shift @l;
352       shift @r;
353     }
356 ################################################################
358 # scan_m4_dirs($TYPE, $ERR_ON_NONEXISTING, @DIRS)
359 # -----------------------------------------------
360 # Scan all M4 files installed in @DIRS for new macro definitions.
361 # Register each file as of type $TYPE (one of the FT_* constants).
362 sub scan_m4_dirs ($$@)
364   my ($type, $err_on_nonexisting, @dirlist) = @_;
366   foreach my $m4dir (@dirlist)
367     {
368       if (! opendir (DIR, $m4dir))
369         {
370           # TODO: maybe avoid complaining only if errno == ENONENT?
371           next unless $err_on_nonexisting;
372           fatal "couldn't open directory '$m4dir': $!";
373         }
375       # We reverse the directory contents so that foo2.m4 gets
376       # used in preference to foo1.m4.
377       foreach my $file (reverse sort grep (! /^\./, readdir (DIR)))
378         {
379           # Only examine .m4 files.
380           next unless $file =~ /\.m4$/;
382           # Skip some files when running out of srcdir.
383           next if $file eq 'aclocal.m4';
385           my $fullfile = File::Spec->canonpath ("$m4dir/$file");
386           scan_file ($type, $fullfile, 'aclocal');
387         }
388       closedir (DIR);
389     }
392 # Scan all the installed m4 files and construct a map.
393 sub scan_m4_files ()
395   # First, scan configure.ac.  It may contain macro definitions,
396   # or may include other files that define macros.
397   scan_file (FT_USER, $configure_ac, 'aclocal');
399   # Then, scan acinclude.m4 if it exists.
400   if (-f 'acinclude.m4')
401     {
402       scan_file (FT_USER, 'acinclude.m4', 'aclocal');
403     }
405   # Finally, scan all files in our search paths.
407   if (@user_includes)
408     {
409       # Don't complain if the first user directory doesn't exist, in case
410       # we need to create it later (can happen if '--install' was given).
411       scan_m4_dirs (FT_USER, !$install, $user_includes[0]);
412       scan_m4_dirs (FT_USER, 1, @user_includes[1..$#user_includes]);
413     }
414   scan_m4_dirs (FT_AUTOMAKE, 1, @automake_includes);
415   scan_m4_dirs (FT_SYSTEM,   1, @system_includes);
417   # Construct a new function that does the searching.  We use a
418   # function (instead of just evaluating $search in the loop) so that
419   # "die" is correctly and easily propagated if run.
420   my $search = "sub search {\nmy \$found = 0;\n";
421   foreach my $key (reverse sort keys %map)
422     {
423       $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { add_macro ("' . $key
424                   . '"); $found = 1; }' . "\n");
425     }
426   $search .= "return \$found;\n};\n";
427   eval $search;
428   prog_error "$@\n search is $search" if $@;
431 ################################################################
433 # Add a macro to the output.
434 sub add_macro ($)
436   my ($macro) = @_;
438   # Ignore unknown required macros.  Either they are not really
439   # needed (e.g., a conditional AC_REQUIRE), in which case aclocal
440   # should be quiet, or they are needed and Autoconf itself will
441   # complain when we trace for macro usage later.
442   return unless defined $map{$macro};
444   verb "saw macro $macro";
445   $macro_seen{$macro} = 1;
446   add_file ($map{$macro});
449 # scan_configure_dep ($file)
450 # --------------------------
451 # Scan a configure dependency (configure.ac, or separate m4 files)
452 # for uses of known macros and AC_REQUIREs of possibly unknown macros.
453 # Recursively scan m4_included files.
454 sub scan_configure_dep ($)
456   my ($file) = @_;
457   # Do not scan a file twice.
458   return ()
459     if exists $scanned_configure_dep{$file};
460   $scanned_configure_dep{$file} = 1;
462   my $mtime = mtime $file;
463   $greatest_mtime = $mtime if $greatest_mtime < $mtime;
465   my $contents = exists $file_contents{$file} ?
466     $file_contents{$file} : contents $file;
468   my $line = 0;
469   my @rlist = ();
470   my @ilist = ();
471   foreach (split ("\n", $contents))
472     {
473       ++$line;
474       # Remove comments from current line.
475       s/\bdnl\b.*$//;
476       s/\#.*$//;
477       # Avoid running all the following regexes on white lines.
478       next if /^\s*$/;
480       while (/$m4_include_rx/go)
481         {
482           my $ifile = $2 || $3;
483           # Skip missing 'sinclude'd files.
484           next if $1 ne 'm4_' && ! -f $ifile;
485           push @ilist, $ifile;
486         }
488       while (/$ac_require_rx/go)
489         {
490           push (@rlist, $1 || $2);
491         }
493       # The search function is constructed dynamically by
494       # scan_m4_files.  The last parenthetical match makes sure we
495       # don't match things that look like macro assignments or
496       # AC_SUBSTs.
497       if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
498         {
499           # Macro not found, but AM_ prefix found.
500           # Make this just a warning, because we do not know whether
501           # the macro is actually used (it could be called conditionally).
502           msg ('unsupported', "$file:$line",
503                "macro '$2' not found in library");
504         }
505     }
507   add_macro ($_) foreach (@rlist);
508   scan_configure_dep ($_) foreach @ilist;
511 # add_file ($FILE)
512 # ----------------
513 # Add $FILE to output.
514 sub add_file ($)
516   my ($file) = @_;
518   # Only add a file once.
519   return if ($file_added{$file});
520   $file_added{$file} = 1;
522   scan_configure_dep $file;
525 # Point to the documentation for underquoted AC_DEFUN only once.
526 my $underquoted_manual_once = 0;
528 # scan_file ($TYPE, $FILE, $WHERE)
529 # --------------------------------
530 # Scan a single M4 file ($FILE), and all files it includes.
531 # Return the list of included files.
532 # $TYPE is one of FT_USER, FT_AUTOMAKE, or FT_SYSTEM, depending
533 # on where the file comes from.
534 # $WHERE is the location to use in the diagnostic if the file
535 # does not exist.
536 sub scan_file ($$$)
538   my ($type, $file, $where) = @_;
539   my $basename = basename $file;
541   # Do not scan the same file twice.
542   return @{$file_includes{$file}} if exists $file_includes{$file};
543   # Prevent potential infinite recursion (if two files include each other).
544   return () if exists $file_contents{$file};
546   unshift @file_order, $file;
548   $file_type{$file} = $type;
550   fatal "$where: file '$file' does not exist" if ! -e $file;
552   my $fh = new Automake::XFile $file;
553   my $contents = '';
554   my @inc_files = ();
555   my %inc_lines = ();
557   my $defun_seen = 0;
558   my $serial_seen = 0;
559   my $serial_older = 0;
561   while ($_ = $fh->getline)
562     {
563       # Ignore '##' lines.
564       next if /^##/;
566       $contents .= $_;
567       my $line = $_;
569       if ($line =~ /$serial_line_rx/go)
570         {
571           my $number = $1;
572           if ($number !~ /$serial_number_rx/go)
573             {
574               msg ('syntax', "$file:$.",
575                    "ill-formed serial number '$number', "
576                    . "expecting a version string with only digits and dots");
577             }
578           elsif ($defun_seen)
579             {
580               # aclocal removes all definitions from M4 file with the
581               # same basename if a greater serial number is found.
582               # Encountering a serial after some macros will undefine
583               # these macros...
584               msg ('syntax', "$file:$.",
585                    'the serial number must appear before any macro definition');
586             }
587           # We really care about serials only for non-automake macros
588           # and when --install is used.  But the above diagnostics are
589           # made regardless of this, because not using --install is
590           # not a reason not the fix macro files.
591           elsif ($install && $type != FT_AUTOMAKE)
592             {
593               $serial_seen = 1;
594               my @new = split (/\./, $number);
596               verb "$file:$.: serial $number";
598               if (!exists $serial{$basename}
599                   || list_compare (@new, @{$serial{$basename}}) > 0)
600                 {
601                   # Delete any definition we knew from the old macro.
602                   foreach my $def (@{$invmap{$basename}})
603                     {
604                       verb "$file:$.: ignoring previous definition of $def";
605                       delete $map{$def};
606                     }
607                   $invmap{$basename} = [];
608                   $serial{$basename} = \@new;
609                 }
610               else
611                 {
612                   $serial_older = 1;
613                 }
614             }
615         }
617       # Remove comments from current line.
618       # Do not do it earlier, because the serial line is a comment.
619       $line =~ s/\bdnl\b.*$//;
620       $line =~ s/\#.*$//;
622       while ($line =~ /$ac_defun_rx/go)
623         {
624           $defun_seen = 1;
625           if (! defined $1)
626             {
627               msg ('syntax', "$file:$.", "underquoted definition of $2"
628                    . "\n  run info Automake 'Extending aclocal'\n"
629                    . "  or see http://www.gnu.org/software/automake/manual/"
630                    . "automake.html#Extending-aclocal")
631                 unless $underquoted_manual_once;
632               $underquoted_manual_once = 1;
633             }
635           # If this macro does not have a serial and we have already
636           # seen a macro with the same basename earlier, we should
637           # ignore the macro (don't exit immediately so we can still
638           # diagnose later #serial numbers and underquoted macros).
639           $serial_older ||= ($type != FT_AUTOMAKE
640                              && !$serial_seen && exists $serial{$basename});
642           my $macro = $1 || $2;
643           if (!$serial_older && !defined $map{$macro})
644             {
645               verb "found macro $macro in $file: $.";
646               $map{$macro} = $file;
647               push @{$invmap{$basename}}, $macro;
648             }
649           else
650             {
651               # Note: we used to give an error here if we saw a
652               # duplicated macro.  However, this turns out to be
653               # extremely unpopular.  It causes actual problems which
654               # are hard to work around, especially when you must
655               # mix-and-match tool versions.
656               verb "ignoring macro $macro in $file: $.";
657             }
658         }
660       while ($line =~ /$m4_include_rx/go)
661         {
662           my $ifile = $2 || $3;
663           # Skip missing 'sinclude'd files.
664           next if $1 ne 'm4_' && ! -f $ifile;
665           push (@inc_files, $ifile);
666           $inc_lines{$ifile} = $.;
667         }
668     }
670   # Ignore any file that has an old serial (or no serial if we know
671   # another one with a serial).
672   return ()
673     if ($serial_older ||
674         ($type != FT_AUTOMAKE && !$serial_seen && exists $serial{$basename}));
676   $file_contents{$file} = $contents;
678   # For some reason I don't understand, it does not work
679   # to do "map { scan_file ($_, ...) } @inc_files" below.
680   # With Perl 5.8.2 it undefines @inc_files.
681   my @copy = @inc_files;
682   my @all_inc_files = (@inc_files,
683                        map { scan_file ($type, $_,
684                                         "$file:$inc_lines{$_}") } @copy);
685   $file_includes{$file} = \@all_inc_files;
686   return @all_inc_files;
689 # strip_redundant_includes (%FILES)
690 # ---------------------------------
691 # Each key in %FILES is a file that must be present in the output.
692 # However some of these files might already include other files in %FILES,
693 # so there is no point in including them another time.
694 # This removes items of %FILES which are already included by another file.
695 sub strip_redundant_includes (%)
697   my %files = @_;
699   # Always include acinclude.m4, even if it does not appear to be used.
700   $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
701   # File included by $configure_ac are redundant.
702   $files{$configure_ac} = 1;
704   # Files at the end of @file_order should override those at the beginning,
705   # so it is important to preserve these trailing files.  We can remove
706   # a file A if it is going to be output before a file B that includes
707   # file A, not the converse.
708   foreach my $file (reverse @file_order)
709     {
710       next unless exists $files{$file};
711       foreach my $ifile (@{$file_includes{$file}})
712         {
713           next unless exists $files{$ifile};
714           delete $files{$ifile};
715           verb "$ifile is already included by $file";
716         }
717     }
719   # configure.ac is implicitly included.
720   delete $files{$configure_ac};
722   return %files;
725 sub trace_used_macros ()
727   my %files = map { $map{$_} => 1 } keys %macro_seen;
728   %files = strip_redundant_includes %files;
730   # When AC_CONFIG_MACRO_DIRS is used, avoid possible spurious warnings
731   # from autom4te about macros being "m4_require'd but not m4_defun'd";
732   # for more background, see:
733   # http://lists.gnu.org/archive/html/autoconf-patches/2012-11/msg00004.html
734   # as well as autoconf commit 'v2.69-44-g1ed0548', "warn: allow aclocal
735   # to silence m4_require warnings".
736   my $early_m4_code .= "m4_define([m4_require_silent_probe], [-])";
738   my $traces = ($ENV{AUTOM4TE} || '@am_AUTOM4TE@');
739   $traces .= " --language Autoconf-without-aclocal-m4 ";
740   $traces = "echo '$early_m4_code' | $traces - ";
742   # Support AC_CONFIG_MACRO_DIRS also with older autoconf.
743   # Note that we can't use '$ac_config_macro_dirs_fallback' here, because
744   # a bug in option parsing code of autom4te 2.68 and earlier will cause
745   # it to read standard input last, even if the "-" argument is specified
746   # early.
747   # FIXME: To be removed in Automake 1.14, once we can assume autoconf
748   #        2.70 or later.
749   $traces .= "$automake_includes[0]/internal/ac-config-macro-dirs.m4 ";
751   # All candidate files.
752   $traces .= join (' ',
753                    (map { "'$_'" }
754                     (grep { exists $files{$_} } @file_order))) . " ";
756   # All candidate macros.
757   $traces .= join (' ',
758                    (map { "--trace='$_:\$f::\$n::\${::}%'" }
759                     ('AC_DEFUN',
760                      'AC_DEFUN_ONCE',
761                      'AU_DEFUN',
762                      '_AM_AUTOCONF_VERSION',
763                      'AC_CONFIG_MACRO_DIR_TRACE',
764                      # FIXME: Tracing the next two macros is a hack for
765                      # compatibility with older autoconf.  Remove this in
766                      # Automake 1.14, when we can assume Autoconf 2.70 or
767                      # later.
768                      'AC_CONFIG_MACRO_DIR',
769                      '_AM_CONFIG_MACRO_DIRS')),
770                    # Do not trace $1 for all other macros as we do
771                    # not need it and it might contains harmful
772                    # characters (like newlines).
773                    (map { "--trace='$_:\$f::\$n'" } (keys %macro_seen)));
775   verb "running $traces $configure_ac";
777   my $tracefh = new Automake::XFile ("$traces $configure_ac |");
779   @ac_config_macro_dirs = ();
781   my %traced = ();
783   while ($_ = $tracefh->getline)
784     {
785       chomp;
786       my ($file, $macro, $arg1) = split (/::/);
788       $traced{$macro} = 1 if exists $macro_seen{$macro};
790       if ($macro eq 'AC_DEFUN' || $macro eq 'AC_DEFUN_ONCE'
791             || $macro eq 'AU_DEFUN')
792         {
793           $map_traced_defs{$arg1} = $file;
794         }
795       elsif ($macro eq '_AM_AUTOCONF_VERSION')
796         {
797           $ac_version = $arg1;
798         }
799       elsif ($macro eq 'AC_CONFIG_MACRO_DIR_TRACE')
800         {
801           push @ac_config_macro_dirs, $arg1;
802         }
803       # FIXME: We still need to trace AC_CONFIG_MACRO_DIR
804       # for compatibility with older autoconf.  Remove this
805       # once we can assume Autoconf 2.70 or later.
806       elsif ($macro eq 'AC_CONFIG_MACRO_DIR')
807         {
808           @ac_config_macro_dirs = ($arg1);
809         }
810       # FIXME:This is an hack for compatibility with older autoconf.
811       # Remove this once we can assume Autoconf 2.70 or later.
812       elsif ($macro eq '_AM_CONFIG_MACRO_DIRS')
813         {
814            # Empty leading/trailing fields might be produced by split,
815            # hence the grep is really needed.
816            push @ac_config_macro_dirs, grep (/./, (split /\s+/, $arg1));
817         }
818     }
820   # FIXME: in Autoconf >= 2.70, AC_CONFIG_MACRO_DIR calls
821   # AC_CONFIG_MACRO_DIR_TRACE behind the scenes, which could
822   # leave unwanted duplicates in @ac_config_macro_dirs.
823   # Remove this in Automake 1.14, when we'll stop tracing
824   # AC_CONFIG_MACRO_DIR explicitly.
825   @ac_config_macro_dirs = uniq @ac_config_macro_dirs;
827   $tracefh->close;
829   return %traced;
832 sub scan_configure ()
834   # Make sure we include acinclude.m4 if it exists.
835   if (-f 'acinclude.m4')
836     {
837       add_file ('acinclude.m4');
838     }
839   scan_configure_dep ($configure_ac);
842 ################################################################
844 # Write output.
845 # Return 0 iff some files were installed locally.
846 sub write_aclocal ($@)
848   my ($output_file, @macros) = @_;
849   my $output = '';
851   my %files = ();
852   # Get the list of files containing definitions for the macros used.
853   # (Filter out unused macro definitions with $map_traced_defs.  This
854   # can happen when an Autoconf macro is conditionally defined:
855   # aclocal sees the potential definition, but this definition is
856   # actually never processed and the Autoconf implementation is used
857   # instead.)
858   for my $m (@macros)
859     {
860       $files{$map{$m}} = 1
861         if (exists $map_traced_defs{$m}
862             && $map{$m} eq $map_traced_defs{$m});
863     }
864   # Do not explicitly include a file that is already indirectly included.
865   %files = strip_redundant_includes %files;
867   my $installed = 0;
869   for my $file (grep { exists $files{$_} } @file_order)
870     {
871       # Check the time stamp of this file, and of all files it includes.
872       for my $ifile ($file, @{$file_includes{$file}})
873         {
874           my $mtime = mtime $ifile;
875           $greatest_mtime = $mtime if $greatest_mtime < $mtime;
876         }
878       # If the file to add looks like outside the project, copy it
879       # to the output.  The regex catches filenames starting with
880       # things like '/', '\', or 'c:\'.
881       if ($file_type{$file} != FT_USER
882           || $file =~ m,^(?:\w:)?[\\/],)
883         {
884           if (!$install || $file_type{$file} != FT_SYSTEM)
885             {
886               # Copy the file into aclocal.m4.
887               $output .= $file_contents{$file} . "\n";
888             }
889           else
890             {
891               # Install the file (and any file it includes).
892               my $dest;
893               for my $ifile (@{$file_includes{$file}}, $file)
894                 {
895                   install_file ($ifile, $user_includes[0]);
896                 }
897               $installed = 1;
898             }
899         }
900       else
901         {
902           # Otherwise, simply include the file.
903           $output .= "m4_include([$file])\n";
904         }
905     }
907   if ($installed)
908     {
909       verb "running aclocal anew, because some files were installed locally";
910       return 0;
911     }
913   # Nothing to output?!
914   # FIXME: Shouldn't we diagnose this?
915   return 1 if ! length ($output);
917   if ($ac_version)
918     {
919       # Do not use "$output_file" here for the same reason we do not
920       # use it in the header below.  autom4te will output the name of
921       # the file in the diagnostic anyway.
922       $output = "m4_ifndef([AC_AUTOCONF_VERSION],
923   [m4_copy([m4_PACKAGE_VERSION], [AC_AUTOCONF_VERSION])])dnl
924 m4_if(m4_defn([AC_AUTOCONF_VERSION]), [$ac_version],,
925 [m4_warning([this file was generated for autoconf $ac_version.
926 You have another version of autoconf.  It may work, but is not guaranteed to.
927 If you have problems, you may need to regenerate the build system entirely.
928 To do so, use the procedure documented by the package, typically 'autoreconf'.])])
930 $output";
931     }
933   # We used to print "# $output_file generated automatically etc."  But
934   # this creates spurious differences when using autoreconf.  Autoreconf
935   # creates aclocal.m4t and then rename it to aclocal.m4, but the
936   # rebuild rules generated by Automake create aclocal.m4 directly --
937   # this would gives two ways to get the same file, with a different
938   # name in the header.
939   $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
941 # Copyright (C) 1996-$RELEASE_YEAR Free Software Foundation, Inc.
943 # This file is free software; the Free Software Foundation
944 # gives unlimited permission to copy and/or distribute it,
945 # with or without modifications, as long as this notice is preserved.
947 # This program is distributed in the hope that it will be useful,
948 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
949 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
950 # PARTICULAR PURPOSE.
952 $ac_config_macro_dirs_fallback
953 $output";
955   # We try not to update $output_file unless necessary, because
956   # doing so invalidate Autom4te's cache and therefore slows down
957   # tools called after aclocal.
958   #
959   # We need to overwrite $output_file in the following situations.
960   #   * The --force option is in use.
961   #   * One of the dependencies is younger.
962   #     (Not updating $output_file in this situation would cause
963   #     make to call aclocal in loop.)
964   #   * The contents of the current file are different from what
965   #     we have computed.
966   if (!$force_output
967       && $greatest_mtime < mtime ($output_file)
968       && $output eq contents ($output_file))
969     {
970       verb "$output_file unchanged";
971       return 1;
972     }
974   verb "writing $output_file";
976   if (!$dry_run)
977     {
978       if (-e $output_file && !unlink $output_file)
979         {
980           fatal "could not remove '$output_file': $!";
981         }
982       my $out = new Automake::XFile "> $output_file";
983       print $out $output;
984     }
985   return 1;
988 ################################################################
990 # Print usage and exit.
991 sub usage ($)
993   my ($status) = @_;
995   print <<'EOF';
996 Usage: aclocal [OPTION]...
998 Generate 'aclocal.m4' by scanning 'configure.ac' or 'configure.in'
1000 Options:
1001       --automake-acdir=DIR  directory holding automake-provided m4 files
1002       --system-acdir=DIR    directory holding third-party system-wide files
1003       --diff[=COMMAND]      run COMMAND [diff -u] on M4 files that would be
1004                             changed (implies --install and --dry-run)
1005       --dry-run             pretend to, but do not actually update any file
1006       --force               always update output file
1007       --help                print this help, then exit
1008   -I DIR                    add directory to search list for .m4 files
1009       --install             copy third-party files to the first -I directory
1010       --output=FILE         put output in FILE (default aclocal.m4)
1011       --print-ac-dir        print name of directory holding system-wide
1012                               third-party m4 files, then exit
1013       --verbose             don't be silent
1014       --version             print version number, then exit
1015   -W, --warnings=CATEGORY   report the warnings falling in CATEGORY
1017 Warning categories include:
1018   syntax        dubious syntactic constructs (default)
1019   unsupported   unknown macros (default)
1020   all           all the warnings (default)
1021   no-CATEGORY   turn off warnings in CATEGORY
1022   none          turn off all the warnings
1023   error         treat warnings as errors
1025 Report bugs to <@PACKAGE_BUGREPORT@>.
1026 GNU Automake home page: <@PACKAGE_URL@>.
1027 General help using GNU software: <http://www.gnu.org/gethelp/>.
1029   exit $status;
1032 # Print version and exit.
1033 sub version ()
1035   print <<EOF;
1036 aclocal (GNU $PACKAGE) $VERSION
1037 Copyright (C) $RELEASE_YEAR Free Software Foundation, Inc.
1038 License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>
1039 This is free software: you are free to change and redistribute it.
1040 There is NO WARRANTY, to the extent permitted by law.
1042 Written by Tom Tromey <tromey\@redhat.com>
1043        and Alexandre Duret-Lutz <adl\@gnu.org>.
1045   exit 0;
1048 # Parse command line.
1049 sub parse_arguments ()
1051   my $print_and_exit = 0;
1052   my $diff_command;
1054   my %cli_options =
1055     (
1056      'help'             => sub { usage(0); },
1057      'version'          => \&version,
1058      'system-acdir=s'   => sub { shift; @system_includes = @_; },
1059      'automake-acdir=s' => sub { shift; @automake_includes = @_; },
1060      'diff:s'           => \$diff_command,
1061      'dry-run'          => \$dry_run,
1062      'force'            => \$force_output,
1063      'I=s'              => \@user_includes,
1064      'install'          => \$install,
1065      'output=s'         => \$output_file,
1066      'print-ac-dir'     => \$print_and_exit,
1067      'verbose'          => sub { setup_channel 'verb', silent => 0; },
1068      'W|warnings=s'     => \&parse_warnings,
1069      );
1071   use Automake::Getopt ();
1072   Automake::Getopt::parse_options %cli_options;
1074   if (@ARGV > 0)
1075     {
1076       fatal ("non-option arguments are not accepted: '$ARGV[0]'.\n"
1077              . "Try '$0 --help' for more information.");
1078     }
1080   if ($print_and_exit)
1081     {
1082       print "@system_includes\n";
1083       exit 0;
1084     }
1086   if (defined $diff_command)
1087     {
1088       $diff_command = 'diff -u' if $diff_command eq '';
1089       @diff_command = split (' ', $diff_command);
1090       $install = 1;
1091       $dry_run = 1;
1092     }
1094   # Finally, adds any directory listed in the 'dirlist' file.
1095   if (open (DIRLIST, "$system_includes[0]/dirlist"))
1096     {
1097       while (<DIRLIST>)
1098         {
1099           # Ignore '#' lines.
1100           next if /^#/;
1101           # strip off newlines and end-of-line comments
1102           s/\s*\#.*$//;
1103           chomp;
1104           foreach my $dir (glob)
1105             {
1106               push (@system_includes, $dir) if -d $dir;
1107             }
1108         }
1109       close (DIRLIST);
1110     }
1113 # Add any directory listed in the 'ACLOCAL_PATH' environment variable
1114 # to the list of system include directories.
1115 sub parse_ACLOCAL_PATH ()
1117   return if not defined $ENV{"ACLOCAL_PATH"};
1118   # Directories in ACLOCAL_PATH should take precedence over system
1119   # directories, so we use unshift.  However, directories that
1120   # come first in ACLOCAL_PATH take precedence over directories
1121   # coming later, which is why the result of split is reversed.
1122   foreach my $dir (reverse split /:/, $ENV{"ACLOCAL_PATH"})
1123     {
1124       unshift (@system_includes, $dir) if $dir ne '' && -d $dir;
1125     }
1128 ################################################################
1130 parse_WARNINGS;             # Parse the WARNINGS environment variable.
1131 parse_arguments;
1132 parse_ACLOCAL_PATH;
1133 $configure_ac = require_configure_ac;
1135 # We may have to rerun aclocal if some file have been installed, but
1136 # it should not happen more than once.  The reason we must run again
1137 # is that once the file has been moved from /usr/share/aclocal/ to the
1138 # local m4/ directory it appears at a new place in the search path,
1139 # hence it should be output at a different position in aclocal.m4.  If
1140 # we did not rerun aclocal, the next run of aclocal would produce a
1141 # different aclocal.m4.
1142 my $loop = 0;
1143 my $rerun_due_to_macrodir = 0;
1144 while (1)
1145   {
1146     ++$loop;
1147     prog_error "too many loops" if $loop > 2 + $rerun_due_to_macrodir;
1149     reset_maps;
1150     scan_m4_files;
1151     scan_configure;
1152     last if $exit_code;
1153     my %macro_traced = trace_used_macros;
1155     if (!$rerun_due_to_macrodir && @ac_config_macro_dirs)
1156       {
1157         # The directory specified in calls to the AC_CONFIG_MACRO_DIRS
1158         # m4 macro (if any) must go after the user includes specified
1159         # explicitly with the '-I' option.
1160         push @user_includes, @ac_config_macro_dirs;
1161         # We might have to scan some new directory of .m4 files.
1162         $rerun_due_to_macrodir++;
1163         next;
1164       }
1166     if ($install && !@user_includes)
1167       {
1168         fatal "installation of third-party macros impossible without " .
1169               "-I options nor AC_CONFIG_MACRO_DIR{,S} m4 macro(s)";
1170       }
1172     last if write_aclocal ($output_file, keys %macro_traced);
1173     last if $dry_run;
1174   }
1175 check_acinclude;
1177 exit $exit_code;
1179 ### Setup "GNU" style for perl-mode and cperl-mode.
1180 ## Local Variables:
1181 ## perl-indent-level: 2
1182 ## perl-continued-statement-offset: 2
1183 ## perl-continued-brace-offset: 0
1184 ## perl-brace-offset: 0
1185 ## perl-brace-imaginary-offset: 0
1186 ## perl-label-offset: -2
1187 ## cperl-indent-level: 2
1188 ## cperl-brace-offset: 0
1189 ## cperl-continued-brace-offset: 0
1190 ## cperl-label-offset: -2
1191 ## cperl-extra-newline-before-brace: t
1192 ## cperl-merge-trailing-else: nil
1193 ## cperl-continued-statement-offset: 2
1194 ## End: