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