* lib/install.sh (dsttmp, rmtmp): Do not use '#' in file names;
[automake.git] / aclocal.in
blob3c87fdc12b48a84ddab66528b95b2e785e2d990d
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
6     if 0;
8 # aclocal - create aclocal.m4 by scanning configure.ac
10 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
11 #           Free Software Foundation, Inc.
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.
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.
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.
28 # Written by Tom Tromey <tromey@redhat.com>.
30 BEGIN
32   my $perllibdir = $ENV{'perllibdir'} || '@datadir@/@PACKAGE@-@APIVERSION@';
33   unshift @INC, $perllibdir;
36 use Automake::General;
37 use Automake::Configure_ac;
38 use Automake::XFile;
39 use File::stat;
41 # Some constants.
42 $VERSION = '@VERSION@';
43 $APIVERSION = '@APIVERSION@';
44 $PACKAGE = '@PACKAGE@';
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";
55 # Some globals.
57 # Exit status.
58 $exit_status = 0;
60 # Text to output.
61 $output = '';
63 # Output file name.
64 $output_file = 'aclocal.m4';
66 # Modification time of the youngest dependency.
67 $greatest_mtime = 0;
69 # Option --force.
70 $force_output = 0;
72 # Which macros have been seen.
73 %macro_seen = ();
75 # Which files have been seen.
76 %file_seen = ();
78 # Map macro names to file names.
79 %map = ();
81 # Map file names to file contents.
82 %file_contents = ();
84 # How much to say.
85 $verbose = 0;
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]+))";
96 # Matches an AC_REQUIRE line.
97 $ac_require_rx = "AC_REQUIRE\\((?:\\[([^]]+)\\]|([^],)\n]+))\\)";
101 local (@dirlist) = &parse_arguments (@ARGV);
102 &scan_m4_files (@dirlist);
103 &scan_configure;
104 if (! $exit_status)
106     &write_aclocal;
108 &check_acinclude;
110 exit $exit_status;
112 ################################################################
114 # Print usage and exit.
115 sub usage ($)
117     local ($status) = @_;
119     print "Usage: aclocal [OPTIONS] ...\n\n";
120     print "\
121 Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'
123   --acdir=DIR           directory holding config files
124   --help                print this help, then exit
125   -I DIR                add directory to search list for .m4 files
126   --force               always update output file
127   --output=FILE         put output in FILE (default aclocal.m4)
128   --print-ac-dir        print name of directory holding m4 files
129   --verbose             don't be silent
130   --version             print version number, then exit
132 Report bugs to <bug-automake\@gnu.org>.\n";
134     exit $status;
137 # Parse command line.
138 sub parse_arguments (@)
140     local (@arglist) = @_;
141     local (@dirlist);
142     local ($print_and_exit) = 0;
144     while (@arglist)
145     {
146         if ($arglist[0] =~ /^--acdir=(.+)$/)
147         {
148             $acdir = $1;
149         }
150         elsif ($arglist[0] =~/^--output=(.+)$/)
151         {
152             $output_file = $1;
153         }
154         elsif ($arglist[0] eq '-I')
155         {
156             shift (@arglist);
157             push (@dirlist, $arglist[0]);
158         }
159         elsif ($arglist[0] eq '--print-ac-dir')
160         {
161             $print_and_exit = 1;
162         }
163         elsif ($arglist[0] eq '--force')
164         {
165             $force_output = 1;
166         }
167         elsif ($arglist[0] eq '--verbose')
168         {
169             ++$verbose;
170         }
171         elsif ($arglist[0] eq '--version')
172         {
173             print "aclocal (GNU $PACKAGE) $VERSION\n\n";
174             print "Copyright (C) 2003 Free Software Foundation, Inc.\n";
175             print "This is free software; see the source for copying conditions.  There is NO\n";
176             print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n";
177             print "Written by Tom Tromey <tromey\@redhat.com>\n";
178             exit 0;
179         }
180         elsif ($arglist[0] eq '--help')
181         {
182             &usage (0);
183         }
184         else
185         {
186             die "aclocal: unrecognized option -- `$arglist[0]'\nTry `aclocal --help' for more information.\n";
187         }
189         shift (@arglist);
190     }
192     if ($print_and_exit)
193     {
194         print $acdir, "\n";
195         exit 0;
196     }
198     $default_dirlist="$acdir/dirlist"
199         if $acdir ne $default_acdir;
201     # Search the versioned directory near the end, and then the
202     # unversioned directory last.  Only do this if the user didn't
203     # override acdir.
204     push (@dirlist, "$acdir-$APIVERSION")
205         if $acdir eq $default_acdir;
207     # By default $(datadir)/aclocal doesn't exist.  We don't want to
208     # get an error in the case where we are searching the default
209     # directory and it hasn't been created.
210     push (@dirlist, $acdir)
211         unless $acdir eq $default_acdir && ! -d $acdir;
213     # Finally, adds any directory listed in the `dirlist' file.
214     if (open (DEFAULT_DIRLIST, $default_dirlist))
215     {
216         while (<DEFAULT_DIRLIST>)
217         {
218             # Ignore '#' lines.
219             next if /^#/;
220             # strip off newlines and end-of-line comments
221             s/\s*\#.*$//;
222             chomp ($contents=$_);
223             if (-d $contents )
224             {
225                 push (@dirlist, $contents);
226             }
227         }
228         close (DEFAULT_DIRLIST);
229     }
232     return @dirlist;
235 ################################################################
237 sub scan_configure ()
239     require_configure_ac;
241     open (CONFIGURE, $configure_ac)
242         || die "aclocal: couldn't open `$configure_ac': $!\n";
244     my $mtime = mtime $configure_ac;
245     $greatest_mtime = $mtime if $greatest_mtime < $mtime;
247     # Make sure we include acinclude.m4 if it exists.
248     if (-f 'acinclude.m4')
249     {
250         &add_file ('acinclude.m4');
251     }
253     while (<CONFIGURE>)
254     {
255         # Remove comments from current line.
256         s/\bdnl\b.*$//;
257         s/\#.*$//;
259         # Search for things we know about.  The "search" sub is
260         # constructed dynamically by scan_m4_files.  The last
261         # parenthethical match makes sure we don't match things that
262         # look like macro assignments or AC_SUBSTs.
263         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
264         {
265             # Macro not found, but AM_ prefix found.
266             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
267             $exit_status = 1;
268         }
269     }
271     close (CONFIGURE);
274 ################################################################
276 # Check macros in acinclude.m4.  If one is not used, warn.
277 sub check_acinclude ()
279     local ($key);
281     foreach $key (keys %map)
282     {
283         next unless $map{$key} eq 'acinclude.m4';
284         if (! $macro_seen{$key})
285         {
286             # FIXME: should print line number of acinclude.m4.
287             warn "aclocal: macro `$key' defined in acinclude.m4 but never used\n";
288         }
289     }
292 ################################################################
294 # Scan all the installed m4 files and construct a map.
295 sub scan_m4_files (@)
297     local (@dirlist) = @_;
299     # First, scan acinclude.m4 if it exists.
300     if (-f 'acinclude.m4')
301     {
302         $file_contents{'acinclude.m4'} = &scan_file ('acinclude.m4');
303     }
305     local ($m4dir);
306     foreach $m4dir (@dirlist)
307     {
308         opendir (DIR, $m4dir)
309             || die "aclocal: couldn't open directory `$m4dir': $!\n";
310         local ($file, $fullfile);
311         foreach $file (sort grep (! /^\./, readdir (DIR)))
312         {
313             # Only examine .m4 files.
314             next unless $file =~ /\.m4$/;
316             # Skip some files when running out of srcdir.
317             next if $file eq 'aclocal.m4';
319             $fullfile = $m4dir . '/' . $file;
320             $file_contents{$fullfile} = &scan_file ($fullfile);
321         }
322         closedir (DIR);
323     }
325     # Construct a new function that does the searching.  We use a
326     # function (instead of just evalling $search in the loop) so that
327     # "die" is correctly and easily propagated if run.
328     my $search = "sub search {\nmy \$found = 0;\n";
329     foreach my $key (reverse sort keys %map)
330     {
331         $search .= ('if (/\b\Q' . $key . '\E(?!\w)/) { & add_macro ("' . $key
332                     . '"); $found = 1; }' . "\n");
333     }
334     $search .= "return \$found;\n};\n";
335     eval $search;
336     die "internal error: $@\n search is $search" if $@;
339 ################################################################
341 # Add a macro to the output.
342 sub add_macro ($)
344     local ($macro) = @_;
346     # We want to ignore AC_ macros.  However, if an AC_ macro is
347     # defined in (eg) acinclude.m4, then we want to make sure we mark
348     # it as seen.
349     return if $macro =~ /^AC_/ && ! defined $map{$macro};
351     if (! defined $map{$macro})
352     {
353         warn "aclocal: macro `$macro' required but not defined\n";
354         $exit_status = 1;
355         return;
356     }
358     print STDERR "aclocal: saw macro $macro\n" if $verbose;
359     $macro_seen{$macro} = 1;
360     &add_file ($map{$macro});
363 # Add a file to output.
364 sub add_file ($)
366     local ($file) = @_;
368     # Only add a file once.
369     return if ($file_seen{$file});
370     $file_seen{$file} = 1;
372     my $mtime = mtime $file;
373     $greatest_mtime = $mtime if $greatest_mtime < $mtime;
375     # If the file to add looks like path outside the project,
376     # copy it to the output.
377     # The regex catches filenames starting with things like
378     #   / \ c:\ ../ ./../ etc.
379     if ($file =~ m,^(?:(?:\w:)?[\\/]|(?:\.[\\/]+)*\.\.[\\/]),)
380       {
381         $output .= $file_contents{$file} . "\n";
382       }
383     else
384       {
385         # Otherwise, simply include the file.
386         $output .= "m4_include([$file])\n";
387       }
388     my (@rlist);
389     foreach (split ("\n", $file_contents{$file}))
390     {
391         # Remove comments from current line.
392         s/\bdnl\b.*$//;
393         s/\#.*$//;
395         if (/$ac_require_rx/g)
396         {
397             push (@rlist, $1 || $2);
398         }
400         # The search function is constructed dynamically by
401         # scan_m4_files.  The last parenthethical match makes sure we
402         # don't match things that look like macro assignments or
403         # AC_SUBSTs.
404         if (! &search && /(^|\s+)(AM_[A-Z0-9_]+)($|[^\]\)=A-Z0-9_])/)
405         {
406             # Macro not found, but AM_ prefix found.
407             warn "aclocal: $configure_ac: $.: macro `$2' not found in library\n";
408             $exit_status = 1;
409         }
410     }
412     local ($macro);
413     foreach $macro (@rlist)
414     {
415         &add_macro ($macro);
416     }
419 # Scan a single M4 file.  Return contents.
420 sub scan_file ($)
422     local ($file) = @_;
424     my $fh = new Automake::XFile $file;
425     my $contents = '';
426     while ($_ = $fh->getline)
427     {
428         # Ignore `##' lines.
429         next if /^##/;
431         $contents .= $_;
433         if (/$ac_defun_rx/)
434         {
435             if (! defined $map{$1 || $2})
436             {
437                 $map{$1 || $2} = $file;
438             }
440             # Note: we used to give an error here if we saw a
441             # duplicated macro.  However, this turns out to be
442             # extremely unpopular.  It causes actual problems which
443             # are hard to work around, especially when you must
444             # mix-and-match tool versions.
446             print STDERR "aclocal: found macro $1 in $file: $.\n" if $verbose;
447         }
448     }
450     return $contents;
453 ################################################################
455 # Write output.
456 sub write_aclocal ()
458     # Nothing to output?!
459     # FIXME: Shouldn't we diagnose this?
460     return if ! length ($output);
462 # We used to print `# $output_file generated automatically etc.'  But
463 # this creates spurious differences when using autoreconf.  Autoreconf
464 # creates aclocal.m4t and then rename it to aclocal.m4, but the
465 # rebuild rules generated by Automake create aclocal.m4 directly --
466 # this would gives two ways to get the same file, with a different
467 # name in the header.
468     $output = "# generated automatically by aclocal $VERSION -*- Autoconf -*-
470 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
471 # Free Software Foundation, Inc.
472 # This file is free software; the Free Software Foundation
473 # gives unlimited permission to copy and/or distribute it,
474 # with or without modifications, as long as this notice is preserved.
476 # This program is distributed in the hope that it will be useful,
477 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
478 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
479 # PARTICULAR PURPOSE.
481 $output";
483     # We try not to update $output_file unless necessary, because
484     # doing so invalidate Autom4te's cache and therefore slows down
485     # tools called after aclocal.
486     #
487     # We need to overwrite $output_file in the following situations.
488     #   * The --force option is in use.
489     #   * One of the dependencies is younger.
490     #     (Not updating $output_file in this situation would cause
491     #     make to call aclocal in loop.)
492     #   * The contents of the current file are different from what
493     #     we have computed.
494     if (!$force_output
495         && $greatest_mtime < mtime ($output_file)
496         && $output eq contents ($output_file))
497       {
498         print STDERR "aclocal: $output_file unchanged\n" if $verbose;
499         return;
500       }
502     print STDERR "aclocal: writing $output_file\n" if $verbose;
504     my $out = new Automake::XFile "> $output_file";
505     print $out $output;
508 ### Setup "GNU" style for perl-mode and cperl-mode.
509 ## Local Variables:
510 ## perl-indent-level: 2
511 ## perl-continued-statement-offset: 2
512 ## perl-continued-brace-offset: 0
513 ## perl-brace-offset: 0
514 ## perl-brace-imaginary-offset: 0
515 ## perl-label-offset: -2
516 ## cperl-indent-level: 2
517 ## cperl-brace-offset: 0
518 ## cperl-continued-brace-offset: 0
519 ## cperl-label-offset: -2
520 ## cperl-extra-newline-before-brace: t
521 ## cperl-merge-trailing-else: nil
522 ## cperl-continued-statement-offset: 2
523 ## End: