Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 1 | import marshal |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 2 | import bkfile |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 3 | |
| 4 | |
| 5 | # Write a file containing frozen code for the modules in the dictionary. |
| 6 | |
| 7 | header = """ |
Guido van Rossum | f75f80e | 1996-06-17 17:48:30 +0000 | [diff] [blame] | 8 | #include "Python.h" |
| 9 | |
| 10 | static struct _frozen _PyImport_FrozenModules[] = { |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 11 | """ |
| 12 | trailer = """\ |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 13 | {0, 0, 0} /* sentinel */ |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 14 | }; |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 15 | """ |
Guido van Rossum | f75f80e | 1996-06-17 17:48:30 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 1abfb9c | 1998-04-23 14:39:05 +0000 | [diff] [blame] | 17 | # if __debug__ == 0 (i.e. -O option given), set Py_OptimizeFlag in frozen app. |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 18 | default_entry_point = """ |
Guido van Rossum | b3afce5 | 1997-07-19 21:58:30 +0000 | [diff] [blame] | 19 | int |
Marc-André Lemburg | 64b4f27 | 2002-04-04 16:15:41 +0000 | [diff] [blame] | 20 | main(int argc, char **argv) |
Guido van Rossum | b3afce5 | 1997-07-19 21:58:30 +0000 | [diff] [blame] | 21 | { |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 22 | extern int Py_FrozenMain(int, char **); |
Guido van Rossum | 1abfb9c | 1998-04-23 14:39:05 +0000 | [diff] [blame] | 23 | """ + ((not __debug__ and """ |
| 24 | Py_OptimizeFlag++; |
| 25 | """) or "") + """ |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 26 | PyImport_FrozenModules = _PyImport_FrozenModules; |
| 27 | return Py_FrozenMain(argc, argv); |
Guido van Rossum | b3afce5 | 1997-07-19 21:58:30 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 30 | """ |
| 31 | |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 32 | def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()): |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 33 | if entry_point is None: entry_point = default_entry_point |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 34 | done = [] |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 35 | files = [] |
Guido van Rossum | 5397039 | 2007-06-12 00:28:30 +0000 | [diff] [blame] | 36 | mods = sorted(dict.keys()) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 37 | for mod in mods: |
| 38 | m = dict[mod] |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 39 | mangled = "__".join(mod.split(".")) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 40 | if m.__code__: |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 41 | file = 'M_' + mangled + '.c' |
| 42 | outfp = bkfile.open(base + file, 'w') |
| 43 | files.append(file) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 44 | if debug: |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 45 | print("freezing", mod, "...") |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 46 | str = marshal.dumps(m.__code__) |
| 47 | size = len(str) |
| 48 | if m.__path__: |
| 49 | # Indicate package by negative size |
| 50 | size = -size |
| 51 | done.append((mod, mangled, size)) |
| 52 | writecode(outfp, mangled, str) |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 53 | outfp.close() |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 54 | if debug: |
Guido van Rossum | 96bf7e8 | 2007-02-09 23:27:01 +0000 | [diff] [blame] | 55 | print("generating table of frozen modules") |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 56 | outfp = bkfile.open(base + 'frozen.c', 'w') |
| 57 | for mod, mangled, size in done: |
| 58 | outfp.write('extern unsigned char M_%s[];\n' % mangled) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 59 | outfp.write(header) |
| 60 | for mod, mangled, size in done: |
| 61 | outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size)) |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 62 | outfp.write('\n') |
| 63 | # The following modules have a NULL code pointer, indicating |
| 64 | # that the prozen program should not search for them on the host |
| 65 | # system. Importing them will *always* raise an ImportError. |
| 66 | # The zero value size is never used. |
| 67 | for mod in fail_import: |
| 68 | outfp.write('\t{"%s", NULL, 0},\n' % (mod,)) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 69 | outfp.write(trailer) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 70 | outfp.write(entry_point) |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 71 | outfp.close() |
| 72 | return files |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 74 | |
| 75 | |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 76 | # Write a C initializer for a module containing the frozen python code. |
| 77 | # The array is called M_<mod>. |
| 78 | |
| 79 | def writecode(outfp, mod, str): |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 80 | outfp.write('unsigned char M_%s[] = {' % mod) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 81 | for i in range(0, len(str), 16): |
| 82 | outfp.write('\n\t') |
Guido van Rossum | 5397039 | 2007-06-12 00:28:30 +0000 | [diff] [blame] | 83 | for c in bytes(str[i:i+16]): |
| 84 | outfp.write('%d,' % c) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 85 | outfp.write('\n};\n') |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 86 | |
| 87 | ## def writecode(outfp, mod, str): |
| 88 | ## outfp.write('unsigned char M_%s[%d] = "%s";\n' % (mod, len(str), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 89 | ## '\\"'.join(map(lambda s: repr(s)[1:-1], str.split('"'))))) |