[BACK]Return to find_depends CVS log [TXT][DIR] Up to [local] / openbsd / fill_chroot

Annotation of openbsd/fill_chroot/find_depends, Revision 1.8

1.1       andrew      1: #!/usr/bin/perl
1.8     ! andrew      2: # $RedRiver: find_depends,v 1.7 2008/04/22 20:08:41 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.8     ! andrew     70:     next if ! $name;
1.1       andrew     71:     my $spec = 'l' . $name . '.' . $maj . '.' . $min;
                     72:
                     73:     if (exists $ld->{$spec}) {
                     74:       next if exists $locs->{$spec};
                     75:
                     76:       $locs->{$spec} = $ld->{$spec};
                     77:
                     78:       $locs = find_libs($locs->{$spec}, $ld, $locs);
                     79:
                     80:     } else {
1.6       andrew     81:       warn "Couldn't find location for lib '$_' (file '$file')";
1.1       andrew     82:     }
                     83:   }
                     84:
                     85:   return $locs;
                     86: }
                     87:
                     88: sub search_file
                     89: {
                     90:   my $file = shift;
                     91:   my @libs;
1.7       andrew     92:
                     93:   open my $libs, '-|', '/usr/bin/ldd', $file or die "Couldn't open ldd '$file': $!";
1.1       andrew     94:   while (<$libs>) {
1.8     ! andrew     95:     next if length($_) < 56;
1.7       andrew     96:     chomp;
                     97:     my $spec = substr $_, 56;
                     98:     next if $spec !~ m{^/}xms;
                     99:     push @libs, $spec;
1.1       andrew    100:   }
                    101:   close $libs;
                    102:
                    103:   return @libs;
                    104: }
                    105:
                    106: sub get_ldconfig
                    107: {
                    108:   my $ldconfig = '/sbin/ldconfig';
                    109:   my (%paths, %libs);
                    110:
1.4       andrew    111:   open my $ld, '-|', $ldconfig, '-r'
                    112:     or die "Couldn't open pipe to ldconfig: $!";
1.1       andrew    113:   while (<$ld>) {
                    114:     chomp;
                    115:     if (/search directories:\s+(.*)/) {
                    116:         #search directories: /usr/lib:/usr/local/lib
                    117:       my @p = split /:/, $1;
                    118:       @paths{@p} = 1;
                    119:     } elsif (/\d+:-(\S+)\s+=>\s+(\S+)/) {
                    120:              #0:-ldes.9.0 => /usr/lib/libdes.so.9.0
                    121:       my $lib = $1;
                    122:       my $loc = $2;
                    123:       #my ($name, $maj, $min) = $lib =~ /l([^\.]+)\.(\d+)\.(\d+)/;
                    124:       #my $spec = 'lib' . $name . '.so.' . $maj . '.' . $min;
                    125:       $libs{$lib} = $loc;
                    126:     } else {
                    127:       #print $_, "\n";
                    128:     }
                    129:   }
                    130:   close $ld;
                    131:
                    132:   $libs{_paths} = [ keys %paths ];
                    133:   return \%libs;
                    134: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>