blib/lib/SPVM/Builder/link.pl

filter: Statementstmt 90.3% 75 / 83 Branchbran 50.0% 13 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 82.7% 101 / 122 CC 1 SCAR 0.0
CC
1
Cov
81%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/script/spvmcc ↑ Summary blib/lib/SPVM/Builder/compile.pl »
1 90 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 90 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 90 ▶use ExtUtils::CBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 90 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 90 ▶use File::Path 'mkpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 90 ▶use File::Basename 'basename', 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 90 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 90 ▶use Digest::SHA 'sha1_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 90 ▶use File::Path 'mkpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 90 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 90 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 90 ▶use lib "$FindBin::Bin/../..";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 90 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Get arguments
17 90 my @argv = split("\0", decode_base64($ARGV[0]));
18 90 my ($command_tmp_dir, $output_file, $class_name, $hint_cc, $output_type, $ld, $dl_func_list_file, $object_file_names_file, $ldflags_file) = @argv;
19  
20 90 my $is_msvc = SPVM::Builder::Util::is_msvc($hint_cc);
21  
22 # Define log file paths
23 90 my $log_stdout = "$command_tmp_dir/stdout.log";
24 90 my $log_stderr = "$command_tmp_dir/stderr.log";
25  
26 # Redirect stdout and stderr to log files
27 90 ▶open(STDOUT, '>', $log_stdout)
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $log_stdout or die "Can't open file '${log_stdout}': $!" 90 0 90
28 or die "Can't open file '$log_stdout': $!";
29 90 ▶open(STDERR, '>', $log_stderr)
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">", $log_stderr or die "Can't open file '${log_stderr}': $!" 90 0 90
30 or die "Can't open file '$log_stderr': $!";
31  
32 # Read dl_func_list
33 90 my $dl_func_list;
34 90 ▶if (my $content = SPVM::Builder::Util::slurp_binary($dl_func_list_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = SPVM::Builder::Util::slurp_binary($dl_func_list_file)) 87 3 90
35 87 $dl_func_list = [split(/\n/, $content)];
36 }
37  
38 # Read object_file_names
39 90 my @object_file_names;
40 90 ▶if (my $content = SPVM::Builder::Util::slurp_binary($object_file_names_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = SPVM::Builder::Util::slurp_binary($object_file_names_file)) 90 0 90
41 90 @object_file_names = split(/\n/, $content);
42 }
43  
44 # Read ldflags
45 90 my @ldflags;
46 90 ▶if (my $content = SPVM::Builder::Util::slurp_binary($ldflags_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = SPVM::Builder::Util::slurp_binary($ldflags_file)) 90 0 90
47 90 @ldflags = split(/\n/, $content);
48 }
49  
50 90 my $perllibs = '';
51 90 ▶if (SPVM::Builder::Util::is_windows) {
Branch: 1 branch
BranchTrueFalseTotal
if (SPVM::Builder::Util::is_windows()) 0 90 90
52 # [HACK]
53 # On Windows, if perllibs is empty, ExtUtils::CBuilder::link may generate
54 # an empty INPUT() section in the response file (linker argument file),
55 # which leads to a build failure.
56 # To prevent this, we explicitly specify kernel32.lib (a core Windows library)
57 0 ▶ if ($is_msvc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_msvc) { } 0 0 0
58 # For MSVC (cl.exe)
59 0 $perllibs = 'kernel32.lib';
60 }
61 else {
62 # For others (e.g. MinGW/gcc)
63 0 $perllibs = '-lkernel32';
64 }
65 }
66  
67 # Configure CBuilder
68 90 my $cbuilder_config = {
69 cc => $hint_cc,
70 ld => $ld,
71 lddlflags => '',
72 shrpenv => '',
73 libpth => '',
74 libperl => '',
75 perllibs => $perllibs
76 };
77  
78 90 my $cbuilder = ExtUtils::CBuilder->new(quiet => 1, config => $cbuilder_config);
79  
80 # Create a dedicated object directory in the temporary directory
81 90 my $tmp_object_dir = "$command_tmp_dir/object";
82 90 mkpath($tmp_object_dir);
83  
84 # Copy all object files with path hashing to avoid collisions
85 90 my @tmp_object_file_names;
86 90 for my $object_file_name (@object_file_names) {
87 161 my $sha1 = Digest::SHA::sha1_hex($object_file_name);
88 161 my $tmp_object_dir = "$tmp_object_dir/$sha1";
89 161 mkpath($tmp_object_dir);
90
91 161 my $tmp_object_file_name = "$tmp_object_dir/" . basename($object_file_name);
92 161 ▶ File::Copy::copy($object_file_name, $tmp_object_file_name)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($object_file_name, $tmp_object_file_name) or die "Can't copy '${object_file_name}' to '${tmp_object_file_name}': $!" 161 0 161
93 or die "Can't copy '$object_file_name' to '$tmp_object_file_name': $!";
94 161 push @tmp_object_file_names, $tmp_object_file_name;
95 }
96  
97 90 mkpath dirname $output_file;
98  
99 # Determine link method and output option
100 90 ▶if ($output_type eq 'static_lib') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output_type eq 'static_lib') { } 0 90 90
101 0 my $tmp_output_file;
102 0 $tmp_output_file = "$command_tmp_dir/link.output";
103 0 my @generate_static_lib_command = ('ar', 'rcs', $tmp_output_file, @tmp_object_file_names);
104
105 # Execute archiver command
106 0 ▶ system(@generate_static_lib_command) == 0
Branch: 1 branch
BranchTrueFalseTotal
system(@generate_static_lib_command) == 0 or confess("Failed to generate static library command. \@generate_static_lib_command='@{generate_static_lib_command}', \$!=$!, \$?=$?.") 0 0 0
107 or confess("Failed to generate static library command. \@generate_static_lib_command='@generate_static_lib_command', \$!=$!, \$?=$?.");
108
109 0 ▶ File::Copy::move($tmp_output_file, $output_file)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::move($tmp_output_file, $output_file) or confess("Can't move '${tmp_output_file}' to '${output_file}': $!") 0 0 0
110 or confess("Can't move '$tmp_output_file' to '$output_file': $!");
111 }
112 else {
113 90 ▶ my $link_method = ($output_type eq 'exe') ? 'link_executable' : 'link';
Branch: 1 branch
BranchTrueFalseTotal
$output_type eq 'exe' ? : 1 89 90
114 90 ▶ my $output_option = ($output_type eq 'exe') ? 'exe_file' : 'lib_file';
Branch: 1 branch
BranchTrueFalseTotal
$output_type eq 'exe' ? : 1 89 90
115
116 # Link to the .tmp file instead of the final file
117 90 my $module_name = "SPVM::$class_name";
118 90 $cbuilder->$link_method(
119 $output_option => $output_file,
120 objects => \@tmp_object_file_names,
121 extra_linker_flags => "@ldflags",
122 module_name => $module_name,
123 dl_func_list => $dl_func_list,
124 );
125 }
126  
« blib/script/spvmcc ↑ Summary blib/lib/SPVM/Builder/compile.pl »