The following Python script can be used to collect and process nm output for all object files in the current directory:
#! /usr/bin/env python import collections import os import re import subprocess addr_re = r"(?P<address>[0-9a-f]{1,16})?" code_re = r"(?P<code>[az])" symbol_re = r"(?P<symbol>[a-z0-9_.$]+)" nm_line_re = re.compile(r"\s+".join([addr_re, code_re, symbol_re]) + "\s*$", re.I) requires = collections.defaultdict(set) provides = collections.defaultdict(set) def get_symbols(fname): lines = subprocess.check_output(["nm", "-g", fname]) for l in lines.splitlines(): m = nm_line_re.match(l) symbol = m.group('symbol') if m.group('code') == 'U': requires[fname].add(symbol) else: provides[symbol].add(fname) for dirpath, dirnames, filenames in os.walk("."): for f in filenames: if f.endswith(".o"): get_symbols(f) def pick(symbols):
The script looks for the current directory and all subdirectories for the .o files, calls nm for each found file and analyzes the result. Characters that are undefined in one .o file and defined in another are interpreted as a dependency between the two files. Symbols defined to nowhere (usually provided by external libraries) are ignored. Finally, the script prints a list of direct dependencies for all object files.
If a symbol is provided by several object files, this script arbitrarily takes a dependency on the object file with the smallest file name (and marks the selected file with * in the output). This behavior can be changed by changing the pick function.
The script works for me on Linux and MacOS, I have not tried any other operating systems, and the script has only been tested slightly.
jochen
source share