generate-win32-export-forwards 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. #Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
  3. #This library is free software; you can redistribute it and/or
  4. #modify it under the terms of the GNU Library General Public
  5. #License as published by the Free Software Foundation; either
  6. #version 2 of the License, or (at your option) any later version.
  7. #This library is distributed in the hope that it will be useful,
  8. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. #Library General Public License for more details.
  11. #You should have received a copy of the GNU Library General Public License
  12. #along with this library; see the file COPYING.LIB. If not, write to
  13. #the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. #Boston, MA 02110-1301, USA.
  15. # Extract /EXPORT: linker directives from static library and write it into a
  16. # separate file as linker pragmas.
  17. # Usage: generate-win32-export-forwards \path\to\static\library.lib outputfile.cpp
  18. # Then compile outputfile.cpp into the final .dll and link the static library
  19. # into the dll.
  20. import subprocess
  21. import sys
  22. import re
  23. def exportForwardsForLibrary(library):
  24. dumpBin = subprocess.Popen("dumpbin /directives " + library, stdout=subprocess.PIPE, universal_newlines=True)
  25. output = dumpBin.communicate()[0]
  26. return output
  27. libraries = sys.argv[1 : -1]
  28. outputFileName = sys.argv[-1]
  29. exportedSymbolRegexp = re.compile("\s*(?P<symbol>/EXPORT:.+)")
  30. symbols = set()
  31. for lib in libraries:
  32. for line in exportForwardsForLibrary(lib).splitlines():
  33. match = exportedSymbolRegexp.match(line)
  34. if match:
  35. symbols.add(match.group("symbol"))
  36. print("Forwarding %s symbols from %s" % (len(symbols), " ".join(libraries)))
  37. exportFile = open(outputFileName, "w")
  38. for symbol in symbols:
  39. exportFile.write("#pragma comment(linker, \"%s\")\n" % symbol);
  40. exportFile.close()