version 1.2, 2005/12/21 18:16:03 |
version 1.13, 2008/09/30 21:54:01 |
|
|
#!/usr/bin/perl |
#!/bin/sh |
# $RedRiver: find_depends,v 1.1 2005/12/21 18:04:06 andrew Exp $ |
# $RedRiver$ |
use strict; |
|
use warnings; |
|
|
|
# find /home/andrew/www/ -name *.so* | xargs find_depends | sort -u | xargs -I {} cp {} /home/andrew/www{} |
find_depends() { |
|
local _file="$1" |
|
local _line |
|
|
die unless @ARGV; |
test -z "$_file" && continue |
|
|
my %libs; |
/usr/bin/ldd "$_file" | awk '$7 ~ /^\// { print $7 }' | { |
|
while read _line; do |
|
test -z "$_line" && continue |
|
echo $_line |
|
|
foreach my $file (@ARGV) { |
test X"$_file" == X"$_line" && continue |
my $l = find_libs($file); |
find_depends "$_line" |
|
done |
foreach (keys %{ $l }) { |
|
$libs{$_} = $l->{$_}; |
|
} |
|
} |
|
|
|
foreach (keys %libs) { |
|
print $libs{$_}, "\n"; |
|
} |
|
|
|
exit; |
|
|
|
sub find_libs |
|
{ |
|
my $file = shift; |
|
my $ld = shift || get_ldconfig(); |
|
my $locs = shift || {}; |
|
|
|
print STDERR "Finding libs for '$file'\n"; |
|
|
|
my @libs = search_file($file); |
|
foreach (@libs) { |
|
my ($name, $maj, $min) = $_ =~ /^([^\.]+)\.so\.(\d+)\.(\d+)$/; |
|
my $spec = 'l' . $name . '.' . $maj . '.' . $min; |
|
|
|
if (exists $ld->{$spec}) { |
|
next if exists $locs->{$spec}; |
|
|
|
$locs->{$spec} = $ld->{$spec}; |
|
|
|
$locs = find_libs($locs->{$spec}, $ld, $locs); |
|
|
|
} else { |
|
warn "Couldn't find location for lib '$_'"; |
|
} |
} |
} |
|
|
|
return $locs; |
|
} |
} |
|
|
sub search_file |
|
{ |
{ |
my $file = shift; |
for f in "$@"; do |
my @libs; |
find_depends "$f" |
|
done |
|
} | sort -u |
|
|
open my $libs, '<', $file or die; |
|
local $/ = chr(0); |
|
while (<$libs>) { |
|
if (m|^(/[^\w\/]+/)?lib(\S+)\.(\d+)\.(\d+)|) { |
|
my ($path, $name, $major, $minor) = ($1, $2, $3, $4); |
|
my $spec="$name.$major.$minor"; |
|
if (defined $path && $path ne '/usr/local/lib') { |
|
$spec="$path/$spec"; |
|
} |
|
push @libs, $spec; |
|
} |
|
} |
|
close $libs; |
|
|
|
return @libs; |
|
} |
|
|
|
sub get_ldconfig |
|
{ |
|
my $ldconfig = '/sbin/ldconfig'; |
|
my (%paths, %libs); |
|
|
|
open my $ld, '-|', $ldconfig, '-r' or die; |
|
while (<$ld>) { |
|
chomp; |
|
if (/search directories:\s+(.*)/) { |
|
#search directories: /usr/lib:/usr/local/lib |
|
my @p = split /:/, $1; |
|
@paths{@p} = 1; |
|
} elsif (/\d+:-(\S+)\s+=>\s+(\S+)/) { |
|
#0:-ldes.9.0 => /usr/lib/libdes.so.9.0 |
|
my $lib = $1; |
|
my $loc = $2; |
|
#my ($name, $maj, $min) = $lib =~ /l([^\.]+)\.(\d+)\.(\d+)/; |
|
#my $spec = 'lib' . $name . '.so.' . $maj . '.' . $min; |
|
$libs{$lib} = $loc; |
|
} else { |
|
#print $_, "\n"; |
|
} |
|
} |
|
close $ld; |
|
|
|
$libs{_paths} = [ keys %paths ]; |
|
return \%libs; |
|
} |
|