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