3 from __future__
import print_function
9 addr_re =
r"(?P<address>[0-9a-f]{1,16})?"
10 code_re =
r"(?P<code>[a-zA-Z])"
11 symbol_re =
r"(?P<symbol>[a-zA-Z0-9_.$@]+)"
12 symbol_demunged_re =
r"(?P<symbol>[a-zA-Z0-9_.$@:&()<>{}\[\]|^!%,~*+-=# ]+)"
13 symbols_re_skip = re.compile(
"(@@)")
14 nm_line_re = re.compile(
r"\s+".
join([addr_re, code_re, symbol_re]) +
"\s*$",
16 ldd_line_re = re.compile(
r"\s+(.*) => (.*) \(0x")
18 requires = collections.defaultdict(set)
19 provides = collections.defaultdict(set)
20 dependencies = collections.defaultdict(set)
21 libraries = collections.defaultdict(set)
24 lines = subprocess.check_output([
"nm",
"-g", fname])
25 for l
in lines.splitlines():
26 m = nm_line_re.match(l)
28 symbol = m.group(
'symbol')
29 if m.group(
'code') ==
'U':
30 requires[os.path.basename(fname)].
add(symbol)
32 provides[symbol].
add(os.path.basename(fname))
35 lines = subprocess.check_output([
"ldd",fname])
36 for l
in lines.splitlines():
37 m = ldd_line_re.match(l)
40 libraries[os.path.basename(fname)].
add(os.path.basename(library.rstrip(
'\r\n')))
43 paths=os.environ[
'LD_LIBRARY_PATH'].
split(
':')
46 for dirpath, dirnames, filenames
in os.walk(p):
48 fpth=os.path.realpath(os.path.join(dirpath,f))
49 filetype = subprocess.check_output([
"file", fpth])
50 if filetype.find(
"dynamically linked") >= 0 :
54 for fname, symbols
in requires.items():
56 for library
in libraries[fname]:
58 if library
in provides[s] : deps.add(library)
59 dependencies[fname]=deps
60 print(fname +
' : primary dependencies : ' +
', '.
join(sorted(dependencies[fname]))+
'\n')
64 if s
not in provides
and not symbols_re_skip.search(s) : unmet.add(s)
65 for u
in sorted(unmet):
66 dm = subprocess.check_output([
"c++filt",u])
67 demangled.add(dm.rstrip(
'\r\n'))
68 if demangled :
print(fname +
': undefined : ' +
', '.
join(sorted(demangled)))
72 for key,values
in dependencies.items():
74 for val
in values: G.add_edge(key,val)
76 for node
in nx.nodes_iter(G):
77 s = nx.dfs_successors(G,node)
80 for key,vals
in s.items() :
81 if key != node : deps.add(key)
84 print(node +
': primary and secondary dependencies :' +
', '.
join(sorted(deps)))
89 for key,values
in dependencies.items():
90 H.add_node(os.path.basename(key))
91 for val
in values: H.add_edge(os.path.basename(key),os.path.basename(val))
92 for node
in nx.nodes_iter(H):
93 T = nx.dfs_tree(H,node)