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