blib/lib/FFI/Build/Platform.pm untested

filter: Statementstmt 0.0% 0 / 219 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 38 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 40 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 403 CC 41 SCAR 29.6
CC
41
Cov
0%
cxx
72.0
ldflags
42.0
for
30.0
CRAP 19.2
1 package FFI::Build::Platform;
2  
3 use strict;
4 use warnings;
5 use 5.008004;
6 use Carp ();
7 use Text::ParseWords ();
8 use FFI::Temp;
9 use Capture::Tiny ();
10 use File::Spec;
11 use FFI::Platypus::ShareConfig;
12  
13 # ABSTRACT: Platform specific configuration.
14 our $VERSION = '2.11'; # VERSION
15  
16  
17 sub new
18 {
19 my($class, $config) = @_;
20 $config ||= do {
21 require Config;
22 \%Config::Config;
23 };
24 my $self = bless {
25 config => $config,
26 }, $class;
27 $self;
28 }
29  
30  
31 my $default;
32 sub default
33 {
34 $default ||= FFI::Build::Platform->new;
35 }
36  
37 sub _self
38 {
39 my($self) = @_;
40 ref $self ? $self : $self->default;
41 }
42  
43  
44 sub osname
45 {
46 _self(shift)->{config}->{osname};
47 }
48  
49  
50 sub object_suffix
51 {
52 _self(shift)->{config}->{obj_ext};
53 }
54  
55  
56 sub library_suffix
57 {
58 my $self = _self(shift);
59 my $osname = $self->osname;
60 my @suffix;
61 if($osname eq 'darwin')
62 {
63 push @suffix, '.dylib', '.bundle';
64 }
65 elsif($osname =~ /^(MSWin32|msys|cygwin)$/)
66 {
67 push @suffix, '.dll';
68 }
69 else
70 {
71 push @suffix, '.' . $self->{config}->{dlext};
72 }
73 wantarray ? @suffix : $suffix[0]; ## no critic (Community::Wantarray)
74 }
75  
76  
77 sub library_prefix
78 {
79 my $self = _self(shift);
80  
81 # this almost certainly requires refinement.
82 if($self->osname eq 'cygwin')
83 {
84 return 'cyg';
85 }
86 elsif($self->osname eq 'msys')
87 {
88 return 'msys-';
89 }
90 elsif($self->osname eq 'MSWin32')
91 {
92 return '';
93 }
94 else
95 {
96 return 'lib';
97 }
98 }
99  
100  
101 sub cc
102 {
103 my $self = _self(shift);
104 my $cc = $self->{config}->{cc};
105 [$self->shellwords($cc)];
106 }
107  
108  
109 sub cpp
110 {
111 my $self = _self(shift);
112 my $cpp = $self->{config}->{cpprun};
113 [$self->shellwords($cpp)];
114 }
115  
116  
117 sub cxx
118 {
119 my $self = _self(shift);
120  
121 my @cc = @{ $self->cc };
122  
123 if($self->{config}->{ccname} eq 'gcc')
124 {
125 if($cc[0] =~ /gcc$/)
126 {
127 my @maybe = @cc;
128 $maybe[0] =~ s/gcc$/g++/;
129 return \@maybe if $self->which($maybe[0]);
130 }
131 if($cc[0] =~ /clang/)
132 {
133 my @maybe = @cc;
134 $maybe[0] =~ s/clang/clang++/;
135 return \@maybe if $self->which($maybe[0]);
136 }
137  
138 # TODO: there are probably situations, eg solaris
139 # where we don't want to try c++ in the case of
140 # a ccname = gcc ?
141 my @maybe = qw( c++ g++ clang++ );
142  
143 foreach my $maybe (@maybe)
144 {
145 return [$maybe] if $self->which($maybe);
146 }
147 }
148 elsif($self->osname eq 'MSWin32' && $self->{config}->{ccname} eq 'cl')
149 {
150 # TODO: see https://github.com/PerlFFI/FFI-Platypus/issues/203
151 #return \@cc;
152 }
153  
154 Carp::croak("unable to detect corresponding C++ compiler");
155 }
156  
157  
158 sub cxxld
159 {
160 my $self = _self(shift);
161  
162 $DB::single = 1;
163  
164 # This is definitely not exhaustive or complete or even
165 # particularlly good. Patches welcome.
166  
167 if($self->osname eq 'darwin')
168 {
169 my @cxx = @{ $self->cxx };
170 return [map { /^(cc|clang|gcc)$/ ? @cxx : $_ } @{ $self->ld }];
171 }
172 else
173 {
174 return $self->cxx;
175 }
176 }
177  
178  
179 sub for
180 {
181 my $self = _self(shift);
182  
183 my @cc = @{ $self->cc };
184  
185 if($self->{config}->{ccname} eq 'gcc')
186 {
187 if($cc[0] =~ /gcc$/)
188 {
189 my @maybe = @cc;
190 $maybe[0] =~ s/gcc$/gfortran/;
191 return \@maybe if $self->which($maybe[0]);
192 }
193  
194 foreach my $maybe (qw( gfortran ))
195 {
196 return [$maybe] if $self->which($maybe);
197 }
198 }
199 else
200 {
201 Carp::croak("unable to detect correspnding Fortran Compiler");
202 }
203 }
204  
205  
206 sub ld
207 {
208 my($self) = @_;
209 my $ld = $self->{config}->{ld};
210 [$self->shellwords($ld)];
211 }
212  
213  
214 sub shellwords
215 {
216 my $self = _self(shift);
217  
218 my $win = !!($self->osname eq 'MSWin32');
219  
220 grep { defined $_ } map {
221 ref $_
222 # if we have an array ref then it has already been shellworded
223 ? @$_
224 : do {
225 # remove leading whitespace, confuses some older versions of shellwords
226 my $str = /^\s*(.*)$/ && $1;
227 # escape things on windows
228 $str =~ s,\\,\\\\,g if $win;
229 Text::ParseWords::shellwords($str);
230 }
231 } @_;
232  
233 }
234  
235  
236 sub ccflags
237 {
238 my $self = _self(shift);
239 my @ccflags;
240 push @ccflags, $self->shellwords($self->{config}->{cccdlflags});
241 push @ccflags, $self->shellwords($self->{config}->{ccflags});
242 push @ccflags, $self->shellwords($self->{config}->{optimize});
243 my $dist_include = eval { File::Spec->catdir(FFI::Platypus::ShareConfig::dist_dir('FFI-Platypus'), 'include') };
244 push @ccflags, "-I$dist_include" unless $@;
245 \@ccflags;
246 }
247  
248  
249 sub ldflags
250 {
251 my $self = _self(shift);
252 my @ldflags = $self->shellwords($self->{config}->{lddlflags});
253 if($self->osname eq 'cygwin')
254 {
255 no warnings 'qw';
256 # doesn't appear to be necessary, Perl has this in lddlflags already on cygwin
257 #push @ldflags, qw( -Wl,--enable-auto-import -Wl,--export-all-symbols -Wl,--enable-auto-image-base );
258 }
259 elsif($self->osname eq 'MSWin32' && $self->{config}->{ccname} eq 'cl')
260 {
261 push @ldflags, qw( -dll );
262 @ldflags = grep !/^-nodefaultlib$/, @ldflags;
263 }
264 elsif($self->osname eq 'MSWin32')
265 {
266 no warnings 'qw';
267 push @ldflags, qw( -Wl,--enable-auto-import -Wl,--export-all-symbols -Wl,--enable-auto-image-base );
268 }
269 elsif($self->osname eq 'darwin')
270 {
271 # we want to build a .dylib instead of a .bundle
272 @ldflags = map { $_ eq '-bundle' ? '-dynamiclib' : $_ } @ldflags;
273 }
274 \@ldflags;
275 }
276  
277  
278 sub cc_mm_works
279 {
280 my $self = _self(shift);
281 my $verbose = shift;
282 $verbose ||= 0;
283  
284 unless(defined $self->{cc_mm_works})
285 {
286 require FFI::Build::File::C;
287 my $c = FFI::Build::File::C->new(\"#include \"foo.h\"\n");
288 my $dir = FFI::Temp->newdir;
289 {
290 open my $fh, '>', "$dir/foo.h";
291 print $fh "\n";
292 close $fh;
293 }
294  
295 my @cmd = (
296 $self->cc,
297 $self->ccflags,
298 "-I$dir",
299 '-MM',
300 $c->path,
301 );
302  
303 my($out, $exit) = Capture::Tiny::capture_merged(sub {
304 $self->run(@cmd);
305 });
306  
307 if($verbose >= 2)
308 {
309 print $out;
310 }
311 elsif($verbose >= 1)
312 {
313 print "CC (checkfor -MM)\n";
314 }
315  
316  
317 if(!$exit && $out =~ /foo\.h/)
318 {
319 $self->{cc_mm_works} = '-MM';
320 }
321 else
322 {
323 $self->{cc_mm_works} = 0;
324 }
325 }
326  
327 $self->{cc_mm_works};
328 }
329  
330  
331 sub flag_object_output
332 {
333 my $self = _self(shift);
334 my $file = shift;
335 if($self->osname eq 'MSWin32' && $self->{config}->{ccname} eq 'cl')
336 {
337 return ("-Fo$file");
338 }
339 else
340 {
341 return ('-o' => $file);
342 }
343 }
344  
345  
346 sub flag_library_output
347 {
348 my $self = _self(shift);
349 my $file = shift;
350 if($self->osname eq 'MSWin32' && $self->{config}->{ccname} eq 'cl')
351 {
352 return ("-OUT:$file");
353 }
354 elsif($self->osname eq 'darwin')
355 {
356 return ('-install_name' => "\@rpath/$file", -o => $file);
357 }
358 else
359 {
360 return ('-o' => $file);
361 }
362 }
363  
364  
365 sub flag_exe_output
366 {
367 my $self = _self(shift);
368 my $file = shift;
369 if($self->osname eq 'MSWin32' && $self->{config}->{ccname} eq 'cl')
370 {
371 my $file = File::Spec->rel2abs($file);
372 return ("/Fe:$file");
373 }
374 else
375 {
376 return ('-o' => $file);
377 }
378 }
379  
380  
381 sub flag_export
382 {
383 my $self = _self(shift);
384 return () unless $self->osname eq 'MSWin32' && $self->{config}->{ccname} eq 'cl';
385 return map { "/EXPORT:$_" } @_;
386 }
387  
388  
389 sub which
390 {
391 my(undef, $command) = @_;
392 require IPC::Cmd;
393 my @command = ref $command ? @$command : ($command);
394 IPC::Cmd::can_run($command[0]);
395 }
396  
397  
398 sub run
399 {
400 my $self = shift;
401 my @command = map { ref $_ ? @$_ : $_ } grep { defined $_ } @_;
402 print "+@command\n";
403 system @command;
404 $?;
405 }
406  
407  
408 sub _c { join ',', @_ }
409 sub _l { join ' ', map { ref $_ ? @$_ : $_ } @_ }
410  
411 sub diag
412 {
413 my $self = _self(shift);
414 my @diag;
415  
416 push @diag, "osname : ". _c($self->osname);
417 push @diag, "cc : ". _l($self->cc);
418 push @diag, "cxx : ". (eval { _l($self->cxx) } || '---' );
419 push @diag, "cxxld : ". (eval { _l($self->cxxld) } || '---' );
420 push @diag, "for : ". (eval { _l($self->for) } || '---' );
421 push @diag, "ld : ". _l($self->ld);
422 push @diag, "ccflags : ". _l($self->ccflags);
423 push @diag, "ldflags : ". _l($self->ldflags);
424 push @diag, "object suffix : ". _c($self->object_suffix);
425 push @diag, "library prefix : ". _c($self->library_prefix);
426 push @diag, "library suffix : ". _c($self->library_suffix);
427 push @diag, "cc mm works : ". $self->cc_mm_works;
428  
429 join "\n", @diag;
430 }
431  
432 1;
433  
434 __END__