* configure.in (AUTOMAKE, ACLOCAL): Search the 'lib' directory
[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 # Exit status.
56 $exit_status = 0;
57
58 # Text to output.
59 $output = '';
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     &write_aclocal;
105 }
106 &check_acinclude;
107
108 exit $exit_status;
109
110 ################################################################
111
112 # Print usage and exit.
113 sub usage ($)
114 {
115     local ($status) = @_;
116
117     print "Usage: aclocal [OPTIONS] ...\n\n";
118     print "\
119 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
120
121   --acdir=DIR           directory holding config files
122   --help                print this help, then exit
123   -I DIR                add directory to search list for .m4 files
124   --force               always update output file
125   --output=FILE         put output in FILE (default aclocal.m4)
126   --print-ac-dir        print name of directory holding m4 files
127   --verbose             don't be silent
128   --version             print version number, then exit
129
130 Report bugs to <bug-automake\@gnu.org>.\n";
131
132     exit $status;
133 }
134
135 # Parse command line.
136 sub parse_arguments (@)
137 {
138     local (@arglist) = @_;
139     local (@dirlist);
140     local ($print_and_exit) = 0;
141
142     while (@arglist)
143     {
144         if ($arglist[0] =~ /^--acdir=(.+)$/)
145         {
146             $acdir = $1;
147         }
148         elsif ($arglist[0] =~/^--output=(.+)$/)
149         {
150             $output_file = $1;
151         }
152         elsif ($arglist[0] eq '-I')
153         {
154             shift (@arglist);
155             push (@dirlist, $arglist[0]);
156         }
157         elsif ($arglist[0] eq '--print-ac-dir')
158         {
159             $print_and_exit = 1;
160         }
161         elsif ($arglist[0] eq '--force')
162         {
163             $force_output = 1;
164         }
165         elsif ($arglist[0] eq '--verbose')
166         {
167             ++$verbose;
168         }
169         elsif ($arglist[0] eq '--version')
170         {
171             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
172             print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
173             print "This is free software; see the source for copying conditions.  There is NO\n";
174             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
175             print "Written by Tom Tromey <tromey\@redhat.com>\n";
176             exit 0;
177         }
178         elsif ($arglist[0] eq '--help')
179         {
180             &usage (0);
181         }
182         else
183         {
184             print STDERR "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
185             exit 1;
186         }
187
188         shift (@arglist);
189     }
190
191     if ($print_and_exit)
192     {
193         print $acdir, "\n";
194         exit 0;
195     }
196
197     $default_dirlist="$acdir/dirlist"
198         if $acdir ne $default_acdir;
199
200     # Search the versioned directory near the end, and then the
201     # unversioned directory last.  Only do this if the user didn't
202     # override acdir.
203     push (@dirlist, "$acdir-$APIVERSION")
204         if $acdir eq $default_acdir;
205
206     # By default $(datadir)/aclocal doesn't exist.  We don't want to
207     # get an error in the case where we are searching the default
208     # directory and it hasn't been created.
209     push (@dirlist, $acdir)
210         unless $acdir eq $default_acdir && ! -d $acdir;
211
212     # Finally, adds any directory listed in the `dirlist' file.
213     if (open (DEFAULT_DIRLIST, $default_dirlist))
214     {
215         while (<DEFAULT_DIRLIST>)
216         {
217             # Ignore '#' lines.
218             next if /^#/;
219             # strip off newlines and end-of-line comments
220             s/\s*\#.*$//;
221             chomp ($contents=$_);
222             if (-d $contents )
223             {
224                 push (@dirlist, $contents);
225             }
226         }
227         close (DEFAULT_DIRLIST);
228     }
229
230
231     return @dirlist;
232 }
233
234 ################################################################
235
236 sub scan_configure ()
237 {
238     require_configure_ac;
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         # parenthethical 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_status = 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         foreach $file (sort grep (! /^\./, readdir (DIR)))
318         {
319             # Only examine .m4 files.
320             next unless $file =~ /\.m4$/;
321
322             # Skip some files when running out of srcdir.
323             next if $file eq 'aclocal.m4';
324
325             $fullfile = $m4dir . '/' . $file;
326             $file_contents{$fullfile} = &scan_file ($fullfile);
327         }
328         closedir (DIR);
329     }
330
331     # Construct a new function that does the searching.  We use a
332     # function (instead of just evalling $search in the loop) so that
333     # "die" is correctly and easily propagated if run.
334     my $search = "sub search {\nmy \$found = 0;\n";
335     foreach my $key (reverse sort keys %map)
336     {
337         $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
338                     . '"); $found = 1; }' . "\n");
339     }
340     $search .= "return \$found;\n};\n";
341     eval $search;
342     die "internal error: $@\n search is $search" if $@;
343 }
344
345 ################################################################
346
347 # Add a macro to the output.
348 sub add_macro ($)
349 {
350     local ($macro) = @_;
351
352     # We want to ignore AC_ macros.  However, if an AC_ macro is
353     # defined in (eg) acinclude.m4, then we want to make sure we mark
354     # it as seen.
355     return if $macro =~ /^AC_/ && ! defined $map{$macro};
356
357     if (! defined $map{$macro})
358     {
359         warn "aclocal: macro `$macro' required but not defined\n";
360         $exit_status = 1;
361         return;
362     }
363
364     print STDERR "aclocal: saw macro $macro\n" if $verbose;
365     $macro_seen{$macro} = 1;
366     &add_file ($map{$macro});
367 }
368
369 # Add a file to output.
370 sub add_file ($)
371 {
372     local ($file) = @_;
373
374     # Only add a file once.
375     return if ($file_seen{$file});
376     $file_seen{$file} = 1;
377
378     my $mtime = mtime $file;
379     $greatest_mtime = $mtime if $greatest_mtime < $mtime;
380
381     # If the file to add looks like path outside the project,
382     # copy it to the output.
383     # The regex catches filenames starting with things like
384     #   / \ c:\ ../ ./../ etc.
385     if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
386       {
387         $output .= $file_contents{$file} . "\n";
388       }
389     else
390       {
391         # Otherwise, simply include the file.
392         $output .= "m4_include([$file])\n";
393       }
394     my (@rlist);
395     foreach (split ("\n", $file_contents{$file}))
396     {
397         # Remove comments from current line.
398         s/\bdnl\b.*$//;
399         s/\#.*$//;
400
401         if (/$ac_require_rx/g)
402         {
403             push (@rlist, $1 || $2);
404         }
405
406         # The search function is constructed dynamically by
407         # scan_m4_files.  The last parenthethical match makes sure we
408         # don't match things that look like macro assignments or
409         # AC_SUBSTs.
410         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
411         {
412             # Macro not found, but AM_ prefix found.
413             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
414             $exit_status = 1;
415         }
416     }
417
418     local ($macro);
419     foreach $macro (@rlist)
420     {
421         &add_macro ($macro);
422     }
423 }
424
425 # Scan a single M4 file.  Return contents.
426 sub scan_file ($)
427 {
428     local ($file) = @_;
429
430     my $fh = new Automake::XFile $file;
431     my $contents = '';
432     while ($_ = $fh->getline)
433     {
434         # Ignore `##' lines.
435         next if /^##/;
436
437         $contents .= $_;
438
439         if (/$ac_defun_rx/)
440         {
441             if (! defined $map{$1 || $2})
442             {
443                 $map{$1 || $2} = $file;
444             }
445
446             # Note: we used to give an error here if we saw a
447             # duplicated macro.  However, this turns out to be
448             # extremely unpopular.  It causes actual problems which
449             # are hard to work around, especially when you must
450             # mix-and-match tool versions.
451
452             print STDERR "aclocal: found macro $1 in $file: $.\n" if $verbose;
453         }
454     }
455
456     return $contents;
457 }
458
459 ################################################################
460
461 # Write output.
462 sub write_aclocal ()
463 {
464     # Nothing to output?!
465     # FIXME: Shouldn't we diagnose this?
466     return if ! length ($output);
467
468 # We used to print `# $output_file generated automatically etc.'  But
469 # this creates spurious differences when using autoreconf.  Autoreconf
470 # creates aclocal.m4t and then rename it to aclocal.m4, but the
471 # rebuild rules generated by Automake create aclocal.m4 directly --
472 # this would gives two ways to get the same file, with a different
473 # name in the header.
474     $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
475
476 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
477 # Free Software Foundation, Inc.
478 # This file is free software; the Free Software Foundation
479 # gives unlimited permission to copy and/or distribute it,
480 # with or without modifications, as long as this notice is preserved.
481
482 # This program is distributed in the hope that it will be useful,
483 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
484 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
485 # PARTICULAR PURPOSE.
486
487 $output";
488
489     # We try not to update $output_file unless necessary, because
490     # doing so invalidate Autom4te's cache and therefore slows down
491     # tools called after aclocal.
492     #
493     # We need to overwrite $output_file in the following situations.
494     #   * The --force option is in use.
495     #   * One of the dependencies is younger.
496     #     (Not updating $output_file in this situation would cause
497     #     make to call aclocal in loop.)
498     #   * The contents of the current file are different from what
499     #     we have computed.
500     if (!$force_output
501         && $greatest_mtime < mtime ($output_file)
502         && $output eq contents ($output_file))
503       {
504         print STDERR "aclocal: $output_file unchanged\n" if $verbose;
505         return;
506       }
507
508     print STDERR "aclocal: writing $output_file\n" if $verbose;
509
510     my $out = new Automake::XFile "> $output_file";
511     print $out $output;
512 }
513
514 ### Setup "GNU" style for perl-mode and cperl-mode.
515 ## Local Variables:
516 ## perl-indent-level: 2
517 ## perl-continued-statement-offset: 2
518 ## perl-continued-brace-offset: 0
519 ## perl-brace-offset: 0
520 ## perl-brace-imaginary-offset: 0
521 ## perl-label-offset: -2
522 ## cperl-indent-level: 2
523 ## cperl-brace-offset: 0
524 ## cperl-continued-brace-offset: 0
525 ## cperl-label-offset: -2
526 ## cperl-extra-newline-before-brace: t
527 ## cperl-merge-trailing-else: nil
528 ## cperl-continued-statement-offset: 2
529 ## End: