Integrate #11263 from macperl; macos and macosx updates.
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils.t
1 #!./perl -w
2
3 print "1..27\n";
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = '../lib';
8 }
9
10 # use warnings;
11 use strict;
12 use ExtUtils::MakeMaker;
13 use ExtUtils::Constant qw (constant_types C_constant XS_constant autoload);
14 use Config;
15 use File::Spec::Functions;
16 use File::Spec;
17 # Because were are going to be changing directory before running Makefile.PL
18 my $perl;
19 $perl = rel2abs( $^X ) unless $] < 5.006; # Hack. Until 5.00503 has rel2abs
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;
25
26 print "# perl=$perl\n";
27 my $runperl = "$perl -x \"-I../../lib\"";
28
29 $| = 1;
30
31 my $dir = "ext-$$";
32 my @files;
33
34 print "# $dir being created...\n";
35 mkdir $dir, 0777 or die "mkdir: $!\n";
36
37 my $output = "output";
38
39 END {
40     use File::Path;
41     print "# $dir being removed...\n";
42     rmtree($dir);
43 }
44
45 my $package = "ExtTest";
46
47 # Test the code that generates 1 and 2 letter name comparisons.
48 my %compass = (
49 N => 0, 'NE' => 45, E => 90, SE => 135, S => 180, SW => 225, W => 270, NW => 315
50 );
51
52 my $parent_rfc1149 =
53   'A Standard for the Transmission of IP Datagrams on Avian Carriers';
54
55 my @names = ("FIVE", {name=>"OK6", type=>"PV",},
56              {name=>"OK7", type=>"PVN",
57               value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
58              {name => "FARTHING", type=>"NV"},
59              {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"},
60              {name => "OPEN", type=>"PV", value=>'"/*"', macro=>1},
61              {name => "CLOSE", type=>"PV", value=>'"*/"',
62               macro=>["#if 1\n", "#endif\n"]},
63              {name => "ANSWER", default=>["UV", 42]}, "NOTDEF",
64              {name => "Yes", type=>"YES"},
65              {name => "No", type=>"NO"},
66              {name => "Undef", type=>"UNDEF"},
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;"},
73 );
74
75 push @names, $_ foreach keys %compass;
76
77 my @names_only = map {(ref $_) ? $_->{name} : $_} @names;
78
79 my $types = {};
80 my $constant_types = constant_types(); # macro defs
81 my $C_constant = join "\n",
82   C_constant ($package, undef, "IV", $types, undef, undef, @names);
83 my $XS_constant = XS_constant ($package, $types); # XS for ExtTest::constant
84
85 ################ Header
86 my $header = catfile($dir, "test.h");
87 push @files, "test.h";
88 open FH, ">$header" or die "open >$header: $!\n";
89 print FH <<"EOT";
90 #define FIVE 5
91 #define OK6 "ok 6\\n"
92 #define OK7 1
93 #define FARTHING 0.25
94 #define NOT_ZERO 1
95 #define Yes 0
96 #define No 1
97 #define Undef 1
98 #define RFC1149 "$parent_rfc1149"
99 #undef NOTDEF
100
101 EOT
102
103 while (my ($point, $bearing) = each %compass) {
104   print FH "#define $point $bearing\n"
105 }
106 close FH or die "close $header: $!\n";
107
108 ################ XS
109 my $xs = catfile($dir, "$package.xs");
110 push @files, "$package.xs";
111 open FH, ">$xs" or die "open >$xs: $!\n";
112
113 print FH <<'EOT';
114 #include "EXTERN.h"
115 #include "perl.h"
116 #include "XSUB.h"
117 EOT
118
119 print FH "#include \"test.h\"\n\n";
120 print FH $constant_types;
121 print FH $C_constant, "\n";
122 print FH "MODULE = $package             PACKAGE = $package\n";
123 print FH "PROTOTYPES: ENABLE\n";
124 print FH $XS_constant;
125 close FH or die "close $xs: $!\n";
126
127 ################ PM
128 my $pm = catfile($dir, "$package.pm");
129 push @files, "$package.pm";
130 open FH, ">$pm" or die "open >$pm: $!\n";
131 print FH "package $package;\n";
132 print FH "use $];\n";
133
134 print FH <<'EOT';
135
136 use strict;
137 EOT
138 printf FH "use warnings;\n" unless $] < 5.006;
139 print FH <<'EOT';
140 use Carp;
141
142 require Exporter;
143 require DynaLoader;
144 use vars qw ($VERSION @ISA @EXPORT_OK $AUTOLOAD);
145
146 $VERSION = '0.01';
147 @ISA = qw(Exporter DynaLoader);
148 @EXPORT_OK = qw(
149 EOT
150
151 print FH "\t$_\n" foreach (@names_only);
152 print FH ");\n";
153 print FH autoload ($package, $]);
154 print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
155 close FH or die "close $pm: $!\n";
156
157 ################ test.pl
158 my $testpl = catfile($dir, "test.pl");
159 push @files, "test.pl";
160 open FH, ">$testpl" or die "open >$testpl: $!\n";
161
162 print FH "use strict;\n";
163 print FH "use $package qw(@names_only);\n";
164 print FH <<"EOT";
165
166 print "1..1\n";
167 if (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 }
174 EOT
175
176 print FH << 'EOT';
177
178 # What follows goes to the temporary file.
179 # IV
180 my $five = FIVE;
181 if ($five == 5) {
182   print "ok 5\n";
183 } else {
184   print "not ok 5 # $five\n";
185 }
186
187 # PV
188 print OK6;
189
190 # PVN containing embedded \0s
191 $_ = OK7;
192 s/.*\0//s;
193 print;
194
195 # NV
196 my $farthing = FARTHING;
197 if ($farthing == 0.25) {
198   print "ok 8\n";
199 } else {
200   print "not ok 8 # $farthing\n";
201 }
202
203 # UV
204 my $not_zero = NOT_ZERO;
205 if ($not_zero > 0 && $not_zero == ~0) {
206   print "ok 9\n";
207 } else {
208   print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
209 }
210
211 # Value includes a "*/" in an attempt to bust out of a C comment.
212 # Also tests custom cpp #if clauses
213 my $close = CLOSE;
214 if ($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.
221 my $answer = ANSWER;
222 if ($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
229 my $notdef = eval { NOTDEF; };
230 if (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
239 my $notthere = eval { &ExtTest::NOTTHERE; };
240 if (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 }
248
249 # Truth
250 my $yes = Yes;
251 if ($yes) {
252   print "ok 14\n";
253 } else {
254   print "not ok 14 # $yes='\$yes'\n";
255 }
256
257 # Falsehood
258 my $no = No;
259 if (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
266 my $undef = Undef;
267 unless (defined $undef) {
268   print "ok 16\n";
269 } else {
270   print "not ok 16 # \$undef='$undef'\n";
271 }
272
273
274 # invalid macro (chosen to look like a mix up between No and SW)
275 $notdef = eval { &ExtTest::So };
276 if (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 };
286 if (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
294 my %compass = (
295 EOT
296
297 while (my ($point, $bearing) = each %compass) {
298   print FH "'$point' => $bearing, "
299 }
300
301 print FH <<'EOT';
302
303 );
304
305 my $fail;
306 while (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 }
319 if ($fail) {
320   print "not ok 19\n";
321 } else {
322   print "ok 19\n";
323 }
324
325 EOT
326
327 print FH <<"EOT";
328 my \$rfc1149 = RFC1149;
329 if (\$rfc1149 ne "$parent_rfc1149") {
330   print "not ok 20 # '\$rfc1149' ne '$parent_rfc1149'\n";
331 } else {
332   print "ok 20\n";
333 }
334
335 if (\$rfc1149 != 1149) {
336   printf "not ok 21 # %d != 1149\n", \$rfc1149;
337 } else {
338   print "ok 21\n";
339 }
340
341 EOT
342
343 print FH <<'EOT';
344 # test macro=>1
345 my $open = OPEN;
346 if ($open eq '/*') {
347   print "ok 22\n";
348 } else {
349   print "not ok 22 # \$open='$open'\n";
350 }
351 EOT
352 close FH or die "close $testpl: $!\n";
353
354 ################ Makefile.PL
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.
357 my $makefilePL = catfile($dir, "Makefile.PL");
358 push @files, "Makefile.PL";
359 open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
360 print FH <<"EOT";
361 #!$perl -w
362 use ExtUtils::MakeMaker;
363 WriteMakefile(
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              );
370 EOT
371
372 close FH or die "close $makefilePL: $!\n";
373
374 chdir $dir or die $!; push @INC,  '../../lib';
375 END {chdir ".." or warn $!};
376
377 my @perlout = `$runperl Makefile.PL PERL_CORE=1`;
378 if ($?) {
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
387 my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
388 my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
389 if (-f "$makefile$makefile_ext") {
390   print "ok 2\n";
391 } else {
392   print "not ok 2\n";
393 }
394 my $makefile_rename = ($^O eq 'VMS' ? '.mms' : '.old');
395 push @files, "$makefile$makefile_rename"; # Renamed by make clean
396
397 my $make = $Config{make};
398
399 $make = $ENV{MAKE} if exists $ENV{MAKE};
400
401 if ($^O eq 'MSWin32' && $make eq 'nmake') { $make .= " -nologo"; }
402
403 my @makeout;
404
405 print "# make = '$make'\n";
406 @makeout = `$make`;
407 if ($?) {
408   print "not ok 3 # $make failed: $?\n";
409   print "# $_" foreach @makeout;
410   exit($?);
411 } else {
412   print "ok 3\n";
413 }
414
415 if ($Config{usedl}) {
416   print "ok 4\n";
417 } else {
418   my $makeperl = "$make perl";
419   print "# make = '$makeperl'\n";
420   @makeout = `$makeperl`;
421   if ($?) {
422     print "not ok 4 # $makeperl failed: $?\n";
423   print "# $_" foreach @makeout;
424     exit($?);
425   } else {
426     print "ok 4\n";
427   }
428 }
429
430 push @files, $output;
431
432 my $maketest = "$make test";
433 print "# make = '$maketest'\n";
434
435 @makeout = `$maketest`;
436
437 if (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 }
444
445 my $test = 23;
446
447 if ($?) {
448   print "not ok $test # $maketest failed: $?\n";
449   print "# $_" foreach @makeout;
450 } else {
451   print "ok $test\n";
452 }
453 $test++;
454
455 my $regen = `$runperl $package.xs`;
456 if ($?) {
457   print "not ok $test # $runperl $package.xs failed: $?\n";
458 } else {
459   print "ok $test\n";
460 }
461 $test++;
462
463 my $expect = $constant_types . $C_constant .
464   "\n#### XS Section:\n" . $XS_constant;
465
466 if ($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++;
474
475 my $makeclean = "$make clean";
476 print "# make = '$makeclean'\n";
477 @makeout = `$makeclean`;
478 if ($?) {
479   print "not ok $test # $make failed: $?\n";
480   print "# $_" foreach @makeout;
481 } else {
482   print "ok $test\n";
483 }
484 $test++;
485
486 foreach (@files) {
487   unlink $_ or warn "unlink $_: $!";
488 }
489
490 my $fail;
491 opendir DIR, "." or die "opendir '.': $!";
492 while (defined (my $entry = readdir DIR)) {
493   next if $entry =~ /^\.\.?$/;
494   print "# Extra file '$entry'\n";
495   $fail = 1;
496 }
497 closedir DIR or warn "closedir '.': $!";
498 if ($fail) {
499   print "not ok $test\n";
500 } else {
501   print "ok $test\n";
502 }