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

filter: Statementstmt 87.8% 174 / 198 Branchbran 80.0% 64 / 80 Conditioncond 65.3% 32 / 49 MC/DCmcdc 46.8% 15 / 32 Subroutinesub 96.1% 25 / 26 Podpod 81.2% 13 / 16 totaltotal 80.5% 323 / 401 CC 103 SCAR 25.3
CC
103
Cov
83%
_match_apply
20.1
_match_apply
20.1
__ANON__
16.0
CRAP 12.5
« blib/lib/SPVM/ExchangeAPI.pm ↑ Summary blib/lib/SPVM/Builder/LinkInfo.pm »
1 package SPVM::Builder::Config::Global;
2  
3 2170 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1483 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 687 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 886 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 687 ▶use SPVM::Builder::Util::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1483 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Fields
12 my $fields;
13 2279 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2279 $fields = [qw(
15 before_compile_cbs
16 before_link_cbs
17 after_link_cbs
18 build_type
19 build_rules
20 )];
21
22 886 has($fields);
23 }
24  
25 {
26 886 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5632 ▶ sub build_type {
Subroutine build_type: called CC=4 SCAR=14.9
Pod documented
28 104 ▶ my $self = shift;
Subroutine build_type: called CC=4 SCAR=14.9
Pod documented
29 104 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 386 0 386
if (@_) { } 5632 0 5632
30 104 my $build_type = $_[0];
31
32 104 my %valid_build_types = map { $_ => 1 } qw(Debug Release RelWithDebInfo MinSizeRel);
33
34 104 ▶ unless (defined $build_type && $valid_build_types{$build_type}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $build_type and $valid_build_types{$build_type}) 0 386 386
unless (defined $build_type and $valid_build_types{$build_type}) 0 5632 5632
Condition33%2/6
defined $build_type and $valid_build_types{$build_type}
!ll&&!rl&&r
00386
defined $build_type and $valid_build_types{$build_type}
!ll&&!rl&&r
005632
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 104 $self->{build_type} = $build_type;
40 104 return $self;
41 }
42 else {
43 0 return $self->{build_type};
44 }
45 }
46 }
47  
48 0 ▶sub option_names {
Subroutine option_names: not called CC=1 SCAR=1.2
Pod undocumented
49 0 ▶ my ($self) = @_;
Subroutine option_names: called CC=1 SCAR=1.2
Pod undocumented
50 0 return [@{$self->SUPER::option_names}, @$fields];
51 }
52  
53 # Class Methods
54 5202 ▶sub new {
Subroutine new: called CC=9 SCAR=22.7
Pod documented
55 104 ▶ my $class = shift;
Subroutine new: called CC=9 SCAR=22.7
Pod documented
56
57 104 ▶ my $self = bless {
Condition33%2/6
ref $class || $class
l!l&&r!l&&!r
02390
ref $class || $class
l!l&&r!l&&!r
052020
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 104 ▶ if (exists $self->{build_type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'build_type'}) { } 0 239 239
if (exists $self->{'build_type'}) { } 0 5202 5202
66 0 $self->build_type($self->{build_type});
67 }
68 else {
69 104 $self->build_type('Release');
70 }
71
72 # Define build types
73 104 my @build_types = qw(Debug Release RelWithDebInfo MinSizeRel);
74 104 for my $build_type (@build_types) {
75
76 104 my $config;
77 104 ▶ if (!defined $build_type || $build_type eq 'Release') {
Branch: 8 branches
BranchTrueFalseTotal
if (not defined $build_type or $build_type eq 'Release') { } 239 717 956
elsif ($build_type eq 'Debug') { } 239 478 717
elsif ($build_type eq 'RelWithDebInfo') { } 239 239 478
elsif ($build_type eq 'MinSizeRel') { } 239 0 239
if (not defined $build_type or $build_type eq 'Release') { } 5202 15606 20808
elsif ($build_type eq 'Debug') { } 5202 10404 15606
elsif ($build_type eq 'RelWithDebInfo') { } 5202 5202 10404
elsif ($build_type eq 'MinSizeRel') { } 5202 0 5202
Condition66%4/6
not defined $build_type or $build_type eq "Release"
l!l&&r!l&&!r
0239717
not defined $build_type or $build_type eq "Release"
l!l&&r!l&&!r
0520215606
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 386 $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 386 $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 386 $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 386 $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 386 $self->build_rule(
126 { global => {build_type => $build_type} },
127 $config
128 );
129 }
130
131 0 return $self;
132 }
133  
134 # Instance Methods
135 5 ▶sub add_before_compile_cb {
Subroutine add_before_compile_cb: called CC=1 SCAR=0.0
Pod documented
136 0 ▶ my ($self, @before_compile_cbs) = @_;
Subroutine add_before_compile_cb: called CC=1 SCAR=0.0
Pod documented
137
138 386 push @{$self->{before_compile_cbs}}, @before_compile_cbs;
139 }
140  
141 5 ▶sub add_before_link_cb {
Subroutine add_before_link_cb: called CC=1 SCAR=1.2
Pod documented
142 0 ▶ my ($self, @before_link_cbs) = @_;
Subroutine add_before_link_cb: called CC=1 SCAR=1.2
Pod documented
143 0 push @{$self->{before_link_cbs}}, @before_link_cbs;
144 }
145  
146 5 ▶sub add_after_link_cb {
Subroutine add_after_link_cb: called CC=1 SCAR=0.0
Pod documented
147 0 ▶ my ($self, @after_link_cbs) = @_;
Subroutine add_after_link_cb: called CC=1 SCAR=0.0
Pod documented
148 239 push @{$self->{after_link_cbs}}, @after_link_cbs;
149 }
150  
151 20808 ▶sub build_rule {
Subroutine build_rule: called CC=6 SCAR=19.6
Pod documented
152 239 ▶ my ($self, $condition, $match_config_or_cb) = @_;
Subroutine build_rule: called CC=6 SCAR=19.6
Pod documented
153
154 # Normalize condition for key validation
155 0 ▶ if (ref $condition eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $condition eq "HASH") 956 0 956
if (ref $condition eq "HASH") 20808 0 20808
156 239 my %normalized;
157 239 for my $name (keys %$condition) {
158 239 ▶ next if $name eq 'global';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "global" 956 0 956
if $name eq "global" 20808 0 20808
159
160 # Remove non-word characters (ASCII only)
161 0 my $target_name = $name;
162 0 $target_name =~ s/[^\w]//ga;
163 0 $normalized{$target_name} = $condition->{$name};
164 }
165
166 # Validate with normalized keys
167 239 SPVM::Builder::Config->new_empty(%normalized);
168 }
169  
170 # Normalize match_config if it's a hash (not a callback)
171 239 ▶ if (ref $match_config_or_cb eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $match_config_or_cb eq "HASH") 956 0 956
if (ref $match_config_or_cb eq "HASH") 20808 0 20808
172 239 my %normalized;
173 0 for my $name (keys %$match_config_or_cb) {
174 # Remove prefix operators like '+', '-', '.'
175 956 my $target_name = $name;
176 239 $target_name =~ s/[^\w]//ga;
177 0 $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 sub _match_apply {
190 32566 ▶ my ($config, $condition, $match_config_or_cb) = @_;
Subroutine _match_apply: called CC=20 SCAR=30.0
191
192 32566 ▶ if ($condition) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition) 33354 8 33362
193 32558 $condition = {%$condition};
194 }
195 32566 my $condition_global = delete $condition->{global};
196
197 33522 my $match = 1;
198 33522 ▶ if ($condition || $condition_global) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition or $condition_global) 33362 0 33362
Condition33%1/3
$condition or $condition_global
l!l&&r!l&&!r
3336200
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 my $check_match = sub {
200 65928 ▶ my ($target_h, $cond_h) = @_;
Subroutine __ANON__: called CC=16 SCAR=27.7
201
202 65928 my $inner_match = 1;
203 65928 ▶ if ($cond_h) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond_h) 66699 25 66724
204 65903 for my $name (keys %$cond_h) {
205 32758 my $cond_val = $cond_h->{$name};
206 32559 my $target_name = $name;
207 32758 my $is_not = ($target_name =~ s/^!//);
208
209 33554 ▶ my $val = $target_h ? $target_h->{$target_name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$target_h ? : 33355 0 33355
210 33554 ▶ my @candidates = (defined $val && ref $val eq 'ARRAY') ? @$val : ($val);
Branch: 1 branch
BranchTrueFalseTotal
defined $val && ref $val eq 'ARRAY' ? : 7 33348 33355
Condition100%3/3
defined $val and ref $val eq "ARRAY"
!ll&&!rl&&r
4333447
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 32559 my $found = 0;
213 32559 for my $c (@candidates) {
214 32560 ▶ if (ref $cond_val eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cond_val eq 'Regexp') { } 4 33352 33356
elsif (defined $cond_val) { } 33346 6 33352
215 4 ▶ 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 32550 ▶ if (defined $c && $c eq $cond_val) { $found = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and $c eq $cond_val) 8340 25006 33346
Condition100%3/3
defined $c and $c eq $cond_val
!ll&&!rl&&r
1250058340
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 6 ▶ if (!defined $c) { $found = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 4 2 6
222 }
223 }
224
225 32559 ▶ $found = !$found if $is_not;
Branch: 1 branch
BranchTrueFalseTotal
if $is_not 7 33348 33355
226
227 33554 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 25006 8349 33355
228 26001 $inner_match = 0;
229 26001 last;
230 }
231 }
232 }
233
234 66724 return $inner_match;
235 33522 };
236
237 33522 my $match_condition = $check_match->($config, $condition);
238 33522 my $match_condition_global = $check_match->($config->{global}, $condition_global);
239
240 32566 ▶ $match = $match_condition && $match_condition_global;
Condition100%3/3
$match_condition && $match_condition_global
!ll&&!rl&&r
6250008356
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 32566 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 8356 25006 33362
244 8157 ▶ if (ref $match_config_or_cb eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $match_config_or_cb eq 'CODE') { } 16 8340 8356
245 972 $match_config_or_cb->($config);
246 }
247 else {
248 9097 for my $match_name (keys %$match_config_or_cb) {
249 41633 my $new_value = $match_config_or_cb->{$match_name};
250
251 41633 ▶ if ($match_name =~ /^\+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_name =~ /^\+(.+)$/) { } 6 41666 41672
252 4786 my $name = $1;
253 4786 my $old_value = $config->{$name};
254
255 4786 ▶ 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 957 $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 957 $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 957 $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 1 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 797 push @{$config->{$name}}, @$new_value;
269 }
270 else {
271 797 ▶ 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 797 ▶ 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 797 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 41467 $config->{$match_name} = $new_value;
278 }
279 }
280 }
281 }
282 }
283  
284 sub apply_build_rules {
285 9726 ▶ my ($self, $config) = @_;
Subroutine apply_build_rules: called CC=2 SCAR=6.9
Pod undocumented
286
287 8930 for my $build_rule (@{$self->build_rules}) {
288 33332 &_match_apply($config, @$build_rule);
289 }
290 }
291  
292 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/ExchangeAPI.pm ↑ Summary blib/lib/SPVM/Builder/LinkInfo.pm »