Fixlet
[automake.git] / aclocal.in
blobbcee148098a0d48999ea8f2967ed6884532687c6
1 #!@PERL@
2 # -*- perl -*-
3 # @configure_input@
5 eval 'exec @PERL@ -S $0 ${1+"$@"}'
6     if 0;
8 # aclocal - scan configure.in and generate aclocal.m4.
10 # Some constants.
11 $VERSION = "@VERSION@";
12 $PACKAGE = "@PACKAGE@";
13 $prefix = "@prefix@";
14 $acdir = "@datadir@/@PACKAGE@";
16 # Some globals.
18 # Exit status.
19 $exit_status = 0;
21 # Text to output.
22 $output = '';
24 # Output file name.
25 $output_file = 'aclocal.m4';
27 # Which macros have been seen.
28 %macro_seen = ();
30 # Which files have been seen.
31 %file_seen = ();
33 # How much to say.
34 $verbosity = 0;
36 @obsolete_macros =
37     (
38      'AC_FEATURE_CTYPE',
39      'AC_FEATURE_ERRNO',
40      'AC_FEATURE_EXIT',
41      'AC_SYSTEM_HEADER',
42      'fp_C_PROTOTYPES',
43      'fp_FUNC_FNMATCH',
44      'fp_PROG_CC_STDC',
45      'fp_PROG_INSTALL',
46      'fp_WITH_DMALLOC',
47      'fp_WITH_REGEX',
48      'gm_PROG_LIBTOOL',
49      'jm_MAINTAINER_MODE',
50      'md_TYPE_PTRDIFF_T',
51      'ud_PATH_LISPDIR',
52 # These aren't quite obsolete.
53 #      'md_PATH_PROG',
54 #      'ud_GNU_GETTEXT',
55 #      'ud_LC_MESSAGES',
56 #      'ud_WITH_NLS'
57      );
59 $obsolete_rx = '(' . join ('|', @obsolete_macros) . ')';
63 &parse_arguments (@ARGV);
64 &scan_configure;
65 if (! $exit_status)
67     &write_aclocal;
70 exit $exit_status;
72 ################################################################
74 # Print usage and exit.
75 sub usage
77     local ($status) = @_;
79     print "Usage: aclocal [OPTIONS] ...\n";
80     print "\
81   --acdir=DIR           directory holding config files
82   --help                print this help, then exit
83   --output=FILE         put output in FILE (default aclocal.m4)
84   --verbose             don't be silent
85   --version             print version number, then exit\n";
87     exit $status;
90 # Parse command line.
91 sub parse_arguments
93     local (@arglist) = @_;
95     while (@arglist)
96     {
97         if ($arglist[0] =~ /^--acdir=(.+)$/)
98         {
99             $acdir = $1;
100         }
101         elsif ($arglist[0] =~/^--output=(.+)$/)
102         {
103             $output_file = $1;
104         }
105         elsif ($arglist[0] eq '--verbose')
106         {
107             ++$verbosity;
108         }
109         elsif ($arglist[0] eq '--version')
110         {
111             print "aclocal - GNU $PACKAGE $VERSION\n";
112             exit 0;
113         }
114         elsif ($arglist[0] eq '--help')
115         {
116             &usage (0);
117         }
118         else
119         {
120             &usage (1);
121         }
123         shift (@arglist);
124     }
127 ################################################################
129 sub scan_configure
131     # First, construct list of regexps to match the things we actually
132     # have.
133     opendir (DIR, $acdir)
134         || die "aclocal: couldn't open directory \`$acdir': $!\n";
135     local ($search, $elt);
136     foreach (sort grep (! /^\./, readdir (DIR)))
137     {
138         # Only examine .m4 files.
139         next unless s/\.m4$//;
141         # Skip some files when running out of srcdir.  Eg "aclocal.m4"
142         # must be skipped.
143         next unless /^[A-Za-z]+_[A-Z_]+$/;
145         print STDERR "Finding $_\n" if $verbosity;
146         ($elt = $_) =~ s/(\W)/\\$1/g;
147         $search .= "&add_file (\"$elt\") if /$elt/;\n";
148     }
149     closedir (DIR);
151     # Construct a new function that does the searching.  We use a
152     # function (instead of just evalling $search in the loop) so that
153     # "die" is correctly and easily propagated if run.
154     eval 'sub search { ' . $search . '};';
156     open (CONFIGURE, "configure.in")
157         || die "aclocal: couldn't open \`configure.in': $!\n";
159     while (<CONFIGURE>)
160     {
161         # Remove comments from current line.
162         s/\bdnl\b.*$//;
163         s/\#.*$//;
165         if (/$obsolete_rx/o)
166         {
167             chop;
168             warn "aclocal: configure.in: $.: obsolete macro \`$_'\n";
169             $exit_status = 1;
170             next;
171         }
173         # Search for things we know about.  The "search" sub is
174         # constructed dynamically, above.
175         &search;
176     }
178     close (CONFIGURE);
180     # Include this file if it exists
181     if (-f 'acinclude.m4')
182     {
183         &add_file ('acinclude.m4');
184     }
187 ################################################################
189 # Add a file to output.
190 sub add_file
192     local ($file) = @_;
193     local ($fullfile) = $file;
195     return if ($file_seen{$file});
196     $file_seen{$file} = 1;
198     if (! -f $file)
199     {
200         $fullfile = $acdir . '/' . $file . '.m4';
201         if (! -f $fullfile)
202         {
203             # Maybe the file is an Autoconf built-in.  Check the only
204             # way we know how.  Suggestions on how to make this better
205             # are welcome.  FIXME should give file and line number of
206             # enclosing file.
207             return if $file =~ /^AC_[A-Z_]+$/;
208             die "aclocal: file \`$file' not found\n";
209         }
210     }
212     open (FILE, $fullfile)
213         || die "aclocal: couldn't open \`$fullfile': $!\n";
215     local (@rlist);
216     while (<FILE>)
217     {
218         $output .= $_;
220         # See if we're defining a macro that was already seen.  This
221         # is mostly a special case for `acinclude.m4'.
222         if (/AC_DEFUN\(\[?([^])\n]+)\]?/)
223         {
224             if ($1 ne $file && $file_seen{$1})
225             {
226                 warn "aclocal: $fullfile: $.: duplicated macro \`$1'\n";
227                 $exit_status = 1;
228             }
229             $macro_seen{$1} = 1;
230         }
232         # See if some other macro is required.
233         if (/AC_REQUIRE\(\[?([^])]*)\]?\)/)
234         {
235             push (@rlist, $1) unless defined $macro_seen{$1};
236         }
237     }
238     $output .= "\n";
239     close (FILE);
241     foreach $file (@rlist)
242     {
243         &add_file ($file);
244     }
247 ################################################################
249 # Write output.
250 sub write_aclocal
252     return if ! $output;
254     print STDERR "Writing aclocal.m4\n" if $verbosity;
256     open (ACLOCAL, "> aclocal.m4")
257         || die "aclocal: couldn't open \`aclocal.m4' for writing: $!\n";
258     print ACLOCAL "dnl aclocal.m4 generated automatically by aclocal $VERSION\n\n";
259     print ACLOCAL $output;
260     close (ACLOCAL);