makedeffile.py
Go to the documentation of this file.
1 # This script creates a Windows .def file containing all the functions
2 # and static class variables to be exported by a DLL. The symbols are
3 # extracted from the output of dumpbin.
4 #
5 # To use this script, first generate a normal .lib library file, with
6 # no special command line options. Then create a .def file with this
7 # script:
8 #
9 # makedeffile.py foo.lib foo 1.0 foo.def
10 #
11 # The command line arguments are the name of the .lib file, the
12 # library name, the library version, and the name of the .def file to
13 # output.
14 #
15 # Once you have a .def file, you can build a .dll, specifying the .def
16 # to the linker.
17 #
18 # An example of building a .dll from foo.cc:
19 #
20 # 1. Compile the source:
21 #
22 # cl -c -O2 -MD -GX -Fofoo.o -Tpfoo.cc
23 #
24 # 2. Build a static library (It probably won't work on its own due to
25 # the -MD switch to cl, but we just need it to get the symbols
26 # out):
27 #
28 # lib -out:foo_static.lib foo.o
29 #
30 # 3. Use this script to build a .def file:
31 #
32 # makedeffile.py foo_static.lib foo 1.0 foo.def
33 #
34 # 4. Build the .dll and .lib with the def file.
35 #
36 # link -out:foo.dll -dll -def:foo.def -implib:foo.lib foo.o
37 #
38 # If you are using this script so you can put omniORB stubs in a DLL,
39 # you also need to set some defines to correctly import symbols into
40 # the application. See chapter 12 of the omniORB manual for details.
41 
42 
43 # The way this script works is to process the output of dumpbin.
44 #
45 # The function symbols are extracted from the output using the
46 # following template:
47 # ... ........ SECT.. notype () External | ?..................
48 #
49 # The static class variable symbols are extracted using the following
50 # template:
51 # ... ........ SECT.. notype External | ?[^?]..............
52 #
53 # Default destructors generated by the compiler and the symbols inside
54 # an anonymous namespace are excluded.
55 #
56 # Class variable and function symbols start with two ?? and class
57 # static variable and static function symbols start with one ?.
58 
59 import re, sys, os, os.path, string
60 
61 def usage(argv):
62  sys.stderr.write("%s <lib file> <library name> <version> <def file>\n" %
63  os.path.basename(argv[0]))
64 
65 def main(argv):
66  try:
67  _, libfile, binname, version, deffile = argv
68  except ValueError:
69  usage(argv)
70  sys.exit(1)
71 
72  cmd = "DUMPBIN.EXE /SYMBOLS %s" % libfile
73  print cmd
74  dumped = os.popen(cmd)
75 
76  definitions = {}
77 
78  linere1 = re.compile(r"^[^ ]+ +[^ ]+ +SECT[^ ]+ +[^ ]+ +\(\) +External +\| +(\?[^ ]*)(.*)\n")
79  linere2 = re.compile(r"^[^ ]+ +[^ ]+ +SECT[^ ]+ +[^ ]+ +External +\| +(\?[^?][^ ]*)(.*)\n")
80 
81  exclude = re.compile(r"deleting destructor[^(]+\(unsigned int\)|anonymous namespace")
82 
83  while 1:
84  line = dumped.readline()
85  if line == "":
86  break
87 
88  match = linere1.search(line) or linere2.search(line)
89  if match:
90  symbol = match.group(1)
91  args = match.group(2)
92  if exclude.search(args):
93  continue
94 
95  definitions[symbol] = None
96 
97  symbols = definitions.keys()
98  symbols.sort()
99 
100  print "Output %d symbols." % len(symbols)
101 
102  out = open(deffile, "w")
103  if string.lower(binname[4:]) == ".exe":
104  out.write("NAME %s\n" % binname)
105  else:
106  out.write("LIBRARY %s\n" % binname)
107  out.write("VERSION %s\n" % version)
108  out.write("EXPORTS\n")
109 
110  for s in symbols:
111  out.write(s + "\n")
112 
113  out.close()
114 
115 
116 if __name__ == "__main__":
117  main(sys.argv)
def usage(argv)
Definition: makedeffile.py:61
def main(argv)
Definition: makedeffile.py:65


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:43