/Tools/freeze/winmakemakefile.py

http://unladen-swallow.googlecode.com/ · Python · 146 lines · 120 code · 18 blank · 8 comment · 13 complexity · 0a804578ee7f6a9214ad364542390134 MD5 · raw file

  1. import sys, os
  2. # Template used then the program is a GUI program
  3. WINMAINTEMPLATE = """
  4. #include <windows.h>
  5. int WINAPI WinMain(
  6. HINSTANCE hInstance, // handle to current instance
  7. HINSTANCE hPrevInstance, // handle to previous instance
  8. LPSTR lpCmdLine, // pointer to command line
  9. int nCmdShow // show state of window
  10. )
  11. {
  12. extern int Py_FrozenMain(int, char **);
  13. PyImport_FrozenModules = _PyImport_FrozenModules;
  14. return Py_FrozenMain(__argc, __argv);
  15. }
  16. """
  17. SERVICETEMPLATE = """
  18. extern int PythonService_main(int, char **);
  19. int main( int argc, char **argv)
  20. {
  21. PyImport_FrozenModules = _PyImport_FrozenModules;
  22. return PythonService_main(argc, argv);
  23. }
  24. """
  25. subsystem_details = {
  26. # -s flag : (C entry point template), (is it __main__?), (is it a DLL?)
  27. 'console' : (None, 1, 0),
  28. 'windows' : (WINMAINTEMPLATE, 1, 0),
  29. 'service' : (SERVICETEMPLATE, 0, 0),
  30. 'com_dll' : ("", 0, 1),
  31. }
  32. def get_custom_entry_point(subsystem):
  33. try:
  34. return subsystem_details[subsystem][:2]
  35. except KeyError:
  36. raise ValueError, "The subsystem %s is not known" % subsystem
  37. def makemakefile(outfp, vars, files, target):
  38. save = sys.stdout
  39. try:
  40. sys.stdout = outfp
  41. realwork(vars, files, target)
  42. finally:
  43. sys.stdout = save
  44. def realwork(vars, moddefns, target):
  45. version_suffix = "%r%r" % sys.version_info[:2]
  46. print "# Makefile for Microsoft Visual C++ generated by freeze.py script"
  47. print
  48. print 'target = %s' % target
  49. print 'pythonhome = %s' % vars['prefix']
  50. print
  51. print 'DEBUG=0 # Set to 1 to use the _d versions of Python.'
  52. print '!IF $(DEBUG)'
  53. print 'debug_suffix=_d'
  54. print 'c_debug=/Zi /Od /DDEBUG /D_DEBUG'
  55. print 'l_debug=/DEBUG'
  56. print 'temp_dir=Build\\Debug'
  57. print '!ELSE'
  58. print 'debug_suffix='
  59. print 'c_debug=/Ox'
  60. print 'l_debug='
  61. print 'temp_dir=Build\\Release'
  62. print '!ENDIF'
  63. print
  64. print '# The following line assumes you have built Python using the standard instructions'
  65. print '# Otherwise fix the following line to point to the library.'
  66. print 'pythonlib = "$(pythonhome)/pcbuild/python%s$(debug_suffix).lib"' % version_suffix
  67. print
  68. # We only ever write one "entry point" symbol - either
  69. # "main" or "WinMain". Therefore, there is no need to
  70. # pass a subsystem switch to the linker as it works it
  71. # out all by itself. However, the subsystem _does_ determine
  72. # the file extension and additional linker flags.
  73. target_link_flags = ""
  74. target_ext = ".exe"
  75. if subsystem_details[vars['subsystem']][2]:
  76. target_link_flags = "-dll"
  77. target_ext = ".dll"
  78. print "# As the target uses Python%s.dll, we must use this compiler option!" % version_suffix
  79. print "cdl = /MD"
  80. print
  81. print "all: $(target)$(debug_suffix)%s" % (target_ext)
  82. print
  83. print '$(temp_dir):'
  84. print ' if not exist $(temp_dir)\. mkdir $(temp_dir)'
  85. print
  86. objects = []
  87. libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib", "oleaut32.lib"]
  88. for moddefn in moddefns:
  89. print "# Module", moddefn.name
  90. for file in moddefn.sourceFiles:
  91. base = os.path.basename(file)
  92. base, ext = os.path.splitext(base)
  93. objects.append(base + ".obj")
  94. print '$(temp_dir)\%s.obj: "%s"' % (base, file)
  95. print "\t@$(CC) -c -nologo /Fo$* $(cdl) $(c_debug) /D BUILD_FREEZE",
  96. print '"-I$(pythonhome)/Include" "-I$(pythonhome)/PC" \\'
  97. print "\t\t$(cflags) $(cdebug) $(cinclude) \\"
  98. extra = moddefn.GetCompilerOptions()
  99. if extra:
  100. print "\t\t%s \\" % (' '.join(extra),)
  101. print '\t\t"%s"' % file
  102. print
  103. # Add .lib files this module needs
  104. for modlib in moddefn.GetLinkerLibs():
  105. if modlib not in libs:
  106. libs.append(modlib)
  107. print "ADDN_LINK_FILES=",
  108. for addn in vars['addn_link']: print '"%s"' % (addn),
  109. print ; print
  110. print "OBJS=",
  111. for obj in objects: print '"$(temp_dir)\%s"' % (obj),
  112. print ; print
  113. print "LIBS=",
  114. for lib in libs: print '"%s"' % (lib),
  115. print ; print
  116. print "$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext)
  117. print "\tlink -out:$(target)$(debug_suffix)%s %s" % (target_ext, target_link_flags),
  118. print "\t$(OBJS) \\"
  119. print "\t$(LIBS) \\"
  120. print "\t$(ADDN_LINK_FILES) \\"
  121. print "\t$(pythonlib) $(lcustom) $(l_debug)\\"
  122. print "\t$(resources)"
  123. print
  124. print "clean:"
  125. print "\t-rm -f *.obj"
  126. print "\t-rm -f $(target).exe"