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