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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
package MakeMaker::Test::Setup::XS;
@ISA = qw(Exporter);
require Exporter;
@EXPORT = qw(run_tests list_dynamic list_static);
use strict;
use File::Path;
use MakeMaker::Test::Utils;
use Config;
use Carp qw(croak);
use Test::More;
use File::Spec;
use File::Temp qw[tempdir];
use Cwd;
use ExtUtils::MM;
# this is to avoid MM->new overwriting _eumm in top dir
my $tempdir = tempdir(DIR => getcwd, CLEANUP => 1);
chdir $tempdir;
my $typemap = 'type map';
my $MM = MM->new({NAME=>'name', NORECURS=>1});
$typemap =~ s/ //g unless $MM->can_dep_space;
chdir File::Spec->updir;
my $PM_TEST = <<'END';
package XS::Test;
require Exporter;
require DynaLoader;
$VERSION = 1.01;
@ISA = qw(Exporter DynaLoader);
@EXPORT = qw(is_even);
bootstrap XS::Test $VERSION;
1;
END
my $XS_TEST = <<'END';
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MODULE = XS::Test PACKAGE = XS::Test
PROTOTYPES: DISABLE
int
is_even(input)
int input
CODE:
RETVAL = (input % 2 == 0);
OUTPUT:
RETVAL
END
my $T_TEST = <<'END';
#!/usr/bin/perl -w
use Test::More tests => 3;
use_ok "XS::Test";
ok !is_even(1);
ok is_even(2);
END
my $MAKEFILEPL = <<'END';
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'XS::%s',
VERSION_FROM => '%s',
TYPEMAPS => [ %s ],
PERL => "$^X -w",
%s
);
END
my $BS_TEST = '$DynaLoader::bscode = q(warn "BIG NOISE";)';
my $T_BOOTSTRAP = <<'EOF';
use Test::More tests => 1;
my $w = '';
$SIG{__WARN__} = sub { $w .= join '', @_; };
require XS::Test;
like $w, qr/NOISE/;
EOF
my $PM_OTHER = <<'END';
package XS::Other;
require Exporter;
require DynaLoader;
$VERSION = 1.20;
@ISA = qw(Exporter DynaLoader);
@EXPORT = qw(is_odd);
bootstrap XS::Other $VERSION;
1;
END
my $XS_OTHER = <<'END';
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MODULE = XS::Other PACKAGE = XS::Other
PROTOTYPES: DISABLE
int
is_odd(input)
int input
CODE:
RETVAL = (INVAR % 2 == 1);
OUTPUT:
RETVAL
END
my $T_OTHER = <<'END';
#!/usr/bin/perl -w
use Test::More tests => 3;
use_ok "XS::Other";
ok is_odd(1);
ok !is_odd(2);
END
my $PLUS1_C = <<'EOF';
#ifdef __cplusplus
extern "C" {
int plus1(int i)
#else
int plus1(i)
int i;
#endif
{ return i + 1; }
#ifdef __cplusplus
}
#endif
EOF
my %Files = (
'lib/XS/Test.pm' => $PM_TEST,
$typemap => '',
'Test.xs' => $XS_TEST,
't/is_even.t' => $T_TEST,
'Makefile.PL' => sprintf($MAKEFILEPL, 'Test', 'lib/XS/Test.pm', qq{'$typemap'}, ''),
);
my %label2files = (basic => \%Files);
$label2files{bscode} = +{
%{ $label2files{'basic'} }, # make copy
'Test_BS' => $BS_TEST,
't/bs.t' => $T_BOOTSTRAP,
};
delete $label2files{bscode}->{'t/is_even.t'};
$label2files{static} = +{
%{ $label2files{'basic'} }, # make copy
'Makefile.PL' => sprintf(
$MAKEFILEPL, 'Test', 'lib/XS/Test.pm', qq{'$typemap'},
q{LINKTYPE => 'static'},
),
"blib/arch/auto/share/dist/x-y/libwhatevs$MM->{LIB_EXT}" => 'hi there', # mimic what File::ShareDir can do
"blib/arch/auto/Alien/ROOT/root/lib/root/root$MM->{LIB_EXT}" => 'hi there', # mimic Alien::ROOT that installs a .a without extralibs.ld
# next two mimic dist that installs a .a WITH extralibs.ld but that is still not XS
"blib/arch/auto/Dist/File$MM->{LIB_EXT}" => 'hi there',
"blib/arch/auto/Dist/extralibs.ld" => '',
};
$label2files{subdirs} = +{
%{ $label2files{'basic'} }, # make copy
'Makefile.PL' => sprintf(
$MAKEFILEPL, 'Test', 'Test.pm', qq{'$typemap'},
q{DEFINE => '-DINVAR=input', INC => "-Inewline\n", LIBS => "-Lnewline\n",},
),
'Other/Makefile.PL' => sprintf($MAKEFILEPL, 'Other', 'Other.pm', qq{}, ''),
'Other/Other.pm' => $PM_OTHER,
'Other/Other.xs' => $XS_OTHER,
't/is_odd.t' => $T_OTHER,
};
virtual_rename('subdirs', 'lib/XS/Test.pm', 'Test.pm');
# to mimic behaviour of Unicode-LineBreak version 2015.07.16
$label2files{subdirscomplex} = +{
%{ $label2files{'subdirs'} }, # make copy
'Other/Makefile.PL' => sprintf(
$MAKEFILEPL,
'Other', 'Other.pm', qq{},
<<'EOF',
C => [qw(lib$(DIRFILESEP)file.c)],
OBJECT => 'lib$(DIRFILESEP)file$(OBJ_EXT)',
EOF
) . <<'EOF',
sub MY::c_o {
package MY;
my $self = shift;
my $inherited = $self->SUPER::c_o(@_);
$inherited =~ s{(:\n\t)(.*(?:\n\t.*)*)}
{ $1 . $self->cd('lib', split /(?<!\\)\n\t/, $2) }eg;
$inherited =~ s{(\s)(\$\*\.c\s)}
{ "$1..\$(DIRFILESEP)$2" }eg;
$inherited;
}
sub MY::top_targets {
<<'SNIP';
all :: lib$(DIRFILESEP)file$(OBJ_EXT)
$(NOECHO) $(NOOP)
config ::
$(NOECHO) $(NOOP)
pure_all ::
$(NOECHO) $(NOOP)
SNIP
}
EOF
'Other/lib/file.c' => $PLUS1_C,
};
delete $label2files{subdirscomplex}{'Other/Other.xs'};
delete $label2files{subdirscomplex}{'t/is_odd.t'};
$label2files{subdirsstatic} = +{
%{ $label2files{'subdirs'} }, # make copy
'Makefile.PL' => sprintf(
$MAKEFILEPL, 'Test', 'Test.pm', qq{'$typemap'},
q{DEFINE => '-DINVAR=input', LINKTYPE => 'static',},
),
};
# to mimic behaviour of CGI-Deurl-XS version 0.08
my $OTHERMAKEFILE = File::Spec->catfile('Other', makefile_name());
$label2files{subdirsskip} = +{
%{ $label2files{subdirscomplex} }, # make copy
'Makefile.PL' => sprintf(
$MAKEFILEPL,
'Test', 'Test.pm', qq{},
q[
MYEXTLIB => '] . File::Spec->catfile('Other', 'libparser$(LIB_EXT)') . q[',
]
)
. q[
sub MY::postamble {
my ($self) = @_;
return '$(MYEXTLIB) : ] . $OTHERMAKEFILE . q['."\n\t".$self->cd('Other', '$(MAKE) $(PASSTHRU)')."\n";
}
],
'Other/Makefile.PL' => sprintf(
$MAKEFILEPL,
'Other', 'Other.pm', qq{},
<<'EOF',
SKIP => [qw(all static dynamic )],
clean => {'FILES' => 'libparser$(LIB_EXT)'},
EOF
) . <<'EOF',
sub MY::top_targets {
my ($self) = @_;
my $static_lib_pure_cmd = $self->static_lib_pure_cmd('$(O_FILES)');
<<'SNIP' . $static_lib_pure_cmd;
all :: static
pure_all :: static
static :: libparser$(LIB_EXT)
libparser$(LIB_EXT): $(O_FILES)
SNIP
}
EOF
't/plus1.t' => <<'END',
#!/usr/bin/perl -w
use Test::More tests => 2;
use_ok "XS::Test";
is XS::Test::plus1(3), 4;
END
'Test.xs' => <<EOF,
#ifdef __cplusplus
extern "C" {
#endif
int plus1(int);
#ifdef __cplusplus
}
#endif
$XS_TEST
int
plus1(input)
int input
CODE:
RETVAL = plus1(input);
OUTPUT:
RETVAL
EOF
};
virtual_rename('subdirsskip', 'Other/lib/file.c', 'Other/file.c');
my $XS_MULTI = $XS_OTHER;
# check compiling from top dir still can include local
$XS_MULTI =~ s:(#include "XSUB.h"):$1\n#include "header.h":;
$label2files{multi} = +{
%{ $label2files{'basic'} }, # make copy
'Makefile.PL' => sprintf(
$MAKEFILEPL, 'Test', 'lib/XS/Test.pm', qq{'lib/XS/$typemap'},
q{XSMULTI => 1,},
),
'lib/XS/Other.pm' => $PM_OTHER,
'lib/XS/Other.xs' => $XS_MULTI,
't/is_odd.t' => $T_OTHER,
'lib/XS/header.h' => "#define INVAR input\n",
};
virtual_rename('multi', $typemap, "lib/XS/$typemap");
virtual_rename('multi', 'Test.xs', 'lib/XS/Test.xs');
$label2files{bscodemulti} = +{
%{ $label2files{'multi'} }, # make copy
'lib/XS/Test_BS' => $BS_TEST,
't/bs.t' => $T_BOOTSTRAP,
};
delete $label2files{bscodemulti}->{'t/is_even.t'};
delete $label2files{bscodemulti}->{'t/is_odd.t'};
$label2files{staticmulti} = +{
%{ $label2files{'multi'} }, # make copy
'Makefile.PL' => sprintf(
$MAKEFILEPL, 'Test', 'lib/XS/Test.pm', qq{'$typemap'},
q{LINKTYPE => 'static', XSMULTI => 1,},
),
};
$label2files{xsbuild} = +{
%{ $label2files{'multi'} }, # make copy
'Makefile.PL' => sprintf(
$MAKEFILEPL, 'Test', 'lib/XS/Test.pm', qq{'$typemap'},
q{
XSMULTI => 1,
XSBUILD => {
xs => {
'lib/XS/Other' => {
DEFINE => '-DINVAR=input',
OBJECT => 'lib/XS/Other$(OBJ_EXT) lib/XS/plus1$(OBJ_EXT)'
}
},
},
},
),
'lib/XS/Other.xs' => <<EOF,
#ifdef __cplusplus
extern "C" {
#endif
int plus1(int);
#ifdef __cplusplus
}
#endif
$XS_OTHER
int
plus1(input)
int input
CODE:
RETVAL = plus1(INVAR);
OUTPUT:
RETVAL
EOF
'lib/XS/plus1.c' => $PLUS1_C,
't/is_odd.t' => <<'END',
#!/usr/bin/perl -w
use Test::More tests => 4;
use_ok "XS::Other";
ok is_odd(1);
ok !is_odd(2);
is XS::Other::plus1(3), 4;
END
};
sub virtual_rename {
my ($label, $oldfile, $newfile) = @_;
$label2files{$label}->{$newfile} = delete $label2files{$label}->{$oldfile};
}
sub setup_xs {
my ($label, $sublabel) = @_;
croak "Must supply label" unless defined $label;
my $files = $label2files{$label};
croak "Must supply valid label" unless defined $files;
croak "Must supply sublabel" unless defined $sublabel;
my $prefix = "XS-Test$label$sublabel";
hash2files($prefix, $files);
return $prefix;
}
sub list_static {
(
( !$Config{usedl} ? [ 'basic', '', '' ] : ()), # still needs testing on static perl
[ 'static', '', '' ],
[ 'basic', ' static', '_static' ],
[ 'multi', ' static', '_static' ],
[ 'subdirs', ' LINKTYPE=static', ' LINKTYPE=static' ],
[ 'subdirsstatic', '', '' ],
[ 'staticmulti', '', '' ],
);
}
sub list_dynamic {
(
[ 'basic', '', '' ],
$^O ne 'MSWin32' ? (
[ 'bscode', '', '' ],
[ 'bscodemulti', '', '' ],
$^O !~ m!^(VMS|aix)$! ? ([ 'subdirscomplex', '', '' ]) : (),
) : (), # DynaLoader different
[ 'subdirs', '', '' ],
[ 'subdirsstatic', ' LINKTYPE=dynamic', ' LINKTYPE=dynamic' ],
[ 'subdirsstatic', ' dynamic', '_dynamic' ],
[ 'multi', '', '' ],
[ 'staticmulti', ' LINKTYPE=dynamic', ' LINKTYPE=dynamic' ],
[ 'staticmulti', ' dynamic', '_dynamic' ],
[ 'xsbuild', '', '' ],
[ 'subdirsskip', '', '' ],
);
}
sub run_tests {
my ($perl, $label, $add_target, $add_testtarget) = @_;
my $sublabel = $add_target;
$sublabel =~ s#[\s=]##g;
ok( my $dir = setup_xs($label, $sublabel), "setup $label$sublabel" );
ok( chdir($dir), "chdir'd to $dir" ) || diag("chdir failed: $!");
my @mpl_out = run(qq{$perl Makefile.PL});
SKIP: {
unless (cmp_ok( $?, '==', 0, 'Makefile.PL exited with zero' )) {
diag(@mpl_out);
skip 'perl Makefile.PL failed', 2;
}
my $make = make_run();
my $target = '';
my %macros = ();
if (defined($add_target)) {
if ($add_target =~ m/(\S+)=(\S+)/) {
$macros{$1} = $2;
}
else {
$target = $add_target;
}
}
my $make_cmd = make_macro($make, $target, %macros);
my $make_out = run($make_cmd);
unless (is( $?, 0, "$make_cmd exited normally" )) {
diag $make_out;
skip 'Make failed - skipping test', 1;
}
$target = 'test';
%macros = ();
if (defined($add_testtarget) && length($add_testtarget)) {
if ($add_testtarget =~ m/(\S+)=(\S+)/) {
$macros{$1} = $2;
}
else {
# an underscore prefix means combine, e.g. 'test' + '_dynamic'
unless ($add_testtarget =~ m/^_/) {
$target .= ($make =~ m/^MM(K|S)/i) ? ',' : ' ';
}
$target .= $add_testtarget;
}
}
my $test_cmd = make_macro($make, $target, %macros);
my $test_out = run($test_cmd);
is( $?, 0, "$test_cmd exited normally" ) || diag "$make_out\n$test_out";
}
chdir File::Spec->updir or die;
if ($ENV{EUMM_KEEP_TESTDIRS}) {
ok 1, "don't teardown $dir";
} else {
ok rmtree($dir), "teardown $dir";
}
}
1;
|