blob: 3843388c119c4eef67aa237d4ea4179439b4eb07 [file] [log] [blame]
Walter Dörwaldaaab30e2002-09-11 20:36:02 +00001import sys, os
Guido van Rossum58a59481997-08-14 01:45:33 +00002
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:
Collin Wintera817e582007-08-22 23:05:06 +000042 raise ValueError("The subsystem %s is not known" % subsystem)
Guido van Rossum78fc3631998-03-20 17:37:24 +000043
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):
Walter Dörwald70a6b492004-02-12 17:35:32 +000054 version_suffix = "%r%r" % sys.version_info[:2]
Guido van Rossum96bf7e82007-02-09 23:27:01 +000055 print("# Makefile for Microsoft Visual C++ generated by freeze.py script")
56 print()
57 print('target = %s' % target)
58 print('pythonhome = %s' % vars['prefix'])
59 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()
Guido van Rossumbaf06031998-08-25 14:06:55 +000073
Guido van Rossum96bf7e82007-02-09 23:27:01 +000074 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.')
76 print('pythonlib = "$(pythonhome)/pcbuild/python%s$(debug_suffix).lib"' % version_suffix)
77 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 Rossum96bf7e82007-02-09 23:27:01 +000091 print("# As the target uses Python%s.dll, we must use this compiler option!" % version_suffix)
92 print("cdl = /MD")
93 print()
94 print("all: $(target)$(debug_suffix)%s" % (target_ext))
95 print()
Guido van Rossumbaf06031998-08-25 14:06:55 +000096
Guido van Rossum96bf7e82007-02-09 23:27:01 +000097 print('$(temp_dir):')
98 print(' if not exist $(temp_dir)\. mkdir $(temp_dir)')
99 print()
Guido van Rossum58a59481997-08-14 01:45:33 +0000100
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:
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000104 print("# Module", moddefn.name)
Guido van Rossum78fc3631998-03-20 17:37:24 +0000105 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 Rossum96bf7e82007-02-09 23:27:01 +0000109 print('$(temp_dir)\%s.obj: "%s"' % (base, file))
110 print("\t@$(CC) -c -nologo /Fo$* $(cdl) $(c_debug) /D BUILD_FREEZE", end=' ')
111 print('"-I$(pythonhome)/Include" "-I$(pythonhome)/PC" \\')
112 print("\t\t$(cflags) $(cdebug) $(cinclude) \\")
Guido van Rossum78fc3631998-03-20 17:37:24 +0000113 extra = moddefn.GetCompilerOptions()
114 if extra:
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000115 print("\t\t%s \\" % (' '.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
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000124 print("ADDN_LINK_FILES=", end=' ')
125 for addn in vars['addn_link']: print('"%s"' % (addn), end=' ')
126 print() ; print()
Guido van Rossum78fc3631998-03-20 17:37:24 +0000127
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000128 print("OBJS=", end=' ')
129 for obj in objects: print('"$(temp_dir)\%s"' % (obj), end=' ')
130 print() ; print()
Guido van Rossum78fc3631998-03-20 17:37:24 +0000131
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000132 print("LIBS=", end=' ')
133 for lib in libs: print('"%s"' % (lib), end=' ')
134 print() ; print()
Guido van Rossum78fc3631998-03-20 17:37:24 +0000135
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000136 print("$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext))
Georg Brandl6d61cb42010-10-21 13:34:51 +0000137 print("\tlink -out:$(target)$(debug_suffix)%s %s" %
138 (target_ext, target_link_flags), "@<<")
139 print("\t$(OBJS)")
140 print("\t$(LIBS)")
141 print("\t$(ADDN_LINK_FILES)")
142 print("\t$(pythonlib) $(lcustom) $(l_debug)")
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000143 print("\t$(resources)")
Georg Brandl6d61cb42010-10-21 13:34:51 +0000144 print("<<")
Guido van Rossum96bf7e82007-02-09 23:27:01 +0000145 print()
146 print("clean:")
147 print("\t-rm -f *.obj")
148 print("\t-rm -f $(target).exe")