1 ################################################################################
5 ## $Date: 2005/03/10 18:08:41 +0100 $
7 ################################################################################
9 ## Version 3.x, Copyright (C) 2004-2005, 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 ################################################################################
35 my($ppport) = $0 =~ /([\w.]+)$/;
36 my $LF = '(?:\r\n|[\r\n])'; # line feed
37 my $HS = "[ \t]"; # horizontal whitespace
41 Getopt::Long::GetOptions(\%opt, qw(
42 help quiet diag! filter! hints! changes! cplusplus
43 patch=s copy=s diff=s compat-version=s
44 list-provided list-unsupported api-info=s
48 if ($@ and grep /^-/, @ARGV) {
49 usage() if "@ARGV" =~ /^--?h(?:elp)?$/;
50 die "Getopt::Long not found. Please don't use any options.\n";
53 usage() if $opt{help};
55 if (exists $opt{'compat-version'}) {
56 my($r,$v,$s) = eval { parse_version($opt{'compat-version'}) };
58 die "Invalid version number format: '$opt{'compat-version'}'\n";
60 die "Only Perl 5 is supported\n" if $r != 5;
61 die "Invalid version number: $opt{'compat-version'}\n" if $v >= 1000 || $s >= 1000;
62 $opt{'compat-version'} = sprintf "%d.%03d%03d", $r, $v, $s;
65 $opt{'compat-version'} = 5;
68 # Never use C comments in this file!!!!!
71 my $rccs = quotemeta $ccs;
72 my $rcce = quotemeta $cce;
74 my %API = map { /^(\w+)\|([^|]*)\|([^|]*)\|(\w*)$/
76 ($2 ? ( base => $2 ) : ()),
77 ($3 ? ( todo => $3 ) : ()),
78 (index($4, 'v') >= 0 ? ( varargs => 1 ) : ()),
79 (index($4, 'p') >= 0 ? ( provided => 1 ) : ()),
80 (index($4, 'n') >= 0 ? ( nothxarg => 1 ) : ()),
82 : die "invalid spec: $_" } qw(
86 if (exists $opt{'list-unsupported'}) {
88 for $f (sort { lc $a cmp lc $b } keys %API) {
89 next unless $API{$f}{todo};
90 print "$f ", '.'x(40-length($f)), " ", format_version($API{$f}{todo}), "\n";
95 # Scan for possible replacement candidates
97 my(%replace, %need, %hints, %depends);
103 if (m{^\s*\*\s(.*?)\s*$}) {
104 $hints{$hint} ||= ''; # suppress warning with older perls
105 $hints{$hint} .= "$1\n";
111 $hint = $1 if m{^\s*$rccs\sHint:\s+(\w+)\s*$};
113 $replace = $1 if m{^\s*$rccs\s+Replace:\s+(\d+)\s+$rcce\s*$};
114 $replace{$2} = $1 if $replace and m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+)};
115 $replace{$2} = $1 if m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+).*$rccs\s+Replace\s+$rcce};
116 $replace{$1} = $2 if m{^\s*$rccs\s+Replace (\w+) with (\w+)\s+$rcce\s*$};
118 if (m{^\s*$rccs\s+(\w+)\s+depends\s+on\s+(\w+(\s*,\s*\w+)*)\s+$rcce\s*$}) {
119 push @{$depends{$1}}, map { s/\s+//g; $_ } split /,/, $2;
122 $need{$1} = 1 if m{^#if\s+defined\(NEED_(\w+)(?:_GLOBAL)?\)};
125 if (exists $opt{'api-info'}) {
128 my $match = $opt{'api-info'} =~ m!^/(.*)/$! ? $1 : "^\Q$opt{'api-info'}\E\$";
129 for $f (sort { lc $a cmp lc $b } keys %API) {
130 next unless $f =~ /$match/;
131 print "\n=== $f ===\n\n";
133 if ($API{$f}{base} || $API{$f}{todo}) {
134 my $base = format_version($API{$f}{base} || $API{$f}{todo});
135 print "Supported at least starting from perl-$base.\n";
138 if ($API{$f}{provided}) {
139 my $todo = $API{$f}{todo} ? format_version($API{$f}{todo}) : "__MIN_PERL__";
140 print "Support by $ppport provided back to perl-$todo.\n";
141 print "Support needs to be explicitly requested by NEED_$f.\n" if exists $need{$f};
142 print "Depends on: ", join(', ', @{$depends{$f}}), ".\n" if exists $depends{$f};
143 print "$hints{$f}" if exists $hints{$f};
147 print "No portability information available.\n";
155 print "Found no API matching '$opt{'api-info'}'.\n";
160 if (exists $opt{'list-provided'}) {
162 for $f (sort { lc $a cmp lc $b } keys %API) {
163 next unless $API{$f}{provided};
165 push @flags, 'explicit' if exists $need{$f};
166 push @flags, 'depend' if exists $depends{$f};
167 push @flags, 'hint' if exists $hints{$f};
168 my $flags = @flags ? ' ['.join(', ', @flags).']' : '';
175 my @srcext = qw( xs c h cc cpp );
176 my $srcext = join '|', @srcext;
180 @files = grep { -f && !exists $seen{$_} } map { glob $_ } @ARGV;
185 File::Find::find(sub {
186 $File::Find::name =~ /\.($srcext)$/i
187 and push @files, $File::Find::name;
191 @files = map { glob "*.$_" } @srcext;
195 if (!@ARGV || $opt{filter}) {
197 my %xsc = map { /(.*)\.xs$/ ? ("$1.c" => 1, "$1.cc" => 1) : () } @files;
199 my $out = exists $xsc{$_} || /\b\Q$ppport\E$/i || !/\.($srcext)$/i;
200 push @{ $out ? \@out : \@in }, $_;
203 warning("Skipping the following files (use --nofilter to avoid this):\n| ", join "\n| ", @out);
209 die "No input files given!\n";
212 my(%files, %global, %revreplace);
213 %revreplace = reverse %replace;
215 my $patch_opened = 0;
217 for $filename (@files) {
218 unless (open IN, "<$filename") {
219 warn "Unable to read from $filename: $!\n";
223 info("Scanning $filename ...");
225 my $c = do { local $/; <IN> };
228 my %file = (orig => $c, changes => 0);
230 # temporarily remove C comments from the code
236 (?:"[^"\\]*(?:\\.[^"\\]*)*" [^"'/]*)+
238 (?:'[^'\\]*(?:\\.[^'\\]*)*' [^"'/]*)+
242 \*[^*]*\*+(?:[^$ccs][^*]*\*+)* /
247 defined $2 and push @ccom, $2;
248 defined $1 ? $1 : "$ccs$#ccom$cce";
251 $file{ccom} = \@ccom;
253 $file{has_inc_ppport} = ($c =~ /#.*include.*\Q$ppport\E/);
257 for $func (keys %API) {
259 $match .= "|$revreplace{$func}" if exists $revreplace{$func};
260 if ($c =~ /\b(?:Perl_)?($match)\b/) {
261 $file{uses_replace}{$1}++ if exists $revreplace{$func} && $1 eq $revreplace{$func};
262 $file{uses_Perl}{$func}++ if $c =~ /\bPerl_$func\b/;
263 if (exists $API{$func}{provided}) {
264 if (!exists $API{$func}{base} || $API{$func}{base} > $opt{'compat-version'}) {
265 $file{uses}{$func}++;
266 my @deps = rec_depend($func);
268 $file{uses_deps}{$func} = \@deps;
270 $file{uses}{$_} = 0 unless exists $file{uses}{$_};
274 if (exists $need{$_}) {
275 $file{needs}{$_} = 'static';
280 if (exists $API{$func}{todo} && $API{$func}{todo} > $opt{'compat-version'}) {
281 if ($c =~ /\b$func\b/) {
282 $file{uses_todo}{$func}++;
288 while ($c =~ /^$HS*#$HS*define$HS+(NEED_(\w+?)(_GLOBAL)?)\b/mg) {
289 if (exists $need{$2}) {
290 $file{defined $3 ? 'needed_global' : 'needed_static'}{$2}++;
293 warning("Possibly wrong #define $1 in $filename");
297 for (qw(uses needs uses_todo needed_global needed_static)) {
298 for $func (keys %{$file{$_}}) {
299 push @{$global{$_}{$func}}, $filename;
303 $files{$filename} = \%file;
306 # Globally resolve NEED_'s
308 for $need (keys %{$global{needs}}) {
309 if (@{$global{needs}{$need}} > 1) {
310 my @targets = @{$global{needs}{$need}};
311 my @t = grep $files{$_}{needed_global}{$need}, @targets;
313 @t = grep /\.xs$/i, @targets;
315 my $target = shift @targets;
316 $files{$target}{needs}{$need} = 'global';
317 for (@{$global{needs}{$need}}) {
318 $files{$_}{needs}{$need} = 'extern' if $_ ne $target;
323 for $filename (@files) {
324 exists $files{$filename} or next;
326 info("=== Analyzing $filename ===");
328 my %file = %{$files{$filename}};
332 for $func (sort keys %{$file{uses_Perl}}) {
333 if ($API{$func}{varargs}) {
334 my $changes = ($c =~ s{\b(Perl_$func\s*\(\s*)(?!aTHX_?)(\)|[^\s)]*\))}
335 { $1 . ($2 eq ')' ? 'aTHX' : 'aTHX_ ') . $2 }ge);
337 warning("Doesn't pass interpreter argument aTHX to Perl_$func");
338 $file{changes} += $changes;
342 warning("Uses Perl_$func instead of $func");
343 $file{changes} += ($c =~ s{\bPerl_$func(\s*)\((\s*aTHX_?)?\s*}
348 for $func (sort keys %{$file{uses_replace}}) {
349 warning("Uses $func instead of $replace{$func}");
350 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
353 for $func (sort keys %{$file{uses}}) {
354 next unless $file{uses}{$func}; # if it's only a dependency
355 if (exists $file{uses_deps}{$func}) {
356 diag("Uses $func, which depends on ", join(', ', @{$file{uses_deps}{$func}}));
358 elsif (exists $replace{$func}) {
359 warning("Uses $func instead of $replace{$func}");
360 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
368 for $func (sort keys %{$file{uses_todo}}) {
369 warning("Uses $func, which may not be portable below perl ",
370 format_version($API{$func}{todo}));
373 for $func (sort keys %{$file{needed_static}}) {
375 if (not exists $file{uses}{$func}) {
376 $message = "No need to define NEED_$func if $func is never used";
378 elsif (exists $file{needs}{$func} && $file{needs}{$func} ne 'static') {
379 $message = "No need to define NEED_$func when already needed globally";
383 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_$func\b.*$LF//mg);
387 for $func (sort keys %{$file{needed_global}}) {
389 if (not exists $global{uses}{$func}) {
390 $message = "No need to define NEED_${func}_GLOBAL if $func is never used";
392 elsif (exists $file{needs}{$func}) {
393 if ($file{needs}{$func} eq 'extern') {
394 $message = "No need to define NEED_${func}_GLOBAL when already needed globally";
396 elsif ($file{needs}{$func} eq 'static') {
397 $message = "No need to define NEED_${func}_GLOBAL when only used in this file";
402 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_${func}_GLOBAL\b.*$LF//mg);
406 $file{needs_inc_ppport} = keys %{$file{uses}};
408 if ($file{needs_inc_ppport}) {
411 for $func (sort keys %{$file{needs}}) {
412 my $type = $file{needs}{$func};
413 next if $type eq 'extern';
414 my $suffix = $type eq 'global' ? '_GLOBAL' : '';
415 unless (exists $file{"needed_$type"}{$func}) {
416 if ($type eq 'global') {
417 diag("Files [@{$global{needs}{$func}}] need $func, adding global request");
420 diag("File needs $func, adding static request");
422 $pp .= "#define NEED_$func$suffix\n";
426 if ($pp && ($c =~ s/^(?=$HS*#$HS*define$HS+NEED_\w+)/$pp/m)) {
431 unless ($file{has_inc_ppport}) {
432 diag("Needs to include '$ppport'");
433 $pp .= qq(#include "$ppport"\n)
437 $file{changes} += ($c =~ s/^($HS*#$HS*define$HS+NEED_\w+.*?)^/$1$pp/ms)
438 || ($c =~ s/^(?=$HS*#$HS*include.*\Q$ppport\E)/$pp/m)
439 || ($c =~ s/^($HS*#$HS*include.*XSUB.*\s*?)^/$1$pp/m)
444 if ($file{has_inc_ppport}) {
445 diag("No need to include '$ppport'");
446 $file{changes} += ($c =~ s/^$HS*?#$HS*include.*\Q$ppport\E.*?$LF//m);
450 # put back in our C comments
453 my @ccom = @{$file{ccom}};
454 for $ix (0 .. $#ccom) {
455 if (!$opt{cplusplus} && $ccom[$ix] =~ s!^//!!) {
457 $file{changes} += $c =~ s/$rccs$ix$rcce/$ccs$ccom[$ix] $cce/;
460 $c =~ s/$rccs$ix$rcce/$ccom[$ix]/;
465 my $s = $cppc != 1 ? 's' : '';
466 warning("Uses $cppc C++ style comment$s, which is not portable");
469 if ($file{changes}) {
470 if (exists $opt{copy}) {
471 my $newfile = "$filename$opt{copy}";
473 error("'$newfile' already exists, refusing to write copy of '$filename'");
477 if (open F, ">$newfile") {
478 info("Writing copy of '$filename' with changes to '$newfile'");
483 error("Cannot open '$newfile' for writing: $!");
487 elsif (exists $opt{patch} || $opt{changes}) {
488 if (exists $opt{patch}) {
489 unless ($patch_opened) {
490 if (open PATCH, ">$opt{patch}") {
494 error("Cannot open '$opt{patch}' for writing: $!");
500 mydiff(\*PATCH, $filename, $c);
504 info("Suggested changes:");
505 mydiff(\*STDOUT, $filename, $c);
509 my $s = $file{changes} == 1 ? '' : 's';
510 info("$file{changes} potentially required change$s detected");
518 close PATCH if $patch_opened;
522 #######################################################################
527 my($file, $str) = @_;
530 if (exists $opt{diff}) {
531 $diff = run_diff($opt{diff}, $file, $str);
534 if (!defined $diff and can_use('Text::Diff')) {
535 $diff = Text::Diff::diff($file, \$str, { STYLE => 'Unified' });
536 $diff = <<HEADER . $diff;
542 if (!defined $diff) {
543 $diff = run_diff('diff -u', $file, $str);
546 if (!defined $diff) {
547 $diff = run_diff('diff', $file, $str);
550 if (!defined $diff) {
551 error("Cannot generate a diff. Please install Text::Diff or use --copy.");
561 my($prog, $file, $str) = @_;
562 my $tmp = 'dppptemp';
567 while (-e "$tmp.$suf") { $suf++ }
570 if (open F, ">$tmp") {
574 if (open F, "$prog $file $tmp |") {
576 s/\Q$tmp\E/$file.patched/;
587 error("Cannot open '$tmp' for writing: $!");
603 return () unless exists $depends{$func};
604 grep !$seen{$_}++, map { ($_, rec_depend($_)) } @{$depends{$func}};
611 if ($ver =~ /^(\d+)\.(\d+)\.(\d+)$/) {
614 elsif ($ver !~ /^\d+\.[\d_]+$/) {
615 die "cannot parse version '$ver'\n";
621 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
626 if ($r < 5 || ($r == 5 && $v < 6)) {
628 die "cannot parse version '$ver'\n";
640 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
645 if ($r < 5 || ($r == 5 && $v < 6)) {
647 die "invalid version '$ver'\n";
651 $ver = sprintf "%d.%03d", $r, $v;
652 $s > 0 and $ver .= sprintf "_%02d", $s;
657 return sprintf "%d.%d.%d", $r, $v, $s;
662 $opt{quiet} and return;
668 $opt{quiet} and return;
669 $opt{diag} and print @_, "\n";
674 $opt{quiet} and return;
675 print "*** ", @_, "\n";
680 print "*** ERROR: ", @_, "\n";
686 $opt{quiet} and return;
687 $opt{hints} or return;
689 exists $hints{$func} or return;
690 $given_hints{$func}++ and return;
691 my $hint = $hints{$func};
693 print " --- hint for $func ---\n", $hint;
698 my($usage) = do { local(@ARGV,$/)=($0); <> } =~ /^=head\d$HS+SYNOPSIS\s*^(.*?)\s*^=/ms;
699 my %M = ( 'I' => '*' );
700 $usage =~ s/^\s*perl\s+\S+/$^X $0/;
701 $usage =~ s/([A-Z])<([^>]+)>/$M{$1}$2$M{$1}/g;
707 See perldoc $0 for details.