winmakemakefile.py 4.71 KB
Newer Older
1
import sys, os
2

3
# Template used then the program is a GUI program
4 5 6 7 8 9 10 11 12 13
WINMAINTEMPLATE = """
#include <windows.h>

int WINAPI WinMain(
    HINSTANCE hInstance,      // handle to current instance
    HINSTANCE hPrevInstance,  // handle to previous instance
    LPSTR lpCmdLine,          // pointer to command line
    int nCmdShow              // show state of window
    )
{
Guido van Rossum's avatar
Guido van Rossum committed
14
    extern int Py_FrozenMain(int, char **);
15 16
    PyImport_FrozenModules = _PyImport_FrozenModules;
    return Py_FrozenMain(__argc, __argv);
17 18 19
}
"""

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
SERVICETEMPLATE = """
extern int PythonService_main(int, char **);

int main( int argc, char **argv)
{
    PyImport_FrozenModules = _PyImport_FrozenModules;
    return PythonService_main(argc, argv);
}
"""

subsystem_details = {
    # -s flag        : (C entry point template), (is it __main__?), (is it a DLL?)
    'console'        : (None,                    1,                 0),
    'windows'        : (WINMAINTEMPLATE,         1,                 0),
    'service'        : (SERVICETEMPLATE,         0,                 0),
    'com_dll'        : ("",                      0,                 1),
}

def get_custom_entry_point(subsystem):
    try:
        return subsystem_details[subsystem][:2]
    except KeyError:
        raise ValueError, "The subsystem %s is not known" % subsystem


45 46 47 48 49 50 51 52
def makemakefile(outfp, vars, files, target):
    save = sys.stdout
    try:
        sys.stdout = outfp
        realwork(vars, files, target)
    finally:
        sys.stdout = save

53
def 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    print
    print 'DEBUG=0 # Set to 1 to use the _d versions of Python.'
    print '!IF $(DEBUG)'
    print 'debug_suffix=_d'
    print 'c_debug=/Zi /Od /DDEBUG /D_DEBUG'
    print 'l_debug=/DEBUG'
    print 'temp_dir=Build\\Debug'
    print '!ELSE'
    print 'debug_suffix='
    print 'c_debug=/Ox'
    print 'l_debug='
    print 'temp_dir=Build\\Release'
    print '!ENDIF'
    print

    print '# The following line assumes you have built Python using the standard instructions'
    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 80 81 82 83 84 85 86 87 88 89

    # We only ever write one "entry point" symbol - either
    # "main" or "WinMain".  Therefore, there is no need to
    # pass a subsystem switch to the linker as it works it
    # out all by itself.  However, the subsystem _does_ determine
    # the file extension and additional linker flags.
    target_link_flags = ""
    target_ext = ".exe"
    if subsystem_details[vars['subsystem']][2]:
        target_link_flags = "-dll"
        target_ext = ".dll"

90

91
    print "# As the target uses Python%s.dll, we must use this compiler option!" % version_suffix
92 93 94
    print "cdl = /MD"
    print
    print "all: $(target)$(debug_suffix)%s" % (target_ext)
95
    print
96 97 98

    print '$(temp_dir):'
    print '  if not exist $(temp_dir)\. mkdir $(temp_dir)'
99 100 101
    print

    objects = []
102
    libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib", "oleaut32.lib"]
103 104 105 106 107 108
    for moddefn in moddefns:
        print "# Module", moddefn.name
        for file in moddefn.sourceFiles:
            base = os.path.basename(file)
            base, ext = os.path.splitext(base)
            objects.append(base + ".obj")
109 110
            print '$(temp_dir)\%s.obj: "%s"' % (base, file)
            print "\t@$(CC) -c -nologo /Fo$* $(cdl) $(c_debug) /D BUILD_FREEZE",
111
            print '"-I$(pythonhome)/Include"  "-I$(pythonhome)/PC" \\'
112 113 114
            print "\t\t$(cflags) $(cdebug) $(cinclude) \\"
            extra = moddefn.GetCompilerOptions()
            if extra:
115
                print "\t\t%s \\" % (' '.join(extra),)
116 117 118 119 120 121 122 123 124 125 126 127 128
            print '\t\t"%s"' % file
            print

        # Add .lib files this module needs
        for modlib in moddefn.GetLinkerLibs():
            if modlib not in libs:
                libs.append(modlib)

    print "ADDN_LINK_FILES=",
    for addn in vars['addn_link']: print '"%s"' % (addn),
    print ; print

    print "OBJS=",
129
    for obj in objects: print '"$(temp_dir)\%s"' % (obj),
130 131 132 133 134 135
    print ; print

    print "LIBS=",
    for lib in libs: print '"%s"' % (lib),
    print ; print

136 137
    print "$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext)
    print "\tlink -out:$(target)$(debug_suffix)%s %s" % (target_ext, target_link_flags),
138 139 140
    print "\t$(OBJS) \\"
    print "\t$(LIBS) \\"
    print "\t$(ADDN_LINK_FILES) \\"
141 142
    print "\t$(pythonlib) $(lcustom) $(l_debug)\\"
    print "\t$(resources)"
143 144
    print
    print "clean:"
145 146
    print "\t-rm -f *.obj"
    print "\t-rm -f $(target).exe"