CAPIMaker.pm

filter: Statementstmt 25.7% 33 / 128 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 62.5% 10 / 16 Podpod 0.0% 0 / 6 totaltotal 16.6% 43 / 259 CC 44 SCAR 54.2
CC
44
Cov
15%
check_config
506.0
load_decl
156.0
gen_file
30.0
CRAP 226.5
↑ Summary CAPIMaker/Template/C_API_H.pm »
1 package Module::CAPIMaker;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Text::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 our @EXPORT = qw(make_c_api);
14  
15 1 ▶use Module::CAPIMaker::Template::Module_H;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Module::CAPIMaker::Template::Module_C;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Module::CAPIMaker::Template::Sample_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Module::CAPIMaker::Template::C_API_H;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
21 0 my $class = shift;
22 0 my %config = @_;
23 0 my $self = { config => \%config,
24 function => {},
25 data => {}
26 };
27  
28 0 ▶ $config{c_api_decl_filename} //= 'c_api.decl';
Condition0%0/2
$config{"c_api_decl_filename"} //= "c_api.decl"
l!l
00
MC/DC0%0/1
$config{"c_api_decl_filename"} //= "c_api.decl"
$config{"c_api_decl_filename"}
Truth table0%0/2
A
A= $config{"c_api_decl_filename"}
Aresult
11
00
29  
30 0 bless $self, $class;
31 }
32  
33 0 ▶sub load_decl {
Subroutine load_decl: not called CC=12 SCAR=50.5
Pod undocumented
34 0 my $self = shift;
35 0 my $config = $self->{config};
36 0 my $fn = $config->{c_api_decl_filename};
37 0 ▶ open my $fh, '<', $fn or die "Unable to open $fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $fn or die "Unable to open ${fn}: $!\n" 0 0 0
38 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
39 0 chomp;
40 0 s/^\s+//; s/\s+$//;
41 0 ▶ next if /^(?:#.*)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(?:#.*)?$/ 0 0 0
42 0 ▶ while (s/\s*\\$/ /) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\s*\\$/ /) 0 0 0
43 0 my $next = <$fh>;
44 0 chomp $next;
45 0 $next =~ s/^\s+//; $next =~ s/\s+$//;
46 0 $_ .= $next;
47 }
48 0 ▶ if (my ($k, $v) = /^(\w+)\s*=\s*(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = /^(\w+)\s*=\s*(.*)/) { } 0 0 0
elsif (/^((?:\w+\b\s*(?:\*+\s*)?)*)(\w+)\s*\(\s*(.*?)\s*\)$/) { } 0 0 0
49 0 ▶ if (my ($mark) = $v =~ /^<<\s*(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($mark) = $v =~ /^<<\s*(\w+)$/) 0 0 0
50 0 $v = '';
51 0 while (1) {
52 0 my $line = <$fh>;
53 0 ▶ defined $line or die "Ending token '$mark' missing at $fn line $.\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $line or die "Ending token '${mark}' missing at $fn line $.\n" 0 0 0
54 0 ▶ last if $line =~ /^$mark$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$mark$/ 0 0 0
55 0 $v .= $line;
56 }
57 }
58 0 $self->{config}{$k} = $v;
59 }
60 elsif (/^((?:\w+\b\s*(?:\*+\s*)?)*)(\w+)\s*\(\s*(.*?)\s*\)$/) {
61 0 my $args = $3;
62 0 my %f = ( decl => $_,
63 type => $1,
64 name => $2,
65 args => $args );
66 0 $self->{function}{$2} = \%f;
67  
68 0 ▶ if ($f{pTHX} = $args =~ s/^pTHX(?:_\s+|$)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f{"pTHX"} = $args =~ s/^pTHX(?:_\s+|$)//) 0 0 0
69 0 $args =~ s/^void$//;
70 0 my @args = split /\s*,\s*/, $args;
71 # warn "args |$args| => |". join('-', @args) . "|";
72 0 $f{macro_args} = join(', ', ('a'..'z')[0..$#args]);
73 0 ▶ $f{call_args} = (@args ? 'aTHX_ (' . join('), (', ('a'..'z')[0..$#args]) .')' : 'aTHX');
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
74 }
75  
76 }
77 else {
78 0 die "Invalid declaration at $fn line $.\n";
79 }
80 }
81 }
82  
83 0 ▶sub check_config {
Subroutine check_config: not called CC=22 SCAR=62.3
Pod undocumented
84 0 my $self = shift;
85 0 my $config = $self->{config};
86  
87 0 my $module_name = $config->{module_name};
88 0 ▶ die "module_name declaration missing from $config->{decl_filename}\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $module_name 0 0 0
89 unless defined $module_name;
90  
91 0 ▶ die "Invalid value for module_name ($module_name)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $module_name =~ /^\w+(?:::\w+)*$/ 0 0 0
92 unless $module_name =~ /^\w+(?:::\w+)*$/;
93  
94 0 ▶ my $c_module_name = $config->{c_module_name} //= do { my $cmn = lc $module_name;
Condition0%0/3
$config->{"c_module_name"} //= do { my $cmn = lc $module_name; $cmn =~ s/\W+/_/g; $cmn }
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"c_module_name"} //= do { my $cmn = lc $module_name; $cmn =~ s/\W+/_/g; $cmn }
$config->{"c_module_name"} do { my $cmn = lc $module_name; $cmn =~ s/\W+/_/g; $cmn }
Truth table0%0/3
A //= B
A= $config->{"c_module_name"}B= do { my $cmn = lc $module_name; $cmn =~ s/\W+/_/g; $cmn }
ABresult
1X1
011
000
95 0 $cmn =~ s/\W+/_/g;
96 0 $cmn };
97 0 ▶ die "Invalid value for c_module_name ($c_module_name)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $c_module_name =~ /^\w+$/ 0 0 0
98 unless $c_module_name =~ /^\w+$/;
99  
100 0 ▶ $config->{author} //= 'Unknown';
Condition0%0/2
$config->{"author"} //= "Unknown"
l!l
00
MC/DC0%0/1
$config->{"author"} //= "Unknown"
$config->{"author"}
Truth table0%0/2
A
A= $config->{"author"}
Aresult
11
00
101 0 ▶ $config->{min_version} //= 1;
Condition0%0/2
$config->{"min_version"} //= 1
l!l
00
MC/DC0%0/1
$config->{"min_version"} //= 1
$config->{"min_version"}
Truth table0%0/2
A
A= $config->{"min_version"}
Aresult
11
00
102 0 ▶ $config->{max_version} //= 1;
Condition0%0/2
$config->{"max_version"} //= 1
l!l
00
MC/DC0%0/1
$config->{"max_version"} //= 1
$config->{"max_version"}
Truth table0%0/2
A
A= $config->{"max_version"}
Aresult
11
00
103  
104 die "Invalid version declaration, min_version ($config->{min_version}) > max_version ($config->{max_version})\n"
105 0 ▶ if $config->{max_version} < $config->{min_version};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"max_version"} < $config->{"min_version"} 0 0 0
106  
107 0 ▶ $config->{required_version} //= $config->{max_version};
Condition0%0/3
$config->{"required_version"} //= $config->{"max_version"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"required_version"} //= $config->{"max_version"}
$config->{"required_version"} $config->{"max_version"}
Truth table0%0/3
A //= B
A= $config->{"required_version"}B= $config->{"max_version"}
ABresult
1X1
011
000
108 0 ▶ $config->{module_version} //= '0';
Condition0%0/2
$config->{"module_version"} //= 0
l!l
00
MC/DC0%0/1
$config->{"module_version"} //= 0
$config->{"module_version"}
Truth table0%0/2
A
A= $config->{"module_version"}
Aresult
11
00
109 0 $config->{capimaker_version} = $VERSION;
110  
111 0 $config->{now} = strftime("%F %T", localtime);
112  
113 0 ▶ $config->{client_dir} //= 'c_api_client';
Condition0%0/2
$config->{"client_dir"} //= "c_api_client"
l!l
00
MC/DC0%0/1
$config->{"client_dir"} //= "c_api_client"
$config->{"client_dir"}
Truth table0%0/2
A
A= $config->{"client_dir"}
Aresult
11
00
114  
115 0 ▶ $config->{module_c_filename} //= "perl_$c_module_name.c";
Condition0%0/2
$config->{"module_c_filename"} //= "perl_$c_module_name.c"
l!l
00
MC/DC0%0/1
$config->{"module_c_filename"} //= "perl_$c_module_name.c"
$config->{"module_c_filename"}
Truth table0%0/2
A
A= $config->{"module_c_filename"}
Aresult
11
00
116 0 ▶ $config->{module_h_filename} //= "perl_$c_module_name.h";
Condition0%0/2
$config->{"module_h_filename"} //= "perl_$c_module_name.h"
l!l
00
MC/DC0%0/1
$config->{"module_h_filename"} //= "perl_$c_module_name.h"
$config->{"module_h_filename"}
Truth table0%0/2
A
A= $config->{"module_h_filename"}
Aresult
11
00
117 0 ▶ $config->{sample_xs_filename} //= "sample.xs";
Condition0%0/2
$config->{"sample_xs_filename"} //= "sample.xs"
l!l
00
MC/DC0%0/1
$config->{"sample_xs_filename"} //= "sample.xs"
$config->{"sample_xs_filename"}
Truth table0%0/2
A
A= $config->{"sample_xs_filename"}
Aresult
11
00
118 0 ▶ $config->{c_api_h_filename} //= "c_api.h";
Condition0%0/2
$config->{"c_api_h_filename"} //= "c_api.h"
l!l
00
MC/DC0%0/1
$config->{"c_api_h_filename"} //= "c_api.h"
$config->{"c_api_h_filename"}
Truth table0%0/2
A
A= $config->{"c_api_h_filename"}
Aresult
11
00
119  
120 0 ▶ $config->{module_h_barrier} //= do { my $ib = "$config->{module_h_filename}_INCLUDED";
Condition0%0/3
$config->{"module_h_barrier"} //= do { my $ib = qq[$config->{"module_h_filename"}_INCLUDED]; $ib =~ s/\W+/_/g; uc $ib }
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"module_h_barrier"} //= do { my $ib = qq[$config->{"module_h_filename"}_INCLUDED]; $ib =~ s/\W+/_/g; uc $ib }
$config->{"module_h_barrier"} do { my $ib = qq[$config->{"module_h_filename"}_INCLUDED]; $ib =~ s/\W+/_/g; uc $ib }
Truth table0%0/3
A //= B
A= $config->{"module_h_barrier"}B= do { my $ib = qq[$config->{"module_h_filename"}_INCLUDED]; $ib =~ s/\W+/_/g; uc $ib }
ABresult
1X1
011
000
121 0 $ib =~ s/\W+/_/g;
122 0 uc $ib };
123 die "Invalid value for module_h_barrier ($config->{module_h_barrier})\n"
124 0 ▶ unless $config->{module_h_barrier} =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"module_h_barrier"} =~ /^\w+$/ 0 0 0
125  
126 0 ▶ $config->{c_api_h_barrier} //= do { my $ib = "$config->{c_api_h_filename}_INCLUDED";
Condition0%0/3
$config->{"c_api_h_barrier"} //= do { my $ib = qq[$config->{"c_api_h_filename"}_INCLUDED]; $ib =~ s/\W+/_/g; uc $ib }
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"c_api_h_barrier"} //= do { my $ib = qq[$config->{"c_api_h_filename"}_INCLUDED]; $ib =~ s/\W+/_/g; uc $ib }
$config->{"c_api_h_barrier"} do { my $ib = qq[$config->{"c_api_h_filename"}_INCLUDED]; $ib =~ s/\W+/_/g; uc $ib }
Truth table0%0/3
A //= B
A= $config->{"c_api_h_barrier"}B= do { my $ib = qq[$config->{"c_api_h_filename"}_INCLUDED]; $ib =~ s/\W+/_/g; uc $ib }
ABresult
1X1
011
000
127 0 $ib =~ s/\W+/_/g;
128 0 uc $ib };
129 die "Invalid value for c_api_h_barrier ($config->{c_api_h_barrier})\n"
130 0 ▶ unless $config->{c_api_h_barrier} =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"c_api_h_barrier"} =~ /^\w+$/ 0 0 0
131  
132  
133 0 ▶ $config->{$_} //= '' for qw(export_prefix
Condition0%0/2
$config->{$_} //= ""
l!l
00
MC/DC0%0/1
$config->{$_} //= ""
$config->{$_}
Truth table0%0/2
A
A= $config->{$_}
Aresult
11
00
134 module_c_beginning
135 module_c_end
136 module_h_beginning
137 module_h_end);
138 }
139  
140 0 ▶sub gen_file {
Subroutine gen_file: not called CC=5 SCAR=34.0
Pod undocumented
141 0 my ($self, $template, $dir, $save_as) = @_;
142 0 my $config = $self->{config};
143 0 ▶ system mkdir => -p => $dir unless -d $dir; # FIX ME!
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
144 0 $save_as = File::Spec->rel2abs(File::Spec->join($dir, $save_as));
145 0 ▶ open my $fh, '>', $save_as or die "Unable to create $save_as: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $save_as or die "Unable to create ${save_as}: $!\n" 0 0 0
146 0 local $Text::Template::ERROR;
147 0 ▶ my $tt = Text::Template->new(TYPE => (ref $template ? 'ARRAY' : 'FILE'),
Branch: 1 branch
BranchTrueFalseTotal
ref $template ? : 0 0 0
148 SOURCE => $template,
149 DELIMITERS => ['<%', '%>'] );
150 $tt->fill_in(HASH => { %$config, function => $self->{function} },
151 0 OUTPUT => $fh);
152 0 ▶ warn "Some error happened while generating $save_as: $Text::Template::ERROR\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Text::Template::ERROR 0 0 0
153 if $Text::Template::ERROR;
154 }
155  
156 0 ▶sub gen_all {
Subroutine gen_all: not called CC=5 SCAR=34.0
Pod undocumented
157 0 my $self = shift;
158 0 my $config = $self->{config};
159 $self->gen_file($config->{module_c_template_filename} // \@Module::CAPIMaker::Template::Module_C::template,
160 $config->{client_dir},
161 0 ▶ $config->{module_c_filename});
Condition0%0/2
$config->{"module_c_template_filename"} // \@Module::CAPIMaker::Template::Module_C::template
l!l
00
MC/DC0%0/1
$config->{"module_c_template_filename"} // \@Module::CAPIMaker::Template::Module_C::template
$config->{"module_c_template_filename"}
Truth table0%0/2
A
A= $config->{"module_c_template_filename"}
Aresult
11
00
162 $self->gen_file($config->{module_h_template_filename} // \@Module::CAPIMaker::Template::Module_H::template,
163 $config->{client_dir},
164 0 ▶ $config->{module_h_filename});
Condition0%0/2
$config->{"module_h_template_filename"} // \@Module::CAPIMaker::Template::Module_H::template
l!l
00
MC/DC0%0/1
$config->{"module_h_template_filename"} // \@Module::CAPIMaker::Template::Module_H::template
$config->{"module_h_template_filename"}
Truth table0%0/2
A
A= $config->{"module_h_template_filename"}
Aresult
11
00
165 $self->gen_file($config->{sample_xs_template_filename} // \@Module::CAPIMaker::Template::Sample_XS::template,
166 $config->{client_dir},
167 0 ▶ $config->{sample_xs_filename});
Condition0%0/2
$config->{"sample_xs_template_filename"} // \@Module::CAPIMaker::Template::Sample_XS::template
l!l
00
MC/DC0%0/1
$config->{"sample_xs_template_filename"} // \@Module::CAPIMaker::Template::Sample_XS::template
$config->{"sample_xs_template_filename"}
Truth table0%0/2
A
A= $config->{"sample_xs_template_filename"}
Aresult
11
00
168 $self->gen_file($config->{c_api_h_template_filename} // \@Module::CAPIMaker::Template::C_API_H::template,
169 '.',
170 0 ▶ $config->{c_api_h_filename});
Condition0%0/2
$config->{"c_api_h_template_filename"} // \@Module::CAPIMaker::Template::C_API_H::template
l!l
00
MC/DC0%0/1
$config->{"c_api_h_template_filename"} // \@Module::CAPIMaker::Template::C_API_H::template
$config->{"c_api_h_template_filename"}
Truth table0%0/2
A
A= $config->{"c_api_h_template_filename"}
Aresult
11
00
171 }
172  
173 0 ▶sub make_c_api {
Subroutine make_c_api: not called CC=3 SCAR=24.8
Pod undocumented
174 0 my %args;
175 0 for (@ARGV) {
176 0 ▶ /^\s*(\w+)\s*=\s*(.*?)\s*$/
Branch: 1 branch
BranchTrueFalseTotal
/^\s*(\w+)\s*=\s*(.*?)\s*$/ or die "Bad argument '${_}'\n" 0 0 0
177 or die "Bad argument '$_'\n";
178 0 $args{$1} = $2;
179 }
180 0 my $mcm = Module::CAPIMaker->new(%args);
181 0 $mcm->load_decl;
182 0 $mcm->check_config;
183 0 $mcm->gen_all;
184 }
185  
186 1 1;
187 __END__
↑ Summary CAPIMaker/Template/C_API_H.pm »