| 1 | package SPVM::Builder::LibInfo; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 349 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 349 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 349 | ▶ | use Config; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 349 | ▶ | use Carp 'confess'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 349 | ▶ | use File::Basename 'dirname'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 349 | ▶ | use SPVM::Builder::Accessor 'has'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 0 | ▶ | use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
__ANON__: called CC=1 SCAR=0.6
Subroutine
__ANON__: called CC=1 SCAR=0.6
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | # Fields | ||||||||||||||
| 13 | 349 | has [qw( | |||||||||||||
| 14 | config | ||||||||||||||
| 15 | name | ||||||||||||||
| 16 | file | ||||||||||||||
| 17 | is_static | ||||||||||||||
| 18 | is_abs | ||||||||||||||
| 19 | )]; | ||||||||||||||
| 20 | |||||||||||||||
| 21 | # Class Methods | ||||||||||||||
| 22 | 442 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 23 | 0 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 24 | |||||||||||||||
| 25 | 0 | my $self = {@_}; | |||||||||||||
| 26 | |||||||||||||||
| 27 | 0 | bless $self, $class; | |||||||||||||
| 28 | |||||||||||||||
| 29 | 0 | my $config = $self->config; | |||||||||||||
| 30 | |||||||||||||||
| 31 | 0 | ▶ | unless ($config) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 32 | 0 | confess("The \"config\" field must be defined."); | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 0 | return $self; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | # Instance Methods | ||||||||||||||
| 39 | 442 | ▶ | sub create_ldflags { | ||||||||||||
|
Subroutine
create_ldflags: called CC=10 SCAR=42.0
Pod
documented
| |||||||||||||||
| 40 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
create_ldflags: not called CC=10 SCAR=42.0
Pod
documented
| |||||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | my $config = $self->config; | |||||||||||||
| 43 | |||||||||||||||
| 44 | # Flags for the linker (e.g., "-l" or "") | ||||||||||||||
| 45 | 0 | my $lib_option_name = $config->lib_option_name; | |||||||||||||
| 46 | 0 | my $lib_option_suffix = $config->lib_option_suffix; | |||||||||||||
| 47 | |||||||||||||||
| 48 | # Library file components for searching | ||||||||||||||
| 49 | 0 | my $lib_prefix = $config->lib_prefix; | |||||||||||||
| 50 | 0 | my $static_lib_ext = $config->static_lib_ext; | |||||||||||||
| 51 | 0 | my $dynamic_lib_ext = $config->dynamic_lib_ext; | |||||||||||||
| 52 | |||||||||||||||
| 53 | 0 | my $lib_dirs = $self->config->lib_dirs; | |||||||||||||
| 54 | |||||||||||||||
| 55 | # Library is linked by file path | ||||||||||||||
| 56 | 0 | my $static = $self->is_static; | |||||||||||||
| 57 | 0 | my $lib_name = $self->name; | |||||||||||||
| 58 | 0 | my $is_abs = $self->is_abs; | |||||||||||||
| 59 | |||||||||||||||
| 60 | 0 | my @link_command_ldflags; | |||||||||||||
| 61 | |||||||||||||||
| 62 | 0 | ▶ | if ($is_abs) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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: 2 branches
| |||||||||||||||
| 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: 2 branches
| |||||||||||||||
| 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: 2 branches
| |||||||||||||||
| 86 | 0 | $found_lib_file = $static_lib_file; | |||||||||||||
| 87 | 0 | last; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 0 | ▶ | if (defined $found_lib_file) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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: 2 branches
| |||||||||||||||
| 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: 2 branches
| |||||||||||||||
| 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: 2 branches
| |||||||||||||||
| 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 | 442 | ▶ | sub to_string { | ||||||||||||
|
Subroutine
to_string: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||
| 119 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
to_string: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | return $self->name; | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | |||||||||||||||
| 124 | 349 | 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 | ||||||||||||||