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 ################################################################################
15 if ($ENV{'SKIP_SLOW_TESTS'}) {
17 skip("skip: SKIP_SLOW_TESTS", 0);
23 use File::Path qw/rmtree mkpath/;
28 my $isVMS = $^O eq 'VMS';
29 my $isMAC = $^O eq 'MacOS';
30 my $perl = find_perl();
32 rmtree($tmp) if -d $tmp;
33 mkpath($tmp) or die "mkpath $tmp: $!\n";
34 chdir($tmp) or die "chdir $tmp: $!\n";
36 if ($ENV{'PERL_CORE'}) {
39 $inc = '"-I../../lib"';
47 unshift @INC, '../../lib';
50 if ($perl =~ m!^\./!) {
55 chdir('..') if !-d $tmp && -d "../$tmp";
56 rmtree($tmp) if -d $tmp;
59 ok(&Devel::PPPort::WriteFile("ppport.h"));
61 # Check GetFileContents()
66 open(F, "<ppport.h") or die "Failed to open ppport.h: $!";
72 ok(Devel::PPPort::GetFileContents("ppport.h"), $data);
73 ok(Devel::PPPort::GetFileContents(), $data);
79 $c =~ s/^/sprintf("# %2d| ", ++$x)/meg;
80 $c .= "\n" unless $c =~ /[\r\n]$/;
86 my @args = ('ppport.h', @_);
87 unshift @args, $inc if $inc;
88 my $run = $perl =~ m/\s/ ? qq("$perl") : $perl;
89 $run .= ' -MMac::err=unix' if $isMAC;
91 $_ = qq("$_") if $isVMS && /^[^"]/;
94 print "# *** running $run ***\n";
95 $run .= ' 2>&1' unless $isMAC;
97 my $out = join '', @out;
99 return wantarray ? @out : $out;
104 my($str, $re, $mod) = @_;
106 eval "\@n = \$str =~ /$re/g$mod;";
109 $err =~ s/^/# *** /mg;
110 print "# *** ERROR ***\n$err\n";
112 return $@ ? -42 : scalar @n;
118 return 0 unless -e $f1 && -e $f2;
121 print "# File: $_\n";
122 unless (open F, $_) {
123 print "# couldn't open $_: $!\n";
126 $_ = do { local $/; <F> };
135 for (split /\s*={70,}\s*/, do { local $/; <DATA> }) {
138 ($c, @f{m/-{20,}\s+(\S+)\s+-{20,}/g}) = split /\s*-{20,}\s+\S+\s+-{20,}\s*/;
139 push @tests, { code => $c, files => \%f };
144 print "#\n", ('# ', '-'x70, "\n")x3, "#\n";
146 for $f (keys %{$t->{files}}) {
147 my @f = split /\//, $f;
150 my $path = join '/', @f;
151 mkpath($path) or die "mkpath('$path'): $!\n";
153 my $txt = $t->{files}{$f};
155 open F, ">$f" or die "open $f: $!\n";
158 print "# *** writing $f ***\n";
162 print "# *** evaluating test code ***\n";
168 $err =~ s/^/# *** /mg;
169 print "# *** ERROR ***\n$err\n";
173 for (keys %{$t->{files}}) {
174 unlink $_ or die "unlink('$_'): $!\n";
182 return $perl if $isVMS;
184 my $exe = $Config{'_exe'} || '';
186 if ($perl =~ /^perl\Q$exe\E$/i) {
188 eval "require File::Spec";
192 $perl = File::Spec->catfile(File::Spec->curdir(), $perl);
196 if ($perl !~ /\Q$exe\E$/i) {
200 warn "find_perl: cannot find $perl from $^X" unless -f $perl;
207 my $o = ppport(qw(--help));
208 ok($o =~ /^Usage:.*ppport\.h/m);
211 $o = ppport(qw(--version));
212 ok($o =~ /^This is.*ppport.*\d+\.\d+(?:_?\d+)?\.$/);
214 $o = ppport(qw(--nochanges));
215 ok($o =~ /^Scanning.*test\.xs/mi);
216 ok($o =~ /Analyzing.*test\.xs/mi);
217 ok(matches($o, '^Scanning', 'm'), 1);
218 ok(matches($o, 'Analyzing', 'm'), 1);
219 ok($o =~ /Uses Perl_newSViv instead of newSViv/);
221 $o = ppport(qw(--quiet --nochanges));
224 ---------------------------- test.xs ------------------------------------------
228 ===============================================================================
230 # check if C and C++ comments are filtered correctly
232 my $o = ppport(qw(--copy=a));
233 ok($o =~ /^Scanning.*MyExt\.xs/mi);
234 ok($o =~ /Analyzing.*MyExt\.xs/mi);
235 ok(matches($o, '^Scanning', 'm'), 1);
236 ok($o =~ /^Needs to include.*ppport\.h/m);
237 ok($o !~ /^Uses grok_bin/m);
238 ok($o !~ /^Uses newSVpv/m);
239 ok($o =~ /Uses 1 C\+\+ style comment/m);
240 ok(eq_files('MyExt.xsa', 'MyExt.ra'));
242 # check if C++ are left untouched with --cplusplus
244 $o = ppport(qw(--copy=b --cplusplus));
245 ok($o =~ /^Scanning.*MyExt\.xs/mi);
246 ok($o =~ /Analyzing.*MyExt\.xs/mi);
247 ok(matches($o, '^Scanning', 'm'), 1);
248 ok($o =~ /^Needs to include.*ppport\.h/m);
249 ok($o !~ /^Uses grok_bin/m);
250 ok($o !~ /^Uses newSVpv/m);
251 ok($o !~ /Uses \d+ C\+\+ style comment/m);
252 ok(eq_files('MyExt.xsb', 'MyExt.rb'));
254 unlink qw(MyExt.xsa MyExt.xsb);
256 ---------------------------- MyExt.xs -----------------------------------------
263 ---------------------------- MyExt.ra -----------------------------------------
271 ---------------------------- MyExt.rb -----------------------------------------
279 ===============================================================================
281 my $o = ppport(qw(--nochanges file1.xs));
282 ok($o =~ /^Scanning.*file1\.xs/mi);
283 ok($o =~ /Analyzing.*file1\.xs/mi);
284 ok($o !~ /^Scanning.*file2\.xs/mi);
285 ok($o =~ /^Uses newCONSTSUB/m);
286 ok($o =~ /^Uses PL_expect/m);
287 ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_flags/m);
288 ok($o =~ /WARNING: PL_expect/m);
289 ok($o =~ /hint for newCONSTSUB/m);
290 ok($o =~ /^Analysis completed \(1 warning\)/m);
291 ok($o =~ /^Looks good/m);
293 $o = ppport(qw(--nochanges --nohints file1.xs));
294 ok($o =~ /^Scanning.*file1\.xs/mi);
295 ok($o =~ /Analyzing.*file1\.xs/mi);
296 ok($o !~ /^Scanning.*file2\.xs/mi);
297 ok($o =~ /^Uses newCONSTSUB/m);
298 ok($o =~ /^Uses PL_expect/m);
299 ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_flags/m);
300 ok($o =~ /WARNING: PL_expect/m);
301 ok($o !~ /hint for newCONSTSUB/m);
302 ok($o =~ /^Analysis completed \(1 warning\)/m);
303 ok($o =~ /^Looks good/m);
305 $o = ppport(qw(--nochanges --nohints --nodiag file1.xs));
306 ok($o =~ /^Scanning.*file1\.xs/mi);
307 ok($o =~ /Analyzing.*file1\.xs/mi);
308 ok($o !~ /^Scanning.*file2\.xs/mi);
309 ok($o !~ /^Uses newCONSTSUB/m);
310 ok($o !~ /^Uses PL_expect/m);
311 ok($o !~ /^Uses SvPV_nolen/m);
312 ok($o =~ /WARNING: PL_expect/m);
313 ok($o !~ /hint for newCONSTSUB/m);
314 ok($o =~ /^Analysis completed \(1 warning\)/m);
315 ok($o =~ /^Looks good/m);
317 $o = ppport(qw(--nochanges --quiet file1.xs));
320 $o = ppport(qw(--nochanges file2.xs));
321 ok($o =~ /^Scanning.*file2\.xs/mi);
322 ok($o =~ /Analyzing.*file2\.xs/mi);
323 ok($o !~ /^Scanning.*file1\.xs/mi);
324 ok($o =~ /^Uses mXPUSHp/m);
325 ok($o =~ /^Needs to include.*ppport\.h/m);
326 ok($o !~ /^Looks good/m);
327 ok($o =~ /^1 potentially required change detected/m);
329 $o = ppport(qw(--nochanges --nohints file2.xs));
330 ok($o =~ /^Scanning.*file2\.xs/mi);
331 ok($o =~ /Analyzing.*file2\.xs/mi);
332 ok($o !~ /^Scanning.*file1\.xs/mi);
333 ok($o =~ /^Uses mXPUSHp/m);
334 ok($o =~ /^Needs to include.*ppport\.h/m);
335 ok($o !~ /^Looks good/m);
336 ok($o =~ /^1 potentially required change detected/m);
338 $o = ppport(qw(--nochanges --nohints --nodiag file2.xs));
339 ok($o =~ /^Scanning.*file2\.xs/mi);
340 ok($o =~ /Analyzing.*file2\.xs/mi);
341 ok($o !~ /^Scanning.*file1\.xs/mi);
342 ok($o !~ /^Uses mXPUSHp/m);
343 ok($o !~ /^Needs to include.*ppport\.h/m);
344 ok($o !~ /^Looks good/m);
345 ok($o =~ /^1 potentially required change detected/m);
347 $o = ppport(qw(--nochanges --quiet file2.xs));
350 ---------------------------- file1.xs -----------------------------------------
352 #define NEED_newCONSTSUB
353 #define NEED_PL_parser
360 ---------------------------- file2.xs -----------------------------------------
364 ===============================================================================
366 my $o = ppport(qw(--nochanges));
367 ok($o =~ /^Scanning.*FooBar\.xs/mi);
368 ok($o =~ /Analyzing.*FooBar\.xs/mi);
369 ok(matches($o, '^Scanning', 'm'), 1);
370 ok($o !~ /^Looks good/m);
371 ok($o =~ /^Uses grok_bin/m);
373 ---------------------------- FooBar.xs ----------------------------------------
379 ===============================================================================
381 my $o = ppport(qw(--nochanges));
382 ok($o =~ /^Scanning.*First\.xs/mi);
383 ok($o =~ /Analyzing.*First\.xs/mi);
384 ok($o =~ /^Scanning.*second\.h/mi);
385 ok($o =~ /Analyzing.*second\.h/mi);
386 ok($o =~ /^Scanning.*sub.*third\.c/mi);
387 ok($o =~ /Analyzing.*sub.*third\.c/mi);
388 ok($o !~ /^Scanning.*foobar/mi);
389 ok(matches($o, '^Scanning', 'm'), 3);
391 ---------------------------- First.xs -----------------------------------------
395 ---------------------------- foobar.xyz ---------------------------------------
399 ---------------------------- second.h -----------------------------------------
403 ---------------------------- sub/third.c --------------------------------------
407 ===============================================================================
409 my $o = ppport(qw(--nochanges));
410 ok($o =~ /Possibly wrong #define NEED_foobar in.*test.xs/i);
412 ---------------------------- test.xs ------------------------------------------
416 ===============================================================================
418 # And now some complex "real-world" example
420 my $o = ppport(qw(--copy=f));
421 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c mod5.c)) {
422 ok($o =~ /^Scanning.*\Q$_\E/mi);
423 ok($o =~ /Analyzing.*\Q$_\E/i);
425 ok(matches($o, '^Scanning', 'm'), 6);
427 ok(matches($o, '^Writing copy of', 'm'), 5);
430 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c)) {
431 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
433 ok(eq_files("${_}f", "${_}r"));
437 ---------------------------- main.xs ------------------------------------------
443 #define NEED_newCONSTSUB
444 #define NEED_grok_hex_GLOBAL
449 Perl_grok_bin(aTHX_ foo, bar);
455 Perl_grok_bin(bar, sv_no);
457 ---------------------------- mod1.c -------------------------------------------
463 #define NEED_grok_bin_GLOBAL
464 #define NEED_newCONSTSUB
471 Perl_sv_catpvf(); /* I know it's wrong ;-) */
474 ---------------------------- mod2.c -------------------------------------------
489 ---------------------------- mod3.c -------------------------------------------
498 ---------------------------- mod4.c -------------------------------------------
506 ---------------------------- mod5.c -------------------------------------------
515 ---------------------------- main.xsr -----------------------------------------
521 #define NEED_eval_pv_GLOBAL
522 #define NEED_grok_hex
523 #define NEED_newCONSTSUB_GLOBAL
534 grok_bin(bar, PL_sv_no);
536 ---------------------------- mod1.cr ------------------------------------------
542 #define NEED_grok_bin_GLOBAL
548 Perl_croak (aTHX_ "foo");
549 Perl_sv_catpvf(aTHX); /* I know it's wrong ;-) */
552 ---------------------------- mod2.cr ------------------------------------------
565 ---------------------------- mod3.cr ------------------------------------------
570 #define NEED_grok_oct
576 ---------------------------- mod4.cr ------------------------------------------
585 ===============================================================================
587 my $o = ppport(qw(--nochanges));
588 ok($o =~ /Uses grok_hex/m);
589 ok($o !~ /Looks good/m);
591 $o = ppport(qw(--nochanges --compat-version=5.8.0));
592 ok($o !~ /Uses grok_hex/m);
593 ok($o =~ /Looks good/m);
595 ---------------------------- FooBar.xs ----------------------------------------
599 ===============================================================================
601 my $o = ppport(qw(--nochanges));
602 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
604 $o = ppport(qw(--nochanges --compat-version=5.5.3));
605 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
607 $o = ppport(qw(--nochanges --compat-version=5.005_03));
608 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
610 $o = ppport(qw(--nochanges --compat-version=5.6.0));
611 ok($o !~ /Uses SvPVutf8_force/m);
613 $o = ppport(qw(--nochanges --compat-version=5.006));
614 ok($o !~ /Uses SvPVutf8_force/m);
616 $o = ppport(qw(--nochanges --compat-version=5.999.999));
617 ok($o !~ /Uses SvPVutf8_force/m);
619 $o = ppport(qw(--nochanges --compat-version=6.0.0));
620 ok($o =~ /Only Perl 5 is supported/m);
622 $o = ppport(qw(--nochanges --compat-version=5.1000.999));
623 ok($o =~ /Invalid version number: 5.1000.999/m);
625 $o = ppport(qw(--nochanges --compat-version=5.999.1000));
626 ok($o =~ /Invalid version number: 5.999.1000/m);
628 ---------------------------- FooBar.xs ----------------------------------------
632 ===============================================================================
634 my $o = ppport(qw(--nochanges));
635 ok($o !~ /potentially required change/);
636 ok(matches($o, '^Looks good', 'm'), 2);
638 ---------------------------- FooBar.xs ----------------------------------------
640 #define NEED_grok_numeric_radix
641 #define NEED_grok_number
644 GROK_NUMERIC_RADIX();
647 ---------------------------- foo.c --------------------------------------------
653 ===============================================================================
655 # check --api-info option
657 my $o = ppport(qw(--api-info=INT2PTR));
658 my %found = map {($_ => 1)} $o =~ /^===\s+(\w+)\s+===/mg;
659 ok(scalar keys %found, 1, "found 1 key");
660 ok(exists $found{INT2PTR});
661 ok(matches($o, '^Supported at least since perl-5\.6\.0', 'm'), 1, "INT2PTR supported without ppport.h to 5.6.0");
662 ok(matches($o, '^ppport.h additionally provides support at least back to perl-5\.003', 'm'), 1, "INT2PTR supported with ppport.h to 5.003");
664 $o = ppport(qw(--api-info=Zero));
665 %found = map {($_ => 1)} $o =~ /^===\s+(\w+)\s+===/mg;
666 ok(scalar keys %found, 1, "found 1 key");
667 ok(exists $found{Zero});
668 ok(matches($o, '^Supported at least since perl-5.003', 'm'), 1, "Zero supported to 5.003");
670 $o = ppport(qw(--api-info=/Zero/));
671 %found = map {($_ => 1)} $o =~ /^===\s+(\w+)\s+===/mg;
672 ok(scalar keys %found, 2, "found 2 keys");
673 ok(exists $found{Zero});
674 ok(exists $found{ZeroD});
676 ===============================================================================
678 # check --list-provided option
680 my @o = ppport(qw(--list-provided));
684 my($name, $flags) = /^(\w+)(?:\s+\[(\w+(?:,\s+\w+)*)\])?$/ or $fail++;
685 exists $p{$name} and $fail++;
686 $p{$name} = defined $flags ? { map { ($_ => 1) } $flags =~ /(\w+)/g } : '';
691 ok(exists $p{call_pv});
692 ok(not ref $p{call_pv});
694 ok(exists $p{grok_bin});
695 ok(ref $p{grok_bin}, 'HASH');
696 ok(scalar keys %{$p{grok_bin}}, 2);
697 ok($p{grok_bin}{explicit});
698 ok($p{grok_bin}{depend});
700 ok(exists $p{gv_stashpvn});
701 ok(ref $p{gv_stashpvn}, 'HASH');
702 ok(scalar keys %{$p{gv_stashpvn}}, 2);
703 ok($p{gv_stashpvn}{depend});
704 ok($p{gv_stashpvn}{hint});
706 ok(exists $p{sv_catpvf_mg});
707 ok(ref $p{sv_catpvf_mg}, 'HASH');
708 ok(scalar keys %{$p{sv_catpvf_mg}}, 2);
709 ok($p{sv_catpvf_mg}{explicit});
710 ok($p{sv_catpvf_mg}{depend});
712 ok(exists $p{PL_signals});
713 ok(ref $p{PL_signals}, 'HASH');
714 ok(scalar keys %{$p{PL_signals}}, 1);
715 ok($p{PL_signals}{explicit});
717 ===============================================================================
719 # check --list-unsupported option
721 my @o = ppport(qw(--list-unsupported));
725 my($name, $ver) = /^(\w+)\s*\.+\s*([\d._]+)$/ or $fail++;
726 exists $p{$name} and $fail++;
732 ok(exists $p{utf8_distance});
733 ok($p{utf8_distance}, '5.6.0');
735 ok(exists $p{save_generic_svref});
736 ok($p{save_generic_svref}, '5.005_03');
738 ===============================================================================
740 # check --nofilter option
742 my $o = ppport(qw(--nochanges));
743 ok($o =~ /^Scanning.*foo\.cpp/mi);
744 ok($o =~ /Analyzing.*foo\.cpp/mi);
745 ok(matches($o, '^Scanning', 'm'), 1);
746 ok(matches($o, 'Analyzing', 'm'), 1);
748 $o = ppport(qw(--nochanges foo.cpp foo.o Makefile.PL));
749 ok($o =~ /Skipping the following files \(use --nofilter to avoid this\):/m);
750 ok(matches($o, '^\|\s+foo\.o', 'mi'), 1);
751 ok(matches($o, '^\|\s+Makefile\.PL', 'mi'), 1);
752 ok($o =~ /^Scanning.*foo\.cpp/mi);
753 ok($o =~ /Analyzing.*foo\.cpp/mi);
754 ok(matches($o, '^Scanning', 'm'), 1);
755 ok(matches($o, 'Analyzing', 'm'), 1);
757 $o = ppport(qw(--nochanges --nofilter foo.cpp foo.o Makefile.PL));
758 ok($o =~ /^Scanning.*foo\.cpp/mi);
759 ok($o =~ /Analyzing.*foo\.cpp/mi);
760 ok($o =~ /^Scanning.*foo\.o/mi);
761 ok($o =~ /Analyzing.*foo\.o/mi);
762 ok($o =~ /^Scanning.*Makefile/mi);
763 ok($o =~ /Analyzing.*Makefile/mi);
764 ok(matches($o, '^Scanning', 'm'), 3);
765 ok(matches($o, 'Analyzing', 'm'), 3);
767 ---------------------------- foo.cpp ------------------------------------------
771 ---------------------------- foo.o --------------------------------------------
775 ---------------------------- Makefile.PL --------------------------------------
779 ===============================================================================
781 # check if explicit variables are handled propery
783 my $o = ppport(qw(--copy=a));
784 ok($o =~ /^Needs to include.*ppport\.h/m);
785 ok($o =~ /^Uses PL_signals/m);
786 ok($o =~ /^File needs PL_signals, adding static request/m);
787 ok(eq_files('MyExt.xsa', 'MyExt.ra'));
789 unlink qw(MyExt.xsa);
791 ---------------------------- MyExt.xs -----------------------------------------
794 if (PL_signals == 42)
797 ---------------------------- MyExt.ra -----------------------------------------
799 #define NEED_PL_signals
802 if (PL_signals == 42)
805 ===============================================================================
807 my $o = ppport(qw(--nochanges file.xs));
808 ok($o =~ /^Uses PL_copline/m);
809 ok($o =~ /WARNING: PL_copline/m);
810 ok($o =~ /^Uses SvUOK/m);
811 ok($o =~ /WARNING: Uses SvUOK, which may not be portable/m);
812 ok($o =~ /^Analysis completed \(2 warnings\)/m);
813 ok($o =~ /^Looks good/m);
815 $o = ppport(qw(--nochanges --compat-version=5.8.0 file.xs));
816 ok($o =~ /^Uses PL_copline/m);
817 ok($o =~ /WARNING: PL_copline/m);
818 ok($o !~ /WARNING: Uses SvUOK, which may not be portable/m);
819 ok($o =~ /^Analysis completed \(1 warning\)/m);
820 ok($o =~ /^Looks good/m);
822 ---------------------------- file.xs -----------------------------------------
824 #define NEED_PL_parser
829 ===============================================================================
831 my $o = ppport(qw(--copy=f));
834 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
836 ok(eq_files("${_}f", "${_}r"));
840 ---------------------------- file.xs -----------------------------------------
842 a_string = "sv_undef"
844 #define SOMETHING defgv
845 /* C-comment: sv_tainted */
847 # This is just a big XS comment using sv_no
849 /* The following, is NOT an XS comment! */
850 # define SOMETHING_ELSE defgv + \
853 ---------------------------- file.xsr -----------------------------------------
856 a_string = "sv_undef"
858 #define SOMETHING PL_defgv
859 /* C-comment: sv_tainted */
861 # This is just a big XS comment using sv_no
863 /* The following, is NOT an XS comment! */
864 # define SOMETHING_ELSE PL_defgv + \
867 ===============================================================================
869 my $o = ppport(qw(--copy=f));
872 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
874 ok(eq_files("${_}f", "${_}r"));
878 ---------------------------- file.xs -----------------------------------------
882 Perl_croak_nocontext("foo");
885 croak_nocontext("foo");
886 Perl_warner_nocontext("foo");
888 warner_nocontext("foo");
891 ---------------------------- file.xsr -----------------------------------------
895 Perl_croak_nocontext("foo");
896 Perl_croak(aTHX_ "bar");
898 croak_nocontext("foo");
899 Perl_warner_nocontext("foo");
900 Perl_warner(aTHX_ "foo");
901 warner_nocontext("foo");