FFI::Build::File::Zig - Documentation and tools for using Platypus with the Zig programming language
version 0.01
From your perl project root:
$ mkdir ffi $ cd ffi $ zig init-lib info: Created build.zig info: Created src/main.zig info: Next, try `zig build --help` or `zig build test`
Edit build.zig, and edit the line b.addStaticLibrary
to look like this:
const lib = b.addSharedLibrary("ffi", "src/main.zig", b.version(0,0,1));
Add Zig code to ffi/src/main.zig
that you want to call from Perl:
export fn add(a: i32, b: i32) callconv(.C) i32 { return a + b; }
Your Perl bindings go in a .pm
file like lib/MyLib.pm
:
package MyLib; use FFI::Platypus 2.00; my $ffi = FFI::Platypus( api => 2, lang => 'Zig' ); $ffi->bundle; $ffi->attach( 'add' => ['i32','i32'] => 'i32' ); 1;
Your Makefile.PL
:
use ExtUtils::MakeMaker; use FFI::Build::MM; my $fbmm = FFI::Build::MM->new; WriteMakefile($fbmm->mm_args( ABSTRACT => 'My Lib', DISTNAME => 'MyLib', NAME => 'MyLib', VERSION_FROM => 'lib/MyLib.pm', BUILD_REQUIRES => { 'FFI::Build::MM' => '1.00', 'FFI::Build::File::Zig' => '0', }, PREREQ_PM => { 'FFI::Platypus' => '2.00', 'FFI::Platypus::Lang::Zig' => '0', }, )); sub MY::postamble { $fbmm->mm_postamble; }
Or alternatively, your dist.ini
if you are using Dist::Zilla:
[FFI::Build] lang = Zig build = Zig
Write a test:
use Test2::V0; use MyLib; is MyLib::add(1,2), 3; done_testing;
This module provides the necessary machinery to bundle Zig code with your Perl extension. It uses FFI::Build and the Zig build system to do the heavy lifting.
The distribution that follows the pattern above works just like a regular Pure-Perl or XS distribution, except:
Running the make
step builds the Zig library as a dynamic library using zig build system, and runs the Zig tests tests if any are available. It then moves the resulting dynamic library in to the appropriate location in blib
so that it can be found at test and runtime.
If you run the tests using prove -l
(that is, without building the distribution), Platypus will find the Zig package in the ffi
directory, build that and use it on the fly. This makes it easier to test your distribution with less explicit building.
This module is smart enough to check the timestamps on the appropriate files so the library won't need to be rebuilt if the source files haven't changed.
For more details using Perl + Zig with FFI, see FFI::Platypus::Lang::Zig.
The Core Platypus documentation.
Zig language plugin for Platypus.
Graham Ollis <plicease@cpan.org>
This software is copyright (c) 2022 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.