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