blob: 3f37e771a2153e0dee96f6bc193787489dbf97da [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 Rossum58a59481997-08-14 01:45:33 +000053 print "# Makefile for Windows (NT or 95) generated by freeze.py script"
54 print
Guido van Rossum2addd2a1998-03-07 05:10:00 +000055 print 'target = %s' % target
56 print 'pythonhome = "%s"' % vars['prefix']
Guido van Rossum77b30081997-08-14 20:13:46 +000057 # XXX The following line is fishy and may need manual fixing
Guido van Rossum2addd2a1998-03-07 05:10:00 +000058 print 'pythonlib = "%s"' % (vars['exec_prefix'] +
59 "/pcbuild/release/python15.lib")
Guido van Rossum78fc3631998-03-20 17:37:24 +000060
61 # We only ever write one "entry point" symbol - either
62 # "main" or "WinMain". Therefore, there is no need to
63 # pass a subsystem switch to the linker as it works it
64 # out all by itself. However, the subsystem _does_ determine
65 # the file extension and additional linker flags.
66 target_link_flags = ""
67 target_ext = ".exe"
68 if subsystem_details[vars['subsystem']][2]:
69 target_link_flags = "-dll"
70 target_ext = ".dll"
71
72 print "cdl = /MD" # XXX - Should this come from vars? User may have specific requirements...
Guido van Rossum58a59481997-08-14 01:45:33 +000073 print
Guido van Rossum78fc3631998-03-20 17:37:24 +000074 print "all: $(target)%s" % (target_ext)
Guido van Rossum58a59481997-08-14 01:45:33 +000075 print
76
77 objects = []
Guido van Rossum78fc3631998-03-20 17:37:24 +000078 libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib"]
79 for moddefn in moddefns:
80 print "# Module", moddefn.name
81 for file in moddefn.sourceFiles:
82 base = os.path.basename(file)
83 base, ext = os.path.splitext(base)
84 objects.append(base + ".obj")
85 print '%s.obj: "%s"' % (base, file)
86 print "\t@$(CC) -c -nologo $(cdl) /D BUILD_FREEZE",
87 print "-I$(pythonhome)/Include -I$(pythonhome)/PC \\"
88 print "\t\t$(cflags) $(cdebug) $(cinclude) \\"
89 extra = moddefn.GetCompilerOptions()
90 if extra:
91 print "\t\t%s \\" % (string.join(extra),)
92 print '\t\t"%s"' % file
93 print
Guido van Rossum58a59481997-08-14 01:45:33 +000094
Guido van Rossum78fc3631998-03-20 17:37:24 +000095 # Add .lib files this module needs
96 for modlib in moddefn.GetLinkerLibs():
97 if modlib not in libs:
98 libs.append(modlib)
99
100 print "ADDN_LINK_FILES=",
101 for addn in vars['addn_link']: print '"%s"' % (addn),
102 print ; print
103
104 print "OBJS=",
105 for obj in objects: print '"%s"' % (obj),
106 print ; print
107
108 print "LIBS=",
109 for lib in libs: print '"%s"' % (lib),
110 print ; print
111
112 print "$(target)%s: $(OBJS)" % (target_ext)
113 print "\tlink -out:$(target)%s %s" % (target_ext, target_link_flags),
114 print "\t$(OBJS) \\"
115 print "\t$(LIBS) \\"
116 print "\t$(ADDN_LINK_FILES) \\"
117 print "\t\t$(pythonlib) $(lcustom)\\"
118 print "\t\t$(resources)"
Guido van Rossum2addd2a1998-03-07 05:10:00 +0000119 print
120 print "clean:"
Guido van Rossum78fc3631998-03-20 17:37:24 +0000121 print "\t-rm -f *.obj"
122 print "\t-rm -f $(target).exe"