1 ################################################################################
5 ## $Date: 2004/08/17 22:04:17 +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'}) {
32 $inc = '-I../../lib' if -d '../../lib';
34 if ($perl =~ m!^\./!) {
39 chdir("..") if !-d $tmp && -d "../$tmp";
43 ok(&Devel::PPPort::WriteFile("ppport.h"));
48 print "# *** running $perl $inc ppport.h @args ***\n";
49 my $out = join '', `$perl $inc ppport.h @args`;
58 my($str, $re, $mod) = @_;
60 eval "\@n = \$str =~ /$re/g$mod;";
63 $err =~ s/^/# *** /mg;
64 print "# *** ERROR ***\n$err\n";
66 return $@ ? -42 : scalar @n;
72 return 0 unless -e $f1 && -e $f2;
77 print "# couldn't open $_: $!\n";
80 $_ = do { local $/; <F> };
91 for (split /\s*={70,}\s*/, do { local $/; <DATA> }) {
94 ($c, @f{m/-{20,}\s+(\S+)\s+-{20,}/g}) = split /\s*-{20,}\s+\S+\s+-{20,}\s*/;
95 push @tests, { code => $c, files => \%f };
101 for $f (keys %{$t->{files}}) {
102 my @f = split /\//, $f;
105 my $path = join '/', @f;
106 mkpath($path) or die "mkpath('$path'): $!\n";
108 my $txt = $t->{files}{$f};
110 open F, ">$f" or die "open $f: $!\n";
114 print "# *** writing $f ***\n$txt\n";
120 $err =~ s/^/# *** /mg;
121 print "# *** ERROR ***\n$err\n";
125 for (keys %{$t->{files}}) {
126 unlink $_ or die "unlink('$_'): $!\n";
134 return $perl if $^O eq 'VMS';
136 my $exe = $Config{'_exe'} || '';
138 if ($perl =~ /^perl\Q$exe\E$/i) {
140 eval "require File::Spec";
144 $perl = File::Spec->catfile(File::Spec->curdir(), $perl);
148 if ($perl !~ /\Q$exe\E$/i) {
152 warn "find_perl: cannot find $perl from $^X" unless -f $perl;
159 my $o = ppport(qw(--help));
160 ok($o =~ /^Usage:.*ppport\.h/m);
163 $o = ppport(qw(--nochanges));
164 ok($o =~ /^scanning.*test\.xs/mi);
165 ok($o =~ /analyzing.*test\.xs/mi);
166 ok(matches($o, '^scanning', 'mi'), 1);
167 ok(matches($o, 'analyzing', 'mi'), 1);
168 ok($o =~ /Uses Perl_newSViv instead of newSViv/);
170 $o = ppport(qw(--quiet --nochanges));
173 ---------------------------- test.xs ------------------------------------------
177 ===============================================================================
179 # check if C and C++ comments are filtered correctly
181 my $o = ppport(qw(--copy=a));
182 ok($o =~ /^scanning.*MyExt\.xs/mi);
183 ok($o =~ /analyzing.*MyExt\.xs/mi);
184 ok(matches($o, '^scanning', 'mi'), 1);
185 ok($o =~ /^Needs to include.*ppport\.h/m);
186 ok($o !~ /^Uses grok_bin/m);
187 ok($o !~ /^Uses newSVpv/m);
188 ok($o =~ /Uses 1 C\+\+ style comment/m);
189 ok(eq_files('MyExt.xsa', 'MyExt.ra'));
191 # check if C++ are left untouched with --cplusplus
193 $o = ppport(qw(--copy=b --cplusplus));
194 ok($o =~ /^scanning.*MyExt\.xs/mi);
195 ok($o =~ /analyzing.*MyExt\.xs/mi);
196 ok(matches($o, '^scanning', 'mi'), 1);
197 ok($o =~ /^Needs to include.*ppport\.h/m);
198 ok($o !~ /^Uses grok_bin/m);
199 ok($o !~ /^Uses newSVpv/m);
200 ok($o !~ /Uses \d+ C\+\+ style comment/m);
201 ok(eq_files('MyExt.xsb', 'MyExt.rb'));
203 unlink qw(MyExt.xsa MyExt.xsb);
205 ---------------------------- MyExt.xs -----------------------------------------
212 ---------------------------- MyExt.ra -----------------------------------------
220 ---------------------------- MyExt.rb -----------------------------------------
228 ===============================================================================
230 my $o = ppport(qw(--nochanges file1.xs));
231 ok($o =~ /^scanning.*file1\.xs/mi);
232 ok($o =~ /analyzing.*file1\.xs/mi);
233 ok($o !~ /^scanning.*file2\.xs/mi);
234 ok($o =~ /^Uses newCONSTSUB/m);
235 ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_nolen/m);
236 ok($o =~ /hint for newCONSTSUB/m);
237 ok($o !~ /hint for sv_2pv_nolen/m);
238 ok($o =~ /^Looks good/m);
240 $o = ppport(qw(--nochanges --nohints file1.xs));
241 ok($o =~ /^scanning.*file1\.xs/mi);
242 ok($o =~ /analyzing.*file1\.xs/mi);
243 ok($o !~ /^scanning.*file2\.xs/mi);
244 ok($o =~ /^Uses newCONSTSUB/m);
245 ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_nolen/m);
246 ok($o !~ /hint for newCONSTSUB/m);
247 ok($o !~ /hint for sv_2pv_nolen/m);
248 ok($o =~ /^Looks good/m);
250 $o = ppport(qw(--nochanges --nohints --nodiag file1.xs));
251 ok($o =~ /^scanning.*file1\.xs/mi);
252 ok($o =~ /analyzing.*file1\.xs/mi);
253 ok($o !~ /^scanning.*file2\.xs/mi);
254 ok($o !~ /^Uses newCONSTSUB/m);
255 ok($o !~ /^Uses SvPV_nolen/m);
256 ok($o !~ /hint for newCONSTSUB/m);
257 ok($o !~ /hint for sv_2pv_nolen/m);
258 ok($o =~ /^Looks good/m);
260 $o = ppport(qw(--nochanges --quiet file1.xs));
263 $o = ppport(qw(--nochanges file2.xs));
264 ok($o =~ /^scanning.*file2\.xs/mi);
265 ok($o =~ /analyzing.*file2\.xs/mi);
266 ok($o !~ /^scanning.*file1\.xs/mi);
267 ok($o =~ /^Uses mXPUSHp/m);
268 ok($o =~ /^Needs to include.*ppport\.h/m);
269 ok($o !~ /^Looks good/m);
270 ok($o =~ /^1 potentially required change detected/m);
272 $o = ppport(qw(--nochanges --nohints file2.xs));
273 ok($o =~ /^scanning.*file2\.xs/mi);
274 ok($o =~ /analyzing.*file2\.xs/mi);
275 ok($o !~ /^scanning.*file1\.xs/mi);
276 ok($o =~ /^Uses mXPUSHp/m);
277 ok($o =~ /^Needs to include.*ppport\.h/m);
278 ok($o !~ /^Looks good/m);
279 ok($o =~ /^1 potentially required change detected/m);
281 $o = ppport(qw(--nochanges --nohints --nodiag file2.xs));
282 ok($o =~ /^scanning.*file2\.xs/mi);
283 ok($o =~ /analyzing.*file2\.xs/mi);
284 ok($o !~ /^scanning.*file1\.xs/mi);
285 ok($o !~ /^Uses mXPUSHp/m);
286 ok($o !~ /^Needs to include.*ppport\.h/m);
287 ok($o !~ /^Looks good/m);
288 ok($o =~ /^1 potentially required change detected/m);
290 $o = ppport(qw(--nochanges --quiet file2.xs));
293 ---------------------------- file1.xs -----------------------------------------
295 #define NEED_newCONSTSUB
296 #define NEED_sv_2pv_nolen
302 ---------------------------- file2.xs -----------------------------------------
306 ===============================================================================
308 my $o = ppport(qw(--nochanges));
309 ok($o =~ /^scanning.*FooBar\.xs/mi);
310 ok($o =~ /analyzing.*FooBar\.xs/mi);
311 ok(matches($o, '^scanning', 'mi'), 1);
312 ok($o !~ /^Looks good/m);
313 ok($o =~ /^Uses grok_bin/m);
315 ---------------------------- FooBar.xs ----------------------------------------
321 ===============================================================================
323 my $o = ppport(qw(--nochanges));
324 ok($o =~ /^scanning.*First\.xs/mi);
325 ok($o =~ /analyzing.*First\.xs/mi);
326 ok($o =~ /^scanning.*second\.h/mi);
327 ok($o =~ /analyzing.*second\.h/mi);
328 ok($o =~ /^scanning.*sub.*third\.c/mi);
329 ok($o =~ /analyzing.*sub.*third\.c/mi);
330 ok($o !~ /^scanning.*foobar/mi);
331 ok(matches($o, '^scanning', 'mi'), 3);
333 ---------------------------- First.xs -----------------------------------------
337 ---------------------------- foobar.xyz ---------------------------------------
341 ---------------------------- second.h -----------------------------------------
345 ---------------------------- sub/third.c --------------------------------------
349 ===============================================================================
351 my $o = ppport(qw(--nochanges));
352 ok($o =~ /Possibly wrong #define NEED_foobar in.*test.xs/i);
354 ---------------------------- test.xs ------------------------------------------
358 ===============================================================================
360 # And now some complex "real-world" example
362 my $o = ppport(qw(--copy=f));
363 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c mod5.c)) {
364 ok($o =~ /^scanning.*\Q$_\E/mi);
365 ok($o =~ /analyzing.*\Q$_\E/i);
367 ok(matches($o, '^scanning', 'mi'), 6);
369 ok(matches($o, '^Writing copy of', 'mi'), 5);
372 for (qw(main.xs mod1.c mod2.c mod3.c mod4.c)) {
373 ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi);
375 ok(eq_files("${_}f", "${_}r"));
379 ---------------------------- main.xs ------------------------------------------
385 #define NEED_newCONSTSUB
386 #define NEED_grok_hex_GLOBAL
391 Perl_grok_bin(aTHX_ foo, bar);
397 Perl_grok_bin(bar, sv_no);
399 ---------------------------- mod1.c -------------------------------------------
405 #define NEED_grok_bin_GLOBAL
406 #define NEED_newCONSTSUB
413 Perl_sv_catpvf(); /* I know it's wrong ;-) */
416 ---------------------------- mod2.c -------------------------------------------
431 ---------------------------- mod3.c -------------------------------------------
440 ---------------------------- mod4.c -------------------------------------------
448 ---------------------------- mod5.c -------------------------------------------
457 ---------------------------- main.xsr -----------------------------------------
463 #define NEED_eval_pv_GLOBAL
464 #define NEED_grok_hex
465 #define NEED_newCONSTSUB_GLOBAL
476 grok_bin(bar, PL_sv_no);
478 ---------------------------- mod1.cr ------------------------------------------
484 #define NEED_grok_bin_GLOBAL
490 Perl_croak (aTHX_ "foo");
491 Perl_sv_catpvf(aTHX); /* I know it's wrong ;-) */
494 ---------------------------- mod2.cr ------------------------------------------
507 ---------------------------- mod3.cr ------------------------------------------
512 #define NEED_grok_oct
518 ---------------------------- mod4.cr ------------------------------------------
527 ===============================================================================
529 my $o = ppport(qw(--nochanges));
530 ok($o =~ /Uses grok_hex/m);
531 ok($o !~ /Looks good/m);
533 $o = ppport(qw(--nochanges --compat-version=5.8.0));
534 ok($o !~ /Uses grok_hex/m);
535 ok($o =~ /Looks good/m);
537 ---------------------------- FooBar.xs ----------------------------------------
541 ===============================================================================
543 my $o = ppport(qw(--nochanges));
544 ok($o =~ /Uses SvPVutf8_force, which may not be portable/m);
546 $o = ppport(qw(--nochanges --compat-version=5.6.0));
547 ok($o !~ /Uses SvPVutf8_force/m);
549 ---------------------------- FooBar.xs ----------------------------------------
553 ===============================================================================
555 my $o = ppport(qw(--nochanges));
556 ok($o !~ /potentially required change/);
557 ok(matches($o, '^Looks good', 'mi'), 2);
559 ---------------------------- FooBar.xs ----------------------------------------
561 #define NEED_grok_numeric_radix
562 #define NEED_grok_number
565 GROK_NUMERIC_RADIX();
568 ---------------------------- foo.c --------------------------------------------