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

filter: Statementstmt 88.0% 118 / 134 Branchbran 50.0% 22 / 44 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 95.2% 20 / 21 Podpod 33.3% 4 / 12 totaltotal 72.7% 168 / 231 CC 31 SCAR 13.9
CC
31
Cov
76%
get_base_dir
7.0
get_base_dir
7.0
new
5.4
CRAP 4.0
« blib/lib/SPVM/Builder/Native/BasicType.pm ↑ Summary blib/lib/SPVM/ExchangeAPI/CallerInfo.pm »
1 package SPVM::Builder::Config::Base;
2  
3 447 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 447 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 447 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 447 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 447 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 447 ▶use File::Basename 'dirname', 'fileparse';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 447 ▶use SPVM::Builder::Config::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Base Fields
12 8135 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 8135 has($base_fields);
26  
27 22359 ▶sub new_empty {
Subroutine new_empty: called CC=2 SCAR=7.4
Pod undocumented
28 21904 ▶ my $class = shift;
Subroutine new_empty: called CC=2 SCAR=7.4
Pod undocumented
29
30 21904 my $self = {@_};
31
32 21904 ▶ bless $self, ref $class || $class;
Condition33%2/6
ref $class || $class
l!l&&r!l&&!r
0219040
ref $class || $class
l!l&&r!l&&!r
076880
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 21904 SPVM::Builder::Util::check_option_names($self, $self->option_names);
35
36 }
37  
38 3191 ▶sub new {
Subroutine new: called CC=5 SCAR=16.8
Pod documented
39 2736 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.8
Pod documented
40
41 2736 my $self = {@_};
42
43 2736 ▶ bless $self, ref $class || $class;
Condition33%2/6
ref $class || $class
l!l&&r!l&&!r
027360
ref $class || $class
l!l&&r!l&&!r
09610
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 2736 SPVM::Builder::Util::check_option_names($self, $self->option_names);
46
47 # category
48 2736 ▶ unless (exists $self->{category}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"category"}) 2736 0 2736
unless (exists $self->{"category"}) 961 0 961
49 2736 $self->category('native');
50 }
51  
52 2736 ▶ unless (exists $self->{loaded_config_files}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"loaded_config_files"}) 2736 0 2736
unless (exists $self->{"loaded_config_files"}) 961 0 961
53 2736 $self->{loaded_config_files} = [];
54 }
55
56 2736 ▶ unless (exists $self->{global}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"global"}) 2736 0 2736
unless (exists $self->{"global"}) 961 0 961
57 2736 $self->{global} = SPVM::Builder::Config::Global->new;
58 }
59
60 2736 return $self;
61 }
62  
63 # Instance Methods related to Base functionality
64 25095 ▶sub option_names {
Subroutine option_names: called CC=1 SCAR=0.0
Pod undocumented
65 24640 ▶ my ($self) = @_;
Subroutine option_names: called CC=1 SCAR=0.0
Pod undocumented
66
67 24640 return $base_fields;
68 }
69  
70 271176 ▶sub create_option {
Subroutine create_option: called CC=2 SCAR=6.9
Pod undocumented
71 270721 ▶ 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 270721 my $pure_name = $name;
75 270721 $pure_name =~ s/^[\-\/]+//;
76
77 270721 ▶ if (length $pure_name <= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $pure_name <= 1) { } 24180 246541 270721
if (length $pure_name <= 1) { } 33636 70333 103969
78 24180 return $self->create_option_short($name, $value);
79 }
80 else {
81 246541 return $self->create_option_long($name, $value);
82 }
83 }
84  
85 11643 ▶sub clone {
Subroutine clone: called CC=4 SCAR=13.9
Pod documented
86 11188 ▶ my ($self) = @_;
Subroutine clone: called CC=4 SCAR=13.9
Pod documented
87
88 11188 my $clone = bless {}, ref $self;
89
90 11188 for my $name (keys %$self) {
91 683045 my $value = $self->{$name};
92
93 683045 ▶ if (ref $value eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 397388 285657 683045
elsif (ref $value eq 'HASH') { } 11188 274469 285657
if (ref $value eq 'ARRAY') { } 59136 43020 102156
elsif (ref $value eq 'HASH') { } 1641 41379 43020
94 397388 $clone->{$name} = [@$value];
95 }
96 elsif (ref $value eq 'HASH') {
97 11188 $clone->{$name} = &_copy_hash($value);
98 }
99 else {
100 274469 $clone->{$name} = $value;
101 }
102 }
103
104 11188 return $clone;
105 }
106  
107 11643 ▶sub _copy_hash {
Subroutine _copy_hash: called CC=4 SCAR=15.4
108 11188 ▶ my ($hash) = @_;
Subroutine _copy_hash: called CC=4 SCAR=15.4
109
110 11188 my $clone = {};
111
112 0 for my $name (keys %$hash) {
113 583 my $value = $hash->{$name};
114
115 0 ▶ if (ref $value eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 128 128
elsif (ref $value eq 'HASH') { } 128 0 128
if (ref $value eq 'ARRAY') { } 0 48 48
elsif (ref $value eq 'HASH') { } 48 0 48
116 455 $clone->{$name} = [@$value];
117 }
118 elsif (ref $value eq 'HASH') {
119 0 $clone->{$name} = {%$value};
120 }
121 else {
122 0 $clone->{$name} = $value;
123 }
124 }
125
126 0 return $clone;
127 }
128  
129 455 ▶sub get_base_dir {
Subroutine get_base_dir: called CC=4 SCAR=19.5
Pod undocumented
130 0 ▶ my ($self) = @_;
Subroutine get_base_dir: not called CC=4 SCAR=19.5
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 455 ▶ for (my $i = 0; $i < $depth; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $depth) 0 0 0
if ($i < $depth) 0 0 0
150 455 $base_dir = dirname($base_dir);
151 }
152  
153 455 return $base_dir;
154 }
155  
156 8135 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 »