blob: 320504b67e96c6e76f3fcceb10a8c265f69a184b [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 Rossumbaf06031998-08-25 14:06:55 +000054 print "# Makefile for Microsoft Visual C++ generated by freeze.py script"
Guido van Rossum58a59481997-08-14 01:45:33 +000055 print
Guido van Rossum2addd2a1998-03-07 05:10:00 +000056 print 'target = %s' % target
Guido van Rossum43128901999-06-21 22:36:53 +000057 print 'pythonhome = %s' % vars['prefix']
Guido van Rossumbaf06031998-08-25 14:06:55 +000058 print
59 print 'DEBUG=0 # Set to 1 to use the _d versions of Python.'
60 print '!IF $(DEBUG)'
61 print 'debug_suffix=_d'
62 print 'c_debug=/Zi /Od /DDEBUG /D_DEBUG'
63 print 'l_debug=/DEBUG'
64 print 'temp_dir=Build\\Debug'
65 print '!ELSE'
66 print 'debug_suffix='
67 print 'c_debug=/Ox'
68 print 'l_debug='
69 print 'temp_dir=Build\\Release'
70 print '!ENDIF'
71 print
72
73 print '# The following line assumes you have built Python using the standard instructions'
74 print '# Otherwise fix the following line to point to the library.'
75 print 'pythonlib = "$(pythonhome)/pcbuild/python15$(debug_suffix).lib"'
76 print
Guido van Rossum78fc3631998-03-20 17:37:24 +000077
78 # We only ever write one "entry point" symbol - either
79 # "main" or "WinMain". Therefore, there is no need to
80 # pass a subsystem switch to the linker as it works it
81 # out all by itself. However, the subsystem _does_ determine
82 # the file extension and additional linker flags.
83 target_link_flags = ""
84 target_ext = ".exe"
85 if subsystem_details[vars['subsystem']][2]:
86 target_link_flags = "-dll"
87 target_ext = ".dll"
88
Guido van Rossumbaf06031998-08-25 14:06:55 +000089
90 print "# As the target uses Python15.dll, we must use this compiler option!"
91 print "cdl = /MD"
Guido van Rossum58a59481997-08-14 01:45:33 +000092 print
Guido van Rossumbaf06031998-08-25 14:06:55 +000093 print "all: $(target)$(debug_suffix)%s" % (target_ext)
94 print
95
96 print '$(temp_dir):'
97 print ' if not exist $(temp_dir)\. mkdir $(temp_dir)'
Guido van Rossum58a59481997-08-14 01:45:33 +000098 print
99
100 objects = []
Guido van Rossumbaf06031998-08-25 14:06:55 +0000101 libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib", "oleaut32.lib"]
Guido van Rossum78fc3631998-03-20 17:37:24 +0000102 for moddefn in moddefns:
103 print "# Module", moddefn.name
104 for file in moddefn.sourceFiles:
105 base = os.path.basename(file)
106 base, ext = os.path.splitext(base)
107 objects.append(base + ".obj")
Guido van Rossumbaf06031998-08-25 14:06:55 +0000108 print '$(temp_dir)\%s.obj: "%s"' % (base, file)
109 print "\t@$(CC) -c -nologo /Fo$* $(cdl) $(c_debug) /D BUILD_FREEZE",
Guido van Rossum43128901999-06-21 22:36:53 +0000110 print '"-I$(pythonhome)/Include" "-I$(pythonhome)/PC" \\'
Guido van Rossum78fc3631998-03-20 17:37:24 +0000111 print "\t\t$(cflags) $(cdebug) $(cinclude) \\"
112 extra = moddefn.GetCompilerOptions()
113 if extra:
114 print "\t\t%s \\" % (string.join(extra),)
115 print '\t\t"%s"' % file
116 print
Guido van Rossum58a59481997-08-14 01:45:33 +0000117
Guido van Rossum78fc3631998-03-20 17:37:24 +0000118 # Add .lib files this module needs
119 for modlib in moddefn.GetLinkerLibs():
120 if modlib not in libs:
121 libs.append(modlib)
122
123 print "ADDN_LINK_FILES=",
124 for addn in vars['addn_link']: print '"%s"' % (addn),
125 print ; print
126
127 print "OBJS=",
Guido van Rossumbaf06031998-08-25 14:06:55 +0000128 for obj in objects: print '"$(temp_dir)\%s"' % (obj),
Guido van Rossum78fc3631998-03-20 17:37:24 +0000129 print ; print
130
131 print "LIBS=",
132 for lib in libs: print '"%s"' % (lib),
133 print ; print
134
Guido van Rossumbaf06031998-08-25 14:06:55 +0000135 print "$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext)
136 print "\tlink -out:$(target)$(debug_suffix)%s %s" % (target_ext, target_link_flags),
Guido van Rossum78fc3631998-03-20 17:37:24 +0000137 print "\t$(OBJS) \\"
138 print "\t$(LIBS) \\"
139 print "\t$(ADDN_LINK_FILES) \\"
Guido van Rossumbaf06031998-08-25 14:06:55 +0000140 print "\t$(pythonlib) $(lcustom) $(l_debug)\\"
141 print "\t$(resources)"
Guido van Rossum2addd2a1998-03-07 05:10:00 +0000142 print
143 print "clean:"
Guido van Rossum78fc3631998-03-20 17:37:24 +0000144 print "\t-rm -f *.obj"
145 print "\t-rm -f $(target).exe"