Fix for PR automake/285:
[automake.git] / aclocal.in
blobf734664b9e44d1b759fc762332d7b2df9f7dad5d
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, 2004
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;
44 use Cwd;
46 # Note that this isn't pkgdatadir, but a separate directory.
47 # Note also that the versioned directory is handled later.
48 $acdir = '@datadir@/aclocal';
49 $default_acdir = $acdir;
50 # contains a list of directories, one per line, to be added
51 # to the dirlist in addition to $acdir, as if -I had been
52 # added to the command line.  If acdir has been redirected,
53 # we will also check the specified acdir (this is done later).
54 $default_dirlist = "$default_acdir/dirlist";
56 # Some globals.
58 # configure.ac or configure.in.
59 my $configure_ac;
61 # Output file name.
62 $output_file = 'aclocal.m4';
64 # Modification time of the youngest dependency.
65 $greatest_mtime = 0;
67 # Option --force.
68 $force_output = 0;
70 # Which macros have been seen.
71 %macro_seen = ();
73 # Which files have been seen.
74 %file_seen = ();
76 # Remember the order into which we scanned the files.
77 # It's important to output the contents of aclocal.m4 in the opposite order.
78 # (Definitions in first files we have scanned should override those from
79 # later files.  So they must appear last in the output.)
80 @file_order = ();
82 # Map macro names to file names.
83 %map = ();
85 # Map file names to file contents.
86 %file_contents = ();
88 # Map file names to included files (transitively closed).
89 %file_includes = ();
91 # How much to say.
92 $verbose = 0;
94 # Matches a macro definition.
95 #   AC_DEFUN([macroname], ...)
96 # or
97 #   AC_DEFUN(macroname, ...)
98 # When macroname is `['-quoted , we accept any character in the name,
99 # except `]'.  Otherwise macroname stops on the first `]', `,', `)',
100 # or `\n' encountered.
101 $ac_defun_rx = "A[CU]_DEFUN\\((?:\\[([^]]+)\\]|([^],)\n]+))";
103 # Matches an AC_REQUIRE line.
104 $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
106 # Matches an m4_include line
107 $m4_include_rx = "(?:m4_)?s?include\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
110 ################################################################
112 # Check macros in acinclude.m4.  If one is not used, warn.
113 sub check_acinclude ()
115   foreach my $key (keys %map)
116     {
117       # FIXME: should print line number of acinclude.m4.
118       warn ("aclocal: warning: macro `$key' defined in "
119             . "acinclude.m4 but never used\n")
120         if $map{$key} eq 'acinclude.m4' && ! $macro_seen{$key};
121     }
124 ################################################################
126 # Scan all the installed m4 files and construct a map.
127 sub scan_m4_files (@)
129     local (@dirlist) = @_;
131     # First, scan configure.ac.  It may contain macro definitions,
132     # or may include other files that define macros.
133     &scan_file ($configure_ac);
135     # Then, scan acinclude.m4 if it exists.
136     if (-f 'acinclude.m4')
137     {
138         &scan_file ('acinclude.m4');
139     }
141     # Finally, scan all files in our search path.
142     local ($m4dir);
143     foreach $m4dir (@dirlist)
144     {
145         if (! opendir (DIR, $m4dir))
146           {
147             print STDERR "aclocal: couldn't open directory `$m4dir': $!\n";
148             exit 1;
149           }
151         local ($file, $fullfile);
152         # We reverse the directory contents so that foo2.m4 gets
153         # used in preference to foo1.m4.
154         foreach $file (reverse sort grep (! /^\./, readdir (DIR)))
155         {
156             # Only examine .m4 files.
157             next unless $file =~ /\.m4$/;
159             # Skip some files when running out of srcdir.
160             next if $file eq 'aclocal.m4';
162             $fullfile = $m4dir . '/' . $file;
163             &scan_file ($fullfile);
164         }
165         closedir (DIR);
166     }
168     # Construct a new function that does the searching.  We use a
169     # function (instead of just evaluating $search in the loop) so that
170     # "die" is correctly and easily propagated if run.
171     my $search = "sub search {\nmy \$found = 0;\n";
172     foreach my $key (reverse sort keys %map)
173     {
174         $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
175                     . '"); $found = 1; }' . "\n");
176     }
177     $search .= "return \$found;\n};\n";
178     eval $search;
179     die "internal error: $@\n search is $search" if $@;
182 ################################################################
184 # Add a macro to the output.
185 sub add_macro ($)
187     local ($macro) = @_;
189     # We want to ignore AC_ macros.  However, if an AC_ macro is
190     # defined in (eg) acinclude.m4, then we want to make sure we mark
191     # it as seen.
192     return if $macro =~ /^AC_/ && ! defined $map{$macro};
194     if (! defined $map{$macro})
195     {
196         warn "aclocal: macro `$macro' required but not defined\n";
197         $exit_code = 1;
198         return;
199     }
201     print STDERR "aclocal: saw macro $macro\n" if $verbose;
202     $macro_seen{$macro} = 1;
203     &add_file ($map{$macro});
206 # rel2abs ($file, $directory)
207 # ---------------------------
208 # Similar to File::Spec->rel2abs ($file, $directory), but
209 # work with Perl 5.005.  (File::Spec->rel2abs is available
210 # only in Perl 5.6.)
211 # Remove this once we require 5.6.
212 sub rel2abs ($$)
214   my ($file, $dir) = @_;
215   if (! File::Spec->file_name_is_absolute ($file))
216     {
217       $dir = cwd () . "/$dir"
218         unless File::Spec->file_name_is_absolute ($dir);
219       $file = "$dir/$file";
220     }
221   $file = File::Spec->canonpath ($file);
222   return $file;
225 # scan_configure_dep ($file)
226 # --------------------------
227 # Scan a configure dependency (configure.ac, or separate m4 files)
228 # for uses of know macros and AC_REQUIREs of possibly unknown macros.
229 # Recursively scan m4_included files.
230 my %scanned_configure_dep = ();
231 sub scan_configure_dep ($)
233   my ($file) = @_;
234   # Do not scan a file twice.
235   return ()
236     if exists $scanned_configure_dep{$file};
237   $scanned_configure_dep{$file} = 1;
239   my $mtime = mtime $file;
240   $greatest_mtime = $mtime if $greatest_mtime < $mtime;
242   my $contents = exists $file_contents{$file} ?
243     $file_contents{$file} : contents $file;
245   my $line = 0;
246   my @rlist = ();
247   my @ilist = ();
248   foreach (split ("\n", $contents))
249     {
250       ++$line;
251       # Remove comments from current line.
252       s/\bdnl\b.*$//;
253       s/\#.*$//;
255       while (/$m4_include_rx/go)
256         {
257           push (@ilist, $1 || $2);
258         }
260       while (/$ac_require_rx/go)
261         {
262           push (@rlist, $1 || $2);
263         }
265       # The search function is constructed dynamically by
266       # scan_m4_files.  The last parenthetical match makes sure we
267       # don't match things that look like macro assignments or
268       # AC_SUBSTs.
269       if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
270         {
271           # Macro not found, but AM_ prefix found.
272           warn "aclocal: $file: $line: macro `$2' not found in library\n";
273           $exit_code = 1;
274         }
275     }
277   add_macro ($_) foreach (@rlist);
278   my $dirname = dirname $file;
279   &scan_configure_dep (rel2abs ($_, $dirname)) foreach (@ilist);
282 # Add a file to output.
283 sub add_file ($)
285   local ($file) = @_;
287   # Only add a file once.
288   return if ($file_seen{$file});
289   $file_seen{$file} = 1;
291   scan_configure_dep $file;
294 # Point to the documentation for underquoted AC_DEFUN only once.
295 my $underquoted_manual_once = 0;
297 # Scan a single M4 file, and all files it includes.
298 # Return the list of included files.
299 sub scan_file ($)
301   my ($file) = @_;
302   my $base = dirname $file;
304   # Do not scan the same file twice.
305   return @$file_includes{$file} if exists $file_includes{$file};
306   # Prevent potential infinite recursion (if two files include each other).
307   return () if exists $file_contents{$file};
309   unshift @file_order, $file;
311   my $fh = new Automake::XFile $file;
312   my $contents = '';
313   my @inc_files = ();
314   while ($_ = $fh->getline)
315     {
316       # Ignore `##' lines.
317       next if /^##/;
319       $contents .= $_;
321       while (/$ac_defun_rx/go)
322         {
323           if (! defined $1)
324             {
325               print STDERR "$file:$.: warning: underquoted definition of $2\n";
326               print STDERR "  run info '(automake)Extending aclocal'\n"
327                 . "  or see http://sources.redhat.com/automake/"
328                 . "automake.html#Extending%20aclocal\n"
329                 unless $underquoted_manual_once;
330               $underquoted_manual_once = 1;
331             }
332           my $macro = $1 || $2;
333           if (! defined $map{$macro})
334             {
335               print STDERR "aclocal: found macro $macro in $file: $.\n"
336                 if $verbose;
337               $map{$macro} = $file;
338             }
339           else
340             {
341               # Note: we used to give an error here if we saw a
342               # duplicated macro.  However, this turns out to be
343               # extremely unpopular.  It causes actual problems which
344               # are hard to work around, especially when you must
345               # mix-and-match tool versions.
346               print STDERR "aclocal: ignoring macro $macro in $file: $.\n"
347                 if $verbose;
348             }
349         }
351       while (/$m4_include_rx/go)
352         {
353           my $ifile = $1 || $2;
354           # m4_include is relative to the directory of the file which
355           # perform the include, but we want paths relative to the
356           # directory where aclocal is run.  Do not use
357           # File::Spec->rel2abs, because we want to store relative
358           # paths (they might be used later of aclocal outputs an
359           # m4_include for this file, or if the user itself includes
360           # this file).
361           $ifile = "$base/$ifile"
362             unless $base eq '.' || File::Spec->file_name_is_absolute ($ifile);
363           push (@inc_files, $ifile);
364         }
365     }
366   $file_contents{$file} = $contents;
368   # For some reason I don't understand, it does not work
369   # to do `map { scan_file ($_) } @inc_files' below.
370   # With Perl 5.8.2 it undefines @inc_files.
371   my @copy = @inc_files;
372   my @all_inc_files = (@inc_files, map { scan_file ($_) } @copy);
373   $file_includes{$file} = \@all_inc_files;
374   return @all_inc_files;
377 # strip_redundant_includes (%FILES)
378 # ---------------------------------
379 # Each key in %FILES is a file that must be present in the output.
380 # However some of these files might already include other files in %FILES,
381 # so there is no point in including them another time.
382 # This removes items of %FILES which are already included by another file.
383 sub strip_redundant_includes (%)
385   my %files = @_;
386   # Files at the end of @file_order should override those at the beginning,
387   # so it is important to preserve these trailing files.  We can remove
388   # a file A if it is going to be output before a file B that includes
389   # file A, not the converse.
390   foreach my $file (reverse @file_order)
391     {
392       next unless exists $files{$file};
393       foreach my $ifile (@{$file_includes{$file}})
394         {
395           next unless exists $files{$ifile};
396           delete $files{$ifile};
397           print STDERR "$ifile is already included by $file\n"
398             if $verbose;
399         }
400     }
401   return %files;
404 sub trace_used_macros ()
406   my %files = map { $map{$_} => 1 } keys %macro_seen;
407   $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
408   %files = strip_redundant_includes %files;
409   # configure.ac is implicitly included.
410   delete $files{$configure_ac};
412   my $traces = ($ENV{AUTOM4TE} || 'autom4te');
413   $traces .= " --language Autoconf-without-aclocal-m4 ";
414   # All candidate files.
415   $traces .= join (' ', grep { exists $files{$_} } @file_order) . " ";
416   # All candidate macros.
417   $traces .= join (' ', map { "--trace='$_:\$n'" } (keys %macro_seen));
419   print STDERR "aclocal: running $traces $configure_ac\n" if $verbose;
421   my $tracefh = new Automake::XFile ("$traces $configure_ac |");
423   my %traced = ();
425   while ($_ = $tracefh->getline)
426     {
427       chomp;
428       $traced{$_} = 1 if $macro_seen{$_};
429     }
431   $tracefh->close;
433   return %traced;
436 sub scan_configure ()
438   # Make sure we include acinclude.m4 if it exists.
439   if (-f 'acinclude.m4')
440     {
441       add_file ('acinclude.m4');
442     }
443   scan_configure_dep ($configure_ac);
446 ################################################################
448 # Write output.
449 sub write_aclocal ($@)
451   my ($output_file, @macros) = @_;
452   my $output = '';
454   my %files = map { $map{$_} => 1 } @macros;
455   $files{'acinclude.m4'} = 1 if -f 'acinclude.m4';
456   %files = strip_redundant_includes %files;
457   delete $files{$configure_ac};
459   for $file (grep { exists $files{$_} } @file_order)
460     {
461       # Check the time stamp of this file, and all files it includes.
462       for my $ifile ($file, @{$file_includes{$file}})
463         {
464           my $mtime = mtime $ifile;
465           $greatest_mtime = $mtime if $greatest_mtime < $mtime;
466         }
468       # If the file to add looks like outside the project, copy it
469       # to the output.  The regex catches filenames starting with
470       # things like `/', `\', or `c:\'.
471       if ($file =~ m,^(?:\w:)?[\\/],)
472         {
473           $output .= $file_contents{$file} . "\n";
474         }
475       else
476         {
477           # Otherwise, simply include the file.
478           $output .= "m4_include([$file])\n";
479         }
480     }
482   # Nothing to output?!
483   # FIXME: Shouldn't we diagnose this?
484   return if ! length ($output);
486   # We used to print `# $output_file generated automatically etc.'  But
487   # this creates spurious differences when using autoreconf.  Autoreconf
488   # creates aclocal.m4t and then rename it to aclocal.m4, but the
489   # rebuild rules generated by Automake create aclocal.m4 directly --
490   # this would gives two ways to get the same file, with a different
491   # name in the header.
492   $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
494 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
495 # Free Software Foundation, Inc.
496 # This file is free software; the Free Software Foundation
497 # gives unlimited permission to copy and/or distribute it,
498 # with or without modifications, as long as this notice is preserved.
500 # This program is distributed in the hope that it will be useful,
501 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
502 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
503 # PARTICULAR PURPOSE.
505 $output";
507   # We try not to update $output_file unless necessary, because
508   # doing so invalidate Autom4te's cache and therefore slows down
509   # tools called after aclocal.
510   #
511   # We need to overwrite $output_file in the following situations.
512   #   * The --force option is in use.
513   #   * One of the dependencies is younger.
514   #     (Not updating $output_file in this situation would cause
515   #     make to call aclocal in loop.)
516   #   * The contents of the current file are different from what
517   #     we have computed.
518   if (!$force_output
519       && $greatest_mtime < mtime ($output_file)
520       && $output eq contents ($output_file))
521     {
522       print STDERR "aclocal: $output_file unchanged\n" if $verbose;
523       return;
524     }
526   print STDERR "aclocal: writing $output_file\n" if $verbose;
528   my $out = new Automake::XFile "> $output_file";
529   print $out $output;
530   return;
533 ################################################################
535 # Print usage and exit.
536 sub usage ($)
538   local ($status) = @_;
540   print "Usage: aclocal [OPTIONS] ...\n\n";
541   print "\
542 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
544   --acdir=DIR           directory holding config files
545   --help                print this help, then exit
546   -I DIR                add directory to search list for .m4 files
547   --force               always update output file
548   --output=FILE         put output in FILE (default aclocal.m4)
549   --print-ac-dir        print name of directory holding m4 files
550   --verbose             don't be silent
551   --version             print version number, then exit
553 Report bugs to <bug-automake\@gnu.org>.\n";
555   exit $status;
558 # Parse command line.
559 sub parse_arguments (@)
561   local (@arglist) = @_;
562   local (@dirlist);
563   local ($print_and_exit) = 0;
565   while (@arglist)
566     {
567       if ($arglist[0] =~ /^--acdir=(.+)$/)
568         {
569           $acdir = $1;
570         }
571       elsif ($arglist[0] =~/^--output=(.+)$/)
572         {
573           $output_file = $1;
574         }
575       elsif ($arglist[0] eq '-I')
576         {
577           shift (@arglist);
578           push (@dirlist, $arglist[0]);
579         }
580       elsif ($arglist[0] eq '--print-ac-dir')
581         {
582           $print_and_exit = 1;
583         }
584       elsif ($arglist[0] eq '--force')
585         {
586           $force_output = 1;
587         }
588       elsif ($arglist[0] eq '--verbose')
589         {
590           ++$verbose;
591         }
592       elsif ($arglist[0] eq '--version')
593         {
594           print "aclocal (GNU $PACKAGE) $VERSION\n";
595           print "Written by Tom Tromey <tromey\@redhat.com>\n\n";
596           print "Copyright (C) 2004 Free Software Foundation, Inc.\n";
597           print "This is free software; see the source for copying conditions.  There is NO\n";
598           print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
599           exit 0;
600         }
601       elsif ($arglist[0] eq '--help')
602         {
603           &usage (0);
604         }
605       else
606         {
607           print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
608           exit 1;
609         }
611       shift (@arglist);
612     }
614   if ($print_and_exit)
615     {
616       print $acdir, "\n";
617       exit 0;
618     }
620   $default_dirlist="$acdir/dirlist"
621     if $acdir ne $default_acdir;
623   # Search the versioned directory near the end, and then the
624   # unversioned directory last.  Only do this if the user didn't
625   # override acdir.
626   push (@dirlist, "$acdir-$APIVERSION")
627     if $acdir eq $default_acdir;
629   # By default $(datadir)/aclocal doesn't exist.  We don't want to
630   # get an error in the case where we are searching the default
631   # directory and it hasn't been created.
632   push (@dirlist, $acdir)
633     unless $acdir eq $default_acdir && ! -d $acdir;
635   # Finally, adds any directory listed in the `dirlist' file.
636   if (open (DEFAULT_DIRLIST, $default_dirlist))
637     {
638       while (<DEFAULT_DIRLIST>)
639         {
640           # Ignore '#' lines.
641           next if /^#/;
642           # strip off newlines and end-of-line comments
643           s/\s*\#.*$//;
644           chomp ($contents=$_);
645           if (-d $contents )
646             {
647               push (@dirlist, $contents);
648             }
649         }
650       close (DEFAULT_DIRLIST);
651     }
653   return @dirlist;
656 ################################################################
658 local (@dirlist) = parse_arguments (@ARGV);
659 $configure_ac = require_configure_ac;
660 scan_m4_files (@dirlist);
661 scan_configure;
662 if (! $exit_code)
663   {
664     my %macro_traced = trace_used_macros;
665     write_aclocal ($output_file, keys %macro_traced);
666   }
667 check_acinclude;
669 exit $exit_code;
671 ### Setup "GNU" style for perl-mode and cperl-mode.
672 ## Local Variables:
673 ## perl-indent-level: 2
674 ## perl-continued-statement-offset: 2
675 ## perl-continued-brace-offset: 0
676 ## perl-brace-offset: 0
677 ## perl-brace-imaginary-offset: 0
678 ## perl-label-offset: -2
679 ## cperl-indent-level: 2
680 ## cperl-brace-offset: 0
681 ## cperl-continued-brace-offset: 0
682 ## cperl-label-offset: -2
683 ## cperl-extra-newline-before-brace: t
684 ## cperl-merge-trailing-else: nil
685 ## cperl-continued-statement-offset: 2
686 ## End: