• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import sys, os
2
3# Template used then the program is a GUI program
4WINMAINTEMPLATE = """
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{
14    extern int Py_FrozenMain(int, char **);
15    PyImport_FrozenModules = _PyImport_FrozenModules;
16    return Py_FrozenMain(__argc, __argv);
17}
18"""
19
20SERVICETEMPLATE = """
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) from None
43
44
45def 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
53def realwork(vars, moddefns, target):
54    version_suffix = "%r%r" % sys.version_info[:2]
55    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()
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.')
76    print('pythonlib = "$(pythonhome)/pcbuild/python%s$(debug_suffix).lib"' % version_suffix)
77    print()
78
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
90
91    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()
96
97    print('$(temp_dir):')
98    print(r'  if not exist $(temp_dir)\. mkdir $(temp_dir)')
99    print()
100
101    objects = []
102    libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib", "oleaut32.lib"]
103    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")
109            print(r'$(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) \\")
113            extra = moddefn.GetCompilerOptions()
114            if extra:
115                print("\t\t%s \\" % (' '.join(extra),))
116            print('\t\t"%s"' % file)
117            print()
118
119        # 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=", end=' ')
125    for addn in vars['addn_link']: print('"%s"' % (addn), end=' ')
126    print() ; print()
127
128    print("OBJS=", end=' ')
129    for obj in objects: print(r'"$(temp_dir)\%s"' % (obj), end=' ')
130    print() ; print()
131
132    print("LIBS=", end=' ')
133    for lib in libs: print('"%s"' % (lib), end=' ')
134    print() ; print()
135
136    print("$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext))
137    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)")
143    print("\t$(resources)")
144    print("<<")
145    print()
146    print("clean:")
147    print("\t-del /f *.obj")
148    print("\t-del /f $(target).exe")
149