|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import subprocess |
| 7 | +import re |
| 8 | + |
| 9 | +re_needso = re.compile(r'^.*\(NEEDED\)\s+Shared library: \[lib(.*)\.so\]\s*$') |
| 10 | + |
| 11 | +blacklist_libs = ( |
| 12 | + 'c', |
| 13 | + 'stdc++', |
| 14 | + 'dl', |
| 15 | + 'python2.7', |
| 16 | + 'sdl', |
| 17 | + 'sdl_image', |
| 18 | + 'sdl_ttf', |
| 19 | + 'z', |
| 20 | + 'm', |
| 21 | + 'GLESv2', |
| 22 | + 'jpeg', |
| 23 | + 'png', |
| 24 | + 'log', |
| 25 | +) |
| 26 | + |
| 27 | +found_libs = [] |
| 28 | +sofiles = [ ] |
| 29 | + |
| 30 | +for directory in sys.argv[2:]: |
| 31 | + |
| 32 | + for fn in os.listdir(directory): |
| 33 | + fn = os.path.join(directory, fn) |
| 34 | + |
| 35 | + if not fn.endswith(".libs"): |
| 36 | + continue |
| 37 | + |
| 38 | + dirfn = fn[:-1] + 'dirs' |
| 39 | + if not os.path.exists(dirfn): |
| 40 | + continue |
| 41 | + |
| 42 | + with open(fn) as f: |
| 43 | + needed_libs = [lib for lib in {ln.strip() for ln in f} if lib not in blacklist_libs and lib not in found_libs] |
| 44 | + |
| 45 | + while needed_libs: |
| 46 | + print('need libs:\n\t' + '\n\t'.join(needed_libs)) |
| 47 | + |
| 48 | + start_needed_libs = needed_libs[:] |
| 49 | + found_sofiles = [] |
| 50 | + |
| 51 | + with open(dirfn) as f: |
| 52 | + for libdir in f: |
| 53 | + if not needed_libs: |
| 54 | + break |
| 55 | + |
| 56 | + libdir = libdir.strip() |
| 57 | + print('scanning %s' % libdir) |
| 58 | + for lib in needed_libs[:]: |
| 59 | + if lib in found_libs: |
| 60 | + continue |
| 61 | + |
| 62 | + if lib.endswith('.a'): |
| 63 | + needed_libs.remove(lib) |
| 64 | + found_libs.append(lib) |
| 65 | + continue |
| 66 | + |
| 67 | + lib_a = 'lib' + lib + '.a' |
| 68 | + libpath_a = os.path.join(libdir, lib_a) |
| 69 | + lib_so = 'lib' + lib + '.so' |
| 70 | + libpath_so = os.path.join(libdir, lib_so) |
| 71 | + plain_so = lib + '.so' |
| 72 | + plainpath_so = os.path.join(libdir, plain_so) |
| 73 | + |
| 74 | + sopath = None |
| 75 | + if os.path.exists(libpath_so): |
| 76 | + sopath = libpath_so |
| 77 | + elif os.path.exists(plainpath_so): |
| 78 | + sopath = plainpath_so |
| 79 | + |
| 80 | + if sopath: |
| 81 | + print('found %s in %s' % (lib, libdir)) |
| 82 | + found_sofiles.append(sopath) |
| 83 | + needed_libs.remove(lib) |
| 84 | + found_libs.append(lib) |
| 85 | + continue |
| 86 | + |
| 87 | + if os.path.exists(libpath_a): |
| 88 | + print('found %s (static) in %s' % (lib, libdir)) |
| 89 | + needed_libs.remove(lib) |
| 90 | + found_libs.append(lib) |
| 91 | + continue |
| 92 | + |
| 93 | + for sofile in found_sofiles: |
| 94 | + print('scanning dependencies for %s' % sofile) |
| 95 | + out = subprocess.check_output([os.environ['READELF'], '-d', sofile]) |
| 96 | + for line in out.splitlines(): |
| 97 | + needso = re_needso.match(line) |
| 98 | + if needso: |
| 99 | + lib = needso.group(1) |
| 100 | + if lib not in needed_libs and lib not in found_libs and lib not in blacklist_libs: |
| 101 | + needed_libs.append(needso.group(1)) |
| 102 | + |
| 103 | + sofiles += found_sofiles |
| 104 | + |
| 105 | + if needed_libs == start_needed_libs: |
| 106 | + raise RuntimeError('Failed to located needed libraries!\n\t' + '\n\t'.join(needed_libs)) |
| 107 | + |
| 108 | +output = sys.argv[1] |
| 109 | + |
| 110 | +with open(output, 'w') as f: |
| 111 | + f.write('\n'.join(sofiles)) |
0 commit comments