blib/lib/SPVM/Builder/link.pl

filter: Statementstmt 90.5% 77 / 85 Branchbran 50.0% 14 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 82.5% 104 / 126 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/script/spvmdeps »
1 116 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 116 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 116 ▶use ExtUtils::CBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 116 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 116 ▶use File::Path 'mkpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 116 ▶use File::Basename 'basename', 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 116 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 116 ▶use Digest::SHA 'sha1_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 116 ▶use File::Path 'mkpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 116 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 116 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 116 ▶use lib "$FindBin::Bin/../..";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 116 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Get arguments
17 116 my @argv = split("\0", decode_base64($ARGV[0]));
18 116 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 116 my $is_msvc = SPVM::Builder::Util::is_msvc($hint_cc);
21  
22 # Define log file paths
23 116 my $log_stdout = "$command_tmp_dir/stdout.log";
24 116 my $log_stderr = "$command_tmp_dir/stderr.log";
25  
26 # Redirect stdout and stderr to log files
27 116 ▶open(STDOUT, '>', $log_stdout)
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $log_stdout or die "Can't open file '${log_stdout}': $!" 116 0 116
28 or die "Can't open file '$log_stdout': $!";
29 116 ▶open(STDERR, '>', $log_stderr)
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">", $log_stderr or die "Can't open file '${log_stderr}': $!" 116 0 116
30 or die "Can't open file '$log_stderr': $!";
31  
32 # Read dl_func_list
33 116 my $dl_func_list;
34 116 ▶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)) 111 5 116
35 111 $dl_func_list = [split(/\n/, $content)];
36 }
37  
38 # Read object_file_names
39 116 my @object_file_names;
40 116 ▶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)) 116 0 116
41 116 @object_file_names = split(/\n/, $content);
42 }
43  
44 # Read ldflags
45 116 my @ldflags;
46 116 ▶if (my $content = SPVM::Builder::Util::slurp_binary($ldflags_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = SPVM::Builder::Util::slurp_binary($ldflags_file)) 116 0 116
47 116 @ldflags = split(/\n/, $content);
48 }
49  
50 116 my $perllibs = '';
51 116 ▶if (SPVM::Builder::Util::is_windows) {
Branch: 1 branch
BranchTrueFalseTotal
if (SPVM::Builder::Util::is_windows()) 0 116 116
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 116 my $cbuilder_config = {
69 cc => $hint_cc,
70 ld => $ld,
71 lddlflags => '',
72 shrpenv => '',
73 libpth => '',
74 libperl => '',
75 perllibs => $perllibs
76 };
77  
78 116 my $cbuilder = ExtUtils::CBuilder->new(quiet => 1, config => $cbuilder_config);
79  
80 # Create a dedicated object directory in the temporary directory
81 116 my $tmp_object_dir = "$command_tmp_dir/object";
82 116 mkpath($tmp_object_dir);
83  
84 # Copy all object files with path hashing to avoid collisions
85 116 my @tmp_object_file_names;
86 116 for my $object_file_name (@object_file_names) {
87 338 my $sha1 = Digest::SHA::sha1_hex($object_file_name);
88 338 my $tmp_object_dir = "$tmp_object_dir/$sha1";
89 338 mkpath($tmp_object_dir);
90
91 338 my $tmp_object_file_name = "$tmp_object_dir/" . basename($object_file_name);
92 338 ▶ 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}': $!" 338 0 338
93 or die "Can't copy '$object_file_name' to '$tmp_object_file_name': $!";
94 338 push @tmp_object_file_names, $tmp_object_file_name;
95 }
96  
97 116 mkpath dirname $output_file;
98  
99 # Determine link method and output option
100 116 ▶if ($output_type eq 'static_lib') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output_type eq 'static_lib') { } 0 116 116
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 116 ▶ my $link_method = ($output_type eq 'exe') ? 'link_executable' : 'link';
Branch: 1 branch
BranchTrueFalseTotal
$output_type eq 'exe' ? : 3 113 116
114 116 ▶ my $output_option = ($output_type eq 'exe') ? 'exe_file' : 'lib_file';
Branch: 1 branch
BranchTrueFalseTotal
$output_type eq 'exe' ? : 3 113 116
115
116 # Use a temporary file for atomic output generation
117 116 my $tmp_output_file = "$command_tmp_dir/link.output";
118
119 # Link to the temp file first
120 116 my $module_name = "SPVM::$class_name";
121 116 $cbuilder->$link_method(
122 $output_option => $tmp_output_file,
123 objects => \@tmp_object_file_names,
124 extra_linker_flags => "@ldflags",
125 module_name => $module_name,
126 dl_func_list => $dl_func_list,
127 );
128
129 116 ▶ 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}': $!") 116 0 116
130 or confess("Can't move '$tmp_output_file' to '$output_file': $!");
131 }
« blib/script/spvmcc ↑ Summary blib/script/spvmdeps »