Preliminary support for `aclocal --install'.
[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, 2005
11 #           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 2, 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, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 # 02111-1307, USA.
27
28 # Written by Tom Tromey <tromey@redhat.com>, and
29 # Alexandre Duret-Lutz <adl@gnu.org>.
30
31 BEGIN
32 {
33   my $perllibdir = $ENV{'perllibdir'} || '@datadir@/@PACKAGE@-@APIVERSION@';
34   unshift @INC, (split '@PATH_SEPARATOR@', $perllibdir);
35 }
36
37 use strict;
38
39 use Automake::Config;
40 use Automake::General;
41 use Automake::Configure_ac;
42 use Automake::Channels;
43 use Automake::ChannelDefs;
44 use Automake::XFile;
45 use Automake::FileUtils;
46 use File::Basename;
47 use File::stat;
48 use Cwd;
49
50 # Some globals.
51
52 # Include paths for searching macros.  We search macros in this order:
53 # user-supplied directories first, then the directory containing the
54 # automake macros, and finally the system-wide directories for
55 # third-party macro.  @user_includes can be augmented with -I.
56 # @system_includes can be augmented with the `dirlist' file.  Also
57 # --acdir will reset both @automake_includes and @system_includes.
58 my @user_includes = ();
59 my @automake_includes = ("@datadir@/aclocal-$APIVERSION");
60 my @system_includes = ('@datadir@/aclocal');
61
62 # Whether we should copy M4 file in $user_includes[0].
63 my $install = 0;
64
65 # configure.ac or configure.in.
66 my $configure_ac;
67
68 # Output file name.
69 my $output_file = 'aclocal.m4';
70
71 # Option --force.
72 my $force_output = 0;
73
74 # Modification time of the youngest dependency.
75 my $greatest_mtime = 0;
76
77 # Which macros have been seen.
78 my %macro_seen = ();
79
80 # Remember the order into which we scanned the files.
81 # It's important to output the contents of aclocal.m4 in the opposite order.
82 # (Definitions in first files we have scanned should override those from
83 # later files.  So they must appear last in the output.)
84 my @file_order = ();
85
86 # Map macro names to file names.
87 my %map = ();
88
89 # Ditto, but records the last definition of each macro as returned by --trace.
90 my %map_traced_defs = ();
91
92 # Map file names to file contents.
93 my %file_contents = ();
94
95 # Map file names to file types.
96 my %file_type = ();
97 use constant FT_USER => 1;
98 use constant FT_AUTOMAKE => 2;
99 use constant FT_SYSTEM => 3;
100
101 # Map file names to included files (transitively closed).
102 my %file_includes = ();
103
104 # Files which have already been added.
105 my %file_added = ();
106
107 # Files that have already been scanned.
108 my %scanned_configure_dep = ();
109
110 # Matches a macro definition.
111 #   AC_DEFUN([macroname], ...)
112 # or
113 #   AC_DEFUN(macroname, ...)
114 # When macroname is `['-quoted , we accept any character in the name,
115 # except `]'.  Otherwise macroname stops on the first `]', `,', `)',
116 # or `\n' encountered.
117 my $ac_defun_rx =
118   "(?:AU_ALIAS|A[CU]_DEFUN|AC_DEFUN_ONCE)\\((?:\\[([^]]+)\\]|([^],)\n]+))";
119
120 # Matches an AC_REQUIRE line.
121 my $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
122
123 # Matches an m4_include line
124 my $m4_include_rx = "(?:m4_)?s?include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
125
126 \f
127 ################################################################
128
129 # Check macros in acinclude.m4.  If one is not used, warn.
130 sub check_acinclude ()
131 {
132   foreach my $key (keys %map)
133     {
134       # FIXME: should print line number of acinclude.m4.
135       msg ('syntax', "warning: macro `$key' defined in "
136            . "acinclude.m4 but never used")
137         if $map{$key} eq 'acinclude.m4' && ! exists $macro_seen{$key};
138     }
139 }
140
141 sub reset_maps ()
142 {
143   $greatest_mtime = 0;
144   %macro_seen = ();
145   @file_order = ();
146   %map = ();
147   %map_traced_defs = ();
148   %file_contents = ();
149   %file_type = ();
150   %file_includes = ();
151   %file_added = ();
152   %scanned_configure_dep = ();
153   undef &search;
154 }
155
156 # install_file ($SRC, $DEST)
157 sub install_file ($$)
158 {
159   my ($src, $dest) = @_;
160
161   if ($force_output
162       || !exists $file_contents{$dest}
163       || $file_contents{$src} ne $file_contents{$dest})
164     {
165       if (system ('cp', $src, $dest))
166         {
167           error ("error while copying `$src' to `$dest'");
168         }
169       else
170         {
171           msg 'note', "installing `$dest'";
172         }
173     }
174 }
175
176 ################################################################
177
178 # scan_m4_dirs($TYPE, @DIRS)
179 # --------------------------
180 # Scan all M4 files installed in @DIRS for new macro definitions.
181 # Register each file as of type $TYPE (one of the FT_* constants).
182 sub scan_m4_dirs ($@)
183 {
184   my ($type, @dirlist) = @_;
185
186   foreach my $m4dir (@dirlist)
187     {
188       if (! opendir (DIR, $m4dir))
189         {
190           fatal "couldn't open directory `$m4dir': $!";
191         }
192
193       # We reverse the directory contents so that foo2.m4 gets
194       # used in preference to foo1.m4.
195       foreach my $file (reverse sort grep (! /^\./, readdir (DIR)))
196         {
197           # Only examine .m4 files.
198           next unless $file =~ /\.m4$/;
199
200           # Skip some files when running out of srcdir.
201           next if $file eq 'aclocal.m4';
202
203           my $fullfile = File::Spec->canonpath ("$m4dir/$file");
204             &scan_file ($type, $fullfile, 'aclocal');
205         }
206       closedir (DIR);
207     }
208 }
209
210 # Scan all the installed m4 files and construct a map.
211 sub scan_m4_files ()
212 {
213   # First, scan configure.ac.  It may contain macro definitions,
214   # or may include other files that define macros.
215   &scan_file (FT_USER, $configure_ac, 'aclocal');
216
217   # Then, scan acinclude.m4 if it exists.
218   if (-f 'acinclude.m4')
219     {
220       &scan_file (FT_USER, 'acinclude.m4', 'aclocal');
221     }
222
223   # Finally, scan all files in our search paths.
224   scan_m4_dirs (FT_USER, @user_includes);
225   scan_m4_dirs (FT_AUTOMAKE, @automake_includes);
226   scan_m4_dirs (FT_SYSTEM, @system_includes);
227
228   # Construct a new function that does the searching.  We use a
229   # function (instead of just evaluating $search in the loop) so that
230   # "die" is correctly and easily propagated if run.
231   my $search = "sub search {\nmy \$found = 0;\n";
232   foreach my $key (reverse sort keys %map)
233     {
234       $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
235                   . '"); $found = 1; }' . "\n");
236     }
237   $search .= "return \$found;\n};\n";
238   eval $search;
239   prog_error "$@\n search is $search" if $@;
240 }
241
242 ################################################################
243
244 # Add a macro to the output.
245 sub add_macro ($)
246 {
247   my ($macro) = @_;
248
249   # Ignore unknown required macros.  Either they are not really
250   # needed (e.g., a conditional AC_REQUIRE), in which case aclocal
251   # should be quiet, or they are needed and Autoconf itself will
252   # complain when we trace for macro usage later.
253   return unless defined $map{$macro};
254
255   verb "saw macro $macro";
256   $macro_seen{$macro} = 1;
257   &add_file ($map{$macro});
258 }
259
260 # scan_configure_dep ($file)
261 # --------------------------
262 # Scan a configure dependency (configure.ac, or separate m4 files)
263 # for uses of know macros and AC_REQUIREs of possibly unknown macros.
264 # Recursively scan m4_included files.
265 sub scan_configure_dep ($)
266 {
267   my ($file) = @_;
268   # Do not scan a file twice.
269   return ()
270     if exists $scanned_configure_dep{$file};
271   $scanned_configure_dep{$file} = 1;
272
273   my $mtime = mtime $file;
274   $greatest_mtime = $mtime if $greatest_mtime < $mtime;
275
276   my $contents = exists $file_contents{$file} ?
277     $file_contents{$file} : contents $file;
278
279   my $line = 0;
280   my @rlist = ();
281   my @ilist = ();
282   foreach (split ("\n", $contents))
283     {
284       ++$line;
285       # Remove comments from current line.
286       s/\bdnl\b.*$//;
287       s/\#.*$//;
288
289       while (/$m4_include_rx/go)
290         {
291           push (@ilist, $1 || $2);
292         }
293
294       while (/$ac_require_rx/go)
295         {
296           push (@rlist, $1 || $2);
297         }
298
299       # The search function is constructed dynamically by
300       # scan_m4_files.  The last parenthetical match makes sure we
301       # don't match things that look like macro assignments or
302       # AC_SUBSTs.
303       if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
304         {
305           # Macro not found, but AM_ prefix found.
306           # Make this just a warning, because we do not know whether
307           # the macro is actually used (it could be called conditionally).
308           msg ('unsupported', "$file:$line",
309                "warning: macro `$2' not found in library");
310         }
311     }
312
313   add_macro ($_) foreach (@rlist);
314   my $dirname = dirname $file;
315   &scan_configure_dep (File::Spec->rel2abs ($_, $dirname)) foreach (@ilist);
316 }
317
318 # add_file ($FILE)
319 # ----------------
320 # Add $FILE to output.
321 sub add_file ($)
322 {
323   my ($file) = @_;
324
325   # Only add a file once.
326   return if ($file_added{$file});
327   $file_added{$file} = 1;
328
329   scan_configure_dep $file;
330 }
331
332 # Point to the documentation for underquoted AC_DEFUN only once.
333 my $underquoted_manual_once = 0;
334
335 # scan_file ($TYPE, $FILE, $WHERE)
336 # -------------------------
337 # Scan a single M4 file ($FILE), and all files it includes.
338 # Return the list of included files.
339 # $TYPE is one of FT_USER, FT_AUTOMAKE, or FT_SYSTEM, depending
340 # on where the file comes from.
341 # $WHERE is the location to use in the diagnostic if the file
342 # does not exist.
343 sub scan_file ($$$)
344 {
345   my ($type, $file, $where) = @_;
346   my $base = dirname $file;
347
348   # Do not scan the same file twice.
349   return @{$file_includes{$file}} if exists $file_includes{$file};
350   # Prevent potential infinite recursion (if two files include each other).
351   return () if exists $file_contents{$file};
352
353   unshift @file_order, $file;
354
355   $file_type{$file} = $type;
356
357   fatal "$where: file `$file' does not exist" if ! -e $file;
358
359   my $fh = new Automake::XFile $file;
360   my $contents = '';
361   my @inc_files = ();
362   my %inc_lines = ();
363   while ($_ = $fh->getline)
364     {
365       # Ignore `##' lines.
366       next if /^##/;
367
368       $contents .= $_;
369       my $line = $_;
370
371       while ($line =~ /$ac_defun_rx/go)
372         {
373           if (! defined $1)
374             {
375               msg ('syntax', "$file:$.", "warning: underquoted definition of $2"
376                    . "\n  run info '(automake)Extending aclocal'\n"
377                    . "  or see http://sources.redhat.com/automake/"
378                    . "automake.html#Extending-aclocal")
379                 unless $underquoted_manual_once;
380               $underquoted_manual_once = 1;
381             }
382           my $macro = $1 || $2;
383           if (! defined $map{$macro})
384             {
385               verb "found macro $macro in $file: $.";
386               $map{$macro} = $file;
387             }
388           else
389             {
390               # Note: we used to give an error here if we saw a
391               # duplicated macro.  However, this turns out to be
392               # extremely unpopular.  It causes actual problems which
393               # are hard to work around, especially when you must
394               # mix-and-match tool versions.
395               verb "ignoring macro $macro in $file: $.";
396             }
397         }
398
399       while ($line =~ /$m4_include_rx/go)
400         {
401           my $ifile = $1 || $2;
402           # m4_include is relative to the directory of the file which
403           # perform the include, but we want paths relative to the
404           # directory where aclocal is run.  Do not use
405           # File::Spec->rel2abs, because we want to store relative
406           # paths (they might be used later of aclocal outputs an
407           # m4_include for this file, or if the user itself includes
408           # this file).
409           $ifile = "$base/$ifile"
410             unless $base eq '.' || File::Spec->file_name_is_absolute ($ifile);
411           push (@inc_files, $ifile);
412           $inc_lines{$ifile} = $.;
413         }
414     }
415   $file_contents{$file} = $contents;
416
417   # For some reason I don't understand, it does not work
418   # to do `map { scan_file ($_, ...) } @inc_files' below.
419   # With Perl 5.8.2 it undefines @inc_files.
420   my @copy = @inc_files;
421   my @all_inc_files = (@inc_files,
422                        map { scan_file ($type, $_,
423                                         "$file:$inc_lines{$_}") } @copy);
424   $file_includes{$file} = \@all_inc_files;
425   return @all_inc_files;
426 }
427
428 # strip_redundant_includes (%FILES)
429 # ---------------------------------
430 # Each key in %FILES is a file that must be present in the output.
431 # However some of these files might already include other files in %FILES,
432 # so there is no point in including them another time.
433 # This removes items of %FILES which are already included by another file.
434 sub strip_redundant_includes (%)
435 {
436   my %files = @_;
437   # Files at the end of @file_order should override those at the beginning,
438   # so it is important to preserve these trailing files.  We can remove
439   # a file A if it is going to be output before a file B that includes
440   # file A, not the converse.
441   foreach my $file (reverse @file_order)
442     {
443       next unless exists $files{$file};
444       foreach my $ifile (@{$file_includes{$file}})
445         {
446           next unless exists $files{$ifile};
447           delete $files{$ifile};
448           verb "$ifile is already included by $file";
449         }
450     }
451   return %files;
452 }
453
454 sub trace_used_macros ()
455 {
456   my %files = map { $map{$_} => 1 } keys %macro_seen;
457   $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
458   %files = strip_redundant_includes %files;
459   # configure.ac is implicitly included.
460   delete $files{$configure_ac};
461
462   my $traces = ($ENV{AUTOM4TE} || 'autom4te');
463   $traces .= " --language Autoconf-without-aclocal-m4 ";
464   # All candidate files.
465   $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
466   # All candidate macros.
467   $traces .= join (' ',
468                    (map { "--trace='$_:\$f::\$n::\$1'" } ('AC_DEFUN',
469                                                           'AC_DEFUN_ONCE',
470                                                           'AU_DEFUN')),
471                    # Do not trace $1 for all other macros as we do
472                    # not need it and it might contains harmful
473                    # characters (like newlines).
474                    (map { "--trace='$_:\$f::\$n'" } (keys %macro_seen)));
475
476   verb "running $traces $configure_ac";
477
478   my $tracefh = new Automake::XFile ("$traces $configure_ac |");
479
480   my %traced = ();
481
482   while ($_ = $tracefh->getline)
483     {
484       chomp;
485       my ($file, $macro, $arg1) = split (/::/);
486
487       $traced{$macro} = 1 if exists $macro_seen{$macro};
488
489       $map_traced_defs{$arg1} = $file
490         if ($macro eq 'AC_DEFUN'
491             || $macro eq 'AC_DEFUN_ONCE'
492             || $macro eq 'AU_DEFUN');
493     }
494
495   $tracefh->close;
496
497   return %traced;
498 }
499
500 sub scan_configure ()
501 {
502   # Make sure we include acinclude.m4 if it exists.
503   if (-f 'acinclude.m4')
504     {
505       add_file ('acinclude.m4');
506     }
507   scan_configure_dep ($configure_ac);
508 }
509
510 ################################################################
511
512 # Write output.
513 # Return 0 iff some files were installed locally.
514 sub write_aclocal ($@)
515 {
516   my ($output_file, @macros) = @_;
517   my $output = '';
518
519   my %files = ();
520   # Get the list of files containing definitions for the macros used.
521   # (Filter out unused macro definitions with $map_traced_defs.  This
522   # can happen when an Autoconf macro is conditionally defined:
523   # aclocal sees the potential definition, but this definition is
524   # actually never processed and the Autoconf implementation is used
525   # instead.)
526   for my $m (@macros)
527     {
528       $files{$map{$m}} = 1
529         if (exists $map_traced_defs{$m}
530             && $map{$m} eq $map_traced_defs{$m});
531     }
532   # Always include acinclude.m4, even if it does not appear to be used.
533   $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
534   # Do not explicitly include a file that is already indirectly included.
535   %files = strip_redundant_includes %files;
536   # Never include configure.ac :)
537   delete $files{$configure_ac};
538
539   my $installed = 0;
540
541   for my $file (grep { exists $files{$_} } @file_order)
542     {
543       # Check the time stamp of this file, and of all files it includes.
544       for my $ifile ($file, @{$file_includes{$file}})
545         {
546           my $mtime = mtime $ifile;
547           $greatest_mtime = $mtime if $greatest_mtime < $mtime;
548         }
549
550       # If the file to add looks like outside the project, copy it
551       # to the output.  The regex catches filenames starting with
552       # things like `/', `\', or `c:\'.
553       if ($file_type{$file} != FT_USER
554           || $file =~ m,^(?:\w:)?[\\/],)
555         {
556           if (!$install || $file_type{$file} != FT_SYSTEM)
557             {
558               # Copy the file into aclocal.m4.
559               $output .= $file_contents{$file} . "\n";
560             }
561           else
562             {
563               # Install the file (and any file it includes).
564               my $dest;
565               for my $ifile (@{$file_includes{$file}}, $file)
566                 {
567                   $dest = "$user_includes[0]/" . basename $ifile;
568                   verb "installing $ifile to $dest";
569                   install_file ($ifile, $dest);
570                 }
571               $installed = 1;
572             }
573         }
574       else
575         {
576           # Otherwise, simply include the file.
577           $output .= "m4_include([$file])\n";
578         }
579     }
580
581   if ($installed)
582     {
583       verb "running aclocal anew, because some files were installed locally";
584       return 0;
585     }
586
587   # Nothing to output?!
588   # FIXME: Shouldn't we diagnose this?
589   return 1 if ! length ($output);
590
591   # We used to print `# $output_file generated automatically etc.'  But
592   # this creates spurious differences when using autoreconf.  Autoreconf
593   # creates aclocal.m4t and then rename it to aclocal.m4, but the
594   # rebuild rules generated by Automake create aclocal.m4 directly --
595   # this would gives two ways to get the same file, with a different
596   # name in the header.
597   $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
598
599 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
600 # 2005  Free Software Foundation, Inc.
601 # This file is free software; the Free Software Foundation
602 # gives unlimited permission to copy and/or distribute it,
603 # with or without modifications, as long as this notice is preserved.
604
605 # This program is distributed in the hope that it will be useful,
606 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
607 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
608 # PARTICULAR PURPOSE.
609
610 $output";
611
612   # We try not to update $output_file unless necessary, because
613   # doing so invalidate Autom4te's cache and therefore slows down
614   # tools called after aclocal.
615   #
616   # We need to overwrite $output_file in the following situations.
617   #   * The --force option is in use.
618   #   * One of the dependencies is younger.
619   #     (Not updating $output_file in this situation would cause
620   #     make to call aclocal in loop.)
621   #   * The contents of the current file are different from what
622   #     we have computed.
623   if (!$force_output
624       && $greatest_mtime < mtime ($output_file)
625       && $output eq contents ($output_file))
626     {
627       verb "$output_file unchanged";
628       return 1;
629     }
630
631   verb "writing $output_file";
632
633   my $out = new Automake::XFile "> $output_file";
634   print $out $output;
635   return 1;
636 }
637
638 ################################################################
639
640 # Print usage and exit.
641 sub usage ($)
642 {
643   my ($status) = @_;
644
645   print "Usage: aclocal [OPTIONS] ...
646
647 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
648
649 Options:
650       --acdir=DIR           directory holding config files (for debugging)
651       --force               always update output file
652       --help                print this help, then exit
653   -I DIR                    add directory to search list for .m4 files
654       --install             copy third-party files to the first -I directory
655       --output=FILE         put output in FILE (default aclocal.m4)
656       --print-ac-dir        print name of directory holding m4 files, then exit
657       --verbose             don't be silent
658       --version             print version number, then exit
659   -W, --warnings=CATEGORY   report the warnings falling in CATEGORY
660
661 Warning categories include:
662   `syntax'        dubious syntactic constructs (default)
663   `unsupported'   unknown macros (default)
664   `all'           all the warnings (default)
665   `no-CATEGORY'   turn off warnings in CATEGORY
666   `none'          turn off all the warnings
667   `error'         treat warnings as errors
668
669 Report bugs to <bug-automake\@gnu.org>.\n";
670
671   exit $status;
672 }
673
674 # Print version and exit.
675 sub version()
676 {
677   print <<EOF;
678 aclocal (GNU $PACKAGE) $VERSION
679 Written by Tom Tromey <tromey\@redhat.com>
680        and Alexandre Duret-Lutz <adl\@gnu.org>.
681
682 Copyright (C) 2005 Free Software Foundation, Inc.
683 This is free software; see the source for copying conditions.  There is NO
684 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
685 EOF
686   exit 0;
687 }
688
689 # Parse command line.
690 sub parse_arguments ()
691 {
692   my $print_and_exit = 0;
693
694   my %cli_options =
695     (
696      'acdir=s'          => sub # Setting --acdir overrides both the
697                              { # automake (versioned) directory and the
698                                # public (unversioned) system directory.
699                                @automake_includes = ();
700                                @system_includes = ($_[1])
701                              },
702      'force'            => \$force_output,
703      'I=s'              => \@user_includes,
704      'install'          => \$install,
705      'output=s'         => \$output_file,
706      'print-ac-dir'     => \$print_and_exit,
707      'verbose'          => sub { setup_channel 'verb', silent => 0; },
708      'W|warnings=s'     => \&parse_warnings,
709      );
710   use Getopt::Long;
711   Getopt::Long::config ("bundling", "pass_through");
712
713   # See if --version or --help is used.  We want to process these before
714   # anything else because the GNU Coding Standards require us to
715   # `exit 0' after processing these options, and we can't guarantee this
716   # if we treat other options first.  (Handling other options first
717   # could produce error diagnostics, and in this condition it is
718   # confusing if aclocal does `exit 0'.)
719   my %cli_options_1st_pass =
720     (
721      'version' => \&version,
722      'help'    => sub { usage(0); },
723      # Recognize all other options (and their arguments) but do nothing.
724      map { $_ => sub {} } (keys %cli_options)
725      );
726   my @ARGV_backup = @ARGV;
727   Getopt::Long::GetOptions %cli_options_1st_pass
728     or exit 1;
729   @ARGV = @ARGV_backup;
730
731   # Now *really* process the options.  This time we know that --help
732   # and --version are not present, but we specify them nonetheless so
733   # that ambiguous abbreviation are diagnosed.
734   Getopt::Long::GetOptions %cli_options, 'version' => sub {}, 'help' => sub {}
735     or exit 1;
736
737   if (@ARGV)
738     {
739       my %argopts;
740       for my $k (keys %cli_options)
741         {
742           if ($k =~ /(.*)=s$/)
743             {
744               map { $argopts{(length ($_) == 1)
745                              ? "-$_" : "--$_" } = 1; } (split (/\|/, $1));
746             }
747         }
748       if (exists $argopts{$ARGV[0]})
749         {
750           fatal ("option `$ARGV[0]' requires an argument\n"
751                  . "Try `$0 --help' for more information.");
752         }
753       else
754         {
755           fatal ("unrecognized option `$ARGV[0]'\n"
756                  . "Try `$0 --help' for more information.");
757         }
758     }
759
760   if ($print_and_exit)
761     {
762       print "@system_includes\n";
763       exit 0;
764     }
765
766   if ($install && !@user_includes)
767     {
768       fatal ("--install should copy macros in the directory indicated by the"
769              . "\nfirst -I option, but no -I was supplied.");
770     }
771
772   if (! -d $system_includes[0])
773     {
774       # By default $(datadir)/aclocal doesn't exist.  We don't want to
775       # get an error in the case where we are searching the default
776       # directory and it hasn't been created.  (We know
777       # @system_includes has its default value if @automake_includes
778       # is not empty, because --acdir is the only way to change this.)
779       @system_includes = () if @automake_includes;
780     }
781   else
782     {
783       # Finally, adds any directory listed in the `dirlist' file.
784       if (open (DIRLIST, "$system_includes[0]/dirlist"))
785         {
786           while (<DIRLIST>)
787             {
788               # Ignore '#' lines.
789               next if /^#/;
790               # strip off newlines and end-of-line comments
791               s/\s*\#.*$//;
792               chomp;
793               push (@system_includes, $_) if -d $_;
794             }
795           close (DIRLIST);
796         }
797     }
798 }
799
800 ################################################################
801
802 parse_WARNINGS;             # Parse the WARNINGS environment variable.
803 parse_arguments;
804 $configure_ac = require_configure_ac;
805
806 # We may have to rerun aclocal if some file have been installed, but
807 # it should not happen more than once.  The reason we must run again
808 # is that once the file has been moved from /usr/share/aclocal/ to the
809 # local m4/ directory it appears at a new place in the search path,
810 # hence it should be output at a different position in aclocal.m4.  If
811 # we did not rerun aclocal, the next run of aclocal would produce a
812 # different aclocal.m4.
813 my $loop = 0;
814 while (1)
815   {
816     ++$loop;
817     prog_error "Too many loops." if $loop > 2;
818
819     reset_maps;
820     scan_m4_files;
821     scan_configure;
822     last if $exit_code;
823     my %macro_traced = trace_used_macros;
824     last if write_aclocal ($output_file, keys %macro_traced);
825   }
826 check_acinclude;
827
828 exit $exit_code;
829
830 ### Setup "GNU" style for perl-mode and cperl-mode.
831 ## Local Variables:
832 ## perl-indent-level: 2
833 ## perl-continued-statement-offset: 2
834 ## perl-continued-brace-offset: 0
835 ## perl-brace-offset: 0
836 ## perl-brace-imaginary-offset: 0
837 ## perl-label-offset: -2
838 ## cperl-indent-level: 2
839 ## cperl-brace-offset: 0
840 ## cperl-continued-brace-offset: 0
841 ## cperl-label-offset: -2
842 ## cperl-extra-newline-before-brace: t
843 ## cperl-merge-trailing-else: nil
844 ## cperl-continued-statement-offset: 2
845 ## End: