Annotation of openbsd/fill_chroot/find_depends, Revision 1.7
1.1 andrew 1: #!/usr/bin/perl
1.7 ! andrew 2: # $RedRiver: find_depends,v 1.6 2007/05/16 19:55:42 andrew Exp $
1.1 andrew 3: use strict;
4: use warnings;
5:
1.4 andrew 6: my %opts;
1.3 andrew 7: my @Files;
8:
9: foreach (@ARGV) {
1.4 andrew 10: if (/^-+(\w+)$/) {
1.3 andrew 11: $opts{$1} = 1;
12: } else {
13: push @Files, $_;
14: }
15: }
16:
1.4 andrew 17: Help() if $opts{h} || $opts{help};
18: Usage() unless @Files;
1.1 andrew 19:
20: my %libs;
1.2 andrew 21:
1.3 andrew 22: foreach my $file (@Files) {
1.2 andrew 23: my $l = find_libs($file);
24:
25: foreach (keys %{ $l }) {
26: $libs{$_} = $l->{$_};
1.1 andrew 27: }
28: }
29:
30: foreach (keys %libs) {
31: print $libs{$_}, "\n";
32: }
33:
34: exit;
35:
1.4 andrew 36: sub Usage
37: {
38: print "Usage: $0 [-v] file [file2 [file3 [...]]]\n";
39: exit;
40: }
41:
42: sub Help
43: {
44: print <<EOL;
45: Hopefully finds all libraries that are required by an executable
46: or shared library.
47:
48: Usage:
49: $0 [-v] file [file2 [file3 [...]]]
50:
51: Example:
52: find /var/www/ -name *.so* | xargs find_depends | \\
53: sort -u | xargs -I {} cp {} /var/www{}
54: EOL
55:
56: exit;
57: }
58:
1.1 andrew 59: sub find_libs
60: {
61: my $file = shift;
62: my $ld = shift || get_ldconfig();
63: my $locs = shift || {};
64:
1.4 andrew 65: print STDERR "Finding libs for '$file'\n" if $opts{v};
1.1 andrew 66:
67: my @libs = search_file($file);
68: foreach (@libs) {
1.7 ! andrew 69: my ($name, $maj, $min) = $_ =~ /lib([^\/]+)\.so\.(\d+)\.(\d+)$/;
1.1 andrew 70: my $spec = 'l' . $name . '.' . $maj . '.' . $min;
71:
72: if (exists $ld->{$spec}) {
73: next if exists $locs->{$spec};
74:
75: $locs->{$spec} = $ld->{$spec};
76:
77: $locs = find_libs($locs->{$spec}, $ld, $locs);
78:
79: } else {
1.6 andrew 80: warn "Couldn't find location for lib '$_' (file '$file')";
1.1 andrew 81: }
82: }
83:
84: return $locs;
85: }
86:
87: sub search_file
88: {
89: my $file = shift;
90: my @libs;
1.7 ! andrew 91:
! 92: open my $libs, '-|', '/usr/bin/ldd', $file or die "Couldn't open ldd '$file': $!";
1.1 andrew 93: while (<$libs>) {
1.7 ! andrew 94: chomp;
! 95: my $spec = substr $_, 56;
! 96: next if $spec !~ m{^/}xms;
! 97: push @libs, $spec;
1.1 andrew 98: }
99: close $libs;
100:
101: return @libs;
102: }
103:
104: sub get_ldconfig
105: {
106: my $ldconfig = '/sbin/ldconfig';
107: my (%paths, %libs);
108:
1.4 andrew 109: open my $ld, '-|', $ldconfig, '-r'
110: or die "Couldn't open pipe to ldconfig: $!";
1.1 andrew 111: while (<$ld>) {
112: chomp;
113: if (/search directories:\s+(.*)/) {
114: #search directories: /usr/lib:/usr/local/lib
115: my @p = split /:/, $1;
116: @paths{@p} = 1;
117: } elsif (/\d+:-(\S+)\s+=>\s+(\S+)/) {
118: #0:-ldes.9.0 => /usr/lib/libdes.so.9.0
119: my $lib = $1;
120: my $loc = $2;
121: #my ($name, $maj, $min) = $lib =~ /l([^\.]+)\.(\d+)\.(\d+)/;
122: #my $spec = 'lib' . $name . '.so.' . $maj . '.' . $min;
123: $libs{$lib} = $loc;
124: } else {
125: #print $_, "\n";
126: }
127: }
128: close $ld;
129:
130: $libs{_paths} = [ keys %paths ];
131: return \%libs;
132: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>