* lib/Automake/Configure_ac.pm (&find_configure_ac)
[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 # configure.ac or configure.in.
56 my $configure_ac = require_configure_ac;
57
58 # Exit status.
59 $exit_status = 0;
60
61 # Text to output.
62 $output = '';
63
64 # Output file name.
65 $output_file = 'aclocal.m4';
66
67 # Modification time of the youngest dependency.
68 $greatest_mtime = 0;
69
70 # Option --force.
71 $force_output = 0;
72
73 # Which macros have been seen.
74 %macro_seen = ();
75
76 # Which files have been seen.
77 %file_seen = ();
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 \f
101
102 local (@dirlist) = &parse_arguments (@ARGV);
103 &scan_m4_files (@dirlist);
104 &scan_configure;
105 if (! $exit_status)
106 {
107     &write_aclocal;
108 }
109 &check_acinclude;
110
111 exit $exit_status;
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
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       # parenthethical 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_status = 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         foreach $file (sort grep (! /^\./, readdir (DIR)))
319         {
320             # Only examine .m4 files.
321             next unless $file =~ /\.m4$/;
322
323             # Skip some files when running out of srcdir.
324             next if $file eq 'aclocal.m4';
325
326             $fullfile = $m4dir . '/' . $file;
327             $file_contents{$fullfile} = &scan_file ($fullfile);
328         }
329         closedir (DIR);
330     }
331
332     # Construct a new function that does the searching.  We use a
333     # function (instead of just evalling $search in the loop) so that
334     # "die" is correctly and easily propagated if run.
335     my $search = "sub search {\nmy \$found = 0;\n";
336     foreach my $key (reverse sort keys %map)
337     {
338         $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
339                     . '"); $found = 1; }' . "\n");
340     }
341     $search .= "return \$found;\n};\n";
342     eval $search;
343     die "internal error: $@\n search is $search" if $@;
344 }
345
346 ################################################################
347
348 # Add a macro to the output.
349 sub add_macro ($)
350 {
351     local ($macro) = @_;
352
353     # We want to ignore AC_ macros.  However, if an AC_ macro is
354     # defined in (eg) acinclude.m4, then we want to make sure we mark
355     # it as seen.
356     return if $macro =~ /^AC_/ && ! defined $map{$macro};
357
358     if (! defined $map{$macro})
359     {
360         warn "aclocal: macro `$macro' required but not defined\n";
361         $exit_status = 1;
362         return;
363     }
364
365     print STDERR "aclocal: saw macro $macro\n" if $verbose;
366     $macro_seen{$macro} = 1;
367     &add_file ($map{$macro});
368 }
369
370 # Add a file to output.
371 sub add_file ($)
372 {
373   local ($file) = @_;
374
375   # Only add a file once.
376   return if ($file_seen{$file});
377   $file_seen{$file} = 1;
378
379   my $mtime = mtime $file;
380   $greatest_mtime = $mtime if $greatest_mtime < $mtime;
381
382   # If the file to add looks like path outside the project,
383   # copy it to the output.
384   # The regex catches filenames starting with things like
385   #   / \ c:\ ../ ./../ etc.
386   if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
387     {
388       $output .= $file_contents{$file} . "\n";
389     }
390   else
391     {
392       # Otherwise, simply include the file.
393       $output .= "m4_include([$file])\n";
394     }
395   my (@rlist);
396   foreach (split ("\n", $file_contents{$file}))
397     {
398       # Remove comments from current line.
399       s/\bdnl\b.*$//;
400       s/\#.*$//;
401
402       if (/$ac_require_rx/g)
403         {
404           push (@rlist, $1 || $2);
405         }
406
407       # The search function is constructed dynamically by
408       # scan_m4_files.  The last parenthethical match makes sure we
409       # don't match things that look like macro assignments or
410       # AC_SUBSTs.
411       if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
412         {
413           # Macro not found, but AM_ prefix found.
414           warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
415           $exit_status = 1;
416         }
417     }
418
419   local ($macro);
420   foreach $macro (@rlist)
421     {
422       &add_macro ($macro);
423     }
424 }
425
426 # Scan a single M4 file.  Return contents.
427 sub scan_file ($)
428 {
429     local ($file) = @_;
430
431     my $fh = new Automake::XFile $file;
432     my $contents = '';
433     while ($_ = $fh->getline)
434     {
435         # Ignore `##' lines.
436         next if /^##/;
437
438         $contents .= $_;
439
440         if (/$ac_defun_rx/)
441         {
442             if (! defined $map{$1 || $2})
443             {
444                 $map{$1 || $2} = $file;
445             }
446
447             # Note: we used to give an error here if we saw a
448             # duplicated macro.  However, this turns out to be
449             # extremely unpopular.  It causes actual problems which
450             # are hard to work around, especially when you must
451             # mix-and-match tool versions.
452
453             print STDERR "aclocal: found macro $1 in $file: $.\n" if $verbose;
454         }
455     }
456
457     return $contents;
458 }
459
460 ################################################################
461
462 # Write output.
463 sub write_aclocal ()
464 {
465     # Nothing to output?!
466     # FIXME: Shouldn't we diagnose this?
467     return if ! length ($output);
468
469 # We used to print `# $output_file generated automatically etc.'  But
470 # this creates spurious differences when using autoreconf.  Autoreconf
471 # creates aclocal.m4t and then rename it to aclocal.m4, but the
472 # rebuild rules generated by Automake create aclocal.m4 directly --
473 # this would gives two ways to get the same file, with a different
474 # name in the header.
475     $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
476
477 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
478 # Free Software Foundation, Inc.
479 # This file is free software; the Free Software Foundation
480 # gives unlimited permission to copy and/or distribute it,
481 # with or without modifications, as long as this notice is preserved.
482
483 # This program is distributed in the hope that it will be useful,
484 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
485 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
486 # PARTICULAR PURPOSE.
487
488 $output";
489
490     # We try not to update $output_file unless necessary, because
491     # doing so invalidate Autom4te's cache and therefore slows down
492     # tools called after aclocal.
493     #
494     # We need to overwrite $output_file in the following situations.
495     #   * The --force option is in use.
496     #   * One of the dependencies is younger.
497     #     (Not updating $output_file in this situation would cause
498     #     make to call aclocal in loop.)
499     #   * The contents of the current file are different from what
500     #     we have computed.
501     if (!$force_output
502         && $greatest_mtime < mtime ($output_file)
503         && $output eq contents ($output_file))
504       {
505         print STDERR "aclocal: $output_file unchanged\n" if $verbose;
506         return;
507       }
508
509     print STDERR "aclocal: writing $output_file\n" if $verbose;
510
511     my $out = new Automake::XFile "> $output_file";
512     print $out $output;
513 }
514
515 ### Setup "GNU" style for perl-mode and cperl-mode.
516 ## Local Variables:
517 ## perl-indent-level: 2
518 ## perl-continued-statement-offset: 2
519 ## perl-continued-brace-offset: 0
520 ## perl-brace-offset: 0
521 ## perl-brace-imaginary-offset: 0
522 ## perl-label-offset: -2
523 ## cperl-indent-level: 2
524 ## cperl-brace-offset: 0
525 ## cperl-continued-brace-offset: 0
526 ## cperl-label-offset: -2
527 ## cperl-extra-newline-before-brace: t
528 ## cperl-merge-trailing-else: nil
529 ## cperl-continued-statement-offset: 2
530 ## End: