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