d8a4f69ac06c3033525801bf70a5fdc1d3244fb9
[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
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 $prefix = "@prefix@";
33   my $perllibdir = $ENV{'perllibdir'} || "@datadir@/@PACKAGE@-@APIVERSION@";
34   unshift @INC, "$perllibdir";
35 }
36
37 use Automake::General;
38 use Automake::XFile;
39
40 # Some constants.
41 $VERSION = "@VERSION@";
42 $APIVERSION = "@APIVERSION@";
43 $PACKAGE = "@PACKAGE@";
44 $prefix = "@prefix@";
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 # Exit status.
58 $exit_status = 0;
59
60 # Name of the top autoconf input: `configure.ac' or `configure.in'.
61 $configure_ac = find_configure_ac;
62
63 # Text to output.
64 $output = '';
65
66 # Output file name.
67 $output_file = 'aclocal.m4';
68
69 # Which macros have been seen.
70 %macro_seen = ();
71
72 # Which files have been seen.
73 %file_seen = ();
74
75 # Map macro names to file names.
76 %map = ();
77
78 # Map file names to file contents.
79 %file_contents = ();
80
81 # How much to say.
82 $verbose = 0;
83
84 # Matches a macro definition.
85 $ac_defun_rx = "A[CU]_DEFUN\\(\\[?([^],)\n]+)\\]?";
86
87 # Matches an AC_REQUIRE line.
88 $ac_require_rx = "AC_REQUIRE\\(\\[?([^])]*)\\]?\\)";
89
90 \f
91
92 local (@dirlist) = &parse_arguments (@ARGV);
93 &scan_m4_files (@dirlist);
94 &scan_configure;
95 if (! $exit_status)
96 {
97     &write_aclocal;
98 }
99 &check_acinclude;
100
101 exit $exit_status;
102
103 ################################################################
104
105 # Print usage and exit.
106 sub usage ($)
107 {
108     local ($status) = @_;
109
110     print "Usage: aclocal [OPTIONS] ...\n\n";
111     print "\
112 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
113
114   --acdir=DIR           directory holding config files
115   --help                print this help, then exit
116   -I DIR                add directory to search list for .m4 files
117   --output=FILE         put output in FILE (default aclocal.m4)
118   --print-ac-dir        print name of directory holding m4 files
119   --verbose             don't be silent
120   --version             print version number, then exit
121
122 Report bugs to <bug-automake\@gnu.org>.\n";
123
124     exit $status;
125 }
126
127 # Parse command line.
128 sub parse_arguments (@)
129 {
130     local (@arglist) = @_;
131     local (@dirlist);
132     local ($print_and_exit) = 0;
133
134     while (@arglist)
135     {
136         if ($arglist[0] =~ /^--acdir=(.+)$/)
137         {
138             $acdir = $1;
139         }
140         elsif ($arglist[0] =~/^--output=(.+)$/)
141         {
142             $output_file = $1;
143         }
144         elsif ($arglist[0] eq '-I')
145         {
146             shift (@arglist);
147             push (@dirlist, $arglist[0]);
148         }
149         elsif ($arglist[0] eq '--print-ac-dir')
150         {
151             $print_and_exit = 1;
152         }
153         elsif ($arglist[0] eq '--verbose')
154         {
155             ++$verbose;
156         }
157         elsif ($arglist[0] eq '--version')
158         {
159             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
160             print "Copyright (C) 2002 Free Software Foundation, Inc.\n";
161             print "This is free software; see the source for copying conditions.  There is NO\n";
162             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
163             print "Written by Tom Tromey <tromey\@redhat.com>\n";
164             exit 0;
165         }
166         elsif ($arglist[0] eq '--help')
167         {
168             &usage (0);
169         }
170         else
171         {
172             die "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
173         }
174
175         shift (@arglist);
176     }
177
178     if ($print_and_exit)
179     {
180         print $acdir, "\n";
181         exit 0;
182     }
183
184     $default_dirlist="$acdir/dirlist"
185         if $acdir ne $default_acdir;
186
187     # Search the versioned directory near the end, and then the
188     # unversioned directory last.  Only do this if the user didn't
189     # override acdir.
190     push (@dirlist, "$acdir-$APIVERSION")
191         if $acdir eq $default_acdir;
192
193     # By default $(datadir)/aclocal doesn't exist.  We don't want to
194     # get an error in the case where we are searching the default
195     # directory and it hasn't been created.
196     push (@dirlist, $acdir)
197         unless $acdir eq $default_acdir && ! -d $acdir;
198
199     # Finally, adds any directory listed in the `dirlist' file.
200     if (open (DEFAULT_DIRLIST, $default_dirlist))
201     {
202         while (<DEFAULT_DIRLIST>)
203         {
204             # Ignore '#' lines.
205             next if /^#/;
206             # strip off newlines and end-of-line comments
207             s/\s*\#.*$//;
208             chomp ($contents=$_);
209             if (-d $contents )
210             {
211                 push (@dirlist, $contents);
212             }
213         }
214         close (DEFAULT_DIRLIST);
215     }
216
217
218     return @dirlist;
219 }
220
221 ################################################################
222
223 sub scan_configure ()
224 {
225     die "aclocal: `configure.ac' or `configure.in' is required\n"
226         if !$configure_ac;
227
228     open (CONFIGURE, $configure_ac)
229         || die "aclocal: couldn't open `$configure_ac': $!\n";
230
231     # Make sure we include acinclude.m4 if it exists.
232     if (-f 'acinclude.m4')
233     {
234         &add_file ('acinclude.m4');
235     }
236
237     while (<CONFIGURE>)
238     {
239         # Remove comments from current line.
240         s/\bdnl\b.*$//;
241         s/\#.*$//;
242
243         # Search for things we know about.  The "search" sub is
244         # constructed dynamically by scan_m4_files.  The last
245         # parenthethical match makes sure we don't match things that
246         # look like macro assignments or AC_SUBSTs.
247         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
248         {
249             # Macro not found, but AM_ prefix found.
250             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
251             $exit_status = 1;
252         }
253     }
254
255     close (CONFIGURE);
256 }
257
258 ################################################################
259
260 # Check macros in acinclude.m4.  If one is not used, warn.
261 sub check_acinclude ()
262 {
263     local ($key);
264
265     foreach $key (keys %map)
266     {
267         next unless $map{$key} eq 'acinclude.m4';
268         if (! $macro_seen{$key})
269         {
270             # FIXME: should print line number of acinclude.m4.
271             warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
272         }
273     }
274 }
275
276 ################################################################
277
278 # Scan all the installed m4 files and construct a map.
279 sub scan_m4_files (@)
280 {
281     local (@dirlist) = @_;
282
283     # First, scan acinclude.m4 if it exists.
284     if (-f 'acinclude.m4')
285     {
286         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
287     }
288
289     local ($m4dir);
290     foreach $m4dir (@dirlist)
291     {
292         opendir (DIR, $m4dir)
293             || die "aclocal: couldn't open directory `$m4dir': $!\n";
294         local ($file, $fullfile);
295         foreach $file (sort grep (! /^\./, readdir (DIR)))
296         {
297             # Only examine .m4 files.
298             next unless $file =~ /\.m4$/;
299
300             # Skip some files when running out of srcdir.
301             next if $file eq 'aclocal.m4';
302
303             $fullfile = $m4dir . '/' . $file;
304             $file_contents{$fullfile} = &scan_file ($fullfile);
305         }
306         closedir (DIR);
307     }
308
309     # Construct a new function that does the searching.  We use a
310     # function (instead of just evalling $search in the loop) so that
311     # "die" is correctly and easily propagated if run.
312     my $search = "sub search {\nmy \$found = 0;\n";
313     foreach my $key (reverse sort keys %map)
314     {
315         # EXPR is a regexp matching the name of the macro.
316         (my $expr = $key) =~ s/(\W)/\\$1/g;
317         $search .= ('if (/\b' . $key . '\b/) { & add_macro (' . $key
318                     . '); $found = 1; }' . "\n");
319     }
320     $search .= "return \$found;\n};\n";
321     eval $search;
322     die "internal error: $@\n search is $search" if $@;
323 }
324
325 ################################################################
326
327 # Add a macro to the output.
328 sub add_macro ($)
329 {
330     local ($macro) = @_;
331
332     # We want to ignore AC_ macros.  However, if an AC_ macro is
333     # defined in (eg) acinclude.m4, then we want to make sure we mark
334     # it as seen.
335     return if $macro =~ /^AC_/ && ! defined $map{$macro};
336
337     if (! defined $map{$macro})
338     {
339         warn "aclocal: macro `$macro' required but not defined\n";
340         $exit_status = 1;
341         return;
342     }
343
344     print STDERR "aclocal: saw macro $macro\n" if $verbose;
345     $macro_seen{$macro} = 1;
346     &add_file ($map{$macro});
347 }
348
349 # Add a file to output.
350 sub add_file ($)
351 {
352     local ($file) = @_;
353
354     # Only add a file once.
355     return if ($file_seen{$file});
356     $file_seen{$file} = 1;
357
358     $output .= $file_contents{$file} . "\n";
359     my (@rlist);
360     foreach (split ("\n", $file_contents{$file}))
361     {
362         # Remove comments from current line.
363         s/\bdnl\b.*$//;
364         s/\#.*$//;
365
366         if (/$ac_require_rx/g)
367         {
368             push (@rlist, $1);
369         }
370
371         # The search function is constructed dynamically by
372         # scan_m4_files.  The last parenthethical match makes sure we
373         # don't match things that look like macro assignments or
374         # AC_SUBSTs.
375         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
376         {
377             # Macro not found, but AM_ prefix found.
378             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
379             $exit_status = 1;
380         }
381     }
382
383     local ($macro);
384     foreach $macro (@rlist)
385     {
386         &add_macro ($macro);
387     }
388 }
389
390 # Scan a single M4 file.  Return contents.
391 sub scan_file ($)
392 {
393     local ($file) = @_;
394
395     my $fh = new Automake::XFile $file;
396     my $contents = '';
397     while ($_ = $fh->getline)
398     {
399         # Ignore `##' lines.
400         next if /^##/;
401
402         $contents .= $_;
403
404         if (/$ac_defun_rx/)
405         {
406             if (! defined $map{$1})
407             {
408                 $map{$1} = $file;
409             }
410
411             # Note: we used to give an error here if we saw a
412             # duplicated macro.  However, this turns out to be
413             # extremely unpopular.  It causes actual problems which
414             # are hard to work around, especially when you must
415             # mix-and-match tool versions.
416
417             print STDERR "aclocal: found macro $1 in $file: $.\n" if $verbose;
418         }
419     }
420
421     return $contents;
422 }
423
424 ################################################################
425
426 # Write output.
427 sub write_aclocal ()
428 {
429     return if ! length ($output);
430
431     print STDERR "aclocal: writing $output_file\n" if $verbose;
432
433     my $out = new Automake::XFile "> $output_file";
434
435 # We used to print `# $output_file generated automatically etc.'  But
436 # this creates spurious differences when using autoreconf.  Autoreconf
437 # creates aclocal.m4t and then rename it to aclocal.m4, but the
438 # rebuild rules generated by Automake create aclocal.m4 directly --
439 # this would gives two ways to get the same file, with a different
440 # name in the header.
441     print $out
442 "# generated automatically by aclocal $VERSION -*- Autoconf -*-
443
444 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
445 # Free Software Foundation, Inc.
446 # This file is free software; the Free Software Foundation
447 # gives unlimited permission to copy and/or distribute it,
448 # with or without modifications, as long as this notice is preserved.
449
450 # This program is distributed in the hope that it will be useful,
451 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
452 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
453 # PARTICULAR PURPOSE.
454
455 $output";
456 }
457
458 ### Setup "GNU" style for perl-mode and cperl-mode.
459 ## Local Variables:
460 ## perl-indent-level: 2
461 ## perl-continued-statement-offset: 2
462 ## perl-continued-brace-offset: 0
463 ## perl-brace-offset: 0
464 ## perl-brace-imaginary-offset: 0
465 ## perl-label-offset: -2
466 ## cperl-indent-level: 2
467 ## cperl-brace-offset: 0
468 ## cperl-continued-brace-offset: 0
469 ## cperl-label-offset: -2
470 ## cperl-extra-newline-before-brace: t
471 ## cperl-merge-trailing-else: nil
472 ## cperl-continued-statement-offset: 2
473 ## End: