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

Annotation of openbsd/fill_chroot/find_depends, Revision 1.5

1.1       andrew      1: #!/usr/bin/perl
1.5     ! andrew      2: # $RedRiver: find_depends,v 1.4 2005/12/21 18:37:38 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) {
                     69:     my ($name, $maj, $min) = $_ =~ /^([^\.]+)\.so\.(\d+)\.(\d+)$/;
                     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 {
                     80:       warn "Couldn't find location for lib '$_'";
                     81:     }
                     82:   }
                     83:
                     84:   return $locs;
                     85: }
                     86:
                     87: sub search_file
                     88: {
                     89:   my $file = shift;
                     90:   my @libs;
                     91:
1.4       andrew     92:   open my $libs, '<', $file or die "Couldn't open lib '$file': $!";
1.1       andrew     93:   local $/ = chr(0);
                     94:   while (<$libs>) {
                     95:     if (m|^(/[^\w\/]+/)?lib(\S+)\.(\d+)\.(\d+)|) {
                     96:       my ($path, $name, $major, $minor) = ($1, $2, $3, $4);
                     97:       my $spec="$name.$major.$minor";
                     98:       if (defined $path && $path ne '/usr/local/lib') {
                     99:         $spec="$path/$spec";
                    100:       }
                    101:       push @libs, $spec;
                    102:     }
                    103:   }
                    104:   close $libs;
                    105:
                    106:   return @libs;
                    107: }
                    108:
                    109: sub get_ldconfig
                    110: {
                    111:   my $ldconfig = '/sbin/ldconfig';
                    112:   my (%paths, %libs);
                    113:
1.4       andrew    114:   open my $ld, '-|', $ldconfig, '-r'
                    115:     or die "Couldn't open pipe to ldconfig: $!";
1.1       andrew    116:   while (<$ld>) {
                    117:     chomp;
                    118:     if (/search directories:\s+(.*)/) {
                    119:         #search directories: /usr/lib:/usr/local/lib
                    120:       my @p = split /:/, $1;
                    121:       @paths{@p} = 1;
                    122:     } elsif (/\d+:-(\S+)\s+=>\s+(\S+)/) {
                    123:              #0:-ldes.9.0 => /usr/lib/libdes.so.9.0
                    124:       my $lib = $1;
                    125:       my $loc = $2;
                    126:       #my ($name, $maj, $min) = $lib =~ /l([^\.]+)\.(\d+)\.(\d+)/;
                    127:       #my $spec = 'lib' . $name . '.so.' . $maj . '.' . $min;
                    128:       $libs{$lib} = $loc;
                    129:     } else {
                    130:       #print $_, "\n";
                    131:     }
                    132:   }
                    133:   close $ld;
                    134:
                    135:   $libs{_paths} = [ keys %paths ];
                    136:   return \%libs;
                    137: }

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