Skip to content

Commit

Permalink
new basic int example
Browse files Browse the repository at this point in the history
  • Loading branch information
plicease committed Oct 31, 2022
1 parent 9f4a81b commit 006ad97
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:

env:
CIP_TAG: ${{ matrix.cip_tag }}
CIP_ENV: TMPDIR=/home/cip/tmp
CIP_ENV: TMPDIR=/home/cip/tmp FFI_PLATYPUS_LANG_GO_TEST_EXAMPLES=1

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/FFI-Platypus-Lang-Go-*
/.build
/share
*.so
*.h
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Graham Ollis <plicease@cpan.org>

# COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Graham Ollis.
This software is copyright (c) 2018-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.
6 changes: 5 additions & 1 deletion dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ installer = Author::Plicease::MakeMaker
test2_v0 = 1
github_user = PerlFFI
irc = irc://irc.perl.org/#native
workflow = linux

preamble = | require './inc/config.pl';

diag_preamble = | $post_diag = sub {
diag_preamble = | use Capture::Tiny qw( capture_merged );
Expand All @@ -22,6 +23,8 @@ diag_preamble = | ();
diag_preamble = | };
diag_preamble = | };

workflow = linux

[Author::Plicease::Core]

[RemovePrereqs]
Expand All @@ -47,6 +50,7 @@ cpan = 1
match = ^examples/Awesome-FFI/ffi/_build
match = ^examples/GoHttpLib/ffi/_build
match = ^blib/
match = ^share/

[MetaNoIndex]
directory = examples
Expand Down
10 changes: 10 additions & 0 deletions examples/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "C"

//export add
func add(x, y int) int {
return x + y
}

func main() {}
16 changes: 16 additions & 0 deletions examples/add.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env perl

use strict;
use warnings;
use FFI::Platypus 2.00;
use FFI::CheckLib qw( find_lib_or_die );
use File::Basename qw( dirname );

my $ffi = FFI::Platypus->new(
api => 2,
lib => './add.so',
lang => 'Go',
);
$ffi->attach( add => ['goint', 'goint'] => 'goint' );

print add(1,2), "\n"; # prints 3
17 changes: 17 additions & 0 deletions inc/mymm-build.pl → inc/config.pl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
use Path::Tiny qw( path );
use FFI::Platypus;

my($out, $err, $exit) = capture {
system 'go' ,'version';
};

unless($exit == 0)
{
print "This dist requires Google Go to be installed";
exit;
}

unless(caller)
{
*File::ShareDir::Dist::Install::install_dir = sub {
'share';
};
}

my $dist = 'FFI-Platypus-Lang-Go';

{
Expand Down
14 changes: 0 additions & 14 deletions inc/mymm.pl

This file was deleted.

21 changes: 0 additions & 21 deletions t/01_use.t

This file was deleted.

75 changes: 75 additions & 0 deletions t/examples.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use Test2::V0 -no_srand => 1;
use Capture::Tiny qw( capture_merged );
use Path::Tiny qw( path );
use File::chdir;

plan skip_all => 'developer test set FFI_PLATYPUS_LANG_GO_TEST_EXAMPLES=1 to run' unless $ENV{FFI_PLATYPUS_LANG_GO_TEST_EXAMPLES} || $ENV{CIPSOMETHING};

my @lib;

if(-d 'blib')
{
push @lib, '-Mblib';
}
else
{
$ENV{PERL_FILE_SHAREDIR_DIST} = join '=', 'FFI-Platypus-Lang-Go', "$CWD/share";
push @lib, '-I../lib';
}

foreach my $dir (qw( examples ))
{
subtest 'Compile/Link Go' => sub {

foreach my $src (path('examples')->children)
{
next unless $src->basename =~ /\.go$/;
my $so = $src->parent->child(do {
my $basename = $src->basename;
$basename =~ s/\.go$/.so/;
$basename;
});

my @cmd = ('go', 'build', -o => "$so", '-buildmode=c-shared', "$src");

my($out, $ret) = capture_merged {
system @cmd;
};

if(is $ret, 0, "@cmd")
{
note $out if $out ne '';
}
else
{
diag $out if $out ne '';
}
}
};

subtest 'Run Perl' => sub {
local $CWD = 'examples';
foreach my $src (path('.')->children)
{
next unless $src->basename =~ /\.pl$/;

my @cmd = ($^X, @lib, "$src");
my($out, $ret) = capture_merged {
system @cmd;
};

if(is $ret, 0, "@cmd")
{
note $out if $out ne '';
}
else
{
diag $out if $out ne '';
}
}
};
}

unlink for grep { $_->basename =~ /\.(h|so)$/ } path('examples')->children;

done_testing;

0 comments on commit 006ad97

Please sign in to comment.