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

filter: Statementstmt 29.6% 35 / 118 Branchbran 4.3% 2 / 46 Conditioncond 10.0% 1 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 71.4% 10 / 14 Podpod 33.3% 1 / 3 totaltotal 24.8% 49 / 197 CC 32 SCAR 54.2
CC
32
Cov
22%
setup
426.2
_get_config_from_build_type_msvc
42.0
__ANON__
12.0
CRAP 224.9
↑ Summary blib/lib/SPVM/Builder.pm »
1 package SPVM::Builder::Config::Global::MSVC;
2  
3 3 ▶use parent 'SPVM::Builder::Config::Global';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Basename 'dirname', 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 my $fields = [qw(
15 cc
16 ld
17 )];
18  
19 3 has($fields);
20  
21 2 ▶sub new {
Subroutine new: called CC=2 SCAR=15.4
Pod documented
22 2 my $self = shift->SUPER::new(@_);
23
24 2 $self->setup;
25
26 # --- Initialize Global Config ---
27 0 ▶ $self->build_rule_any(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 my ($config) = @_;
29
30 0 $config->clear_system_fields;
31 0 });
32
33 # 1. Common settings for all configs
34 0 $self->build_rule_any({
35 # Compiler flags
36 cc => 'cl',
37 cc_version => $self->create_cc_version,
38 long_option_sep => ':',
39 cc_output_option_name => '-Fo',
40 copyright_print_ccflags => ['-nologo'],
41 thread_ccflags => [],
42 ndebug_ccflags => ['-DNDEBUG'],
43 function_level_linking_ccflags => ['-Gy'],
44 source_encoding_ccflags => ['-utf-8'],
45 library_linkage_ccflags => ['-MT'],
46
47 # Linker flags
48 ld => $self->ld,
49 ld_version => $self->create_cc_version,
50 hint_cc => $self->cc,
51 static_lib_braces => ["", ""],
52 lib_prefix => "",
53 lib_option_name => "",
54 lib_option_suffix => ".lib",
55 ld_output_option_name => '-OUT',
56 lib_dir_option_name => '-LIBPATH',
57 libbcrypt_ldflags => ['bcrypt.lib'],
58 copyright_print_ldflags => ['-nologo'],
59 debug_info_ldflags => ['-DEBUG'],
60 });
61
62 # C specific rules
63 0 $self->build_rule({language => 'c'}, {
64 'language_ccflags' => ['-TC'],
65 });
66
67 # Ensure C11 as baseline if unspecified or c99
68 0 $self->build_rule({language => 'c', std => qr/^(|c99)$/}, {
69 std => 'c11',
70 });
71  
72 # C++ specific rules
73 0 $self->build_rule({language => 'cpp'}, {
74 'language_ccflags' => ['-TP'],
75 'cpp_exception_handling_ccflags' => ['-EHsc'],
76 });
77  
78 # Ensure C++14 as baseline if specified as c++11
79 0 $self->build_rule({language => 'cpp', std => 'c++11'}, {
80 std => 'c++14',
81 });
82
83 0 $self->build_rule({output_type => 'dynamic_lib'}, {
84 'dynamic_lib_ldflags' => ['-DLL'],
85 library_linkage_ccflags => ['-MD'],
86 });
87
88 # Define build types
89 0 my @build_types = qw(Debug Release RelWithDebInfo MinSizeRel);
90 0 for my $build_type (@build_types) {
91 0 my $config = &_get_config_from_build_type_msvc($build_type);
92
93 0 $self->build_rule(
94 { global => {build_type => $build_type} },
95 $config
96 );
97 }
98
99 0 return $self;
100 }
101  
102 0 ▶sub _get_config_from_build_type_msvc {
Subroutine _get_config_from_build_type_msvc: not called CC=6 SCAR=37.4
103 0 my ($build_type) = @_;
104
105 0 my $config;
106
107 0 ▶ if (!defined $build_type || $build_type eq 'Release') {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $build_type or $build_type eq 'Release') { } 0 0 0
elsif ($build_type eq 'Debug') { } 0 0 0
elsif ($build_type eq 'RelWithDebInfo') { } 0 0 0
elsif ($build_type eq 'MinSizeRel') { } 0 0 0
Condition0%0/3
not defined $build_type or $build_type eq "Release"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $build_type or $build_type eq "Release"
not defined $build_type $build_type eq "Release"
Truth table0%0/3
not(A) or B
A= not defined $build_typeB= $build_type eq "Release"
ABresult
1X1
011
000
108 0 $config = {
109 optimize => '-O2',
110 debug_info_ccflags => [],
111 ndebug_ccflags => ['-DNDEBUG'],
112
113 ld_optimize => '/OPT:REF /OPT:ICF',
114 debug_info_ldflags => [],
115 };
116 }
117 elsif ($build_type eq 'Debug') {
118 0 $config = {
119 optimize => '-Od',
120 debug_info_ccflags => ['-Z7'],
121 ndebug_ccflags => [],
122
123 ld_optimize => '',
124 debug_info_ldflags => ['/DEBUG'],
125 ld_debug_output_option_name => '/PDB',
126 enable_ld_debug_output_option => 1,
127 debug_output_file_ext => 'pdb',
128 };
129 }
130 elsif ($build_type eq 'RelWithDebInfo') {
131 0 $config = {
132 optimize => '-O2',
133 debug_info_ccflags => ['-Z7'],
134 ndebug_ccflags => ['-DNDEBUG'],
135
136 ld_optimize => '/OPT:REF /OPT:ICF',
137 debug_info_ldflags => ['/DEBUG'],
138 ld_debug_output_option_name => '/PDB',
139 enable_ld_debug_output_option => 1,
140 debug_output_file_ext => 'pdb',
141 };
142 }
143 elsif ($build_type eq 'MinSizeRel') {
144 0 $config = {
145 optimize => '-O1',
146 debug_info_ccflags => [],
147 ndebug_ccflags => ['-DNDEBUG'],
148
149 ld_optimize => '/OPT:REF /OPT:ICF',
150 debug_info_ldflags => [],
151 };
152 }
153 else {
154 0 die "Unknown build_type: $build_type";
155 }
156
157 0 return $config;
158 }
159  
160 2 ▶sub setup {
Subroutine setup: called CC=23 SCAR=60.5
Pod undocumented
161 2 my ($self) = @_;
162
163 2 my $cl_long_path = $self->cc;
164
165 2 ▶ unless (defined $cl_long_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cl_long_path) 2 0 2
166 # Determine architecture (Windows only)
167 2 ▶ my $arch = $ENV{PROCESSOR_ARCHITECTURE} || '';
Condition50%1/2
$ENV{'PROCESSOR_ARCHITECTURE'} || ''
l!l
02
MC/DC0%0/1
$ENV{'PROCESSOR_ARCHITECTURE'} || ''
$ENV{'PROCESSOR_ARCHITECTURE'}
Truth table50%1/2
A
A= $ENV{'PROCESSOR_ARCHITECTURE'}
Aresult
11
00
168
169 2 ▶ unless ($arch) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arch) 2 0 2
170 2 Carp::confess("Can't determine processor architecture. PROCESSOR_ARCHITECTURE environment variable not set.");
171 }
172
173 # Map Windows architecture to MSVC host/target path
174 0 my $host_target;
175 0 ▶ if ($arch eq 'AMD64') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arch eq 'AMD64') { } 0 0 0
elsif ($arch eq 'ARM64') { } 0 0 0
176 0 $host_target = 'Hostx64/x64';
177 }
178 elsif ($arch eq 'ARM64') {
179 0 $host_target = 'Hostarm64/arm64';
180 }
181 else {
182 0 Carp::confess("Unsupported processor architecture: $arch");
183 }
184
185 # Search for cl.exe in Visual Studio installation directories
186 0 my @search_dirs = (
187 'C:/Program Files/Microsoft Visual Studio',
188 'C:/Program Files (x86)/Microsoft Visual Studio',
189 );
190
191 0 for my $base_dir (@search_dirs) {
192 0 ▶ next unless -d $base_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $base_dir 0 0 0
193
194 File::Find::find({
195 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
196 0 ▶ return if $cl_long_path; # Already found
Branch: 1 branch
BranchTrueFalseTotal
if $cl_long_path 0 0 0
197
198 0 my $full_path = $File::Find::name;
199 # Check if path contains the correct host/target
200 0 ▶ if ($full_path =~ m#\Q$host_target\E/cl.exe$#i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_path =~ m[\Q$host_target\E/cl.exe$]i) 0 0 0
201 0 $cl_long_path = $full_path;
202 }
203 },
204 0 no_chdir => 1,
205 }, $base_dir);
206
207 0 ▶ last if $cl_long_path;
Branch: 1 branch
BranchTrueFalseTotal
if $cl_long_path 0 0 0
208 }
209
210 0 ▶ unless (defined $cl_long_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cl_long_path) 0 0 0
211 0 Carp::confess("Can't find cl.exe in Microsoft Visual Studio directories for architecture: $arch");
212 }
213 }
214
215 0 ▶ unless (-f $cl_long_path && -x $cl_long_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $cl_long_path and -x $cl_long_path) 0 0 0
Condition0%0/3
-f $cl_long_path and -x $cl_long_path
!ll&&!rl&&r
000
MC/DC0%0/2
-f $cl_long_path and -x $cl_long_path
-f $cl_long_path -x $cl_long_path
Truth table0%0/3
A and B
A= -f $cl_long_pathB= -x $cl_long_path
ABresult
0X0
100
111
216 0 Carp::confess("'$cl_long_path' is not an executable file.");
217 }
218
219 0 $self->cc(basename $cl_long_path);
220
221 0 ▶ unless (defined $self->ld) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->ld) 0 0 0
222 0 $self->ld('link.exe');
223 }
224
225 0 require Win32;
226
227 # Convert to short path (handle spaces)
228 0 my $cl_short_path = Win32::GetShortPathName($cl_long_path);
229 0 ▶ unless ($cl_short_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl_short_path) 0 0 0
230 0 Carp::confess("Failed to convert path to short format: $cl_long_path");
231 }
232
233 # Derive vcvarsall.bat path from cl.exe path
234 # Replace: VC/Tools/MSVC/{version}/bin/Hostx64/x64
235 # With: VC/Auxiliary/Build
236 0 my $vcvarsall_path = $cl_short_path;
237 0 $vcvarsall_path =~ s|VC/Tools/MSVC/[^/]+/bin/[^/]+/[^/]+|VC/Auxiliary/Build|i;
238 0 $vcvarsall_path =~ s/cl\.exe$/vcvarsall.bat/i;
239
240 0 ▶ unless (-f $vcvarsall_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $vcvarsall_path) 0 0 0
241 0 Carp::confess("Can't find vcvarsall.bat at: $vcvarsall_path");
242 }
243
244 # Parse Host and Target from path
245 0 ▶ unless ($cl_short_path =~ m#bin/Host(x64|x86|arm64|arm)/(x64|x86|arm64|arm)/cl\.exe#i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl_short_path =~ m[bin/Host(x64|x86|arm64|arm)/(x64|x86|arm64|arm)/cl\.exe]i) 0 0 0
246 0 Carp::confess("Failed to parse architecture from cl.exe path: $cl_short_path");
247 }
248 0 my $host = lc $1;
249 0 my $target = lc $2;
250  
251 # Determine vcvarsall.bat argument
252 0 ▶ my $vcvarsall_arg = ($host eq $target) ? $target : "${host}_${target}";
Branch: 1 branch
BranchTrueFalseTotal
$host eq $target ? : 0 0 0
253
254 # For system/backtick execution, convert path to Windows format with backslashes
255 0 my $vcvarsall_path_win = $vcvarsall_path;
256 0 $vcvarsall_path_win =~ s|/|\\|g;
257
258 # Execute vcvarsall.bat and capture environment variables
259 # Set MSYS2_ARG_CONV_EXCL to prevent MSYS2 path conversion in Git Bash
260 0 local $ENV{MSYS2_ARG_CONV_EXCL} = '*';
261
262 0 my $cmd = qq{cmd.exe /c "$vcvarsall_path_win" $vcvarsall_arg && set};
263 0 my $vcvarsall_output = `$cmd`;
264 0 my @vcvarsall_lines = split /\x0D?\x0A/, $vcvarsall_output;
265
266 0 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
267 0 Carp::confess("Failed to execute vcvarsall.bat: $vcvarsall_path");
268 }
269
270 # Remove all environment variables that match INCLUDE, LIB, or PATH case-insensitively
271 0 for my $name (keys %ENV) {
272 0 ▶ if ($name =~ /^(?:INCLUDE|LIB|PATH)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(?:INCLUDE|LIB|PATH)$/i) 0 0 0
273 0 delete $ENV{$name};
274 }
275 }
276
277 0 my %msvc_env;
278 0 for my $line (@vcvarsall_lines) {
279 0 $line =~ s/[\x0D\x0A]//g;
280
281 0 ▶ if ($line =~ /^(INCLUDE|LIB|PATH)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(INCLUDE|LIB|PATH)=(.*)$/) 0 0 0
282 0 my ($name, $value) = ($1, $2);
283 0 $ENV{$name} = $value;
284 0 ▶ unless (length $ENV{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $ENV{$name}) 0 0 0
285 0 Carp::confess("Failed to capture $name environment variable from vcvarsall.bat");
286 }
287 }
288 }
289
290 # Set diagnostic message encoding to English (ASCII)
291 0 $ENV{VSLANG} = '1033';
292 }
293  
294 0 ▶sub create_cc_version {
Subroutine create_cc_version: not called CC=2 SCAR=17.9
Pod undocumented
295 0 my ($self) = @_;
296
297 0 my $cc = $self->cc;
298
299 0 ▶ my $cc_version = `$cc 2>&1` // '';
Condition0%0/2
`$cc 2>&1` // ""
l!l
00
MC/DC0%0/1
`$cc 2>&1` // ""
`$cc 2>&1`
Truth table0%0/2
A
A= `$cc 2>&1`
Aresult
11
00
300
301 0 $cc_version =~ s/\r?\n.*//s;
302
303 0 return $cc_version;
304 }
305  
306 3 1;
307  
308 =head1 Name
309  
310 SPVM::Builder::Config::Global::MSVC - Global Config for MSVC(Microsoft Visual C++)
311  
312 =head1 Description
313  
314 L<SPVM::Builder::Config::Global::MSVC> creates a global config for MSVC(Microsoft Visual C++).
315  
316 =head2 Usage
317  
318 use SPVM::Builder::Config::Global::MSVC;
319
320 my $config_global = SPVM::Builder::Config::Global::MSVC->new;
321  
322 =head1 Super Class
323  
324 L<SPVM::Builder::Config::Global>
325  
326 =head1 Class Methods
327  
328 =head2 new
329  
330 my $config_global = SPVM::Builder::Config::Global::MSVC->new(%fields);
331  
332 Creates a new L<SPVM::Builder::Config::Global::MSVC> object by calling C<new> method in the super class with I<%fields> and returns the new object.
333  
↑ Summary blib/lib/SPVM/Builder.pm »