|
| 1 | +"""nm2def.py |
| 2 | +
|
| 3 | +Helpers to extract symbols from Unix libs and auto-generate |
| 4 | +Windows definition files from them. Depends on nm(1). Tested |
| 5 | +on Linux and Solaris only (-p option to nm is for Solaris only). |
| 6 | +
|
| 7 | +By Marc-Andre Lemburg, Aug 1998. |
| 8 | +
|
| 9 | +Additional notes: the output of nm is supposed to look like this: |
| 10 | +
|
| 11 | +acceler.o: |
| 12 | +000001fd T PyGrammar_AddAccelerators |
| 13 | + U PyGrammar_FindDFA |
| 14 | +00000237 T PyGrammar_RemoveAccelerators |
| 15 | + U _IO_stderr_ |
| 16 | + U exit |
| 17 | + U fprintf |
| 18 | + U free |
| 19 | + U malloc |
| 20 | + U printf |
| 21 | +
|
| 22 | +grammar1.o: |
| 23 | +00000000 T PyGrammar_FindDFA |
| 24 | +00000034 T PyGrammar_LabelRepr |
| 25 | + U _PyParser_TokenNames |
| 26 | + U abort |
| 27 | + U printf |
| 28 | + U sprintf |
| 29 | +
|
| 30 | +... |
| 31 | +
|
| 32 | +Even if this isn't the default output of your nm, there is generally an |
| 33 | +option to produce this format (since it is the original v7 Unix format). |
| 34 | +
|
| 35 | +""" |
| 36 | +import os,re,string,sys |
| 37 | + |
| 38 | +PYTHONLIB = 'libpython'+sys.version[:3]+'.a' |
| 39 | +PC_PYTHONLIB = 'Python'+sys.version[0]+sys.version[2]+'.dll' |
| 40 | +NM = 'nm -p -g %s' # For Linux, use "nm -g %s" |
| 41 | + |
| 42 | +def symbols(lib=PYTHONLIB,types=('T','C','D')): |
| 43 | + |
| 44 | + lines = os.popen(NM % lib).readlines() |
| 45 | + lines = map(string.strip,lines) |
| 46 | + symbols = {} |
| 47 | + for line in lines: |
| 48 | + if len(line) == 0 or ':' in line: |
| 49 | + continue |
| 50 | + items = string.split(line) |
| 51 | + if len(items) != 3: |
| 52 | + continue |
| 53 | + address, type, name = items |
| 54 | + if type not in types: |
| 55 | + continue |
| 56 | + symbols[name] = address,type |
| 57 | + return symbols |
| 58 | + |
| 59 | +def export_list(symbols): |
| 60 | + |
| 61 | + data = [] |
| 62 | + code = [] |
| 63 | + for name,(addr,type) in symbols.items(): |
| 64 | + if type in ('C','D'): |
| 65 | + data.append('\t'+name) |
| 66 | + else: |
| 67 | + code.append('\t'+name) |
| 68 | + data.sort() |
| 69 | + data.append('') |
| 70 | + code.sort() |
| 71 | + return string.join(data,' DATA\n')+'\n'+string.join(code,'\n') |
| 72 | + |
| 73 | +# Definition file template |
| 74 | +DEF_TEMPLATE = """\ |
| 75 | +EXPORTS |
| 76 | +%s |
| 77 | +""" |
| 78 | + |
| 79 | +# Special symbols that have to be included even though they don't |
| 80 | +# pass the filter |
| 81 | +SPECIALS = ( |
| 82 | + ) |
| 83 | + |
| 84 | +def filter_Python(symbols,specials=SPECIALS): |
| 85 | + |
| 86 | + for name in symbols.keys(): |
| 87 | + if name[:2] == 'Py' or name[:3] == '_Py': |
| 88 | + pass |
| 89 | + elif name not in specials: |
| 90 | + del symbols[name] |
| 91 | + |
| 92 | +def main(): |
| 93 | + |
| 94 | + s = symbols(PYTHONLIB) |
| 95 | + filter_Python(s) |
| 96 | + exports = export_list(s) |
| 97 | + f = sys.stdout # open('PC/python_nt.def','w') |
| 98 | + f.write(DEF_TEMPLATE % (exports)) |
| 99 | + f.close() |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + main() |
0 commit comments