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