* automake.in (scan_texinfo_file): Do not compare $outfile to ''
[automake.git] / aclocal.in
blobe9c64712761464251bb23b1dd646e118381fd431
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
6     if 0;
8 # aclocal - create aclocal.m4 by scanning configure.ac
10 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
11 #           Free Software Foundation, Inc.
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2, or (at your option)
16 # any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 # 02111-1307, USA.
28 # Written by Tom Tromey <tromey@redhat.com>.
30 BEGIN
32   my $perllibdir = $ENV{'perllibdir'} || '@datadir@/@PACKAGE@-@APIVERSION@';
33   unshift @INC, (split ':', $perllibdir);
36 use Automake::Config;
37 use Automake::General;
38 use Automake::Configure_ac;
39 use Automake::Channels;
40 use Automake::XFile;
41 use Automake::FileUtils;
42 use File::Basename;
43 use File::stat;
45 # Note that this isn't pkgdatadir, but a separate directory.
46 # Note also that the versioned directory is handled later.
47 $acdir = '@datadir@/aclocal';
48 $default_acdir = $acdir;
49 # contains a list of directories, one per line, to be added
50 # to the dirlist in addition to $acdir, as if -I had been
51 # added to the command line.  If acdir has been redirected,
52 # we will also check the specified acdir (this is done later).
53 $default_dirlist = "$default_acdir/dirlist";
55 # Some globals.
57 # configure.ac or configure.in.
58 my $configure_ac;
60 # Output file name.
61 $output_file = 'aclocal.m4';
63 # Modification time of the youngest dependency.
64 $greatest_mtime = 0;
66 # Option --force.
67 $force_output = 0;
69 # Which macros have been seen.
70 %macro_seen = ();
72 # Which files have been seen.
73 %file_seen = ();
75 # Remember the order into which we scanned the files.
76 # It's important to output the contents of aclocal.m4 in the opposite order.
77 # (Definitions in first files we have scanned should override those from
78 # later files.  So they must appear last in the output.)
79 @file_order = ();
81 # Map macro names to file names.
82 %map = ();
84 # Map file names to file contents.
85 %file_contents = ();
87 # Map file names to included files (transitively closed).
88 %file_includes = ();
90 # How much to say.
91 $verbose = 0;
93 # Matches a macro definition.
94 #   AC_DEFUN([macroname], ...)
95 # or
96 #   AC_DEFUN(macroname, ...)
97 # When macroname is `['-quoted , we accept any character in the name,
98 # except `]'.  Otherwise macroname stops on the first `]', `,', `)',
99 # or `\n' encountered.
100 $ac_defun_rx = "A[CU]_DEFUN\\((?:\\[([^]]+)\\]|([^],)\n]+))";
102 # Matches an AC_REQUIRE line.
103 $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
105 # Matches an m4_include line
106 $m4_include_rx = "(?:m4_)?s?include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
109 ################################################################
111 # Check macros in acinclude.m4.  If one is not used, warn.
112 sub check_acinclude ()
114   foreach my $key (keys %map)
115     {
116       # FIXME: should print line number of acinclude.m4.
117       warn ("aclocal: warning: macro `$key' defined in "
118             . "acinclude.m4 but never used\n")
119         if $map{$key} eq 'acinclude.m4' && ! $macro_seen{$key};
120     }
123 ################################################################
125 # Scan all the installed m4 files and construct a map.
126 sub scan_m4_files (@)
128     local (@dirlist) = @_;
130     # First, scan configure.ac.  It may contain macro definitions,
131     # or may include other files that define macros.
132     &scan_file ($configure_ac);
134     # Then, scan acinclude.m4 if it exists.
135     if (-f 'acinclude.m4')
136     {
137         &scan_file ('acinclude.m4');
138     }
140     # Finally, scan all files in our search path.
141     local ($m4dir);
142     foreach $m4dir (@dirlist)
143     {
144         if (! opendir (DIR, $m4dir))
145           {
146             print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
147             exit 1;
148           }
150         local ($file, $fullfile);
151         # We reverse the directory contents so that foo2.m4 gets
152         # used in preference to foo1.m4.
153         foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
154         {
155             # Only examine .m4 files.
156             next unless $file =~ /\.m4$/;
158             # Skip some files when running out of srcdir.
159             next if $file eq 'aclocal.m4';
161             $fullfile = $m4dir . '/' . $file;
162             &scan_file ($fullfile);
163         }
164         closedir (DIR);
165     }
167     # Construct a new function that does the searching.  We use a
168     # function (instead of just evaluating $search in the loop) so that
169     # "die" is correctly and easily propagated if run.
170     my $search = "sub search {\nmy \$found = 0;\n";
171     foreach my $key (reverse sort keys %map)
172     {
173         $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
174                     . '"); $found = 1; }' . "\n");
175     }
176     $search .= "return \$found;\n};\n";
177     eval $search;
178     die "internal error: $@\n search is $search" if $@;
181 ################################################################
183 # Add a macro to the output.
184 sub add_macro ($)
186     local ($macro) = @_;
188     # We want to ignore AC_ macros.  However, if an AC_ macro is
189     # defined in (eg) acinclude.m4, then we want to make sure we mark
190     # it as seen.
191     return if $macro =~ /^AC_/ && ! defined $map{$macro};
193     if (! defined $map{$macro})
194     {
195         warn "aclocal: macro `$macro' required but not defined\n";
196         $exit_code = 1;
197         return;
198     }
200     print STDERR "aclocal: saw macro $macro\n" if $verbose;
201     $macro_seen{$macro} = 1;
202     &add_file ($map{$macro});
205 # scan_contents ($file)
206 # --------------------------------
207 my %scanned_configure_dep = ();
208 sub scan_configure_dep ($)
210   my ($file) = @_;
211   # Do not scan a file twice.
212   return ()
213     if exists $scanned_configure_dep{$file};
214   $scanned_configure_dep{$file} = 1;
216   my $mtime = mtime $file;
217   $greatest_mtime = $mtime if $greatest_mtime < $mtime;
219   my $contents = exists $file_contents{$file} ?
220     $file_contents{$file} : contents $file;
222   my $line = 0;
223   my @rlist = ();
224   my @ilist = ();
225   foreach (split ("\n", $contents))
226     {
227       ++$line;
228       # Remove comments from current line.
229       s/\bdnl\b.*$//;
230       s/\#.*$//;
232       while (/$m4_include_rx/go)
233         {
234           push (@ilist, $1 || $2);
235         }
237       while (/$ac_require_rx/go)
238         {
239           push (@rlist, $1 || $2);
240         }
242       # The search function is constructed dynamically by
243       # scan_m4_files.  The last parenthetical match makes sure we
244       # don't match things that look like macro assignments or
245       # AC_SUBSTs.
246       if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
247         {
248           # Macro not found, but AM_ prefix found.
249           warn "aclocal: $file: $line: macro `$2' not found in library\n";
250           $exit_code = 1;
251         }
252     }
254   add_macro ($_) foreach (@rlist);
255   my $dirname = dirname $file;
256   &scan_configure_dep (File::Spec->rel2abs ($_, $dirname)) foreach (@ilist);
259 # Add a file to output.
260 sub add_file ($)
262   local ($file) = @_;
264   # Only add a file once.
265   return if ($file_seen{$file});
266   $file_seen{$file} = 1;
268   scan_configure_dep $file;
271 # Point to the documentation for underquoted AC_DEFUN only once.
272 my $underquoted_manual_once = 0;
274 # Scan a single M4 file, and all files it includes.
275 # Return the list of included files.
276 sub scan_file ($)
278   my ($file) = @_;
279   my $base = dirname $file;
281   # Do not scan the same file twice.
282   return @$file_includes{$file} if exists $file_includes{$file};
283   # Prevent potential infinite recursion (if two files include each other).
284   return () if exists $file_contents{$file};
286   unshift @file_order, $file;
288   my $fh = new Automake::XFile $file;
289   my $contents = '';
290   my @inc_files = ();
291   while ($_ = $fh->getline)
292     {
293       # Ignore `##' lines.
294       next if /^##/;
296       $contents .= $_;
298       while (/$ac_defun_rx/go)
299         {
300           if (! defined $1)
301             {
302               print STDERR "$file:$.: warning: underquoted definition of $2\n";
303               print STDERR "  run info '(automake)Extending aclocal'\n"
304                 . "  or see http://sources.redhat.com/automake/"
305                 . "automake.html#Extending%20aclocal\n"
306                 unless $underquoted_manual_once;
307               $underquoted_manual_once = 1;
308             }
309           my $macro = $1 || $2;
310           if (! defined $map{$macro})
311             {
312               print STDERR "aclocal: found macro $macro in $file: $.\n"
313                 if $verbose;
314               $map{$macro} = $file;
315             }
316           else
317             {
318               # Note: we used to give an error here if we saw a
319               # duplicated macro.  However, this turns out to be
320               # extremely unpopular.  It causes actual problems which
321               # are hard to work around, especially when you must
322               # mix-and-match tool versions.
323               print STDERR "aclocal: ignoring macro $macro in $file: $.\n"
324                 if $verbose;
325             }
326         }
328       while (/$m4_include_rx/go)
329         {
330           my $ifile = $1 || $2;
331           # m4_include is relative to the directory of the file which
332           # perform the include, but we want paths relative to the
333           # directory where aclocal is run.  Do not use
334           # File::Spec->rel2abs, because we want to store relative
335           # paths (they might be used later of aclocal outputs an
336           # m4_include for this file, or if the user itself includes
337           # this file).
338           $ifile = "$base/$ifile"
339             unless $base eq '.' || File::Spec->file_name_is_absolute ($ifile);
340           push (@inc_files, $ifile);
341         }
342     }
343   $file_contents{$file} = $contents;
345   # For some reason I don't understand, it does not work
346   # to do `map { scan_file ($_) } @inc_files' below.
347   # With Perl 5.8.2 it undefines @inc_files.
348   my @copy = @inc_files;
349   my @all_inc_files = (@inc_files, map { scan_file ($_) } @copy);
350   $file_includes{$file} = \@all_inc_files;
351   return @all_inc_files;
354 # strip_redundant_includes (%FILES)
355 # ---------------------------------
356 # Each key in %FILES is a file that must be present in the output.
357 # However some of these files might already include other files in %FILES,
358 # so there is no point in including them another time.
359 # This removes items of %FILES which are already included by another file.
360 sub strip_redundant_includes (%)
362   my %files = @_;
363   # Files at the end of @file_order should override those at the beginning,
364   # so it is important to preserve these trailing files.  We can remove
365   # a file A if it is going to be output before a file B that includes
366   # file A, not the converse.
367   foreach my $file (reverse @file_order)
368     {
369       next unless exists $files{$file};
370       foreach my $ifile (@{$file_includes{$file}})
371         {
372           next unless exists $files{$ifile};
373           delete $files{$ifile};
374           print STDERR "$ifile is already included by $file\n"
375             if $verbose;
376         }
377     }
378   return %files;
381 sub trace_used_macros ()
383   my %files = map { $map{$_} => 1 } keys %macro_seen;
384   $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
385   %files = strip_redundant_includes %files;
386   # configure.ac is implicitly included.
387   delete $files{$configure_ac};
389   my $traces = ($ENV{AUTOM4TE} || 'autom4te');
390   $traces .= " --language Autoconf-without-aclocal-m4 ";
391   # All candidate files.
392   $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
393   # All candidate macros.
394   $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
396   print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
398   my $tracefh = new Automake::XFile ("$traces $configure_ac |");
400   my %traced = ();
402   while ($_ = $tracefh->getline)
403     {
404       chomp;
405       $traced{$_} = 1 if $macro_seen{$_};
406     }
408   $tracefh->close;
410   return %traced;
413 sub scan_configure ()
415   # Make sure we include acinclude.m4 if it exists.
416   if (-f 'acinclude.m4')
417     {
418       add_file ('acinclude.m4');
419     }
420   scan_configure_dep ($configure_ac);
423 ################################################################
425 # Write output.
426 sub write_aclocal ($@)
428   my ($output_file, @macros) = @_;
429   my $output = '';
431   my %files = map { $map{$_} => 1 } @macros;
432   $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
433   %files = strip_redundant_includes %files;
434   delete $files{$configure_ac};
436   for $file (grep { exists $files{$_} } @file_order)
437     {
438       # Check the time stamp of this file, and all files it includes.
439       for my $ifile ($file, @{$file_includes{$file}})
440         {
441           my $mtime = mtime $ifile;
442           $greatest_mtime = $mtime if $greatest_mtime < $mtime;
443         }
445       # If the file to add looks like outside the project, copy it
446       # to the output.  The regex catches filenames starting with
447       # things like `/', `\', or `c:\'.
448       if ($file =~ m,^(?:\w:)?[\\/],)
449         {
450           $output .= $file_contents{$file} . "\n";
451         }
452       else
453         {
454           # Otherwise, simply include the file.
455           $output .= "m4_include([$file])\n";
456         }
457     }
459   # Nothing to output?!
460   # FIXME: Shouldn't we diagnose this?
461   return if ! length ($output);
463   # We used to print `# $output_file generated automatically etc.'  But
464   # this creates spurious differences when using autoreconf.  Autoreconf
465   # creates aclocal.m4t and then rename it to aclocal.m4, but the
466   # rebuild rules generated by Automake create aclocal.m4 directly --
467   # this would gives two ways to get the same file, with a different
468   # name in the header.
469   $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
471 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
472 # Free Software Foundation, Inc.
473 # This file is free software; the Free Software Foundation
474 # gives unlimited permission to copy and/or distribute it,
475 # with or without modifications, as long as this notice is preserved.
477 # This program is distributed in the hope that it will be useful,
478 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
479 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
480 # PARTICULAR PURPOSE.
482 $output";
484   # We try not to update $output_file unless necessary, because
485   # doing so invalidate Autom4te's cache and therefore slows down
486   # tools called after aclocal.
487   #
488   # We need to overwrite $output_file in the following situations.
489   #   * The --force option is in use.
490   #   * One of the dependencies is younger.
491   #     (Not updating $output_file in this situation would cause
492   #     make to call aclocal in loop.)
493   #   * The contents of the current file are different from what
494   #     we have computed.
495   if (!$force_output
496       && $greatest_mtime < mtime ($output_file)
497       && $output eq contents ($output_file))
498     {
499       print STDERR "aclocal: $output_file unchanged\n" if $verbose;
500       return;
501     }
503   print STDERR "aclocal: writing $output_file\n" if $verbose;
505   my $out = new Automake::XFile "> $output_file";
506   print $out $output;
507   return;
510 ################################################################
512 # Print usage and exit.
513 sub usage ($)
515   local ($status) = @_;
517   print "Usage: aclocal [OPTIONS] ...\n\n";
518   print "\
519 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
521   --acdir=DIR           directory holding config files
522   --help                print this help, then exit
523   -I DIR                add directory to search list for .m4 files
524   --force               always update output file
525   --output=FILE         put output in FILE (default aclocal.m4)
526   --print-ac-dir        print name of directory holding m4 files
527   --verbose             don't be silent
528   --version             print version number, then exit
530 Report bugs to <bug-automake\@gnu.org>.\n";
532   exit $status;
535 # Parse command line.
536 sub parse_arguments (@)
538   local (@arglist) = @_;
539   local (@dirlist);
540   local ($print_and_exit) = 0;
542   while (@arglist)
543     {
544       if ($arglist[0] =~ /^--acdir=(.+)$/)
545         {
546           $acdir = $1;
547         }
548       elsif ($arglist[0] =~/^--output=(.+)$/)
549         {
550           $output_file = $1;
551         }
552       elsif ($arglist[0] eq '-I')
553         {
554           shift (@arglist);
555           push (@dirlist, $arglist[0]);
556         }
557       elsif ($arglist[0] eq '--print-ac-dir')
558         {
559           $print_and_exit = 1;
560         }
561       elsif ($arglist[0] eq '--force')
562         {
563           $force_output = 1;
564         }
565       elsif ($arglist[0] eq '--verbose')
566         {
567           ++$verbose;
568         }
569       elsif ($arglist[0] eq '--version')
570         {
571           print "aclocal (GNU $PACKAGE) $VERSION\n\n";
572           print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
573           print "This is free software; see the source for copying conditions.  There is NO\n";
574           print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
575           print "Written by Tom Tromey <tromey\@redhat.com>\n";
576           exit 0;
577         }
578       elsif ($arglist[0] eq '--help')
579         {
580           &usage (0);
581         }
582       else
583         {
584           print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
585           exit 1;
586         }
588       shift (@arglist);
589     }
591   if ($print_and_exit)
592     {
593       print $acdir, "\n";
594       exit 0;
595     }
597   $default_dirlist="$acdir/dirlist"
598     if $acdir ne $default_acdir;
600   # Search the versioned directory near the end, and then the
601   # unversioned directory last.  Only do this if the user didn't
602   # override acdir.
603   push (@dirlist, "$acdir-$APIVERSION")
604     if $acdir eq $default_acdir;
606   # By default $(datadir)/aclocal doesn't exist.  We don't want to
607   # get an error in the case where we are searching the default
608   # directory and it hasn't been created.
609   push (@dirlist, $acdir)
610     unless $acdir eq $default_acdir && ! -d $acdir;
612   # Finally, adds any directory listed in the `dirlist' file.
613   if (open (DEFAULT_DIRLIST, $default_dirlist))
614     {
615       while (<DEFAULT_DIRLIST>)
616         {
617           # Ignore '#' lines.
618           next if /^#/;
619           # strip off newlines and end-of-line comments
620           s/\s*\#.*$//;
621           chomp ($contents=$_);
622           if (-d $contents )
623             {
624               push (@dirlist, $contents);
625             }
626         }
627       close (DEFAULT_DIRLIST);
628     }
630   return @dirlist;
633 ################################################################
635 local (@dirlist) = parse_arguments (@ARGV);
636 $configure_ac = require_configure_ac;
637 scan_m4_files (@dirlist);
638 scan_configure;
639 if (! $exit_code)
640   {
641     my %macro_traced = trace_used_macros;
642     write_aclocal ($output_file, keys %macro_traced);
643   }
644 check_acinclude;
646 exit $exit_code;
648 ### Setup "GNU" style for perl-mode and cperl-mode.
649 ## Local Variables:
650 ## perl-indent-level: 2
651 ## perl-continued-statement-offset: 2
652 ## perl-continued-brace-offset: 0
653 ## perl-brace-offset: 0
654 ## perl-brace-imaginary-offset: 0
655 ## perl-label-offset: -2
656 ## cperl-indent-level: 2
657 ## cperl-brace-offset: 0
658 ## cperl-continued-brace-offset: 0
659 ## cperl-label-offset: -2
660 ## cperl-extra-newline-before-brace: t
661 ## cperl-merge-trailing-else: nil
662 ## cperl-continued-statement-offset: 2
663 ## End: