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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
#! @PERL@ -w
# -*- perl -*-
# autoscan - Create configure.scan (a preliminary configure.ac) for a package.
# Copyright 2010,2023 Thomas E. Dickey
# Copyright 1994, 1999, 2000, 2001 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., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# Written by David MacKenzie <djm@gnu.ai.mit.edu>.
use 5.005;
use File::Basename;
use File::Find;
use Getopt::Long;
use IO::File;
use strict;
use vars qw(@cfiles @makefiles @shfiles %c_keywords %printed);
my $me = basename ($0);
my $verbose = 0;
# $USED{KIND}{ITEM} is set if ITEM is used in the program.
# It is set to its list of locations.
my %used = ();
# $MACRO{KIND}{ITEM} is the list of macros to use to test ITEM.
my %macro = ();
# $NEEDED_MACROS{MACRO} is an array of locations requiring MACRO.
my %needed_macros = ();
my @kinds = qw (functions headers identifiers programs makevars libraries);
# For each kind, the default macro.
my %generic_macro =
(
'functions' => 'AC_CHECK_FUNCS',
'headers' => 'AC_CHECK_HEADERS',
'identifiers' => 'AC_CHECK_TYPES',
'programs' => 'AC_CHECK_PROGS',
'libraries' => 'AC_CHECK_LIB'
);
my %kind_comment =
(
'functions' => 'Checks for library functions.',
'headers' => 'Checks for header files.',
'identifiers' => 'Checks for typedefs, structures, and compiler characteristics.',
'programs' => 'Checks for programs.',
);
my $configure_scan = 'configure.scan';
my $log = new IO::File ">$me.log"
or die "$me: cannot open $me.log: $!\n";
# Autoconf and lib files.
my $autoconf;
my $datadir = $ENV{"AC_MACRODIR"} || "@datadir@";
# Exit nonzero whenever closing STDOUT fails.
sub END
{
use POSIX qw (_exit);
# This is required if the code might send any output to stdout
# E.g., even --version or --help. So it's best to do it unconditionally.
close STDOUT
or (warn "$me: closing standard output: $!\n"), _exit (1);
}
## ------------------------ ##
## Command line interface. ##
## ------------------------ ##
# print_usage ()
# --------------
# Display usage (--help).
sub print_usage ()
{
print "Usage: $0 [OPTION] ... [SRCDIR]
Examine source files in the directory tree rooted at SRCDIR, or the
current directory if none is given. Search the source files for
common portability problems, check for incompleteness of
`configure.ac', and create a file `$configure_scan' which is a
preliminary `configure.ac' for that package.
-h, --help print this help, then exit
-V, --version print version number, then exit
-v, --verbose verbosely report processing
Library directories:
-A, --autoconf-dir=ACDIR Autoconf's files location (rarely needed)
-l, --localdir=DIR location of `aclocal.m4' and `acconfig.h'
Report bugs to <@PACKAGE_BUGREPORT@>.\n";
exit 0;
}
# print_version ()
# ----------------
# Display version (--version).
sub print_version
{
print "autoscan (@PACKAGE_NAME@) @VERSION@
Written by David J. MacKenzie.
Copyright 1994, 1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
exit 0;
}
# parse_args ()
# -------------
# Process any command line arguments.
sub parse_args ()
{
my $srcdir;
Getopt::Long::config ("bundling");
Getopt::Long::GetOptions ("A|autoconf-dir|m|macrodir=s" => \$datadir,
"h|help" => \&print_usage,
"V|version" => \&print_version,
"v|verbose" => \$verbose)
or exit 1;
die "$me: too many arguments
Try `$me --help' for more information.\n"
if (@ARGV > 1);
($srcdir) = @ARGV;
$srcdir = "."
if !defined $srcdir;
print "srcdir=$srcdir\n" if $verbose;
chdir $srcdir || die "$me: cannot cd to $srcdir: $!\n";
}
# find_autoconf
# -------------
# Find the lib files and autoconf.
sub find_autoconf
{
my $dir = dirname ($0);
# We test "$dir/autoconf" in case we are in the build tree, in which case
# the names are not transformed yet.
foreach my $file ($ENV{"AUTOCONF"} || '',
"$dir/@autoconf-name@",
"$dir/autoconf",
"@bindir@/@autoconf-name@")
{
if (-x $file)
{
$autoconf = $file;
last;
}
}
}
# $CONFIGURE_AC
# &find_configure_ac ()
# ---------------------
sub find_configure_ac ()
{
if (-f 'configure.ac')
{
if (-f 'configure.in')
{
warn "warning: `configure.ac' and `configure.in' both present.\n";
warn "warning: proceeding with `configure.ac'.\n";
}
return 'configure.ac';
}
elsif (-f 'configure.in')
{
return 'configure.in';
}
return;
}
# init_tables ()
# --------------
# Put values in the tables of what to do with each token.
sub init_tables ()
{
# 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 (qw (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{$_} = 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.ac files.
my $tables_are_consistent = 1;
foreach my $kind (@kinds)
{
my $file = "$datadir/ac$kind";
my $table = new IO::File $file
or die "$me: cannot open $file: $!\n";
while ($_ = $table->getline)
{
# Ignore blank lines and comments.
next
if /^\s*$/ || /^\s*\#/;
unless (/^(\S+)\s+(\S.*)$/ || /^(\S+)\s*$/)
{
die "$me: cannot parse definition in $file:\n$_\n";
}
my $word = $1;
my $macro = $2 || $generic_macro{$kind};
# The default macro must be explicitly listed for words
# which have a specific macros. This allows to enforce
# consistency checks.
if (!defined $2 && exists $macro{$kind}{$word})
{
warn ("$datadir/ac$kind:$.: "
. "ignoring implicit call to the generic macro for $word\n");
$tables_are_consistent = 0;
}
else
{
push @{$macro{$kind}{$word}}, $macro;
}
}
$table->close
or die "$me: cannot close $file: $!\n";
}
die "$me: some tables are inconsistent\n"
if !$tables_are_consistent;
}
## ----------------------- ##
## Scanning source files. ##
## ----------------------- ##
# scan_c_file(FILENAME)
# ---------------------
sub scan_c_file ($)
{
my ($filename) = @_;
push (@cfiles, $File::Find::name);
# Nonzero if in a multiline comment.
my $in_comment = 0;
my $file = new IO::File "<$filename"
or die "$me: cannot open $filename: $!\n";
while ($_ = $file->getline)
{
# 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*<([^>]*)>/)
{
push (@{$used{'headers'}{$1}}, "$File::Find::name:$.");
}
# 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/\b([a-zA-Z_]\w*)\s*\(/ /)
{
push (@{$used{'functions'}{$1}}, "$File::Find::name:$.")
if !defined $c_keywords{$1};
}
while (s/\b([a-zA-Z_]\w*)\b/ /)
{
push (@{$used{'identifiers'}{$1}}, "$File::Find::name:$.")
if !defined $c_keywords{$1};
}
}
$file->close
or die "$me: cannot close $filename: $!\n";
}
# scan_makefile(MAKEFILE-NAME)
# ----------------------------
sub scan_makefile ($)
{
my ($filename) = @_;
push (@makefiles, $File::Find::name);
my $file = new IO::File "<$filename"
or die "$me: cannot open $filename: $!\n";
while ($_ = $file->getline)
{
# Strip out comments and variable references.
s/#.*//;
s/\$\([^\)]*\)//g;
s/\$\{[^\}]*\}//g;
s/@[^@]*@//g;
# Variable assignments.
while (s/\b([a-zA-Z_]\w*)\s*=/ /)
{
push (@{$used{'makevars'}{$1}}, "$File::Find::name:$.");
}
# Libraries.
while (s/\B-l([a-zA-Z_]\w*)\b/ /)
{
push (@{$used{'libraries'}{$1}}, "$File::Find::name:$.");
}
# Tokens in the code.
while (s/(?<![-\w.])([a-zA-Z_][\w+.-]+)/ /)
{
push (@{$used{'programs'}{$1}}, "$File::Find::name:$.");
}
}
$file->close
or die "$me: cannot close $filename: $!\n";
}
# scan_sh_file(SHELL-SCRIPT-NAME)
# -------------------------------
sub scan_sh_file ($)
{
my ($filename) = @_;
push (@shfiles, $File::Find::name);
my $file = new IO::File "<$filename"
or die "$me: cannot open $filename: $!\n";
while ($_ = $file->getline)
{
# Strip out comments and variable references.
s/#.*//;
s/#.*//;
s/\$\{[^\}]*\}//g;
s/@[^@]*@//g;
# Tokens in the code.
while (s/\b([a-zA-Z_]\w*)\b/ /)
{
push (@{$used{'programs'}{$1}}, "$File::Find::name:$.");
}
}
$file->close
or die "$me: cannot close $filename: $!\n";
}
# scan_file ()
# ------------
# Called by &find on each file. $_ contains the current filename with
# the current directory of the walk through.
sub scan_file ()
{
# Wanted only if there is no corresponding FILE.in.
return
if -f "$_.in";
# Save $_ as Find::File requires it to be preserved.
my $underscore = $_;
# Strip a useless leading `./'.
$File::Find::name =~ s,^\./,,;
if (/\.[chlym](\.in)?$/)
{
push (@{$used{'programs'}{"cc"}}, $File::Find::name);
scan_c_file ($_);
}
elsif (/\.(cc|cpp|cxx|CC|C|hh|hpp|hxx|HH|H|yy|ypp|ll|lpp)(\.in)?$/)
{
push (@{$used{'programs'}{"c++"}}, $File::Find::name);
scan_c_file ($_);
}
elsif (/^[Mm]akefile(\.in)?$/ || /^GNUmakefile(\.in)?$/)
{
scan_makefile ($_);
}
elsif (/\.sh(\.in)?$/)
{
scan_sh_file ($_);
}
$_ = $underscore;
}
# scan_files ()
# -------------
# Read through the files and collect lists of tokens in them
# that might create nonportabilities.
sub scan_files ()
{
find (\&scan_file, '.');
if ($verbose)
{
print "cfiles:", join(" ", @cfiles), "\n";
print "makefiles:", join(" ", @makefiles), "\n";
print "shfiles:", join(" ", @shfiles), "\n";
foreach my $kind (@kinds)
{
print "\n$kind:\n";
foreach my $word (sort keys %{$used{$kind}})
{
print "$word: @{$used{$kind}{$word}}\n";
}
}
}
}
## ----------------------- ##
## Output configure.scan. ##
## ----------------------- ##
# output_kind ($FILE, $KIND)
# --------------------------
sub output_kind ($$)
{
my ($file, $kind) = @_;
# Lists of words to be checked with the generic macro.
my @have;
print $file "\n# $kind_comment{$kind}\n"
if exists $kind_comment{$kind};
foreach my $word (sort keys %{$used{$kind}})
{
# Words that were caught, but not to be checked according to
# the autoscan library files.
next
if ! exists $macro{$kind}{$word};
# Output the needed macro invocations in $configure_scan if not
# already printed, and remember these macros are needed.
foreach my $macro (@{$macro{$kind}{$word}})
{
if (exists $generic_macro{$kind}
&& $macro eq $generic_macro{$kind})
{
push (@have, $word);
push (@{$needed_macros{"$generic_macro{$kind}([$word])"}},
@{$used{$kind}{$word}});
}
else
{
if (! $printed{$macro})
{
print $file "$macro\n";
$printed{$macro} = 1;
}
push (@{$needed_macros{$macro}},
@{$used{$kind}{$word}});
}
}
}
print $file "$generic_macro{$kind}([" . join(' ', sort(@have)) . "])\n"
if @have;
}
# output_libraries ($FILE)
# ------------------------
sub output_libraries ($)
{
my ($file) = @_;
print $file "\n# Checks for libraries.\n";
foreach my $word (sort keys %{$used{'libraries'}})
{
print $file "# FIXME: Replace `main' with a function in `-l$word':\n";
print $file "AC_CHECK_LIB([$word], [main])\n";
}
}
# output (CONFIGURE_SCAN)
# -----------------------
# Print a proto configure.ac.
sub output ($)
{
my $configure_scan = shift;
my %unique_makefiles;
my $file = new IO::File ">$configure_scan"
or die "$me: cannot create $configure_scan: $!\n";
print $file "# Process this file with autoconf to produce a configure script.\n";
print $file "AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)\n";
if (defined $cfiles[0])
{
print $file "AC_CONFIG_SRCDIR([$cfiles[0]])\n";
print $file "AC_CONFIG_HEADER([config.h])\n";
}
output_kind ($file, 'programs');
output_kind ($file, 'makevars');
output_libraries ($file);
output_kind ($file, 'headers');
output_kind ($file, 'identifiers');
output_kind ($file, 'functions');
# Change DIR/Makefile.in to DIR/Makefile.
foreach my $m (@makefiles)
{
$m =~ s/\.in$//;
$unique_makefiles{$m}++;
}
print $file "\nAC_CONFIG_FILES([",
join ("\n ", sort keys %unique_makefiles), "])\n";
print $file "AC_OUTPUT\n";
$file->close
or die "$me: cannot close $configure_scan: $!\n";
}
## --------------------------------------- ##
## Checking the accuracy of configure.ac. ##
## --------------------------------------- ##
# check_configure_ac (CONFIGURE_AC)
# ---------------------------------
# Use autoconf to check if all the suggested macros are included
# in CONFIGURE_AC.
sub check_configure_ac ($)
{
my ($configure_ac) = @_;
my ($trace_option) = '';
# Find what needed macros are invoked in CONFIGURE_AC.
foreach my $macro (sort keys %needed_macros)
{
$macro =~ s/\(.*//;
$trace_option .= " -t $macro";
}
my $traces =
new IO::File "$autoconf -A $datadir $trace_option $configure_ac|"
or die "$me: cannot create read traces: $!\n";
while ($_ = $traces->getline)
{
chomp;
my ($file, $line, $macro, @args) = split (/:/, $_);
if ($macro =~ /^AC_CHECK_(HEADER|FUNC|TYPE|MEMBER)S$/)
{
# To be rigorous, we should distinguish between space and comma
# separated macros. But there is no point.
foreach my $word (split (/\s|,/, $args[0]))
{
# AC_CHECK_MEMBERS wants `struct' or `union'.
if ($macro eq "AC_CHECK_MEMBERS"
&& $word =~ /^stat.st_/)
{
$word = "struct " . $word;
}
delete ($needed_macros{"$macro([$word])"});
}
}
else
{
delete ($needed_macros{$macro});
}
}
$traces->close
or die "$me: cannot close: $!\n";
# Report the missing macros.
foreach my $macro (sort keys %needed_macros)
{
warn ("$configure_ac: warning: missing $macro wanted by: "
. (${$needed_macros{$macro}}[0])
. "\n");
print $log "$me: warning: missing $macro wanted by: \n";
foreach my $need (@{$needed_macros{$macro}})
{
print $log "\t$need\n";
}
}
}
## -------------- ##
## Main program. ##
## -------------- ##
parse_args;
# Find the lib files and autoconf.
find_autoconf;
my $configure_ac = find_configure_ac;
init_tables;
scan_files;
output ('configure.scan');
if ($configure_ac)
{
check_configure_ac ($configure_ac);
}
$log->close
or die "$me: cannot close $me.log: $!\n";
exit 0;
|