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