blib/lib/SPVM/Builder/CompileInfo.pm

filter: Statementstmt 56.7% 93 / 164 Branchbran 39.2% 11 / 28 Conditioncond 50.0% 2 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 70.5% 12 / 17 Podpod 80.0% 8 / 10 totaltotal 56.4% 127 / 225 CC 19 SCAR 17.6
CC
19
Cov
54%
create_ccflags
11.1
create_ccflags
11.1
create_command
6.0
CRAP 5.8
« blib/lib/SPVM/Global.pm ↑ Summary blib/lib/SPVM/Builder/Exe.pm »
1 package SPVM::Builder::CompileInfo;
2  
3 684 ▶use parent 'SPVM::Builder::CommandInfo';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 684 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 684 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 684 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 684 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 684 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 684 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Fields
13 684 my $field_names = [qw(
14 source_dir
15 source_rel_file
16 dependent_files
17 category
18 )];
19 684 has $field_names;
20  
21 # Class methods
22 8170 ▶sub new {
Subroutine new: called CC=1 SCAR=1.2
Pod documented
23
24 0 ▶ my $self = shift->SUPER::new(
Subroutine new: not called CC=1 SCAR=1.2
Pod documented
25 dependent_files => [],
26 @_
27 );
28
29 0 return $self;
30 }
31  
32 # Instance Methods
33 8170 ▶sub field_names {
Subroutine field_names: called CC=1 SCAR=1.2
Pod undocumented
34 0 ▶ my ($self) = @_;
Subroutine field_names: not called CC=1 SCAR=1.2
Pod undocumented
35
36 0 return [@{$self->SUPER::field_names}, @$field_names];
37 }
38  
39 229707 ▶sub create_command {
Subroutine create_command: called CC=4 SCAR=17.9
Pod documented
40 0 ▶ my ($self, $options) = @_;
Subroutine create_command: not called CC=4 SCAR=17.9
Pod documented
41
42 0 ▶ $options //= {};
Condition50%2/4
$options //= {}
l!l
10664219043
$options //= {}
l!l
00
MC/DC100%1/1
$options //= {}
$options
MC/DC0%0/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
Truth table0%0/2
A
A= $options
Aresult
11
00
43
44 0 my $no_output_option = $options->{no_output_option};
45
46 0 my $config = $self->config;
47
48 0 my $cc = $config->cc;
49
50 0 my $output_file = $self->output_file;
51 0 my $source_file = $self->source_file;
52
53 0 my $compile_command_args = $self->create_ccflags;
54
55 # Get output option name
56 0 my $cc_output_option_name = $config->cc_output_option_name;
57
58 # Build command
59 0 my @compile_command;
60
61 0 push @compile_command, ($cc, '-c');
62
63 0 ▶ if (length $output_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $output_file) 221537 8170 229707
if (length $output_file) 0 0 0
64 0 ▶ unless ($no_output_option) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($no_output_option) 219043 2494 221537
unless ($no_output_option) 0 0 0
65 0 my $output_option = $config->create_option_short($cc_output_option_name, $output_file);
66 0 push @compile_command, $output_option;
67 }
68 }
69
70 0 push @compile_command, (@$compile_command_args, $source_file);
71
72 0 return \@compile_command;
73 }
74  
75 229707 ▶sub create_ccflags {
Subroutine create_ccflags: called CC=6 SCAR=24.0
Pod documented
76 0 ▶ my ($self) = @_;
Subroutine create_ccflags: not called CC=6 SCAR=24.0
Pod documented
77
78 0 my $config = $self->config;
79
80 0 my $class_name = $config->class_name;
81
82 0 my @compile_command_args;
83
84 0 my $ccflags = $config->ccflags;
85 0 push @compile_command_args, grep { length $_ } @$ccflags;
86
87 0 my $defines = $config->defines;
88 0 push @compile_command_args, map { "-D$_" } grep { length $_ } @$defines;
89
90 0 my $std = $config->std;
91 0 ▶ if (length $std) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $std) 229707 0 229707
if (length $std) 0 0 0
92 0 push @compile_command_args, $config->create_option("-std", $std);
93 }
94
95 0 my $field_names = $config->get_cc_system_field_names;
96 0 my $output_type = $config->output_type;
97  
98 0 for my $field_name (@$field_names) {
99 0 push @compile_command_args, grep { length $_ } @{$config->$field_name};
100 }
101
102 0 my $optimize = $config->optimize;
103
104 0 ▶ if (length $optimize) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $optimize) 66438 163269 229707
if (length $optimize) 0 0 0
105 0 push @compile_command_args, split(/ +/, $optimize);
106 }
107
108 # include directories
109 {
110 0 my @all_include_dirs;
111
112 # SPVM core native directory
113 0 my $spvm_core_include_dir = $config->spvm_core_include_dir;
114 0 ▶ if (length $spvm_core_include_dir) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $spvm_core_include_dir) 229707 0 229707
if (length $spvm_core_include_dir) 0 0 0
115 0 push @all_include_dirs, $spvm_core_include_dir;
116 }
117
118 # include directories
119 0 my $include_dirs = $config->include_dirs;
120 0 push @all_include_dirs, grep { length $_ } @$include_dirs;
121
122 # Native include directory
123 0 my $native_include_dir = $config->native_include_dir;
124 0 ▶ if (length $native_include_dir) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $native_include_dir) 20229 209478 229707
if (length $native_include_dir) 0 0 0
125 0 push @all_include_dirs, $native_include_dir;
126 }
127
128 # Resource include directories
129 0 my $resource_include_dirs = $config->resource_include_dirs;
130 0 push @all_include_dirs, grep { length $_ } @$resource_include_dirs;
131
132 0 my @all_include_dirs_args = map { "-I" . SPVM::Builder::Util::quote_literal($_); } @all_include_dirs;
133
134 0 push @compile_command_args, @all_include_dirs_args;
135 }
136
137 0 return \@compile_command_args;
138 }
139  
140 237877 ▶sub source_file {
Subroutine source_file: called CC=2 SCAR=10.7
Pod documented
141 0 ▶ my $self = shift;
Subroutine source_file: not called CC=2 SCAR=10.7
Pod documented
142
143 0 my $source_dir = $self->source_dir;
144 0 my $source_rel_file = $self->source_rel_file;
145
146 0 ▶ unless ($source_dir) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($source_dir) 0 237877 237877
unless ($source_dir) 0 0 0
147 0 confess();
148 }
149
150 0 my $source_file = "$source_dir/$source_rel_file";
151
152 0 return $source_file;
153 }
154  
155 684 1;
156  
157 =head1 Name
158  
159 SPVM::Builder::CompileInfo - Compilation Unit Information
160  
161 =head1 Description
162  
163 SPVM::Builder::CompileInfo class manages information for a single compilation unit.
164  
165 =head1 Super Class
166  
167 L<SPVM::Builder::CommandInfo>
168  
169 =head1 Fields
170  
171 =head2 source_rel_file
172  
173 my $source_rel_file = $compile_info->source_rel_file;
174 $compile_info->source_rel_file($source_rel_file);
175  
176 Gets and sets the C<source_rel_file> field. It is a relative path of a source file.
177  
178 The base directory of this path is the L<SPVM::Builder::Config#source_dir|SPVM::Builder::Config/"source_dir"> field.
179  
180 =head2 dependent_files
181  
182 my $dependent_files = $compile_info->dependent_files;
183 $compile_info->dependent_files($dependent_files);
184  
185 Gets and sets the C<dependent_files> field. It is an array reference of the files that the source file depends on.
186  
187 If this field is defined, these files are added to the dependency list of a build rule in L<SPVM::Builder::Ninja>.
188  
189 If one of the dependent files is changed, the source file is recompiled.
190  
191 =head2 category
192  
193 my $category = $config->category;
194 $config->category($category);
195  
196 Gets and sets the C<category> field.
197  
198 The category must be one of the following values:
199  
200 =over 2
201  
202 =item * C<bootstrap>
203  
204 A bootstrap file.
205  
206 =item * C<spvm_core>
207  
208 An SPVM core source file.
209  
210 =item * C<native_source>
211  
212 A native source file in C<src/> directory.
213  
214 =item * C<native_class>
215  
216 A native class file.
217  
218 =item * C<precompile_class>
219  
220 A precompile class file.
221  
222 =back
223  
224 =head1 Class Methods
225  
226 =head2 new
227  
228 my $compile_info = SPVM::Builder::CompileInfo->new(%fields);
229  
230 Creates a new L<SPVM::Builder::CompileInfo> object given L</"Fields">, and returns it.
231  
232 Exceptions:
233  
234 L<"config"|SPVM::Builder::CommandInfo/"config"> field must be defined.
235  
236 =head1 Instance Methods
237  
238 =head2 create_command
239  
240 my $compile_command = $compile_info->create_command;
241 my $compile_command_no_output = $compile_info->create_command({no_output_option => 1});
242  
243 Creates an array reference of the compilation command, and returns it.
244  
245 If the C<no_output_option> option is a true value, the output option (e.g. C<-o foo.o>) is not added to the command.
246  
247 Return Value Examples:
248  
249 [qw(cc -o foo.o -c -O2 -Ipath/include foo.c)]
250  
251 # With no_output_option => 1
252 [qw(cc -c -O2 -Ipath/include foo.c)]
253  
254 =head2 create_ccflags
255  
256 my $config_args = $compile_info->create_ccflags;
257  
258 Creates an array reference of the compilation options, and returns it.
259  
260 The source file L<"source_file"> and the output file L<"output_file"|SPVM::Builder::CommandInfo/"output_file"> are not contained.
261  
262 Return Value Examples:
263  
264 [qw(-O2 -Ipath/include)]
265  
266 =head2 source_file
267  
268 my $source_file = $compile_info->source_file;
269 $compile_info->source_file($source_file);
270  
271 Gets the source file path from the L<SPVM::Builder::Config#source_dir|SPVM::Builder::Config/"source_dir"> field and the L</"source_rel_file"> field.
272  
273 =head1 Copyright & License
274  
275 Copyright (c) 2023 Yuki Kimoto
276  
277 MIT License
« blib/lib/SPVM/Global.pm ↑ Summary blib/lib/SPVM/Builder/Exe.pm »