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