version 1.3, 2005/12/21 18:22:32 |
version 1.10, 2008/09/16 23:15:03 |
|
|
#!/usr/bin/perl |
#!/usr/bin/perl |
# $RedRiver: find_depends,v 1.2 2005/12/21 18:16:03 andrew Exp $ |
# $RedRiver: find_depends,v 1.9 2008/05/05 19:02:57 andrew Exp $ |
use strict; |
use strict; |
use warnings; |
use warnings; |
|
|
# find /home/andrew/www/ -name *.so* | xargs find_depends \ |
|
# | sort -u | xargs -I {} cp {} /home/andrew/www{} |
|
|
|
my @Files; |
|
my %opts; |
my %opts; |
|
my @Files; |
|
|
foreach (@ARGV) { |
foreach (@ARGV) { |
if (/^-(\w+)$/) { |
if (/^-+(\w+)$/) { |
$opts{$1} = 1; |
$opts{$1} = 1; |
} else { |
} else { |
push @Files, $_; |
push @Files, $_; |
} |
} |
} |
} |
|
|
die unless @Files; |
Help() if $opts{h} || $opts{help}; |
|
Usage() unless @Files; |
|
|
my %libs; |
my %libs; |
|
|
|
|
|
|
exit; |
exit; |
|
|
|
sub Usage |
|
{ |
|
print "Usage: $0 [-v] file [file2 [file3 [...]]]\n"; |
|
exit; |
|
} |
|
|
|
sub Help |
|
{ |
|
print <<EOL; |
|
Hopefully finds all libraries that are required by an executable |
|
or shared library. |
|
|
|
Usage: |
|
$0 [-v] file [file2 [file3 [...]]] |
|
|
|
Example: |
|
find /var/www/ -name *.so* | xargs find_depends | \\ |
|
sort -u | xargs -I {} cp {} /var/www{} |
|
EOL |
|
|
|
exit; |
|
} |
|
|
sub find_libs |
sub find_libs |
{ |
{ |
my $file = shift; |
my $file = shift; |
my $ld = shift || get_ldconfig(); |
my $ld = shift || get_ldconfig(); |
my $locs = shift || {}; |
my $locs = shift || {}; |
|
|
print STDERR "Finding libs for '$file'\n" unless $opts{q}; |
print STDERR "Finding libs for '$file'\n" if $opts{v}; |
|
|
my @libs = search_file($file); |
my @libs = search_file($file); |
foreach (@libs) { |
foreach (@libs) { |
my ($name, $maj, $min) = $_ =~ /^([^\.]+)\.so\.(\d+)\.(\d+)$/; |
my ($name, $maj, $min) = $_ =~ /lib([^\/]+)\.so\.(\d+)\.(\d+)$/; |
|
next if ! $name; |
my $spec = 'l' . $name . '.' . $maj . '.' . $min; |
my $spec = 'l' . $name . '.' . $maj . '.' . $min; |
|
print " Found spec '$spec'\n" if $opts{v}; |
|
|
if (exists $ld->{$spec}) { |
if (exists $ld->{$spec}) { |
next if exists $locs->{$spec}; |
next if exists $locs->{$spec}; |
|
|
$locs->{$spec} = $ld->{$spec}; |
$locs->{$spec} = $ld->{$spec}; |
|
$locs = find_libs($ld->{$spec}, $ld, $locs); |
$locs = find_libs($locs->{$spec}, $ld, $locs); |
|
|
|
} else { |
} else { |
warn "Couldn't find location for lib '$_'"; |
warn "Couldn't find location for lib '$_' (file '$file')"; |
} |
} |
} |
} |
|
|
|
|
{ |
{ |
my $file = shift; |
my $file = shift; |
my @libs; |
my @libs; |
|
|
open my $libs, '<', $file or die; |
open my $libs, '-|', '/usr/bin/ldd', $file or die "Couldn't open ldd '$file': $!"; |
local $/ = chr(0); |
|
while (<$libs>) { |
while (<$libs>) { |
if (m|^(/[^\w\/]+/)?lib(\S+)\.(\d+)\.(\d+)|) { |
chomp; |
my ($path, $name, $major, $minor) = ($1, $2, $3, $4); |
my ($spec) = (split(/\s+/, $_))[7]; |
my $spec="$name.$major.$minor"; |
next if ! $spec; |
if (defined $path && $path ne '/usr/local/lib') { |
next if $spec !~ m{^/}xms; |
$spec="$path/$spec"; |
push @libs, $spec; |
} |
|
push @libs, $spec; |
|
} |
|
} |
} |
close $libs; |
close $libs; |
|
|
|
|
my $ldconfig = '/sbin/ldconfig'; |
my $ldconfig = '/sbin/ldconfig'; |
my (%paths, %libs); |
my (%paths, %libs); |
|
|
open my $ld, '-|', $ldconfig, '-r' or die; |
open my $ld, '-|', $ldconfig, '-r' |
|
or die "Couldn't open pipe to ldconfig: $!"; |
while (<$ld>) { |
while (<$ld>) { |
chomp; |
chomp; |
if (/search directories:\s+(.*)/) { |
if (/search directories:\s+(.*)/) { |