| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use lib 't/lib'; |
| 3 |
use Test::Cleanup; |
| 4 |
use Test::Platypus; |
| 5 |
use FFI::Build; |
| 6 |
use FFI::Build::Platform; |
| 7 |
use FFI::Temp; |
| 8 |
use Capture::Tiny qw( capture_merged ); |
| 9 |
use File::Spec; |
| 10 |
use File::Path qw( rmtree ); |
| 11 |
use File::Glob qw( bsd_glob ); |
| 12 |
|
| 13 |
subtest 'basic' => sub { |
| 14 |
|
| 15 |
my $build = FFI::Build->new('foo'); |
| 16 |
isa_ok $build, 'FFI::Build'; |
| 17 |
like $build->file->path, qr/foo/, 'foo is somewhere in the native name for the lib'; |
| 18 |
note "lib.file.path = @{[ $build->file->path ]}"; |
| 19 |
|
| 20 |
ok(-d $build->file->dirname, "dir is a dir" ); |
| 21 |
isa_ok $build->platform, 'FFI::Build::Platform'; |
| 22 |
|
| 23 |
$build->source('corpus/ffi_build/source/*.c'); |
| 24 |
|
| 25 |
my($cfile) = $build->source; |
| 26 |
isa_ok $cfile, 'FFI::Build::File::C'; |
| 27 |
|
| 28 |
}; |
| 29 |
|
| 30 |
subtest 'file classes' => sub { |
| 31 |
{ |
| 32 |
package FFI::Build::File::Foo1; |
| 33 |
use parent qw( FFI::Build::File::Base ); |
| 34 |
$INC{'FFI/Build/File/Foo1.pm'} = __FILE__; |
| 35 |
} |
| 36 |
|
| 37 |
{ |
| 38 |
package FFI::Build::File::Foo2; |
| 39 |
use parent qw( FFI::Build::File::Base ); |
| 40 |
} |
| 41 |
|
| 42 |
my @list = FFI::Build::_file_classes(); |
| 43 |
ok( @list > 0, "at least one" ); |
| 44 |
note "class = $_" for @list; |
| 45 |
}; |
| 46 |
|
| 47 |
subtest 'build' => sub { |
| 48 |
|
| 49 |
foreach my $type (qw( name object array )) |
| 50 |
{ |
| 51 |
|
| 52 |
subtest $type => sub { |
| 53 |
|
| 54 |
my $tempdir = FFI::Temp->newdir; |
| 55 |
|
| 56 |
my $build = FFI::Build->new('foo', |
| 57 |
dir => $tempdir, |
| 58 |
buildname => "tmpbuild.tmpbuild.$$.@{[ time ]}", |
| 59 |
verbose => 2, |
| 60 |
); |
| 61 |
|
| 62 |
my @source; |
| 63 |
|
| 64 |
if($type eq 'name') |
| 65 |
{ |
| 66 |
@source = 'corpus/ffi_build/project1/*.c'; |
| 67 |
} |
| 68 |
elsif($type eq 'object') |
| 69 |
{ |
| 70 |
@source = map { FFI::Build::File::C->new($_) } bsd_glob('corpus/ffi_build/project1/*.c'); |
| 71 |
} |
| 72 |
elsif($type eq 'array') |
| 73 |
{ |
| 74 |
@source = map { [ C => $_ ] } bsd_glob('corpus/ffi_build/project1/*.c'); |
| 75 |
} |
| 76 |
|
| 77 |
$build->source(@source); |
| 78 |
note "$_" for $build->source; |
| 79 |
|
| 80 |
my($out, $dll, $error) = capture_merged { |
| 81 |
my $dll = eval { $build->build }; |
| 82 |
($dll, $@); |
| 83 |
}; |
| 84 |
|
| 85 |
ok $error eq '', 'no error'; |
| 86 |
|
| 87 |
if($error) |
| 88 |
{ |
| 89 |
diag $out; |
| 90 |
return; |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
note $out; |
| 95 |
} |
| 96 |
|
| 97 |
platypus 2 => sub { |
| 98 |
my $ffi = shift; |
| 99 |
$ffi->lib($dll); |
| 100 |
|
| 101 |
is( |
| 102 |
$ffi->function(foo1 => [] => 'int')->call, |
| 103 |
42, |
| 104 |
); |
| 105 |
|
| 106 |
is( |
| 107 |
$ffi->function(foo2 => [] => 'string')->call, |
| 108 |
"42", |
| 109 |
); |
| 110 |
}; |
| 111 |
|
| 112 |
$build->clean; |
| 113 |
|
| 114 |
cleanup( |
| 115 |
$build->file->dirname, |
| 116 |
File::Spec->catdir(qw( corpus ffi_build project1 ), $build->buildname) |
| 117 |
); |
| 118 |
}; |
| 119 |
} |
| 120 |
|
| 121 |
}; |
| 122 |
|
| 123 |
subtest 'build c++' => sub { |
| 124 |
|
| 125 |
skip_all 'Test requires C++ compiler' |
| 126 |
unless eval { FFI::Build::Platform->which(FFI::Build::Platform->cxx) }; |
| 127 |
|
| 128 |
my $tempdir = FFI::Temp->newdir( TEMPLATE => "tmpbuild.XXXXXX" ); |
| 129 |
|
| 130 |
my $build = FFI::Build->new('foo', |
| 131 |
dir => $tempdir, |
| 132 |
buildname => "tmpbuild.$$.@{[ time ]}",, |
| 133 |
verbose => 2, |
| 134 |
); |
| 135 |
|
| 136 |
$build->source('corpus/ffi_build/project-cxx/*.cxx'); |
| 137 |
$build->source('corpus/ffi_build/project-cxx/*.cpp'); |
| 138 |
note "$_" for $build->source; |
| 139 |
|
| 140 |
my($out, $dll, $error) = capture_merged { |
| 141 |
my $dll = eval { $build->build }; |
| 142 |
($dll, $@); |
| 143 |
}; |
| 144 |
|
| 145 |
ok $error eq '', 'no error'; |
| 146 |
|
| 147 |
if($error) |
| 148 |
{ |
| 149 |
diag $out; |
| 150 |
return; |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
note $out; |
| 155 |
} |
| 156 |
|
| 157 |
platypus 2 => sub { |
| 158 |
my $ffi = shift; |
| 159 |
$ffi->lib($dll); |
| 160 |
|
| 161 |
my $foo1 = eval { $ffi->function( foo1 => [] => 'int' ) }; |
| 162 |
my $foo2 = eval { $ffi->function( foo2 => [] => 'string' ) }; |
| 163 |
|
| 164 |
ok defined $foo1, "foo1 found"; |
| 165 |
ok defined $foo2, "foo2 found"; |
| 166 |
|
| 167 |
|
| 168 |
SKIP: { |
| 169 |
if(defined $foo1 && defined $foo2) |
| 170 |
{ |
| 171 |
is( |
| 172 |
$ffi->function(foo1 => [] => 'int')->call, |
| 173 |
42, |
| 174 |
); |
| 175 |
is( |
| 176 |
$ffi->function(foo2 => [] => 'string')->call, |
| 177 |
"42", |
| 178 |
); |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
diag "[build log follows]\n"; |
| 183 |
diag $out; |
| 184 |
skip "foo1 or foo2 not found", 2 unless defined $foo1 && defined $foo2; |
| 185 |
} |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
$build->clean; |
| 190 |
|
| 191 |
cleanup( |
| 192 |
$build->file->dirname, |
| 193 |
File::Spec->catdir(qw( corpus ffi_build project-cxx ), $build->buildname) |
| 194 |
); |
| 195 |
|
| 196 |
}; |
| 197 |
|
| 198 |
subtest 'alien' => sub { |
| 199 |
|
| 200 |
skip_all 'Test requires Acme::Alien::DontPanic 1.03' |
| 201 |
unless eval { require Acme::Alien::DontPanic; Acme::Alien::DontPanic->VERSION("1.03") }; |
| 202 |
|
| 203 |
|
| 204 |
my $tempdir = FFI::Temp->newdir( TEMPLATE => "tmpbuild.XXXXXX" ); |
| 205 |
my $build = FFI::Build->new('bar', |
| 206 |
dir => $tempdir, |
| 207 |
buildname => "tmpbuild.$$.@{[ time ]}", |
| 208 |
verbose => 2, |
| 209 |
alien => ['Acme::Alien::DontPanic'], |
| 210 |
); |
| 211 |
|
| 212 |
$build->source('corpus/ffi_build/project2/*.c'); |
| 213 |
note "$_" for $build->source; |
| 214 |
|
| 215 |
my($out, $dll, $error) = capture_merged { |
| 216 |
my $dll = eval { $build->build }; |
| 217 |
($dll, $@); |
| 218 |
}; |
| 219 |
|
| 220 |
ok $error eq '', 'no error'; |
| 221 |
|
| 222 |
if($error) |
| 223 |
{ |
| 224 |
diag $out; |
| 225 |
return; |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
note $out; |
| 230 |
} |
| 231 |
|
| 232 |
platypus 1 => sub { |
| 233 |
my $ffi = shift; |
| 234 |
$ffi->lib($dll); |
| 235 |
|
| 236 |
is( |
| 237 |
$ffi->function(myanswer => [] => 'int')->call, |
| 238 |
42, |
| 239 |
); |
| 240 |
}; |
| 241 |
|
| 242 |
cleanup( |
| 243 |
$build->file->dirname, |
| 244 |
File::Spec->catdir(qw( corpus ffi_build project2 ), $build->buildname) |
| 245 |
); |
| 246 |
}; |
| 247 |
|
| 248 |
done_testing; |