More VMS tweaks from Craig A. Berry.
[p5sagit/p5-mst-13.2.git] / t / lib / extutils.t
1 #!./perl -w
2
3 print "1..12\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
20 print "# perl=$perl\n";
21 my $runperl = "$perl \"-I../../lib\"";
22
23 $| = 1;
24
25 my $dir = "ext-$$";
26 my @files;
27
28 print "# $dir being created...\n";
29 mkdir $dir, 0777 or die "mkdir: $!\n";
30
31
32 END {
33     use File::Path;
34     print "# $dir being removed...\n";
35     rmtree($dir);
36 }
37
38 my @names = ("FIVE", {name=>"OK6", type=>"PV",},
39              {name=>"OK7", type=>"PVN",
40               value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
41              {name => "FARTHING", type=>"NV"},
42              {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"});
43
44 my @names_only = map {(ref $_) ? $_->{name} : $_} @names;
45
46 my $package = "ExtTest";
47 ################ Header
48 my $header = catfile($dir, "test.h");
49 push @files, "test.h";
50 open FH, ">$header" or die "open >$header: $!\n";
51 print FH <<'EOT';
52 #define FIVE 5
53 #define OK6 "ok 6\n"
54 #define OK7 1
55 #define FARTHING 0.25
56 #define NOT_ZERO 1
57 EOT
58 close FH or die "close $header: $!\n";
59
60 ################ XS
61 my $xs = catfile($dir, "$package.xs");
62 push @files, "$package.xs";
63 open FH, ">$xs" or die "open >$xs: $!\n";
64
65 print FH <<'EOT';
66 #include "EXTERN.h"
67 #include "perl.h"
68 #include "XSUB.h"
69 EOT
70
71 print FH "#include \"test.h\"\n\n";
72 print FH constant_types(); # macro defs
73 my $types = {};
74 foreach (C_constant (undef, "IV", $types, undef, undef, @names) ) {
75   print FH $_, "\n"; # C constant subs
76 }
77 print FH "MODULE = $package             PACKAGE = $package\n";
78 print FH "PROTOTYPES: ENABLE\n";
79 print FH XS_constant ($package, $types); # XS for ExtTest::constant
80 close FH or die "close $xs: $!\n";
81
82 ################ PM
83 my $pm = catfile($dir, "$package.pm");
84 push @files, "$package.pm";
85 open FH, ">$pm" or die "open >$pm: $!\n";
86 print FH "package $package;\n";
87 print FH "use $];\n";
88
89 print FH <<'EOT';
90
91 use strict;
92 use warnings;
93 use Carp;
94
95 require Exporter;
96 require DynaLoader;
97 use AutoLoader;
98 use vars qw ($VERSION @ISA @EXPORT_OK);
99
100 $VERSION = '0.01';
101 @ISA = qw(Exporter DynaLoader);
102 @EXPORT_OK = qw(
103 EOT
104
105 print FH "\t$_\n" foreach (@names_only);
106 print FH ");\n";
107 print FH autoload ($package, $]);
108 print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
109 close FH or die "close $pm: $!\n";
110
111 ################ test.pl
112 my $testpl = catfile($dir, "test.pl");
113 push @files, "test.pl";
114 open FH, ">$testpl" or die "open >$testpl: $!\n";
115
116 print FH "use $package qw(@names_only);\n";
117 print FH <<'EOT';
118
119 my $five = FIVE;
120 if ($five == 5) {
121   print "ok 5\n";
122 } else {
123   print "not ok 5 # $five\n";
124 }
125
126 print OK6;
127
128 $_ = OK7;
129 s/.*\0//s;
130 print;
131
132 my $farthing = FARTHING;
133 if ($farthing == 0.25) {
134   print "ok 8\n";
135 } else {
136   print "not ok 8 # $farthing\n";
137 }
138
139 my $not_zero = NOT_ZERO;
140 if ($not_zero > 0 && $not_zero == ~0) {
141   print "ok 9\n";
142 } else {
143   print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
144 }
145
146
147 EOT
148
149 close FH or die "close $testpl: $!\n";
150
151 ################ Makefile.PL
152 # Keep the dependancy in the Makefile happy
153 my $makefilePL = catfile($dir, "Makefile.PL");
154 push @files, "Makefile.PL";
155 open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
156 print FH <<"EOT";
157 use ExtUtils::MakeMaker;
158 WriteMakefile(
159               'NAME'            => "$package",
160               'VERSION_FROM'    => "$package.pm", # finds \$VERSION
161               (\$] >= 5.005 ?
162                (#ABSTRACT_FROM => "$package.pm", # XXX add this
163                 AUTHOR     => "$0") : ())
164              );
165 EOT
166
167 close FH or die "close $makefilePL: $!\n";
168
169 chdir $dir or die $!; push @INC,  '../../lib';
170 END {chdir ".." or warn $!};
171
172 my @perlout = `$runperl Makefile.PL`;
173 if ($?) {
174   print "not ok 1 # $runperl Makefile.PL failed: $?\n";
175   print "# $_" foreach @perlout;
176   exit($?);
177 } else {
178   print "ok 1\n";
179 }
180
181
182 my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
183 my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
184 if (-f "$makefile$makefile_ext") {
185   print "ok 2\n";
186 } else {
187   print "not ok 2\n";
188 }
189 my $makefile_rename = ($^O eq 'VMS' ? '.mms' : '.old');
190 push @files, "$makefile$makefile_rename"; # Renamed by make clean
191
192 my $make = $Config{make};
193
194 $make = $ENV{MAKE} if exists $ENV{MAKE};
195
196 my $makeout;
197
198 print "# make = '$make'\n";
199 $makeout = `$make`;
200 if ($?) {
201   print "not ok 3 # $make failed: $?\n";
202   exit($?);
203 } else {
204   print "ok 3\n";
205 }
206
207 if ($Config{usedl}) {
208   print "ok 4\n";
209 } else {
210   push @files, "perl$Config{exe_ext}";
211   my $makeperl = "$make perl";
212   print "# make = '$makeperl'\n";
213   $makeout = `$makeperl`;
214   if ($?) {
215     print "not ok 4 # $makeperl failed: $?\n";
216     exit($?);
217   } else {
218     print "ok 4\n";
219   }
220 }
221
222 my $maketest = "$make test";
223 print "# make = '$maketest'\n";
224 $makeout = `$maketest`;
225 if ($?) {
226   print "not ok 10 # $maketest failed: $?\n";
227 } else {
228   # Perl babblings
229   $makeout =~ s/^\s*PERL_DL_NONLAZY=.+?\n//m;
230
231   # GNU make babblings
232   $makeout =~ s/^\w*?make.+?(?:entering|leaving) directory.+?\n//mig;
233
234   # Hopefully gets most make's babblings
235   # make -f Makefile.aperl perl
236   $makeout =~ s/^\w*?make.+\sperl[^A-Za-z0-9]*\n//mig;
237   # make[1]: `perl' is up to date.
238   $makeout =~ s/^\w*?make.+perl.+?is up to date.*?\n//mig;
239
240   # echo of running the test script
241   $makeout =~ s/^MCR.+test.pl\n//mig if $^O eq 'VMS';
242
243   print $makeout;
244   print "ok 10\n";
245 }
246
247 my $makeclean = "$make clean";
248 print "# make = '$makeclean'\n";
249 $makeout = `$makeclean`;
250 if ($?) {
251   print "not ok 11 # $make failed: $?\n";
252 } else {
253   print "ok 11\n";
254 }
255
256 foreach (@files) {
257   unlink $_ or warn "unlink $_: $!";
258 }
259
260 my $fail;
261 opendir DIR, "." or die "opendir '.': $!";
262 while (defined (my $entry = readdir DIR)) {
263   next if $entry =~ /^\.\.?$/;
264   print "# Extra file '$entry'\n";
265   $fail = 1;
266 }
267 closedir DIR or warn "closedir '.': $!";
268 if ($fail) {
269   print "not ok 12\n";
270 } else {
271   print "ok 12\n";
272 }