blib/lib/SPVM/Builder/Config/Base.pm

filter: Statementstmt 87.3% 117 / 134 Branchbran 47.7% 21 / 44 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 95.2% 20 / 21 Podpod 33.3% 4 / 12 totaltotal 71.8% 166 / 231 CC 31 SCAR 14.4
CC
31
Cov
75%
get_base_dir
8.9
get_base_dir
8.9
new
5.4
CRAP 4.2
« blib/lib/SPVM/Builder/Native/BasicType.pm ↑ Summary blib/lib/SPVM/ExchangeAPI/CallerInfo.pm »
1 package SPVM::Builder::Config::Base;
2  
3 1052 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 913 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1052 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 913 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 913 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 913 ▶use File::Basename 'dirname', 'fileparse';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 913 ▶use SPVM::Builder::Config::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Base Fields
12 913 my $base_fields = [qw(
13 mode
14 file
15 class_name
16 category
17 quiet
18 is_resource
19 loaded_config_files
20 global
21 resource_loader_config
22 )];
23  
24 # Accessors generation for base fields
25 913 has($base_fields);
26  
27 31024 ▶sub new_empty {
Subroutine new_empty: called CC=2 SCAR=7.4
Pod undocumented
28 4 ▶ my $class = shift;
Subroutine new_empty: called CC=2 SCAR=7.4
Pod undocumented
29
30 4 my $self = {@_};
31
32 4 ▶ bless $self, ref $class || $class;
Condition33%2/6
ref $class || $class
l!l&&r!l&&!r
02960
ref $class || $class
l!l&&r!l&&!r
0310240
MC/DC0%0/2
ref $class || $class
ref $class $class
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
33
34 4 SPVM::Builder::Util::check_option_names($self, $self->option_names);
35
36 }
37  
38 3876 ▶sub new {
Subroutine new: called CC=5 SCAR=16.8
Pod documented
39 4 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.8
Pod documented
40
41 4 my $self = {@_};
42
43 4 ▶ bless $self, ref $class || $class;
Condition33%2/6
ref $class || $class
l!l&&r!l&&!r
0370
ref $class || $class
l!l&&r!l&&!r
038760
MC/DC0%0/2
ref $class || $class
ref $class $class
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
44
45 4 SPVM::Builder::Util::check_option_names($self, $self->option_names);
46
47 # category
48 4 ▶ unless (exists $self->{category}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"category"}) 37 0 37
unless (exists $self->{"category"}) 3876 0 3876
49 4 $self->category('native');
50 }
51  
52 4 ▶ unless (exists $self->{loaded_config_files}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"loaded_config_files"}) 37 0 37
unless (exists $self->{"loaded_config_files"}) 3876 0 3876
53 4 $self->{loaded_config_files} = [];
54 }
55
56 4 ▶ unless (exists $self->{global}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"global"}) 37 0 37
unless (exists $self->{"global"}) 3876 0 3876
57 4 $self->{global} = SPVM::Builder::Config::Global->new;
58 }
59
60 4 return $self;
61 }
62  
63 # Instance Methods related to Base functionality
64 34900 ▶sub option_names {
Subroutine option_names: called CC=1 SCAR=0.0
Pod undocumented
65 4 ▶ my ($self) = @_;
Subroutine option_names: called CC=1 SCAR=0.0
Pod undocumented
66
67 4 return $base_fields;
68 }
69  
70 370742 ▶sub create_option {
Subroutine create_option: called CC=2 SCAR=6.9
Pod undocumented
71 4 ▶ my ($self, $name, $value) = @_;
Subroutine create_option: called CC=2 SCAR=6.9
Pod undocumented
72
73 # Extract the option name without leading hyphens or slashes to check its length
74 4 my $pure_name = $name;
75 4 $pure_name =~ s/^[\-\/]+//;
76
77 4 ▶ if (length $pure_name <= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $pure_name <= 1) { } 0 85 85
if (length $pure_name <= 1) { } 57640 313102 370742
78 296 return $self->create_option_short($name, $value);
79 }
80 else {
81 296 return $self->create_option_long($name, $value);
82 }
83 }
84  
85 13375 ▶sub clone {
Subroutine clone: called CC=4 SCAR=13.9
Pod documented
86 296 ▶ my ($self) = @_;
Subroutine clone: called CC=4 SCAR=13.9
Pod documented
87
88 296 my $clone = bless {}, ref $self;
89
90 37 for my $name (keys %$self) {
91 37 my $value = $self->{$name};
92
93 37 ▶ if (ref $value eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 4990 3569 8559
elsif (ref $value eq 'HASH') { } 139 3430 3569
if (ref $value eq 'ARRAY') { } 476051 342588 818639
elsif (ref $value eq 'HASH') { } 13375 329213 342588
94 37 $clone->{$name} = [@$value];
95 }
96 elsif (ref $value eq 'HASH') {
97 37 $clone->{$name} = &_copy_hash($value);
98 }
99 else {
100 37 $clone->{$name} = $value;
101 }
102 }
103
104 37 return $clone;
105 }
106  
107 13375 ▶sub _copy_hash {
Subroutine _copy_hash: called CC=4 SCAR=14.3
108 37 ▶ my ($hash) = @_;
Subroutine _copy_hash: called CC=4 SCAR=14.3
109
110 37 my $clone = {};
111
112 37 for my $name (keys %$hash) {
113 37 my $value = $hash->{$name};
114
115 333 ▶ if (ref $value eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 4 4
elsif (ref $value eq 'HASH') { } 4 0 4
if (ref $value eq 'ARRAY') { } 0 164 164
elsif (ref $value eq 'HASH') { } 164 0 164
116 0 $clone->{$name} = [@$value];
117 }
118 elsif (ref $value eq 'HASH') {
119 85 $clone->{$name} = {%$value};
120 }
121 else {
122 0 $clone->{$name} = $value;
123 }
124 }
125
126 85 return $clone;
127 }
128  
129 0 ▶sub get_base_dir {
Subroutine get_base_dir: not called CC=4 SCAR=21.9
Pod undocumented
130 0 ▶ my ($self) = @_;
Subroutine get_base_dir: called CC=4 SCAR=21.9
Pod undocumented
131  
132 0 my $file = $self->file;
133 0 my $class_name = $self->class_name;
134  
135 0 ▶ unless (defined $file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $file) 0 0 0
unless (defined $file) 0 0 0
136 0 confess("The file must be defined.");
137 }
138  
139 0 ▶ unless (defined $class_name) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $class_name) 0 0 0
unless (defined $class_name) 0 0 0
140 0 confess("the class_name must be defined.");
141 }
142  
143 0 my $abs_file = File::Spec->rel2abs($file);
144 0 my $base_dir = dirname($abs_file);
145  
146 0 my @parts = split(/::/, $class_name);
147 0 my $depth = scalar @parts;
148  
149 0 ▶ for (my $i = 0; $i < $depth; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $depth) 0 0 0
if ($i < $depth) 0 0 0
150 0 $base_dir = dirname($base_dir);
151 }
152  
153 0 return $base_dir;
154 }
155  
156 913 1;
157  
158 =head1 Name
159  
160 SPVM::Builder::Config - Config for Compiling and Linking Native Classes
161  
162 =head1 Description
163  
164 SPVM::Builder::Config class gets and sets config for compiling and linking L<native classes|SPVM::Document::NativeClass>.
165  
166 =head1 Fields
167  
168 =head2 class_name
169  
170 my $class_name = $config->class_name;
171 $config->class_name($class_name);
172  
173 Gets and sets C<class_name> field, the name of the class configured by this config.
174  
175 This field is automatically set and users nomally do not change it.
176  
177 =head2 file
178  
179 my $file = $config->file;
180 $config->file($file);
181  
182 Gets and sets C<file> field, the file path of this config.
183  
184 =head2 category
185  
186 my $category = $config->category;
187 $config->category($category);
188  
189 Gets and sets C<category> field.
190  
191 If this field is C<precompile>, this config is for precompilation,
192  
193 If this field is C<native>, this config is for a native class.
194  
195 This field is automatically set and users nomally do not change it.
196  
197 =head2 is_resource
198  
199 my $is_resource = $config->is_resource;
200 $config->is_resource($is_resource);
201  
202 Gets and sets C<is_resource> field.
203  
204 If this field is true, this config is for a L<resource|SPVM::Document::Resource> class.
205  
206 =head2 quiet
207  
208 my $quiet = $config->quiet;
209 $config->quiet($quiet);
210  
211 Gets and sets C<quiet> field.
212  
213 If this field is a true value, the messages from the compiler and the linker are output to C<stderr>.
214  
215 If this field is a false value except for undef, the messages from the compiler and the linker are not output.
216  
217 If this field is undef, this config does specify whether the messages from the compiler and the linker are output.
218  
219 =head2 mode
220  
221 my $mode = $config->mode;
222 $config->mode($mode);
223  
224 Gets and sets C<mode> field.
225  
226 =head2 global
227  
228 my $config_global = $config->global;
229 $config->global($config_global);
230  
231 Gets and sets the C<global> field, which provides global compilation rules.
232  
233 This field is an L<SPVM::Builder::Config::Global> object (like L<SPVM::Builder::Config::Exe>) that stores shared settings used across multiple compilation units.
234  
235 For example, when the L<spvmcc> command generates an executable file, it sets this field to an L<SPVM::Builder::Config::Exe> object. This allows individual resource configurations to inherit or refer to global flags, such as optimization levels or common library paths, defined in the executable's main configuration.
236  
237 =head2 loaded_config_files
238  
239 Returns the config files loaded by L<SPVM::Builder::Config::Util/"load_config"> function.
240  
241 =head2 resource_loader_config
242  
243 Gets and sets the C<resource_loader_config> field, which is the config that loaded this resource.
244  
245 =head1 Class Methods
246  
247 =head2 new
248  
249 my $config = SPVM::Builder::Config::Base->new(%fields);
250  
251 Creates a new C<SPVM::Builder::Config::Base> object with L<fields|/"Fields">, and returns it.
252  
253 Field Default Values:
254  
255 =over 2
256  
257 =item * L</"file">
258  
259 This value is set automatically.
260  
261 =item * L</"category">
262  
263 "native"
264  
265 =back
266  
267 =cut
268  
269 =head1 Instance Methods
270  
271 =head2 clone
272  
273 my $clone = $self->clone;
274  
275 Clones L<SPVM::Builder::Config> object, and returns it.
276  
277 =head2 Config Mode
278  
279 A config can have its mode if the config is one for an executable file generated by L<spvmcc> command.
280  
281 The mode is written in the format C<.MODE_NAME> just before C<.build> extension of a config file.
282  
283 C<MODE_NAME> must consist of C<a-zA-Z0-9_>.
284  
285 Examples:
286
287 # production mode
288 MyClass.production.build
289
290 # debug mode
291 MyClass.debug.build
292  
293 Use L</"mode"> field to get the config mode.
294  
295 my $mode = $config->mode;
296  
297 The L<spvmcc> command has C<--mode> option for giving a config mode.
298  
299 spvmcc -o myapp --mode production myapp.spvm
300  
301 =head1 Copyright & License
302  
303 Copyright (c) 2023 Yuki Kimoto
304  
305 MIT License
« blib/lib/SPVM/Builder/Native/BasicType.pm ↑ Summary blib/lib/SPVM/ExchangeAPI/CallerInfo.pm »