| 1 |
package FFI::Platypus::Bundle; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use 5.008004; |
| 6 |
use Carp (); |
| 7 |
|
| 8 |
# ABSTRACT: Bundle foreign code with your Perl module |
| 9 |
our $VERSION = '2.12'; # VERSION |
| 10 |
|
| 11 |
|
| 12 |
package FFI::Platypus; |
| 13 |
|
| 14 |
sub _bundle |
| 15 |
{ |
| 16 |
my @arg_ptrs; |
| 17 |
|
| 18 |
if(defined $_[-1] && ref($_[-1]) eq 'ARRAY') |
| 19 |
{ |
| 20 |
@arg_ptrs = @{ pop @_ }; |
| 21 |
} |
| 22 |
|
| 23 |
push @arg_ptrs, undef; |
| 24 |
|
| 25 |
my($self, $package) = @_; |
| 26 |
$package = caller unless defined $package; |
| 27 |
|
| 28 |
require List::Util; |
| 29 |
|
| 30 |
my($pm) = do { |
| 31 |
my $pm = "$package.pm"; |
| 32 |
$pm =~ s{::}{/}g; |
| 33 |
# if the module is already loaded, we can use %INC |
| 34 |
# otherwise we can go through @INC and find the first .pm |
| 35 |
# this doesn't handle all edge cases, but probably enough |
| 36 |
List::Util::first(sub { (defined $_) && (-f $_) }, ($INC{$pm}, map { "$_/$pm" } @INC)); |
| 37 |
}; |
| 38 |
|
| 39 |
Carp::croak "unable to find module $package" unless $pm; |
| 40 |
|
| 41 |
my @parts = split /::/, $package; |
| 42 |
my $incroot = $pm; |
| 43 |
{ |
| 44 |
my $c = @parts; |
| 45 |
$incroot =~ s![\\/][^\\/]+$!! while $c--; |
| 46 |
} |
| 47 |
|
| 48 |
my $txtfn = List::Util::first(sub { -f $_ }, do { |
| 49 |
my $dir = join '/', @parts; |
| 50 |
my $file = $parts[-1] . ".txt"; |
| 51 |
( |
| 52 |
"$incroot/auto/$dir/$file", |
| 53 |
"$incroot/../arch/auto/$dir/$file", |
| 54 |
); |
| 55 |
}); |
| 56 |
|
| 57 |
my $lib; |
| 58 |
|
| 59 |
if($txtfn) |
| 60 |
{ |
| 61 |
$lib = do { |
| 62 |
my $fh; |
| 63 |
open($fh, '<', $txtfn) or die "unable to read $txtfn $!"; |
| 64 |
my $line = <$fh>; |
| 65 |
close $fh; |
| 66 |
$line =~ /^FFI::Build\@(.*)$/ |
| 67 |
? "$incroot/$1" |
| 68 |
: Carp::croak "bad format $txtfn"; |
| 69 |
}; |
| 70 |
Carp::croak "bundle code is missing: $lib" unless -f $lib; |
| 71 |
} |
| 72 |
elsif(-d "$incroot/../ffi") |
| 73 |
{ |
| 74 |
require FFI::Build::MM; |
| 75 |
require Capture::Tiny; |
| 76 |
require Cwd; |
| 77 |
require File::Spec; |
| 78 |
my $save = Cwd::getcwd(); |
| 79 |
chdir "$incroot/.."; |
| 80 |
my($output, $error) = Capture::Tiny::capture_merged(sub { |
| 81 |
$lib = eval { |
| 82 |
my $dist_name = $package; |
| 83 |
$dist_name =~ s/::/-/g; |
| 84 |
my $fbmm = FFI::Build::MM->new( save => 0 ); |
| 85 |
$fbmm->mm_args( DISTNAME => $dist_name ); |
| 86 |
my $build = $fbmm->load_build('ffi', undef, 'ffi/_build'); |
| 87 |
$build->build; |
| 88 |
}; |
| 89 |
$@; |
| 90 |
}); |
| 91 |
if($error) |
| 92 |
{ |
| 93 |
chdir $save; |
| 94 |
print STDERR $output; |
| 95 |
die $error; |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
$lib = File::Spec->rel2abs($lib); |
| 100 |
chdir $save; |
| 101 |
} |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
Carp::croak "unable to find bundle code for $package"; |
| 106 |
} |
| 107 |
|
| 108 |
my $handle = FFI::Platypus::DL::dlopen($lib, FFI::Platypus::DL::RTLD_PLATYPUS_DEFAULT()) |
| 109 |
or Carp::croak "error loading bundle code: $lib @{[ FFI::Platypus::DL::dlerror() ]}"; |
| 110 |
|
| 111 |
$self->{handles}->{$lib} = $handle; |
| 112 |
|
| 113 |
$self->lib($lib); |
| 114 |
|
| 115 |
if(my $init = eval { $self->function( 'ffi_pl_bundle_init' => [ 'string', 'sint32', 'opaque[]' ] => 'void' ) }) |
| 116 |
{ |
| 117 |
$init->call($package, scalar(@arg_ptrs)-1, \@arg_ptrs); |
| 118 |
} |
| 119 |
|
| 120 |
if(my $init = eval { $self->function( 'ffi_pl_bundle_constant' => [ 'string', 'opaque' ] => 'void' ) }) |
| 121 |
{ |
| 122 |
require FFI::Platypus::Constant; |
| 123 |
my $api = FFI::Platypus::Constant->new($package); |
| 124 |
$init->call($package, $api->ptr); |
| 125 |
} |
| 126 |
|
| 127 |
if(my $address = $self->find_symbol( 'ffi_pl_bundle_fini' )) |
| 128 |
{ |
| 129 |
push @{ $self->{fini} }, sub { |
| 130 |
my $self = shift; |
| 131 |
$self->function( $address => [ 'string' ] => 'void' ) |
| 132 |
->call( $package ); |
| 133 |
}; |
| 134 |
} |
| 135 |
|
| 136 |
$self; |
| 137 |
} |
| 138 |
|
| 139 |
1; |
| 140 |
|
| 141 |
__END__ |
| 142 |
|
| 143 |
=pod |
| 144 |
|
| 145 |
=encoding UTF-8 |
| 146 |
|
| 147 |
=head1 NAME |
| 148 |
|
| 149 |
FFI::Platypus::Bundle - Bundle foreign code with your Perl module |
| 150 |
|
| 151 |
=head1 VERSION |
| 152 |
|
| 153 |
version 2.12 |
| 154 |
|
| 155 |
=head1 SYNOPSIS |
| 156 |
|
| 157 |
C<ffi/foo.c>: |
| 158 |
|
| 159 |
#include <ffi_platypus_bundle.h> |
| 160 |
#include <string.h> |
| 161 |
|
| 162 |
typedef struct { |
| 163 |
char *name; |
| 164 |
int value; |
| 165 |
} foo_t; |
| 166 |
|
| 167 |
foo_t* |
| 168 |
foo__new(const char *class_name, const char *name, int value) { |
| 169 |
(void)class_name; |
| 170 |
foo_t *self = malloc( sizeof( foo_t ) ); |
| 171 |
self->name = strdup(name); |
| 172 |
self->value = value; |
| 173 |
return self; |
| 174 |
} |
| 175 |
|
| 176 |
const char * |
| 177 |
foo__name(foo_t *self) { |
| 178 |
return self->name; |
| 179 |
} |
| 180 |
|
| 181 |
int |
| 182 |
foo__value(foo_t *self) { |
| 183 |
return self->value; |
| 184 |
} |
| 185 |
|
| 186 |
void |
| 187 |
foo__DESTROY(foo_t *self) { |
| 188 |
free(self->name); |
| 189 |
free(self); |
| 190 |
} |
| 191 |
|
| 192 |
C<lib/Foo.pm>: |
| 193 |
|
| 194 |
package Foo; |
| 195 |
|
| 196 |
use strict; |
| 197 |
use warnings; |
| 198 |
use FFI::Platypus 2.00; |
| 199 |
|
| 200 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 201 |
|
| 202 |
$ffi->type('object(Foo)' => 'foo_t'); |
| 203 |
$ffi->mangler(sub { |
| 204 |
my $name = shift; |
| 205 |
$name =~ s/^/foo__/; |
| 206 |
$name; |
| 207 |
}); |
| 208 |
|
| 209 |
$ffi->bundle; |
| 210 |
|
| 211 |
$ffi->attach( new => [ 'string', 'string', 'int' ] => 'foo_t' ); |
| 212 |
$ffi->attach( name => [ 'foo_t' ] => 'string' ); |
| 213 |
$ffi->attach( value => [ 'foo_t' ] => 'int' ); |
| 214 |
$ffi->attach( DESTROY => [ 'foo_t' ] => 'void' ); |
| 215 |
|
| 216 |
1; |
| 217 |
|
| 218 |
C<t/foo.t> |
| 219 |
|
| 220 |
use Test2::V0; |
| 221 |
use Foo; |
| 222 |
|
| 223 |
my $foo = Foo->new("platypus", 10); |
| 224 |
isa_ok $foo, 'Foo'; |
| 225 |
is $foo->name, "platypus"; |
| 226 |
is $foo->value, 10; |
| 227 |
|
| 228 |
done_testing; |
| 229 |
|
| 230 |
C<Makefile.PL>: |
| 231 |
|
| 232 |
use ExtUtils::MakeMaker; |
| 233 |
use FFI::Build::MM; |
| 234 |
my $fbmm = FFI::Build::MM->new; |
| 235 |
WriteMakefile( |
| 236 |
$fbmm->mm_args( |
| 237 |
NAME => 'Foo', |
| 238 |
DISTNAME => 'Foo', |
| 239 |
VERSION => '1.00', |
| 240 |
# ... |
| 241 |
) |
| 242 |
); |
| 243 |
|
| 244 |
sub MY::postamble |
| 245 |
{ |
| 246 |
$fbmm->mm_postamble; |
| 247 |
} |
| 248 |
|
| 249 |
or C<dist.ini>: |
| 250 |
|
| 251 |
name = Foo |
| 252 |
version = 0.01 |
| 253 |
... |
| 254 |
|
| 255 |
[FFI::Build] |
| 256 |
version = 1.04 |
| 257 |
|
| 258 |
=head1 DESCRIPTION |
| 259 |
|
| 260 |
This document serves as a tutorial for using the new bundling interface provided |
| 261 |
by L<FFI::Platypus> as of api version 1. It requires L<FFI::Platypus> of at least |
| 262 |
1.00. |
| 263 |
|
| 264 |
Sometimes when writing FFI bindings you need to include a little C code (or your |
| 265 |
favorite compiled language) to finish things off. Alternatively, you might just |
| 266 |
want to write some C code (or your favorite compiled language) to include with your |
| 267 |
Perl module to make a tight loop faster. The bundling interface has you covered. |
| 268 |
|
| 269 |
=head2 Basic example |
| 270 |
|
| 271 |
To illustrate we will go through the files in the synopsis and explain |
| 272 |
how and why they work. To start with we have some C code which emulates object |
| 273 |
oriented code using C<foo__> as a prefix. We use a C struct that we call |
| 274 |
C<foo_t> to store our object data. On the C level the struct acts as a class, |
| 275 |
when combined with its functions that act as methods. The constructor just |
| 276 |
allocates the memory it needs for the C<foo_t> instance, fills in the |
| 277 |
appropriate fields and returns the pointer: |
| 278 |
|
| 279 |
foo_t* |
| 280 |
foo__new(const char *class_name, const char *name, int value) |
| 281 |
{ |
| 282 |
(void) class_name; |
| 283 |
foo_t *self = malloc( sizeof( foo_t ) ); |
| 284 |
self->name = strdup(name); |
| 285 |
self->value = value; |
| 286 |
return self; |
| 287 |
} |
| 288 |
|
| 289 |
We include a class name as the first argument, because Perl will include that |
| 290 |
when calling the constructor, but we do not use it here. An exercise for the |
| 291 |
reader would be to add hierarchical inheritance. |
| 292 |
|
| 293 |
There are also some methods which return member values. This class has only |
| 294 |
read only members, but you could have read/write or other methods depending |
| 295 |
on your needs. |
| 296 |
|
| 297 |
const char * |
| 298 |
foo__name(foo_t *self) |
| 299 |
{ |
| 300 |
return self->name; |
| 301 |
} |
| 302 |
|
| 303 |
We also include a destructor so that the memory owned by the object can be |
| 304 |
freed when it is no longer needed. |
| 305 |
|
| 306 |
void |
| 307 |
foo__DESTROY(foo_t *self) |
| 308 |
{ |
| 309 |
free(self->name); |
| 310 |
free(self); |
| 311 |
} |
| 312 |
|
| 313 |
This might start to look a little like a Perl module, and when we look at the Perl |
| 314 |
code that binds to this code, you will see why. First lets prepare the |
| 315 |
L<FFI::Platypus> instance and specify the correct api version: |
| 316 |
|
| 317 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 318 |
|
| 319 |
The bundle interface is only supported with api version 1, so if you try to use |
| 320 |
version 0 it will not work. Next we define an object type for C<foo_t> which will |
| 321 |
associate it with the Perl class C<Foo>. |
| 322 |
|
| 323 |
$ffi->type('object(Foo)' => 'foo_t'); |
| 324 |
|
| 325 |
As object type is a blessed reference to an opaque (default) or integer type which |
| 326 |
can be used as a Perl object. Platypus does the translating of Perl object to and |
| 327 |
from the foo_t pointers that the C code understands. For more details on Platypus |
| 328 |
types see L<FFI::Platypus::Type>. |
| 329 |
|
| 330 |
Next we set the mangler on the Platypus instance so that we can refer to function |
| 331 |
names without the C<foo__> prefix. You could just not use the prefix in your C |
| 332 |
code and skip this step, or you could refer to the function names in their full |
| 333 |
in your Perl code, however, this saves extra typing and allows you to bundle more |
| 334 |
than one class with your Perl code without having to worry about name conflicts. |
| 335 |
|
| 336 |
$ffi->mangler(sub { |
| 337 |
my $name = shift; |
| 338 |
$name =~ s/^/foo__/; |
| 339 |
$name; |
| 340 |
}); |
| 341 |
|
| 342 |
Finally we let Platypus know that we will be bundling code. |
| 343 |
|
| 344 |
$ffi->bundle; |
| 345 |
|
| 346 |
By default, this searches for the appropriate place for your dynamic libraries using |
| 347 |
the current package. In some cases you may need to override this, for example if your |
| 348 |
dist is named C<Foo-Bar> but your specific class is named C<Foo::Bar::Baz>, you'd |
| 349 |
want something like this: |
| 350 |
|
| 351 |
package Foo::Bar::Baz; |
| 352 |
use FFI::Platypus 2.00; |
| 353 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 354 |
$ffi->bundle('Foo::Bar'); |
| 355 |
... |
| 356 |
|
| 357 |
Now, finally we can attach the methods for our class: |
| 358 |
|
| 359 |
$ffi->attach( new => [ 'string', 'int' ] => 'foo_t' ); |
| 360 |
$ffi->attach( name => [ 'foo_t' ] => 'string' ); |
| 361 |
$ffi->attach( value => [ 'foo_t' ] => 'int' ); |
| 362 |
$ffi->attach( DESTROY => [ 'foo_t' ] => 'void' ); |
| 363 |
|
| 364 |
Note that we do not have to include the C<foo__> prefix because of the way we set up |
| 365 |
the mangler. If we hadn't done that then we could instead attach with the full names: |
| 366 |
|
| 367 |
$ffi->attach( [ 'foo__new' => 'new' ] => [ 'string', 'int' ] => 'foo_t' ); |
| 368 |
$ffi->attach( [ 'foo__name' => 'name' ] => [ 'foo_t' ] => 'string' ); |
| 369 |
... |
| 370 |
|
| 371 |
You're done! You can now use this class. Lets write a test to make sure it works, |
| 372 |
|
| 373 |
use Test2::V0; |
| 374 |
use Foo; |
| 375 |
|
| 376 |
my $foo = Foo->new("platypus", 10); |
| 377 |
isa_ok $foo, 'Foo'; |
| 378 |
is $foo->name, "platypus"; |
| 379 |
is $foo->value, 10; |
| 380 |
|
| 381 |
done_testing; |
| 382 |
|
| 383 |
and use C<prove> to check that it works: |
| 384 |
|
| 385 |
% prove -lvm |
| 386 |
t/foo.t .. |
| 387 |
ok 1 - An object of class 'Foo' isa 'Foo' |
| 388 |
ok 2 |
| 389 |
ok 3 |
| 390 |
1..3 |
| 391 |
ok |
| 392 |
All tests successful. |
| 393 |
Files=1, Tests=3, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.14 cusr 0.03 csys = 0.19 CPU) |
| 394 |
Result: PASS |
| 395 |
|
| 396 |
Platypus automatically compiles and links the dynamic library for you: |
| 397 |
|
| 398 |
% ls ffi/_build |
| 399 |
foo.c.o libFoo.so |
| 400 |
|
| 401 |
The C code will be rebuilt next time if the source code is newer than the object or dynamic libraries |
| 402 |
files. If the source files are not changed, then it won't be rebuilt to save time. If you are using |
| 403 |
the code without MakeMaker, or another build system you are responsible for cleaning up these files. |
| 404 |
This is intended as a convenience to allow you to test your code without having to invoke MakeMaker, |
| 405 |
or C<dzil> or whatever build system you are using. |
| 406 |
|
| 407 |
When you distribute your module though, you will want the dynamic library built just once |
| 408 |
at build-time and installed correctly so that it can be found at run-time. You don't need |
| 409 |
to make any changes to your C or Perl code, but you do need to tell MakeMaker to build and |
| 410 |
install the appropriate files using L<FFI::Build::MM>: |
| 411 |
|
| 412 |
use ExtUtils::MakeMaker; |
| 413 |
use FFI::Build::MM; |
| 414 |
my $fbmm = FFI::Build::MM->new; |
| 415 |
WriteMakefile( |
| 416 |
$fbmm->mm_args( |
| 417 |
NAME => 'Foo', |
| 418 |
DISTNAME => 'Foo', |
| 419 |
VERSION => '1.00', |
| 420 |
# ... |
| 421 |
) |
| 422 |
); |
| 423 |
|
| 424 |
sub MY::postamble |
| 425 |
{ |
| 426 |
$fbmm->mm_postamble; |
| 427 |
} |
| 428 |
|
| 429 |
And we can invoke all the normal MakeMaker style stuff and our C code will be compiled, linked |
| 430 |
and installed at the appropriate steps. |
| 431 |
|
| 432 |
% perl Makefile.PL |
| 433 |
Generating a Unix-style Makefile |
| 434 |
Writing Makefile for Foo |
| 435 |
Writing MYMETA.yml and MYMETA.json |
| 436 |
% make |
| 437 |
cp lib/Foo.pm blib/lib/Foo.pm |
| 438 |
"/Users/ollisg/perl5/perlbrew/perls/perl-5.30.0/bin/perl" -MFFI::Build::MM=cmd -e fbx_build |
| 439 |
CC ffi/foo.c |
| 440 |
LD blib/lib/auto/share/dist/Foo/lib/libFoo.dylib |
| 441 |
% make test |
| 442 |
"/Users/ollisg/perl5/perlbrew/perls/perl-5.30.0/bin/perl" -MFFI::Build::MM=cmd -e fbx_build |
| 443 |
"/Users/ollisg/perl5/perlbrew/perls/perl-5.30.0/bin/perl" -MFFI::Build::MM=cmd -e fbx_test |
| 444 |
PERL_DL_NONLAZY=1 "/Users/ollisg/perl5/perlbrew/perls/perl-5.30.0/bin/perl" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t |
| 445 |
t/foo.t .. ok |
| 446 |
All tests successful. |
| 447 |
Files=1, Tests=3, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.06 cusr 0.01 csys = 0.08 CPU) |
| 448 |
Result: PASS |
| 449 |
|
| 450 |
If the C<Makefile.PL> file above looks overly complicated, you can use the |
| 451 |
L<Dist::Zilla::Plugin::FFI::Build> plugin to simplify your life if you are |
| 452 |
using L<Dist::Zilla>: |
| 453 |
|
| 454 |
[FFI::Build] |
| 455 |
version = 1.04 |
| 456 |
|
| 457 |
Specifying version 1.04 will ensure that any C<.o> or C<.so> files are pruned |
| 458 |
from your build tree and not distributed by mistake. |
| 459 |
|
| 460 |
=head2 Initialization example |
| 461 |
|
| 462 |
The bundle interface also gives you entry points which will be called automatically |
| 463 |
when your code is loaded and unloaded if they are found. |
| 464 |
|
| 465 |
=over 4 |
| 466 |
|
| 467 |
=item C<ffi_pl_bundle_init> |
| 468 |
|
| 469 |
void ffi_pl_bundle_init(const char *package, int argc, void *argv[]); |
| 470 |
|
| 471 |
Called when the dynamic library is loaded. C<package> is the Perl package |
| 472 |
that called C<bundle> from Perl space. C<argc> and C<argv> represents an |
| 473 |
array of opaque pointers that can be passed as an array to bundle as the |
| 474 |
last argument. (the count C<argc> is a little redundant because C<argv> |
| 475 |
is also NULL terminated). |
| 476 |
|
| 477 |
=item C<ffi_pl_bundle_constant> |
| 478 |
|
| 479 |
void ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c); |
| 480 |
|
| 481 |
Called immediately after C<ffi_pl_bundle_init>, and is intended to allow |
| 482 |
you to set Perl constants from C space. For details on how this works |
| 483 |
and what methods you can call on the C<ffi_platypus_constant_t> instance, |
| 484 |
see L<FFI::Platypus::Constant>. |
| 485 |
|
| 486 |
=item C<ffi_pl_bundle_fini> |
| 487 |
|
| 488 |
void ffi_pl_bundle_fini(const char *package); |
| 489 |
|
| 490 |
Called when the dynamic library is unloaded. C<package> is the Perl |
| 491 |
package that called C<bundle> from Perl space when the library was |
| 492 |
loaded. B<CAVEAT>: if you attach any functions then this will |
| 493 |
never be called, because attaching functions locks the Platypus |
| 494 |
instance into memory along with the libraries which it is using. |
| 495 |
|
| 496 |
=back |
| 497 |
|
| 498 |
Here is an example that passes the version and a callback back into Perl |
| 499 |
space that emulates the Perl 5.10 C<say> feature. |
| 500 |
|
| 501 |
C<ffi/init.c>: |
| 502 |
|
| 503 |
#include <ffi_platypus_bundle.h> |
| 504 |
|
| 505 |
char buffer[512]; |
| 506 |
const char *version; |
| 507 |
void (*say)(const char *); |
| 508 |
|
| 509 |
void |
| 510 |
ffi_pl_bundle_init(const char *package, int argc, void *argv[]) |
| 511 |
{ |
| 512 |
version = argv[0]; |
| 513 |
say = argv[1]; |
| 514 |
|
| 515 |
say("in init!"); |
| 516 |
|
| 517 |
snprintf(buffer, 512, "package = %s, version = %s", package, version); |
| 518 |
say(buffer); |
| 519 |
|
| 520 |
snprintf(buffer, 512, "args = %d", argc); |
| 521 |
say(buffer); |
| 522 |
} |
| 523 |
|
| 524 |
void |
| 525 |
ffi_pl_bundle_fini(const char *package) |
| 526 |
{ |
| 527 |
say("in fini!"); |
| 528 |
} |
| 529 |
|
| 530 |
C<lib/Init.pm>: |
| 531 |
|
| 532 |
package Init; |
| 533 |
|
| 534 |
use strict; |
| 535 |
use warnings; |
| 536 |
use FFI::Platypus 2.00; |
| 537 |
|
| 538 |
our $VERSION = '1.00'; |
| 539 |
|
| 540 |
{ |
| 541 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 542 |
|
| 543 |
my $say = $ffi->closure(sub { |
| 544 |
my $string = shift; |
| 545 |
print "$string\n"; |
| 546 |
}); |
| 547 |
|
| 548 |
$ffi->bundle([ |
| 549 |
$ffi->cast( 'string' => 'opaque', $VERSION ), |
| 550 |
$ffi->cast( '(string)->void' => 'opaque', $say ), |
| 551 |
]); |
| 552 |
|
| 553 |
undef $ffi; |
| 554 |
undef $say; |
| 555 |
} |
| 556 |
|
| 557 |
1; |
| 558 |
|
| 559 |
The deinitialization order for the C<$say> callback and the C<$ffi> |
| 560 |
instance is essential here, so we do it manually with C<undef>: |
| 561 |
|
| 562 |
undef $ffi; |
| 563 |
undef $say; |
| 564 |
|
| 565 |
First we deallocate C<$ffi> which calls C<ffi_pl_bundle_fini>, |
| 566 |
which calls C<$say>, so we want to make sure the latter is still |
| 567 |
allocated. Once C<ffi_pl_bundle_fini> is done, we can safely |
| 568 |
deallocate C<$say>. |
| 569 |
|
| 570 |
If C<ffi_pl_bundle_fini> didn't call back into Perl space like |
| 571 |
this then we don't have to be as careful about deallocating |
| 572 |
things in Perl space. |
| 573 |
|
| 574 |
=head2 Compiler or linker flags example |
| 575 |
|
| 576 |
There are times when you will want to specify your own compiler and |
| 577 |
linker flags for the C code that you are bundling. The C<TL;DR> is that |
| 578 |
you can put a C<.fbx> file in your C<ffi> directory. This is a Perl |
| 579 |
script that returns a hash reference that is passed into the |
| 580 |
L<FFI::Build> constructor. This allows you to set a number of options, |
| 581 |
including compiler and linker flags. A more detailed example follows: |
| 582 |
|
| 583 |
You may want or need to set compiler and linker flags for your bundled |
| 584 |
C code. For example, say we have a header file, but instead of |
| 585 |
putting it in the C<ffi> directory we want to put it in a separate |
| 586 |
directory called C<include>. |
| 587 |
|
| 588 |
C<include/answer.h>: |
| 589 |
|
| 590 |
#ifndef ANSWER_H |
| 591 |
#define ANSWER_H |
| 592 |
|
| 593 |
int answer(void); |
| 594 |
|
| 595 |
#endif |
| 596 |
|
| 597 |
C<ffi/answer.c>: |
| 598 |
|
| 599 |
int |
| 600 |
answer(void) |
| 601 |
{ |
| 602 |
/* the answer to life the universe and everything */ |
| 603 |
return 42; |
| 604 |
} |
| 605 |
|
| 606 |
C<lib/Answer.pm>: |
| 607 |
|
| 608 |
package Answer; |
| 609 |
|
| 610 |
use strict; |
| 611 |
use warnings; |
| 612 |
use FFI::Platypus 2.00; |
| 613 |
use Exporter qw( import ); |
| 614 |
|
| 615 |
our @EXPORT = qw( answer ); |
| 616 |
|
| 617 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 618 |
$ffi->bundle; |
| 619 |
$ffi->attach( answer => [] => 'int' ); |
| 620 |
|
| 621 |
1; |
| 622 |
|
| 623 |
If you try to use this module just as-is you will get an error, about |
| 624 |
not being able to find the header file. Probably something like this: |
| 625 |
|
| 626 |
ffi/answer.c:1:10: fatal error: 'answer.h' file not found |
| 627 |
|
| 628 |
So we put a C<answer.fbx> file in the C<ffi> directory. (In case you |
| 629 |
are wondering FBX stands for "Ffi Build and file eXtensions should |
| 630 |
whenever possible be three characters long"). The name of the file |
| 631 |
can be anything so long as it ends in C<.fbx>, we just choose C<answer> |
| 632 |
here because that is the name of the project. |
| 633 |
|
| 634 |
C<ffi/answer.fbx>: |
| 635 |
|
| 636 |
our $DIR; |
| 637 |
|
| 638 |
return { |
| 639 |
cflags => "-I/include", |
| 640 |
source => "$DIR/*.c", |
| 641 |
} |
| 642 |
|
| 643 |
The C<$DIR> variable is provided by the builder code. It is the root |
| 644 |
of the distribution, and is helpful if you need a fully qualified path. |
| 645 |
In this case you could have also used C<ffi/*.c>. |
| 646 |
|
| 647 |
The script returns a hash reference which is passed into the L<FFI::Build> |
| 648 |
constructor, so you can use any of the options supported by that |
| 649 |
class. Now we should be able to use our bundled module: |
| 650 |
|
| 651 |
% perl -Ilib -MAnswer=answer -E 'say answer' |
| 652 |
42 |
| 653 |
|
| 654 |
=head2 Using bundled code with Alien. |
| 655 |
|
| 656 |
A useful technique is to use Platypus with L<Alien> technology. The |
| 657 |
L<Alien> namespace is reserved for providing external non-Perl dependencies |
| 658 |
for CPAN modules. The nominal L<Alien> module when installed looks |
| 659 |
for the library locally, and if it can't be found it fetches it from |
| 660 |
the internet, builds it, and installs it in a private directory so that |
| 661 |
it can be used by other CPAN modules. For L<Alien>s that provide |
| 662 |
shared libraries, and that have simple interfaces that do not require |
| 663 |
additional C code you can easily just pass the shared libraries |
| 664 |
to Platypus directly. For modules that require some bundled C code |
| 665 |
and an L<Alien> you have to link the L<Alien> library with your bundled |
| 666 |
code. If the L<Alien> uses the L<Alien::Base> interface then all you have |
| 667 |
to do is give the name of the L<Alien> to L<FFI::Build>. |
| 668 |
|
| 669 |
For example, the C<bzip2> library provides an interface that requires |
| 670 |
the caller to allocate a C C<struct> and then pass it to its various |
| 671 |
functions. The C<struct> is actually pretty simple and you could use |
| 672 |
L<FFI::C> or L<FFI::Platypus::Record>, but here is an example of how you |
| 673 |
would connect bundled C code with an L<Alien>. |
| 674 |
|
| 675 |
C<ffi/compress.c>: |
| 676 |
|
| 677 |
#include <bzlib.h> |
| 678 |
#include <stdlib.h> |
| 679 |
|
| 680 |
int |
| 681 |
bzip2__new(bz_stream **stream, int blockSize100k, int verbosity, int workFactor ) |
| 682 |
{ |
| 683 |
*stream = calloc(1, sizeof(bz_stream)); |
| 684 |
|
| 685 |
return BZ2_bzCompressInit(*stream, blockSize100k, verbosity, workFactor ); |
| 686 |
} |
| 687 |
|
| 688 |
C<lib/Bzip2.pm>: |
| 689 |
|
| 690 |
package Bzip2; |
| 691 |
|
| 692 |
use strict; |
| 693 |
use warnings; |
| 694 |
use FFI::Platypus 2.00; |
| 695 |
use FFI::Platypus::Memory qw( free ); |
| 696 |
|
| 697 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 698 |
$ffi->bundle; |
| 699 |
|
| 700 |
$ffi->mangler(sub { |
| 701 |
my $name = shift; |
| 702 |
$name =~ s/^/bzip2__/ unless $name =~ /^BZ2_/; |
| 703 |
$name; |
| 704 |
}); |
| 705 |
|
| 706 |
=head2 new |
| 707 |
|
| 708 |
my $bzip2 = Bzip2->new($block_size_100k, $verbosity, $work_flow); |
| 709 |
|
| 710 |
=cut |
| 711 |
|
| 712 |
$ffi->attach( new => ['opaque*', 'int', 'int', 'int'] => 'int' => sub { |
| 713 |
my $xsub = shift; |
| 714 |
my $class = shift; |
| 715 |
my $ptr; |
| 716 |
my $ret = $xsub->(\$ptr, @_); |
| 717 |
return bless \$ptr, $class; |
| 718 |
}); |
| 719 |
|
| 720 |
$ffi->attach( [ BZ2_bzCompressEnd => 'DESTROY' ] => ['opaque'] => 'int' => sub { |
| 721 |
my $xsub = shift; |
| 722 |
my $self = shift; |
| 723 |
my $ret = $xsub->($$self); |
| 724 |
free $$self; |
| 725 |
}); |
| 726 |
|
| 727 |
1; |
| 728 |
|
| 729 |
The C<.fbx> file that goes with this to make it work with L<Alien::Libbz2> |
| 730 |
is now pretty trivial: |
| 731 |
|
| 732 |
C<ffi/bz2.fbx>: |
| 733 |
|
| 734 |
{ |
| 735 |
alien => ['Alien::Libbz2'], |
| 736 |
source => ['ffi/*.c'], |
| 737 |
}; |
| 738 |
|
| 739 |
=head1 AUTHOR |
| 740 |
|
| 741 |
Author: Graham Ollis E<lt>[email protected]<gt> |
| 742 |
|
| 743 |
Contributors: |
| 744 |
|
| 745 |
Bakkiaraj Murugesan (bakkiaraj) |
| 746 |
|
| 747 |
Dylan Cali (calid) |
| 748 |
|
| 749 |
pipcet |
| 750 |
|
| 751 |
Zaki Mughal (zmughal) |
| 752 |
|
| 753 |
Fitz Elliott (felliott) |
| 754 |
|
| 755 |
Vickenty Fesunov (vyf) |
| 756 |
|
| 757 |
Gregor Herrmann (gregoa) |
| 758 |
|
| 759 |
Shlomi Fish (shlomif) |
| 760 |
|
| 761 |
Damyan Ivanov |
| 762 |
|
| 763 |
Ilya Pavlov (Ilya33) |
| 764 |
|
| 765 |
Petr Písař (ppisar) |
| 766 |
|
| 767 |
Mohammad S Anwar (MANWAR) |
| 768 |
|
| 769 |
Håkon Hægland (hakonhagland, HAKONH) |
| 770 |
|
| 771 |
Meredith (merrilymeredith, MHOWARD) |
| 772 |
|
| 773 |
Diab Jerius (DJERIUS) |
| 774 |
|
| 775 |
Eric Brine (IKEGAMI) |
| 776 |
|
| 777 |
szTheory |
| 778 |
|
| 779 |
José Joaquín Atria (JJATRIA) |
| 780 |
|
| 781 |
Pete Houston (openstrike, HOUSTON) |
| 782 |
|
| 783 |
Lukas Mai (MAUKE) |
| 784 |
|
| 785 |
=head1 COPYRIGHT AND LICENSE |
| 786 |
|
| 787 |
This software is copyright (c) 2015-2022 by Graham Ollis. |
| 788 |
|
| 789 |
This is free software; you can redistribute it and/or modify it under |
| 790 |
the same terms as the Perl 5 programming language system itself. |
| 791 |
|
| 792 |
=cut |