summaryrefslogtreecommitdiff
path: root/t/lib/extutils.t
blob: 759d7616b00d551105bca050a6cef07848b44c23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!./perl -w

print "1..12\n";

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
}

use warnings;
use strict;
use ExtUtils::MakeMaker;
use ExtUtils::Constant qw (constant_types C_constant XS_constant autoload);
use Config;
use File::Spec::Functions;
use File::Spec;
# Because were are going to be changing directory before running Makefile.PL
my $perl = File::Spec->rel2abs( $^X );

print "# perl=$perl\n";
my $runperl = "$perl \"-I../../lib\"";

$| = 1;

my $dir = "ext-$$";
my @files;

print "# $dir being created...\n";
mkdir $dir, 0777 or die "mkdir: $!\n";


END {
    use File::Path;
    print "# $dir being removed...\n";
    rmtree($dir);
}

my @names = ("FIVE", {name=>"OK6", type=>"PV",},
             {name=>"OK7", type=>"PVN",
              value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
             {name => "FARTHING", type=>"NV"},
             {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"});

my @names_only = map {(ref $_) ? $_->{name} : $_} @names;

my $package = "ExtTest";
################ Header
my $header = catfile($dir, "test.h");
push @files, "test.h";
open FH, ">$header" or die "open >$header: $!\n";
print FH <<'EOT';
#define FIVE 5
#define OK6 "ok 6\n"
#define OK7 1
#define FARTHING 0.25
#define NOT_ZERO 1
EOT
close FH or die "close $header: $!\n";

################ XS
my $xs = catfile($dir, "$package.xs");
push @files, "$package.xs";
open FH, ">$xs" or die "open >$xs: $!\n";

print FH <<'EOT';
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
EOT

print FH "#include \"test.h\"\n\n";
print FH constant_types(); # macro defs
my $types = {};
foreach (C_constant (undef, "IV", $types, undef, undef, @names) ) {
  print FH $_, "\n"; # C constant subs
}
print FH "MODULE = $package		PACKAGE = $package\n";
print FH "PROTOTYPES: ENABLE\n";
print FH XS_constant ($package, $types); # XS for ExtTest::constant
close FH or die "close $xs: $!\n";

################ PM
my $pm = catfile($dir, "$package.pm");
push @files, "$package.pm";
open FH, ">$pm" or die "open >$pm: $!\n";
print FH "package $package;\n";
print FH "use $];\n";

print FH <<'EOT';

use strict;
use warnings;
use Carp;

require Exporter;
require DynaLoader;
use AutoLoader;
use vars qw ($VERSION @ISA @EXPORT_OK);

$VERSION = '0.01';
@ISA = qw(Exporter DynaLoader);
@EXPORT_OK = qw(
EOT

print FH "\t$_\n" foreach (@names_only);
print FH ");\n";
print FH autoload ($package, $]);
print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
close FH or die "close $pm: $!\n";

################ test.pl
my $testpl = catfile($dir, "test.pl");
push @files, "test.pl";
open FH, ">$testpl" or die "open >$testpl: $!\n";

print FH "use $package qw(@names_only);\n";
print FH <<'EOT';

my $five = FIVE;
if ($five == 5) {
  print "ok 5\n";
} else {
  print "not ok 5 # $five\n";
}

print OK6;

$_ = OK7;
s/.*\0//s;
print;

my $farthing = FARTHING;
if ($farthing == 0.25) {
  print "ok 8\n";
} else {
  print "not ok 8 # $farthing\n";
}

my $not_zero = NOT_ZERO;
if ($not_zero > 0 && $not_zero == ~0) {
  print "ok 9\n";
} else {
  print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
}


EOT

close FH or die "close $testpl: $!\n";

################ Makefile.PL
# Keep the dependancy in the Makefile happy
my $makefilePL = catfile($dir, "Makefile.PL");
push @files, "Makefile.PL";
open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
print FH <<"EOT";
use ExtUtils::MakeMaker;
WriteMakefile(
              'NAME'		=> "$package",
              'VERSION_FROM'	=> "$package.pm", # finds \$VERSION
              (\$] >= 5.005 ?
               (#ABSTRACT_FROM => "$package.pm", # XXX add this
                AUTHOR     => "$0") : ())
             );
EOT

close FH or die "close $makefilePL: $!\n";

chdir $dir or die $!; push @INC,  '../../lib';
END {chdir ".." or warn $!};

my @perlout = `$runperl Makefile.PL`;
if ($?) {
  print "not ok 1 # $runperl Makefile.PL failed: $?\n";
  print "# $_" foreach @perlout;
  exit($?);
} else {
  print "ok 1\n";
}


my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
if (-f "$makefile$makefile_ext") {
  print "ok 2\n";
} else {
  print "not ok 2\n";
}
my $makefile_rename = ($^O eq 'VMS' ? '.mms' : '.old');
push @files, "$makefile$makefile_rename"; # Renamed by make clean

my $make = $Config{make};

$make = $ENV{MAKE} if exists $ENV{MAKE};

my $makeout;

print "# make = '$make'\n";
$makeout = `$make`;
if ($?) {
  print "not ok 3 # $make failed: $?\n";
  exit($?);
} else {
  print "ok 3\n";
}

if ($Config{usedl}) {
  print "ok 4\n";
} else {
  push @files, "perl$Config{exe_ext}";
  my $makeperl = "$make perl";
  print "# make = '$makeperl'\n";
  $makeout = `$makeperl`;
  if ($?) {
    print "not ok 4 # $makeperl failed: $?\n";
    exit($?);
  } else {
    print "ok 4\n";
  }
}

my $maketest = "$make test";
print "# make = '$maketest'\n";
$makeout = `$maketest`;
if ($?) {
  print "not ok 10 # $maketest failed: $?\n";
} else {
  # Perl babblings
  $makeout =~ s/^\s*PERL_DL_NONLAZY=.+?\n//m;

  # GNU make babblings
  $makeout =~ s/^\w*?make.+?(?:entering|leaving) directory.+?\n//mig;

  # Hopefully gets most make's babblings
  # make -f Makefile.aperl perl
  $makeout =~ s/^\w*?make.+\sperl[^A-Za-z0-9]*\n//mig;
  # make[1]: `perl' is up to date.
  $makeout =~ s/^\w*?make.+perl.+?is up to date.*?\n//mig;

  print $makeout;
  print "ok 10\n";
}

my $makeclean = "$make clean";
print "# make = '$makeclean'\n";
$makeout = `$makeclean`;
if ($?) {
  print "not ok 11 # $make failed: $?\n";
} else {
  print "ok 11\n";
}

foreach (@files) {
  unlink $_ or warn "unlink $_: $!";
}

my $fail;
opendir DIR, "." or die "opendir '.': $!";
while (defined (my $entry = readdir DIR)) {
  next if $entry =~ /^\.\.?$/;
  print "# Extra file '$entry'\n";
  $fail = 1;
}
closedir DIR or warn "closedir '.': $!";
if ($fail) {
  print "not ok 12\n";
} else {
  print "ok 12\n";
}