You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.4 KiB

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