makedeffile.py
Go to the documentation of this file.
00001 # This script creates a Windows .def file containing all the functions
00002 # and static class variables to be exported by a DLL. The symbols are
00003 # extracted from the output of dumpbin.
00004 #
00005 # To use this script, first generate a normal .lib library file, with
00006 # no special command line options. Then create a .def file with this
00007 # script:
00008 #
00009 #   makedeffile.py foo.lib foo 1.0 foo.def
00010 #
00011 # The command line arguments are the name of the .lib file, the
00012 # library name, the library version, and the name of the .def file to
00013 # output.
00014 #
00015 # Once you have a .def file, you can build a .dll, specifying the .def
00016 # to the linker.
00017 #
00018 # An example of building a .dll from foo.cc:
00019 #
00020 # 1. Compile the source:
00021 #
00022 #      cl -c -O2 -MD -GX -Fofoo.o -Tpfoo.cc
00023 #
00024 # 2. Build a static library (It probably won't work on its own due to
00025 #    the -MD switch to cl, but we just need it to get the symbols
00026 #    out):
00027 #
00028 #      lib -out:foo_static.lib foo.o
00029 #
00030 # 3. Use this script to build a .def file:
00031 #
00032 #      makedeffile.py foo_static.lib foo 1.0 foo.def
00033 #
00034 # 4. Build the .dll and .lib with the def file.
00035 #
00036 #      link -out:foo.dll -dll -def:foo.def -implib:foo.lib foo.o
00037 #
00038 # If you are using this script so you can put omniORB stubs in a DLL,
00039 # you also need to set some defines to correctly import symbols into
00040 # the application. See chapter 12 of the omniORB manual for details.
00041 
00042 
00043 # The way this script works is to process the output of dumpbin.
00044 #
00045 # The function symbols are extracted from the output using the
00046 # following template:
00047 # ... ........ SECT..  notype ()     External      | ?..................
00048 #
00049 # The static class variable symbols are extracted using the following
00050 # template:
00051 # ... ........ SECT..  notype        External      | ?[^?]..............
00052 #
00053 # Default destructors generated by the compiler and the symbols inside
00054 # an anonymous namespace are excluded.
00055 #
00056 # Class variable and function symbols start with two ??  and class
00057 # static variable and static function symbols start with one ?.
00058 
00059 import re, sys, os, os.path, string
00060 
00061 def usage(argv):
00062     sys.stderr.write("%s <lib file> <library name> <version> <def file>\n" %
00063                      os.path.basename(argv[0]))
00064 
00065 def main(argv):
00066     try:
00067         _, libfile, binname, version, deffile = argv
00068     except ValueError:
00069         usage(argv)
00070         sys.exit(1)
00071 
00072     cmd = "DUMPBIN.EXE /SYMBOLS %s" % libfile
00073     print cmd
00074     dumped = os.popen(cmd)
00075 
00076     definitions = {}
00077 
00078     linere1 = re.compile(r"^[^ ]+ +[^ ]+ +SECT[^ ]+ +[^ ]+ +\(\) +External +\| +(\?[^ ]*)(.*)\n")
00079     linere2 = re.compile(r"^[^ ]+ +[^ ]+ +SECT[^ ]+ +[^ ]+ +External +\| +(\?[^?][^ ]*)(.*)\n")
00080 
00081     exclude = re.compile(r"deleting destructor[^(]+\(unsigned int\)|anonymous namespace")
00082 
00083     while 1:
00084         line = dumped.readline()
00085         if line == "":
00086             break
00087 
00088         match = linere1.search(line) or linere2.search(line)
00089         if match:
00090             symbol = match.group(1)
00091             args   = match.group(2)
00092             if exclude.search(args):
00093                 continue
00094 
00095             definitions[symbol] = None
00096 
00097     symbols = definitions.keys()
00098     symbols.sort()
00099 
00100     print "Output %d symbols." % len(symbols)
00101 
00102     out = open(deffile, "w")
00103     if string.lower(binname[4:]) == ".exe":
00104         out.write("NAME %s\n" % binname)
00105     else:
00106         out.write("LIBRARY %s\n" % binname)
00107     out.write("VERSION %s\n" % version)
00108     out.write("EXPORTS\n")
00109 
00110     for s in symbols:
00111         out.write(s + "\n")
00112 
00113     out.close()
00114 
00115 
00116 if __name__ == "__main__":
00117     main(sys.argv)


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:04