Integrate perlio:
[p5sagit/p5-mst-13.2.git] / t / lib / extutils.t
CommitLineData
af6c647e 1#!./perl -w
2
cea00dc5 3print "1..26\n";
af6c647e 4
5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8}
9
10use warnings;
11use strict;
12use ExtUtils::MakeMaker;
13use ExtUtils::Constant qw (constant_types C_constant XS_constant autoload);
14use Config;
835f860c 15use File::Spec::Functions;
16use File::Spec;
17# Because were are going to be changing directory before running Makefile.PL
18my $perl = File::Spec->rel2abs( $^X );
6d79cad2 19# ExtUtils::Constant::C_constant uses $^X inside a comment, and we want to
20# compare output to ensure that it is the same. We were probably run as ./perl
21# whereas we will run the child with the full path in $perl. So make $^X for
22# us the same as our child will see.
23$^X = $perl;
af6c647e 24
835f860c 25print "# perl=$perl\n";
6d79cad2 26my $runperl = "$perl -x \"-I../../lib\"";
94b1a389 27
af6c647e 28$| = 1;
29
30my $dir = "ext-$$";
0ddb8edc 31my @files;
94b1a389 32
33print "# $dir being created...\n";
34mkdir $dir, 0777 or die "mkdir: $!\n";
35
af6c647e 36
37END {
94b1a389 38 use File::Path;
39 print "# $dir being removed...\n";
40 rmtree($dir);
af6c647e 41}
42
6d79cad2 43my $package = "ExtTest";
44
8ac27563 45# Test the code that generates 1 and 2 letter name comparisons.
46my %compass = (
47N => 0, NE => 45, E => 90, SE => 135, S => 180, SW => 225, W => 270, NW => 315
48);
49
cea00dc5 50my $parent_rfc1149 =
51 'A Standard for the Transmission of IP Datagrams on Avian Carriers';
52
835f860c 53my @names = ("FIVE", {name=>"OK6", type=>"PV",},
54 {name=>"OK7", type=>"PVN",
55 value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
af6c647e 56 {name => "FARTHING", type=>"NV"},
6d79cad2 57 {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"},
3414cef0 58 {name => "OPEN", type=>"PV", value=>'"/*"',
59 macro=>["#if 1\n", "#endif\n"]},
6d79cad2 60 {name => "CLOSE", type=>"PV", value=>'"*/"',
61 macro=>["#if 1\n", "#endif\n"]},
3414cef0 62 {name => "ANSWER", default=>["UV", 42]}, "NOTDEF",
63 {name => "Yes", type=>"YES"},
64 {name => "No", type=>"NO"},
19d75eda 65 {name => "Undef", type=>"UNDEF"},
cea00dc5 66# OK. It wasn't really designed to allow the creation of dual valued constants.
67# It was more for INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
68 {name=>"RFC1149", type=>"SV", value=>"sv_2mortal(temp_sv)",
69 pre=>"SV *temp_sv = newSVpv(RFC1149, 0); "
70 . "(void) SvUPGRADE(temp_sv,SVt_PVIV); SvIOK_on(temp_sv); "
71 . "SvIVX(temp_sv) = 1149;"},
3414cef0 72);
af6c647e 73
8ac27563 74push @names, $_ foreach keys %compass;
75
af6c647e 76my @names_only = map {(ref $_) ? $_->{name} : $_} @names;
77
6d79cad2 78my $types = {};
79my $constant_types = constant_types(); # macro defs
80my $C_constant = join "\n",
81 C_constant ($package, undef, "IV", $types, undef, undef, @names);
82my $XS_constant = XS_constant ($package, $types); # XS for ExtTest::constant
83
af6c647e 84################ Header
94b1a389 85my $header = catfile($dir, "test.h");
0ddb8edc 86push @files, "test.h";
94b1a389 87open FH, ">$header" or die "open >$header: $!\n";
cea00dc5 88print FH <<"EOT";
835f860c 89#define FIVE 5
90#define OK6 "ok 6\n"
91#define OK7 1
af6c647e 92#define FARTHING 0.25
93#define NOT_ZERO 1
3414cef0 94#define Yes 0
95#define No 1
96#define Undef 1
cea00dc5 97#define RFC1149 "$parent_rfc1149"
6d79cad2 98#undef NOTDEF
8ac27563 99
af6c647e 100EOT
8ac27563 101
102while (my ($point, $bearing) = each %compass) {
103 print FH "#define $point $bearing\n"
104}
94b1a389 105close FH or die "close $header: $!\n";
af6c647e 106
107################ XS
94b1a389 108my $xs = catfile($dir, "$package.xs");
0ddb8edc 109push @files, "$package.xs";
94b1a389 110open FH, ">$xs" or die "open >$xs: $!\n";
af6c647e 111
112print FH <<'EOT';
113#include "EXTERN.h"
114#include "perl.h"
115#include "XSUB.h"
116EOT
117
118print FH "#include \"test.h\"\n\n";
6d79cad2 119print FH $constant_types;
120print FH $C_constant, "\n";
af6c647e 121print FH "MODULE = $package PACKAGE = $package\n";
122print FH "PROTOTYPES: ENABLE\n";
6d79cad2 123print FH $XS_constant;
94b1a389 124close FH or die "close $xs: $!\n";
af6c647e 125
126################ PM
94b1a389 127my $pm = catfile($dir, "$package.pm");
0ddb8edc 128push @files, "$package.pm";
94b1a389 129open FH, ">$pm" or die "open >$pm: $!\n";
af6c647e 130print FH "package $package;\n";
131print FH "use $];\n";
132
133print FH <<'EOT';
134
135use strict;
136use warnings;
137use Carp;
138
139require Exporter;
140require DynaLoader;
af6c647e 141use vars qw ($VERSION @ISA @EXPORT_OK);
142
143$VERSION = '0.01';
144@ISA = qw(Exporter DynaLoader);
145@EXPORT_OK = qw(
146EOT
147
148print FH "\t$_\n" foreach (@names_only);
149print FH ");\n";
150print FH autoload ($package, $]);
151print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
94b1a389 152close FH or die "close $pm: $!\n";
af6c647e 153
154################ test.pl
94b1a389 155my $testpl = catfile($dir, "test.pl");
0ddb8edc 156push @files, "test.pl";
94b1a389 157open FH, ">$testpl" or die "open >$testpl: $!\n";
af6c647e 158
6d79cad2 159print FH "use strict;\n";
af6c647e 160print FH "use $package qw(@names_only);\n";
161print FH <<'EOT';
162
6d79cad2 163# IV
835f860c 164my $five = FIVE;
165if ($five == 5) {
166 print "ok 5\n";
af6c647e 167} else {
835f860c 168 print "not ok 5 # $five\n";
af6c647e 169}
170
6d79cad2 171# PV
835f860c 172print OK6;
af6c647e 173
6d79cad2 174# PVN containing embedded \0s
835f860c 175$_ = OK7;
af6c647e 176s/.*\0//s;
177print;
178
6d79cad2 179# NV
af6c647e 180my $farthing = FARTHING;
181if ($farthing == 0.25) {
835f860c 182 print "ok 8\n";
af6c647e 183} else {
835f860c 184 print "not ok 8 # $farthing\n";
af6c647e 185}
186
6d79cad2 187# UV
af6c647e 188my $not_zero = NOT_ZERO;
189if ($not_zero > 0 && $not_zero == ~0) {
835f860c 190 print "ok 9\n";
af6c647e 191} else {
835f860c 192 print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
af6c647e 193}
194
6d79cad2 195# Value includes a "*/" in an attempt to bust out of a C comment.
196# Also tests custom cpp #if clauses
197my $close = CLOSE;
198if ($close eq '*/') {
199 print "ok 10\n";
200} else {
201 print "not ok 10 # \$close='$close'\n";
202}
203
204# Default values if macro not defined.
205my $answer = ANSWER;
206if ($answer == 42) {
207 print "ok 11\n";
208} else {
209 print "not ok 11 # What do you get if you multiply six by nine? '$answer'\n";
210}
211
212# not defined macro
213my $notdef = eval { NOTDEF; };
214if (defined $notdef) {
215 print "not ok 12 # \$notdef='$notdef'\n";
216} elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
217 print "not ok 12 # \$@='$@'\n";
218} else {
219 print "ok 12\n";
220}
221
222# not a macro
223my $notthere = eval { &ExtTest::NOTTHERE; };
224if (defined $notthere) {
225 print "not ok 13 # \$notthere='$notthere'\n";
226} elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
227 chomp $@;
228 print "not ok 13 # \$@='$@'\n";
229} else {
230 print "ok 13\n";
231}
af6c647e 232
3414cef0 233# Truth
234my $yes = Yes;
235if ($yes) {
236 print "ok 14\n";
237} else {
238 print "not ok 14 # $yes='\$yes'\n";
239}
240
241# Falsehood
242my $no = No;
243if (defined $no and !$no) {
244 print "ok 15\n";
245} else {
246 print "not ok 15 # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";
247}
248
249# Undef
250my $undef = Undef;
251unless (defined $undef) {
252 print "ok 16\n";
253} else {
254 print "not ok 16 # \$undef='$undef'\n";
255}
256
8ac27563 257
258# invalid macro (chosen to look like a mix up between No and SW)
259$notdef = eval { &ExtTest::So };
260if (defined $notdef) {
261 print "not ok 17 # \$notdef='$notdef'\n";
262} elsif ($@ !~ /^So is not a valid ExtTest macro/) {
263 print "not ok 17 # \$@='$@'\n";
264} else {
265 print "ok 17\n";
266}
267
268# invalid defined macro
269$notdef = eval { &ExtTest::EW };
270if (defined $notdef) {
271 print "not ok 18 # \$notdef='$notdef'\n";
272} elsif ($@ !~ /^EW is not a valid ExtTest macro/) {
273 print "not ok 18 # \$@='$@'\n";
274} else {
275 print "ok 18\n";
276}
277
278my %compass = (
279EOT
280
281while (my ($point, $bearing) = each %compass) {
282 print FH "$point => $bearing, "
283}
284
285print FH <<'EOT';
286
287);
288
289my $fail;
290while (my ($point, $bearing) = each %compass) {
291 my $val = eval $point;
292 if ($@) {
293 print "# $point: \$@='$@'\n";
294 $fail = 1;
295 } elsif (!defined $bearing) {
296 print "# $point: \$val=undef\n";
297 $fail = 1;
298 } elsif ($val != $bearing) {
299 print "# $point: \$val=$val, not $bearing\n";
300 $fail = 1;
301 }
302}
303if ($fail) {
304 print "not ok 19\n";
305} else {
306 print "ok 19\n";
307}
308
af6c647e 309EOT
310
cea00dc5 311print FH <<"EOT";
312my \$rfc1149 = RFC1149;
313if (\$rfc1149 ne "$parent_rfc1149") {
314 print "not ok 20 # '\$rfc1149' ne '$parent_rfc1149'\n";
315} else {
316 print "ok 20\n";
317}
318
319if (\$rfc1149 != 1149) {
320 printf "not ok 21 # %d != 1149\n", \$rfc1149;
321} else {
322 print "ok 21\n";
323}
324EOT
94b1a389 325close FH or die "close $testpl: $!\n";
af6c647e 326
835f860c 327################ Makefile.PL
6d79cad2 328# We really need a Makefile.PL because make test for a no dynamic linking perl
329# will run Makefile.PL again as part of the "make perl" target.
94b1a389 330my $makefilePL = catfile($dir, "Makefile.PL");
0ddb8edc 331push @files, "Makefile.PL";
94b1a389 332open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
835f860c 333print FH <<"EOT";
6d79cad2 334#!$perl -w
835f860c 335use ExtUtils::MakeMaker;
336WriteMakefile(
337 'NAME' => "$package",
338 'VERSION_FROM' => "$package.pm", # finds \$VERSION
339 (\$] >= 5.005 ?
340 (#ABSTRACT_FROM => "$package.pm", # XXX add this
341 AUTHOR => "$0") : ())
342 );
343EOT
344
94b1a389 345close FH or die "close $makefilePL: $!\n";
af6c647e 346
347chdir $dir or die $!; push @INC, '../../lib';
348END {chdir ".." or warn $!};
349
835f860c 350my @perlout = `$runperl Makefile.PL`;
351if ($?) {
352 print "not ok 1 # $runperl Makefile.PL failed: $?\n";
353 print "# $_" foreach @perlout;
354 exit($?);
355} else {
356 print "ok 1\n";
357}
358
359
24874030 360my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
361my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
362if (-f "$makefile$makefile_ext") {
835f860c 363 print "ok 2\n";
af6c647e 364} else {
835f860c 365 print "not ok 2\n";
af6c647e 366}
24874030 367my $makefile_rename = ($^O eq 'VMS' ? '.mms' : '.old');
368push @files, "$makefile$makefile_rename"; # Renamed by make clean
af6c647e 369
370my $make = $Config{make};
94b1a389 371
af6c647e 372$make = $ENV{MAKE} if exists $ENV{MAKE};
94b1a389 373
374my $makeout;
375
af6c647e 376print "# make = '$make'\n";
94b1a389 377$makeout = `$make`;
378if ($?) {
835f860c 379 print "not ok 3 # $make failed: $?\n";
94b1a389 380 exit($?);
af6c647e 381} else {
835f860c 382 print "ok 3\n";
383}
384
385if ($Config{usedl}) {
386 print "ok 4\n";
387} else {
388 push @files, "perl$Config{exe_ext}";
389 my $makeperl = "$make perl";
390 print "# make = '$makeperl'\n";
391 $makeout = `$makeperl`;
392 if ($?) {
393 print "not ok 4 # $makeperl failed: $?\n";
394 exit($?);
395 } else {
396 print "ok 4\n";
397 }
af6c647e 398}
399
cea00dc5 400my $test = 22;
0ddb8edc 401my $maketest = "$make test";
402print "# make = '$maketest'\n";
403$makeout = `$maketest`;
94b1a389 404
3414cef0 405# echo of running the test script
406$makeout =~ s/^\s*PERL_DL_NONLAZY=.+?\n//m;
407$makeout =~ s/^MCR.+test.pl\n//mig if $^O eq 'VMS';
408
409# GNU make babblings
410$makeout =~ s/^\w*?make.+?(?:entering|leaving) directory.+?\n//mig;
94b1a389 411
3414cef0 412# Hopefully gets most make's babblings
413# make -f Makefile.aperl perl
414$makeout =~ s/^\w*?make.+\sperl[^A-Za-z0-9]*\n//mig;
415# make[1]: `perl' is up to date.
416$makeout =~ s/^\w*?make.+perl.+?is up to date.*?\n//mig;
835f860c 417
3414cef0 418print $makeout;
419
420if ($?) {
421 print "not ok $test # $maketest failed: $?\n";
422} else {
6d79cad2 423 print "ok $test\n";
424}
425$test++;
426
427my $regen = `$runperl $package.xs`;
428if ($?) {
429 print "not ok $test # $runperl $package.xs failed: $?\n";
430} else {
431 print "ok $test\n";
af6c647e 432}
6d79cad2 433$test++;
434
435my $expect = $constant_types . $C_constant .
436 "\n#### XS Section:\n" . $XS_constant;
437
438if ($expect eq $regen) {
439 print "ok $test\n";
440} else {
441 print "not ok $test\n";
442 # open FOO, ">expect"; print FOO $expect;
443 # open FOO, ">regen"; print FOO $regen; close FOO;
444}
445$test++;
0ddb8edc 446
447my $makeclean = "$make clean";
448print "# make = '$makeclean'\n";
449$makeout = `$makeclean`;
450if ($?) {
6d79cad2 451 print "not ok $test # $make failed: $?\n";
0ddb8edc 452} else {
6d79cad2 453 print "ok $test\n";
0ddb8edc 454}
6d79cad2 455$test++;
0ddb8edc 456
457foreach (@files) {
458 unlink $_ or warn "unlink $_: $!";
459}
460
461my $fail;
462opendir DIR, "." or die "opendir '.': $!";
463while (defined (my $entry = readdir DIR)) {
464 next if $entry =~ /^\.\.?$/;
465 print "# Extra file '$entry'\n";
466 $fail = 1;
467}
468closedir DIR or warn "closedir '.': $!";
469if ($fail) {
6d79cad2 470 print "not ok $test\n";
0ddb8edc 471} else {
6d79cad2 472 print "ok $test\n";
0ddb8edc 473}