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