File Coverage

blib/lib/SPVM/Builder/LinkInfo.pm
Criterion Covered Total %
statement 77 78 98.7
branch 12 14 85.7
condition n/a
subroutine 12 12 100.0
pod 7 7 100.0
total 108 111 97.3


line stmt bran cond sub pod time code
1             package SPVM::Builder::LinkInfo;
2              
3 280     280   71548 use strict;
  280         576  
  280         7986  
4 280     280   1497 use warnings;
  280         581  
  280         6393  
5 280     280   1355 use Config;
  280         556  
  280         12269  
6 280     280   1862 use Carp 'confess';
  280         652  
  280         14402  
7 280     280   1903 use File::Basename 'dirname';
  280         597  
  280         198631  
8              
9             # Fields
10             sub config {
11 273     273 1 2073 my $self = shift;
12 273 100       1814 if (@_) {
13 1         3 $self->{config} = $_[0];
14 1         2 return $self;
15             }
16             else {
17 272         1541 return $self->{config};
18             }
19             }
20              
21             sub output_file {
22 597     597 1 2093 my $self = shift;
23 597 100       2882 if (@_) {
24 1         7 $self->{output_file} = $_[0];
25 1         3 return $self;
26             }
27             else {
28 596         3927 return $self->{output_file};
29             }
30             }
31              
32             sub object_files {
33 930     930 1 2516 my $self = shift;
34 930 100       4071 if (@_) {
35 2         4 $self->{object_files} = $_[0];
36 2         4 return $self;
37             }
38             else {
39 928         6044 return $self->{object_files};
40             }
41             }
42              
43             # Class Methods
44             sub new {
45 332     332 1 1888 my $class = shift;
46            
47 332         5865 my $self = {@_};
48              
49 332         2626 bless $self, $class;
50            
51 332 100       3018 unless (defined $self->object_files) {
52 1         4 $self->object_files([]);
53             }
54              
55 332         1817 return $self;
56             }
57              
58             # Instance Methods
59             sub create_link_command {
60 1     1 1 18 my ($self) = @_;
61            
62 1         25 my $config = $self->config;
63            
64 1         40 my $ld = $config->ld;
65 1         28 my $output_file = $self->output_file;
66 1         24 my $object_files = $self->object_files;
67 1         28 my $object_file_names = [map { $_->to_string; } @$object_files];
  63         166  
68            
69 1         18 my $link_command_args = $self->create_link_command_args;
70            
71             # Note: Arguments of the link command(these contain -l flags) must be
72             # after object file names for resolving symbol names properly
73 1         30 my @link_command = ($ld, '-o', $output_file, @$object_file_names, @$link_command_args);
74            
75 1         19 return \@link_command;
76             }
77              
78             sub create_link_command_args {
79 261     261 1 1399 my ($self) = @_;
80            
81 261         1917 my $config = $self->config;
82            
83 261         1295 my @merged_ldflags;
84            
85 261 50       1929 if (defined $config->ld_optimize) {
86 261         2702 push @merged_ldflags, split(/ +/, $config->ld_optimize);
87             }
88            
89 261         2093 my $output_type = $config->output_type;
90 261 100       2325 if ($output_type eq 'dynamic_lib') {
91 259         1005 push @merged_ldflags, @{$config->dynamic_lib_ldflags};
  259         2267  
92             }
93            
94 261         1948 my $ldflags = $config->ldflags;
95 261         959 push @merged_ldflags, @{$config->ldflags};
  261         1266  
96            
97 261         1033 push @merged_ldflags, @{$config->thread_ldflags};
  261         2570  
98            
99 261         1667 my $lib_dirs = $config->lib_dirs;
100            
101 261         1810 my @lib_dirs_ldflags = map { "-L$_" } @$lib_dirs;
  2         29  
102 261         956 push @merged_ldflags, @lib_dirs_ldflags;
103            
104 261         880 my @lib_ldflags;
105 261         1497 my $libs = $config->libs;
106 261         2534 for my $lib (@$libs) {
107 4         16 my $lib_ldflag;
108            
109 4 50       32 unless (ref $lib) {
110 0         0 $lib = SPVM::Builder::LibInfo->new(name => $lib);
111             }
112            
113 4         47 $lib_ldflag = $lib->to_arg;
114 4         30 push @lib_ldflags, $lib_ldflag;
115             }
116            
117 261         1298 push @merged_ldflags, @lib_ldflags;
118            
119 261         2156 return \@merged_ldflags;
120             }
121              
122             sub to_cmd {
123 1     1 1 20 my ($self) = @_;
124              
125 1         35 my $link_command = $self->create_link_command;
126 1         58 my $link_command_string = "@$link_command";
127            
128 1         16 return $link_command_string;
129             }
130              
131             1;
132              
133             =head1 Name
134              
135             SPVM::Builder::LinkInfo - Link Information
136              
137             =head1 Description
138              
139             The SPVM::Builder::LinkInfo class has methods to manipulate link information.
140              
141             =head1 Usage
142              
143             my $link_info = SPVM::Builder::LinkInfo->new(%fields);
144             my $link_command_string = $link_info->to_cmd;
145              
146             =head1 Fields
147              
148             =head2 config
149              
150             my $config = $link_info->config;
151             $link_info->config($config);
152              
153             Gets and sets the C field.
154              
155             This field is a L object used to link the object files.
156              
157             =head2 output_file
158              
159             my $output_file = $link_info->output_file;
160             $link_info->output_file($output_file);
161              
162             Gets and sets the C field.
163              
164             This field is an output file.
165              
166             =head2 object_files
167              
168             my $object_files = $link_info->object_files;
169             $link_info->object_files($object_files);
170              
171             Gets and sets the C field.
172              
173             This field is an array reference of L objects.
174              
175             =head1 Class Methods
176              
177             =head2 new
178              
179             my $link_info = SPVM::Builder::LinkInfo->new(%fields);
180              
181             Creates a new C object with L.
182              
183             Default Field Values:
184              
185             If a field is not defined, the field is set to the following default value.
186              
187             =over 2
188              
189             =item * L
190              
191             undef
192              
193             =item * L
194              
195             undef
196              
197             =item * L
198              
199             []
200              
201             =back
202              
203             =head1 Instance Methods
204              
205             =head2 create_link_command
206              
207             my $link_command = $link_info->create_link_command;
208              
209             Creates a link command, and returns it. The return value is an array reference.
210              
211             The following one is an example of the return value.
212              
213             [qw(cc -o dylib.so foo.o bar.o -shared -O2 -Llibdir -lz)]
214              
215             =head2 create_link_command_args
216              
217             my $link_command_args = $link_info->create_link_command_args;
218              
219             Creates the parts of the arguments of the link command from the information of the L field, and returns it. The return value is an array reference.
220              
221             The C<-o> option and the object file names are not contained.
222              
223             The following one is an example of the return value.
224              
225             [qw(-shared -O2 -Llibdir -lz)]
226              
227             =head2 to_cmd
228              
229             my $link_command_string = $link_info->to_cmd;
230              
231             Calls the L method and joins all elements of the returned array reference with a space, and returns it.
232              
233             The following one is an example of the return value.
234              
235             "cc -o dylib.so foo.o bar.o -shared -O2 -Llibdir -lz"
236              
237             =head1 Copyright & License
238              
239             Copyright (c) 2023 Yuki Kimoto
240              
241             MIT License