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