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