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