This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add new mpeix file from #26807
[perl5.git] / ext / Devel / PPPort / parts / inc / ppphbin
CommitLineData
adfe19db
MHM
1################################################################################
2##
9132e1a3 3## $Revision: 26 $
adfe19db 4## $Author: mhx $
9132e1a3 5## $Date: 2005/01/31 08:10:53 +0100 $
adfe19db
MHM
6##
7################################################################################
8##
9132e1a3 9## Version 3.x, Copyright (C) 2004-2005, Marcus Holland-Moritz.
adfe19db
MHM
10## Version 2.x, Copyright (C) 2001, Paul Marquess.
11## Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
12##
13## This program is free software; you can redistribute it and/or
14## modify it under the same terms as Perl itself.
15##
16################################################################################
17
18=provides
19
20=implementation
21
22=cut
23
24use strict;
25
26my %opt = (
27 quiet => 0,
28 diag => 1,
29 hints => 1,
30 changes => 1,
31 cplusplus => 0,
32);
33
34my($ppport) = $0 =~ /([\w.]+)$/;
35my $LF = '(?:\r\n|[\r\n])'; # line feed
36my $HS = "[ \t]"; # horizontal whitespace
37
38eval {
39 require Getopt::Long;
40 Getopt::Long::GetOptions(\%opt, qw(
41 help quiet diag! hints! changes! cplusplus
42 patch=s copy=s diff=s compat-version=s
04fc8b94 43 list-provided list-unsupported api-info=s
adfe19db
MHM
44 )) or usage();
45};
46
47if ($@ and grep /^-/, @ARGV) {
48 usage() if "@ARGV" =~ /^--?h(?:elp)?$/;
49 die "Getopt::Long not found. Please don't use any options.\n";
50}
51
52usage() if $opt{help};
53
54if (exists $opt{'compat-version'}) {
55 my($r,$v,$s) = eval { parse_version($opt{'compat-version'}) };
56 if ($@) {
57 die "Invalid version number format: '$opt{'compat-version'}'\n";
58 }
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;
62}
63else {
64 $opt{'compat-version'} = 5;
65}
66
67# Never use C comments in this file!!!!!
68my $ccs = '/'.'*';
69my $cce = '*'.'/';
70my $rccs = quotemeta $ccs;
71my $rcce = quotemeta $cce;
72
73my @files;
74
75if (@ARGV) {
76 @files = map { glob $_ } @ARGV;
77}
78else {
79 eval {
80 require File::Find;
81 File::Find::find(sub {
82 $File::Find::name =~ /\.(xs|c|h|cc)$/i
83 and push @files, $File::Find::name;
84 }, '.');
85 };
86 if ($@) {
87 @files = map { glob $_ } qw(*.xs *.c *.h *.cc);
88 }
89 my %filter = map { /(.*)\.xs$/ ? ("$1.c" => 1) : () } @files;
90 @files = grep { !/\b\Q$ppport\E$/i && !exists $filter{$_} } @files;
91}
92
93unless (@files) {
94 die "No input files given!\n";
95}
96
97my %API = map { /^(\w+)\|([^|]*)\|([^|]*)\|(\w*)$/
98 ? ( $1 => {
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 ) : ()),
104 } )
105 : die "invalid spec: $_" } qw(
106__PERL_API__
107);
108
109if (exists $opt{'list-unsupported'}) {
110 my $f;
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";
114 }
115 exit 0;
116}
117
118# Scan for possible replacement candidates
119
120my(%replace, %need, %hints, %depends);
121my $replace = 0;
122my $hint = '';
123
124while (<DATA>) {
125 if ($hint) {
126 if (m{^\s*\*\s(.*?)\s*$}) {
127 $hints{$hint} ||= ''; # suppress warning with older perls
128 $hints{$hint} .= "$1\n";
129 }
130 else {
131 $hint = '';
132 }
133 }
134 $hint = $1 if m{^\s*$rccs\sHint:\s+(\w+)\s*$};
135
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*$};
140
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;
143 }
144
145 $need{$1} = 1 if m{^#if\s+defined\(NEED_(\w+)(?:_GLOBAL)?\)};
146}
147
04fc8b94
MHM
148if (exists $opt{'api-info'}) {
149 my $f;
150 my $count = 0;
9132e1a3 151 my $match = $opt{'api-info'} =~ m!^/(.*)/$! ? $1 : "^\Q$opt{'api-info'}\E\$";
04fc8b94 152 for $f (sort { lc $a cmp lc $b } keys %API) {
9132e1a3 153 next unless $f =~ /$match/;
04fc8b94
MHM
154 print "\n=== $f ===\n\n";
155 my $info = 0;
156 if ($API{$f}{base} || $API{$f}{todo}) {
157 my $base = format_version($API{$f}{base} || $API{$f}{todo});
9132e1a3 158 print "Supported at least starting from perl-$base.\n";
04fc8b94
MHM
159 $info++;
160 }
161 if ($API{$f}{provided}) {
162 my $todo = $API{$f}{todo} ? format_version($API{$f}{todo}) : "__MIN_PERL__";
9132e1a3 163 print "Support by $ppport provided back to perl-$todo.\n";
04fc8b94
MHM
164 print "Support needs to be explicitly requested by NEED_$f.\n" if exists $need{$f};
165 print "Depends on: ", join(', ', @{$depends{$f}}), ".\n" if exists $depends{$f};
166 print "$hints{$f}" if exists $hints{$f};
167 $info++;
168 }
169 unless ($info) {
170 print "No portability information available.\n";
171 }
172 $count++;
173 }
174 if ($count > 0) {
175 print "\n";
176 }
177 else {
9132e1a3 178 print "Found no API matching '$opt{'api-info'}'.\n";
04fc8b94
MHM
179 }
180 exit 0;
181}
182
adfe19db
MHM
183if (exists $opt{'list-provided'}) {
184 my $f;
185 for $f (sort { lc $a cmp lc $b } keys %API) {
186 next unless $API{$f}{provided};
187 my @flags;
188 push @flags, 'explicit' if exists $need{$f};
189 push @flags, 'depend' if exists $depends{$f};
190 push @flags, 'hint' if exists $hints{$f};
191 my $flags = @flags ? ' ['.join(', ', @flags).']' : '';
192 print "$f$flags\n";
193 }
194 exit 0;
195}
196
197my(%files, %global, %revreplace);
198%revreplace = reverse %replace;
199my $filename;
200my $patch_opened = 0;
201
202for $filename (@files) {
203 unless (open IN, "<$filename") {
204 warn "Unable to read from $filename: $!\n";
205 next;
206 }
207
208 info("Scanning $filename ...");
209
210 my $c = do { local $/; <IN> };
211 close IN;
212
213 my %file = (orig => $c, changes => 0);
214
215 # temporarily remove C comments from the code
216 my @ccom;
217 $c =~ s{
218 (
219 [^"'/]+
220 |
221 (?:"[^"\\]*(?:\\.[^"\\]*)*" [^"'/]*)+
222 |
223 (?:'[^'\\]*(?:\\.[^'\\]*)*' [^"'/]*)+
224 )
225 |
226 (/ (?:
227 \*[^*]*\*+(?:[^$ccs][^*]*\*+)* /
228 |
229 /[^\r\n]*
230 ))
231 }{
232 defined $2 and push @ccom, $2;
233 defined $1 ? $1 : "$ccs$#ccom$cce";
234 }egsx;
235
236 $file{ccom} = \@ccom;
237 $file{code} = $c;
238 $file{has_inc_ppport} = ($c =~ /#.*include.*\Q$ppport\E/);
239
240 my $func;
241
242 for $func (keys %API) {
243 my $match = $func;
244 $match .= "|$revreplace{$func}" if exists $revreplace{$func};
245 if ($c =~ /\b(?:Perl_)?($match)\b/) {
246 $file{uses_replace}{$1}++ if exists $revreplace{$func} && $1 eq $revreplace{$func};
247 $file{uses_Perl}{$func}++ if $c =~ /\bPerl_$func\b/;
248 if (exists $API{$func}{provided}) {
249 if (!exists $API{$func}{base} || $API{$func}{base} > $opt{'compat-version'}) {
250 $file{uses}{$func}++;
adfe19db
MHM
251 my @deps = rec_depend($func);
252 if (@deps) {
253 $file{uses_deps}{$func} = \@deps;
254 for (@deps) {
255 $file{uses}{$_} = 0 unless exists $file{uses}{$_};
adfe19db
MHM
256 }
257 }
258 for ($func, @deps) {
259 if (exists $need{$_}) {
260 $file{needs}{$_} = 'static';
adfe19db
MHM
261 }
262 }
263 }
264 }
265 if (exists $API{$func}{todo} && $API{$func}{todo} > $opt{'compat-version'}) {
266 if ($c =~ /\b$func\b/) {
267 $file{uses_todo}{$func}++;
adfe19db
MHM
268 }
269 }
270 }
271 }
272
273 while ($c =~ /^$HS*#$HS*define$HS+(NEED_(\w+?)(_GLOBAL)?)\b/mg) {
274 if (exists $need{$2}) {
275 $file{defined $3 ? 'needed_global' : 'needed_static'}{$2}++;
adfe19db
MHM
276 }
277 else {
278 warning("Possibly wrong #define $1 in $filename");
279 }
280 }
281
96ad942f
MHM
282 for (qw(uses needs uses_todo needed_global needed_static)) {
283 for $func (keys %{$file{$_}}) {
284 push @{$global{$_}{$func}}, $filename;
285 }
286 }
287
adfe19db
MHM
288 $files{$filename} = \%file;
289}
290
291# Globally resolve NEED_'s
292my $need;
293for $need (keys %{$global{needs}}) {
294 if (@{$global{needs}{$need}} > 1) {
295 my @targets = @{$global{needs}{$need}};
296 my @t = grep $files{$_}{needed_global}{$need}, @targets;
297 @targets = @t if @t;
298 @t = grep /\.xs$/i, @targets;
299 @targets = @t if @t;
300 my $target = shift @targets;
301 $files{$target}{needs}{$need} = 'global';
302 for (@{$global{needs}{$need}}) {
303 $files{$_}{needs}{$need} = 'extern' if $_ ne $target;
304 }
305 }
306}
307
308for $filename (@files) {
309 exists $files{$filename} or next;
310
311 info("=== Analyzing $filename ===");
312
313 my %file = %{$files{$filename}};
314 my $func;
315 my $c = $file{code};
316
317 for $func (sort keys %{$file{uses_Perl}}) {
318 if ($API{$func}{varargs}) {
319 my $changes = ($c =~ s{\b(Perl_$func\s*\(\s*)(?!aTHX_?)(\)|[^\s)]*\))}
320 { $1 . ($2 eq ')' ? 'aTHX' : 'aTHX_ ') . $2 }ge);
321 if ($changes) {
322 warning("Doesn't pass interpreter argument aTHX to Perl_$func");
323 $file{changes} += $changes;
324 }
325 }
326 else {
327 warning("Uses Perl_$func instead of $func");
328 $file{changes} += ($c =~ s{\bPerl_$func(\s*)\((\s*aTHX_?)?\s*}
329 {$func$1(}g);
330 }
331 }
332
333 for $func (sort keys %{$file{uses_replace}}) {
334 warning("Uses $func instead of $replace{$func}");
335 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
336 }
337
338 for $func (sort keys %{$file{uses}}) {
339 next unless $file{uses}{$func}; # if it's only a dependency
340 if (exists $file{uses_deps}{$func}) {
341 diag("Uses $func, which depends on ", join(', ', @{$file{uses_deps}{$func}}));
342 }
343 elsif (exists $replace{$func}) {
344 warning("Uses $func instead of $replace{$func}");
345 $file{changes} += ($c =~ s/\b$func\b/$replace{$func}/g);
346 }
347 else {
348 diag("Uses $func");
349 }
350 hint($func);
351 }
352
353 for $func (sort keys %{$file{uses_todo}}) {
354 warning("Uses $func, which may not be portable below perl ",
355 format_version($API{$func}{todo}));
356 }
357
358 for $func (sort keys %{$file{needed_static}}) {
359 my $message = '';
360 if (not exists $file{uses}{$func}) {
361 $message = "No need to define NEED_$func if $func is never used";
362 }
363 elsif (exists $file{needs}{$func} && $file{needs}{$func} ne 'static') {
364 $message = "No need to define NEED_$func when already needed globally";
365 }
366 if ($message) {
367 diag($message);
368 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_$func\b.*$LF//mg);
369 }
370 }
371
372 for $func (sort keys %{$file{needed_global}}) {
373 my $message = '';
374 if (not exists $global{uses}{$func}) {
375 $message = "No need to define NEED_${func}_GLOBAL if $func is never used";
376 }
377 elsif (exists $file{needs}{$func}) {
378 if ($file{needs}{$func} eq 'extern') {
379 $message = "No need to define NEED_${func}_GLOBAL when already needed globally";
380 }
381 elsif ($file{needs}{$func} eq 'static') {
382 $message = "No need to define NEED_${func}_GLOBAL when only used in this file";
383 }
384 }
385 if ($message) {
386 diag($message);
387 $file{changes} += ($c =~ s/^$HS*#$HS*define$HS+NEED_${func}_GLOBAL\b.*$LF//mg);
388 }
389 }
390
391 $file{needs_inc_ppport} = keys %{$file{uses}};
392
393 if ($file{needs_inc_ppport}) {
394 my $pp = '';
395
396 for $func (sort keys %{$file{needs}}) {
397 my $type = $file{needs}{$func};
398 next if $type eq 'extern';
399 my $suffix = $type eq 'global' ? '_GLOBAL' : '';
400 unless (exists $file{"needed_$type"}{$func}) {
401 if ($type eq 'global') {
402 diag("Files [@{$global{needs}{$func}}] need $func, adding global request");
403 }
404 else {
405 diag("File needs $func, adding static request");
406 }
407 $pp .= "#define NEED_$func$suffix\n";
408 }
409 }
410
411 if ($pp && ($c =~ s/^(?=$HS*#$HS*define$HS+NEED_\w+)/$pp/m)) {
412 $pp = '';
413 $file{changes}++;
414 }
415
416 unless ($file{has_inc_ppport}) {
417 diag("Needs to include '$ppport'");
418 $pp .= qq(#include "$ppport"\n)
419 }
420
421 if ($pp) {
422 $file{changes} += ($c =~ s/^($HS*#$HS*define$HS+NEED_\w+.*?)^/$1$pp/ms)
423 || ($c =~ s/^(?=$HS*#$HS*include.*\Q$ppport\E)/$pp/m)
424 || ($c =~ s/^($HS*#$HS*include.*XSUB.*\s*?)^/$1$pp/m)
425 || ($c =~ s/^/$pp/);
426 }
427 }
428 else {
429 if ($file{has_inc_ppport}) {
430 diag("No need to include '$ppport'");
431 $file{changes} += ($c =~ s/^$HS*?#$HS*include.*\Q$ppport\E.*?$LF//m);
432 }
433 }
434
435 # put back in our C comments
436 my $ix;
437 my $cppc = 0;
438 my @ccom = @{$file{ccom}};
439 for $ix (0 .. $#ccom) {
440 if (!$opt{cplusplus} && $ccom[$ix] =~ s!^//!!) {
441 $cppc++;
442 $file{changes} += $c =~ s/$rccs$ix$rcce/$ccs$ccom[$ix] $cce/;
443 }
444 else {
445 $c =~ s/$rccs$ix$rcce/$ccom[$ix]/;
446 }
447 }
448
449 if ($cppc) {
450 my $s = $cppc != 1 ? 's' : '';
451 warning("Uses $cppc C++ style comment$s, which is not portable");
452 }
453
454 if ($file{changes}) {
455 if (exists $opt{copy}) {
456 my $newfile = "$filename$opt{copy}";
457 if (-e $newfile) {
458 error("'$newfile' already exists, refusing to write copy of '$filename'");
459 }
460 else {
461 local *F;
462 if (open F, ">$newfile") {
463 info("Writing copy of '$filename' with changes to '$newfile'");
464 print F $c;
465 close F;
466 }
467 else {
468 error("Cannot open '$newfile' for writing: $!");
469 }
470 }
471 }
472 elsif (exists $opt{patch} || $opt{changes}) {
473 if (exists $opt{patch}) {
474 unless ($patch_opened) {
475 if (open PATCH, ">$opt{patch}") {
476 $patch_opened = 1;
477 }
478 else {
479 error("Cannot open '$opt{patch}' for writing: $!");
480 delete $opt{patch};
481 $opt{changes} = 1;
482 goto fallback;
483 }
484 }
485 mydiff(\*PATCH, $filename, $c);
486 }
487 else {
488fallback:
489 info("Suggested changes:");
490 mydiff(\*STDOUT, $filename, $c);
491 }
492 }
493 else {
494 my $s = $file{changes} == 1 ? '' : 's';
495 info("$file{changes} potentially required change$s detected");
496 }
497 }
498 else {
499 info("Looks good");
500 }
501}
502
503close PATCH if $patch_opened;
504
505exit 0;
506
507#######################################################################
508
509sub mydiff
510{
511 local *F = shift;
512 my($file, $str) = @_;
513 my $diff;
514
515 if (exists $opt{diff}) {
516 $diff = run_diff($opt{diff}, $file, $str);
517 }
518
519 if (!defined $diff and can_use('Text::Diff')) {
520 $diff = Text::Diff::diff($file, \$str, { STYLE => 'Unified' });
521 $diff = <<HEADER . $diff;
522--- $file
523+++ $file.patched
524HEADER
525 }
526
527 if (!defined $diff) {
528 $diff = run_diff('diff -u', $file, $str);
529 }
530
531 if (!defined $diff) {
532 $diff = run_diff('diff', $file, $str);
533 }
534
535 if (!defined $diff) {
536 error("Cannot generate a diff. Please install Text::Diff or use --copy.");
537 return;
538 }
539
540 print F $diff;
541
542}
543
544sub run_diff
545{
546 my($prog, $file, $str) = @_;
547 my $tmp = 'dppptemp';
548 my $suf = 'aaa';
549 my $diff = '';
550 local *F;
551
552 while (-e "$tmp.$suf") { $suf++ }
553 $tmp = "$tmp.$suf";
554
555 if (open F, ">$tmp") {
556 print F $str;
557 close F;
558
559 if (open F, "$prog $file $tmp |") {
560 while (<F>) {
561 s/\Q$tmp\E/$file.patched/;
562 $diff .= $_;
563 }
564 close F;
565 unlink $tmp;
566 return $diff;
567 }
568
569 unlink $tmp;
570 }
571 else {
572 error("Cannot open '$tmp' for writing: $!");
573 }
574
575 return undef;
576}
577
578sub can_use
579{
580 eval "use @_;";
581 return $@ eq '';
582}
583
584sub rec_depend
585{
586 my $func = shift;
96ad942f 587 my %seen;
adfe19db 588 return () unless exists $depends{$func};
96ad942f 589 grep !$seen{$_}++, map { ($_, rec_depend($_)) } @{$depends{$func}};
adfe19db
MHM
590}
591
592sub parse_version
593{
594 my $ver = shift;
595
596 if ($ver =~ /^(\d+)\.(\d+)\.(\d+)$/) {
597 return ($1, $2, $3);
598 }
599 elsif ($ver !~ /^\d+\.[\d_]+$/) {
600 die "cannot parse version '$ver'\n";
601 }
602
603 $ver =~ s/_//g;
604 $ver =~ s/$/000000/;
605
606 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
607
608 $v = int $v;
609 $s = int $s;
610
611 if ($r < 5 || ($r == 5 && $v < 6)) {
612 if ($s % 10) {
613 die "cannot parse version '$ver'\n";
614 }
615 }
616
617 return ($r, $v, $s);
618}
619
620sub format_version
621{
622 my $ver = shift;
623
624 $ver =~ s/$/000000/;
625 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
626
627 $v = int $v;
628 $s = int $s;
629
630 if ($r < 5 || ($r == 5 && $v < 6)) {
631 if ($s % 10) {
632 die "invalid version '$ver'\n";
633 }
634 $s /= 10;
635
636 $ver = sprintf "%d.%03d", $r, $v;
637 $s > 0 and $ver .= sprintf "_%02d", $s;
638
639 return $ver;
640 }
641
642 return sprintf "%d.%d.%d", $r, $v, $s;
643}
644
645sub info
646{
647 $opt{quiet} and return;
648 print @_, "\n";
649}
650
651sub diag
652{
653 $opt{quiet} and return;
654 $opt{diag} and print @_, "\n";
655}
656
657sub warning
658{
659 $opt{quiet} and return;
660 print "*** ", @_, "\n";
661}
662
663sub error
664{
665 print "*** ERROR: ", @_, "\n";
666}
667
668my %given_hints;
669sub hint
670{
671 $opt{quiet} and return;
672 $opt{hints} or return;
673 my $func = shift;
674 exists $hints{$func} or return;
675 $given_hints{$func}++ and return;
676 my $hint = $hints{$func};
677 $hint =~ s/^/ /mg;
678 print " --- hint for $func ---\n", $hint;
679}
680
681sub usage
682{
683 my($usage) = do { local(@ARGV,$/)=($0); <> } =~ /^=head\d$HS+SYNOPSIS\s*^(.*?)\s*^=/ms;
684 my %M = ( 'I' => '*' );
685 $usage =~ s/^\s*perl\s+\S+/$^X $0/;
686 $usage =~ s/([A-Z])<([^>]+)>/$M{$1}$2$M{$1}/g;
687
688 print <<ENDUSAGE;
689
690Usage: $usage
691
692See perldoc $0 for details.
693
694ENDUSAGE
695
696 exit 2;
697}