use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
use tools;
our $VERSION = "0.005";
my $target_os;
my $target_arch;
sub parse_error($\@$) {
my ( $tool, $bulk, $n ) = @_;
my @bulk;
for ( my $i = 0; $i < @$bulk; ++ $i ) {
push( @bulk, ( $i == $n ? ">>> " : " " ) . $bulk->[ $i ] );
};
runtime_error( "Fail to parse $tool output:", @bulk, "(eof)" );
};
sub get_deps_ldd($) {
my $lib = shift ( @_ );
my $tool = "ldd";
my @bulk;
my @deps;
execute( [ $tool, $lib ], -stdout => \@bulk );
debug( @bulk, "(eof)" );
foreach my $i ( 0 .. @bulk - 1 ) {
my $line = $bulk[ $i ];
if ( $line !~ m{^\s*(?:([_a-z0-9.+-/]*)\s+=>\s+)?([_a-z0-9.+-/]*)\s+\(0x[0-9a-z]*\)$}i ) {
parse_error( $tool, @bulk, $i );
};
my $dep = ( defined( $1 ) ? $1 : $2 );
push( @deps, $dep );
};
return @deps;
};
sub get_deps_readelf($) {
my $file = shift ( @_ );
my $tool;
my @bulk;
my @deps;
if($target_arch eq "mic") {
$tool = "x86_64-k1om-linux-readelf";
} else {
$tool = "readelf";
}
$ENV{ LANG } = "C";
execute( [ $tool, "-d", $file ], -stdout => \@bulk );
debug( @bulk, "(eof)" );
my $i = 0;
( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} )
or parse_error( $tool, @bulk, $i );
++ $i;
if ( $i == @bulk - 1 and $bulk[ $i ] =~ m{^There is no dynamic section in this file\.\s*$} ) {
return @deps;
};
( $i < @bulk and $bulk[ $i ] =~ m{^Dynamic (?:segment|section) at offset 0x[0-9a-f]+ contains \d+ entries:\s*$} )
or parse_error( $tool, @bulk, $i );
++ $i;
( $i < @bulk and $bulk[ $i ] =~ m{^\s*Tag\s+Type\s+Name/Value\s*$} )
or parse_error( $tool, @bulk, $i );
++ $i;
while ( $i < @bulk ) {
my $line = $bulk[ $i ];
if ( $line !~ m{^\s*0x[0-9a-f]+\s+\(?([_A-Z0-9]+)\)?\s+(.*)\s*$}i ) {
parse_error( $tool, @bulk, $i );
};
my ( $type, $value ) = ( $1, $2 );
if ( $type eq "NEEDED" ) {
if ( $value !~ m{\AShared library: \[(.*)\]\z} ) {
parse_error( $tool, @bulk, $i );
};
my $dep = $1;
push( @deps, $dep );
};
++ $i;
};
return @deps;
};
sub get_deps_otool($) {
my $file = shift ( @_ );
my $name = get_file( $file );
my $tool = "otool";
my @bulk;
my @deps;
if ( $target_arch eq "32e" ) {
my $path = which( "otool64" );
if ( defined ( $path ) ) {
$tool = "otool64";
};
};
execute( [ $tool, "-L", $file ], -stdout => \@bulk );
debug( @bulk, "(eof)" );
my $i = 0;
( $i < @bulk and $bulk[ $i ] =~ m{^\Q$file\E:$} )
or parse_error( $tool, @bulk, $i );
++ $i;
if ( $name =~ m{\.dylib\z} ) {
$name = '@rpath/' . $name;
( $i < @bulk and $bulk[ $i ] =~ m{^\s+\Q$name\E\s+\(compatibility version.*\)$} )
or parse_error( $tool, @bulk, $i );
++ $i;
};
while ( $i < @bulk ) {
my $line = $bulk[ $i ];
if ( $line !~ m/^\s*(.*)\s+\(compatibility version\s.*\)$/ ) {
parse_error( $tool, @bulk, $i );
};
my ( $dep ) = ( $1 );
push( @deps, $dep );
++ $i;
};
return @deps;
};
sub get_deps_link($) {
my ( $lib ) = @_;
my $tool = "link";
my @bulk;
my @deps;
my $ext = lc( get_ext( $lib ) );
if ( $ext !~ m{\A\.(?:lib|dll|exe)\z}i ) {
runtime_error( "Incorrect file is specified: `$lib'; only `lib', `dll' or `exe' file expected" );
};
execute(
[ $tool, "/dump", ( $ext eq ".lib" ? "/directives" : "/dependents" ), $lib ],
-stdout => \@bulk
);
debug( @bulk, "(eof)" );
my $i = 0;
( $i < @bulk and $bulk[ $i ] =~ m{^Microsoft \(R\) COFF\/PE Dumper Version.*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
( $i < @bulk and $bulk[ $i ] =~ m{^Copyright \(C\) Microsoft Corporation\..*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
( $i < @bulk and $bulk[ $i ] =~ m{^Dump of file\s\Q$lib\E$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
( $i < @bulk and $bulk[ $i ] =~ m{^File Type:\s(.*)$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
if ( $ext eq ".lib" ) {
my %deps;
while ( $i < @bulk ) {
my $line = $bulk[ $i ];
if ( 0 ) {
} elsif ( $line =~ m{^\s*[-/]defaultlib\:(.*)\s*$}i ) {
my $dep = $1;
# Normalize library name:
$dep = lc( $1 ); # Convert to lower case.
$dep =~ s{\A"(.*)"\z}{$1}; # Drop surrounding quotes (if any).
$dep =~ s{\.lib\z}{}; # Drop .lib suffix (if any).
$deps{ $dep } = 1;
} elsif ( $line =~ m{^\s*Linker Directives\s*$} ) {
} elsif ( $line =~ m{^\s*-+\s*$} ) {
} elsif ( $line =~ m{^\s*/alternatename\:.*$} ) {
} elsif ( $line =~ m{^\s*$} ) {
} elsif ( $line =~ m{^\s*/FAILIFMISMATCH\:.*$} ) {
# This directive is produced only by _MSC_VER=1600
} elsif ( $line =~ m{^\s*Summary\s*$} ) {
last;
} else {
parse_error( $tool, @bulk, $i );
}; # if
++ $i;
} # while
@deps = keys( %deps );
} else {
( $i < @bulk and $bulk[ $i ] =~ m{\s*Image has the following dependencies\:$} )
or parse_error( $tool, @bulk, $i );
++ $i;
while ( $i < @bulk ) {
my $line = $bulk[ $i ];
if ( 0 ) {
} elsif ( $line =~ m{^\s*$} ) {
# Ignore empty lines.
} elsif ( $line =~ m{^\s*(.*\.dll)$}i ) {
my $dep = lc( $1 );
push( @deps, $dep );
} elsif ( $line =~ m{^\s*Summary$} ) {
last;
} else {
parse_error( $tool, @bulk, $i );
}; # if
++ $i;
}; # while
}; # if
return @deps;
}; # sub get_deps_link
# --------------------------------------------------------------------------------------------------
# Main.
# --------------------------------------------------------------------------------------------------
# Parse command line.
my $expected;
my $bare;
Getopt::Long::Configure( "permute" );
get_options(
"os=s" => \$target_os,
"arch=s" => \$target_arch,
"bare" => \$bare,
"expected=s" => \$expected,
);
my @expected;
if ( defined( $expected ) ) {
if ( $expected ne "none" ) {
@expected = sort( split( ",", $expected ) );
if ( $target_os eq "win" ) {
@expected = map( lc( $_ ), @expected );
}; # if
}; # if
}; # if
if ( @ARGV < 1 ) {
cmdline_error( "Specify a library name to check for dependencies" );
}; # if
if ( @ARGV > 1 ) {
cmdline_error( "Too many arguments" );
}; # if
my $lib = shift( @ARGV );
if ( not -e $lib ){
runtime_error( "Specified file does not exist: \"$lib\"" );
}; # if
# Select appropriate get_deps implementation.
if ( 0 ) {
} elsif ( $target_os eq "lin" ) {
*get_deps = \*get_deps_readelf;
} elsif ( $target_os eq "mac" ) {
*get_deps = \*get_deps_otool;
} elsif ( $target_os eq "win" ) {
*get_deps = \*get_deps_link;
} else {
runtime_error( "OS \"$target_os\" not supported" );
}; # if
# Do the work.
my @deps = sort( get_deps( $lib ) );
if ( $bare ) {
print( map( "$_\n", @deps ) );
} else {
info( "Dependencies:", @deps ? map( " $_", @deps ) : "(none)" );
}; # if
if ( defined( $expected ) ) {
my %deps = map( ( $_ => 1 ), @deps );
foreach my $dep ( @expected ) {
delete( $deps{ $dep } );
}; # foreach
my @unexpected = sort( keys( %deps ) );
if ( @unexpected ) {
runtime_error( "Unexpected dependencies:", map( " $_", @unexpected ) );
}; # if
}; # if
exit( 0 );
__END__
=pod
=head1 NAME
B<check-depends.pl> -- Check dependencies for a specified library.
=head1 SYNOPSIS
B<check-depends.pl> I<OPTIONS>... I<library>
=head1 DESCRIPTION
C<check-depends.pl> finds direct dependencies for a specified library. List of actual dependencies
is sorted alphabetically and printed. If list of expected dependencies is specified, the scripts
checks the library has only allowed dependencies. In case of not expected dependencies. the script
issues error message and exits with non-zero code.
Linux* OS and OS X*: The script finds dependencies only for dynamic libraries. Windows* OS: The script
finds dependencies for either static or dynamic libraries.
The script uses external tools. On Linux* OS, it runs F<readelf>, on OS X* -- F<otool> (or F<otool64>),
on Windows* OS -- F<link>.
On Windows* OS dependencies are printed in lower case, case of expected dependencies ignored.
=head1 OPTIONS
=over
=item B<--bare>
Do not use fancy formatting; produce plain, bare output: just a list of libraries,
a library per line.
=item B<--expected=>I<list>
I<list> is comma-separated list of expected dependencies (or C<none>).
If C<--expected> option specified, C<check-depends.pl> checks the specified library
has only expected dependencies.
=item B<--os=>I<str>
Specify target OS (tool to use) manually.
Useful for cross-build, when host OS is not the same as target OS.
I<str> should be either C<lin>, C<mac>, or C<win>.
=back
=head2 Standard Options
=over
=item B<--help>
Print short help message and exit.
=item B<--doc>
=item B<--manual>
Print full documentation and exit.
=item B<--quiet>
Do not output informational messages.
=item B<--version>
Print version and exit.
=back
=head1 ARGUMENTS
=over
=item I<library>
A name of library to find or check dependencies.
=back
=head1 EXAMPLES
Just print library dependencies (Windows* OS):
> check-depends.pl exports/win_32/lib/libompmd.dll
check-depends.pl: (i) Dependencies:
check-depends.pl: (i) kernel32.dll
Print library dependencies, use bare output (Linux* OS):
$ check-depends.pl --bare exports/lin_32e/lib/libomp_db.so
libc.so.6
libdl.so.2
libpthread.so.0
Check the library does not have any dependencies (OS X*):
$ check-depends.pl --expected=none exports/mac_32/lib/libomp.dylib
check-depends.pl: (i) Dependencies:
check-depends.pl: (i) /usr/lib/libSystem.B.dylib
check-depends.pl: (x) Unexpected dependencies:
check-depends.pl: (x) /usr/lib/libSystem.B.dylib
$ echo $?
2
# end of file #