| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 | 2 |
| 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 import os | 7 import os |
| 8 import re | 8 import re |
| 9 import sys | 9 import sys |
| 10 | 10 |
| 11 _SHARED_RE = re.compile(r"Shared library: \[([^\]]+)\]") | 11 _SHARED_RE = re.compile(r"Shared library: \[([^\]]+)\]") |
| 12 _RPATH_RE = re.compile(r"Library rpath: \[([^\]]+)\]") | 12 _RPATH_RE = re.compile(r"Library rpath: \[([^\]]+)\]") |
| 13 | 13 |
| 14 | 14 |
| 15 class CheckDependencies(object): | 15 class CheckDependencies(object): |
| 16 """Check that dependencies for binaries can be found in the specified dir.""" | 16 """Check that dependencies for binaries can be found in the specified dir.""" |
| 17 | 17 |
| 18 def __init__(self, root, verbose=False): | 18 def __init__(self, root, verbose=False): |
| 19 """Initializer. | 19 """Initializer. |
| 20 | 20 |
| 21 Args: | 21 Args: |
| 22 root: The sysroot (e.g. "/") | 22 root: The sysroot (e.g. "/") |
| 23 verbose: Print helpful messages. | 23 verbose: Print helpful messages. |
| 24 """ | 24 """ |
| 25 | 25 |
| 26 self.root_ = root | 26 self._root = root |
| 27 self.libcache_ = set() | 27 self._libcache = set() |
| 28 self.verbose_ = verbose | 28 self._verbose = verbose |
| 29 | 29 |
| 30 # Insert some default directories into our library cache. | 30 # Insert some default directories into our library cache. |
| 31 # The /usr/lib/nss and /usr/lib/nspr directories are | 31 # The /usr/lib/nss and /usr/lib/nspr directories are |
| 32 # required for understanding old-style Netscape plugins. | 32 # required for understanding old-style Netscape plugins. |
| 33 self._ReadLibs([ | 33 self._ReadLibs([ |
| 34 "%s/lib" % root, | 34 "%s/lib" % root, |
| 35 "%s/usr/lib" % root, | 35 "%s/usr/lib" % root, |
| 36 "%s/usr/lib/nss" % root, | 36 "%s/usr/lib/nss" % root, |
| 37 "%s/usr/lib/nspr" % root | 37 "%s/usr/lib/nspr" % root |
| 38 ], self.libcache_) | 38 ], self._libcache) |
| 39 | 39 |
| 40 def _ReadLibs(self, paths, libcache): | 40 def _ReadLibs(self, paths, libcache): |
| 41 for path in paths: | 41 for path in paths: |
| 42 if os.path.exists(path): | 42 if os.path.exists(path): |
| 43 for lib in os.listdir(path): | 43 for lib in os.listdir(path): |
| 44 libcache.add(lib) | 44 libcache.add(lib) |
| 45 | 45 |
| 46 def _ReadDependencies(self, binary): | 46 def _ReadDependencies(self, binary): |
| 47 """Run readelf -d on BINARY, returning (deps, rpaths).""" | 47 """Run readelf -d on BINARY, returning (deps, rpaths).""" |
| 48 | 48 |
| 49 deps = set() | 49 deps = set() |
| 50 rpaths = set() | 50 rpaths = set() |
| 51 | 51 |
| 52 # Read list of dynamic libraries, ignoring error messages that occur | 52 # Read list of dynamic libraries, ignoring error messages that occur |
| 53 # when we look at files that aren't actually libraries | 53 # when we look at files that aren't actually libraries |
| 54 f = os.popen("readelf -d '%s' 2>/dev/null" % binary) | 54 f = os.popen("readelf -d '%s' 2>/dev/null" % binary) |
| 55 for line in f: | 55 for line in f: |
| 56 | 56 |
| 57 # Grab dependencies | 57 # Grab dependencies |
| 58 m = _SHARED_RE.search(line) | 58 m = _SHARED_RE.search(line) |
| 59 if m: | 59 if m: |
| 60 deps.add(m.group(1)) | 60 deps.add(m.group(1)) |
| 61 | 61 |
| 62 # Add RPATHs in our search path | 62 # Add RPATHs in our search path |
| 63 m = _RPATH_RE.search(line) | 63 m = _RPATH_RE.search(line) |
| 64 if m: | 64 if m: |
| 65 for path in m.group(1).split(":"): | 65 for path in m.group(1).split(":"): |
| 66 rpaths.add(os.path.join(self.root_, path[1:])) | 66 rpaths.add(os.path.join(self._root, path[1:])) |
| 67 f.close() | 67 f.close() |
| 68 | 68 |
| 69 return (deps, rpaths) | 69 return (deps, rpaths) |
| 70 | 70 |
| 71 def CheckDependencies(self, binary): | 71 def CheckDependencies(self, binary): |
| 72 """Check whether the libs for BINARY can be found in our sysroot.""" | 72 """Check whether the libs for BINARY can be found in our sysroot.""" |
| 73 | 73 |
| 74 good = True | 74 good = True |
| 75 | 75 |
| 76 deps, rpaths = self._ReadDependencies(binary) | 76 deps, rpaths = self._ReadDependencies(binary) |
| 77 | 77 |
| 78 if self.verbose_: | 78 if self._verbose: |
| 79 for lib in self.libcache_ & deps: | 79 for lib in self._libcache & deps: |
| 80 print "Found %s" % lib | 80 print "Found %s" % lib |
| 81 | 81 |
| 82 for lib in deps - self.libcache_: | 82 for lib in deps - self._libcache: |
| 83 if lib[0] != "/": | 83 if lib[0] != "/": |
| 84 for path in rpaths: | 84 for path in rpaths: |
| 85 if os.path.exists(os.path.join(path, lib)): | 85 if os.path.exists(os.path.join(path, lib)): |
| 86 if self.verbose_: | 86 if self._verbose: |
| 87 print "Found %s" % lib | 87 print "Found %s" % lib |
| 88 break | 88 break |
| 89 else: | 89 else: |
| 90 print >>sys.stderr, "Problem with %s: Can't find %s" % (binary, lib) | 90 print >>sys.stderr, "Problem with %s: Can't find %s" % (binary, lib) |
| 91 good = False | 91 good = False |
| 92 else: | 92 else: |
| 93 full_path = os.path.join(self.root_, lib[1:]) | 93 full_path = os.path.join(self._root, lib[1:]) |
| 94 if os.path.exists(full_path): | 94 if os.path.exists(full_path): |
| 95 if self.verbose_: print "Found %s" % lib | 95 if self._verbose: print "Found %s" % lib |
| 96 else: | 96 else: |
| 97 print >>sys.stderr, "Problem with %s: Can't find %s" % (binary, lib) | 97 print >>sys.stderr, "Problem with %s: Can't find %s" % (binary, lib) |
| 98 good = False | 98 good = False |
| 99 | 99 |
| 100 return good | 100 return good |
| 101 | 101 |
| 102 | 102 |
| 103 def main(): | 103 def main(): |
| 104 | |
| 105 if len(sys.argv) < 3: | 104 if len(sys.argv) < 3: |
| 106 print "Usage: %s [-v] sysroot binary [ binary ... ]" % sys.argv[0] | 105 print "Usage: %s [-v] sysroot binary [ binary ... ]" % sys.argv[0] |
| 107 sys.exit(1) | 106 sys.exit(1) |
| 108 | 107 |
| 109 verbose = False | 108 verbose = False |
| 110 if sys.argv[1] == "-v": | 109 if sys.argv[1] == "-v": |
| 111 verbose = True | 110 verbose = True |
| 112 sys.argv = sys.argv[0:1] + sys.argv[2:] | 111 sys.argv = sys.argv[0:1] + sys.argv[2:] |
| 113 | 112 |
| 114 checker = CheckDependencies(sys.argv[1], verbose) | 113 checker = CheckDependencies(sys.argv[1], verbose) |
| 115 errors = False | 114 errors = False |
| 116 for binary in sys.argv[2:]: | 115 for binary in sys.argv[2:]: |
| 117 if verbose: print "Checking %s" % binary | 116 if verbose: print "Checking %s" % binary |
| 118 if not checker.CheckDependencies(binary): | 117 if not checker.CheckDependencies(binary): |
| 119 errors = True | 118 errors = True |
| 120 | 119 |
| 121 if errors: | 120 if errors: |
| 122 sys.exit(1) | 121 sys.exit(1) |
| 123 else: | 122 else: |
| 124 sys.exit(0) | 123 sys.exit(0) |
| 125 | 124 |
| 126 if __name__ == "__main__": | 125 if __name__ == "__main__": |
| 127 main() | 126 main() |
| OLD | NEW |