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