blob: b72fcf486fc6c256e82ba5d98605364c3725a4a4 [file] [log] [blame]
Antoine Pitroue67f48c2012-06-19 22:29:35 +02001/* This is built as a stand-alone executable by the Makefile, and helps turn
2 Lib/importlib/_bootstrap.py into a frozen module in Python/importlib.h
3*/
4
5#include <Python.h>
6#include <marshal.h>
7
8#include <stdio.h>
9#include <sys/types.h>
10#include <sys/stat.h>
Martin v. Löwis96d97ec2012-07-28 20:46:52 +020011#ifndef MS_WINDOWS
Antoine Pitroue67f48c2012-06-19 22:29:35 +020012#include <unistd.h>
Martin v. Löwis96d97ec2012-07-28 20:46:52 +020013#endif
Antoine Pitroue67f48c2012-06-19 22:29:35 +020014
15
16/* To avoid a circular dependency on frozen.o, we create our own structure
17 of frozen modules instead, left deliberately blank so as to avoid
18 unintentional import of a stale version of _frozen_importlib. */
19
Serhiy Storchakab48af342015-02-26 15:27:57 +020020static const struct _frozen _PyImport_FrozenModules[] = {
Antoine Pitroue67f48c2012-06-19 22:29:35 +020021 {0, 0, 0} /* sentinel */
22};
23
Martin v. Löwisee365ac2012-07-28 21:55:20 +020024#ifndef MS_WINDOWS
25/* On Windows, this links with the regular pythonXY.dll, so this variable comes
26 from frozen.obj. In the Makefile, frozen.o is not linked into this executable,
27 so we define the variable here. */
Benjamin Peterson7701e6e2013-03-13 14:06:39 -050028const struct _frozen *PyImport_FrozenModules;
Martin v. Löwisee365ac2012-07-28 21:55:20 +020029#endif
30
Berker Peksagda8cef42014-11-24 23:26:08 +020031const char header[] = "/* Auto-generated by Programs/_freeze_importlib.c */";
Antoine Pitroue67f48c2012-06-19 22:29:35 +020032
33int
34main(int argc, char *argv[])
35{
36 char *inpath, *outpath;
Christian Heimes43d82df2013-07-21 23:05:04 +020037 FILE *infile = NULL, *outfile = NULL;
Victor Stinnere134a7f2015-03-30 10:09:31 +020038 struct _Py_stat_struct status;
Antoine Pitroue67f48c2012-06-19 22:29:35 +020039 size_t text_size, data_size, n;
Christian Heimes43d82df2013-07-21 23:05:04 +020040 char *text = NULL;
Antoine Pitrou0ab5cf92012-06-25 17:32:43 +020041 unsigned char *data;
Christian Heimes43d82df2013-07-21 23:05:04 +020042 PyObject *code = NULL, *marshalled = NULL;
Antoine Pitroue67f48c2012-06-19 22:29:35 +020043
Martin v. Löwis96d97ec2012-07-28 20:46:52 +020044 PyImport_FrozenModules = _PyImport_FrozenModules;
45
Antoine Pitroue67f48c2012-06-19 22:29:35 +020046 if (argc != 3) {
47 fprintf(stderr, "need to specify input and output paths\n");
48 return 2;
49 }
50 inpath = argv[1];
51 outpath = argv[2];
52 infile = fopen(inpath, "rb");
53 if (infile == NULL) {
54 fprintf(stderr, "cannot open '%s' for reading\n", inpath);
Christian Heimes43d82df2013-07-21 23:05:04 +020055 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +020056 }
Victor Stinnere134a7f2015-03-30 10:09:31 +020057 if (_Py_fstat_noraise(fileno(infile), &status)) {
Antoine Pitroue67f48c2012-06-19 22:29:35 +020058 fprintf(stderr, "cannot fstat '%s'\n", inpath);
Christian Heimes43d82df2013-07-21 23:05:04 +020059 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +020060 }
Victor Stinnere134a7f2015-03-30 10:09:31 +020061 text_size = status.st_size;
Antoine Pitroue67f48c2012-06-19 22:29:35 +020062 text = (char *) malloc(text_size + 1);
63 if (text == NULL) {
Antoine Pitroue67f48c2012-06-19 22:29:35 +020064 fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
Christian Heimes43d82df2013-07-21 23:05:04 +020065 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +020066 }
67 n = fread(text, 1, text_size, infile);
68 fclose(infile);
69 infile = NULL;
70 if (n < text_size) {
71 fprintf(stderr, "read too short: got %ld instead of %ld bytes\n",
72 (long) n, (long) text_size);
Christian Heimes43d82df2013-07-21 23:05:04 +020073 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +020074 }
75 text[text_size] = '\0';
76
77 Py_NoUserSiteDirectory++;
78 Py_NoSiteFlag++;
79 Py_IgnoreEnvironmentFlag++;
80
81 Py_SetProgramName(L"./_freeze_importlib");
82 /* Don't install importlib, since it could execute outdated bytecode. */
83 _Py_InitializeEx_Private(1, 0);
84
85 code = Py_CompileStringExFlags(text, "<frozen importlib._bootstrap>",
86 Py_file_input, NULL, 0);
87 if (code == NULL)
88 goto error;
Christian Heimes43d82df2013-07-21 23:05:04 +020089 free(text);
90 text = NULL;
91
Antoine Pitroue67f48c2012-06-19 22:29:35 +020092 marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
Christian Heimes43d82df2013-07-21 23:05:04 +020093 Py_CLEAR(code);
Antoine Pitroue67f48c2012-06-19 22:29:35 +020094 if (marshalled == NULL)
95 goto error;
96
97 assert(PyBytes_CheckExact(marshalled));
Antoine Pitrou0ab5cf92012-06-25 17:32:43 +020098 data = (unsigned char *) PyBytes_AS_STRING(marshalled);
Antoine Pitroue67f48c2012-06-19 22:29:35 +020099 data_size = PyBytes_GET_SIZE(marshalled);
100
Martin v. Löwis96d97ec2012-07-28 20:46:52 +0200101 /* Open the file in text mode. The hg checkout should be using the eol extension,
Martin v. Löwisee365ac2012-07-28 21:55:20 +0200102 which in turn should cause the EOL style match the C library's text mode */
103 outfile = fopen(outpath, "w");
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200104 if (outfile == NULL) {
105 fprintf(stderr, "cannot open '%s' for writing\n", outpath);
Christian Heimes43d82df2013-07-21 23:05:04 +0200106 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200107 }
108 fprintf(outfile, "%s\n", header);
Benjamin Peterson7701e6e2013-03-13 14:06:39 -0500109 fprintf(outfile, "const unsigned char _Py_M__importlib[] = {\n");
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200110 for (n = 0; n < data_size; n += 16) {
111 size_t i, end = Py_MIN(n + 16, data_size);
112 fprintf(outfile, " ");
113 for (i = n; i < end; i++) {
Antoine Pitrou0ab5cf92012-06-25 17:32:43 +0200114 fprintf(outfile, "%d,", (unsigned int) data[i]);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200115 }
116 fprintf(outfile, "\n");
117 }
118 fprintf(outfile, "};\n");
119
Christian Heimes43d82df2013-07-21 23:05:04 +0200120 Py_CLEAR(marshalled);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200121
122 Py_Finalize();
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200123 if (outfile) {
124 if (ferror(outfile)) {
125 fprintf(stderr, "error when writing to '%s'\n", outpath);
Christian Heimes43d82df2013-07-21 23:05:04 +0200126 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200127 }
128 fclose(outfile);
129 }
130 return 0;
131
132error:
133 PyErr_Print();
134 Py_Finalize();
135 if (infile)
136 fclose(infile);
137 if (outfile)
138 fclose(outfile);
Christian Heimes43d82df2013-07-21 23:05:04 +0200139 if (text)
140 free(text);
141 if (marshalled)
142 Py_DECREF(marshalled);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200143 return 1;
144}