blib/lib/SPVM/Builder/LibInfo.pm

filter: Statementstmt 60.2% 44 / 73 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 55.6% 59 / 106 CC 21 SCAR 35.2
CC
21
Cov
48%
create_ldflags
57.8
create_ldflags
57.8
new
2.0
CRAP 33.6
« blib/lib/SPVM/Builder/Config/Linker.pm ↑ Summary blib/lib/SPVM/Builder/CC.pm »
1 package SPVM::Builder::LibInfo;
2  
3 1804 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 902 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 902 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 902 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 902 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 902 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 ▶use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Fields
13 has [qw(
14 config
15 name
16 file
17 is_static
18 is_abs
19 )];
20  
21 # Class Methods
22 sub new {
23 15 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
24
25 15 my $self = {@_};
26
27 15 bless $self, $class;
28
29 15 my $config = $self->config;
30
31 15 ▶ unless ($config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config) 0 0 0
32 15 confess("The \"config\" field must be defined.");
33 }
34
35 15 return $self;
36 }
37  
38 # Instance Methods
39 sub create_ldflags {
40 15 ▶ my ($self) = @_;
Subroutine create_ldflags: called CC=10 SCAR=40.6
Pod documented
41
42 15 my $config = $self->config;
43
44 # Flags for the linker (e.g., "-l" or "")
45 15 my $lib_option_name = $config->lib_option_name;
46 15 my $lib_option_suffix = $config->lib_option_suffix;
47
48 # Library file components for searching
49 15 my $lib_prefix = $config->lib_prefix;
50 15 my $static_lib_ext = $config->static_lib_ext;
51 15 my $dynamic_lib_ext = $config->dynamic_lib_ext;
52
53 15 my $lib_dirs = $self->config->lib_dirs;
54
55 # Library is linked by file path
56 15 my $static = $self->is_static;
57 15 my $lib_name = $self->name;
58 15 my $is_abs = $self->is_abs;
59
60 15 my @link_command_ldflags;
61
62 0 ▶ if ($is_abs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_abs) { } 0 0 0
63 0 my $found_lib_file;
64 0 for my $lib_dir (@$lib_dirs) {
65 0 $lib_dir =~ s|[\\/]$||;
66
67 # Search dynamic library
68 0 ▶ unless ($static) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($static) 0 0 0
69 # Unix: libfoo.so, MSVC: foo.dll
70 # Search using $lib_prefix and $dynamic_lib_ext
71 0 my $dynamic_lib_file_base = "${lib_prefix}${lib_name}.${dynamic_lib_ext}";
72 0 my $dynamic_lib_file = "$lib_dir/$dynamic_lib_file_base";
73
74 0 ▶ if (-f $dynamic_lib_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $dynamic_lib_file) 0 0 0
75 0 $found_lib_file = $dynamic_lib_file;
76 0 last;
77 }
78 }
79
80 # Search static library
81 # Unix: libfoo.a, MSVC: foo.lib
82 # Search using $lib_prefix and $static_lib_ext
83 0 my $static_lib_file_base = "${lib_prefix}${lib_name}${static_lib_ext}";
84 0 my $static_lib_file = "$lib_dir/$static_lib_file_base";
85 0 ▶ if (-f $static_lib_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $static_lib_file) 0 0 0
86 0 $found_lib_file = $static_lib_file;
87 0 last;
88 }
89 }
90
91 0 ▶ if (defined $found_lib_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found_lib_file) 0 0 0
92 0 $self->file = $found_lib_file;
93 0 push @link_command_ldflags, $self->file;
94 }
95 }
96 else {
97 0 my $name = $self->name;
98 0 ▶ if ($self->is_static) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_static) { } 0 0 0
99 0 my $static_lib_begin = $config->static_lib_braces->[0];
100 0 my $static_lib_end = $config->static_lib_braces->[1];
101
102 0 ▶ if (length $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name) 0 0 0
103 # Construct flag: e.g. "-Wl,-Bstatic -lfoo" or "foo.lib"
104 0 push @link_command_ldflags, "$static_lib_begin ${lib_option_name}${name}${lib_option_suffix} $static_lib_end";
105 }
106 }
107 else {
108 0 ▶ if (length $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name) 0 0 0
109 # Construct flag: e.g. "-lfoo" or "foo.lib"
110 0 push @link_command_ldflags, "${lib_option_name}${name}${lib_option_suffix}";
111 }
112 }
113 }
114
115 0 return \@link_command_ldflags;
116 }
117  
118 sub to_string {
119 0 ▶ my ($self) = @_;
Subroutine to_string: called CC=1 SCAR=6.9
Pod documented
120
121 0 return $self->name;
122 }
123  
124 1;
125  
126 =head1 Name
127  
128 SPVM::Builder::LibInfo - Library Information for a Linker
129  
130 =head1 Description
131  
132 SPVM::Builder::LibInfo class manages a library information for a linker.
133  
134 =head1 Usage
135  
136 my $lib_info = SPVM::Builder::LibInfo->new(%fields);
137 my $lib_ldflags = $lib_info->create_ldflags;
138  
139 =head1 Fields
140  
141 =head2 config
142  
143 my $config = $lib_info->config;
144 $lib_info->config($config);
145  
146 Gets and sets the C<config> field, an L<SPVM::Builder::Config> object.
147  
148 =head2 name
149  
150 my $name = $lib_info->name;
151 $lib_info->name($name);
152  
153 Gets and sets the C<name> field, a library name.
154  
155 Examples:
156
157 $lib_info->name('z');
158
159 $lib_info->name('png');
160  
161 =head2 file
162  
163 my $file = $lib_info->file;
164 $lib_info->file($file);
165  
166 Gets and sets the C<file> field, the absolute path of the library file such as C</path/libz.so>, C</path/libpng.a>.
167  
168 =head2 is_static
169  
170 my $is_static = $lib_info->is_static;
171 $lib_info->is_static($is_static);
172  
173 Gets and sets the C<is_static> field. If this field is a true value, this library is linked statically. Otherwise, is linked dynamically.
174  
175 =head2 is_abs
176  
177 my $is_abs = $lib_info->is_abs;
178 $lib_info->is_abs($is_abs);
179  
180 Gets and sets the C<is_abs> field. If this field is a true value, the library is linked by the absolute path L</"file">. Otherwise, is linked by the relative path from library search path.
181  
182 =head1 Class Methods
183  
184 =head2 new
185  
186 my $lib_info = SPVM::Builder::LibInfo->new(%fields);
187  
188 Creates a new L<SPVM::Builder::LibInfo> object given L<fields|/"Fields">, and returns it.
189  
190 Exceptions:
191  
192 L</"config"> field must be defined.
193  
194 =head1 Instance Methods
195  
196 =head2 create_ldflags
197  
198 my $lib_ldflags = $lib_info->create_ldflags;
199  
200 Creates an array reference of the library part of the linker flags given to the linker L<ld|SPVM::Builder::Config::Linker/"ld">, and returns it.
201  
202 Return Value Examples:
203
204 ["-lfoo", "-Wl,-Bstatic -lfoo -Wl,-Bdynamic", "/path/foo.so", "/path/foo.a"]
205  
206 =head2 to_string
207  
208 my $lib_name = $lib_info->to_string;
209  
210 Returns L</"name"> field.
211  
212 =head1 Operators
213  
214 Overloads the following operators.
215  
216 =head2 bool
217  
218 my $bool = !!$lib_info;
219  
220 Always true.
221  
222 =head2 stringify
223  
224 my $lib_name = "$lib_info";
225  
226 Alias for L</"to_string"> method.
227  
228 =head1 Copyright & License
229  
230 Copyright (c) 2023 Yuki Kimoto
231  
232 MIT License
« blib/lib/SPVM/Builder/Config/Linker.pm ↑ Summary blib/lib/SPVM/Builder/CC.pm »