#!/usr/bin/python

import os,sys,re,string,glob

if len(glob.glob("*/*.a"))<10:
    sys.stderr.write("not enough libraries; you probably need to build ocropus first")
    sys.exit(255)

sources = {}
nsymbols = 0

for line in os.popen("nm -o -C */*.a","r").readlines():
    line = line[:-1]
    fields = line.split(None,3)
    if len(fields)!=3: continue
    source,kind,symbol = fields
    source = re.sub(":[0-9a-zA-Z]*$","",source)
    if kind!="T": continue
    nsymbols += 1
    if [s for s in sources.get(symbol,[]) if s==source]!=[]: continue
    # print [source,symbol]
    sources[symbol]= sources.get(symbol,[]) + [source]

for symbol in sources:
    if len(sources[symbol])>1:
        print symbol,"is multiply defined:"
        for source in sources[symbol]: print "  ",source

print "checked",nsymbols,"symbols"
