CheckCompiler.pm

filter: Statementstmt 94.7% 54 / 57 Branchbran 65.3% 17 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 84.3% 86 / 102 CC 15 SCAR 17.4
CC
15
Cov
84%
check_compile
10.9
_is_gcc
4.4
_gcc_version
2.3
CRAP 5.7
↑ Summary AssertC99.pm »
1 package Devel::CheckCompiler;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 our $VERSION = '0.07';
6 5 ▶use parent qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 our @EXPORT = qw/check_c99 check_c99_or_exit check_compile/;
9 5 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use ExtUtils::CBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 my $C99_SOURCE = <<'C99';
13 // include a C99 header
14 #include <stdbool.h>
15 inline // a C99 keyword with C99 style comments
16 int test_c99() {
17 int i = 0;
18 i++;
19 int j = i - 1; // another C99 feature: declaration after statement
20 // another C99 feature: for loop variable declarations
21 for (int k = 0; k < 3; i++) {
22 }
23 return j;
24 }
25 C99
26  
27 4 ▶sub check_c99 {
Subroutine check_c99: called CC=1 SCAR=0.0
Pod documented
28 4 check_compile($C99_SOURCE);
29 }
30  
31 4 ▶sub check_c99_or_exit {
Subroutine check_c99_or_exit: called CC=2 SCAR=6.9
Pod documented
32 4 ▶ check_compile($C99_SOURCE) or do {
Branch: 1 branch
BranchTrueFalseTotal
check_compile($C99_SOURCE) or do { warn "Your system is not support C99(OS unsupported)\n"; exit 0 } 2 2 4
33 2 warn "Your system is not support C99(OS unsupported)\n";
34 2 exit 0;
35 };
36 }
37  
38 7 ▶sub _is_gcc {
Subroutine _is_gcc: called CC=4 SCAR=14.9
39 7 ▶ return 0 if $Config{gccversion} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::CheckCompiler::Config{"gccversion"} eq "" 0 7 7
40 # For clang on MacOSX and *BSD distributions
41 7 ▶ return 0 if $Config{gccversion} =~ m/clang/i;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::CheckCompiler::Config{"gccversion"} =~ /clang/i 0 7 7
42 # For Intel C and C++ compiler
43 7 ▶ return 0 if $Config{gccversion} =~ m/intel/i;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::CheckCompiler::Config{"gccversion"} =~ /intel/i 0 7 7
44  
45 7 return 1;
46 }
47  
48 7 ▶sub _gcc_version {
Subroutine _gcc_version: called CC=2 SCAR=8.1
49 7 ▶ if ($Config{gccversion} =~ m/^(\d+)\.(\d+)\.(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Devel::CheckCompiler::Config{"gccversion"} =~ /^(\d+)\.(\d+)\.(\d+)/) 7 0 7
50 return {
51 7 major => $1,
52 minor => $2,
53 patch => $3,
54 };
55 }
56  
57 0 return;
58 }
59  
60 11 ▶sub check_compile {
Subroutine check_compile: called CC=10 SCAR=23.8
Pod documented
61 11 my ($src, %opt) = @_;
62  
63 11 my $cbuilder = ExtUtils::CBuilder->new(quiet => 1);
64 11 ▶ return 0 unless $cbuilder->have_compiler;
Branch: 1 branch
BranchTrueFalseTotal
unless $cbuilder->have_compiler 4 7 11
65  
66 7 require File::Temp;
67  
68 7 my $tmpfile = File::Temp->new(SUFFIX => '.c');
69 7 $tmpfile->print($src);
70 7 $tmpfile->close();
71  
72 7 my $compile_flags;
73 7 ▶ if (_is_gcc()) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_gcc()) 7 0 7
74 7 my $gcc_version = _gcc_version();
75 # '-std=gnu11' is default from GCC 5
76 7 ▶ if (defined $gcc_version && $gcc_version->{major} < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gcc_version and $gcc_version->{"major"} < 5) 0 7 7
Condition33%1/3
defined $gcc_version and $gcc_version->{"major"} < 5
!ll&&!rl&&r
070
MC/DC0%0/2
defined $gcc_version and $gcc_version->{"major"} < 5
defined $gcc_version $gcc_version->{"major"} < 5
Truth table33%1/3
A and B
A= defined $gcc_versionB= $gcc_version->{"major"} < 5
ABresult
0X0
100
111
77 0 $compile_flags = ['-std=c99'];
78 }
79 }
80  
81 7 my $objname = eval {
82 7 $cbuilder->compile(source => $tmpfile->filename, extra_compiler_flags => $compile_flags);
83 };
84 7 ▶ if ($objname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objname) { } 6 1 7
85 6 my $ret = 1;
86 6 ▶ if ($opt{executable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"executable"}) 2 4 6
87 2 my $execfile = eval {
88 $cbuilder->link_executable(objects => $objname,
89 2 extra_linker_flags => $opt{extra_linker_flags});
90 };
91  
92 2 ▶ if ($execfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($execfile) { } 2 0 2
93 2 ▶ unlink $execfile or warn "Cannot unlink $execfile (ignored): $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $execfile or warn "Cannot unlink $execfile (ignored): $!" 2 0 2
94 } else {
95 0 $ret = 0;
96 }
97 }
98 6 ▶ unlink $objname or warn "Cannot unlink $objname (ignored): $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $objname or warn "Cannot unlink $objname (ignored): $!" 6 0 6
99 6 return $ret;
100 } else {
101 1 return 0;
102 }
103 }
104  
105 5 1;
106 __END__
↑ Summary AssertC99.pm »