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