1 ################################################################################
5 ## $Date: 2010/03/07 13:15:46 +0100 $
7 ################################################################################
9 ## Version 3.x, Copyright (C) 2004-2010, 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 ################################################################################
21 if ($ENV{'SKIP_SLOW_TESTS'}) {
23 skip("skip: SKIP_SLOW_TESTS", 0);
29 use File::Path qw/rmtree mkpath/;
34 my $isVMS = $^O eq 'VMS';
35 my $isMAC = $^O eq 'MacOS';
36 my $perl = find_perl();
38 rmtree($tmp) if -d $tmp;
39 mkpath($tmp) or die "mkpath $tmp: $!\n";
40 chdir($tmp) or die "chdir $tmp: $!\n";
42 if ($ENV{'PERL_CORE'}) {
45 $inc = '"-I../../lib"';
53 unshift @INC, '../../lib';
56 if ($perl =~ m!^\./!) {
61 chdir('..') if !-d $tmp && -d "../$tmp";
62 rmtree($tmp) if -d $tmp;
65 ok(&Devel::PPPort::WriteFile("ppport.h"));
71 $c .= "\n" unless $c =~ /[\r\n]$/;
77 my @args = ('ppport.h', @_);
78 unshift @args, $inc if $inc;
79 my $run = $perl =~ m/\s/ ? qq("$perl") : $perl;
80 $run .= ' -MMac::err=unix' if $isMAC;
82 $_ = qq("$_") if $isVMS && /^[^"]/;
85 print "# *** running $run ***\n";
86 $run .= ' 2>&1' unless $isMAC;
88 my $out = join '', @out;
90 return wantarray ? @out : $out;
95 my($str, $re, $mod) = @_;
97 eval "\@n = \$str =~ /$re/g$mod;";
100 $err =~ s/^/# *** /mg;
101 print "# *** ERROR ***\n$err\n";
103 return $@ ? -42 : scalar @n;
109 return 0 unless -e $f1 && -e $f2;
112 print "# File: $_\n";
113 unless (open F, $_) {
114 print "# couldn't open $_: $!\n";
117 $_ = do { local $/; <F> };
126 for (split /\s*={70,}\s*/, do { local $/; <DATA> }) {
129 ($c, @f{m/-{20,}\s+(\S+)\s+-{20,}/g}) = split /\s*-{20,}\s+\S+\s+-{20,}\s*/;
130 push @tests, { code => $c, files => \%f };
135 print "#\n", ('# ', '-'x70, "\n")x3, "#\n";
137 for $f (keys %{$t->{files}}) {
138 my @f = split /\//, $f;
141 my $path = join '/', @f;
142 mkpath($path) or die "mkpath('$path'): $!\n";
144 my $txt = $t->{files}{$f};
146 open F, ">$f" or die "open $f: $!\n";
150 print "# *** writing $f ***\n$txt\n";
153 my $code = $t->{code};
154 $code =~ s/^/# | /mg;
156 print "# *** evaluating test code ***\n$code\n";
161 $err =~ s/^/# *** /mg;
162 print "# *** ERROR ***\n$err\n";
166 for (keys %{$t->{files}}) {
167 unlink $_ or die "unlink('$_'): $!\n";
175 return $perl if $isVMS;
177 my $exe = $Config{'_exe'} || '';
179 if ($perl =~ /^perl\Q$exe\E$/i) {
181 eval "require File::Spec";
185 $perl = File::Spec->catfile(File::Spec->curdir(), $perl);
189 if ($perl !~ /\Q$exe\E$/i) {
193 warn "find_perl: cannot find $perl from $^X" unless -f $perl;
200 my $o = ppport(qw(--help));
201 ok($o =~ /^Usage:.*ppport\.h/m);
204 $o = ppport(qw(--version));
205 ok($o =~ /^This is.*ppport.*\d+\.\d+(?:_?\d+)?\.$/);
207 $o = ppport(qw(--nochanges));
208 ok($o =~ /^Scanning.*test\.xs/mi);
209 ok($o =~ /Analyzing.*test\.xs/mi);
210 ok(matches($o, '^Scanning', 'm'), 1);
211 ok(matches($o, 'Analyzing', 'm'), 1);
212 ok($o =~ /Uses Perl_newSViv instead of newSViv/);
214 $o = ppport(qw(--quiet --nochanges));
217 ---------------------------- test.xs ------------------------------------------
221 ===============================================================================
223 # check if C and C++ comments are filtered correctly
225 my $o = ppport(qw(--copy=a));
226 ok($o =~ /^Scanning.*MyExt\.xs/mi);
227 ok($o =~ /Analyzing.*MyExt\.xs/mi);
228 ok(matches($o, '^Scanning', 'm'), 1);
229 ok($o =~ /^Needs to include.*ppport\.h/m);
230 ok($o !~ /^Uses grok_bin/m);
231 ok($o !~ /^Uses newSVpv/m);
232 ok($o =~ /Uses 1 C\+\+ style comment/m);
233 ok(eq_files('MyExt.xsa', 'MyExt.ra'));
235 # check if C++ are left untouched with --cplusplus
237 $o = ppport(qw(--copy=b --cplusplus));
238 ok($o =~ /^Scanning.*MyExt\.xs/mi);
239 ok($o =~ /Analyzing.*MyExt\.xs/mi);
240 ok(matches($o, '^Scanning', 'm'), 1);
241 ok($o =~ /^Needs to include.*ppport\.h/m);
242 ok($o !~ /^Uses grok_bin/m);
243 ok($o !~ /^Uses newSVpv/m);
244 ok($o !~ /Uses \d+ C\+\+ style comment/m);
245 ok(eq_files('MyExt.xsb', 'MyExt.rb'));
247 unlink qw(MyExt.xsa MyExt.xsb);
249 ---------------------------- MyExt.xs -----------------------------------------
256 ---------------------------- MyExt.ra -----------------------------------------
264 ---------------------------- MyExt.rb -----------------------------------------
272 ===============================================================================
274 my $o = ppport(qw(--nochanges file1.xs));
275 ok($o =~ /^Scanning.*file1\.xs/mi);
276 ok($o =~ /Analyzing.*file1\.xs/mi);
277 ok($o !~ /^Scanning.*file2\.xs/mi);
278 ok($o =~ /^Uses newCONSTSUB/m);
279 ok($o =~ /^Uses PL_expect/m);
280 ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_flags/m);
281 ok($o =~ /WARNING: PL_expect/m);
282 ok($o =~ /hint for newCONSTSUB/m);
283 ok($o =~ /^Analysis completed \(1 warning\)/m);
284 ok($o =~ /^Looks good/m);
286 $o = ppport(qw(--nochanges --nohints file1.xs));
287 ok($o =~ /^Scanning.*file1\.xs/mi);
288 ok($o =~ /Analyzing.*file1\.xs/mi);
289 ok($o !~ /^Scanning.*file2\.xs/mi);
290 ok($o =~ /^Uses newCONSTSUB/m);
291 ok($o =~ /^Uses PL_expect/m);
292 ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_flags/m);
293 ok($o =~ /WARNING: PL_expect/m);
294 ok($o !~ /hint for newCONSTSUB/m);
295 ok($o =~ /^Analysis completed \(1 warning\)/m);
296 ok($o =~ /^Looks good/m);
298 $o = ppport(qw(--nochanges --nohints --nodiag file1.xs));
299 ok($o =~ /^Scanning.*file1\.xs/mi);
300 ok($o =~ /Analyzing.*file1\.xs/mi);
301 ok($o !~ /^Scanning.*file2\.xs/mi);
302 ok($o !~ /^Uses newCONSTSUB/m);
303 ok($o !~ /^Uses PL_expect/m);
304 ok($o !~ /^Uses SvPV_nolen/m);
305 ok($o =~ /WARNING: PL_expect/m);
306 ok($o !~ /hint for newCONSTSUB/m);
307 ok($o =~ /^Analysis completed \(1 warning\)/m);
308 ok($o =~ /^Looks good/m);
310 $o = ppport(qw(--nochanges --quiet file1.xs));
313 $o = ppport(qw(--nochanges file2.xs));
314 ok($o =~ /^Scanning.*file2\.xs/mi);
315 ok($o =~ /Analyzing.*file2\.xs/mi);
316 ok($o !~ /^Scanning.*file1\.xs/mi);
317 ok($o =~ /^Uses mXPUSHp/m);
318 ok($o =~ /^Needs to include.*ppport\.h/m);
319 ok($o !~ /^Looks good/m);
320 ok($o =~ /^1 potentially required change detected/m);
322 $o = ppport(qw(--nochanges --nohints file2.xs));
323 ok($o =~ /^Scanning.*file2\.xs/mi);
324 ok($o =~ /Analyzing.*file2\.xs/mi);
325 ok($o !~ /^Scanning.*file1\.xs/mi);
326 ok($o =~ /^Uses mXPUSHp/m);
327 ok($o =~ /^Needs to include.*ppport\.h/m);
328 ok($o !~ /^Looks good/m);
329 ok($o =~ /^1 potentially required change detected/m);
331 $o = ppport(qw(--nochanges --nohints --nodiag file2.xs));
332 ok($o =~ /^Scanning.*file2\.xs/mi);
333 ok($o =~ /Analyzing.*file2\.xs/mi);
334 ok($o !~ /^Scanning.*file1\.xs/mi);
335 ok($o !~ /^Uses mXPUSHp/m);
336 ok($o !~ /^Needs to include.*ppport\.h/m);
337 ok($o !~ /^Looks good/m);
338 ok($o =~ /^1 potentially required change detected/m);
340 $o = ppport(qw(--nochanges --quiet file2.xs));
343 ---------------------------- file1.xs -----------------------------------------
345 #define NEED_newCONSTSUB
346 #define NEED_sv_2pv_flags
347 #define NEED_PL_parser
354 ---------------------------- file2.xs -----------------------------------------
358 ===============================================================================
360 my $o = ppport(qw(--nochanges));
361 ok($o =~ /^Scanning.*FooBar\.xs/mi);
362 ok($o =~ /Analyzing.*FooBar\.xs/mi);
363 ok(matches($o, '^Scanning', 'm'), 1);
364 ok($o !~ /^Looks good/m);
365 ok($o =~ /^Uses grok_bin/m);
367 ---------------------------- FooBar.xs ----------------------------------------
373 ===============================================================================
375 my $o = ppport(qw(--nochanges));
376 ok($o =~ /^Scanning.*First\.xs/mi);
377 ok($o =~ /Analyzing.*First\.xs/mi);
378 ok($o =~ /^Scanning.*second\.h/mi);
379 ok($o =~ /Analyzing.*second\.h/mi);
380 ok($o =~ /^Scanning.*sub.*third\.c/mi);
381 ok($o =~ /Analyzing.*sub.*third\.c/mi);
382 ok($o !~ /^Scanning.*foobar/mi);
383 ok(matches($o, '^Scanning', 'm'), 3);
385 ---------------------------- First.xs -----------------------------------------
389 ---------------------------- foobar.xyz ---------------------------------------
393 ---------------------------- second.h -----------------------------------------
397 ---------------------------- sub/third.c --------------------------------------
401 ===============================================================================
403 my $o = ppport(qw(--nochanges));
404 ok($o =~ /Possibly wrong #define NEED_foobar in.*test.xs/i);
406 ---------------------------- test.xs ------------------------------------------
410 ===============================================================================
412 # And now some complex "real-world" example
414 my $o = ppport(qw(--copy=f));
415 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c mod5.c)) {
416 ok($o =~ /^Scanning.*\Q$_\E/mi);
417 ok($o =~ /Analyzing.*\Q$_\E/i);
419 ok(matches($o, '^Scanning', 'm'), 6);
421 ok(matches($o, '^Writing copy of', 'm'), 5);
424 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c)) {
425 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
427 ok(eq_files("${_}f", "${_}r"));
431 ---------------------------- main.xs ------------------------------------------
437 #define NEED_newCONSTSUB
438 #define NEED_grok_hex_GLOBAL
443 Perl_grok_bin(aTHX_ foo, bar);
449 Perl_grok_bin(bar, sv_no);
451 ---------------------------- mod1.c -------------------------------------------
457 #define NEED_grok_bin_GLOBAL
458 #define NEED_newCONSTSUB
465 Perl_sv_catpvf(); /* I know it's wrong ;-) */
468 ---------------------------- mod2.c -------------------------------------------
483 ---------------------------- mod3.c -------------------------------------------
492 ---------------------------- mod4.c -------------------------------------------
500 ---------------------------- mod5.c -------------------------------------------
509 ---------------------------- main.xsr -----------------------------------------
515 #define NEED_eval_pv_GLOBAL
516 #define NEED_grok_hex
517 #define NEED_newCONSTSUB_GLOBAL
528 grok_bin(bar, PL_sv_no);
530 ---------------------------- mod1.cr ------------------------------------------
536 #define NEED_grok_bin_GLOBAL
542 Perl_croak (aTHX_ "foo");
543 Perl_sv_catpvf(aTHX); /* I know it's wrong ;-) */
546 ---------------------------- mod2.cr ------------------------------------------
559 ---------------------------- mod3.cr ------------------------------------------
564 #define NEED_grok_oct
570 ---------------------------- mod4.cr ------------------------------------------
579 ===============================================================================
581 my $o = ppport(qw(--nochanges));
582 ok($o =~ /Uses grok_hex/m);
583 ok($o !~ /Looks good/m);
585 $o = ppport(qw(--nochanges --compat-version=5.8.0));
586 ok($o !~ /Uses grok_hex/m);
587 ok($o =~ /Looks good/m);
589 ---------------------------- FooBar.xs ----------------------------------------
593 ===============================================================================
595 my $o = ppport(qw(--nochanges));
596 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
598 $o = ppport(qw(--nochanges --compat-version=5.5.3));
599 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
601 $o = ppport(qw(--nochanges --compat-version=5.005_03));
602 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
604 $o = ppport(qw(--nochanges --compat-version=5.6.0));
605 ok($o !~ /Uses SvPVutf8_force/m);
607 $o = ppport(qw(--nochanges --compat-version=5.006));
608 ok($o !~ /Uses SvPVutf8_force/m);
610 $o = ppport(qw(--nochanges --compat-version=5.999.999));
611 ok($o !~ /Uses SvPVutf8_force/m);
613 $o = ppport(qw(--nochanges --compat-version=6.0.0));
614 ok($o =~ /Only Perl 5 is supported/m);
616 $o = ppport(qw(--nochanges --compat-version=5.1000.999));
617 ok($o =~ /Invalid version number: 5.1000.999/m);
619 $o = ppport(qw(--nochanges --compat-version=5.999.1000));
620 ok($o =~ /Invalid version number: 5.999.1000/m);
622 ---------------------------- FooBar.xs ----------------------------------------
626 ===============================================================================
628 my $o = ppport(qw(--nochanges));
629 ok($o !~ /potentially required change/);
630 ok(matches($o, '^Looks good', 'm'), 2);
632 ---------------------------- FooBar.xs ----------------------------------------
634 #define NEED_grok_numeric_radix
635 #define NEED_grok_number
638 GROK_NUMERIC_RADIX();
641 ---------------------------- foo.c --------------------------------------------
647 ===============================================================================
649 # check --api-info option
651 my $o = ppport(qw(--api-info=INT2PTR));
652 my %found = map {($_ => 1)} $o =~ /^===\s+(\w+)\s+===/mg;
653 ok(scalar keys %found, 1);
654 ok(exists $found{INT2PTR});
655 ok(matches($o, '^Supported at least starting from perl-5\.6\.0\.', 'm'), 1);
656 ok(matches($o, '^Support by .*ppport.* provided back to perl-5\.003\.', 'm'), 1);
658 $o = ppport(qw(--api-info=Zero));
659 %found = map {($_ => 1)} $o =~ /^===\s+(\w+)\s+===/mg;
660 ok(scalar keys %found, 1);
661 ok(exists $found{Zero});
662 ok(matches($o, '^No portability information available\.', 'm'), 1);
664 $o = ppport(qw(--api-info=/Zero/));
665 %found = map {($_ => 1)} $o =~ /^===\s+(\w+)\s+===/mg;
666 ok(scalar keys %found, 2);
667 ok(exists $found{Zero});
668 ok(exists $found{ZeroD});
670 ===============================================================================
672 # check --list-provided option
674 my @o = ppport(qw(--list-provided));
678 my($name, $flags) = /^(\w+)(?:\s+\[(\w+(?:,\s+\w+)*)\])?$/ or $fail++;
679 exists $p{$name} and $fail++;
680 $p{$name} = defined $flags ? { map { ($_ => 1) } $flags =~ /(\w+)/g } : '';
685 ok(exists $p{call_pv});
686 ok(not ref $p{call_pv});
688 ok(exists $p{grok_bin});
689 ok(ref $p{grok_bin}, 'HASH');
690 ok(scalar keys %{$p{grok_bin}}, 2);
691 ok($p{grok_bin}{explicit});
692 ok($p{grok_bin}{depend});
694 ok(exists $p{gv_stashpvn});
695 ok(ref $p{gv_stashpvn}, 'HASH');
696 ok(scalar keys %{$p{gv_stashpvn}}, 2);
697 ok($p{gv_stashpvn}{depend});
698 ok($p{gv_stashpvn}{hint});
700 ok(exists $p{sv_catpvf_mg});
701 ok(ref $p{sv_catpvf_mg}, 'HASH');
702 ok(scalar keys %{$p{sv_catpvf_mg}}, 2);
703 ok($p{sv_catpvf_mg}{explicit});
704 ok($p{sv_catpvf_mg}{depend});
706 ok(exists $p{PL_signals});
707 ok(ref $p{PL_signals}, 'HASH');
708 ok(scalar keys %{$p{PL_signals}}, 1);
709 ok($p{PL_signals}{explicit});
711 ===============================================================================
713 # check --list-unsupported option
715 my @o = ppport(qw(--list-unsupported));
719 my($name, $ver) = /^(\w+)\s*\.+\s*([\d._]+)$/ or $fail++;
720 exists $p{$name} and $fail++;
726 ok(exists $p{utf8_distance});
727 ok($p{utf8_distance}, '5.6.0');
729 ok(exists $p{save_generic_svref});
730 ok($p{save_generic_svref}, '5.005_03');
732 ===============================================================================
734 # check --nofilter option
736 my $o = ppport(qw(--nochanges));
737 ok($o =~ /^Scanning.*foo\.cpp/mi);
738 ok($o =~ /Analyzing.*foo\.cpp/mi);
739 ok(matches($o, '^Scanning', 'm'), 1);
740 ok(matches($o, 'Analyzing', 'm'), 1);
742 $o = ppport(qw(--nochanges foo.cpp foo.o Makefile.PL));
743 ok($o =~ /Skipping the following files \(use --nofilter to avoid this\):/m);
744 ok(matches($o, '^\|\s+foo\.o', 'mi'), 1);
745 ok(matches($o, '^\|\s+Makefile\.PL', 'mi'), 1);
746 ok($o =~ /^Scanning.*foo\.cpp/mi);
747 ok($o =~ /Analyzing.*foo\.cpp/mi);
748 ok(matches($o, '^Scanning', 'm'), 1);
749 ok(matches($o, 'Analyzing', 'm'), 1);
751 $o = ppport(qw(--nochanges --nofilter foo.cpp foo.o Makefile.PL));
752 ok($o =~ /^Scanning.*foo\.cpp/mi);
753 ok($o =~ /Analyzing.*foo\.cpp/mi);
754 ok($o =~ /^Scanning.*foo\.o/mi);
755 ok($o =~ /Analyzing.*foo\.o/mi);
756 ok($o =~ /^Scanning.*Makefile/mi);
757 ok($o =~ /Analyzing.*Makefile/mi);
758 ok(matches($o, '^Scanning', 'm'), 3);
759 ok(matches($o, 'Analyzing', 'm'), 3);
761 ---------------------------- foo.cpp ------------------------------------------
765 ---------------------------- foo.o --------------------------------------------
769 ---------------------------- Makefile.PL --------------------------------------
773 ===============================================================================
775 # check if explicit variables are handled propery
777 my $o = ppport(qw(--copy=a));
778 ok($o =~ /^Needs to include.*ppport\.h/m);
779 ok($o =~ /^Uses PL_signals/m);
780 ok($o =~ /^File needs PL_signals, adding static request/m);
781 ok(eq_files('MyExt.xsa', 'MyExt.ra'));
783 unlink qw(MyExt.xsa);
785 ---------------------------- MyExt.xs -----------------------------------------
788 if (PL_signals == 42)
791 ---------------------------- MyExt.ra -----------------------------------------
793 #define NEED_PL_signals
796 if (PL_signals == 42)
799 ===============================================================================
801 my $o = ppport(qw(--nochanges file.xs));
802 ok($o =~ /^Uses PL_copline/m);
803 ok($o =~ /WARNING: PL_copline/m);
804 ok($o =~ /^Uses SvUOK/m);
805 ok($o =~ /WARNING: Uses SvUOK, which may not be portable/m);
806 ok($o =~ /^Analysis completed \(2 warnings\)/m);
807 ok($o =~ /^Looks good/m);
809 $o = ppport(qw(--nochanges --compat-version=5.8.0 file.xs));
810 ok($o =~ /^Uses PL_copline/m);
811 ok($o =~ /WARNING: PL_copline/m);
812 ok($o !~ /WARNING: Uses SvUOK, which may not be portable/m);
813 ok($o =~ /^Analysis completed \(1 warning\)/m);
814 ok($o =~ /^Looks good/m);
816 ---------------------------- file.xs -----------------------------------------
818 #define NEED_PL_parser
823 ===============================================================================
825 my $o = ppport(qw(--copy=f));
828 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
830 ok(eq_files("${_}f", "${_}r"));
834 ---------------------------- file.xs -----------------------------------------
836 a_string = "sv_undef"
838 #define SOMETHING defgv
839 /* C-comment: sv_tainted */
841 # This is just a big XS comment using sv_no
843 /* The following, is NOT an XS comment! */
844 # define SOMETHING_ELSE defgv + \
847 ---------------------------- file.xsr -----------------------------------------
850 a_string = "sv_undef"
852 #define SOMETHING PL_defgv
853 /* C-comment: sv_tainted */
855 # This is just a big XS comment using sv_no
857 /* The following, is NOT an XS comment! */
858 # define SOMETHING_ELSE PL_defgv + \
861 ===============================================================================
863 my $o = ppport(qw(--copy=f));
866 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
868 ok(eq_files("${_}f", "${_}r"));
872 ---------------------------- file.xs -----------------------------------------
874 #define NEED_sv_2pv_flags
875 #define NEED_vnewSVpvf
878 Perl_croak_nocontext("foo");
881 croak_nocontext("foo");
882 Perl_warner_nocontext("foo");
884 warner_nocontext("foo");
887 ---------------------------- file.xsr -----------------------------------------
889 #define NEED_sv_2pv_flags
890 #define NEED_vnewSVpvf
893 Perl_croak_nocontext("foo");
894 Perl_croak(aTHX_ "bar");
896 croak_nocontext("foo");
897 Perl_warner_nocontext("foo");
898 Perl_warner(aTHX_ "foo");
899 warner_nocontext("foo");