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