summaryrefslogtreecommitdiff
path: root/util/autoconf/autoscan
blob: 74debe52a8a1a9447a6b242fb81632a715b8dfbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/athena/bin/perl
# autoscan - Create configure.scan (a preliminary configure.in) for a package.
# Copyright (C) 1994 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Written by David MacKenzie <djm@gnu.ai.mit.edu>.

require "find.pl";

$datadir = $ENV{"AC_MACRODIR"} || "/usr/local/share/autoconf";
$verbose = 0;
# Reference these variables to pacify perl -w.
undef %identifiers_macros;
undef %makevars_macros;
undef %programs_macros;

&parse_args;
&init_tables;
&find('.');
&scan_files;
&output;

exit 0;

# Process any command line arguments.
sub parse_args
{
    local ($usage) =
	"Usage: autoscan [--macrodir=dir] [--help] [--verbose] [--version] [srcdir]\n"; 

    foreach $_ (@ARGV) {
	if (/^--m[a-z]*=(.*)/) {
	    $datadir = $1;
	} elsif (/^--h/) {
	    print "$usage";
	    exit 0;
	} elsif (/^--verb/) {
	    $verbose = 1;
	} elsif (/^--vers/) {
	    &version;
	} elsif (/^[^-]/) {
	    die "$usage" if defined($srcdir);
	    # Top level directory of the package being autoscanned.
	    $srcdir = $_;
	} else {
	    die "$usage";
	}
    }

    $srcdir="." if !defined($srcdir);

    print "srcdir=$srcdir\n" if $verbose;
    chdir $srcdir || die "$0: cannot cd to $srcdir: $!\n";

    open(CONF, ">configure.scan") ||
	die "$0: cannot create configure.scan: $!\n";
}

# Print the version number and exit.
sub version
{
    open(ACG, "<$datadir/acgeneral.m4") ||
	die "$0: cannot open $datadir/acgeneral.m4: $!\n";
    while (<ACG>) {
	if (/define.AC_ACVERSION.\s*([0-9.]+)/) {
	    print "Autoconf version $1\n";
	    exit 0;
	}
    }
    die "Autoconf version unknown\n";
}

# Put values in the tables of what to do with each token.
sub init_tables
{
    local($kind, $word, $macro);

    # Initialize a table of C keywords (to ignore).
    # Taken from K&R 1st edition p. 180.
    # ANSI C, GNU C, and C++ keywords can introduce portability problems,
    # so don't ignore them.
    foreach $word ('int', 'char', 'float', 'double', 'struct', 'union',
		   'long', 'short', 'unsigned', 'auto', 'extern', 'register',
		   'typedef', 'static', 'goto', 'return', 'sizeof', 'break',
		   'continue', 'if', 'else', 'for', 'do', 'while', 'switch',
		   'case', 'default') {
	$c_keywords{$word} = 0;
    }

    # The data file format supports only one line of macros per function.
    # If more than that is required for a common portability problem,
    # a new Autoconf macro should probably be written for that case,
    # instead of duplicating the code in lots of configure.in files.

    foreach $kind ('functions', 'headers', 'identifiers', 'programs',
		   'makevars') {
	open(TABLE, "<$datadir/ac$kind") ||
	    die "$0: cannot open $datadir/ac$kind: $!\n";
	while (<TABLE>) {
	    next if /^\s*$/ || /^\s*#/; # Ignore blank lines and comments.
	    ($word, $macro) = split;
	    eval "\$$kind" . "_macros{\$word} = \$macro";
	}
	close(TABLE);
    }
}

# Collect names of various kinds of files in the package.
# Called by &find on each file.
sub wanted
{
    if (/^.*\.[chlymC]$/ || /^.*\.cc$/) {
	$name =~ s?^\./??; push(@cfiles, $name);
    }
    elsif (/^[Mm]akefile$/ || /^[Mm]akefile\.in$/ || /^GNUmakefile$/) {
	$name =~ s?^\./??; push(@makefiles, $name);
    }
    elsif (/^.*\.sh$/) {
	$name =~ s?^\./??; push(@shfiles, $name);
    }
}

# Read through the files and collect lists of tokens in them
# that might create nonportabilities.
sub scan_files
{
    $initfile = $cfiles[0];		# Pick one at random.

    if ($verbose) {
	print "cfiles:", join(" ", @cfiles), "\n";
	print "makefiles:", join(" ", @makefiles), "\n";
	print "shfiles:", join(" ", @shfiles), "\n";
    }

    foreach $file (@cfiles) {
	&scan_c_file($file);
    }

    foreach $file (@makefiles) {
	&scan_makefile($file);
    }

    foreach $file (@shfiles) {
	&scan_sh_file($file);
    }
}

sub scan_c_file
{
    local($file) = @_;
    local($in_comment) = 0;	# Nonzero if in a multiline comment.

    open(CFILE, "<$file") || die "$0: cannot open $file: $!\n";
    while (<CFILE>) {
	# Strip out comments, approximately.
	# Ending on this line.
	if ($in_comment && m,\*/,) {
	    s,.*\*/,,;
	    $in_comment = 0;
	}
	# All on one line.
	s,/\*.*\*/,,g;
	# Starting on this line.
	if (m,/\*,) {
	    $in_comment = 1;
	}
	# Continuing on this line.
	next if $in_comment;

	# Preprocessor directives.
	if (/^\s*#\s*include\s*<([^>]*)>/) {
	    $headers{$1}++;
	}
	# Ignore other preprocessor directives.
	next if /^\s*#/;

	# Remove string and character constants.
	s,\"[^\"]*\",,g;
        s,\'[^\']*\',,g;

	# Tokens in the code.
	# Maybe we should ignore function definitions (in column 0)?
	while (s/\W([a-zA-Z_]\w*)\s*\(/ /) {
	    $functions{$1}++ if !defined($c_keywords{$1});
	}
	while (s/\W([a-zA-Z_]\w*)\W/ /) {
	    $identifiers{$1}++ if !defined($c_keywords{$1});
	}
    }
    close(CFILE);

    if ($verbose) {
	local($word);

	print "\n$file functions:\n";
	foreach $word (sort keys %functions) {
	    print "$word $functions{$word}\n";
	}

	print "\n$file identifiers:\n";
	foreach $word (sort keys %identifiers) {
	    print "$word $identifiers{$word}\n";
	}

	print "\n$file headers:\n";
	foreach $word (sort keys %headers) {
	    print "$word $headers{$word}\n";
	}
    }
}

sub scan_makefile
{
    local($file) = @_;

    open(MFILE, "<$file") || die "$0: cannot open $file: $!\n";
    while (<MFILE>) {
	# Strip out comments and variable references.
	s/#.*//;
	s/\$\([^\)]*\)//g;
	s/\${[^\}]*}//g;
	s/@[^@]*@//g;

	# Variable assignments.
	while (s/\W([a-zA-Z_]\w*)\s*=/ /) {
	    $makevars{$1}++;
	}
	# Libraries.
	while (s/\W-l([a-zA-Z_]\w*)\W/ /) {
	    $libraries{$1}++;
	}
	# Tokens in the code.
	while (s/\W([a-zA-Z_]\w*)\W/ /) {
	    $programs{$1}++;
	}
    }
    close(MFILE);

    if ($verbose) {
	local($word);

	print "\n$file makevars:\n";
	foreach $word (sort keys %makevars) {
	    print "$word $makevars{$word}\n";
	}

	print "\n$file libraries:\n";
	foreach $word (sort keys %libraries) {
	    print "$word $libraries{$word}\n";
	}

	print "\n$file programs:\n";
	foreach $word (sort keys %programs) {
	    print "$word $programs{$word}\n";
	}
    }
}

sub scan_sh_file
{
    local($file) = @_;

    open(MFILE, "<$file") || die "$0: cannot open $file: $!\n";
    while (<MFILE>) {
	# Strip out comments and variable references.
	s/#.*//;
	s/\${[^\}]*}//g;
	s/@[^@]*@//g;

	# Tokens in the code.
	while (s/\W([a-zA-Z_]\w*)\W/ /) {
	    $programs{$1}++;
	}
    }
    close(MFILE);

    if ($verbose) {
	local($word);

	print "\n$file programs:\n";
	foreach $word (sort keys %programs) {
	    print "$word $programs{$word}\n";
	}
    }
}

# Print a configure.in.
sub output
{
    local (%unique_makefiles);

    print CONF "dnl Process this file with autoconf to produce a configure script.\n";
    print CONF "AC_INIT($initfile)\n";

    &output_programs;
    &output_headers;
    &output_identifiers;
    &output_functions;

    # Change DIR/Makefile.in to DIR/Makefile.
    foreach $_ (@makefiles) {
	s/\.in$//;
	$unique_makefiles{$_}++;
    }
    print CONF "\nAC_OUTPUT(", join(" ", keys(%unique_makefiles)), ")\n";

    close CONF;
}

# Print Autoconf macro $1 if it's not undef and hasn't been printed already.
sub print_unique
{
    local($macro) = @_;

    if (defined($macro) && !defined($printed{$macro})) {
	print CONF "$macro\n";
	$printed{$macro} = 1;
    }
}

sub output_programs
{
    local ($word);

    print CONF "\ndnl Checks for programs.\n";
    foreach $word (sort keys %programs) {
	&print_unique($programs_macros{$word});
    }
    foreach $word (sort keys %makevars) {
	&print_unique($makevars_macros{$word});
    }
    print CONF "\ndnl Checks for libraries.\n";
    foreach $word (sort keys %libraries) {
	print CONF "AC_HAVE_LIBRARY($word)\n";
    }
}

sub output_headers
{
    local ($word);

    print CONF "\ndnl Checks for header files.\n";
    foreach $word (sort keys %headers) {
	if (defined($headers_macros{$word}) &&
	    $headers_macros{$word} eq 'AC_CHECK_HEADERS') {
	    push(@have_headers, $word);
	} else {	
	    &print_unique($headers_macros{$word});
	}
    }
    print CONF "AC_CHECK_HEADERS(" . join(' ', sort(@have_headers)) . ")\n"
	if defined(@have_headers);
}

sub output_identifiers
{
    local ($word);

    print CONF "\ndnl Checks for typedefs, structures, and compiler characteristics.\n";
    foreach $word (sort keys %identifiers) {
	&print_unique($identifiers_macros{$word});
    }
}

sub output_functions
{
    local ($word);

    print CONF "\ndnl Checks for library functions.\n";
    foreach $word (sort keys %functions) {
	if (defined($functions_macros{$word}) &&
	    $functions_macros{$word} eq 'AC_CHECK_FUNCS') {
	    push(@have_funcs, $word);
	} else {	
	    &print_unique($functions_macros{$word});
	}
    }
    print CONF "AC_CHECK_FUNCS(" . join(' ', sort(@have_funcs)) . ")\n"
	if defined(@have_funcs);
}