2 # autoscan - Create configure.scan (a preliminary configure.in) for a package.
3 # Copyright (C) 1994 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 # Written by David MacKenzie <djm@gnu.ai.mit.edu>.
24 $datadir = $ENV{"AC_MACRODIR"} || "@datadir@";
26 # Reference these variables to pacify perl -w.
27 undef %identifiers_macros;
28 undef %makevars_macros;
29 undef %programs_macros;
39 # Process any command line arguments.
43 "Usage: autoscan [--macrodir=dir] [--help] [--verbose] [--version] [srcdir]\n";
46 if (/^--m[a-z]*=(.*)/) {
56 die "$usage" if defined($srcdir);
57 # Top level directory of the package being autoscanned.
64 $srcdir="." if !defined($srcdir);
66 print "srcdir=$srcdir\n" if $verbose;
67 chdir $srcdir || die "$0: cannot cd to $srcdir: $!\n";
69 open(CONF, ">configure.scan") ||
70 die "$0: cannot create configure.scan: $!\n";
73 # Print the version number and exit.
76 open(ACG, "<$datadir/acgeneral.m4") ||
77 die "$0: cannot open $datadir/acgeneral.m4: $!\n";
79 if (/define.AC_ACVERSION.\s*([0-9.]+)/) {
80 print "Autoconf version $1\n";
84 die "Autoconf version unknown\n";
87 # Put values in the tables of what to do with each token.
90 local($kind, $word, $macro);
92 # Initialize a table of C keywords (to ignore).
93 # Taken from K&R 1st edition p. 180.
94 # ANSI C, GNU C, and C++ keywords can introduce portability problems,
95 # so don't ignore them.
96 foreach $word ('int', 'char', 'float', 'double', 'struct', 'union',
97 'long', 'short', 'unsigned', 'auto', 'extern', 'register',
98 'typedef', 'static', 'goto', 'return', 'sizeof', 'break',
99 'continue', 'if', 'else', 'for', 'do', 'while', 'switch',
101 $c_keywords{$word} = 0;
104 # The data file format supports only one line of macros per function.
105 # If more than that is required for a common portability problem,
106 # a new Autoconf macro should probably be written for that case,
107 # instead of duplicating the code in lots of configure.in files.
109 foreach $kind ('functions', 'headers', 'identifiers', 'programs',
111 open(TABLE, "<$datadir/ac$kind") ||
112 die "$0: cannot open $datadir/ac$kind: $!\n";
114 next if /^\s*$/ || /^\s*#/; # Ignore blank lines and comments.
115 ($word, $macro) = split;
116 eval "\$$kind" . "_macros{\$word} = \$macro";
122 # Collect names of various kinds of files in the package.
123 # Called by &find on each file.
126 # Wanted only if there is no corresponding FILE.in.
130 # Strip a useless leading `./'.
133 if (/^.*\.[chlymC](\.in)?$/)
135 push (@cfiles, $name);
138 elsif (/^.*\.(cc|cpp|cxx|CC|C|hh|hpp|hxx|HH|H|yy|ypp|ll|lpp)(\.in)?$/)
140 push (@cfiles, $name);
143 elsif (/^[Mm]akefile(\.in)?$/ || /^GNUmakefile$/)
145 # Wanted only if there is no corresponding Makefile.in.
146 # Using Find, $_ contains the current filename with the current
147 # directory of the walk through.
148 push (@makefiles, $name);
150 elsif (/^.*\.sh(\.in)?$/)
152 push (@shfiles, $name);
156 # Read through the files and collect lists of tokens in them
157 # that might create nonportabilities.
160 $initfile = $cfiles[0]; # Pick one at random.
163 print "cfiles:", join(" ", @cfiles), "\n";
164 print "makefiles:", join(" ", @makefiles), "\n";
165 print "shfiles:", join(" ", @shfiles), "\n";
168 foreach $file (@cfiles) {
172 foreach $file (@makefiles) {
173 &scan_makefile($file);
176 foreach $file (@shfiles) {
177 &scan_sh_file($file);
184 local($in_comment) = 0; # Nonzero if in a multiline comment.
186 open(CFILE, "<$file") || die "$0: cannot open $file: $!\n";
188 # Strip out comments, approximately.
189 # Ending on this line.
190 if ($in_comment && m,\*/,) {
196 # Starting on this line.
200 # Continuing on this line.
203 # Preprocessor directives.
204 if (/^\s*#\s*include\s*<([^>]*)>/) {
207 # Ignore other preprocessor directives.
210 # Remove string and character constants.
214 # Tokens in the code.
215 # Maybe we should ignore function definitions (in column 0)?
216 while (s/\W([a-zA-Z_]\w*)\s*\(/ /) {
217 $functions{$1}++ if !defined($c_keywords{$1});
219 while (s/\W([a-zA-Z_]\w*)\W/ /) {
220 $identifiers{$1}++ if !defined($c_keywords{$1});
228 print "\n$file functions:\n";
229 foreach $word (sort keys %functions) {
230 print "$word $functions{$word}\n";
233 print "\n$file identifiers:\n";
234 foreach $word (sort keys %identifiers) {
235 print "$word $identifiers{$word}\n";
238 print "\n$file headers:\n";
239 foreach $word (sort keys %headers) {
240 print "$word $headers{$word}\n";
249 open(MFILE, "<$file") || die "$0: cannot open $file: $!\n";
251 # Strip out comments and variable references.
257 # Variable assignments.
258 while (s/\W([a-zA-Z_]\w*)\s*=/ /) {
262 while (s/\W-l([a-zA-Z_]\w*)\W/ /) {
265 # Tokens in the code.
266 while (s/\W([a-zA-Z_][\w\+\.-]*)\W/ /) {
275 print "\n$file makevars:\n";
276 foreach $word (sort keys %makevars) {
277 print "$word $makevars{$word}\n";
280 print "\n$file libraries:\n";
281 foreach $word (sort keys %libraries) {
282 print "$word $libraries{$word}\n";
285 print "\n$file programs:\n";
286 foreach $word (sort keys %programs) {
287 print "$word $programs{$word}\n";
296 open(MFILE, "<$file") || die "$0: cannot open $file: $!\n";
298 # Strip out comments and variable references.
303 # Tokens in the code.
304 while (s/\W([a-zA-Z_]\w*)\W/ /) {
313 print "\n$file programs:\n";
314 foreach $word (sort keys %programs) {
315 print "$word $programs{$word}\n";
320 # Print a configure.in.
323 local (%unique_makefiles);
325 print CONF "dnl Process this file with autoconf to produce a configure script.\n";
326 print CONF "AC_INIT($initfile)\n";
333 # Change DIR/Makefile.in to DIR/Makefile.
334 foreach $_ (@makefiles) {
336 $unique_makefiles{$_}++;
338 print CONF "\nAC_OUTPUT(", join(" ", keys(%unique_makefiles)), ")\n";
343 # Print Autoconf macro $1 if it's not undef and hasn't been printed already.
348 if (defined($macro) && !defined($printed{$macro})) {
349 print CONF "$macro\n";
350 $printed{$macro} = 1;
358 print CONF "\ndnl Checks for programs.\n";
359 foreach $word (sort keys %programs) {
360 &print_unique($programs_macros{$word});
362 foreach $word (sort keys %makevars) {
363 &print_unique($makevars_macros{$word});
365 print CONF "\ndnl Checks for libraries.\n";
366 foreach $word (sort keys %libraries) {
367 print CONF "dnl Replace `\main\' with a function in -l$word:\n";
368 print CONF "AC_CHECK_LIB($word, main)\n";
376 print CONF "\ndnl Checks for header files.\n";
377 foreach $word (sort keys %headers) {
378 if (defined($headers_macros{$word}) &&
379 $headers_macros{$word} eq 'AC_CHECK_HEADERS') {
380 push(@have_headers, $word);
382 &print_unique($headers_macros{$word});
385 print CONF "AC_CHECK_HEADERS(" . join(' ', sort(@have_headers)) . ")\n"
386 if defined(@have_headers);
389 sub output_identifiers
393 print CONF "\ndnl Checks for typedefs, structures, and compiler characteristics.\n";
394 foreach $word (sort keys %identifiers) {
395 &print_unique($identifiers_macros{$word});
403 print CONF "\ndnl Checks for library functions.\n";
404 foreach $word (sort keys %functions) {
405 if (defined($functions_macros{$word}) &&
406 $functions_macros{$word} eq 'AC_CHECK_FUNCS') {
407 push(@have_funcs, $word);
409 &print_unique($functions_macros{$word});
412 print CONF "AC_CHECK_FUNCS(" . join(' ', sort(@have_funcs)) . ")\n"
413 if defined(@have_funcs);