-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.pl
129 lines (105 loc) · 2.72 KB
/
config.pl
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
use strict;
use warnings;
use Capture::Tiny qw( capture capture_merged );
use File::ShareDir::Dist::Install qw( install_config_set );
use File::chdir;
use File::Temp qw( tempdir );
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';
{
my($out, $err, $exit) = capture {
system 'go' ,'version';
};
unless($exit == 0)
{
print "This dist requires Google Go to be installed";
exit 2;
}
my($version, $arch) = $out =~ /go version (\S+) (\S+)/;
print "Found go @{[ $version || '???' ]}\n";
print "For arch @{[ $arch || '???' ]}\n";
install_config_set $dist, go_version => $version;
install_config_set $dist, go_arch => $arch;
}
# TODO: gostring, goslice, gointerface
my %types = qw(
goint8 sint8
goint16 sint16
goint32 sint32
goint64 sint64
gouint8 sint8
gouint16 sint16
gouint32 sint32
gouint64 sint64
gobyte uint8
gorune sint32
gofloat32 float
gofloat64 double
gomap opaque
gochan opaque
);
{
local $CWD = tempdir( CLEANUP => 1, DIR => '.' );
path('simple.go')->spew(<<'EOF');
package main
import "C"
import "unsafe"
var mybool bool
//export SizeOfBool
func SizeOfBool() uintptr { return unsafe.Sizeof(mybool) }
var myint int
//export SizeOfInt
func SizeOfInt() uintptr { return unsafe.Sizeof(myint) }
var myuint uint
//export SizeOfUint
func SizeOfUint() uintptr { return unsafe.Sizeof(myuint) }
func main() {}
EOF
my($out, $exit) = capture_merged {
my @cmd = qw( go build -o simple.so -buildmode=c-shared simple.go );
print "+ @cmd\n";
system @cmd;
};
if($exit)
{
print "error building simple c-shared file\n";
print $out;
exit 2;
}
unless(-f 'simple.so')
{
print "Command returned success, but did not create a c-shared file\n";
print $out;
exit 2;
}
my $ffi = FFI::Platypus->new;
$ffi->lib('./simple.so');
$types{gobool} = 'uint' . ($ffi->function( SizeOfBool => [] => 'size_t' )->call * 8);
$types{goint} = 'sint' . ($ffi->function( SizeOfInt => [] => 'size_t' )->call * 8);
$types{gouint} = 'uint' . ($ffi->function( SizeOfUint => [] => 'size_t' )->call * 8);
$types{gouintptr} = 'uint' . ($ffi->sizeof('size_t')*8);
if(eval { $ffi->sizeof('complex_float'); 1 })
{
$types{gocomplex64} = 'complex_float';
}
if(eval { $ffi->sizeof('complex_double'); 1 })
{
$types{gocomplex128} = 'complex_double';
}
}
install_config_set $dist, go_types => \%types;