blib/lib/Affix/Platform/Unix.pm

filter: Statementstmt 77.6% 94 / 121 Branchbran 42.8% 30 / 70 Conditioncond 40.7% 11 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 2 totaltotal 59.5% 150 / 252 CC 50 SCAR 30.4
CC
50
Cov
62%
_findLib_dynaloader
64.2
find_library
27.9
_get_soname
23.7
CRAP 20.9
« blib/lib/Affix/Platform/Windows.pm ↑ Summary blib/lib/Affix/Platform/Haiku.pm »
1 58 package Affix::Platform::Unix v1.3.0 {
2 58 ▶ use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 58 ▶ use Path::Tiny qw[path];
Subroutine BEGIN: called CC=1 SCAR=0.0
4 58 ▶ use Config qw[%Config];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 58 ▶ use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 58 ▶ use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 58 our @EXPORT_OK = qw[find_library];
8 58 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
9 58 my $so = $Config{so};
10  
11 34 ▶ sub is_elf ($filename) {
Subroutine is_elf: called CC=3 SCAR=11.0
Pod undocumented
12 34 my $elf_header = "\x7fELF"; # ELF header in binary format
13 34 ▶ open( my $fh, '<:raw', $filename ) or return 0; # Open in binary mode
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $filename or return 0 29 5 34
14 29 ▶ sysread( $fh, my $header, 4 ) || return;
Branch: 1 branch
BranchTrueFalseTotal
sysread $fh, my $header, 4 or return 29 0 29
15 29 close($fh);
16 29 return $header eq $elf_header;
17 }
18  
19 # The loader can hand back a library of the wrong bitness (e.g. the 32-bit
20 # /usr/lib/libm.so on Solaris/Illumos to a 64-bit perl), which dlopen()
21 # rejects. Reject any candidate whose ELF class does not match perl's.
22 22 ▶ sub _is_usable_lib ($path) {
Subroutine _is_usable_lib: called CC=4 SCAR=14.8
23 22 ▶ return 0 unless is_elf($path);
Branch: 1 branch
BranchTrueFalseTotal
unless is_elf($path) 13 9 22
24 9 ▶ open( my $fh, '<:raw', $path ) or return 0; # EI_CLASS is at byte 4: 1=32-bit, 2=64-bit
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or return 0 9 0 9
25 0 ▶ sysread( $fh, my $header, 5 ) == 5 or do { close($fh); return 0 };
Branch: 1 branch
BranchTrueFalseTotal
sysread($fh, my $header, 5) == 5 or do { close $fh; return 0 } 9 0 9
26 9 close($fh);
27 9 my $class64 = substr( $header, 4, 1 ) eq "\x02";
28 9 return $class64 == ( $Config{ptrsize} == 8 );
29 }
30  
31 13 ▶ sub _findLib_ldconfig ($name) {
Subroutine _findLib_ldconfig: called CC=5 SCAR=16.3
32  
33 # Get the first part of the architecture name (e.g., "x86_64" from "x86_64-linux-gnu")
34 13 my ($arch_part) = split /-/, $Config{archname};
35 my $architecture = {
36 'x86_64' => 'x86_64',
37 'amd64' => 'x86_64', # A common alias
38 'aarch64' => 'ARM64',
39 'arm64' => 'ARM64',
40 'ppc64' => 'PPC64',
41 'sparc64' => 'SPARC64',
42 'ia64' => 'Itanium',
43 'riscv64' => 'RISCV',
44 'riscv' => 'RISCV'
45 13 }->{$arch_part};
46  
47 # Not a glibc box (Solaris/Illumos, *BSD, Haiku, ...): this probe is
48 # meaningless, so bow out gracefully and let the gcc/ld fallbacks try.
49 13 ▶ return unless defined $architecture;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $architecture 0 13 13
50  
51 # Use the portable $Config{longsize} which gives the size of a C long in bytes.
52 13 ▶ my $lookup_key = $architecture . ( $Config{longsize} == 8 ? '-64' : '-32' );
Branch: 1 branch
BranchTrueFalseTotal
$Affix::Platform::Unix::Config{'longsize'} == 8 ? : 13 0 13
53 my $machine = {
54 'x86_64-64' => 'libc6,x86-64',
55 'PPC64-64' => 'libc6,64bit',
56 'SPARC64-64' => 'libc6,64bit',
57 'Itanium-64' => 'libc6,IA-64',
58 'ARM64-64' => 'libc6,AArch64',
59 'RISCV-64' => 'libc6,rv64gc'
60 13 }->{$lookup_key};
61  
62 # If this specific architecture/bitness combination isn't in our list, return nothing.
63 13 ▶ $machine // return;
Condition50%1/2
$machine // (return)
l!l
130
MC/DC0%0/1
$machine // (return)
$machine
Truth table50%1/2
A
A= $machine
Aresult
11
00
64  
65 # XXX assuming GLIBC's ldconfig (with option -p)
66 9 grep { is_elf($_) } map {
67 13 /^(?:lib)?\Q$name\E(?:\-\S+)?\.\s*.*\(\Q$machine\E.*\)\s+=>\s+(.+)$/;
68 1781 ▶ defined $1 ? path($1)->realpath : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 9 1772 1781
69 } split /\R\s*/, `export LC_ALL 'C'; export LANG 'C'; /sbin/ldconfig -p 2>&1`;
70 }
71  
72 13 ▶ sub _findLib_dynaloader($name) {
Subroutine _findLib_dynaloader: called CC=9 SCAR=41.6
73  
74 # The bare '-l' guess can come back empty on Solaris/Illumos: dl_findfile's
75 # dirlist rarely holds the 64-bit subdir where the real libs live, and the
76 # unversioned libc/libm there are symlinks. Probe the explicit dirs instead.
77 13 ▶ if ( $^O =~ /(?:solaris|sunos)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /(?:solaris|sunos)/iu) 0 13 13
78 0 ▶ my $arch_dir = ( $Config{archname} =~ /^(?:sparc|sun4)/i ) ? 'sparcv9' : '64';
Branch: 1 branch
BranchTrueFalseTotal
$Affix::Platform::Unix::Config{'archname'} =~ /^(?:sparc|sun4)/iu ? : 0 0 0
79 0 my @dirs;
80 0 ▶ for my $base ( split /[ \t]+/, $Config{libpth} // '' ) {
Condition0%0/2
$Affix::Platform::Unix::Config{"libpth"} // ""
l!l
00
MC/DC0%0/1
$Affix::Platform::Unix::Config{"libpth"} // ""
$Affix::Platform::Unix::Config{"libpth"}
Truth table0%0/2
A
A= $Affix::Platform::Unix::Config{"libpth"}
Aresult
11
00
81 0 ▶ next unless $base;
Branch: 1 branch
BranchTrueFalseTotal
unless $base 0 0 0
82 0 ▶ push @dirs, "$base/$arch_dir" if -d "$base/$arch_dir";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$base/$arch_dir" 0 0 0
83 0 push @dirs, $base;
84 }
85 0 ▶ push @dirs, "/usr/lib/$arch_dir" if -d "/usr/lib/$arch_dir";
Branch: 1 branch
BranchTrueFalseTotal
if -d "/usr/lib/$arch_dir" 0 0 0
86 0 my @found = DynaLoader::dl_findfile( @dirs, "lib$name.so" );
87 0 ▶ return @found if @found;
Branch: 1 branch
BranchTrueFalseTotal
if @found 0 0 0
88 }
89 13 DynaLoader::dl_findfile( '-l' . $name );
90 }
91  
92 10 ▶ sub _findLib_ld($name) {
Subroutine _findLib_ld: called CC=2 SCAR=7.0
93 10 local $ENV{LC_ALL} = 'C';
94 10 local $ENV{LANG} = 'C';
95 10 ▶ open( my $fh, '-|', 'ld', '-t', '-o', '/dev/null', "-l$name" ) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "ld", "-t", "-o", "/dev/null", "-l$name" or return 10 0 10
96 10 my $output = do { local $/; <$fh> };
97 10 close $fh;
98 10 return $output;
99 }
100  
101 11 ▶ sub _findLib_gcc($name) {
Subroutine _findLib_gcc: called CC=6 SCAR=20.2
102 11 $name =~ s[^lib][];
103 11 CORE::state $compiler;
104 1 ▶ $compiler //= sub {
Subroutine __ANON__: called CC=7 SCAR=22.1
105 1 require ExtUtils::MakeMaker;
106 1 for my $cc (qw[gcc cc]) {
107 1 ▶ next unless MM->maybe_command($cc) || grep { -x "$_/$cc" } split /:/, ( $ENV{PATH} // '' );
Branch: 1 branch
BranchTrueFalseTotal
unless "MM"->maybe_command($cc) or grep {-x "$_/$cc";} split(/:/u, $ENV{"PATH"} // "", 0) 0 1 1
Condition40%2/5
"MM"->maybe_command($cc) or grep {-x "$_/$cc";} split(/:/u, $ENV{"PATH"} // "", 0)
l!l&&r!l&&!r
010
$ENV{"PATH"} // ""
l!l
10
MC/DC0%0/2
"MM"->maybe_command($cc) or grep {-x "$_/$cc";} split(/:/u, $ENV{"PATH"} // "", 0)
"MM"->maybe_command($cc) grep {-x "$_/$cc";} split(/:/u, $ENV{"PATH"} // "", 0)
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table33%1/3
A or B
A= "MM"->maybe_command($cc)B= grep {-x "$_/$cc";} split(/:/u, $ENV{"PATH"} // "", 0)
ABresult
1X1
011
000
Truth table50%1/2
A
A= $ENV{"PATH"}
Aresult
11
00
108 1 ▶ if ( open( my $fh, '-|', $cc, '--version' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "-|", $cc, "--version") 1 0 1
109 1 my $line = <$fh>;
110 1 close $fh;
111 1 ▶ return $cc if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 1 0 1
112 }
113 }
114 0 return 'gcc';
115 }
116 11 ▶ ->();
Condition66%2/3
$compiler //= sub { require ExtUtils::MakeMaker; foreach my $cc ("gcc", "cc") { next unless "MM"->maybe_command($cc) or grep {-x "$_/$cc";} split(/:/u, $ENV{"PATH"} // "", 0); if (open my $fh, "-|", $cc, "--version") { my $line = readline $fh; close $fh; return $cc if defined $line; } ; } ; return "gcc"; } ->()
l!l&&r!l&&!r
1010
MC/DC0%0/2
$compiler //= sub { require ExtUtils::MakeMaker; foreach my $cc ("gcc", "cc") { next unless "MM"->maybe_command($cc) or grep {-x "$_/$cc";} split(/:/u, $ENV{"PATH"} // "", 0); if (open my $fh, "-|", $cc, "--version") { my $line = readline $fh; close $fh; return $cc if defined $line; } ; } ; return "gcc"; } ->()
$compiler sub { require ExtUtils::MakeMaker; foreach my $cc ("gcc", "cc") { next unless "MM"->maybe_command($cc) or grep {-x "$_/$cc";} split(/:/u, $ENV{"PATH"} // "", 0); if (open my $fh, "-|", $cc, "--version") { my $line = readline $fh; close $fh; return $cc if defined $line; } ; } ; return "gcc"; } ->()
Truth table66%2/3
A //= B
A= $compilerB= sub { require ExtUtils::MakeMaker; foreach my $cc ("gcc", "cc") { next unless "MM"->maybe_command($cc) or grep {-x "$_/$cc";} split(/:/u, $ENV{"PATH"} // "", 0); if (open my $fh, "-|", $cc, "--version") { my $line = readline $fh; close $fh; return $cc if defined $line; } ; } ; return "gcc"; } ->()
ABresult
1X1
011
000
117 11 my $trace;
118 {
119 58 ▶ use File::Temp qw[tempfile];
Subroutine BEGIN: called CC=1 SCAR=0.0
120 11 my ( undef, $temp_file ) = tempfile();
121 11 ▶ if ( open( my $fh, '-|', $compiler, '-shared', '-Wl,-t', '-o', $temp_file, "-l$name" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "-|", $compiler, "-shared", "-Wl,-t", "-o", $temp_file, "-l$name") 11 0 11
122 11 $trace = do { local $/; <$fh> };
123 11 close $fh;
124 }
125 11 ▶ if ( !defined $trace || !length $trace ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $trace and length $trace) 0 11 11
Condition33%1/3
defined $trace and length $trace
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $trace and length $trace
defined $trace length $trace
Truth table33%1/3
A and B
A= defined $traceB= length $trace
ABresult
0X0
100
111
126 0 ▶ if ( open( my $fh2, '-|', $compiler, '--print-file-name', "lib$name.$so" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh2, "-|", $compiler, "--print-file-name", "lib$name.$so") 0 0 0
127 0 $trace = do { local $/; <$fh2> };
128 0 close $fh2;
129 }
130 }
131 };
132 11 grep {/^.*?\/lib\Q$name\E\.[^\s]+$/} split /\n/, $trace;
133 }
134  
135 13 ▶ sub find_library ( $name, $version //= '' ) { # TODO: actually feed version to diff methods
Subroutine find_library: called CC=16 SCAR=33.3
Pod undocumented
136 13 ▶ if ( -f $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $name) 0 13 13
137 0 ▶ $name = readlink $name if -l $name; # Handle symbolic links
Branch: 1 branch
BranchTrueFalseTotal
if -l $name 0 0 0
138 0 ▶ return $name if is_elf($name);
Branch: 1 branch
BranchTrueFalseTotal
if is_elf($name) 0 0 0
139 }
140 13 CORE::state $cache;
141 13 ▶ unless ( defined $cache->{$name}{$version} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cache->{$name}{$version}) 13 0 13
142 13 my @ret = grep { _is_usable_lib($_) } _findLib_dynaloader($name);
143 13 ▶ @ret = grep { _is_usable_lib($_) } _findLib_ldconfig($name) unless @ret;
Branch: 1 branch
BranchTrueFalseTotal
unless @ret 13 0 13
144 0 ▶ @ret = grep { _is_usable_lib($_) } _findLib_gcc($name) unless @ret;
Branch: 1 branch
BranchTrueFalseTotal
unless @ret 4 9 13
145 13 ▶ @ret = grep { _is_usable_lib($_) } _findLib_ld($name) unless @ret;
Branch: 1 branch
BranchTrueFalseTotal
unless @ret 4 9 13
146 13 ▶ return unless @ret;
Branch: 1 branch
BranchTrueFalseTotal
unless @ret 4 9 13
147 9 for my $lib ( map { path($_)->realpath } @ret ) {
148 9 ▶ next unless $lib =~ /^.*?\/lib\Q$name\E.*\.\Q$so\E(?:\.([\d\.\-]+))?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $lib =~ m[^.*?/lib\Q$name\E.*\.\Q$so\E(?:\.([\d\.\-]+))?$]u 0 9 9
149 9 ▶ $version = $1 if defined $1 && $version eq '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 and $version eq "" 9 0 9
Condition33%1/3
defined $1 and $version eq ""
!ll&&!rl&&r
009
MC/DC0%0/2
defined $1 and $version eq ""
defined $1 $version eq ""
Truth table33%1/3
A and B
A= defined $1B= $version eq ""
ABresult
0X0
100
111
150 9 ▶ $cache->{$name}{$version} //= $lib;
Condition33%1/3
$cache->{$name}{$version} //= $lib
l!l&&r!l&&!r
090
MC/DC0%0/2
$cache->{$name}{$version} //= $lib
$cache->{$name}{$version} $lib
Truth table33%1/3
A //= B
A= $cache->{$name}{$version}B= $lib
ABresult
1X1
011
000
151 }
152 }
153 9 ▶ $cache->{$name}{$version} // ();
Condition33%1/3
$cache->{$name}{$version} // ()
l!l&&r!l&&!r
900
MC/DC0%0/2
$cache->{$name}{$version} // ()
$cache->{$name}{$version} ()
Truth table33%1/3
A // B
A= $cache->{$name}{$version}B= ()
ABresult
1X1
011
000
154 }
155  
156 8 ▶ sub _get_soname ($file) { # assuming GNU binutils / ELF
Subroutine _get_soname: called CC=6 SCAR=31.7
157 8 ▶ return undef unless $file && -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file and -f $file 8 0 8
Condition66%2/3
$file and -f $file
!ll&&!rl&&r
260
MC/DC0%0/2
$file and -f $file
$file -f $file
Truth table66%2/3
A and B
A= $fileB= -f $file
ABresult
0X0
100
111
158 0 ▶ open( my $fh, '-|', 'objdump', '-p', '-j', '.dynamic', $file ) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "objdump", "-p", "-j", ".dynamic", $file or return 0 0 0
159 0 my $dump = do { local $/; <$fh> };
160 0 close $fh;
161 0 ▶ return unless defined $dump;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dump 0 0 0
162 0 ▶ $dump =~ /\sSONAME\s+([^\s]+)/ ? $1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$dump =~ /\sSONAME\s+([^\s]+)/u ? : 0 0 0
163 }
164 }
165 58 1;
« blib/lib/Affix/Platform/Windows.pm ↑ Summary blib/lib/Affix/Platform/Haiku.pm »