blob: 120c106224c6109294d8ca01d8cf645aac7a4a51 [file] [log] [blame]
Guido van Rossume7f39531998-08-10 13:12:22 +00001"""nm2def.py
2
3Helpers to extract symbols from Unix libs and auto-generate
4Windows definition files from them. Depends on nm(1). Tested
5on Linux and Solaris only (-p option to nm is for Solaris only).
6
7By Marc-Andre Lemburg, Aug 1998.
8
9Additional notes: the output of nm is supposed to look like this:
10
11acceler.o:
12000001fd T PyGrammar_AddAccelerators
13 U PyGrammar_FindDFA
1400000237 T PyGrammar_RemoveAccelerators
15 U _IO_stderr_
16 U exit
17 U fprintf
18 U free
19 U malloc
20 U printf
21
22grammar1.o:
2300000000 T PyGrammar_FindDFA
2400000034 T PyGrammar_LabelRepr
25 U _PyParser_TokenNames
26 U abort
27 U printf
28 U sprintf
29
30...
31
32Even if this isn't the default output of your nm, there is generally an
33option to produce this format (since it is the original v7 Unix format).
34
35"""
36import os,re,string,sys
37
38PYTHONLIB = 'libpython'+sys.version[:3]+'.a'
39PC_PYTHONLIB = 'Python'+sys.version[0]+sys.version[2]+'.dll'
40NM = 'nm -p -g %s' # For Linux, use "nm -g %s"
41
42def 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
59def 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
74DEF_TEMPLATE = """\
75EXPORTS
76%s
77"""
78
79# Special symbols that have to be included even though they don't
80# pass the filter
81SPECIALS = (
82 )
83
84def 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
92def 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
101if __name__ == '__main__':
102 main()