blob: d668a6e8273d50e3bc550eab47e6b87377c44377 [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 Rossumcfd76a21999-11-02 15:44:40 +000014 extern int Py_FrozenMain(int, char **);
Guido van Rossum78fc3631998-03-20 17:37:24 +000015 PyImport_FrozenModules = _PyImport_FrozenModules;
16 return Py_FrozenMain(__argc, __argv);
Guido van Rossumf8883501998-03-04 18:12:39 +000017}
18"""
19
Guido van Rossum78fc3631998-03-20 17:37:24 +000020SERVICETEMPLATE = """
21extern int PythonService_main(int, char **);
22
23int main( int argc, char **argv)
24{
25 PyImport_FrozenModules = _PyImport_FrozenModules;
26 return PythonService_main(argc, argv);
27}
28"""
29
30subsystem_details = {
31 # -s flag : (C entry point template), (is it __main__?), (is it a DLL?)
32 'console' : (None, 1, 0),
33 'windows' : (WINMAINTEMPLATE, 1, 0),
34 'service' : (SERVICETEMPLATE, 0, 0),
35 'com_dll' : ("", 0, 1),
36}
37
38def get_custom_entry_point(subsystem):
39 try:
40 return subsystem_details[subsystem][:2]
41 except KeyError:
42 raise ValueError, "The subsystem %s is not known" % subsystem
43
44
Guido van Rossum58a59481997-08-14 01:45:33 +000045def makemakefile(outfp, vars, files, target):
46 save = sys.stdout
47 try:
48 sys.stdout = outfp
49 realwork(vars, files, target)
50 finally:
51 sys.stdout = save
52
Guido van Rossum78fc3631998-03-20 17:37:24 +000053def realwork(vars, moddefns, target):
Guido van Rossumf67c2382000-07-13 15:45:17 +000054 version_suffix = `sys.version_info[0]`+`sys.version_info[1]`
Guido van Rossumbaf06031998-08-25 14:06:55 +000055 print "# Makefile for Microsoft Visual C++ generated by freeze.py script"
Guido van Rossum58a59481997-08-14 01:45:33 +000056 print
Guido van Rossum2addd2a1998-03-07 05:10:00 +000057 print 'target = %s' % target
Guido van Rossum43128901999-06-21 22:36:53 +000058 print 'pythonhome = %s' % vars['prefix']
Guido van Rossumbaf06031998-08-25 14:06:55 +000059 print
60 print 'DEBUG=0 # Set to 1 to use the _d versions of Python.'
61 print '!IF $(DEBUG)'
62 print 'debug_suffix=_d'
63 print 'c_debug=/Zi /Od /DDEBUG /D_DEBUG'
64 print 'l_debug=/DEBUG'
65 print 'temp_dir=Build\\Debug'
66 print '!ELSE'
67 print 'debug_suffix='
68 print 'c_debug=/Ox'
69 print 'l_debug='
70 print 'temp_dir=Build\\Release'
71 print '!ENDIF'
72 print
73
74 print '# The following line assumes you have built Python using the standard instructions'
75 print '# Otherwise fix the following line to point to the library.'
Guido van Rossumf67c2382000-07-13 15:45:17 +000076 print 'pythonlib = "$(pythonhome)/pcbuild/python%s$(debug_suffix).lib"' % version_suffix
Guido van Rossumbaf06031998-08-25 14:06:55 +000077 print
Guido van Rossum78fc3631998-03-20 17:37:24 +000078
79 # We only ever write one "entry point" symbol - either
80 # "main" or "WinMain". Therefore, there is no need to
81 # pass a subsystem switch to the linker as it works it
82 # out all by itself. However, the subsystem _does_ determine
83 # the file extension and additional linker flags.
84 target_link_flags = ""
85 target_ext = ".exe"
86 if subsystem_details[vars['subsystem']][2]:
87 target_link_flags = "-dll"
88 target_ext = ".dll"
89
Guido van Rossumbaf06031998-08-25 14:06:55 +000090
Guido van Rossumf67c2382000-07-13 15:45:17 +000091 print "# As the target uses Python%s.dll, we must use this compiler option!" % version_suffix
Guido van Rossumbaf06031998-08-25 14:06:55 +000092 print "cdl = /MD"
Guido van Rossum58a59481997-08-14 01:45:33 +000093 print
Guido van Rossumbaf06031998-08-25 14:06:55 +000094 print "all: $(target)$(debug_suffix)%s" % (target_ext)
95 print
96
97 print '$(temp_dir):'
98 print ' if not exist $(temp_dir)\. mkdir $(temp_dir)'
Guido van Rossum58a59481997-08-14 01:45:33 +000099 print
100
101 objects = []
Guido van Rossumbaf06031998-08-25 14:06:55 +0000102 libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib", "oleaut32.lib"]
Guido van Rossum78fc3631998-03-20 17:37:24 +0000103 for moddefn in moddefns:
104 print "# Module", moddefn.name
105 for file in moddefn.sourceFiles:
106 base = os.path.basename(file)
107 base, ext = os.path.splitext(base)
108 objects.append(base + ".obj")
Guido van Rossumbaf06031998-08-25 14:06:55 +0000109 print '$(temp_dir)\%s.obj: "%s"' % (base, file)
110 print "\t@$(CC) -c -nologo /Fo$* $(cdl) $(c_debug) /D BUILD_FREEZE",
Guido van Rossum43128901999-06-21 22:36:53 +0000111 print '"-I$(pythonhome)/Include" "-I$(pythonhome)/PC" \\'
Guido van Rossum78fc3631998-03-20 17:37:24 +0000112 print "\t\t$(cflags) $(cdebug) $(cinclude) \\"
113 extra = moddefn.GetCompilerOptions()
114 if extra:
115 print "\t\t%s \\" % (string.join(extra),)
116 print '\t\t"%s"' % file
117 print
Guido van Rossum58a59481997-08-14 01:45:33 +0000118
Guido van Rossum78fc3631998-03-20 17:37:24 +0000119 # Add .lib files this module needs
120 for modlib in moddefn.GetLinkerLibs():
121 if modlib not in libs:
122 libs.append(modlib)
123
124 print "ADDN_LINK_FILES=",
125 for addn in vars['addn_link']: print '"%s"' % (addn),
126 print ; print
127
128 print "OBJS=",
Guido van Rossumbaf06031998-08-25 14:06:55 +0000129 for obj in objects: print '"$(temp_dir)\%s"' % (obj),
Guido van Rossum78fc3631998-03-20 17:37:24 +0000130 print ; print
131
132 print "LIBS=",
133 for lib in libs: print '"%s"' % (lib),
134 print ; print
135
Guido van Rossumbaf06031998-08-25 14:06:55 +0000136 print "$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext)
137 print "\tlink -out:$(target)$(debug_suffix)%s %s" % (target_ext, target_link_flags),
Guido van Rossum78fc3631998-03-20 17:37:24 +0000138 print "\t$(OBJS) \\"
139 print "\t$(LIBS) \\"
140 print "\t$(ADDN_LINK_FILES) \\"
Guido van Rossumbaf06031998-08-25 14:06:55 +0000141 print "\t$(pythonlib) $(lcustom) $(l_debug)\\"
142 print "\t$(resources)"
Guido van Rossum2addd2a1998-03-07 05:10:00 +0000143 print
144 print "clean:"
Guido van Rossum78fc3631998-03-20 17:37:24 +0000145 print "\t-rm -f *.obj"
146 print "\t-rm -f $(target).exe"