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

filter: Statementstmt 81.5% 164 / 201 Branchbran 80.0% 64 / 80 Conditioncond 65.3% 32 / 49 MC/DCmcdc 46.8% 15 / 32 Subroutinesub 84.6% 22 / 26 Podpod 81.2% 13 / 16 totaltotal 76.7% 310 / 404 CC 103 SCAR 25.4
CC
103
Cov
79%
_match_apply
20.1
_match_apply
20.1
__ANON__
16.0
CRAP 12.7
« blib/lib/SPVM/Builder/Util.pm ↑ Summary blib/lib/SPVM/Builder/LinkInfo.pm »
1 package SPVM::Builder::Config::Global;
2  
3 461 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 461 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 461 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 461 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 461 ▶use SPVM::Builder::Util::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 461 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Fields
12 1753 my $fields;
13 461 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 461 $fields = [qw(
15 before_compile_cbs
16 before_link_cbs
17 after_link_cbs
18 build_type
19 build_rules
20 )];
21
22 461 has($fields);
23 }
24  
25 {
26 461 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5154 ▶ sub build_type {
Subroutine build_type: called CC=4 SCAR=17.4
Pod documented
28 2946 ▶ my $self = shift;
Subroutine build_type: called CC=4 SCAR=17.4
Pod documented
29 2946 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 2946 0 2946
if (@_) { } 1292 0 1292
30 2946 my $build_type = $_[0];
31
32 0 my %valid_build_types = map { $_ => 1 } qw(Debug Release RelWithDebInfo MinSizeRel);
33
34 2946 ▶ unless (defined $build_type && $valid_build_types{$build_type}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $build_type and $valid_build_types{$build_type}) 0 2946 2946
unless (defined $build_type and $valid_build_types{$build_type}) 0 1292 1292
Condition33%2/6
defined $build_type and $valid_build_types{$build_type}
!ll&&!rl&&r
002946
defined $build_type and $valid_build_types{$build_type}
!ll&&!rl&&r
001292
MC/DC0%0/2
defined $build_type and $valid_build_types{$build_type}
defined $build_type $valid_build_types{$build_type}
MC/DC0%0/2
defined $build_type and $valid_build_types{$build_type}
defined $build_type $valid_build_types{$build_type}
Truth table33%1/3
A and B
A= defined $build_typeB= $valid_build_types{$build_type}
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $build_typeB= $valid_build_types{$build_type}
ABresult
0X0
100
111
35 0 my $valid_build_types_string = join(', ', sort keys %valid_build_types);
36 0 confess("The build_type '$build_type' is invalid. It must be one of ($valid_build_types_string).");
37 }
38
39 0 $self->{build_type} = $build_type;
40 0 return $self;
41 }
42 else {
43 0 return $self->{build_type};
44 }
45 }
46 }
47  
48 1753 ▶sub option_names {
Subroutine option_names: called CC=1 SCAR=0.8
Pod undocumented
49 0 ▶ my ($self) = @_;
Subroutine option_names: not called CC=1 SCAR=1.2
Pod undocumented
50 0 return [@{$self->SUPER::option_names}, @$fields];
51 }
52  
53 # Class Methods
54 3338 ▶sub new {
Subroutine new: called CC=9 SCAR=23.5
Pod documented
55 0 ▶ my $class = shift;
Subroutine new: called CC=9 SCAR=23.5
Pod documented
56
57 2786 ▶ my $self = bless {
Condition33%2/6
ref $class || $class
l!l&&r!l&&!r
027860
ref $class || $class
l!l&&r!l&&!r
09130
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
58 before_compile_cbs => [],
59 before_link_cbs => [],
60 after_link_cbs => [],
61 build_rules => [],
62 @_
63 }, ref $class || $class;
64
65 2786 ▶ if (exists $self->{build_type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'build_type'}) { } 0 2786 2786
if (exists $self->{'build_type'}) { } 0 913 913
66 0 $self->build_type($self->{build_type});
67 }
68 else {
69 2786 $self->build_type('Release');
70 }
71
72 # Define build types
73 2786 my @build_types = qw(Debug Release RelWithDebInfo MinSizeRel);
74 2786 for my $build_type (@build_types) {
75
76 11144 my $config;
77 11144 ▶ if (!defined $build_type || $build_type eq 'Release') {
Branch: 8 branches
BranchTrueFalseTotal
if (not defined $build_type or $build_type eq 'Release') { } 2786 8358 11144
elsif ($build_type eq 'Debug') { } 2786 5572 8358
elsif ($build_type eq 'RelWithDebInfo') { } 2786 2786 5572
elsif ($build_type eq 'MinSizeRel') { } 2786 0 2786
if (not defined $build_type or $build_type eq 'Release') { } 913 2739 3652
elsif ($build_type eq 'Debug') { } 913 1826 2739
elsif ($build_type eq 'RelWithDebInfo') { } 913 913 1826
elsif ($build_type eq 'MinSizeRel') { } 913 0 913
Condition66%4/6
not defined $build_type or $build_type eq "Release"
l!l&&r!l&&!r
027868358
not defined $build_type or $build_type eq "Release"
l!l&&r!l&&!r
09132739
MC/DC50%1/2
not defined $build_type or $build_type eq "Release"
not defined $build_type $build_type eq "Release"
MC/DC50%1/2
not defined $build_type or $build_type eq "Release"
not defined $build_type $build_type eq "Release"
Truth table66%2/3
not(A) or B
A= not defined $build_typeB= $build_type eq "Release"
ABresult
1X1
011
000
Truth table66%2/3
not(A) or B
A= not defined $build_typeB= $build_type eq "Release"
ABresult
1X1
011
000
78 # Release: Full optimization for both CC and LD
79 2786 $config = {
80 optimize => '-O3',
81 debug_info_ccflags => [],
82 ndebug_ccflags => ['-DNDEBUG'],
83
84 ld_optimize => '-O3',
85 debug_info_ldflags => [],
86 };
87 }
88 elsif ($build_type eq 'Debug') {
89 # Debug: No optimization
90 0 $config = {
91 optimize => '',
92 debug_info_ccflags => ['-g'],
93 ndebug_ccflags => [],
94
95 ld_optimize => '',
96 debug_info_ldflags => ['-g'],
97 };
98 }
99 elsif ($build_type eq 'RelWithDebInfo') {
100 # RelWithDebInfo: Balanced optimization with debug info
101 2786 $config = {
102 optimize => '-O2',
103 debug_info_ccflags => ['-g'],
104 ndebug_ccflags => ['-DNDEBUG'],
105
106 ld_optimize => '-O2',
107 debug_info_ldflags => ['-g'],
108 };
109 }
110 elsif ($build_type eq 'MinSizeRel') {
111 # MinSizeRel: Optimize for size
112 2786 $config = {
113 optimize => '-Os',
114 debug_info_ccflags => [],
115 ndebug_ccflags => ['-DNDEBUG'],
116
117 ld_optimize => '-Os',
118 debug_info_ldflags => [],
119 };
120 }
121 else {
122 0 die "Unknown build_type: $build_type";
123 }
124
125 0 $self->build_rule(
126 { global => {build_type => $build_type} },
127 $config
128 );
129 }
130
131 0 return $self;
132 }
133  
134 # Instance Methods
135 441 ▶sub add_before_compile_cb {
Subroutine add_before_compile_cb: called CC=1 SCAR=6.9
Pod documented
136 0 ▶ my ($self, @before_compile_cbs) = @_;
Subroutine add_before_compile_cb: not called CC=1 SCAR=6.9
Pod documented
137
138 0 push @{$self->{before_compile_cbs}}, @before_compile_cbs;
139 }
140  
141 441 ▶sub add_before_link_cb {
Subroutine add_before_link_cb: called CC=1 SCAR=4.6
Pod documented
142 0 ▶ my ($self, @before_link_cbs) = @_;
Subroutine add_before_link_cb: not called CC=1 SCAR=4.6
Pod documented
143 0 push @{$self->{before_link_cbs}}, @before_link_cbs;
144 }
145  
146 441 ▶sub add_after_link_cb {
Subroutine add_after_link_cb: called CC=1 SCAR=0.0
Pod documented
147 2208 ▶ my ($self, @after_link_cbs) = @_;
Subroutine add_after_link_cb: not called CC=1 SCAR=0.0
Pod documented
148 2208 push @{$self->{after_link_cbs}}, @after_link_cbs;
149 }
150  
151 11585 ▶sub build_rule {
Subroutine build_rule: called CC=6 SCAR=18.8
Pod documented
152 13352 ▶ my ($self, $condition, $match_config_or_cb) = @_;
Subroutine build_rule: called CC=6 SCAR=18.8
Pod documented
153
154 # Normalize condition for key validation
155 13352 ▶ if (ref $condition eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $condition eq "HASH") 11144 0 11144
if (ref $condition eq "HASH") 3652 0 3652
156 0 my %normalized;
157 0 for my $name (keys %$condition) {
158 0 ▶ next if $name eq 'global';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "global" 11144 0 11144
if $name eq "global" 3652 0 3652
159
160 # Remove non-word characters (ASCII only)
161 2208 my $target_name = $name;
162 2208 $target_name =~ s/[^\w]//ga;
163 552 $normalized{$target_name} = $condition->{$name};
164 }
165
166 # Validate with normalized keys
167 11144 SPVM::Builder::Config->new_empty(%normalized);
168 }
169  
170 # Normalize match_config if it's a hash (not a callback)
171 11696 ▶ if (ref $match_config_or_cb eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $match_config_or_cb eq "HASH") 11144 0 11144
if (ref $match_config_or_cb eq "HASH") 3652 0 3652
172 13904 my %normalized;
173 13904 for my $name (keys %$match_config_or_cb) {
174 # Remove prefix operators like '+', '-', '.'
175 55720 my $target_name = $name;
176 55720 $target_name =~ s/[^\w]//ga;
177 55720 $normalized{$target_name} = $match_config_or_cb->{$name};
178 }
179
180 # Validate with normalized keys
181 0 SPVM::Builder::Config->new_empty(%normalized);
182 }
183
184 0 push @{$self->{build_rules}}, [$condition, $match_config_or_cb];
185 }
186  
187 0 ▶sub build_rule_any { shift->build_rule(undef, @_) }
Subroutine build_rule_any: called CC=1 SCAR=6.9
Pod documented
188  
189 21055 ▶sub _match_apply {
Subroutine _match_apply: called CC=20 SCAR=30.0
190 20614 my ($config, $condition, $match_config_or_cb) = @_;
191
192 20614 ▶ if ($condition) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition) 22814 8 22822
193 20606 $condition = {%$condition};
194 }
195 20614 my $condition_global = delete $condition->{global};
196
197 20614 my $match = 1;
198 20614 ▶ if ($condition || $condition_global) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition or $condition_global) 22822 0 22822
Condition33%1/3
$condition or $condition_global
l!l&&r!l&&!r
2282200
MC/DC0%0/2
$condition or $condition_global
$condition $condition_global
Truth table33%1/3
A or B
A= $conditionB= $condition_global
ABresult
1X1
011
000
199 41669 ▶ my $check_match = sub {
Subroutine __ANON__: called CC=16 SCAR=27.7
200 42884 my ($target_h, $cond_h) = @_;
201
202 42884 my $inner_match = 1;
203 45644 ▶ if ($cond_h) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond_h) 45619 25 45644
204 41755 for my $name (keys %$cond_h) {
205 21159 my $cond_val = $cond_h->{$name};
206 21159 my $target_name = $name;
207 22815 my $is_not = ($target_name =~ s/^!//);
208
209 21489 ▶ my $val = $target_h ? $target_h->{$target_name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$target_h ? : 22815 0 22815
210 21489 ▶ my @candidates = (defined $val && ref $val eq 'ARRAY') ? @$val : ($val);
Branch: 1 branch
BranchTrueFalseTotal
defined $val && ref $val eq 'ARRAY' ? : 7 22808 22815
Condition100%3/3
defined $val and ref $val eq "ARRAY"
!ll&&!rl&&r
4228047
MC/DC100%2/2
defined $val and ref $val eq "ARRAY"
defined $val ref $val eq "ARRAY"
Truth table100%3/3
A and B
A= defined $valB= ref $val eq "ARRAY"
ABresult
0X0
100
111
211
212 21489 my $found = 0;
213 21048 for my $c (@candidates) {
214 21049 ▶ if (ref $cond_val eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cond_val eq 'Regexp') { } 4 22812 22816
elsif (defined $cond_val) { } 22806 6 22812
215 445 ▶ if (defined $c && $c =~ $cond_val) { $found = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and $c =~ /$cond_val/) 2 2 4
Condition66%2/3
defined $c and $c =~ /$cond_val/
!ll&&!rl&&r
022
MC/DC50%1/2
defined $c and $c =~ /$cond_val/
defined $c $c =~ /$cond_val/
Truth table66%2/3
A and B
A= defined $cB= $c =~ /$cond_val/
ABresult
0X0
100
111
216 }
217 elsif (defined $cond_val) {
218 21039 ▶ if (defined $c && $c eq $cond_val) { $found = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and $c eq $cond_val) 5705 17101 22806
Condition100%3/3
defined $c and $c eq $cond_val
!ll&&!rl&&r
1171005705
MC/DC100%2/2
defined $c and $c eq $cond_val
defined $c $c eq $cond_val
Truth table100%3/3
A and B
A= defined $cB= $c eq $cond_val
ABresult
0X0
100
111
219 }
220 else {
221 447 ▶ if (!defined $c) { $found = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 4 2 6
222 }
223 }
224
225 21048 ▶ $found = !$found if $is_not;
Branch: 1 branch
BranchTrueFalseTotal
if $is_not 7 22808 22815
226
227 21048 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 17101 5714 22815
228 15886 $inner_match = 0;
229 15886 last;
230 }
231 }
232 }
233
234 41669 return $inner_match;
235 23374 };
236
237 25030 my $match_condition = $check_match->($config, $condition);
238 25030 my $match_condition_global = $check_match->($config->{global}, $condition_global);
239
240 25030 ▶ $match = $match_condition && $match_condition_global;
Condition100%3/3
$match_condition && $match_condition_global
!ll&&!rl&&r
6170955721
MC/DC100%2/2
$match_condition && $match_condition_global
$match_condition $match_condition_global
Truth table100%3/3
A && B
A= $match_conditionB= $match_condition_global
ABresult
0X0
100
111
241 }
242
243 25030 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 5721 17101 22822
244 7377 ▶ if (ref $match_config_or_cb eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $match_config_or_cb eq 'CODE') { } 16 5705 5721
245 2224 $match_config_or_cb->($config);
246 }
247 else {
248 7361 for my $match_name (keys %$match_config_or_cb) {
249 27945 my $new_value = $match_config_or_cb->{$match_name};
250
251 27945 ▶ if ($match_name =~ /^\+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_name =~ /^\+(.+)$/) { } 6 28491 28497
252 2214 my $name = $1;
253 2214 my $old_value = $config->{$name};
254
255 2214 ▶ if (!defined $old_value) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $old_value) { } 1 5 6
elsif (ref $old_value eq '' and ref $new_value eq '') { } 1 4 5
elsif (ref $old_value eq '' and ref $new_value eq 'ARRAY') { } 1 3 4
elsif (ref $old_value eq 'ARRAY' and ref $new_value eq '') { } 1 2 3
elsif (ref $old_value eq 'ARRAY' and ref $new_value eq 'ARRAY') { } 1 1 2
256 1 $config->{$name} = $new_value;
257 }
258 ▶ elsif (ref $old_value eq '' && ref $new_value eq '') {
Condition100%3/3
ref $old_value eq "" and ref $new_value eq ""
!ll&&!rl&&r
311
MC/DC100%2/2
ref $old_value eq "" and ref $new_value eq ""
ref $old_value eq "" ref $new_value eq ""
Truth table100%3/3
A and B
A= ref $old_value eq ""B= ref $new_value eq ""
ABresult
0X0
100
111
259 1 $config->{$name} .= $new_value;
260 }
261 ▶ elsif (ref $old_value eq '' && ref $new_value eq 'ARRAY') {
Condition66%2/3
ref $old_value eq "" and ref $new_value eq "ARRAY"
!ll&&!rl&&r
301
MC/DC50%1/2
ref $old_value eq "" and ref $new_value eq "ARRAY"
ref $old_value eq "" ref $new_value eq "ARRAY"
Truth table66%2/3
A and B
A= ref $old_value eq ""B= ref $new_value eq "ARRAY"
ABresult
0X0
100
111
262 1 $config->{$name} = [$old_value, @$new_value];
263 }
264 ▶ elsif (ref $old_value eq 'ARRAY' && ref $new_value eq '') {
Condition100%3/3
ref $old_value eq "ARRAY" and ref $new_value eq ""
!ll&&!rl&&r
111
MC/DC100%2/2
ref $old_value eq "ARRAY" and ref $new_value eq ""
ref $old_value eq "ARRAY" ref $new_value eq ""
Truth table100%3/3
A and B
A= ref $old_value eq "ARRAY"B= ref $new_value eq ""
ABresult
0X0
100
111
265 2209 push @{$config->{$name}}, $new_value;
266 }
267 ▶ elsif (ref $old_value eq 'ARRAY' && ref $new_value eq 'ARRAY') {
Condition66%2/3
ref $old_value eq "ARRAY" and ref $new_value eq "ARRAY"
!ll&&!rl&&r
101
MC/DC50%1/2
ref $old_value eq "ARRAY" and ref $new_value eq "ARRAY"
ref $old_value eq "ARRAY" ref $new_value eq "ARRAY"
Truth table66%2/3
A and B
A= ref $old_value eq "ARRAY"B= ref $new_value eq "ARRAY"
ABresult
0X0
100
111
268 553 push @{$config->{$name}}, @$new_value;
269 }
270 else {
271 1 ▶ my $old_value_type = ref $old_value || 'scalar(string)';
Condition50%1/2
ref $old_value || 'scalar(string)'
l!l
10
MC/DC0%0/1
ref $old_value || 'scalar(string)'
ref $old_value
Truth table50%1/2
A
A= ref $old_value
Aresult
11
00
272 1 ▶ my $new_value_type = ref $new_value || 'scalar(string)';
Condition50%1/2
ref $new_value || 'scalar(string)'
l!l
01
MC/DC0%0/1
ref $new_value || 'scalar(string)'
ref $new_value
Truth table50%1/2
A
A= ref $new_value
Aresult
11
00
273 2209 confess "The addition of the \"$match_name\" field is not supported for the combination of $old_value_type and $new_value_type.";
274 }
275 }
276 else {
277 27939 $config->{$match_name} = $new_value;
278 }
279 }
280 }
281 }
282 }
283  
284 5587 ▶sub apply_build_rules {
Subroutine apply_build_rules: called CC=2 SCAR=6.9
Pod undocumented
285 5587 my ($self, $config) = @_;
286
287 5587 for my $build_rule (@{$self->build_rules}) {
288 21025 &_match_apply($config, @$build_rule);
289 }
290 }
291  
292 1753 1;
293  
294 =head1 Name
295  
296 SPVM::Builder::Config::Global - Excutable File Config
297  
298 =head1 Description
299  
300 The SPVM::Builder::Config::Global class has methods to manipulate the config for the excutable file generated by L<spvmcc> command.
301  
302 =head1 Usage
303  
304 use SPVM::Builder::Config::Global;
305
306 my $config_global = SPVM::Builder::Config::Global->new;
307  
308 =head1 Usage
309  
310 use SPVM::Builder::Config::Global;
311
312 my $config_global = SPVM::Builder::Config::Global->new;
313
314 # Basic conditional update (Exact match)
315 # If language is 'c', set optimization to -O3
316 $config_global->build_rule({language => 'c'}, {optimize => '-O3'});
317  
318 # Regex matching and field appending (+)
319 # If dialect matches c11/c17, add a specific warning flag
320 $config_global->build_rule({dialect => qr/^c1[17]$/}, {'+ccflags' => ['-Wpedantic']});
321  
322 # Array Sensitivity (Checking if a flag exists in an array)
323 # If '-DDEBUG' is already in ccflags, add debug linker flags
324 $config_global->build_rule({ccflags => '-DDEBUG'}, {'+ldflags' => ['-DEBUG']});
325  
326 # Negative Match (!prefix)
327 # If '-Zi' is NOT in ccflags, enable linker optimizations
328 $config_global->build_rule({'!ccflags' => '-Zi'}, {'+ld_optimize' => '-OPT:REF,ICF'});
329  
330 # Complex conditions (AND logic)
331 # If it's C++ AND dialect is NOT c++11 (e.g. c++14), add exception handling
332 $config_global->build_rule({language => 'cpp', '!dialect' => 'c++11'}, {
333 '+ccflags' => ['-EHsc']
334 });
335  
336 # Procedural update with a callback
337 # Dynamically modify the config object based on custom logic
338 $config_global->build_rule({language => 'c'}, sub {
339 my $config = shift;
340 if ($ENV{MY_CUSTOM_OPT}) {
341 $config->optimize('-Ofast');
342 }
343 });
344  
345 # Apply to all configurations (build_rule_any)
346 # Ensure all configs have the -nologo flag regardless of any condition
347 $config_global->build_rule_any({'+ccflags' => ['-nologo']});
348  
349 # Reset or global initialization via callback
350 # Useful for clearing fields before applying specific rules
351 $config_global->build_rule_any(sub {
352 my $config = shift;
353 $config->clear_system_fields;
354 });
355  
356 =head1 Details
357  
358 =head2 Warning: Should Not Change Compiler Flags
359  
360 The fields for compiler flags in L<SPVM::Builder::Config> such as L<SPVM::Builder::Config/"cc">, L<SPVM::Builder::Config/"std"> should not be changed.
361  
362 This is because the compiler flags are used to compile SPVM core source files and a bootstrap source file generagted by C<spvmcc> command.
363  
364 =head1 Fields
365  
366 =head2 before_compile_cbs
367  
368 my $before_compile_cbs = $self->before_compile_cbs;
369 $self->before_compile_cbs($before_compile_cbs);
370  
371 Gets and sets the C<before_compile_cbs> field, an array reference of callbacks that work globally.
372  
373 These callbacks are executed just after an L<SPVM::Builder::CompileInfo> object is completely constructed and just before the compile command L</"cc"> is planned to be executed.
374  
375 This affects all compilations.
376  
377 Note that even if the actual compile command is skipped (for example, when the object file is already up-to-date), these callbacks are still executed as long as the compilation is planned in the build pipeline.
378  
379 The 1st argument of each callback is an L<SPVM::Builder::CompileInfo> object.
380  
381 You can modify each field of the L<SPVM::Builder::CompileInfo> object and its internal L<SPVM::Builder::Config> object (obtained via the C<config> method) within the callbacks to dynamically customize the compilation process.
382  
383 =head2 before_link_cbs
384  
385 my $before_link_cbs = $config->before_link_cbs;
386 $config->before_link_cbs($before_link_cbs);
387  
388 Gets and sets the C<before_link_cbs> field, an array reference of callbacks that work globally.
389  
390 These callbacks are executed just after an L<SPVM::Builder::LinkInfo> object is completely constructed and just before the link command L</"ld"> is planned to be executed.
391  
392 This affects all linkings.
393  
394 Note that even if the actual link command is skipped (for example, when the dynamic link file is already up-to-date), these callbacks are still executed as long as the linking is planned in the build pipeline.
395  
396 The 1st argument of each callback is an L<SPVM::Builder::LinkInfo> object.
397  
398 You can modify each field of the L<SPVM::Builder::LinkInfo> object and its internal L<SPVM::Builder::Config> object (obtained via the C<config> method) within the callbacks to dynamically customize the linking process.
399  
400 =head2 after_link_cbs
401  
402 my $after_link_cbs = $config->after_link_cbs;
403 $config->after_link_cbs($after_link_cbs);
404  
405 Gets and sets the C<after_link_cbs> field, an array reference of callbacks that work globally.
406  
407 These callbacks are executed just after the link command L</"ld"> is executed or planned to be executed.
408  
409 This affects all linkings.
410  
411 Note that even if the actual link command is skipped (for example, when the dynamic link file is already up-to-date), these callbacks are still executed as long as the linking is planned in the build pipeline.
412  
413 The 1st argument of each callback is an L<SPVM::Builder::LinkInfo> object.
414  
415 You can check each field of the L<SPVM::Builder::LinkInfo> object and its internal L<SPVM::Builder::Config> object (obtained via the C<config> method) within the callbacks to perform post-linking processes.
416  
417 =head2 build_type
418  
419 my $build_type = $config->build_type;
420 $config->build_type($build_type);
421  
422 Sets and gets the C<build_type> field.
423  
424 The C<build_type> field must be one of the following CMake-compatible values:
425  
426 =over 2
427  
428 =item * C<Debug>
429  
430 =item * C<Release>
431  
432 =item * C<RelWithDebInfo>
433  
434 =item * C<MinSizeRel>
435  
436 =back
437  
438 If an invalid value is specified, an exception is thrown.
439  
440 Examples:
441  
442 # Set build_type
443 $config->build_type('Debug');
444
445 # Get build_type
446 my $build_type = $config->build_type;
447  
448 =head1 Methods
449  
450 =head2 new
451  
452 my $config_global = SPVM::Builder::Config::Global->new(%fields);
453  
454 Create a new L<SPVM::Builder::Config::Global> object with L</"Fields"> and fields of its super classes.
455  
456 This method calls the C<new> method of its L<super class|/"Inheritance"> given %fields with field default values applied.
457  
458 Field Default Values:
459  
460 =over 2
461  
462 =item * L</"build_type">
463  
464 "Release"
465  
466 =item * L<output_type|SPVM::Builder::Config/"output_type">
467  
468 "exe"
469  
470 =item * L</"before_compile_cbs">
471  
472 []
473  
474 =item * L</"before_link_cbs">
475  
476 []
477  
478 =item * L</"after_link_cbs">
479  
480 []
481  
482 =item * Other Fields
483  
484 undef
485  
486 =back
487  
488 =head2 add_before_compile_cb
489  
490 $self->add_before_compile_cb(@before_compile_cbs);
491  
492 Adds @before_compile_cbs to the end of L</"before_compile_cbs"> field.
493  
494 Examples:
495  
496 $self->add_before_compile_cb(sub {
497 my ($compile_info) = @_;
498
499 my $config = $compile_info->config;
500
501 my $cc_command = $compile_info->to_command;
502
503 # Do something
504 });
505  
506 =head2 add_before_link_cb
507  
508 $config->add_before_link_cb(@before_link_cbs);
509  
510 Adds @before_link_cbs to the end of L</"before_link_cbs"> field.
511  
512 Examples:
513  
514 $config->add_before_link_cb(sub {
515 my ($link_info) = @_;
516
517 my $config = $link_info->config;
518
519 my $object_file_infos = $link_info->object_file_infos;
520
521 # Do something
522
523 });
524  
525 =head2 add_after_link_cb
526  
527 $config->add_after_link_cb(@after_link_cbs);
528  
529 Adds @after_link_cbs to the end of L</"after_link_cbs"> field.
530  
531 Examples:
532  
533 $config->add_after_link_cb(sub {
534 my ($link_info) = @_;
535
536 my $config = $link_info->config;
537
538 my $object_file_infos = $link_info->object_file_infos;
539
540 # Do something
541
542 });
543  
544 =head2 build_rule
545  
546 $global_config->build_rule($condition, $match_config_or_cb);
547  
548 Adds a rule to dynamically update the configuration before compilation if the given conditions are met.
549  
550 Parameters:
551  
552 =over 2
553  
554 =item * C<$condition>
555  
556 A hash reference specifying the match criteria. Each key represents a field name in the target configuration.
557  
558 A match occurs only if B<all> specified conditions in the hash are satisfied (AND logic). For each individual field, the matching behavior is as follows:
559  
560 =over 4
561  
562 =item * B<Negative Match (!prefix)>: If a field name starts with B<C<!>> (e.g., C<'!ccflags' => '-O2'>), the condition is B<inverted>. It matches only if the criteria is B<not> met.
563  
564 =item * B<Array Sensitivity>: If the field value in the target configuration is an B<array reference>, the condition matches if B<at least one element> within that array satisfies the criteria below (OR logic within the array). When combined with the B<C<!>> prefix, it matches only if B<none> of the elements satisfy the criteria.
565  
566 =item * B<Regex Match>: If the condition value is a C<Regexp> object (e.g., C<qr/.../>), it performs a regex match against the field value (or its elements).
567  
568 =item * B<Undef Match>: If the condition value is C<undef>, it matches if the target field (or an element within its array) is also C<undef>.
569  
570 =item * B<String Match>: Otherwise, it performs a string equality check (C<eq>) against the field value (or its elements).
571  
572 =back
573  
574 =item * C<$match_config_or_cb>
575  
576 A hash reference, an L<SPVM::Builder::Config> object, or a code reference (callback).
577  
578 If it is a hash reference or a config object, the fields in the target configuration are updated.
579 You can use the B<C<+>> prefix in the field name to B<append> values instead of overwriting them:
580  
581 =over 4
582  
583 =item * B<C<+field => $string>> : If the existing value is a string, it concatenates the new string. If the existing value is an array reference, it pushes the new string into the array.
584  
585 =item * B<C<+field => $array_ref>> : Pushes the elements of the array reference into the existing array. If the existing value is a scalar, it is promoted to an array before the push.
586  
587 =back
588  
589 If it is a code reference, the callback is executed with the target L<SPVM::Builder::Config> object as its first argument. This allows for complex, procedural updates to the configuration.
590  
591 =back
592  
593 =head2 build_rule_any
594  
595 $global_config->build_rule_any($match_config_or_cb);
596  
597 A syntax sugar for L</"match"> with no conditions.
598 The C<$match_config> will be applied to all configurations before compilation.
599  
600 =cut
601  
602 =head1 Copyright & License
603  
604 Copyright (c) 2023 Yuki Kimoto
605  
606 MIT License
« blib/lib/SPVM/Builder/Util.pm ↑ Summary blib/lib/SPVM/Builder/LinkInfo.pm »