makefreeze.py 2.64 KB
Newer Older
1
import marshal
2
import bkfile
3 4 5 6 7


# Write a file containing frozen code for the modules in the dictionary.

header = """
8 9 10
#include "Python.h"

static struct _frozen _PyImport_FrozenModules[] = {
11 12
"""
trailer = """\
13
    {0, 0, 0} /* sentinel */
14
};
15
"""
16

17
# if __debug__ == 0 (i.e. -O option given), set Py_OptimizeFlag in frozen app.
18
default_entry_point = """
19
int
20
main(int argc, char **argv)
21
{
22
        extern int Py_FrozenMain(int, char **);
23 24 25
""" + ((not __debug__ and """
        Py_OptimizeFlag++;
""") or "")  + """
26 27
        PyImport_FrozenModules = _PyImport_FrozenModules;
        return Py_FrozenMain(argc, argv);
28 29
}

30 31
"""

32
def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()):
33
    if entry_point is None: entry_point = default_entry_point
34
    done = []
35
    files = []
36
    mods = sorted(dict.keys())
37 38
    for mod in mods:
        m = dict[mod]
39
        mangled = "__".join(mod.split("."))
40
        if m.__code__:
41 42 43
            file = 'M_' + mangled + '.c'
            outfp = bkfile.open(base + file, 'w')
            files.append(file)
44
            if debug:
Guido van Rossum's avatar
Guido van Rossum committed
45
                print("freezing", mod, "...")
46 47 48 49 50 51 52
            str = marshal.dumps(m.__code__)
            size = len(str)
            if m.__path__:
                # Indicate package by negative size
                size = -size
            done.append((mod, mangled, size))
            writecode(outfp, mangled, str)
53
            outfp.close()
54
    if debug:
Guido van Rossum's avatar
Guido van Rossum committed
55
        print("generating table of frozen modules")
56 57 58
    outfp = bkfile.open(base + 'frozen.c', 'w')
    for mod, mangled, size in done:
        outfp.write('extern unsigned char M_%s[];\n' % mangled)
59 60 61
    outfp.write(header)
    for mod, mangled, size in done:
        outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size))
62 63 64 65 66 67 68
    outfp.write('\n')
    # The following modules have a NULL code pointer, indicating
    # that the prozen program should not search for them on the host
    # system. Importing them will *always* raise an ImportError.
    # The zero value size is never used.
    for mod in fail_import:
        outfp.write('\t{"%s", NULL, 0},\n' % (mod,))
69
    outfp.write(trailer)
70
    outfp.write(entry_point)
71 72
    outfp.close()
    return files
73

74 75 76 77 78 79


# Write a C initializer for a module containing the frozen python code.
# The array is called M_<mod>.

def writecode(outfp, mod, str):
80
    outfp.write('unsigned char M_%s[] = {' % mod)
81 82
    for i in range(0, len(str), 16):
        outfp.write('\n\t')
83 84
        for c in bytes(str[i:i+16]):
            outfp.write('%d,' % c)
85
    outfp.write('\n};\n')
86 87 88

## def writecode(outfp, mod, str):
##     outfp.write('unsigned char M_%s[%d] = "%s";\n' % (mod, len(str),
89
##     '\\"'.join(map(lambda s: repr(s)[1:-1], str.split('"')))))