checklibs.pl script to list dynamic library dependencies
Developer — 6 Nov 2007 06:11 — 856 days ago

Note: There’s an updated version of this script, see this later article.

This is a script I get to use a lot these days and other Mac OS X developers might find it useful.

It uses otool -L to determine and print all dynamic library dependencies of a given executable or library file recursively and you use it like this:

$ checklibs.pl /bin/ls 

/bin/ls:
	/usr/lib/libgcc_s.1.dylib
	/usr/lib/libncurses.5.4.dylib
	/usr/lib/libSystem.B.dylib

/usr/lib/libgcc_s.1.dylib:
	/usr/lib/libSystem.B.dylib

/usr/lib/libncurses.5.4.dylib:
	/usr/lib/libgcc_s.1.dylib
	/usr/lib/libSystem.B.dylib

/usr/lib/libSystem.B.dylib:
	/usr/lib/system/libmathCommon.A.dylib

Here’s the script:

#!/usr/bin/perl
#
# Written by Marc Liyanage <http://www.entropy.ch>
#

use strict;
use warnings;


my ($file) = @ARGV;
die $! unless (-f $file);

my $libs = {};
check_libs(file => $file, libs => $libs);

print
    map {("\n$_:\n", map {"\t$_\n"} sort {lc($a) cmp lc($b)} @{$libs->{$_}})}
    sort {lc($a) cmp lc($b)}
    grep {@{$libs->{$_}}}
    keys(%$libs);

sub check_libs {
    my (%args) = @_;
    my $libs = $args{libs};
    my @file_libs = grep {$_ ne $args{file}} grep {$_} map {/^\s+(\S+)/} qx(otool -L "$args{file}");
    $libs->{$args{file}} = \@file_libs;
    foreach my $lib (grep {!$libs->{$_}} @file_libs) {
        unless (-f $lib) {
            $libs->{$lib} = ['(missing)'];
            next;
        }
        check_libs(%args, file => $lib);        
    }
}
Comments
Posted by Josh on 11 Nov 2007 06:26

Thanks, that came in handy today! yay Google + blogs :D

Powered By blojsom