| 1 | 119 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 119 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 119 | ▶ | use ExtUtils::CBuilder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 119 | ▶ | use File::Copy (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 119 | ▶ | use File::Path 'mkpath'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 119 | ▶ | use File::Basename 'basename', 'dirname'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 119 | ▶ | use MIME::Base64 qw(decode_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 119 | ▶ | use Digest::SHA 'sha1_hex'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 119 | ▶ | use File::Path 'mkpath'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 119 | ▶ | use Carp 'confess'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 119 | ▶ | use FindBin; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 119 | ▶ | use lib "$FindBin::Bin/../.."; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 119 | ▶ | use SPVM::Builder::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | # Get arguments | ||||||||||
| 17 | 119 | my @argv = split("\0", decode_base64($ARGV[0])); | |||||||||
| 18 | 119 | 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 | 119 | my $is_msvc = SPVM::Builder::Util::is_msvc($hint_cc); | |||||||||
| 21 | |||||||||||
| 22 | # Define log file paths | ||||||||||
| 23 | 119 | my $log_stdout = "$command_tmp_dir/stdout.log"; | |||||||||
| 24 | 119 | my $log_stderr = "$command_tmp_dir/stderr.log"; | |||||||||
| 25 | |||||||||||
| 26 | # Redirect stdout and stderr to log files | ||||||||||
| 27 | 119 | ▶ | open(STDOUT, '>', $log_stdout) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | or die "Can't open file '$log_stdout': $!"; | ||||||||||
| 29 | 119 | ▶ | open(STDERR, '>', $log_stderr) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | or die "Can't open file '$log_stderr': $!"; | ||||||||||
| 31 | |||||||||||
| 32 | # Read dl_func_list | ||||||||||
| 33 | 119 | my $dl_func_list; | |||||||||
| 34 | 119 | ▶ | if (my $content = SPVM::Builder::Util::slurp_binary($dl_func_list_file)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 114 | $dl_func_list = [split(/\n/, $content)]; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | # Read object_file_names | ||||||||||
| 39 | 119 | my @object_file_names; | |||||||||
| 40 | 119 | ▶ | if (my $content = SPVM::Builder::Util::slurp_binary($object_file_names_file)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 119 | @object_file_names = split(/\n/, $content); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | # Read ldflags | ||||||||||
| 45 | 119 | my @ldflags; | |||||||||
| 46 | 119 | ▶ | if (my $content = SPVM::Builder::Util::slurp_binary($ldflags_file)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 119 | @ldflags = split(/\n/, $content); | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 119 | my $perllibs = ''; | |||||||||
| 51 | 119 | ▶ | if (SPVM::Builder::Util::is_windows) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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 | 119 | my $cbuilder_config = { | |||||||||
| 69 | cc => $hint_cc, | ||||||||||
| 70 | ld => $ld, | ||||||||||
| 71 | lddlflags => '', | ||||||||||
| 72 | shrpenv => '', | ||||||||||
| 73 | libpth => '', | ||||||||||
| 74 | libperl => '', | ||||||||||
| 75 | perllibs => $perllibs | ||||||||||
| 76 | }; | ||||||||||
| 77 | |||||||||||
| 78 | 119 | my $cbuilder = ExtUtils::CBuilder->new(quiet => 1, config => $cbuilder_config); | |||||||||
| 79 | |||||||||||
| 80 | # Create a dedicated object directory in the temporary directory | ||||||||||
| 81 | 119 | my $tmp_object_dir = "$command_tmp_dir/object"; | |||||||||
| 82 | 119 | mkpath($tmp_object_dir); | |||||||||
| 83 | |||||||||||
| 84 | # Copy all object files with path hashing to avoid collisions | ||||||||||
| 85 | 119 | my @tmp_object_file_names; | |||||||||
| 86 | 119 | 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
| |||||||||||
| 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 | 119 | mkpath dirname $output_file; | |||||||||
| 98 | |||||||||||
| 99 | # Use a temporary file for atomic output generation | ||||||||||
| 100 | 119 | my $tmp_output_file = "$output_file.tmp.$$"; | |||||||||
| 101 | |||||||||||
| 102 | # Determine link method and output option | ||||||||||
| 103 | 119 | ▶ | if ($output_type eq 'static_lib') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | my @generate_static_lib_command = ('ar', 'rcs', $tmp_output_file, @tmp_object_file_names); | |||||||||
| 105 | |||||||||||
| 106 | # Execute archiver command | ||||||||||
| 107 | 0 | ▶ | system(@generate_static_lib_command) == 0 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | or confess("Failed to generate static library command. \@generate_static_lib_command='@generate_static_lib_command', \$!=$!, \$?=$?."); | ||||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | File::Copy::move($tmp_output_file, $output_file) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | or confess("Can't move '$tmp_output_file' to '$output_file': $!"); | ||||||||||
| 112 | } | ||||||||||
| 113 | else { | ||||||||||
| 114 | 119 | ▶ | my $link_method = ($output_type eq 'exe') ? 'link_executable' : 'link'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 119 | ▶ | my $output_option = ($output_type eq 'exe') ? 'exe_file' : 'lib_file'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | |||||||||||
| 117 | # Link to the temp file first | ||||||||||
| 118 | 119 | my $module_name = "SPVM::$class_name"; | |||||||||
| 119 | 119 | $cbuilder->$link_method( | |||||||||
| 120 | $output_option => $tmp_output_file, | ||||||||||
| 121 | objects => \@tmp_object_file_names, | ||||||||||
| 122 | extra_linker_flags => "@ldflags", | ||||||||||
| 123 | module_name => $module_name, | ||||||||||
| 124 | dl_func_list => $dl_func_list, | ||||||||||
| 125 | ); | ||||||||||
| 126 | |||||||||||
| 127 | 119 | ▶ | File::Copy::move($tmp_output_file, $output_file) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | or confess("Can't move '$tmp_output_file' to '$output_file': $!"); | ||||||||||
| 129 | } | ||||||||||