/Tools/freeze/checkextensions_win32.py

http://unladen-swallow.googlecode.com/ · Python · 188 lines · 136 code · 26 blank · 26 comment · 26 complexity · 4ed39ef9a21361c198bd3b9f2c137764 MD5 · raw file

  1. """Extension management for Windows.
  2. Under Windows it is unlikely the .obj files are of use, as special compiler options
  3. are needed (primarily to toggle the behavior of "public" symbols.
  4. I dont consider it worth parsing the MSVC makefiles for compiler options. Even if
  5. we get it just right, a specific freeze application may have specific compiler
  6. options anyway (eg, to enable or disable specific functionality)
  7. So my basic stragtegy is:
  8. * Have some Windows INI files which "describe" one or more extension modules.
  9. (Freeze comes with a default one for all known modules - but you can specify
  10. your own).
  11. * This description can include:
  12. - The MSVC .dsp file for the extension. The .c source file names
  13. are extraced from there.
  14. - Specific compiler/linker options
  15. - Flag to indicate if Unicode compilation is expected.
  16. At the moment the name and location of this INI file is hardcoded,
  17. but an obvious enhancement would be to provide command line options.
  18. """
  19. import os, sys
  20. try:
  21. import win32api
  22. except ImportError:
  23. win32api = None # User has already been warned
  24. class CExtension:
  25. """An abstraction of an extension implemented in C/C++
  26. """
  27. def __init__(self, name, sourceFiles):
  28. self.name = name
  29. # A list of strings defining additional compiler options.
  30. self.sourceFiles = sourceFiles
  31. # A list of special compiler options to be applied to
  32. # all source modules in this extension.
  33. self.compilerOptions = []
  34. # A list of .lib files the final .EXE will need.
  35. self.linkerLibs = []
  36. def GetSourceFiles(self):
  37. return self.sourceFiles
  38. def AddCompilerOption(self, option):
  39. self.compilerOptions.append(option)
  40. def GetCompilerOptions(self):
  41. return self.compilerOptions
  42. def AddLinkerLib(self, lib):
  43. self.linkerLibs.append(lib)
  44. def GetLinkerLibs(self):
  45. return self.linkerLibs
  46. def checkextensions(unknown, extra_inis, prefix):
  47. # Create a table of frozen extensions
  48. defaultMapName = os.path.join( os.path.split(sys.argv[0])[0], "extensions_win32.ini")
  49. if not os.path.isfile(defaultMapName):
  50. sys.stderr.write("WARNING: %s can not be found - standard extensions may not be found\n" % defaultMapName)
  51. else:
  52. # must go on end, so other inis can override.
  53. extra_inis.append(defaultMapName)
  54. ret = []
  55. for mod in unknown:
  56. for ini in extra_inis:
  57. # print "Looking for", mod, "in", win32api.GetFullPathName(ini),"...",
  58. defn = get_extension_defn( mod, ini, prefix )
  59. if defn is not None:
  60. # print "Yay - found it!"
  61. ret.append( defn )
  62. break
  63. # print "Nope!"
  64. else: # For not broken!
  65. sys.stderr.write("No definition of module %s in any specified map file.\n" % (mod))
  66. return ret
  67. def get_extension_defn(moduleName, mapFileName, prefix):
  68. if win32api is None: return None
  69. os.environ['PYTHONPREFIX'] = prefix
  70. dsp = win32api.GetProfileVal(moduleName, "dsp", "", mapFileName)
  71. if dsp=="":
  72. return None
  73. # We allow environment variables in the file name
  74. dsp = win32api.ExpandEnvironmentStrings(dsp)
  75. # If the path to the .DSP file is not absolute, assume it is relative
  76. # to the description file.
  77. if not os.path.isabs(dsp):
  78. dsp = os.path.join( os.path.split(mapFileName)[0], dsp)
  79. # Parse it to extract the source files.
  80. sourceFiles = parse_dsp(dsp)
  81. if sourceFiles is None:
  82. return None
  83. module = CExtension(moduleName, sourceFiles)
  84. # Put the path to the DSP into the environment so entries can reference it.
  85. os.environ['dsp_path'] = os.path.split(dsp)[0]
  86. os.environ['ini_path'] = os.path.split(mapFileName)[0]
  87. cl_options = win32api.GetProfileVal(moduleName, "cl", "", mapFileName)
  88. if cl_options:
  89. module.AddCompilerOption(win32api.ExpandEnvironmentStrings(cl_options))
  90. exclude = win32api.GetProfileVal(moduleName, "exclude", "", mapFileName)
  91. exclude = exclude.split()
  92. if win32api.GetProfileVal(moduleName, "Unicode", 0, mapFileName):
  93. module.AddCompilerOption('/D UNICODE /D _UNICODE')
  94. libs = win32api.GetProfileVal(moduleName, "libs", "", mapFileName).split()
  95. for lib in libs:
  96. module.AddLinkerLib(win32api.ExpandEnvironmentStrings(lib))
  97. for exc in exclude:
  98. if exc in module.sourceFiles:
  99. modules.sourceFiles.remove(exc)
  100. return module
  101. # Given an MSVC DSP file, locate C source files it uses
  102. # returns a list of source files.
  103. def parse_dsp(dsp):
  104. # print "Processing", dsp
  105. # For now, only support
  106. ret = []
  107. dsp_path, dsp_name = os.path.split(dsp)
  108. try:
  109. lines = open(dsp, "r").readlines()
  110. except IOError, msg:
  111. sys.stderr.write("%s: %s\n" % (dsp, msg))
  112. return None
  113. for line in lines:
  114. fields = line.strip().split("=", 2)
  115. if fields[0]=="SOURCE":
  116. if os.path.splitext(fields[1])[1].lower() in ['.cpp', '.c']:
  117. ret.append( win32api.GetFullPathName(os.path.join(dsp_path, fields[1] ) ) )
  118. return ret
  119. def write_extension_table(fname, modules):
  120. fp = open(fname, "w")
  121. try:
  122. fp.write (ext_src_header)
  123. # Write fn protos
  124. for module in modules:
  125. # bit of a hack for .pyd's as part of packages.
  126. name = module.name.split('.')[-1]
  127. fp.write('extern void init%s(void);\n' % (name) )
  128. # Write the table
  129. fp.write (ext_tab_header)
  130. for module in modules:
  131. name = module.name.split('.')[-1]
  132. fp.write('\t{"%s", init%s},\n' % (name, name) )
  133. fp.write (ext_tab_footer)
  134. fp.write(ext_src_footer)
  135. finally:
  136. fp.close()
  137. ext_src_header = """\
  138. #include "Python.h"
  139. """
  140. ext_tab_header = """\
  141. static struct _inittab extensions[] = {
  142. """
  143. ext_tab_footer = """\
  144. /* Sentinel */
  145. {0, 0}
  146. };
  147. """
  148. ext_src_footer = """\
  149. extern DL_IMPORT(int) PyImport_ExtendInittab(struct _inittab *newtab);
  150. int PyInitFrozenExtensions()
  151. {
  152. return PyImport_ExtendInittab(extensions);
  153. }
  154. """