1 ################################################################################
5 ## $Date: 2004/10/14 20:16:03 +0200 $
7 ################################################################################
9 ## Version 3.x, Copyright (C) 2004, 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 ################################################################################
20 use File::Path qw/rmtree mkpath/;
25 my $perl = find_perl();
27 rmtree($tmp) if -d $tmp;
28 mkpath($tmp) or die "mkpath $tmp: $!\n";
29 chdir($tmp) or die "chdir $tmp: $!\n";
31 if ($ENV{'PERL_CORE'}) {
33 $inc = $^O eq 'VMS' ? '-"I../../lib"' : '-I../../lib';
34 unshift @INC, '../../lib';
37 if ($perl =~ m!^\./!) {
42 chdir('..') if !-d $tmp && -d "../$tmp";
43 rmtree($tmp) if -d $tmp;
46 ok(&Devel::PPPort::WriteFile("ppport.h"));
51 print "# *** running $perl $inc ppport.h @args ***\n";
52 my $out = join '', `$perl $inc ppport.h @args`;
61 my($str, $re, $mod) = @_;
63 eval "\@n = \$str =~ /$re/g$mod;";
66 $err =~ s/^/# *** /mg;
67 print "# *** ERROR ***\n$err\n";
69 return $@ ? -42 : scalar @n;
75 return 0 unless -e $f1 && -e $f2;
80 print "# couldn't open $_: $!\n";
83 $_ = do { local $/; <F> };
94 for (split /\s*={70,}\s*/, do { local $/; <DATA> }) {
97 ($c, @f{m/-{20,}\s+(\S+)\s+-{20,}/g}) = split /\s*-{20,}\s+\S+\s+-{20,}\s*/;
98 push @tests, { code => $c, files => \%f };
104 for $f (keys %{$t->{files}}) {
105 my @f = split /\//, $f;
108 my $path = join '/', @f;
109 mkpath($path) or die "mkpath('$path'): $!\n";
111 my $txt = $t->{files}{$f};
113 open F, ">$f" or die "open $f: $!\n";
117 print "# *** writing $f ***\n$txt\n";
123 $err =~ s/^/# *** /mg;
124 print "# *** ERROR ***\n$err\n";
128 for (keys %{$t->{files}}) {
129 unlink $_ or die "unlink('$_'): $!\n";
137 return $perl if $^O eq 'VMS';
139 my $exe = $Config{'_exe'} || '';
141 if ($perl =~ /^perl\Q$exe\E$/i) {
143 eval "require File::Spec";
147 $perl = File::Spec->catfile(File::Spec->curdir(), $perl);
151 if ($perl !~ /\Q$exe\E$/i) {
155 warn "find_perl: cannot find $perl from $^X" unless -f $perl;
162 my $o = ppport(qw(--help));
163 ok($o =~ /^Usage:.*ppport\.h/m);
166 $o = ppport(qw(--nochanges));
167 ok($o =~ /^scanning.*test\.xs/mi);
168 ok($o =~ /analyzing.*test\.xs/mi);
169 ok(matches($o, '^scanning', 'mi'), 1);
170 ok(matches($o, 'analyzing', 'mi'), 1);
171 ok($o =~ /Uses Perl_newSViv instead of newSViv/);
173 $o = ppport(qw(--quiet --nochanges));
176 ---------------------------- test.xs ------------------------------------------
180 ===============================================================================
182 # check if C and C++ comments are filtered correctly
184 my $o = ppport(qw(--copy=a));
185 ok($o =~ /^scanning.*MyExt\.xs/mi);
186 ok($o =~ /analyzing.*MyExt\.xs/mi);
187 ok(matches($o, '^scanning', 'mi'), 1);
188 ok($o =~ /^Needs to include.*ppport\.h/m);
189 ok($o !~ /^Uses grok_bin/m);
190 ok($o !~ /^Uses newSVpv/m);
191 ok($o =~ /Uses 1 C\+\+ style comment/m);
192 ok(eq_files('MyExt.xsa', 'MyExt.ra'));
194 # check if C++ are left untouched with --cplusplus
196 $o = ppport(qw(--copy=b --cplusplus));
197 ok($o =~ /^scanning.*MyExt\.xs/mi);
198 ok($o =~ /analyzing.*MyExt\.xs/mi);
199 ok(matches($o, '^scanning', 'mi'), 1);
200 ok($o =~ /^Needs to include.*ppport\.h/m);
201 ok($o !~ /^Uses grok_bin/m);
202 ok($o !~ /^Uses newSVpv/m);
203 ok($o !~ /Uses \d+ C\+\+ style comment/m);
204 ok(eq_files('MyExt.xsb', 'MyExt.rb'));
206 unlink qw(MyExt.xsa MyExt.xsb);
208 ---------------------------- MyExt.xs -----------------------------------------
215 ---------------------------- MyExt.ra -----------------------------------------
223 ---------------------------- MyExt.rb -----------------------------------------
231 ===============================================================================
233 my $o = ppport(qw(--nochanges file1.xs));
234 ok($o =~ /^scanning.*file1\.xs/mi);
235 ok($o =~ /analyzing.*file1\.xs/mi);
236 ok($o !~ /^scanning.*file2\.xs/mi);
237 ok($o =~ /^Uses newCONSTSUB/m);
238 ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_nolen/m);
239 ok($o =~ /hint for newCONSTSUB/m);
240 ok($o !~ /hint for sv_2pv_nolen/m);
241 ok($o =~ /^Looks good/m);
243 $o = ppport(qw(--nochanges --nohints file1.xs));
244 ok($o =~ /^scanning.*file1\.xs/mi);
245 ok($o =~ /analyzing.*file1\.xs/mi);
246 ok($o !~ /^scanning.*file2\.xs/mi);
247 ok($o =~ /^Uses newCONSTSUB/m);
248 ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_nolen/m);
249 ok($o !~ /hint for newCONSTSUB/m);
250 ok($o !~ /hint for sv_2pv_nolen/m);
251 ok($o =~ /^Looks good/m);
253 $o = ppport(qw(--nochanges --nohints --nodiag file1.xs));
254 ok($o =~ /^scanning.*file1\.xs/mi);
255 ok($o =~ /analyzing.*file1\.xs/mi);
256 ok($o !~ /^scanning.*file2\.xs/mi);
257 ok($o !~ /^Uses newCONSTSUB/m);
258 ok($o !~ /^Uses SvPV_nolen/m);
259 ok($o !~ /hint for newCONSTSUB/m);
260 ok($o !~ /hint for sv_2pv_nolen/m);
261 ok($o =~ /^Looks good/m);
263 $o = ppport(qw(--nochanges --quiet file1.xs));
266 $o = ppport(qw(--nochanges file2.xs));
267 ok($o =~ /^scanning.*file2\.xs/mi);
268 ok($o =~ /analyzing.*file2\.xs/mi);
269 ok($o !~ /^scanning.*file1\.xs/mi);
270 ok($o =~ /^Uses mXPUSHp/m);
271 ok($o =~ /^Needs to include.*ppport\.h/m);
272 ok($o !~ /^Looks good/m);
273 ok($o =~ /^1 potentially required change detected/m);
275 $o = ppport(qw(--nochanges --nohints file2.xs));
276 ok($o =~ /^scanning.*file2\.xs/mi);
277 ok($o =~ /analyzing.*file2\.xs/mi);
278 ok($o !~ /^scanning.*file1\.xs/mi);
279 ok($o =~ /^Uses mXPUSHp/m);
280 ok($o =~ /^Needs to include.*ppport\.h/m);
281 ok($o !~ /^Looks good/m);
282 ok($o =~ /^1 potentially required change detected/m);
284 $o = ppport(qw(--nochanges --nohints --nodiag file2.xs));
285 ok($o =~ /^scanning.*file2\.xs/mi);
286 ok($o =~ /analyzing.*file2\.xs/mi);
287 ok($o !~ /^scanning.*file1\.xs/mi);
288 ok($o !~ /^Uses mXPUSHp/m);
289 ok($o !~ /^Needs to include.*ppport\.h/m);
290 ok($o !~ /^Looks good/m);
291 ok($o =~ /^1 potentially required change detected/m);
293 $o = ppport(qw(--nochanges --quiet file2.xs));
296 ---------------------------- file1.xs -----------------------------------------
298 #define NEED_newCONSTSUB
299 #define NEED_sv_2pv_nolen
305 ---------------------------- file2.xs -----------------------------------------
309 ===============================================================================
311 my $o = ppport(qw(--nochanges));
312 ok($o =~ /^scanning.*FooBar\.xs/mi);
313 ok($o =~ /analyzing.*FooBar\.xs/mi);
314 ok(matches($o, '^scanning', 'mi'), 1);
315 ok($o !~ /^Looks good/m);
316 ok($o =~ /^Uses grok_bin/m);
318 ---------------------------- FooBar.xs ----------------------------------------
324 ===============================================================================
326 my $o = ppport(qw(--nochanges));
327 ok($o =~ /^scanning.*First\.xs/mi);
328 ok($o =~ /analyzing.*First\.xs/mi);
329 ok($o =~ /^scanning.*second\.h/mi);
330 ok($o =~ /analyzing.*second\.h/mi);
331 ok($o =~ /^scanning.*sub.*third\.c/mi);
332 ok($o =~ /analyzing.*sub.*third\.c/mi);
333 ok($o !~ /^scanning.*foobar/mi);
334 ok(matches($o, '^scanning', 'mi'), 3);
336 ---------------------------- First.xs -----------------------------------------
340 ---------------------------- foobar.xyz ---------------------------------------
344 ---------------------------- second.h -----------------------------------------
348 ---------------------------- sub/third.c --------------------------------------
352 ===============================================================================
354 my $o = ppport(qw(--nochanges));
355 ok($o =~ /Possibly wrong #define NEED_foobar in.*test.xs/i);
357 ---------------------------- test.xs ------------------------------------------
361 ===============================================================================
363 # And now some complex "real-world" example
365 my $o = ppport(qw(--copy=f));
366 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c mod5.c)) {
367 ok($o =~ /^scanning.*\Q$_\E/mi);
368 ok($o =~ /analyzing.*\Q$_\E/i);
370 ok(matches($o, '^scanning', 'mi'), 6);
372 ok(matches($o, '^Writing copy of', 'mi'), 5);
375 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c)) {
376 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
378 ok(eq_files("${_}f", "${_}r"));
382 ---------------------------- main.xs ------------------------------------------
388 #define NEED_newCONSTSUB
389 #define NEED_grok_hex_GLOBAL
394 Perl_grok_bin(aTHX_ foo, bar);
400 Perl_grok_bin(bar, sv_no);
402 ---------------------------- mod1.c -------------------------------------------
408 #define NEED_grok_bin_GLOBAL
409 #define NEED_newCONSTSUB
416 Perl_sv_catpvf(); /* I know it's wrong ;-) */
419 ---------------------------- mod2.c -------------------------------------------
434 ---------------------------- mod3.c -------------------------------------------
443 ---------------------------- mod4.c -------------------------------------------
451 ---------------------------- mod5.c -------------------------------------------
460 ---------------------------- main.xsr -----------------------------------------
466 #define NEED_eval_pv_GLOBAL
467 #define NEED_grok_hex
468 #define NEED_newCONSTSUB_GLOBAL
479 grok_bin(bar, PL_sv_no);
481 ---------------------------- mod1.cr ------------------------------------------
487 #define NEED_grok_bin_GLOBAL
493 Perl_croak (aTHX_ "foo");
494 Perl_sv_catpvf(aTHX); /* I know it's wrong ;-) */
497 ---------------------------- mod2.cr ------------------------------------------
510 ---------------------------- mod3.cr ------------------------------------------
515 #define NEED_grok_oct
521 ---------------------------- mod4.cr ------------------------------------------
530 ===============================================================================
532 my $o = ppport(qw(--nochanges));
533 ok($o =~ /Uses grok_hex/m);
534 ok($o !~ /Looks good/m);
536 $o = ppport(qw(--nochanges --compat-version=5.8.0));
537 ok($o !~ /Uses grok_hex/m);
538 ok($o =~ /Looks good/m);
540 ---------------------------- FooBar.xs ----------------------------------------
544 ===============================================================================
546 my $o = ppport(qw(--nochanges));
547 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
549 $o = ppport(qw(--nochanges --compat-version=5.6.0));
550 ok($o !~ /Uses SvPVutf8_force/m);
552 ---------------------------- FooBar.xs ----------------------------------------
556 ===============================================================================
558 my $o = ppport(qw(--nochanges));
559 ok($o !~ /potentially required change/);
560 ok(matches($o, '^Looks good', 'mi'), 2);
562 ---------------------------- FooBar.xs ----------------------------------------
564 #define NEED_grok_numeric_radix
565 #define NEED_grok_number
568 GROK_NUMERIC_RADIX();
571 ---------------------------- foo.c --------------------------------------------