blib/lib/Call/Haskell.pm untested

filter: Statementstmt 0.0% 0 / 208 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 318 CC 34 SCAR 58.9
CC
34
Cov
0%
build
650.0
import
30.0
find_packages
30.0
CRAP 360.4
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.2.0');
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 #TODO: instead of 'packages', we should scan the source code for import statements and then use
28 # ghc-pkg find-module $module to find the package
29 if ( scalar @import_list == 1 ) {
30 $config{'functions'} = $import_list[0];
31 }
32 else {
33 my %import_config = @import_list;
34 map { $config{ lc($_) } = $import_config{$_} } keys(%import_config);
35 }
36 if (exists $config{'import'} ) {
37 $config{'functions'}=$config{'import'};
38 }
39 my $func_import_str = $config{'functions'};
40  
41 $func_import_str =~ s/\s+//g;
42 $func_import_str =~ s/\)$//;
43 my @func_imports = split( /\),/, $func_import_str );
44 if ( scalar @func_imports > 1 ) {
45 die
46 "Sorry, at the moment you can only call functions from a single Haskell module\n";
47 }
48 my $fq_func = shift @func_imports;
49 ( my $module, my $funclst ) = split( /\(/, $fq_func );
50 my @mfuncs = split( /,/, $funclst );
51  
52 # my %funcs=();
53 # for my $fq_func (@func_imports) {
54 # say "<$fq_func>";
55 # (my $module, my $funclst)=split(/\(/,$fq_func);
56 # my @mfuncs = split(/,/,$funclst);
57 # for my $func (@mfuncs) {
58 # push @{$funcs{$module}},$func;
59 # }
60 # }
61 build( $module, \@mfuncs, $config{'path'}, $config{'packages'}, $config{'clean'},
62 $config{'verbose'} , $config{'perl_types'} );
63 my $wd = cwd();
64 # say "Before EXPORT code";
65 for my $fn (@mfuncs) {
66 if (-e "$wd/_Call_Haskell/CallHaskellWrappers/$fn.pm") {
67 # push @Call::Haskell::EXPORT, "${fn}_ser";
68 #Call::Haskell->export_to_level( 1, 'Call::Haskell', @Call::Haskell::EXPORT );
69 require "$wd/_Call_Haskell/CallHaskellWrappers/$fn.pm";
70 eval("CallHaskellWrappers::$fn->import()");
71 }
72 push @Call::Haskell::EXPORT, $fn;
73 }
74 #say "After EXPORT code, now exporting to level 1";
75 Call::Haskell->export_to_level( 1, 'Call::Haskell', @Call::Haskell::EXPORT );
76 # say "LEAVING Call::Haskell::Import";
77 }
78  
79 sub build {
80 ( my $hs_module, my $function_names, my $hs_module_dir, my $extra_packages, my $CLEAN, $VV, my $perl_types ) =
81 @_; #'ProcessString';
82 my $wd = cwd();
83 if ($CLEAN) {
84 system('rm -Rf _Call_Haskell _Inline');
85 system('rm *.o *.hi');
86  
87 }
88 ( my $inline_c_code, my $generated ) =
89 create_hs_ffi_generator( $hs_module, $function_names, $hs_module_dir, $CLEAN,
90 $VV, $perl_types );
91  
92 if ( $generated == 1 ) { $CLEAN = 1 }
93 print "INLINE C CODE:\n" . $inline_c_code if $VV;
94  
95 if ( not -d './tmp' ) {
96 mkdir 'tmp';
97 }
98  
99 my $perl_link_options_str = $Config::Config{lddlflags};
100 my $perl_ld_str = $Config::Config{ld};
101 if ( $perl_link_options_str =~ /-fstack-protector/ ) {
102 $perl_link_options_str =~ s/-fstack-protector//;
103 }
104 print "PERL LD = $perl_ld_str\nPERL LDDLFLAGS = $perl_link_options_str\n"
105 if $VV;
106  
107 # The code below could go into a module
108 #say join(':',@INC),';',"\n",Dumper(%INC);#,';',
109 my $Call_Haskell_path=$INC{"Call/Haskell.pm"};
110  
111 my $hs_FFIGenerator_dir=$Call_Haskell_path;
112 $hs_FFIGenerator_dir=~s/Call.Haskell.pm$//;
113  
114 #if ($hs_FFIGenerator_dir=~/^\./) {die "\nThe path to the Call::Haskell module _must_ be absolute, please redefine your PERL5LIB\n\n"; }
115 $hs_FFIGenerator_dir='.';
116 say "FFIGenerator path:",$hs_FFIGenerator_dir if $VV;
117 my $hs_ffi_module = $hs_module . 'FFIWrapper';
118  
119 # This requires test_hs_c_str.c to exist and be compiled. I should generate that!
120 my $test_src = 'test_src';
121 my $test_out = 'testl';
122 my $c_wrapper = $hs_module . 'CWrapper';
123 my $hs_lib = $hs_module . 'HsC';
124 my $link_options_cache = '.lddlflags.cache';
125 my $obj_file_names_cache ='.objs.cache';
126  
127 # # Clean up
128 if ($CLEAN) {
129 system("rm *.o *.hi lib$hs_lib.a $test_src.c $test_out $link_options_cache $obj_file_names_cache 2>/dev/null ");
130 system("rm -Rf ./tmp/* 2>/dev/null");
131 }
132  
133 # Now, first compile the Haskell file:
134 my $object_files="";
135 my @packages=();
136 if (not -e "$obj_file_names_cache") {
137 # if ( not -e "$hs_ffi_module.o" ) {
138 say "ghc -c -O --make -i$wd/$hs_module_dir -i$hs_FFIGenerator_dir $hs_ffi_module" if $VV;
139 my @ghc_lines=`ghc -c -O --make -i$wd/$hs_module_dir -i$hs_FFIGenerator_dir $hs_ffi_module`;
140 @packages=find_packages(@ghc_lines);
141 for my $line (@ghc_lines) {
142 chomp $line;
143 $line=~s/^.*,\s//;
144 $line=~s/\s\)\s*$/ /;
145 $object_files.=$line;
146 }
147 # say "<<$object_files>>";
148  
149 # Here we should write these to a file cache
150 open my $OBJS, '>', $obj_file_names_cache;
151 print $OBJS $object_files;
152 close $OBJS;
153 }
154 else {
155 say "USING CACHED OBJECT NAMES" if $VV;
156 open my $OBJS, '<', $obj_file_names_cache;
157 $object_files = <$OBJS>;
158 close $OBJS;
159 # cache the list of object files
160 }
161 if ( not -e "$c_wrapper.o" ) {
162 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" if $VV;
163 system(
164 "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"
165 );
166 }
167 if ( not -e "lib$hs_lib.a" ) {
168 say "ar rcs lib$hs_lib.a $c_wrapper.o $object_files" if $VV;
169 # say "OLD: 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";
170 # system( "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");
171 system( "ar rcs lib$hs_lib.a $c_wrapper.o $object_files");
172 }
173 if ( $CLEAN or not -e "$test_src.c" ) {
174 my $test_src_code = "#include \"${c_wrapper}.h\"
175 int main(int argc, char* argv[]) {
176 hs_${hs_module}_init();
177 hs_${hs_module}_end();
178 return 1;
179 }
180 ";
181 open my $TST, '>', "$test_src.c";
182 print $TST $test_src_code;
183 close $TST;
184 system("gcc -c $test_src.c");
185 }
186 my $link_options = '';
187 my $ld = '';
188  
189 # if there is no cached link options file
190 my $package_str = join(' ', map {"-package $_"} (@packages,@{$extra_packages}));
191 if ( not -e $link_options_cache ) {
192 print
193 "ghc -v -keep-tmp-files -tmpdir=./tmp -no-hs-main $test_src.o -L. -l$hs_lib -package parsec -package containers $package_str -o $test_out 2>&1\n"
194 if $VV;
195 my @ghc_link_output =
196 `ghc -v -keep-tmp-files -tmpdir=./tmp -no-hs-main $test_src.o -L. -l$hs_lib -package parsec -package containers $package_str -o $test_out 2>&1`;
197 my $ghc_link_options_str = $ghc_link_output[-1];
198 $ghc_link_options_str =~ s/^\s*\'//;
199 $ghc_link_options_str =~ s/\'\s*$//;
200 # print "HASKELL LD CMD: $ghc_link_options_str\n" if $VV;
201 my @ghc_link_options = ();
202 if ( $ghc_link_options_str =~ /\'\s+\'/ ) {
203  
204 @ghc_link_options = split( /\'\s+\'/, $ghc_link_options_str );
205 }
206 else {
207 @ghc_link_options = split( /\s+/, $ghc_link_options_str );
208 }
209 $ld = shift @ghc_link_options;
210  
211 for my $opt (@ghc_link_options) {
212 $opt eq '-m64' && do { $opt = ''; next };
213 $opt eq '-fno-stack-protector' && do { $opt = ''; next };
214 $opt eq '-o' && do { $opt = ''; next };
215 $opt eq $test_out && do { $opt = ''; next };
216 $opt eq '-L.' && do { $opt = '-L../../../../_Call_Haskell'; next };
217 $opt eq "$test_src.o"
218 && do { $opt = '../../../../_Call_Haskell/' . $opt; next };
219 $opt =~ /^tmp.ghc/
220 && do { $opt = '../../../../_Call_Haskell/' . $opt; next };
221 }
222 $ghc_link_options_str = join( ' ', @ghc_link_options );
223 $link_options = "$perl_link_options_str $ghc_link_options_str";
224  
225 # Here we should write these to a file cache
226 open my $LO, '>', $link_options_cache;
227 print $LO "$ld :: $link_options";
228 close $LO;
229 }
230 else {
231 say "USING CACHED LINK OPTIONS" if $VV;
232 open my $LO, '<', $link_options_cache;
233 my $ld_link_options = <$LO>;
234 close $LO;
235 ( $ld, $link_options ) = split( /\s::\s/, $ld_link_options );
236 }
237 print "=" x 80, "\n" if $VV;
238 print "LDDLFLAGS = $link_options\n" if $VV;
239 print "=" x 80, "\n" if $VV;
240 chdir $wd;
241 Inline->import(
242 C => Config => LD => $ld,
243 LDDLFLAGS => $link_options,
244 'INC' => "-I$wd/_Call_Haskell"
245 );
246 Inline->import( C => $inline_c_code );
247 say "CALL hs_begin" if $VV;
248 hs_begin(1);
249 # say "LEAVING build()" if $VV;
250 }
251  
252 sub find_packages { my @ghc_make_output = @_;
253 #TODO: instead of 'packages', we should scan the source code for import statements and then use
254 # ghc-pkg find-module $module to find the package
255 my @sources=();
256 for my $line (@ghc_make_output) {
257 # say "LINE: $line";
258 chomp $line;
259 $line=~s/,.+$//;
260 $line=~s/^.+\(\s//;
261 # say "LINE-PROC: $line";
262 push @sources,$line;
263 }
264 my %packages=();
265 for my $src (@sources) {
266 # say "grep -E '^\\s*import\\s+[A-Z]' $src";
267 my @module_lines=`grep -E '^\\s*import\\s+[A-Z]' $src`;
268 for my $line (@module_lines) {
269 # say "MODLINE: $line";
270 my $module = $line;
271 $module=~s/^\s*import\s//;
272 $module=~s/qualified\s//;
273 $module=~s/hiding\s.*$//;
274 $module=~s/as\s.*$//;
275 $module=~s/\s*\(.*$//;
276 # say "MOD: $module";
277 my @pkg_lines=`ghc-pkg find-module $module`;
278 for my $line (@pkg_lines ) {
279 next if $line=~/^\s*$/;
280 next if $line=~/:/;
281 next if $line=~/\(/;
282 $line=~s/^\s+//;
283 $line=~s/\-.+\s*$//;
284 # say "PKG: <$line>";
285 if ($line ne 'base' and $line ne 'mtl') {
286 $packages{$line}=1;
287 last;
288 }
289 }
290 }
291 }
292 delete $packages{'containers'};
293 delete $packages{'parsec'};
294 return (keys %packages);
295 }
296  
297 END {
298 say "CALL hs_end" if $VV;
299 hs_end(0);
300 }
301  
302 1;
303 __END__