1 ################################################################################
5 ## $Date: 2007/08/20 18:06:48 +0200 $
7 ################################################################################
9 ## Version 3.x, Copyright (C) 2004-2007, 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 SvPV_nolen.*depends.*sv_2pv_flags/m);
280 ok($o =~ /WARNING: PL_expect/m);
281 ok($o =~ /hint for newCONSTSUB/m);
282 ok($o =~ /^Looks good/m);
284 $o = ppport(qw(--nochanges --nohints file1.xs));
285 ok($o =~ /^Scanning.*file1\.xs/mi);
286 ok($o =~ /Analyzing.*file1\.xs/mi);
287 ok($o !~ /^Scanning.*file2\.xs/mi);
288 ok($o =~ /^Uses newCONSTSUB/m);
289 ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_flags/m);
290 ok($o =~ /WARNING: PL_expect/m);
291 ok($o !~ /hint for newCONSTSUB/m);
292 ok($o =~ /^Looks good/m);
294 $o = ppport(qw(--nochanges --nohints --nodiag file1.xs));
295 ok($o =~ /^Scanning.*file1\.xs/mi);
296 ok($o =~ /Analyzing.*file1\.xs/mi);
297 ok($o !~ /^Scanning.*file2\.xs/mi);
298 ok($o !~ /^Uses newCONSTSUB/m);
299 ok($o !~ /^Uses SvPV_nolen/m);
300 ok($o =~ /WARNING: PL_expect/m);
301 ok($o !~ /hint for newCONSTSUB/m);
302 ok($o =~ /^Looks good/m);
304 $o = ppport(qw(--nochanges --quiet file1.xs));
307 $o = ppport(qw(--nochanges file2.xs));
308 ok($o =~ /^Scanning.*file2\.xs/mi);
309 ok($o =~ /Analyzing.*file2\.xs/mi);
310 ok($o !~ /^Scanning.*file1\.xs/mi);
311 ok($o =~ /^Uses mXPUSHp/m);
312 ok($o =~ /^Needs to include.*ppport\.h/m);
313 ok($o !~ /^Looks good/m);
314 ok($o =~ /^1 potentially required change detected/m);
316 $o = ppport(qw(--nochanges --nohints file2.xs));
317 ok($o =~ /^Scanning.*file2\.xs/mi);
318 ok($o =~ /Analyzing.*file2\.xs/mi);
319 ok($o !~ /^Scanning.*file1\.xs/mi);
320 ok($o =~ /^Uses mXPUSHp/m);
321 ok($o =~ /^Needs to include.*ppport\.h/m);
322 ok($o !~ /^Looks good/m);
323 ok($o =~ /^1 potentially required change detected/m);
325 $o = ppport(qw(--nochanges --nohints --nodiag file2.xs));
326 ok($o =~ /^Scanning.*file2\.xs/mi);
327 ok($o =~ /Analyzing.*file2\.xs/mi);
328 ok($o !~ /^Scanning.*file1\.xs/mi);
329 ok($o !~ /^Uses mXPUSHp/m);
330 ok($o !~ /^Needs to include.*ppport\.h/m);
331 ok($o !~ /^Looks good/m);
332 ok($o =~ /^1 potentially required change detected/m);
334 $o = ppport(qw(--nochanges --quiet file2.xs));
337 ---------------------------- file1.xs -----------------------------------------
339 #define NEED_newCONSTSUB
340 #define NEED_sv_2pv_flags
347 ---------------------------- file2.xs -----------------------------------------
351 ===============================================================================
353 my $o = ppport(qw(--nochanges));
354 ok($o =~ /^Scanning.*FooBar\.xs/mi);
355 ok($o =~ /Analyzing.*FooBar\.xs/mi);
356 ok(matches($o, '^Scanning', 'm'), 1);
357 ok($o !~ /^Looks good/m);
358 ok($o =~ /^Uses grok_bin/m);
360 ---------------------------- FooBar.xs ----------------------------------------
366 ===============================================================================
368 my $o = ppport(qw(--nochanges));
369 ok($o =~ /^Scanning.*First\.xs/mi);
370 ok($o =~ /Analyzing.*First\.xs/mi);
371 ok($o =~ /^Scanning.*second\.h/mi);
372 ok($o =~ /Analyzing.*second\.h/mi);
373 ok($o =~ /^Scanning.*sub.*third\.c/mi);
374 ok($o =~ /Analyzing.*sub.*third\.c/mi);
375 ok($o !~ /^Scanning.*foobar/mi);
376 ok(matches($o, '^Scanning', 'm'), 3);
378 ---------------------------- First.xs -----------------------------------------
382 ---------------------------- foobar.xyz ---------------------------------------
386 ---------------------------- second.h -----------------------------------------
390 ---------------------------- sub/third.c --------------------------------------
394 ===============================================================================
396 my $o = ppport(qw(--nochanges));
397 ok($o =~ /Possibly wrong #define NEED_foobar in.*test.xs/i);
399 ---------------------------- test.xs ------------------------------------------
403 ===============================================================================
405 # And now some complex "real-world" example
407 my $o = ppport(qw(--copy=f));
408 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c mod5.c)) {
409 ok($o =~ /^Scanning.*\Q$_\E/mi);
410 ok($o =~ /Analyzing.*\Q$_\E/i);
412 ok(matches($o, '^Scanning', 'm'), 6);
414 ok(matches($o, '^Writing copy of', 'm'), 5);
417 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c)) {
418 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
420 ok(eq_files("${_}f", "${_}r"));
424 ---------------------------- main.xs ------------------------------------------
430 #define NEED_newCONSTSUB
431 #define NEED_grok_hex_GLOBAL
436 Perl_grok_bin(aTHX_ foo, bar);
442 Perl_grok_bin(bar, sv_no);
444 ---------------------------- mod1.c -------------------------------------------
450 #define NEED_grok_bin_GLOBAL
451 #define NEED_newCONSTSUB
458 Perl_sv_catpvf(); /* I know it's wrong ;-) */
461 ---------------------------- mod2.c -------------------------------------------
476 ---------------------------- mod3.c -------------------------------------------
485 ---------------------------- mod4.c -------------------------------------------
493 ---------------------------- mod5.c -------------------------------------------
502 ---------------------------- main.xsr -----------------------------------------
508 #define NEED_eval_pv_GLOBAL
509 #define NEED_grok_hex
510 #define NEED_newCONSTSUB_GLOBAL
521 grok_bin(bar, PL_sv_no);
523 ---------------------------- mod1.cr ------------------------------------------
529 #define NEED_grok_bin_GLOBAL
535 Perl_croak (aTHX_ "foo");
536 Perl_sv_catpvf(aTHX); /* I know it's wrong ;-) */
539 ---------------------------- mod2.cr ------------------------------------------
552 ---------------------------- mod3.cr ------------------------------------------
557 #define NEED_grok_oct
563 ---------------------------- mod4.cr ------------------------------------------
572 ===============================================================================
574 my $o = ppport(qw(--nochanges));
575 ok($o =~ /Uses grok_hex/m);
576 ok($o !~ /Looks good/m);
578 $o = ppport(qw(--nochanges --compat-version=5.8.0));
579 ok($o !~ /Uses grok_hex/m);
580 ok($o =~ /Looks good/m);
582 ---------------------------- FooBar.xs ----------------------------------------
586 ===============================================================================
588 my $o = ppport(qw(--nochanges));
589 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
591 $o = ppport(qw(--nochanges --compat-version=5.5.3));
592 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
594 $o = ppport(qw(--nochanges --compat-version=5.005_03));
595 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
597 $o = ppport(qw(--nochanges --compat-version=5.6.0));
598 ok($o !~ /Uses SvPVutf8_force/m);
600 $o = ppport(qw(--nochanges --compat-version=5.006));
601 ok($o !~ /Uses SvPVutf8_force/m);
603 $o = ppport(qw(--nochanges --compat-version=5.999.999));
604 ok($o !~ /Uses SvPVutf8_force/m);
606 $o = ppport(qw(--nochanges --compat-version=6.0.0));
607 ok($o =~ /Only Perl 5 is supported/m);
609 $o = ppport(qw(--nochanges --compat-version=5.1000.999));
610 ok($o =~ /Invalid version number: 5.1000.999/m);
612 $o = ppport(qw(--nochanges --compat-version=5.999.1000));
613 ok($o =~ /Invalid version number: 5.999.1000/m);
615 ---------------------------- FooBar.xs ----------------------------------------
619 ===============================================================================
621 my $o = ppport(qw(--nochanges));
622 ok($o !~ /potentially required change/);
623 ok(matches($o, '^Looks good', 'm'), 2);
625 ---------------------------- FooBar.xs ----------------------------------------
627 #define NEED_grok_numeric_radix
628 #define NEED_grok_number
631 GROK_NUMERIC_RADIX();
634 ---------------------------- foo.c --------------------------------------------
640 ===============================================================================
642 # check --api-info option
644 my $o = ppport(qw(--api-info=INT2PTR));
645 my %found = map {($_ => 1)} $o =~ /^===\s+(\w+)\s+===/mg;
646 ok(scalar keys %found, 1);
647 ok(exists $found{INT2PTR});
648 ok(matches($o, '^Supported at least starting from perl-5\.6\.0\.', 'm'), 1);
649 ok(matches($o, '^Support by .*ppport.* provided back to perl-5\.003\.', 'm'), 1);
651 $o = ppport(qw(--api-info=Zero));
652 %found = map {($_ => 1)} $o =~ /^===\s+(\w+)\s+===/mg;
653 ok(scalar keys %found, 1);
654 ok(exists $found{Zero});
655 ok(matches($o, '^No portability information available\.', 'm'), 1);
657 $o = ppport(qw(--api-info=/Zero/));
658 %found = map {($_ => 1)} $o =~ /^===\s+(\w+)\s+===/mg;
659 ok(scalar keys %found, 2);
660 ok(exists $found{Zero});
661 ok(exists $found{ZeroD});
663 ===============================================================================
665 # check --list-provided option
667 my @o = ppport(qw(--list-provided));
671 my($name, $flags) = /^(\w+)(?:\s+\[(\w+(?:,\s+\w+)*)\])?$/ or $fail++;
672 exists $p{$name} and $fail++;
673 $p{$name} = defined $flags ? { map { ($_ => 1) } $flags =~ /(\w+)/g } : '';
678 ok(exists $p{call_sv});
679 ok(not ref $p{call_sv});
681 ok(exists $p{grok_bin});
682 ok(ref $p{grok_bin}, 'HASH');
683 ok(scalar keys %{$p{grok_bin}}, 2);
684 ok($p{grok_bin}{explicit});
685 ok($p{grok_bin}{depend});
687 ok(exists $p{gv_stashpvn});
688 ok(ref $p{gv_stashpvn}, 'HASH');
689 ok(scalar keys %{$p{gv_stashpvn}}, 2);
690 ok($p{gv_stashpvn}{depend});
691 ok($p{gv_stashpvn}{hint});
693 ok(exists $p{sv_catpvf_mg});
694 ok(ref $p{sv_catpvf_mg}, 'HASH');
695 ok(scalar keys %{$p{sv_catpvf_mg}}, 2);
696 ok($p{sv_catpvf_mg}{explicit});
697 ok($p{sv_catpvf_mg}{depend});
699 ok(exists $p{PL_signals});
700 ok(ref $p{PL_signals}, 'HASH');
701 ok(scalar keys %{$p{PL_signals}}, 1);
702 ok($p{PL_signals}{explicit});
704 ===============================================================================
706 # check --list-unsupported option
708 my @o = ppport(qw(--list-unsupported));
712 my($name, $ver) = /^(\w+)\s*\.+\s*([\d._]+)$/ or $fail++;
713 exists $p{$name} and $fail++;
719 ok(exists $p{utf8_distance});
720 ok($p{utf8_distance}, '5.6.0');
722 ok(exists $p{save_generic_svref});
723 ok($p{save_generic_svref}, '5.005_03');
725 ===============================================================================
727 # check --nofilter option
729 my $o = ppport(qw(--nochanges));
730 ok($o =~ /^Scanning.*foo\.cpp/mi);
731 ok($o =~ /Analyzing.*foo\.cpp/mi);
732 ok(matches($o, '^Scanning', 'm'), 1);
733 ok(matches($o, 'Analyzing', 'm'), 1);
735 $o = ppport(qw(--nochanges foo.cpp foo.o Makefile.PL));
736 ok($o =~ /Skipping the following files \(use --nofilter to avoid this\):/m);
737 ok(matches($o, '^\|\s+foo\.o', 'mi'), 1);
738 ok(matches($o, '^\|\s+Makefile\.PL', 'mi'), 1);
739 ok($o =~ /^Scanning.*foo\.cpp/mi);
740 ok($o =~ /Analyzing.*foo\.cpp/mi);
741 ok(matches($o, '^Scanning', 'm'), 1);
742 ok(matches($o, 'Analyzing', 'm'), 1);
744 $o = ppport(qw(--nochanges --nofilter foo.cpp foo.o Makefile.PL));
745 ok($o =~ /^Scanning.*foo\.cpp/mi);
746 ok($o =~ /Analyzing.*foo\.cpp/mi);
747 ok($o =~ /^Scanning.*foo\.o/mi);
748 ok($o =~ /Analyzing.*foo\.o/mi);
749 ok($o =~ /^Scanning.*Makefile/mi);
750 ok($o =~ /Analyzing.*Makefile/mi);
751 ok(matches($o, '^Scanning', 'm'), 3);
752 ok(matches($o, 'Analyzing', 'm'), 3);
754 ---------------------------- foo.cpp ------------------------------------------
758 ---------------------------- foo.o --------------------------------------------
762 ---------------------------- Makefile.PL --------------------------------------
766 ===============================================================================
768 # check if explicit variables are handled propery
770 my $o = ppport(qw(--copy=a));
771 ok($o =~ /^Needs to include.*ppport\.h/m);
772 ok($o =~ /^Uses PL_signals/m);
773 ok($o =~ /^File needs PL_signals, adding static request/m);
774 ok(eq_files('MyExt.xsa', 'MyExt.ra'));
776 unlink qw(MyExt.xsa);
778 ---------------------------- MyExt.xs -----------------------------------------
781 if (PL_signals == 42)
784 ---------------------------- MyExt.ra -----------------------------------------
786 #define NEED_PL_signals
789 if (PL_signals == 42)
792 ===============================================================================
794 my $o = ppport(qw(--nochanges file.xs));
795 ok($o =~ /^Uses PL_copline/m);
796 ok($o =~ /WARNING: PL_copline/m);
797 ok($o =~ /^Uses SvUOK/m);
798 ok($o =~ /WARNING: Uses SvUOK, which may not be portable/m);
799 ok($o =~ /^Analysis completed \(2 warnings\)/m);
800 ok($o =~ /^Looks good/m);
802 $o = ppport(qw(--nochanges --compat-version=5.8.0 file.xs));
803 ok($o =~ /^Uses PL_copline/m);
804 ok($o =~ /WARNING: PL_copline/m);
805 ok($o !~ /WARNING: Uses SvUOK, which may not be portable/m);
806 ok($o =~ /^Analysis completed \(1 warning\)/m);
807 ok($o =~ /^Looks good/m);
809 ---------------------------- file.xs -----------------------------------------
815 ===============================================================================
817 my $o = ppport(qw(--copy=f));
820 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
822 ok(eq_files("${_}f", "${_}r"));
826 ---------------------------- file.xs -----------------------------------------
828 a_string = "sv_undef"
830 #define SOMETHING defgv
831 /* C-comment: sv_tainted */
833 # This is just a big XS comment using sv_no
835 /* The following, is NOT an XS comment! */
836 # define SOMETHING_ELSE defgv + \
839 ---------------------------- file.xsr -----------------------------------------
842 a_string = "sv_undef"
844 #define SOMETHING PL_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 PL_defgv + \