blib/lib/ExtUtils/CppGuess.pm

filter: Statementstmt 77.5% 76 / 98 Branchbran 38.0% 19 / 50 Conditioncond 33.3% 9 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 76.9% 20 / 26 Podpod 87.5% 7 / 8 totaltotal 58.4% 131 / 224 CC 36 SCAR 20.0
CC
36
Cov
59%
new
21.4
_guess_win32
12.0
guess_compiler
6.1
CRAP 7.4
↑ Summary Makefile.PL »
1 package ExtUtils::CppGuess;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 ExtUtils::CppGuess - guess C++ compiler and flags
9  
10 =head1 SYNOPSIS
11  
12 With L<Extutils::MakeMaker>:
13  
14 use ExtUtils::CppGuess;
15  
16 my $guess = ExtUtils::CppGuess->new;
17  
18 WriteMakefile
19 ( # MakeMaker args,
20 $guess->makemaker_options,
21 );
22  
23 With L<Module::Build>:
24  
25 my $guess = ExtUtils::CppGuess->new;
26  
27 my $build = Module::Build->new
28 ( # Module::Build arguments
29 $guess->module_build_options,
30 );
31 $build->create_build_script;
32  
33 =head1 DESCRIPTION
34  
35 C<ExtUtils::CppGuess> attempts to guess the system's C++ compiler
36 that is compatible with the C compiler that your perl was built with.
37  
38 It can generate the necessary options to the L<Module::Build>
39 constructor or to L<ExtUtils::MakeMaker>'s C<WriteMakefile>
40 function.
41  
42 =head1 METHODS
43  
44 =head2 new
45  
46 Creates a new C<ExtUtils::CppGuess> object.
47 Takes the path to the C compiler as the C<cc> argument,
48 but falls back to the value of C<$Config{cc}>, which should
49 be what you want anyway.
50  
51 You can specify C<extra_compiler_flags> and C<extra_linker_flags>
52 (as strings) which will be merged in with the auto-detected ones.
53  
54 =head2 module_build_options
55  
56 Returns the correct options to the constructor of C<Module::Build>.
57 These are:
58  
59 extra_compiler_flags
60 extra_linker_flags
61  
62 =head2 makemaker_options
63  
64 Returns the correct options to the C<WriteMakefile> function of
65 C<ExtUtils::MakeMaker>.
66 These are:
67  
68 CCFLAGS
69 dynamic_lib => { OTHERLDFLAGS => ... }
70  
71 If you specify the extra compiler or linker flags in the
72 constructor, they'll be merged into C<CCFLAGS> or
73 C<OTHERLDFLAGS> respectively.
74  
75 =head2 is_gcc
76  
77 Returns true if the detected compiler is in the gcc family.
78  
79 =head2 is_msvc
80  
81 Returns true if the detected compiler is in the MS VC family.
82  
83 =head2 add_extra_compiler_flags
84  
85 Takes a string as argument that is added to the string of extra compiler
86 flags.
87  
88 =head2 add_extra_linker_flags
89  
90 Takes a string as argument that is added to the string of extra linker
91 flags.
92  
93 =head1 AUTHOR
94  
95 Mattia Barbon <mbarbon@cpan.org>
96  
97 Steffen Mueller <smueller@cpan.org>
98  
99 Tobias Leich <froggs@cpan.org>
100  
101 =head1 COPYRIGHT AND LICENSE
102  
103 Copyright 2010, 2011 by Mattia Barbon.
104  
105 This program is free software; you can redistribute it and/or
106 modify it under the same terms as Perl itself.
107  
108 =cut
109  
110 1 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
111 1 ▶use File::Basename qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
112 1 ▶use Capture::Tiny 'capture_merged';
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 1 our $VERSION = '0.11';
115  
116 sub new {
117 1 ▶ my( $class, %args ) = @_;
Subroutine new: called CC=11 SCAR=30.6
Pod documented
118 1 my $self = bless { %args }, $class;
119  
120 # Allow override of default %Config::Config; useful in testing.
121 1 ▶ if( ! exists $self->{config} || ! defined $self->{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"config"} and defined $self->{"config"}) 1 0 1
Condition33%1/3
exists $self->{"config"} and defined $self->{"config"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $self->{"config"} and defined $self->{"config"}
exists $self->{"config"} defined $self->{"config"}
Truth table33%1/3
A and B
A= exists $self->{"config"}B= defined $self->{"config"}
ABresult
0X0
100
111
122 1 $self->{config} = \%Config::Config;
123 }
124  
125 # Allow a 'cc' %args. If not supplied, pull from {config}, or $Config{cc}.
126 1 ▶ if( ! exists $self->{cc} || ! defined $self->{cc} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"cc"} and defined $self->{"cc"}) 1 0 1
Condition33%1/3
exists $self->{"cc"} and defined $self->{"cc"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $self->{"cc"} and defined $self->{"cc"}
exists $self->{"cc"} defined $self->{"cc"}
Truth table33%1/3
A and B
A= exists $self->{"cc"}B= defined $self->{"cc"}
ABresult
0X0
100
111
127 $self->{cc}
128 = exists $self->{config}{cc} && defined $self->{config}{cc}
129 ? $self->{config}{cc}
130 1 ▶ : $Config::Config{cc};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'config'}{'cc'} && defined $self->{'config'}{'cc'} ? : 1 0 1
Condition33%1/3
exists $self->{"config"}{"cc"} and defined $self->{"config"}{"cc"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $self->{"config"}{"cc"} and defined $self->{"config"}{"cc"}
exists $self->{"config"}{"cc"} defined $self->{"config"}{"cc"}
Truth table33%1/3
A and B
A= exists $self->{"config"}{"cc"}B= defined $self->{"config"}{"cc"}
ABresult
0X0
100
111
131 }
132  
133 # Set up osname.
134 1 ▶ if( ! exists $self->{os} || ! defined $self->{os} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"os"} and defined $self->{"os"}) 1 0 1
Condition33%1/3
exists $self->{"os"} and defined $self->{"os"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $self->{"os"} and defined $self->{"os"}
exists $self->{"os"} defined $self->{"os"}
Truth table33%1/3
A and B
A= exists $self->{"os"}B= defined $self->{"os"}
ABresult
0X0
100
111
135 $self->{os}
136 = exists $self->{config}{osname} && defined $self->{config}{osname}
137 ? $self->{config}{osname}
138 1 ▶ : $^O;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'config'}{'osname'} && defined $self->{'config'}{'osname'} ? : 1 0 1
Condition33%1/3
exists $self->{"config"}{"osname"} and defined $self->{"config"}{"osname"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $self->{"config"}{"osname"} and defined $self->{"config"}{"osname"}
exists $self->{"config"}{"osname"} defined $self->{"config"}{"osname"}
Truth table33%1/3
A and B
A= exists $self->{"config"}{"osname"}B= defined $self->{"config"}{"osname"}
ABresult
0X0
100
111
139 }
140  
141 1 return $self;
142 }
143  
144 # Thus saith the law: All references to %Config::Config shall come through
145 # $self->_config. Accessors shall provide access to key components thereof.
146 # Testing shall thus grow stronger, verifying performance for platforms diverse
147 # to which access we have not.
148  
149 2 ▶sub _config { shift->{config} }
Subroutine _config: called CC=1 SCAR=0.0
150 1 ▶sub _cc { shift->{cc} }
Subroutine _cc: called CC=1 SCAR=0.0
151 2 ▶sub _os { shift->{os} }
Subroutine _os: called CC=1 SCAR=0.0
152  
153  
154 sub guess_compiler {
155 4 ▶ my $self = shift;
Subroutine guess_compiler: called CC=5 SCAR=18.1
Pod undocumented
156  
157 4 ▶ return $self->{guess} if $self->{guess};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"guess"} 3 1 4
158  
159 1 ▶ if( $self->_os =~ /^mswin/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_os =~ /^mswin/i) { } 0 1 1
160 0 ▶ $self->_guess_win32() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_guess_win32 or return 0 0 0
161 } else {
162 1 ▶ $self->_guess_unix() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_guess_unix or return 0 1 1
163 }
164 1 return $self->{guess};
165 }
166  
167  
168 sub _get_cflags {
169 2 ▶ my $self = shift;
Subroutine _get_cflags: called CC=3 SCAR=11.2
170  
171 2 ▶ $self->guess_compiler or die;
Branch: 1 branch
BranchTrueFalseTotal
$self->guess_compiler or die 0 2 2
172  
173 2 my $cflags = ' ' . $self->_config->{ccflags};
174 2 $cflags .= ' ' . $self->{guess}{extra_cflags};
175 $cflags .= ' ' . $self->{extra_compiler_flags}
176 2 ▶ if defined $self->{extra_compiler_flags};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"extra_compiler_flags"} 0 2 2
177  
178 2 return $cflags;
179 }
180  
181  
182 sub _get_lflags {
183 2 ▶ my $self = shift;
Subroutine _get_lflags: called CC=3 SCAR=11.3
184  
185 2 ▶ $self->guess_compiler || die;
Branch: 1 branch
BranchTrueFalseTotal
$self->guess_compiler or die 0 2 2
186  
187 2 my $lflags = $self->{guess}{extra_lflags};
188 $lflags .= ' ' . $self->{extra_linker_flags}
189 2 ▶ if defined $self->{extra_linker_flags};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"extra_linker_flags"} 0 2 2
190  
191 2 return $lflags;
192 }
193  
194  
195 sub makemaker_options {
196 1 ▶ my $self = shift;
Subroutine makemaker_options: called CC=1 SCAR=0.0
Pod documented
197  
198 1 my $lflags = $self->_get_lflags;
199 1 my $cflags = $self->_get_cflags;
200  
201 return (
202 1 CCFLAGS => $cflags,
203 dynamic_lib => { OTHERLDFLAGS => $lflags },
204 );
205 }
206  
207  
208 sub module_build_options {
209 1 ▶ my $self = shift;
Subroutine module_build_options: called CC=1 SCAR=0.0
Pod documented
210  
211 1 my $lflags = $self->_get_lflags;
212 1 my $cflags = $self->_get_cflags;
213  
214 return (
215 1 extra_compiler_flags => $cflags,
216 extra_linker_flags => $lflags,
217 );
218 }
219  
220  
221 sub _guess_win32 {
222 0 ▶ my $self = shift;
Subroutine _guess_win32: not called CC=3 SCAR=24.8
223 0 my $c_compiler = $self->_cc;
224 # $c_compiler = $Config::Config{cc} if not defined $c_compiler;
225  
226 0 ▶ if( $self->_cc_is_gcc( $c_compiler ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_cc_is_gcc($c_compiler)) { } 0 0 0
elsif ($self->_cc_is_msvc($c_compiler)) { } 0 0 0
227 $self->{guess} = {
228 0 extra_cflags => ' -xc++ ',
229 extra_lflags => ' -lstdc++ ',
230 };
231 } elsif( $self->_cc_is_msvc( $c_compiler ) ) {
232 $self->{guess} = {
233 0 extra_cflags => ' -TP -EHsc ',
234 extra_lflags => ' msvcprt.lib ',
235 };
236 } else {
237 0 die "Unable to determine a C++ compiler for '$c_compiler'";
238 }
239  
240 0 return 1;
241 }
242  
243  
244 sub _guess_unix {
245 1 ▶ my $self = shift;
Subroutine _guess_unix: called CC=4 SCAR=15.5
246 1 my $c_compiler = $self->_cc;
247 # $c_compiler = $Config::Config{cc} if not defined $c_compiler;
248  
249 1 ▶ if( !$self->_cc_is_gcc( $c_compiler ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_cc_is_gcc($c_compiler)) 0 1 1
250 0 die "Unable to determine a C++ compiler for '$c_compiler'";
251 }
252  
253 $self->{guess} = {
254 1 extra_cflags => ' -xc++ ',
255 extra_lflags => ' -lstdc++ ',
256 };
257 $self->{guess}{extra_lflags} .= ' -lgcc_s'
258 1 ▶ if $self->_os eq 'netbsd' && $self->{guess}{extra_lflags} !~ /-lgcc_s/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_os eq "netbsd" and not $self->{"guess"}{"extra_lflags"} =~ /-lgcc_s/ 0 1 1
Condition33%1/3
$self->_os eq "netbsd" and not $self->{"guess"}{"extra_lflags"} =~ /-lgcc_s/
!ll&&!rl&&r
100
MC/DC0%0/2
$self->_os eq "netbsd" and not $self->{"guess"}{"extra_lflags"} =~ /-lgcc_s/
$self->_os eq "netbsd" not $self->{"guess"}{"extra_lflags"} =~ /-lgcc_s/
Truth table33%1/3
A and not(B)
A= $self->_os eq "netbsd"B= not $self->{"guess"}{"extra_lflags"} =~ /-lgcc_s/
ABresult
0X0
100
111
259  
260 1 return 1;
261 }
262  
263 # originally from Alien::wxWidgets::Utility
264 # Why was this hanging around outside of all functions, and without any other
265 # use of $quotes?
266 # my $quotes = $self->_os =~ /MSWin32/ ? '"' : "'";
267  
268 sub _capture {
269 2 ▶ my @cmd = @_;
Subroutine _capture: called CC=2 SCAR=7.0
270  
271 2 ▶ my $out = capture_merged { system(@cmd) };
Subroutine __ANON__: called CC=1 SCAR=0.0
272 2 ▶ $out = '' if not defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 2 2
273  
274 2 return $out;
275 }
276  
277 # capture the output of a command that is run with piping
278 # to stdin of the command. We immediately close the pipe.
279 sub _capture_empty_stdin {
280 1 ▶ my $cmd = shift;
Subroutine _capture_empty_stdin: called CC=2 SCAR=7.1
281 my $out = capture_merged {
282 1 ▶ if ( open my $fh, '|-', $cmd ) {
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "|-", $cmd) 1 0 1
283 1 close $fh;
284 }
285 1 };
286 1 ▶ $out = '' if not defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 1 1
287  
288 1 return $out;
289 }
290  
291  
292 sub _cc_is_msvc {
293 0 ▶ my( $self, $cc ) = @_;
Subroutine _cc_is_msvc: not called CC=2 SCAR=17.9
294 $self->{is_msvc}
295 0 ▶ = ($self->_os =~ /MSWin32/ and File::Basename::basename($cc) =~ /^cl/i);
Condition0%0/3
$self->_os =~ /MSWin32/ && File::Basename::basename($cc) =~ /^cl/i
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_os =~ /MSWin32/ && File::Basename::basename($cc) =~ /^cl/i
$self->_os =~ /MSWin32/ File::Basename::basename($cc) =~ /^cl/i
Truth table0%0/3
A && B
A= $self->_os =~ /MSWin32/B= File::Basename::basename($cc) =~ /^cl/i
ABresult
0X0
100
111
296 0 return $self->{is_msvc};
297 }
298  
299  
300 sub _cc_is_gcc {
301 1 ▶ my( $self, $cc ) = @_;
Subroutine _cc_is_gcc: called CC=5 SCAR=17.2
302  
303 1 $self->{is_gcc} = 0;
304 1 my $cc_version = _capture( "$cc --version" );
305 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($cc_version =~ /\bg(?:cc|\+\+)/i or scalar _capture("$cc") =~ /\bgcc\b/i or scalar _capture_empty_stdin("$cc -dM -E -") =~ /__GNUC__/ or scalar $cc_version =~ /\bcc\b.*Free Software Foundation/is) 1 0 1
Condition50%3/6
$cc_version =~ /\bg(?:cc|\+\+)/i or scalar _capture("$cc") =~ /\bgcc\b/i or scalar _capture_empty_stdin("$cc -dM -E -") =~ /__GNUC__/ or scalar $cc_version =~ /\bcc\b.*Free Software Foundation/is
l!l
10
$cc_version =~ /\bg(?:cc|\+\+)/i or scalar _capture("$cc") =~ /\bgcc\b/i or scalar _capture_empty_stdin("$cc -dM -E -") =~ /__GNUC__/
l!l
01
$cc_version =~ /\bg(?:cc|\+\+)/i or scalar _capture("$cc") =~ /\bgcc\b/i
l!l
01
MC/DC0%0/1
$cc_version =~ /\bg(?:cc|\+\+)/i or scalar _capture("$cc") =~ /\bgcc\b/i or scalar _capture_empty_stdin("$cc -dM -E -") =~ /__GNUC__/ or scalar $cc_version =~ /\bcc\b.*Free Software Foundation/is
$cc_version =~ /\bg(?:cc|\+\+)/i
Truth table50%1/2
A
A= $cc_version =~ /\bg(?:cc|\+\+)/i
Aresult
11
00
306 $cc_version =~ m/\bg(?:cc|\+\+)/i # 3.x, some 4.x
307 || scalar( _capture( "$cc" ) =~ m/\bgcc\b/i ) # 2.95
308 || scalar(_capture_empty_stdin("$cc -dM -E -") =~ /__GNUC__/) # more or less universal?
309 || scalar($cc_version =~ m/\bcc\b.*Free Software Foundation/si) # some 4.x?
310 ) {
311 1 $self->{is_gcc} = 1;
312 }
313  
314 1 return $self->{is_gcc};
315 }
316  
317  
318 sub is_gcc {
319 0 ▶ my $self = shift;
Subroutine is_gcc: not called CC=2 SCAR=17.9
Pod documented
320 0 ▶ $self->guess_compiler || die;
Branch: 1 branch
BranchTrueFalseTotal
$self->guess_compiler or die 0 0 0
321 0 return $self->{is_gcc};
322 }
323  
324 sub is_msvc {
325 0 ▶ my $self = shift;
Subroutine is_msvc: not called CC=2 SCAR=17.9
Pod documented
326  
327 0 ▶ $self->guess_compiler || die;
Branch: 1 branch
BranchTrueFalseTotal
$self->guess_compiler or die 0 0 0
328  
329 0 return $self->{is_msvc};
330 }
331  
332 sub add_extra_compiler_flags {
333 0 ▶ my( $self, $string ) = @_;
Subroutine add_extra_compiler_flags: not called CC=2 SCAR=17.9
Pod documented
334  
335 $self->{extra_compiler_flags}
336 = defined($self->{extra_compiler_flags})
337 0 ▶ ? $self->{extra_compiler_flags} . ' ' . $string
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'extra_compiler_flags'} ? : 0 0 0
338 : $string;
339 }
340  
341  
342 sub add_extra_linker_flags {
343 0 ▶ my( $self, $string ) = @_;
Subroutine add_extra_linker_flags: not called CC=2 SCAR=17.9
Pod documented
344 $self->{extra_linker_flags}
345 = defined($self->{extra_linker_flags})
346 0 ▶ ? $self->{extra_linker_flags} . ' ' . $string
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'extra_linker_flags'} ? : 0 0 0
347 : $string;
348 }
349  
350  
351 1 1;
↑ Summary Makefile.PL »