1 ################################################################################
5 ## $Date: 2006/07/03 10:21:54 +0200 $
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 ################################################################################
24 my $VERSION = __VERSION__;
37 my($ppport) = $0 =~ /([\w.]+)$/;
38 my $LF = '(?:\r\n|[\r\n])'; # line feed
39 my $HS = "[ \t]"; # horizontal whitespace
43 Getopt::Long::GetOptions(\%opt, qw(
44 help quiet diag! filter! hints! changes! cplusplus strip version
45 patch=s copy=s diff=s compat-version=s
46 list-provided list-unsupported api-info=s
50 if ($@ and grep /^-/, @ARGV) {
51 usage() if "@ARGV" =~ /^--?h(?:elp)?$/;
52 die "Getopt::Long not found. Please don't use any options.\n";
56 print "This is $0 $VERSION.\n";
60 usage() if $opt{help};
61 strip() if $opt{strip};
63 if (exists $opt{'compat-version'}) {
64 my($r,$v,$s) = eval { parse_version($opt{'compat-version'}) };
66 die "Invalid version number format: '$opt{'compat-version'}'\n";
68 die "Only Perl 5 is supported\n" if $r != 5;
69 die "Invalid version number: $opt{'compat-version'}\n" if $v >= 1000 || $s >= 1000;
70 $opt{'compat-version'} = sprintf "%d.%03d%03d", $r, $v, $s;
73 $opt{'compat-version'} = 5;
76 # Never use C comments in this file!!!!!
79 my $rccs = quotemeta $ccs;
80 my $rcce = quotemeta $cce;
82 my %API = map { /^(\w+)\|([^|]*)\|([^|]*)\|(\w*)$/
84 ($2 ? ( base => $2 ) : ()),
85 ($3 ? ( todo => $3 ) : ()),
86 (index($4, 'v') >= 0 ? ( varargs => 1 ) : ()),
87 (index($4, 'p') >= 0 ? ( provided => 1 ) : ()),
88 (index($4, 'n') >= 0 ? ( nothxarg => 1 ) : ()),
90 : die "invalid spec: $_" } qw(
94 if (exists $opt{'list-unsupported'}) {
96 for $f (sort { lc $a cmp lc $b } keys %API) {
97 next unless $API{$f}{todo};
98 print "$f ", '.'x(40-length($f)), " ", format_version($API{$f}{todo}), "\n";
103 # Scan for possible replacement candidates
105 my(%replace, %need, %hints, %depends);
111 if (m{^\s*\*\s(.*?)\s*$}) {
112 $hints{$hint} ||= ''; # suppress warning with older perls
113 $hints{$hint} .= "$1\n";
119 $hint = $1 if m{^\s*$rccs\sHint:\s+(\w+)\s*$};
121 $replace = $1 if m{^\s*$rccs\s+Replace:\s+(\d+)\s+$rcce\s*$};
122 $replace{$2} = $1 if $replace and m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+)};
123 $replace{$2} = $1 if m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+).*$rccs\s+Replace\s+$rcce};
124 $replace{$1} = $2 if m{^\s*$rccs\s+Replace (\w+) with (\w+)\s+$rcce\s*$};
126 if (m{^\s*$rccs\s+(\w+)\s+depends\s+on\s+(\w+(\s*,\s*\w+)*)\s+$rcce\s*$}) {
127 push @{$depends{$1}}, map { s/\s+//g; $_ } split /,/, $2;
130 $need{$1} = 1 if m{^#if\s+defined\(NEED_(\w+)(?:_GLOBAL)?\)};
133 if (exists $opt{'api-info'}) {
136 my $match = $opt{'api-info'} =~ m!^/(.*)/$! ? $1 : "^\Q$opt{'api-info'}\E\$";
137 for $f (sort { lc $a cmp lc $b } keys %API) {
138 next unless $f =~ /$match/;
139 print "\n=== $f ===\n\n";
141 if ($API{$f}{base} || $API{$f}{todo}) {
142 my $base = format_version($API{$f}{base} || $API{$f}{todo});
143 print "Supported at least starting from perl-$base.\n";
146 if ($API{$f}{provided}) {
147 my $todo = $API{$f}{todo} ? format_version($API{$f}{todo}) : "__MIN_PERL__";
148 print "Support by $ppport provided back to perl-$todo.\n";
149 print "Support needs to be explicitly requested by NEED_$f.\n" if exists $need{$f};
150 print "Depends on: ", join(', ', @{$depends{$f}}), ".\n" if exists $depends{$f};
151 print "$hints{$f}" if exists $hints{$f};
155 print "No portability information available.\n";
163 print "Found no API matching '$opt{'api-info'}'.\n";
168 if (exists $opt{'list-provided'}) {
170 for $f (sort { lc $a cmp lc $b } keys %API) {
171 next unless $API{$f}{provided};
173 push @flags, 'explicit' if exists $need{$f};
174 push @flags, 'depend' if exists $depends{$f};
175 push @flags, 'hint' if exists $hints{$f};
176 my $flags = @flags ? ' ['.join(', ', @flags).']' : '';
183 my @srcext = qw( xs c h cc cpp );
184 my $srcext = join '|', @srcext;
188 @files = grep { -f && !exists $seen{$_} } map { glob $_ } @ARGV;
193 File::Find::find(sub {
194 $File::Find::name =~ /\.($srcext)$/i
195 and push @files, $File::Find::name;
199 @files = map { glob "*.$_" } @srcext;
203 if (!@ARGV || $opt{filter}) {
205 my %xsc = map { /(.*)\.xs$/ ? ("$1.c" => 1, "$1.cc" => 1) : () } @files;
207 my $out = exists $xsc{$_} || /\b\Q$ppport\E$/i || !/\.($srcext)$/i;
208 push @{ $out ? \@out : \@in }, $_;
211 warning("Skipping the following files (use --nofilter to avoid this):\n| ", join "\n| ", @out);
217 die "No input files given!\n";
220 my(%files, %global, %revreplace);
221 %revreplace = reverse %replace;
223 my $patch_opened = 0;
225 for $filename (@files) {
226 unless (open IN, "<$filename") {
227 warn "Unable to read from $filename: $!\n";
231 info("Scanning $filename ...");
233 my $c = do { local $/; <IN> };
236 my %file = (orig => $c, changes => 0);
238 # temporarily remove C comments from the code
244 (?:"[^"\\]*(?:\\.[^"\\]*)*" [^"'/]*)+
246 (?:'[^'\\]*(?:\\.[^'\\]*)*' [^"'/]*)+
250 \*[^*]*\*+(?:[^$ccs][^*]*\*+)* /
255 defined $2 and push @ccom, $2;
256 defined $1 ? $1 : "$ccs$#ccom$cce";
259 $file{ccom} = \@ccom;
261 $file{has_inc_ppport} = ($c =~ /#.*include.*\Q$ppport\E/);
265 for $func (keys %API) {
267 $match .= "|$revreplace{$func}" if exists $revreplace{$func};
268 if ($c =~ /\b(?:Perl_)?($match)\b/) {
269 $file{uses_replace}{$1}++ if exists $revreplace{$func} && $1 eq $revreplace{$func};
270 $file{uses_Perl}{$func}++ if $c =~ /\bPerl_$func\b/;
271 if (exists $API{$func}{provided}) {
272 if (!exists $API{$func}{base} || $API{$func}{base} > $opt{'compat-version'}) {
273 $file{uses}{$func}++;
274 my @deps = rec_depend($func);
276 $file{uses_deps}{$func} = \@deps;
278 $file{uses}{$_} = 0 unless exists $file{uses}{$_};
282 if (exists $need{$_}) {
283 $file{needs}{$_} = 'static';
288 if (exists $API{$func}{todo} && $API{$func}{todo} > $opt{'compat-version'}) {
289 if ($c =~ /\b$func\b/) {
290 $file{uses_todo}{$func}++;
296 while ($c =~ /^$HS*#$HS*define$HS+(NEED_(\w+?)(_GLOBAL)?)\b/mg) {
297 if (exists $need{$2}) {
298 $file{defined $3 ? 'needed_global' : 'needed_static'}{$2}++;
301 warning("Possibly wrong #define $1 in $filename");
305 for (qw(uses needs uses_todo needed_global needed_static)) {
306 for $func (keys %{$file{$_}}) {
307 push @{$global{$_}{$func}}, $filename;
311 $files{$filename} = \%file;
314 # Globally resolve NEED_'s
316 for $need (keys %{$global{needs}}) {
317 if (@{$global{needs}{$need}} > 1) {
318 my @targets = @{$global{needs}{$need}};
319 my @t = grep $files{$_}{needed_global}{$need}, @targets;
321 @t = grep /\.xs$/i, @targets;
323 my $target = shift @targets;
324 $files{$target}{needs}{$need} = 'global';
325 for (@{$global{needs}{$need}}) {
326 $files{$_}{needs}{$need} = 'extern' if $_ ne $target;
331 for $filename (@files) {
332 exists $files{$filename} or next;
334 info("=== Analyzing $filename ===");
336 my %file = %{$files{$filename}};
340 for $func (sort keys %{$file{uses_Perl}}) {
341 if ($API{$func}{varargs}) {
342 my $changes = ($c =~ s{\b(Perl_$func\s*\(\s*)(?!aTHX_?)(\)|[^\s)]*\))}
343 { $1 . ($2 eq ')' ? 'aTHX' : 'aTHX_ ') . $2 }ge);
345 warning("Doesn't pass interpreter argument aTHX to Perl_$func");
346 $file{changes} += $changes;
350 warning("Uses Perl_$func instead of $func");
351 $file{changes} += ($c =~ s{\bPerl_$func(\s*)\((\s*aTHX_?)?\s*}
356 for $func (sort keys %{$file{uses_replace}}) {
357 warning("Uses $func instead of $replace{$func}");
358 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
361 for $func (sort keys %{$file{uses}}) {
362 next unless $file{uses}{$func}; # if it's only a dependency
363 if (exists $file{uses_deps}{$func}) {
364 diag("Uses $func, which depends on ", join(', ', @{$file{uses_deps}{$func}}));
366 elsif (exists $replace{$func}) {
367 warning("Uses $func instead of $replace{$func}");
368 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
376 for $func (sort keys %{$file{uses_todo}}) {
377 warning("Uses $func, which may not be portable below perl ",
378 format_version($API{$func}{todo}));
381 for $func (sort keys %{$file{needed_static}}) {
383 if (not exists $file{uses}{$func}) {
384 $message = "No need to define NEED_$func if $func is never used";
386 elsif (exists $file{needs}{$func} && $file{needs}{$func} ne 'static') {
387 $message = "No need to define NEED_$func when already needed globally";
391 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_$func\b.*$LF//mg);
395 for $func (sort keys %{$file{needed_global}}) {
397 if (not exists $global{uses}{$func}) {
398 $message = "No need to define NEED_${func}_GLOBAL if $func is never used";
400 elsif (exists $file{needs}{$func}) {
401 if ($file{needs}{$func} eq 'extern') {
402 $message = "No need to define NEED_${func}_GLOBAL when already needed globally";
404 elsif ($file{needs}{$func} eq 'static') {
405 $message = "No need to define NEED_${func}_GLOBAL when only used in this file";
410 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_${func}_GLOBAL\b.*$LF//mg);
414 $file{needs_inc_ppport} = keys %{$file{uses}};
416 if ($file{needs_inc_ppport}) {
419 for $func (sort keys %{$file{needs}}) {
420 my $type = $file{needs}{$func};
421 next if $type eq 'extern';
422 my $suffix = $type eq 'global' ? '_GLOBAL' : '';
423 unless (exists $file{"needed_$type"}{$func}) {
424 if ($type eq 'global') {
425 diag("Files [@{$global{needs}{$func}}] need $func, adding global request");
428 diag("File needs $func, adding static request");
430 $pp .= "#define NEED_$func$suffix\n";
434 if ($pp && ($c =~ s/^(?=$HS*#$HS*define$HS+NEED_\w+)/$pp/m)) {
439 unless ($file{has_inc_ppport}) {
440 diag("Needs to include '$ppport'");
441 $pp .= qq(#include "$ppport"\n)
445 $file{changes} += ($c =~ s/^($HS*#$HS*define$HS+NEED_\w+.*?)^/$1$pp/ms)
446 || ($c =~ s/^(?=$HS*#$HS*include.*\Q$ppport\E)/$pp/m)
447 || ($c =~ s/^($HS*#$HS*include.*XSUB.*\s*?)^/$1$pp/m)
452 if ($file{has_inc_ppport}) {
453 diag("No need to include '$ppport'");
454 $file{changes} += ($c =~ s/^$HS*?#$HS*include.*\Q$ppport\E.*?$LF//m);
458 # put back in our C comments
461 my @ccom = @{$file{ccom}};
462 for $ix (0 .. $#ccom) {
463 if (!$opt{cplusplus} && $ccom[$ix] =~ s!^//!!) {
465 $file{changes} += $c =~ s/$rccs$ix$rcce/$ccs$ccom[$ix] $cce/;
468 $c =~ s/$rccs$ix$rcce/$ccom[$ix]/;
473 my $s = $cppc != 1 ? 's' : '';
474 warning("Uses $cppc C++ style comment$s, which is not portable");
477 if ($file{changes}) {
478 if (exists $opt{copy}) {
479 my $newfile = "$filename$opt{copy}";
481 error("'$newfile' already exists, refusing to write copy of '$filename'");
485 if (open F, ">$newfile") {
486 info("Writing copy of '$filename' with changes to '$newfile'");
491 error("Cannot open '$newfile' for writing: $!");
495 elsif (exists $opt{patch} || $opt{changes}) {
496 if (exists $opt{patch}) {
497 unless ($patch_opened) {
498 if (open PATCH, ">$opt{patch}") {
502 error("Cannot open '$opt{patch}' for writing: $!");
508 mydiff(\*PATCH, $filename, $c);
512 info("Suggested changes:");
513 mydiff(\*STDOUT, $filename, $c);
517 my $s = $file{changes} == 1 ? '' : 's';
518 info("$file{changes} potentially required change$s detected");
526 close PATCH if $patch_opened;
530 #######################################################################
535 my($file, $str) = @_;
538 if (exists $opt{diff}) {
539 $diff = run_diff($opt{diff}, $file, $str);
542 if (!defined $diff and can_use('Text::Diff')) {
543 $diff = Text::Diff::diff($file, \$str, { STYLE => 'Unified' });
544 $diff = <<HEADER . $diff;
550 if (!defined $diff) {
551 $diff = run_diff('diff -u', $file, $str);
554 if (!defined $diff) {
555 $diff = run_diff('diff', $file, $str);
558 if (!defined $diff) {
559 error("Cannot generate a diff. Please install Text::Diff or use --copy.");
569 my($prog, $file, $str) = @_;
570 my $tmp = 'dppptemp';
575 while (-e "$tmp.$suf") { $suf++ }
578 if (open F, ">$tmp") {
582 if (open F, "$prog $file $tmp |") {
584 s/\Q$tmp\E/$file.patched/;
595 error("Cannot open '$tmp' for writing: $!");
611 return () unless exists $depends{$func};
612 grep !$seen{$_}++, map { ($_, rec_depend($_)) } @{$depends{$func}};
619 if ($ver =~ /^(\d+)\.(\d+)\.(\d+)$/) {
622 elsif ($ver !~ /^\d+\.[\d_]+$/) {
623 die "cannot parse version '$ver'\n";
629 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
634 if ($r < 5 || ($r == 5 && $v < 6)) {
636 die "cannot parse version '$ver'\n";
648 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
653 if ($r < 5 || ($r == 5 && $v < 6)) {
655 die "invalid version '$ver'\n";
659 $ver = sprintf "%d.%03d", $r, $v;
660 $s > 0 and $ver .= sprintf "_%02d", $s;
665 return sprintf "%d.%d.%d", $r, $v, $s;
670 $opt{quiet} and return;
676 $opt{quiet} and return;
677 $opt{diag} and print @_, "\n";
682 $opt{quiet} and return;
683 print "*** ", @_, "\n";
688 print "*** ERROR: ", @_, "\n";
694 $opt{quiet} and return;
695 $opt{hints} or return;
697 exists $hints{$func} or return;
698 $given_hints{$func}++ and return;
699 my $hint = $hints{$func};
701 print " --- hint for $func ---\n", $hint;
706 my($usage) = do { local(@ARGV,$/)=($0); <> } =~ /^=head\d$HS+SYNOPSIS\s*^(.*?)\s*^=/ms;
707 my %M = ( 'I' => '*' );
708 $usage =~ s/^\s*perl\s+\S+/$^X $0/;
709 $usage =~ s/([A-Z])<([^>]+)>/$M{$1}$2$M{$1}/g;
715 See perldoc $0 for details.
724 my $self = do { local(@ARGV,$/)=($0); <> };
725 my($copy) = $self =~ /^=head\d\s+COPYRIGHT\s*^(.*?)^=\w+/ms;
726 $copy =~ s/^(?=\S+)/ /gms;
727 $self =~ s/^$HS+Do NOT edit.*?(?=^-)/$copy/ms;
728 $self =~ s/^SKIP.*(?=^__DATA__)/SKIP
729 if (\@ARGV && \$ARGV[0] eq '--unstrip') {
730 eval { require Devel::PPPort };
731 \$@ and die "Cannot require Devel::PPPort, please install.\\n";
732 if (\$Devel::PPPort::VERSION < $VERSION) {
733 die "$0 was originally generated with Devel::PPPort $VERSION.\\n"
734 . "Your Devel::PPPort is only version \$Devel::PPPort::VERSION.\\n"
735 . "Please install a newer version, or --unstrip will not work.\\n";
737 Devel::PPPort::WriteFile(\$0);
742 Sorry, but this is a stripped version of \$0.
744 To be able to use its original script and doc functionality,
745 please try to regenerate this file using:
752 open OUT, ">$0" or die "cannot strip $0: $!\n";