blob: 48e312aa0105b56483fe1e07debebd46a493b977 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001#!/usr/bin/env perl
2
3#
4#//===----------------------------------------------------------------------===//
5#//
6#// The LLVM Compiler Infrastructure
7#//
8#// This file is dual licensed under the MIT and the University of Illinois Open
9#// Source Licenses. See LICENSE.txt for details.
10#//
11#//===----------------------------------------------------------------------===//
12#
13
14use strict;
15use warnings;
16
17use FindBin;
18use lib "$FindBin::Bin/lib";
19
20use tools;
Jim Cownie5e8470a2013-09-27 10:38:44 +000021
22our $VERSION = "0.005";
Jonathan Peyton4c91ad12016-01-26 19:44:31 +000023my $target_os;
24my $target_arch;
Jim Cownie5e8470a2013-09-27 10:38:44 +000025
26# --------------------------------------------------------------------------------------------------
27# Ouput parse error.
28# $tool -- Name of tool.
29# @bulk -- Output of the tool.
30# $n -- Number of line caused parse error.
31sub parse_error($\@$) {
32 my ( $tool, $bulk, $n ) = @_;
33 my @bulk;
34 for ( my $i = 0; $i < @$bulk; ++ $i ) {
35 push( @bulk, ( $i == $n ? ">>> " : " " ) . $bulk->[ $i ] );
36 }; # for $i
37 runtime_error( "Fail to parse $tool output:", @bulk, "(eof)" );
38}; # sub parse_error
39
40
41# --------------------------------------------------------------------------------------------------
42# Linux* OS version of get_deps() parses output of ldd:
43#
44# $ ldd libname.so
45# libc.so.6 => /lib64/libc.so.6 (0x00002b60fedd8000)
46# libdl.so.2 => /lib64/libdl.so.2 (0x00002b60ff12b000)
47# libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b60ff32f000)
48# /lib64/ld-linux-x86-64.so.2 (0x0000003879400000)
49#
50# Note: ldd printd all the dependencies, direct and indirect. (For example, if specified library
51# requires libdl.so, and libdl.so requires /lib/ld-linux.so, ldd prints both libdl.so and
52# /lib/ld-linux.so). If you do not want indirect dependencies, look at readelf tool.
53#
54sub get_deps_ldd($) {
55
56 my $lib = shift ( @_ );
57 my $tool = "ldd";
58 my @bulk;
59 my @deps;
60
61 execute( [ $tool, $lib ], -stdout => \@bulk );
62 debug( @bulk, "(eof)" );
63
64 foreach my $i ( 0 .. @bulk - 1 ) {
65 my $line = $bulk[ $i ];
66 if ( $line !~ m{^\s*(?:([_a-z0-9.+-/]*)\s+=>\s+)?([_a-z0-9.+-/]*)\s+\(0x[0-9a-z]*\)$}i ) {
67 parse_error( $tool, @bulk, $i );
68 }; # if
69 my $dep = ( defined( $1 ) ? $1 : $2 );
70 push( @deps, $dep );
71 }; # foreach $i
72
73 return @deps;
74
75}; # sub get_deps_ldd
76
77
78# --------------------------------------------------------------------------------------------------
79# Another Linux* OS version of get_deps() parses output of readelf:
80#
Jonathan Peytonf0efbb52015-06-01 02:41:44 +000081# $ readelf -d exports/lin_32e/lib/libomp.so
Jim Cownie5e8470a2013-09-27 10:38:44 +000082#
83# Dynamic segment at offset 0x87008 contains 24 entries:
84# Tag Type Name/Value
85# 0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
86# 0x0000000000000001 (NEEDED) Shared library: [libdl.so.2]
87# 0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0]
Jonathan Peytonf0efbb52015-06-01 02:41:44 +000088# 0x000000000000000e (SONAME) Library soname: [libomp.so]
Jim Cownie5e8470a2013-09-27 10:38:44 +000089# 0x000000000000000d (FINI) 0x51caa
90# 0x0000000000000004 (HASH) 0x158
91# 0x0000000000000005 (STRTAB) 0x9350
92# ...
93#
94# Note: In contrast to ldd, readlef shows only direct dependencies.
95#
96sub get_deps_readelf($) {
97
98 my $file = shift ( @_ );
Andrey Churbanovd315cea2015-01-16 12:54:51 +000099 my $tool;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000100 my @bulk;
101 my @deps;
102
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000103 if($target_arch eq "mic") {
104 $tool = "x86_64-k1om-linux-readelf";
105 } else {
106 $tool = "readelf";
107 }
108
Sylvestre Ledru6dabac82015-09-16 12:01:14 +0000109 # Force the readelf call to be in English. For example, when readelf
110 # is called on a french localization, it will find "Librairie partagees"
111 # instead of shared library
112 $ENV{ LANG } = "C";
113
Jim Cownie5e8470a2013-09-27 10:38:44 +0000114 execute( [ $tool, "-d", $file ], -stdout => \@bulk );
115 debug( @bulk, "(eof)" );
116
117 my $i = 0;
118 # Parse header.
119 ( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} )
120 or parse_error( $tool, @bulk, $i );
121 ++ $i;
122 if ( $i == @bulk - 1 and $bulk[ $i ] =~ m{^There is no dynamic section in this file\.\s*$} ) {
123 # This is not dynamic executable => no dependencies.
124 return @deps;
125 }; # if
126 ( $i < @bulk and $bulk[ $i ] =~ m{^Dynamic (?:segment|section) at offset 0x[0-9a-f]+ contains \d+ entries:\s*$} )
127 or parse_error( $tool, @bulk, $i );
128 ++ $i;
129 ( $i < @bulk and $bulk[ $i ] =~ m{^\s*Tag\s+Type\s+Name/Value\s*$} )
130 or parse_error( $tool, @bulk, $i );
131 ++ $i;
132 # Parse body.
133 while ( $i < @bulk ) {
134 my $line = $bulk[ $i ];
135 if ( $line !~ m{^\s*0x[0-9a-f]+\s+\(([_A-Z0-9]+)\)\s+(.*)\s*$}i ) {
136 parse_error( $tool, @bulk, $i );
137 }; # if
138 my ( $type, $value ) = ( $1, $2 );
139 if ( $type eq "NEEDED" ) {
140 if ( $value !~ m{\AShared library: \[(.*)\]\z} ) {
141 parse_error( $tool, @bulk, $i );
142 }; # if
143 my $dep = $1;
144 push( @deps, $dep );
145 }; # if
146 ++ $i;
147 }; # foreach $i
148
149 return @deps;
150
151}; # sub get_deps_readelf
152
153
154# --------------------------------------------------------------------------------------------------
155# OS X* version of get_deps() parses output of otool:
156#
157# $ otool -L libname.dylib
Jonathan Peytonf0efbb52015-06-01 02:41:44 +0000158# exports/mac_32/lib.thin/libomp.dylib:
159# libomp.dylib (compatibility version 5.0.0, current version 5.0.0)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000160# /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 88.1.3)
161#
162sub get_deps_otool($) {
163
164 my $file = shift ( @_ );
165 my $name = get_file( $file );
166 my $tool = "otool";
167 my @bulk;
168 my @deps;
169
170 if ( $target_arch eq "32e" ) {
171 # On older (Tiger) systems otool does not recognize 64-bit binaries, so try to locate
172 # otool64.
173 my $path = which( "otool64" );
174 if ( defined ( $path ) ) {
175 $tool = "otool64";
176 }; # if
177 }; # if
178
179 execute( [ $tool, "-L", $file ], -stdout => \@bulk );
180 debug( @bulk, "(eof)" );
181
182 my $i = 0;
183 # Parse the first one or two lines separately.
184 ( $i < @bulk and $bulk[ $i ] =~ m{^\Q$file\E:$} )
185 or parse_error( $tool, @bulk, $i );
186 ++ $i;
187 if ( $name =~ m{\.dylib\z} ) {
188 # In case of dynamic library otool print the library itself as a dependent library.
189 ( $i < @bulk and $bulk[ $i ] =~ m{^\s+\Q$name\E\s+\(compatibility version.*\)$} )
190 or parse_error( $tool, @bulk, $i );
191 ++ $i;
192 }; # if
193
194 # Then parse the rest.
195 while ( $i < @bulk ) {
196 my $line = $bulk[ $i ];
197 if ( $line !~ m/^\s*(.*)\s+\(compatibility version\s.*\)$/ ) {
198 parse_error( $tool, @bulk, $i );
199 }; # if
200 my ( $dep ) = ( $1 );
201 push( @deps, $dep );
202 ++ $i;
203 }; # while
204
205 return @deps;
206
207}; # sub get_deps_otool
208
209
210# --------------------------------------------------------------------------------------------------
211# Windows* OS version of get_deps() parses output of link:
212#
213# > link -dump -dependents libname.dll
214# Microsoft (R) COFF/PE Dumper Version 8.00.40310.39
215# Copyright (C) Microsoft Corporation. All rights reserved.
Jonathan Peytonf0efbb52015-06-01 02:41:44 +0000216# Dump of file S:\Projects.OMP\users\omalyshe\omp\libomp\exports\win_64\lib\libompmd.dll
Jim Cownie5e8470a2013-09-27 10:38:44 +0000217# File Type: DLL
218# Image has the following dependencies:
219# KERNEL32.dll
220# Summary
221# C000 .data
222# 6000 .pdata
223# 18000 .rdata
224# ...
225#
226# > link -dump -directives libname.lib
227# Microsoft (R) COFF/PE Dumper Version 8.00.40310.39
228# Copyright (C) Microsoft Corporation. All rights reserved.
229# Dump of file S:\Projects.OMP\users\omalyshe\omp\libomp\exports\win_32e\lib\libimp5mt.lib
230# File Type: LIBRARY
231# Linker Directives
232# -----------------
233# -defaultlib:"uuid.lib"
234# -defaultlib:"uuid.lib"
235# .....
236# Summary
237# 3250 .bss
238# 3FBC .data
239# 34 .data1
240# ....
241sub get_deps_link($) {
242
243 my ( $lib ) = @_;
244 my $tool = "link";
245 my @bulk;
246 my @deps;
247
248 my $ext = lc( get_ext( $lib ) );
249 if ( $ext !~ m{\A\.(?:lib|dll|exe)\z}i ) {
250 runtime_error( "Incorrect file is specified: `$lib'; only `lib', `dll' or `exe' file expected" );
251 }; # if
252
253 execute(
254 [ $tool, "/dump", ( $ext eq ".lib" ? "/directives" : "/dependents" ), $lib ],
255 -stdout => \@bulk
256 );
257
258 debug( @bulk, "(eof)" );
259
260 my $i = 0;
261 ( $i < @bulk and $bulk[ $i ] =~ m{^Microsoft \(R\) COFF\/PE Dumper Version.*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
262 ( $i < @bulk and $bulk[ $i ] =~ m{^Copyright \(C\) Microsoft Corporation\..*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
263 ( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
264 ( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
265 ( $i < @bulk and $bulk[ $i ] =~ m{^Dump of file\s\Q$lib\E$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
266 ( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
267 ( $i < @bulk and $bulk[ $i ] =~ m{^File Type:\s(.*)$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
268 ( $i < @bulk and $bulk[ $i ] =~ m{^\s*$} ) or parse_error( $tool, @bulk, $i ); ++ $i;
269
270 if ( $ext eq ".lib" ) {
271
272 my %deps;
273 while ( $i < @bulk ) {
274 my $line = $bulk[ $i ];
275 if ( 0 ) {
276 } elsif ( $line =~ m{^\s*[-/]defaultlib\:(.*)\s*$}i ) {
277 my $dep = $1;
278 # Normalize library name:
279 $dep = lc( $1 ); # Convert to lower case.
280 $dep =~ s{\A"(.*)"\z}{$1}; # Drop surrounding quotes (if any).
281 $dep =~ s{\.lib\z}{}; # Drop .lib suffix (if any).
282 $deps{ $dep } = 1;
283 } elsif ( $line =~ m{^\s*Linker Directives\s*$} ) {
284 } elsif ( $line =~ m{^\s*-+\s*$} ) {
285 } elsif ( $line =~ m{^\s*/alternatename\:.*$} ) {
286 } elsif ( $line =~ m{^\s*$} ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000287 } elsif ( $line =~ m{^\s*/FAILIFMISMATCH\:.*$} ) {
288 # This directive is produced only by _MSC_VER=1600
Jim Cownie5e8470a2013-09-27 10:38:44 +0000289 } elsif ( $line =~ m{^\s*Summary\s*$} ) {
290 last;
291 } else {
292 parse_error( $tool, @bulk, $i );
293 }; # if
294 ++ $i;
295 } # while
296 @deps = keys( %deps );
297
298 } else {
299
300 ( $i < @bulk and $bulk[ $i ] =~ m{\s*Image has the following dependencies\:$} )
301 or parse_error( $tool, @bulk, $i );
302 ++ $i;
303 while ( $i < @bulk ) {
304 my $line = $bulk[ $i ];
305 if ( 0 ) {
306 } elsif ( $line =~ m{^\s*$} ) {
307 # Ignore empty lines.
308 } elsif ( $line =~ m{^\s*(.*\.dll)$}i ) {
309 my $dep = lc( $1 );
310 push( @deps, $dep );
311 } elsif ( $line =~ m{^\s*Summary$} ) {
312 last;
313 } else {
314 parse_error( $tool, @bulk, $i );
315 }; # if
316 ++ $i;
317 }; # while
318
319 }; # if
320
321 return @deps;
322
323}; # sub get_deps_link
324
325
326# --------------------------------------------------------------------------------------------------
327# Main.
328# --------------------------------------------------------------------------------------------------
329
330# Parse command line.
331my $expected;
332my $bare;
333Getopt::Long::Configure( "permute" );
334get_options(
Jonathan Peyton4c91ad12016-01-26 19:44:31 +0000335 "os=s" => \$target_os,
336 "arch=s" => \$target_arch,
Jim Cownie5e8470a2013-09-27 10:38:44 +0000337 "bare" => \$bare,
338 "expected=s" => \$expected,
339);
340my @expected;
341if ( defined( $expected ) ) {
342 if ( $expected ne "none" ) {
343 @expected = sort( split( ",", $expected ) );
344 if ( $target_os eq "win" ) {
345 @expected = map( lc( $_ ), @expected );
346 }; # if
347 }; # if
348}; # if
349if ( @ARGV < 1 ) {
350 cmdline_error( "Specify a library name to check for dependencies" );
351}; # if
352if ( @ARGV > 1 ) {
353 cmdline_error( "Too many arguments" );
354}; # if
355my $lib = shift( @ARGV );
356if ( not -e $lib ){
357 runtime_error( "Specified file does not exist: \"$lib\"" );
358}; # if
359
360# Select appropriate get_deps implementation.
361if ( 0 ) {
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000362} elsif ( $target_os eq "lin" ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000363 *get_deps = \*get_deps_readelf;
364} elsif ( $target_os eq "mac" ) {
365 *get_deps = \*get_deps_otool;
366} elsif ( $target_os eq "win" ) {
367 *get_deps = \*get_deps_link;
368} else {
369 runtime_error( "OS \"$target_os\" not supported" );
370}; # if
371
372# Do the work.
373my @deps = sort( get_deps( $lib ) );
374if ( $bare ) {
375 print( map( "$_\n", @deps ) );
376} else {
377 info( "Dependencies:", @deps ? map( " $_", @deps ) : "(none)" );
378}; # if
379if ( defined( $expected ) ) {
380 my %deps = map( ( $_ => 1 ), @deps );
381 foreach my $dep ( @expected ) {
382 delete( $deps{ $dep } );
383 }; # foreach
384 my @unexpected = sort( keys( %deps ) );
385 if ( @unexpected ) {
386 runtime_error( "Unexpected dependencies:", map( " $_", @unexpected ) );
387 }; # if
388}; # if
389
390exit( 0 );
391
392__END__
393
394=pod
395
396=head1 NAME
397
398B<check-depends.pl> -- Check dependencies for a specified library.
399
400=head1 SYNOPSIS
401
402B<check-depends.pl> I<OPTIONS>... I<library>
403
404=head1 DESCRIPTION
405
406C<check-depends.pl> finds direct dependencies for a specified library. List of actual dependencies
407is sorted alphabetically and printed. If list of expected dependencies is specified, the scripts
408checks the library has only allowed dependencies. In case of not expected depndencies the script
409issues error message and exits with non-zero code.
410
411Linux* OS and OS X*: The script finds dependencies only for dymamic libraries. Windows* OS: The script
412finds dependencies for either static or dymamic libraries.
413
414The script uses external tools. On Linux* OS, it runs F<readelf>, on OS X* -- F<otool> (or F<otool64>),
415on Windows* OS -- F<link>.
416
417On Windows* OS dependencies are printed in lower case, case of expected dependencies ignored.
418
419=head1 OPTIONS
420
421=over
422
423=item B<--bare>
424
425Do not use fancy formatting; produce plain, bare output: just a list of libraries,
426a library per line.
427
428=item B<--expected=>I<list>
429
430I<list> is comma-separated list of expected dependencies (or C<none>).
431If C<--expected> option specified, C<check-depends.pl> checks the specified library
432has only expected dependencies.
433
434=item B<--os=>I<str>
435
436Specify target OS (tool to use) manually.
437Useful for cross-build, when host OS is not the same as target OS.
438I<str> should be either C<lin>, C<mac>, or C<win>.
439
440=back
441
442=head2 Standard Options
443
444=over
445
446=item B<--help>
447
448Print short help message and exit.
449
450=item B<--doc>
451
452=item B<--manual>
453
454Print full documentation and exit.
455
456=item B<--quiet>
457
458Do not output informational messages.
459
460=item B<--version>
461
462Print version and exit.
463
464=back
465
466=head1 ARGUMENTS
467
468=over
469
470=item I<library>
471
472A name of library to find or check dependencies.
473
474=back
475
476=head1 EXAMPLES
477
478Just print library dependencies (Windows* OS):
479
Jonathan Peytonf0efbb52015-06-01 02:41:44 +0000480 > check-depends.pl exports/win_32/lib/libompmd.dll
Jim Cownie5e8470a2013-09-27 10:38:44 +0000481 check-depends.pl: (i) Dependencies:
482 check-depends.pl: (i) kernel32.dll
483
484Print library dependencies, use bare output (Linux* OS):
485
486 $ check-depends.pl --bare exports/lin_32e/lib/libomp_db.so
487 libc.so.6
488 libdl.so.2
489 libpthread.so.0
490
491Check the library does not have any dependencies (OS X*):
492
Jonathan Peytonf0efbb52015-06-01 02:41:44 +0000493 $ check-depends.pl --expected=none exports/mac_32/lib/libomp.dylib
Jim Cownie5e8470a2013-09-27 10:38:44 +0000494 check-depends.pl: (i) Dependencies:
495 check-depends.pl: (i) /usr/lib/libSystem.B.dylib
496 check-depends.pl: (x) Unexpected dependencies:
497 check-depends.pl: (x) /usr/lib/libSystem.B.dylib
498 $ echo $?
499 2
500
501=cut
502
503# end of file #
504