1 ################################################################################
5 ## $Date: 2006/01/14 18:08:02 +0100 $
7 ################################################################################
9 ## Version 3.x, Copyright (C) 2004-2006, 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 ################################################################################
36 my($ppport) = $0 =~ /([\w.]+)$/;
37 my $LF = '(?:\r\n|[\r\n])'; # line feed
38 my $HS = "[ \t]"; # horizontal whitespace
42 Getopt::Long::GetOptions(\%opt, qw(
43 help quiet diag! filter! hints! changes! cplusplus strip
44 patch=s copy=s diff=s compat-version=s
45 list-provided list-unsupported api-info=s
49 if ($@ and grep /^-/, @ARGV) {
50 usage() if "@ARGV" =~ /^--?h(?:elp)?$/;
51 die "Getopt::Long not found. Please don't use any options.\n";
54 usage() if $opt{help};
55 strip() if $opt{strip};
57 if (exists $opt{'compat-version'}) {
58 my($r,$v,$s) = eval { parse_version($opt{'compat-version'}) };
60 die "Invalid version number format: '$opt{'compat-version'}'\n";
62 die "Only Perl 5 is supported\n" if $r != 5;
63 die "Invalid version number: $opt{'compat-version'}\n" if $v >= 1000 || $s >= 1000;
64 $opt{'compat-version'} = sprintf "%d.%03d%03d", $r, $v, $s;
67 $opt{'compat-version'} = 5;
70 # Never use C comments in this file!!!!!
73 my $rccs = quotemeta $ccs;
74 my $rcce = quotemeta $cce;
76 my %API = map { /^(\w+)\|([^|]*)\|([^|]*)\|(\w*)$/
78 ($2 ? ( base => $2 ) : ()),
79 ($3 ? ( todo => $3 ) : ()),
80 (index($4, 'v') >= 0 ? ( varargs => 1 ) : ()),
81 (index($4, 'p') >= 0 ? ( provided => 1 ) : ()),
82 (index($4, 'n') >= 0 ? ( nothxarg => 1 ) : ()),
84 : die "invalid spec: $_" } qw(
88 if (exists $opt{'list-unsupported'}) {
90 for $f (sort { lc $a cmp lc $b } keys %API) {
91 next unless $API{$f}{todo};
92 print "$f ", '.'x(40-length($f)), " ", format_version($API{$f}{todo}), "\n";
97 # Scan for possible replacement candidates
99 my(%replace, %need, %hints, %depends);
105 if (m{^\s*\*\s(.*?)\s*$}) {
106 $hints{$hint} ||= ''; # suppress warning with older perls
107 $hints{$hint} .= "$1\n";
113 $hint = $1 if m{^\s*$rccs\sHint:\s+(\w+)\s*$};
115 $replace = $1 if m{^\s*$rccs\s+Replace:\s+(\d+)\s+$rcce\s*$};
116 $replace{$2} = $1 if $replace and m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+)};
117 $replace{$2} = $1 if m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+).*$rccs\s+Replace\s+$rcce};
118 $replace{$1} = $2 if m{^\s*$rccs\s+Replace (\w+) with (\w+)\s+$rcce\s*$};
120 if (m{^\s*$rccs\s+(\w+)\s+depends\s+on\s+(\w+(\s*,\s*\w+)*)\s+$rcce\s*$}) {
121 push @{$depends{$1}}, map { s/\s+//g; $_ } split /,/, $2;
124 $need{$1} = 1 if m{^#if\s+defined\(NEED_(\w+)(?:_GLOBAL)?\)};
127 if (exists $opt{'api-info'}) {
130 my $match = $opt{'api-info'} =~ m!^/(.*)/$! ? $1 : "^\Q$opt{'api-info'}\E\$";
131 for $f (sort { lc $a cmp lc $b } keys %API) {
132 next unless $f =~ /$match/;
133 print "\n=== $f ===\n\n";
135 if ($API{$f}{base} || $API{$f}{todo}) {
136 my $base = format_version($API{$f}{base} || $API{$f}{todo});
137 print "Supported at least starting from perl-$base.\n";
140 if ($API{$f}{provided}) {
141 my $todo = $API{$f}{todo} ? format_version($API{$f}{todo}) : "__MIN_PERL__";
142 print "Support by $ppport provided back to perl-$todo.\n";
143 print "Support needs to be explicitly requested by NEED_$f.\n" if exists $need{$f};
144 print "Depends on: ", join(', ', @{$depends{$f}}), ".\n" if exists $depends{$f};
145 print "$hints{$f}" if exists $hints{$f};
149 print "No portability information available.\n";
157 print "Found no API matching '$opt{'api-info'}'.\n";
162 if (exists $opt{'list-provided'}) {
164 for $f (sort { lc $a cmp lc $b } keys %API) {
165 next unless $API{$f}{provided};
167 push @flags, 'explicit' if exists $need{$f};
168 push @flags, 'depend' if exists $depends{$f};
169 push @flags, 'hint' if exists $hints{$f};
170 my $flags = @flags ? ' ['.join(', ', @flags).']' : '';
177 my @srcext = qw( xs c h cc cpp );
178 my $srcext = join '|', @srcext;
182 @files = grep { -f && !exists $seen{$_} } map { glob $_ } @ARGV;
187 File::Find::find(sub {
188 $File::Find::name =~ /\.($srcext)$/i
189 and push @files, $File::Find::name;
193 @files = map { glob "*.$_" } @srcext;
197 if (!@ARGV || $opt{filter}) {
199 my %xsc = map { /(.*)\.xs$/ ? ("$1.c" => 1, "$1.cc" => 1) : () } @files;
201 my $out = exists $xsc{$_} || /\b\Q$ppport\E$/i || !/\.($srcext)$/i;
202 push @{ $out ? \@out : \@in }, $_;
205 warning("Skipping the following files (use --nofilter to avoid this):\n| ", join "\n| ", @out);
211 die "No input files given!\n";
214 my(%files, %global, %revreplace);
215 %revreplace = reverse %replace;
217 my $patch_opened = 0;
219 for $filename (@files) {
220 unless (open IN, "<$filename") {
221 warn "Unable to read from $filename: $!\n";
225 info("Scanning $filename ...");
227 my $c = do { local $/; <IN> };
230 my %file = (orig => $c, changes => 0);
232 # temporarily remove C comments from the code
238 (?:"[^"\\]*(?:\\.[^"\\]*)*" [^"'/]*)+
240 (?:'[^'\\]*(?:\\.[^'\\]*)*' [^"'/]*)+
244 \*[^*]*\*+(?:[^$ccs][^*]*\*+)* /
249 defined $2 and push @ccom, $2;
250 defined $1 ? $1 : "$ccs$#ccom$cce";
253 $file{ccom} = \@ccom;
255 $file{has_inc_ppport} = ($c =~ /#.*include.*\Q$ppport\E/);
259 for $func (keys %API) {
261 $match .= "|$revreplace{$func}" if exists $revreplace{$func};
262 if ($c =~ /\b(?:Perl_)?($match)\b/) {
263 $file{uses_replace}{$1}++ if exists $revreplace{$func} && $1 eq $revreplace{$func};
264 $file{uses_Perl}{$func}++ if $c =~ /\bPerl_$func\b/;
265 if (exists $API{$func}{provided}) {
266 if (!exists $API{$func}{base} || $API{$func}{base} > $opt{'compat-version'}) {
267 $file{uses}{$func}++;
268 my @deps = rec_depend($func);
270 $file{uses_deps}{$func} = \@deps;
272 $file{uses}{$_} = 0 unless exists $file{uses}{$_};
276 if (exists $need{$_}) {
277 $file{needs}{$_} = 'static';
282 if (exists $API{$func}{todo} && $API{$func}{todo} > $opt{'compat-version'}) {
283 if ($c =~ /\b$func\b/) {
284 $file{uses_todo}{$func}++;
290 while ($c =~ /^$HS*#$HS*define$HS+(NEED_(\w+?)(_GLOBAL)?)\b/mg) {
291 if (exists $need{$2}) {
292 $file{defined $3 ? 'needed_global' : 'needed_static'}{$2}++;
295 warning("Possibly wrong #define $1 in $filename");
299 for (qw(uses needs uses_todo needed_global needed_static)) {
300 for $func (keys %{$file{$_}}) {
301 push @{$global{$_}{$func}}, $filename;
305 $files{$filename} = \%file;
308 # Globally resolve NEED_'s
310 for $need (keys %{$global{needs}}) {
311 if (@{$global{needs}{$need}} > 1) {
312 my @targets = @{$global{needs}{$need}};
313 my @t = grep $files{$_}{needed_global}{$need}, @targets;
315 @t = grep /\.xs$/i, @targets;
317 my $target = shift @targets;
318 $files{$target}{needs}{$need} = 'global';
319 for (@{$global{needs}{$need}}) {
320 $files{$_}{needs}{$need} = 'extern' if $_ ne $target;
325 for $filename (@files) {
326 exists $files{$filename} or next;
328 info("=== Analyzing $filename ===");
330 my %file = %{$files{$filename}};
334 for $func (sort keys %{$file{uses_Perl}}) {
335 if ($API{$func}{varargs}) {
336 my $changes = ($c =~ s{\b(Perl_$func\s*\(\s*)(?!aTHX_?)(\)|[^\s)]*\))}
337 { $1 . ($2 eq ')' ? 'aTHX' : 'aTHX_ ') . $2 }ge);
339 warning("Doesn't pass interpreter argument aTHX to Perl_$func");
340 $file{changes} += $changes;
344 warning("Uses Perl_$func instead of $func");
345 $file{changes} += ($c =~ s{\bPerl_$func(\s*)\((\s*aTHX_?)?\s*}
350 for $func (sort keys %{$file{uses_replace}}) {
351 warning("Uses $func instead of $replace{$func}");
352 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
355 for $func (sort keys %{$file{uses}}) {
356 next unless $file{uses}{$func}; # if it's only a dependency
357 if (exists $file{uses_deps}{$func}) {
358 diag("Uses $func, which depends on ", join(', ', @{$file{uses_deps}{$func}}));
360 elsif (exists $replace{$func}) {
361 warning("Uses $func instead of $replace{$func}");
362 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
370 for $func (sort keys %{$file{uses_todo}}) {
371 warning("Uses $func, which may not be portable below perl ",
372 format_version($API{$func}{todo}));
375 for $func (sort keys %{$file{needed_static}}) {
377 if (not exists $file{uses}{$func}) {
378 $message = "No need to define NEED_$func if $func is never used";
380 elsif (exists $file{needs}{$func} && $file{needs}{$func} ne 'static') {
381 $message = "No need to define NEED_$func when already needed globally";
385 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_$func\b.*$LF//mg);
389 for $func (sort keys %{$file{needed_global}}) {
391 if (not exists $global{uses}{$func}) {
392 $message = "No need to define NEED_${func}_GLOBAL if $func is never used";
394 elsif (exists $file{needs}{$func}) {
395 if ($file{needs}{$func} eq 'extern') {
396 $message = "No need to define NEED_${func}_GLOBAL when already needed globally";
398 elsif ($file{needs}{$func} eq 'static') {
399 $message = "No need to define NEED_${func}_GLOBAL when only used in this file";
404 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_${func}_GLOBAL\b.*$LF//mg);
408 $file{needs_inc_ppport} = keys %{$file{uses}};
410 if ($file{needs_inc_ppport}) {
413 for $func (sort keys %{$file{needs}}) {
414 my $type = $file{needs}{$func};
415 next if $type eq 'extern';
416 my $suffix = $type eq 'global' ? '_GLOBAL' : '';
417 unless (exists $file{"needed_$type"}{$func}) {
418 if ($type eq 'global') {
419 diag("Files [@{$global{needs}{$func}}] need $func, adding global request");
422 diag("File needs $func, adding static request");
424 $pp .= "#define NEED_$func$suffix\n";
428 if ($pp && ($c =~ s/^(?=$HS*#$HS*define$HS+NEED_\w+)/$pp/m)) {
433 unless ($file{has_inc_ppport}) {
434 diag("Needs to include '$ppport'");
435 $pp .= qq(#include "$ppport"\n)
439 $file{changes} += ($c =~ s/^($HS*#$HS*define$HS+NEED_\w+.*?)^/$1$pp/ms)
440 || ($c =~ s/^(?=$HS*#$HS*include.*\Q$ppport\E)/$pp/m)
441 || ($c =~ s/^($HS*#$HS*include.*XSUB.*\s*?)^/$1$pp/m)
446 if ($file{has_inc_ppport}) {
447 diag("No need to include '$ppport'");
448 $file{changes} += ($c =~ s/^$HS*?#$HS*include.*\Q$ppport\E.*?$LF//m);
452 # put back in our C comments
455 my @ccom = @{$file{ccom}};
456 for $ix (0 .. $#ccom) {
457 if (!$opt{cplusplus} && $ccom[$ix] =~ s!^//!!) {
459 $file{changes} += $c =~ s/$rccs$ix$rcce/$ccs$ccom[$ix] $cce/;
462 $c =~ s/$rccs$ix$rcce/$ccom[$ix]/;
467 my $s = $cppc != 1 ? 's' : '';
468 warning("Uses $cppc C++ style comment$s, which is not portable");
471 if ($file{changes}) {
472 if (exists $opt{copy}) {
473 my $newfile = "$filename$opt{copy}";
475 error("'$newfile' already exists, refusing to write copy of '$filename'");
479 if (open F, ">$newfile") {
480 info("Writing copy of '$filename' with changes to '$newfile'");
485 error("Cannot open '$newfile' for writing: $!");
489 elsif (exists $opt{patch} || $opt{changes}) {
490 if (exists $opt{patch}) {
491 unless ($patch_opened) {
492 if (open PATCH, ">$opt{patch}") {
496 error("Cannot open '$opt{patch}' for writing: $!");
502 mydiff(\*PATCH, $filename, $c);
506 info("Suggested changes:");
507 mydiff(\*STDOUT, $filename, $c);
511 my $s = $file{changes} == 1 ? '' : 's';
512 info("$file{changes} potentially required change$s detected");
520 close PATCH if $patch_opened;
524 #######################################################################
529 my($file, $str) = @_;
532 if (exists $opt{diff}) {
533 $diff = run_diff($opt{diff}, $file, $str);
536 if (!defined $diff and can_use('Text::Diff')) {
537 $diff = Text::Diff::diff($file, \$str, { STYLE => 'Unified' });
538 $diff = <<HEADER . $diff;
544 if (!defined $diff) {
545 $diff = run_diff('diff -u', $file, $str);
548 if (!defined $diff) {
549 $diff = run_diff('diff', $file, $str);
552 if (!defined $diff) {
553 error("Cannot generate a diff. Please install Text::Diff or use --copy.");
563 my($prog, $file, $str) = @_;
564 my $tmp = 'dppptemp';
569 while (-e "$tmp.$suf") { $suf++ }
572 if (open F, ">$tmp") {
576 if (open F, "$prog $file $tmp |") {
578 s/\Q$tmp\E/$file.patched/;
589 error("Cannot open '$tmp' for writing: $!");
605 return () unless exists $depends{$func};
606 grep !$seen{$_}++, map { ($_, rec_depend($_)) } @{$depends{$func}};
613 if ($ver =~ /^(\d+)\.(\d+)\.(\d+)$/) {
616 elsif ($ver !~ /^\d+\.[\d_]+$/) {
617 die "cannot parse version '$ver'\n";
623 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
628 if ($r < 5 || ($r == 5 && $v < 6)) {
630 die "cannot parse version '$ver'\n";
642 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
647 if ($r < 5 || ($r == 5 && $v < 6)) {
649 die "invalid version '$ver'\n";
653 $ver = sprintf "%d.%03d", $r, $v;
654 $s > 0 and $ver .= sprintf "_%02d", $s;
659 return sprintf "%d.%d.%d", $r, $v, $s;
664 $opt{quiet} and return;
670 $opt{quiet} and return;
671 $opt{diag} and print @_, "\n";
676 $opt{quiet} and return;
677 print "*** ", @_, "\n";
682 print "*** ERROR: ", @_, "\n";
688 $opt{quiet} and return;
689 $opt{hints} or return;
691 exists $hints{$func} or return;
692 $given_hints{$func}++ and return;
693 my $hint = $hints{$func};
695 print " --- hint for $func ---\n", $hint;
700 my($usage) = do { local(@ARGV,$/)=($0); <> } =~ /^=head\d$HS+SYNOPSIS\s*^(.*?)\s*^=/ms;
701 my %M = ( 'I' => '*' );
702 $usage =~ s/^\s*perl\s+\S+/$^X $0/;
703 $usage =~ s/([A-Z])<([^>]+)>/$M{$1}$2$M{$1}/g;
709 See perldoc $0 for details.
718 my $self = do { local(@ARGV,$/)=($0); <> };
719 $self =~ s/^$HS+Do NOT edit.*?(?=^-)//ms;
720 $self =~ s/^SKIP.*(?=^__DATA__)/SKIP
721 if (\@ARGV && \$ARGV[0] eq '--unstrip') {
722 eval { require Devel::PPPort };
723 \$@ and die "Cannot require Devel::PPPort, please install.\\n";
724 Devel::PPPort::WriteFile(\$0);
729 Sorry, but this is a stripped version of \$0.
731 To be able to use its original script and doc functionality,
732 please try to regenerate this file using:
739 open OUT, ">$0" or die "cannot strip $0: $!\n";