blib/lib/Call/Haskell.pm untested

filter: Statementstmt 0.0% 0 / 158 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 247 SCAR 65.5
CC
26
Cov
0%
build
462.0
import
30.0
END
6.0
CRAP 702.0
1 package Call::Haskell;
2 use warnings;
3 use strict;
4 use v5.16;
5  
6 use Call::Haskell::FFIGenerator qw( create_hs_ffi_generator );
7 use Cwd;
8 use Config;
9 require Inline;
10  
11 use version; our $VERSION = version->declare('v0.1.1');
12 @Call::Haskell::ISA = qw(Exporter);
13 my $VV = 0;
14  
15  
16 sub import {
17 my ( $hs, @import_list ) = @_;
18  
19 my %config = (
20 'functions' => '',
21 'path' => '.',
22 'clean' => 0,
23 'verbose' => 0,
24 'perl_types' => '',
25 'packages' => [],
26 );
27 if ( scalar @import_list == 1 ) {
28 $config{'functions'} = $import_list[0];
29 }
30 else {
31 my %import_config = @import_list;
32 map { $config{ lc($_) } = $import_config{$_} } keys(%import_config);
33 }
34 if (exists $config{'import'} ) {
35 $config{'functions'}=$config{'import'};
36 }
37 my $func_import_str = $config{'functions'};
38  
39 $func_import_str =~ s/\s+//g;
40 $func_import_str =~ s/\)$//;
41 my @func_imports = split( /\),/, $func_import_str );
42 if ( scalar @func_imports > 1 ) {
43 die
44 "Sorry, at the moment you can only call functions from a single Haskell module\n";
45 }
46 my $fq_func = shift @func_imports;
47 ( my $module, my $funclst ) = split( /\(/, $fq_func );
48 my @mfuncs = split( /,/, $funclst );
49  
50 # my %funcs=();
51 # for my $fq_func (@func_imports) {
52 # say "<$fq_func>";
53 # (my $module, my $funclst)=split(/\(/,$fq_func);
54 # my @mfuncs = split(/,/,$funclst);
55 # for my $func (@mfuncs) {
56 # push @{$funcs{$module}},$func;
57 # }
58 # }
59 build( $module, \@mfuncs, $config{'path'}, $config{'clean'},
60 $config{'verbose'} , $config{'perl_types'} );
61 my $wd = cwd();
62 # say "Before EXPORT code";
63 for my $fn (@mfuncs) {
64 if (-e "$wd/_Call_Haskell/CallHaskellWrappers/$fn.pm") {
65 # push @Call::Haskell::EXPORT, "${fn}_ser";
66 #Call::Haskell->export_to_level( 1, 'Call::Haskell', @Call::Haskell::EXPORT );
67 require "$wd/_Call_Haskell/CallHaskellWrappers/$fn.pm";
68 eval("CallHaskellWrappers::$fn->import()");
69 }
70 push @Call::Haskell::EXPORT, $fn;
71 }
72 #say "After EXPORT code, now exporting to level 1";
73 Call::Haskell->export_to_level( 1, 'Call::Haskell', @Call::Haskell::EXPORT );
74 # say "LEAVING Call::Haskell::Import";
75 }
76  
77 sub build {
78 ( my $hs_module, my $function_names, my $hs_module_dir, my $CLEAN, $VV, my $perl_types ) =
79 @_; #'ProcessString';
80 my $wd = cwd();
81 if ($CLEAN) {
82 system('rm -Rf _Call_Haskell _Inline');
83 }
84 ( my $inline_c_code, my $generated ) =
85 create_hs_ffi_generator( $hs_module, $function_names, $hs_module_dir, $CLEAN,
86 $VV, $perl_types );
87  
88 if ( $generated == 1 ) { $CLEAN = 1 }
89 print "INLINE C CODE:\n" . $inline_c_code if $VV;
90  
91 if ( not -d './tmp' ) {
92 mkdir 'tmp';
93 }
94  
95 my $perl_link_options_str = $Config::Config{lddlflags};
96 my $perl_ld_str = $Config::Config{ld};
97 if ( $perl_link_options_str =~ /-fstack-protector/ ) {
98 $perl_link_options_str =~ s/-fstack-protector//;
99 }
100 print "PERL LD = $perl_ld_str\nPERL LDDLFLAGS = $perl_link_options_str\n"
101 if $VV;
102  
103 # The code below could go into a module
104 #say join(':',@INC),';',"\n",Dumper(%INC);#,';',
105 my $Call_Haskell_path=$INC{"Call/Haskell.pm"};
106  
107 my $hs_FFIGenerator_dir=$Call_Haskell_path;
108 $hs_FFIGenerator_dir=~s/Call.Haskell.pm$//;
109  
110 #if ($hs_FFIGenerator_dir=~/^\./) {die "\nThe path to the Call::Haskell module _must_ be absolute, please redefine your PERL5LIB\n\n"; }
111 $hs_FFIGenerator_dir='.';
112 say "FFIGenerator path:",$hs_FFIGenerator_dir if $VV;
113 my $hs_ffi_module = $hs_module . 'FFIWrapper';
114  
115 # This requires test_hs_c_str.c to exist and be compiled. I should generate that!
116 my $test_src = 'test_src';
117 my $test_out = 'testl';
118 my $c_wrapper = $hs_module . 'CWrapper';
119 my $hs_lib = $hs_module . 'HsC';
120 my $link_options_cache = '.lddlflags.cache';
121  
122 # # Clean up
123 if ($CLEAN) {
124 system("rm *.o *.hi lib$hs_lib.a $test_src.c $test_out $link_options_cache");
125 system("rm -Rf ./tmp/*");
126 }
127  
128 # Now, first compile the Haskell file:
129 if ( not -e "$hs_ffi_module.o" ) {
130 say "ghc -c -O --make -i$wd/$hs_module_dir -i$hs_FFIGenerator_dir $hs_ffi_module";
131 system("ghc -c -O --make -i$wd/$hs_module_dir -i$hs_FFIGenerator_dir $hs_ffi_module");
132 }
133 if ( not -e "$c_wrapper.o" ) {
134 say "ghc --make -i$wd/$hs_module_dir -i$hs_FFIGenerator_dir -optc-O -no-hs-main -c $c_wrapper.c $hs_ffi_module $hs_module FFIGenerator.ShowToPerl";
135 system(
136 "ghc --make -i$wd/$hs_module_dir -i$hs_FFIGenerator_dir -optc-O -no-hs-main -c $c_wrapper.c $hs_ffi_module $hs_module FFIGenerator.ShowToPerl"
137 );
138 }
139 if ( not -e "lib$hs_lib.a" ) {
140 say
141 "ar rcs lib$hs_lib.a $c_wrapper.o $hs_ffi_module.o $wd/$hs_module_dir/$hs_module.o $hs_FFIGenerator_dir/FFIGenerator/ShowToPerl.o";
142 system(
143 "ar rcs lib$hs_lib.a $c_wrapper.o $hs_ffi_module.o $wd/$hs_module_dir/$hs_module.o $hs_FFIGenerator_dir/FFIGenerator/ShowToPerl.o"
144 );
145 }
146 if ( $CLEAN or not -e "$test_src.c" ) {
147 my $test_src_code = "#include \"${c_wrapper}.h\"
148 int main(int argc, char* argv[]) {
149 hs_${hs_module}_init();
150 hs_${hs_module}_end();
151 return 1;
152 }
153 ";
154 open my $TST, '>', "$test_src.c";
155 print $TST $test_src_code;
156 close $TST;
157 system("gcc -c $test_src.c");
158 }
159 my $link_options = '';
160 my $ld = '';
161  
162 # if there is no cached link options file
163 if ( not -e $link_options_cache ) {
164 print
165 "ghc -v -keep-tmp-files -tmpdir=./tmp -no-hs-main $test_src.o -L. -l$hs_lib -package parsec -package containers -o $test_out 2>&1\n"
166 if $VV;
167 my @ghc_link_output =
168 `ghc -v -keep-tmp-files -tmpdir=./tmp -no-hs-main $test_src.o -L. -l$hs_lib -package parsec -package containers -o $test_out 2>&1`;
169 my $ghc_link_options_str = $ghc_link_output[-1];
170 $ghc_link_options_str =~ s/^\s*\'//;
171 $ghc_link_options_str =~ s/\'\s*$//;
172 # print "HASKELL LD CMD: $ghc_link_options_str\n" if $VV;
173 my @ghc_link_options = ();
174 if ( $ghc_link_options_str =~ /\'\s+\'/ ) {
175  
176 @ghc_link_options = split( /\'\s+\'/, $ghc_link_options_str );
177 }
178 else {
179 @ghc_link_options = split( /\s+/, $ghc_link_options_str );
180 }
181 $ld = shift @ghc_link_options;
182  
183 for my $opt (@ghc_link_options) {
184 $opt eq '-m64' && do { $opt = ''; next };
185 $opt eq '-fno-stack-protector' && do { $opt = ''; next };
186 $opt eq '-o' && do { $opt = ''; next };
187 $opt eq $test_out && do { $opt = ''; next };
188 $opt eq '-L.' && do { $opt = '-L../../../../_Call_Haskell'; next };
189 $opt eq "$test_src.o"
190 && do { $opt = '../../../../_Call_Haskell/' . $opt; next };
191 $opt =~ /^tmp.ghc/
192 && do { $opt = '../../../../_Call_Haskell/' . $opt; next };
193 }
194 $ghc_link_options_str = join( ' ', @ghc_link_options );
195 $link_options = "$perl_link_options_str $ghc_link_options_str";
196  
197 # Here we should write these to a file cache
198 open my $LO, '>', $link_options_cache;
199 print $LO "$ld :: $link_options";
200 close $LO;
201 }
202 else {
203 say "USING CACHED LINK OPTIONS" if $VV;
204 open my $LO, '<', $link_options_cache;
205 my $ld_link_options = <$LO>;
206 close $LO;
207 ( $ld, $link_options ) = split( /\s::\s/, $ld_link_options );
208 }
209 print "=" x 80, "\n" if $VV;
210 print "LDDLFLAGS = $link_options\n" if $VV;
211 print "=" x 80, "\n" if $VV;
212 chdir $wd;
213 Inline->import(
214 C => Config => LD => $ld,
215 LDDLFLAGS => $link_options,
216 'INC' => "-I$wd/_Call_Haskell"
217 );
218 Inline->import( C => $inline_c_code );
219 say "CALL hs_begin" if $VV;
220 hs_begin(1);
221 # say "LEAVING build()" if $VV;
222 }
223  
224 END {
225 say "CALL hs_end" if $VV;
226 hs_end(0);
227 }
228  
229 1;
230 __END__