File: [local] / openbsd / fill_chroot / find_depends (download)
Revision 1.3, Wed Dec 21 18:22:32 2005 UTC (18 years, 6 months ago) by andrew
Branch: MAIN
Changes since 1.2: +17 -5 lines
allow some options (specificly -q) prbly change that to -v when we get usage()
|
#!/usr/bin/perl
# $RedRiver: find_depends,v 1.2 2005/12/21 18:16:03 andrew Exp $
use strict;
use warnings;
# find /home/andrew/www/ -name *.so* | xargs find_depends \
# | sort -u | xargs -I {} cp {} /home/andrew/www{}
my @Files;
my %opts;
foreach (@ARGV) {
if (/^-(\w+)$/) {
$opts{$1} = 1;
} else {
push @Files, $_;
}
}
die unless @Files;
my %libs;
foreach my $file (@Files) {
my $l = find_libs($file);
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" unless $opts{q};
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;
my @libs;
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;
}