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