1 ################################################################################
3 ## Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
4 ## Version 2.x, Copyright (C) 2001, Paul Marquess.
5 ## Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
7 ## This program is free software; you can redistribute it and/or
8 ## modify it under the same terms as Perl itself.
10 ################################################################################
18 # Disable broken TRIE-optimization
19 BEGIN { eval '${^RE_TRIE_MAXBUF} = -1' if "$]" >= 5.009004 && "$]" <= 5.009005 }
21 my $VERSION = __VERSION__;
34 my($ppport) = $0 =~ /([\w.]+)$/;
35 my $LF = '(?:\r\n|[\r\n])'; # line feed
36 my $HS = "[ \t]"; # horizontal whitespace
38 # Never use C comments in this file!
41 my $rccs = quotemeta $ccs;
42 my $rcce = quotemeta $cce;
46 Getopt::Long::GetOptions(\%opt, qw(
47 help quiet diag! filter! hints! changes! cplusplus strip version
48 patch=s copy=s diff=s compat-version=s
49 list-provided list-unsupported api-info=s
53 if ($@ and grep /^-/, @ARGV) {
54 usage() if "@ARGV" =~ /^--?h(?:elp)?$/;
55 die "Getopt::Long not found. Please don't use any options.\n";
59 print "This is $0 $VERSION.\n";
63 usage() if $opt{help};
64 strip() if $opt{strip};
66 if (exists $opt{'compat-version'}) {
67 my($r,$v,$s) = eval { parse_version($opt{'compat-version'}) };
69 $opt{'compat-version'} = sprintf "%d.%03d%03d", $r, $v, $s;
72 $opt{'compat-version'} = 5;
75 my %API = map { /^(\w+)\|([^|]*)\|([^|]*)\|(\w*)$/
77 ($2 ? ( base => $2 ) : ()),
78 ($3 ? ( todo => $3 ) : ()),
79 (index($4, 'v') >= 0 ? ( varargs => 1 ) : ()),
80 (index($4, 'p') >= 0 ? ( provided => 1 ) : ()),
81 (index($4, 'n') >= 0 ? ( nothxarg => 1 ) : ()),
83 : die "invalid spec: $_" } qw(
87 if (exists $opt{'list-unsupported'}) {
89 for $f (sort dictionary_order keys %API) {
90 next unless $API{$f}{todo};
91 print "$f ", '.'x(40-length($f)), " ", format_version($API{$f}{todo}), "\n";
96 # Scan for possible replacement candidates
98 my(%replace, %need, %hints, %warnings, %depends);
100 my($hint, $define, $function);
106 / (?: \*[^*]*\*+(?:[^$ccs][^*]*\*+)* / | /[^\r\n]*)
107 | "[^"\\]*(?:\\.[^"\\]*)*"
108 | '[^'\\]*(?:\\.[^'\\]*)*' }{}egsx;
109 grep { exists $API{$_} } $code =~ /(\w+)/mg;
114 my $h = $hint->[0] eq 'Hint' ? \%hints : \%warnings;
115 if (m{^\s*\*\s(.*?)\s*$}) {
116 for (@{$hint->[1]}) {
117 $h->{$_} ||= ''; # suppress warning with older perls
124 $hint = [$1, [split /,?\s+/, $2]]
125 if m{^\s*$rccs\s+(Hint|Warning):\s+(\w+(?:,?\s+\w+)*)\s*$};
128 if ($define->[1] =~ /\\$/) {
132 if (exists $API{$define->[0]} && $define->[1] !~ /^DPPP_\(/) {
133 my @n = find_api($define->[1]);
134 push @{$depends{$define->[0]}}, @n if @n
140 $define = [$1, $2] if m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(.*)};
144 if (exists $API{$function->[0]}) {
145 my @n = find_api($function->[1]);
146 push @{$depends{$function->[0]}}, @n if @n
151 $function->[1] .= $_;
155 $function = [$1, ''] if m{^DPPP_\(my_(\w+)\)};
157 $replace = $1 if m{^\s*$rccs\s+Replace:\s+(\d+)\s+$rcce\s*$};
158 $replace{$2} = $1 if $replace and m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+)};
159 $replace{$2} = $1 if m{^\s*#\s*define\s+(\w+)(?:\([^)]*\))?\s+(\w+).*$rccs\s+Replace\s+$rcce};
160 $replace{$1} = $2 if m{^\s*$rccs\s+Replace (\w+) with (\w+)\s+$rcce\s*$};
162 if (m{^\s*$rccs\s+(\w+(\s*,\s*\w+)*)\s+depends\s+on\s+(\w+(\s*,\s*\w+)*)\s+$rcce\s*$}) {
163 my @deps = map { s/\s+//g; $_ } split /,/, $3;
165 for $d (map { s/\s+//g; $_ } split /,/, $1) {
166 push @{$depends{$d}}, @deps;
170 $need{$1} = 1 if m{^#if\s+defined\(NEED_(\w+)(?:_GLOBAL)?\)};
173 for (values %depends) {
175 $_ = [sort dictionary_order grep !$s{$_}++, @$_];
178 if (exists $opt{'api-info'}) {
181 my $match = $opt{'api-info'} =~ m!^/(.*)/$! ? $1 : "^\Q$opt{'api-info'}\E\$";
182 for $f (sort dictionary_order keys %API) {
183 next unless $f =~ /$match/;
184 print "\n=== $f ===\n\n";
186 if ($API{$f}{base} || $API{$f}{todo}) {
187 my $base = format_version($API{$f}{base} || $API{$f}{todo});
188 print "Supported at least starting from perl-$base.\n";
191 if ($API{$f}{provided}) {
192 my $todo = $API{$f}{todo} ? format_version($API{$f}{todo}) : "__MIN_PERL__";
193 print "Support by $ppport provided back to perl-$todo.\n";
194 print "Support needs to be explicitly requested by NEED_$f.\n" if exists $need{$f};
195 print "Depends on: ", join(', ', @{$depends{$f}}), ".\n" if exists $depends{$f};
196 print "\n$hints{$f}" if exists $hints{$f};
197 print "\nWARNING:\n$warnings{$f}" if exists $warnings{$f};
200 print "No portability information available.\n" unless $info;
203 $count or print "Found no API matching '$opt{'api-info'}'.";
208 if (exists $opt{'list-provided'}) {
210 for $f (sort dictionary_order keys %API) {
211 next unless $API{$f}{provided};
213 push @flags, 'explicit' if exists $need{$f};
214 push @flags, 'depend' if exists $depends{$f};
215 push @flags, 'hint' if exists $hints{$f};
216 push @flags, 'warning' if exists $warnings{$f};
217 my $flags = @flags ? ' ['.join(', ', @flags).']' : '';
224 my @srcext = qw( .xs .c .h .cc .cpp -c.inc -xs.inc );
225 my $srcext = join '|', map { quotemeta $_ } @srcext;
232 push @files, $_ unless $seen{$_}++;
234 else { warn "'$_' is not a file.\n" }
237 my @new = grep { -f } glob $_
238 or warn "'$_' does not exist.\n";
239 push @files, grep { !$seen{$_}++ } @new;
246 File::Find::find(sub {
247 $File::Find::name =~ /($srcext)$/i
248 and push @files, $File::Find::name;
252 @files = map { glob "*$_" } @srcext;
256 if (!@ARGV || $opt{filter}) {
258 my %xsc = map { /(.*)\.xs$/ ? ("$1.c" => 1, "$1.cc" => 1) : () } @files;
260 my $out = exists $xsc{$_} || /\b\Q$ppport\E$/i || !/($srcext)$/i;
261 push @{ $out ? \@out : \@in }, $_;
264 warning("Skipping the following files (use --nofilter to avoid this):\n| ", join "\n| ", @out);
269 die "No input files given!\n" unless @files;
271 my(%files, %global, %revreplace);
272 %revreplace = reverse %replace;
274 my $patch_opened = 0;
276 for $filename (@files) {
277 unless (open IN, "<$filename") {
278 warn "Unable to read from $filename: $!\n";
282 info("Scanning $filename ...");
284 my $c = do { local $/; <IN> };
287 my %file = (orig => $c, changes => 0);
289 # Temporarily remove C/XS comments and strings from the code
293 ( ^$HS*\#$HS*include\b[^\r\n]+\b(?:\Q$ppport\E|XSUB\.h)\b[^\r\n]*
294 | ^$HS*\#$HS*(?:define|elif|if(?:def)?)\b[^\r\n]* )
296 | "[^"\\]*(?:\\.[^"\\]*)*"
297 | '[^'\\]*(?:\\.[^'\\]*)*'
298 | / (?: \*[^*]*\*+(?:[^$ccs][^*]*\*+)* / | /[^\r\n]* ) )
299 }{ defined $2 and push @ccom, $2;
300 defined $1 ? $1 : "$ccs$#ccom$cce" }mgsex;
302 $file{ccom} = \@ccom;
304 $file{has_inc_ppport} = $c =~ /^$HS*#$HS*include[^\r\n]+\b\Q$ppport\E\b/m;
308 for $func (keys %API) {
310 $match .= "|$revreplace{$func}" if exists $revreplace{$func};
311 if ($c =~ /\b(?:Perl_)?($match)\b/) {
312 $file{uses_replace}{$1}++ if exists $revreplace{$func} && $1 eq $revreplace{$func};
313 $file{uses_Perl}{$func}++ if $c =~ /\bPerl_$func\b/;
314 if (exists $API{$func}{provided}) {
315 $file{uses_provided}{$func}++;
316 if (!exists $API{$func}{base} || $API{$func}{base} > $opt{'compat-version'}) {
317 $file{uses}{$func}++;
318 my @deps = rec_depend($func);
320 $file{uses_deps}{$func} = \@deps;
322 $file{uses}{$_} = 0 unless exists $file{uses}{$_};
326 $file{needs}{$_} = 'static' if exists $need{$_};
330 if (exists $API{$func}{todo} && $API{$func}{todo} > $opt{'compat-version'}) {
331 if ($c =~ /\b$func\b/) {
332 $file{uses_todo}{$func}++;
338 while ($c =~ /^$HS*#$HS*define$HS+(NEED_(\w+?)(_GLOBAL)?)\b/mg) {
339 if (exists $need{$2}) {
340 $file{defined $3 ? 'needed_global' : 'needed_static'}{$2}++;
342 else { warning("Possibly wrong #define $1 in $filename") }
345 for (qw(uses needs uses_todo needed_global needed_static)) {
346 for $func (keys %{$file{$_}}) {
347 push @{$global{$_}{$func}}, $filename;
351 $files{$filename} = \%file;
354 # Globally resolve NEED_'s
356 for $need (keys %{$global{needs}}) {
357 if (@{$global{needs}{$need}} > 1) {
358 my @targets = @{$global{needs}{$need}};
359 my @t = grep $files{$_}{needed_global}{$need}, @targets;
361 @t = grep /\.xs$/i, @targets;
363 my $target = shift @targets;
364 $files{$target}{needs}{$need} = 'global';
365 for (@{$global{needs}{$need}}) {
366 $files{$_}{needs}{$need} = 'extern' if $_ ne $target;
371 for $filename (@files) {
372 exists $files{$filename} or next;
374 info("=== Analyzing $filename ===");
376 my %file = %{$files{$filename}};
381 for $func (sort dictionary_order keys %{$file{uses_Perl}}) {
382 if ($API{$func}{varargs}) {
383 unless ($API{$func}{nothxarg}) {
384 my $changes = ($c =~ s{\b(Perl_$func\s*\(\s*)(?!aTHX_?)(\)|[^\s)]*\))}
385 { $1 . ($2 eq ')' ? 'aTHX' : 'aTHX_ ') . $2 }ge);
387 warning("Doesn't pass interpreter argument aTHX to Perl_$func");
388 $file{changes} += $changes;
393 warning("Uses Perl_$func instead of $func");
394 $file{changes} += ($c =~ s{\bPerl_$func(\s*)\((\s*aTHX_?)?\s*}
399 for $func (sort dictionary_order keys %{$file{uses_replace}}) {
400 warning("Uses $func instead of $replace{$func}");
401 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
404 for $func (sort dictionary_order keys %{$file{uses_provided}}) {
405 if ($file{uses}{$func}) {
406 if (exists $file{uses_deps}{$func}) {
407 diag("Uses $func, which depends on ", join(', ', @{$file{uses_deps}{$func}}));
413 $warnings += hint($func);
416 unless ($opt{quiet}) {
417 for $func (sort dictionary_order keys %{$file{uses_todo}}) {
418 print "*** WARNING: Uses $func, which may not be portable below perl ",
419 format_version($API{$func}{todo}), ", even with '$ppport'\n";
424 for $func (sort dictionary_order keys %{$file{needed_static}}) {
426 if (not exists $file{uses}{$func}) {
427 $message = "No need to define NEED_$func if $func is never used";
429 elsif (exists $file{needs}{$func} && $file{needs}{$func} ne 'static') {
430 $message = "No need to define NEED_$func when already needed globally";
434 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_$func\b.*$LF//mg);
438 for $func (sort dictionary_order keys %{$file{needed_global}}) {
440 if (not exists $global{uses}{$func}) {
441 $message = "No need to define NEED_${func}_GLOBAL if $func is never used";
443 elsif (exists $file{needs}{$func}) {
444 if ($file{needs}{$func} eq 'extern') {
445 $message = "No need to define NEED_${func}_GLOBAL when already needed globally";
447 elsif ($file{needs}{$func} eq 'static') {
448 $message = "No need to define NEED_${func}_GLOBAL when only used in this file";
453 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_${func}_GLOBAL\b.*$LF//mg);
457 $file{needs_inc_ppport} = keys %{$file{uses}};
459 if ($file{needs_inc_ppport}) {
462 for $func (sort dictionary_order keys %{$file{needs}}) {
463 my $type = $file{needs}{$func};
464 next if $type eq 'extern';
465 my $suffix = $type eq 'global' ? '_GLOBAL' : '';
466 unless (exists $file{"needed_$type"}{$func}) {
467 if ($type eq 'global') {
468 diag("Files [@{$global{needs}{$func}}] need $func, adding global request");
471 diag("File needs $func, adding static request");
473 $pp .= "#define NEED_$func$suffix\n";
477 if ($pp && ($c =~ s/^(?=$HS*#$HS*define$HS+NEED_\w+)/$pp/m)) {
482 unless ($file{has_inc_ppport}) {
483 diag("Needs to include '$ppport'");
484 $pp .= qq(#include "$ppport"\n)
488 $file{changes} += ($c =~ s/^($HS*#$HS*define$HS+NEED_\w+.*?)^/$1$pp/ms)
489 || ($c =~ s/^(?=$HS*#$HS*include.*\Q$ppport\E)/$pp/m)
490 || ($c =~ s/^($HS*#$HS*include.*XSUB.*\s*?)^/$1$pp/m)
495 if ($file{has_inc_ppport}) {
496 diag("No need to include '$ppport'");
497 $file{changes} += ($c =~ s/^$HS*?#$HS*include.*\Q$ppport\E.*?$LF//m);
501 # put back in our C comments
504 my @ccom = @{$file{ccom}};
505 for $ix (0 .. $#ccom) {
506 if (!$opt{cplusplus} && $ccom[$ix] =~ s!^//!!) {
508 $file{changes} += $c =~ s/$rccs$ix$rcce/$ccs$ccom[$ix] $cce/;
511 $c =~ s/$rccs$ix$rcce/$ccom[$ix]/;
516 my $s = $cppc != 1 ? 's' : '';
517 warning("Uses $cppc C++ style comment$s, which is not portable");
520 my $s = $warnings != 1 ? 's' : '';
521 my $warn = $warnings ? " ($warnings warning$s)" : '';
522 info("Analysis completed$warn");
524 if ($file{changes}) {
525 if (exists $opt{copy}) {
526 my $newfile = "$filename$opt{copy}";
528 error("'$newfile' already exists, refusing to write copy of '$filename'");
532 if (open F, ">$newfile") {
533 info("Writing copy of '$filename' with changes to '$newfile'");
538 error("Cannot open '$newfile' for writing: $!");
542 elsif (exists $opt{patch} || $opt{changes}) {
543 if (exists $opt{patch}) {
544 unless ($patch_opened) {
545 if (open PATCH, ">$opt{patch}") {
549 error("Cannot open '$opt{patch}' for writing: $!");
555 mydiff(\*PATCH, $filename, $c);
559 info("Suggested changes:");
560 mydiff(\*STDOUT, $filename, $c);
564 my $s = $file{changes} == 1 ? '' : 's';
565 info("$file{changes} potentially required change$s detected");
573 close PATCH if $patch_opened;
577 #######################################################################
579 sub try_use { eval "use @_;"; return $@ eq '' }
584 my($file, $str) = @_;
587 if (exists $opt{diff}) {
588 $diff = run_diff($opt{diff}, $file, $str);
591 if (!defined $diff and try_use('Text::Diff')) {
592 $diff = Text::Diff::diff($file, \$str, { STYLE => 'Unified' });
593 $diff = <<HEADER . $diff;
599 if (!defined $diff) {
600 $diff = run_diff('diff -u', $file, $str);
603 if (!defined $diff) {
604 $diff = run_diff('diff', $file, $str);
607 if (!defined $diff) {
608 error("Cannot generate a diff. Please install Text::Diff or use --copy.");
617 my($prog, $file, $str) = @_;
618 my $tmp = 'dppptemp';
623 while (-e "$tmp.$suf") { $suf++ }
626 if (open F, ">$tmp") {
630 if (open F, "$prog $file $tmp |") {
632 s/\Q$tmp\E/$file.patched/;
643 error("Cannot open '$tmp' for writing: $!");
651 my($func, $seen) = @_;
652 return () unless exists $depends{$func};
653 $seen = {%{$seen||{}}};
654 return () if $seen->{$func}++;
656 grep !$s{$_}++, map { ($_, rec_depend($_, $seen)) } @{$depends{$func}};
661 $opt{quiet} and return;
667 $opt{quiet} and return;
668 $opt{diag} and print @_, "\n";
673 $opt{quiet} and return;
674 print "*** ", @_, "\n";
679 print "*** ERROR: ", @_, "\n";
686 $opt{quiet} and return;
689 if (exists $warnings{$func} && !$given_warnings{$func}++) {
690 my $warn = $warnings{$func};
691 $warn =~ s!^!*** !mg;
692 print "*** WARNING: $func\n", $warn;
695 if ($opt{hints} && exists $hints{$func} && !$given_hints{$func}++) {
696 my $hint = $hints{$func};
698 print " --- hint for $func ---\n", $hint;
705 my($usage) = do { local(@ARGV,$/)=($0); <> } =~ /^=head\d$HS+SYNOPSIS\s*^(.*?)\s*^=/ms;
706 my %M = ( 'I' => '*' );
707 $usage =~ s/^\s*perl\s+\S+/$^X $0/;
708 $usage =~ s/([A-Z])<([^>]+)>/$M{$1}$2$M{$1}/g;
714 See perldoc $0 for details.
723 my $self = do { local(@ARGV,$/)=($0); <> };
724 my($copy) = $self =~ /^=head\d\s+COPYRIGHT\s*^(.*?)^=\w+/ms;
725 $copy =~ s/^(?=\S+)/ /gms;
726 $self =~ s/^$HS+Do NOT edit.*?(?=^-)/$copy/ms;
727 $self =~ s/^SKIP.*(?=^__DATA__)/SKIP
728 if (\@ARGV && \$ARGV[0] eq '--unstrip') {
729 eval { require Devel::PPPort };
730 \$@ and die "Cannot require Devel::PPPort, please install.\\n";
731 if (eval \$Devel::PPPort::VERSION < $VERSION) {
732 die "$0 was originally generated with Devel::PPPort $VERSION.\\n"
733 . "Your Devel::PPPort is only version \$Devel::PPPort::VERSION.\\n"
734 . "Please install a newer version, or --unstrip will not work.\\n";
736 Devel::PPPort::WriteFile(\$0);
741 Sorry, but this is a stripped version of \$0.
743 To be able to use its original script and doc functionality,
744 please try to regenerate this file using:
750 my($pl, $c) = $self =~ /(.*^__DATA__)(.*)/ms;
752 / (?: \*[^*]*\*+(?:[^$ccs][^*]*\*+)* / | /[^\r\n]*)
753 | ( "[^"\\]*(?:\\.[^"\\]*)*"
754 | '[^'\\]*(?:\\.[^'\\]*)*' )
755 | ($HS+) }{ defined $2 ? ' ' : ($1 || '') }gsex;
758 $c =~ s!^\s*#\s*!#!mg;
761 open OUT, ">$0" or die "cannot strip $0: $!\n";