1 ################################################################################
5 ## $Date: 2004/12/29 14:54:27 +0100 $
7 ################################################################################
9 ## Version 3.x, Copyright (C) 2004, Marcus Holland-Moritz.
10 ## Version 2.x, Copyright (C) 2001, Paul Marquess.
11 ## Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
13 ## This program is free software; you can redistribute it and/or
14 ## modify it under the same terms as Perl itself.
16 ################################################################################
34 my($ppport) = $0 =~ /([\w.]+)$/;
35 my $LF = '(?:\r\n|[\r\n])'; # line feed
36 my $HS = "[ \t]"; # horizontal whitespace
40 Getopt::Long::GetOptions(\%opt, qw(
41 help quiet diag! hints! changes! cplusplus
42 patch=s copy=s diff=s compat-version=s
43 list-provided list-unsupported api-info=s
47 if ($@ and grep /^-/, @ARGV) {
48 usage() if "@ARGV" =~ /^--?h(?:elp)?$/;
49 die "Getopt::Long not found. Please don't use any options.\n";
52 usage() if $opt{help};
54 if (exists $opt{'compat-version'}) {
55 my($r,$v,$s) = eval { parse_version($opt{'compat-version'}) };
57 die "Invalid version number format: '$opt{'compat-version'}'\n";
59 die "Only Perl 5 is supported\n" if $r != 5;
60 die "Invalid version number: $opt{'compat-version'}\n" if $v >= 1000 || $v >= 1000;
61 $opt{'compat-version'} = sprintf "%d.%03d%03d", $r, $v, $s;
64 $opt{'compat-version'} = 5;
67 # Never use C comments in this file!!!!!
70 my $rccs = quotemeta $ccs;
71 my $rcce = quotemeta $cce;
76 @files = map { glob $_ } @ARGV;
81 File::Find::find(sub {
82 $File::Find::name =~ /\.(xs|c|h|cc)$/i
83 and push @files, $File::Find::name;
87 @files = map { glob $_ } qw(*.xs *.c *.h *.cc);
89 my %filter = map { /(.*)\.xs$/ ? ("$1.c" => 1) : () } @files;
90 @files = grep { !/\b\Q$ppport\E$/i && !exists $filter{$_} } @files;
94 die "No input files given!\n";
97 my %API = map { /^(\w+)\|([^|]*)\|([^|]*)\|(\w*)$/
99 ($2 ? ( base => $2 ) : ()),
100 ($3 ? ( todo => $3 ) : ()),
101 (index($4, 'v') >= 0 ? ( varargs => 1 ) : ()),
102 (index($4, 'p') >= 0 ? ( provided => 1 ) : ()),
103 (index($4, 'n') >= 0 ? ( nothxarg => 1 ) : ()),
105 : die "invalid spec: $_" } qw(
109 if (exists $opt{'list-unsupported'}) {
111 for $f (sort { lc $a cmp lc $b } keys %API) {
112 next unless $API{$f}{todo};
113 print "$f ", '.'x(40-length($f)), " ", format_version($API{$f}{todo}), "\n";
118 # Scan for possible replacement candidates
120 my(%replace, %need, %hints, %depends);
126 if (m{^\s*\*\s(.*?)\s*$}) {
127 $hints{$hint} ||= ''; # suppress warning with older perls
128 $hints{$hint} .= "$1\n";
134 $hint = $1 if m{^\s*$rccs\sHint:\s+(\w+)\s*$};
136 $replace = $1 if m{^\s*$rccs\s+Replace:\s+(\d+)\s+$rcce\s*$};
137 $replace{$2} = $1 if $replace and m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+)};
138 $replace{$2} = $1 if m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+).*$rccs\s+Replace\s+$rcce};
139 $replace{$1} = $2 if m{^\s*$rccs\s+Replace (\w+) with (\w+)\s+$rcce\s*$};
141 if (m{^\s*$rccs\s+(\w+)\s+depends\s+on\s+(\w+(\s*,\s*\w+)*)\s+$rcce\s*$}) {
142 push @{$depends{$1}}, map { s/\s+//g; $_ } split /,/, $2;
145 $need{$1} = 1 if m{^#if\s+defined\(NEED_(\w+)(?:_GLOBAL)?\)};
148 if (exists $opt{'api-info'}) {
151 for $f (sort { lc $a cmp lc $b } keys %API) {
152 next unless $f =~ /$opt{'api-info'}/;
153 print "\n=== $f ===\n\n";
155 if ($API{$f}{base} || $API{$f}{todo}) {
156 my $base = format_version($API{$f}{base} || $API{$f}{todo});
157 print "May not be supported below perl-$base.\n";
160 if ($API{$f}{provided}) {
161 my $todo = $API{$f}{todo} ? format_version($API{$f}{todo}) : "__MIN_PERL__";
162 print "Support by $ppport provided down to perl-$todo.\n";
163 print "Support needs to be explicitly requested by NEED_$f.\n" if exists $need{$f};
164 print "Depends on: ", join(', ', @{$depends{$f}}), ".\n" if exists $depends{$f};
165 print "$hints{$f}" if exists $hints{$f};
169 print "No portability information available.\n";
177 print "Found no API matching $opt{'api-info'}.\n";
182 if (exists $opt{'list-provided'}) {
184 for $f (sort { lc $a cmp lc $b } keys %API) {
185 next unless $API{$f}{provided};
187 push @flags, 'explicit' if exists $need{$f};
188 push @flags, 'depend' if exists $depends{$f};
189 push @flags, 'hint' if exists $hints{$f};
190 my $flags = @flags ? ' ['.join(', ', @flags).']' : '';
196 my(%files, %global, %revreplace);
197 %revreplace = reverse %replace;
199 my $patch_opened = 0;
201 for $filename (@files) {
202 unless (open IN, "<$filename") {
203 warn "Unable to read from $filename: $!\n";
207 info("Scanning $filename ...");
209 my $c = do { local $/; <IN> };
212 my %file = (orig => $c, changes => 0);
214 # temporarily remove C comments from the code
220 (?:"[^"\\]*(?:\\.[^"\\]*)*" [^"'/]*)+
222 (?:'[^'\\]*(?:\\.[^'\\]*)*' [^"'/]*)+
226 \*[^*]*\*+(?:[^$ccs][^*]*\*+)* /
231 defined $2 and push @ccom, $2;
232 defined $1 ? $1 : "$ccs$#ccom$cce";
235 $file{ccom} = \@ccom;
237 $file{has_inc_ppport} = ($c =~ /#.*include.*\Q$ppport\E/);
241 for $func (keys %API) {
243 $match .= "|$revreplace{$func}" if exists $revreplace{$func};
244 if ($c =~ /\b(?:Perl_)?($match)\b/) {
245 $file{uses_replace}{$1}++ if exists $revreplace{$func} && $1 eq $revreplace{$func};
246 $file{uses_Perl}{$func}++ if $c =~ /\bPerl_$func\b/;
247 if (exists $API{$func}{provided}) {
248 if (!exists $API{$func}{base} || $API{$func}{base} > $opt{'compat-version'}) {
249 $file{uses}{$func}++;
250 my @deps = rec_depend($func);
252 $file{uses_deps}{$func} = \@deps;
254 $file{uses}{$_} = 0 unless exists $file{uses}{$_};
258 if (exists $need{$_}) {
259 $file{needs}{$_} = 'static';
264 if (exists $API{$func}{todo} && $API{$func}{todo} > $opt{'compat-version'}) {
265 if ($c =~ /\b$func\b/) {
266 $file{uses_todo}{$func}++;
272 while ($c =~ /^$HS*#$HS*define$HS+(NEED_(\w+?)(_GLOBAL)?)\b/mg) {
273 if (exists $need{$2}) {
274 $file{defined $3 ? 'needed_global' : 'needed_static'}{$2}++;
277 warning("Possibly wrong #define $1 in $filename");
281 for (qw(uses needs uses_todo needed_global needed_static)) {
282 for $func (keys %{$file{$_}}) {
283 push @{$global{$_}{$func}}, $filename;
287 $files{$filename} = \%file;
290 # Globally resolve NEED_'s
292 for $need (keys %{$global{needs}}) {
293 if (@{$global{needs}{$need}} > 1) {
294 my @targets = @{$global{needs}{$need}};
295 my @t = grep $files{$_}{needed_global}{$need}, @targets;
297 @t = grep /\.xs$/i, @targets;
299 my $target = shift @targets;
300 $files{$target}{needs}{$need} = 'global';
301 for (@{$global{needs}{$need}}) {
302 $files{$_}{needs}{$need} = 'extern' if $_ ne $target;
307 for $filename (@files) {
308 exists $files{$filename} or next;
310 info("=== Analyzing $filename ===");
312 my %file = %{$files{$filename}};
316 for $func (sort keys %{$file{uses_Perl}}) {
317 if ($API{$func}{varargs}) {
318 my $changes = ($c =~ s{\b(Perl_$func\s*\(\s*)(?!aTHX_?)(\)|[^\s)]*\))}
319 { $1 . ($2 eq ')' ? 'aTHX' : 'aTHX_ ') . $2 }ge);
321 warning("Doesn't pass interpreter argument aTHX to Perl_$func");
322 $file{changes} += $changes;
326 warning("Uses Perl_$func instead of $func");
327 $file{changes} += ($c =~ s{\bPerl_$func(\s*)\((\s*aTHX_?)?\s*}
332 for $func (sort keys %{$file{uses_replace}}) {
333 warning("Uses $func instead of $replace{$func}");
334 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
337 for $func (sort keys %{$file{uses}}) {
338 next unless $file{uses}{$func}; # if it's only a dependency
339 if (exists $file{uses_deps}{$func}) {
340 diag("Uses $func, which depends on ", join(', ', @{$file{uses_deps}{$func}}));
342 elsif (exists $replace{$func}) {
343 warning("Uses $func instead of $replace{$func}");
344 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
352 for $func (sort keys %{$file{uses_todo}}) {
353 warning("Uses $func, which may not be portable below perl ",
354 format_version($API{$func}{todo}));
357 for $func (sort keys %{$file{needed_static}}) {
359 if (not exists $file{uses}{$func}) {
360 $message = "No need to define NEED_$func if $func is never used";
362 elsif (exists $file{needs}{$func} && $file{needs}{$func} ne 'static') {
363 $message = "No need to define NEED_$func when already needed globally";
367 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_$func\b.*$LF//mg);
371 for $func (sort keys %{$file{needed_global}}) {
373 if (not exists $global{uses}{$func}) {
374 $message = "No need to define NEED_${func}_GLOBAL if $func is never used";
376 elsif (exists $file{needs}{$func}) {
377 if ($file{needs}{$func} eq 'extern') {
378 $message = "No need to define NEED_${func}_GLOBAL when already needed globally";
380 elsif ($file{needs}{$func} eq 'static') {
381 $message = "No need to define NEED_${func}_GLOBAL when only used in this file";
386 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_${func}_GLOBAL\b.*$LF//mg);
390 $file{needs_inc_ppport} = keys %{$file{uses}};
392 if ($file{needs_inc_ppport}) {
395 for $func (sort keys %{$file{needs}}) {
396 my $type = $file{needs}{$func};
397 next if $type eq 'extern';
398 my $suffix = $type eq 'global' ? '_GLOBAL' : '';
399 unless (exists $file{"needed_$type"}{$func}) {
400 if ($type eq 'global') {
401 diag("Files [@{$global{needs}{$func}}] need $func, adding global request");
404 diag("File needs $func, adding static request");
406 $pp .= "#define NEED_$func$suffix\n";
410 if ($pp && ($c =~ s/^(?=$HS*#$HS*define$HS+NEED_\w+)/$pp/m)) {
415 unless ($file{has_inc_ppport}) {
416 diag("Needs to include '$ppport'");
417 $pp .= qq(#include "$ppport"\n)
421 $file{changes} += ($c =~ s/^($HS*#$HS*define$HS+NEED_\w+.*?)^/$1$pp/ms)
422 || ($c =~ s/^(?=$HS*#$HS*include.*\Q$ppport\E)/$pp/m)
423 || ($c =~ s/^($HS*#$HS*include.*XSUB.*\s*?)^/$1$pp/m)
428 if ($file{has_inc_ppport}) {
429 diag("No need to include '$ppport'");
430 $file{changes} += ($c =~ s/^$HS*?#$HS*include.*\Q$ppport\E.*?$LF//m);
434 # put back in our C comments
437 my @ccom = @{$file{ccom}};
438 for $ix (0 .. $#ccom) {
439 if (!$opt{cplusplus} && $ccom[$ix] =~ s!^//!!) {
441 $file{changes} += $c =~ s/$rccs$ix$rcce/$ccs$ccom[$ix] $cce/;
444 $c =~ s/$rccs$ix$rcce/$ccom[$ix]/;
449 my $s = $cppc != 1 ? 's' : '';
450 warning("Uses $cppc C++ style comment$s, which is not portable");
453 if ($file{changes}) {
454 if (exists $opt{copy}) {
455 my $newfile = "$filename$opt{copy}";
457 error("'$newfile' already exists, refusing to write copy of '$filename'");
461 if (open F, ">$newfile") {
462 info("Writing copy of '$filename' with changes to '$newfile'");
467 error("Cannot open '$newfile' for writing: $!");
471 elsif (exists $opt{patch} || $opt{changes}) {
472 if (exists $opt{patch}) {
473 unless ($patch_opened) {
474 if (open PATCH, ">$opt{patch}") {
478 error("Cannot open '$opt{patch}' for writing: $!");
484 mydiff(\*PATCH, $filename, $c);
488 info("Suggested changes:");
489 mydiff(\*STDOUT, $filename, $c);
493 my $s = $file{changes} == 1 ? '' : 's';
494 info("$file{changes} potentially required change$s detected");
502 close PATCH if $patch_opened;
506 #######################################################################
511 my($file, $str) = @_;
514 if (exists $opt{diff}) {
515 $diff = run_diff($opt{diff}, $file, $str);
518 if (!defined $diff and can_use('Text::Diff')) {
519 $diff = Text::Diff::diff($file, \$str, { STYLE => 'Unified' });
520 $diff = <<HEADER . $diff;
526 if (!defined $diff) {
527 $diff = run_diff('diff -u', $file, $str);
530 if (!defined $diff) {
531 $diff = run_diff('diff', $file, $str);
534 if (!defined $diff) {
535 error("Cannot generate a diff. Please install Text::Diff or use --copy.");
545 my($prog, $file, $str) = @_;
546 my $tmp = 'dppptemp';
551 while (-e "$tmp.$suf") { $suf++ }
554 if (open F, ">$tmp") {
558 if (open F, "$prog $file $tmp |") {
560 s/\Q$tmp\E/$file.patched/;
571 error("Cannot open '$tmp' for writing: $!");
587 return () unless exists $depends{$func};
588 grep !$seen{$_}++, map { ($_, rec_depend($_)) } @{$depends{$func}};
595 if ($ver =~ /^(\d+)\.(\d+)\.(\d+)$/) {
598 elsif ($ver !~ /^\d+\.[\d_]+$/) {
599 die "cannot parse version '$ver'\n";
605 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
610 if ($r < 5 || ($r == 5 && $v < 6)) {
612 die "cannot parse version '$ver'\n";
624 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
629 if ($r < 5 || ($r == 5 && $v < 6)) {
631 die "invalid version '$ver'\n";
635 $ver = sprintf "%d.%03d", $r, $v;
636 $s > 0 and $ver .= sprintf "_%02d", $s;
641 return sprintf "%d.%d.%d", $r, $v, $s;
646 $opt{quiet} and return;
652 $opt{quiet} and return;
653 $opt{diag} and print @_, "\n";
658 $opt{quiet} and return;
659 print "*** ", @_, "\n";
664 print "*** ERROR: ", @_, "\n";
670 $opt{quiet} and return;
671 $opt{hints} or return;
673 exists $hints{$func} or return;
674 $given_hints{$func}++ and return;
675 my $hint = $hints{$func};
677 print " --- hint for $func ---\n", $hint;
682 my($usage) = do { local(@ARGV,$/)=($0); <> } =~ /^=head\d$HS+SYNOPSIS\s*^(.*?)\s*^=/ms;
683 my %M = ( 'I' => '*' );
684 $usage =~ s/^\s*perl\s+\S+/$^X $0/;
685 $usage =~ s/([A-Z])<([^>]+)>/$M{$1}$2$M{$1}/g;
691 See perldoc $0 for details.