File Coverage

blib/lib/SPVM/Builder/LibInfo.pm
Criterion Covered Total %
statement 56 72 77.7
branch 13 22 59.0
condition n/a
subroutine 12 16 75.0
pod 8 8 100.0
total 89 118 75.4


line stmt bran cond sub pod time code
1             package SPVM::Builder::LibInfo;
2              
3 283     283   2015 use strict;
  283         698  
  283         8274  
4 283     283   1619 use warnings;
  283         807  
  283         6697  
5 283     283   2501 use Config;
  283         877  
  283         10539  
6 283     283   1926 use Carp 'confess';
  283         729  
  283         15224  
7 283     283   2036 use File::Basename 'dirname';
  283         2132  
  283         28251  
8              
9 283     283   2175 use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;
  283     0   788  
  283         2911  
  0         0  
  0         0  
10              
11             # Fields
12             sub name {
13 7     7 1 34 my $self = shift;
14 7 100       42 if (@_) {
15 3         32 $self->{name} = $_[0];
16 3         19 return $self;
17             }
18             else {
19 4         25 return $self->{name};
20             }
21             }
22              
23             sub file {
24 0     0 1 0 my $self = shift;
25 0 0       0 if (@_) {
26 0         0 $self->{file} = $_[0];
27 0         0 return $self;
28             }
29             else {
30 0         0 return $self->{file};
31             }
32             }
33              
34             sub is_static {
35 10     10 1 30 my $self = shift;
36 10 100       44 if (@_) {
37 3         34 $self->{is_static} = $_[0];
38 3         19 return $self;
39             }
40             else {
41 7         220 return $self->{is_static};
42             }
43             }
44              
45             sub is_abs {
46 10     10 1 36 my $self = shift;
47 10 100       49 if (@_) {
48 3         31 $self->{is_abs} = $_[0];
49 3         20 return $self;
50             }
51             else {
52 7         55 return $self->{is_abs};
53             }
54             }
55              
56             sub static_option_cb {
57 6     6 1 24 my $self = shift;
58 6 100       46 if (@_) {
59 3         33 $self->{static_option_cb} = $_[0];
60 3         18 return $self;
61             }
62             else {
63 3         61 return $self->{static_option_cb};
64             }
65             }
66              
67             # Class Methods
68             sub new {
69 3     3 1 26 my $class = shift;
70            
71 3         17 my $self = {@_};
72              
73 3         29 bless $self, $class;
74              
75 3 50       29 unless (defined $self->is_static) {
76 3         21 $self->is_static(0);
77             }
78            
79 3 50       29 unless (defined $self->is_abs) {
80 3         34 $self->is_abs(0);
81             }
82            
83 3 50       20 unless (defined $self->static_option_cb) {
84             my $default_static_option_cb = sub {
85 0     0   0 my ($self, $name) = @_;
86            
87 0         0 $name = "-Wl,-Bstatic -l$name -Wl,-Bdynamic";
88            
89 0         0 return $name;
90 3         85 };
91 3         36 $self->static_option_cb($default_static_option_cb);
92             }
93            
94 3         19 return $self;
95             }
96              
97             # Instance Methods
98             sub to_arg {
99 4     4 1 230 my ($self) = @_;
100            
101 4         20 my $link_command_arg;
102            
103 4 50       26 if ($self->is_abs) {
104 0 0       0 if (defined $self->file) {
105 0         0 $link_command_arg = $self->file;
106             }
107             else {
108 0         0 $link_command_arg = "";
109             }
110             }
111             else {
112 4         40 my $name = $self->name;
113 4 50       56 if ($self->is_static) {
114 0         0 $link_command_arg = $self->static_option_cb->($self, $name);
115             }
116             else {
117 4         15 $link_command_arg = "-l$name";
118             }
119             }
120            
121 4         22 return $link_command_arg;
122             }
123              
124             sub to_string {
125 0     0 1   my ($self) = @_;
126            
127 0           return $self->name;
128             }
129              
130             1;
131              
132             =head1 Name
133              
134             SPVM::Builder::LibInfo - Library Information
135              
136             =head1 Description
137              
138             The SPVM::Builder::LibInfo class has methods to manipulate library information.
139              
140             =head1 Usage
141              
142             my $lib_info = SPVM::Builder::LibInfo->new(%fields);
143             my $lib_arg = $lib_info->to_arg;
144              
145             =head1 Fields
146              
147             =head2 name
148              
149             my $name = $lib_info->name;
150             $lib_info->name($name);
151              
152             Gets and sets the C field.
153              
154             This field is a library name.
155              
156             Examples:
157            
158             $lib_info->name('z');
159            
160             $lib_info->name('png');
161              
162             =head2 file
163              
164             my $file = $lib_info->file;
165             $lib_info->file($file);
166              
167             Gets and sets the C field.
168              
169             This field is the absolute path of the library file like C, C.
170              
171             =head2 is_static
172              
173             my $is_static = $lib_info->is_static;
174             $lib_info->is_static($is_static);
175              
176             Gets and sets the C field.
177              
178             If this field is a true value, a static library is linked.
179              
180             =head2 is_abs
181              
182             my $is_abs = $lib_info->is_abs;
183             $lib_info->is_abs($is_abs);
184              
185             Gets and sets the C field.
186              
187             If this field is a true value, the library is linked by the library name like C<-lfoo>.
188              
189             Otherwise the library is linked by the absolute path of the library like C.
190              
191             =head2 static_option_cb
192              
193             my $static_option_cb = $lib_info->static_option_cb;
194             $lib_info->static_option_cb($static_option_cb);
195              
196             Gets and sets the C field.
197              
198             This field is the callback to create a linker option to link a static library.
199              
200             =head1 Class Methods
201              
202             =head2 new
203              
204             my $lib_info = SPVM::Builder::LibInfo->new(%fields);
205              
206             Creates a L object with L.
207              
208             Default Field Values:
209              
210             If a field is not defined, the field is set to the following default value.
211              
212             =over 2
213              
214             =item * L
215              
216             undef
217              
218             =item * L
219              
220             undef
221              
222             =item * L
223              
224             0
225              
226             =item * L
227              
228             0
229              
230             =item * L
231              
232             sub {
233             my ($self, $name) = @_;
234            
235             $name = "-Wl,-Bstatic -l$name -Wl,-Bdynamic";
236            
237             return $name;
238             };
239              
240             =back
241              
242             =head1 Instance Methods
243              
244             =head2 to_arg
245              
246             my $link_command_arg = $lib_info->to_arg;
247              
248             Creates an argument of the link command from the L field and L field, and returns it.
249              
250             The following ones are examples of the return value.
251            
252             -lfoo
253             -Wl,-Bstatic -lfoo -Wl,-Bdynamic
254             /path/foo.so
255             /path/foo.a
256              
257             =head2 to_string
258              
259             my $lib_name = $lib_info->to_string;
260              
261             Returns the L field.
262              
263             =head1 Operators
264              
265             Overloads the following operators.
266              
267             =head2 bool
268              
269             my $bool = !!$lib_info;
270              
271             Always true.
272              
273             =head2 stringify
274              
275             my $lib_name = "$lib_info";
276              
277             Alias for the L method.
278              
279             =head1 Copyright & License
280              
281             Copyright (c) 2023 Yuki Kimoto
282              
283             MIT License