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