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

Annotation of openbsd/fill_chroot/find_depends, Revision 1.4

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

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