Upgrade to Attribute::Handlers 0.70.
[p5sagit/p5-mst-13.2.git] / t / lib / extutils.t
CommitLineData
af6c647e 1#!./perl -w
2
6d79cad2 3print "1..18\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
835f860c 45my @names = ("FIVE", {name=>"OK6", type=>"PV",},
46 {name=>"OK7", type=>"PVN",
47 value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
af6c647e 48 {name => "FARTHING", type=>"NV"},
6d79cad2 49 {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"},
50 {name => "CLOSE", type=>"PV", value=>'"*/"',
51 macro=>["#if 1\n", "#endif\n"]},
52 {name => "ANSWER", default=>["UV", 42]}, "NOTDEF");
af6c647e 53
54my @names_only = map {(ref $_) ? $_->{name} : $_} @names;
55
6d79cad2 56my $types = {};
57my $constant_types = constant_types(); # macro defs
58my $C_constant = join "\n",
59 C_constant ($package, undef, "IV", $types, undef, undef, @names);
60my $XS_constant = XS_constant ($package, $types); # XS for ExtTest::constant
61
af6c647e 62################ Header
94b1a389 63my $header = catfile($dir, "test.h");
0ddb8edc 64push @files, "test.h";
94b1a389 65open FH, ">$header" or die "open >$header: $!\n";
af6c647e 66print FH <<'EOT';
835f860c 67#define FIVE 5
68#define OK6 "ok 6\n"
69#define OK7 1
af6c647e 70#define FARTHING 0.25
71#define NOT_ZERO 1
6d79cad2 72#undef NOTDEF
af6c647e 73EOT
94b1a389 74close FH or die "close $header: $!\n";
af6c647e 75
76################ XS
94b1a389 77my $xs = catfile($dir, "$package.xs");
0ddb8edc 78push @files, "$package.xs";
94b1a389 79open FH, ">$xs" or die "open >$xs: $!\n";
af6c647e 80
81print FH <<'EOT';
82#include "EXTERN.h"
83#include "perl.h"
84#include "XSUB.h"
85EOT
86
87print FH "#include \"test.h\"\n\n";
6d79cad2 88print FH $constant_types;
89print FH $C_constant, "\n";
af6c647e 90print FH "MODULE = $package PACKAGE = $package\n";
91print FH "PROTOTYPES: ENABLE\n";
6d79cad2 92print FH $XS_constant;
94b1a389 93close FH or die "close $xs: $!\n";
af6c647e 94
95################ PM
94b1a389 96my $pm = catfile($dir, "$package.pm");
0ddb8edc 97push @files, "$package.pm";
94b1a389 98open FH, ">$pm" or die "open >$pm: $!\n";
af6c647e 99print FH "package $package;\n";
100print FH "use $];\n";
101
102print FH <<'EOT';
103
104use strict;
105use warnings;
106use Carp;
107
108require Exporter;
109require DynaLoader;
af6c647e 110use vars qw ($VERSION @ISA @EXPORT_OK);
111
112$VERSION = '0.01';
113@ISA = qw(Exporter DynaLoader);
114@EXPORT_OK = qw(
115EOT
116
117print FH "\t$_\n" foreach (@names_only);
118print FH ");\n";
119print FH autoload ($package, $]);
120print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
94b1a389 121close FH or die "close $pm: $!\n";
af6c647e 122
123################ test.pl
94b1a389 124my $testpl = catfile($dir, "test.pl");
0ddb8edc 125push @files, "test.pl";
94b1a389 126open FH, ">$testpl" or die "open >$testpl: $!\n";
af6c647e 127
6d79cad2 128print FH "use strict;\n";
af6c647e 129print FH "use $package qw(@names_only);\n";
130print FH <<'EOT';
131
6d79cad2 132# IV
835f860c 133my $five = FIVE;
134if ($five == 5) {
135 print "ok 5\n";
af6c647e 136} else {
835f860c 137 print "not ok 5 # $five\n";
af6c647e 138}
139
6d79cad2 140# PV
835f860c 141print OK6;
af6c647e 142
6d79cad2 143# PVN containing embedded \0s
835f860c 144$_ = OK7;
af6c647e 145s/.*\0//s;
146print;
147
6d79cad2 148# NV
af6c647e 149my $farthing = FARTHING;
150if ($farthing == 0.25) {
835f860c 151 print "ok 8\n";
af6c647e 152} else {
835f860c 153 print "not ok 8 # $farthing\n";
af6c647e 154}
155
6d79cad2 156# UV
af6c647e 157my $not_zero = NOT_ZERO;
158if ($not_zero > 0 && $not_zero == ~0) {
835f860c 159 print "ok 9\n";
af6c647e 160} else {
835f860c 161 print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
af6c647e 162}
163
6d79cad2 164# Value includes a "*/" in an attempt to bust out of a C comment.
165# Also tests custom cpp #if clauses
166my $close = CLOSE;
167if ($close eq '*/') {
168 print "ok 10\n";
169} else {
170 print "not ok 10 # \$close='$close'\n";
171}
172
173# Default values if macro not defined.
174my $answer = ANSWER;
175if ($answer == 42) {
176 print "ok 11\n";
177} else {
178 print "not ok 11 # What do you get if you multiply six by nine? '$answer'\n";
179}
180
181# not defined macro
182my $notdef = eval { NOTDEF; };
183if (defined $notdef) {
184 print "not ok 12 # \$notdef='$notdef'\n";
185} elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
186 print "not ok 12 # \$@='$@'\n";
187} else {
188 print "ok 12\n";
189}
190
191# not a macro
192my $notthere = eval { &ExtTest::NOTTHERE; };
193if (defined $notthere) {
194 print "not ok 13 # \$notthere='$notthere'\n";
195} elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
196 chomp $@;
197 print "not ok 13 # \$@='$@'\n";
198} else {
199 print "ok 13\n";
200}
af6c647e 201
202EOT
203
94b1a389 204close FH or die "close $testpl: $!\n";
af6c647e 205
835f860c 206################ Makefile.PL
6d79cad2 207# We really need a Makefile.PL because make test for a no dynamic linking perl
208# will run Makefile.PL again as part of the "make perl" target.
94b1a389 209my $makefilePL = catfile($dir, "Makefile.PL");
0ddb8edc 210push @files, "Makefile.PL";
94b1a389 211open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
835f860c 212print FH <<"EOT";
6d79cad2 213#!$perl -w
835f860c 214use ExtUtils::MakeMaker;
215WriteMakefile(
216 'NAME' => "$package",
217 'VERSION_FROM' => "$package.pm", # finds \$VERSION
218 (\$] >= 5.005 ?
219 (#ABSTRACT_FROM => "$package.pm", # XXX add this
220 AUTHOR => "$0") : ())
221 );
222EOT
223
94b1a389 224close FH or die "close $makefilePL: $!\n";
af6c647e 225
226chdir $dir or die $!; push @INC, '../../lib';
227END {chdir ".." or warn $!};
228
835f860c 229my @perlout = `$runperl Makefile.PL`;
230if ($?) {
231 print "not ok 1 # $runperl Makefile.PL failed: $?\n";
232 print "# $_" foreach @perlout;
233 exit($?);
234} else {
235 print "ok 1\n";
236}
237
238
24874030 239my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
240my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
241if (-f "$makefile$makefile_ext") {
835f860c 242 print "ok 2\n";
af6c647e 243} else {
835f860c 244 print "not ok 2\n";
af6c647e 245}
24874030 246my $makefile_rename = ($^O eq 'VMS' ? '.mms' : '.old');
247push @files, "$makefile$makefile_rename"; # Renamed by make clean
af6c647e 248
249my $make = $Config{make};
94b1a389 250
af6c647e 251$make = $ENV{MAKE} if exists $ENV{MAKE};
94b1a389 252
253my $makeout;
254
af6c647e 255print "# make = '$make'\n";
94b1a389 256$makeout = `$make`;
257if ($?) {
835f860c 258 print "not ok 3 # $make failed: $?\n";
94b1a389 259 exit($?);
af6c647e 260} else {
835f860c 261 print "ok 3\n";
262}
263
264if ($Config{usedl}) {
265 print "ok 4\n";
266} else {
267 push @files, "perl$Config{exe_ext}";
268 my $makeperl = "$make perl";
269 print "# make = '$makeperl'\n";
270 $makeout = `$makeperl`;
271 if ($?) {
272 print "not ok 4 # $makeperl failed: $?\n";
273 exit($?);
274 } else {
275 print "ok 4\n";
276 }
af6c647e 277}
278
6d79cad2 279my $test = 14;
0ddb8edc 280my $maketest = "$make test";
281print "# make = '$maketest'\n";
282$makeout = `$maketest`;
94b1a389 283if ($?) {
6d79cad2 284 print "not ok $test # $maketest failed: $?\n";
af6c647e 285} else {
6d79cad2 286 # echo of running the test script
737e87b0 287 $makeout =~ s/^\s*PERL_DL_NONLAZY=.+?\n//m;
6d79cad2 288 $makeout =~ s/^MCR.+test.pl\n//mig if $^O eq 'VMS';
94b1a389 289
290 # GNU make babblings
291 $makeout =~ s/^\w*?make.+?(?:entering|leaving) directory.+?\n//mig;
292
835f860c 293 # Hopefully gets most make's babblings
294 # make -f Makefile.aperl perl
295 $makeout =~ s/^\w*?make.+\sperl[^A-Za-z0-9]*\n//mig;
296 # make[1]: `perl' is up to date.
297 $makeout =~ s/^\w*?make.+perl.+?is up to date.*?\n//mig;
298
94b1a389 299 print $makeout;
6d79cad2 300 print "ok $test\n";
301}
302$test++;
303
304my $regen = `$runperl $package.xs`;
305if ($?) {
306 print "not ok $test # $runperl $package.xs failed: $?\n";
307} else {
308 print "ok $test\n";
af6c647e 309}
6d79cad2 310$test++;
311
312my $expect = $constant_types . $C_constant .
313 "\n#### XS Section:\n" . $XS_constant;
314
315if ($expect eq $regen) {
316 print "ok $test\n";
317} else {
318 print "not ok $test\n";
319 # open FOO, ">expect"; print FOO $expect;
320 # open FOO, ">regen"; print FOO $regen; close FOO;
321}
322$test++;
0ddb8edc 323
324my $makeclean = "$make clean";
325print "# make = '$makeclean'\n";
326$makeout = `$makeclean`;
327if ($?) {
6d79cad2 328 print "not ok $test # $make failed: $?\n";
0ddb8edc 329} else {
6d79cad2 330 print "ok $test\n";
0ddb8edc 331}
6d79cad2 332$test++;
0ddb8edc 333
334foreach (@files) {
335 unlink $_ or warn "unlink $_: $!";
336}
337
338my $fail;
339opendir DIR, "." or die "opendir '.': $!";
340while (defined (my $entry = readdir DIR)) {
341 next if $entry =~ /^\.\.?$/;
342 print "# Extra file '$entry'\n";
343 $fail = 1;
344}
345closedir DIR or warn "closedir '.': $!";
346if ($fail) {
6d79cad2 347 print "not ok $test\n";
0ddb8edc 348} else {
6d79cad2 349 print "ok $test\n";
0ddb8edc 350}