blob: 1069966a1876ed61a195aa2fefaa56340a483d95 [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
Antoine Pitroue67f48c2012-06-19 22:29:35 +020015/* To avoid a circular dependency on frozen.o, we create our own structure
16 of frozen modules instead, left deliberately blank so as to avoid
17 unintentional import of a stale version of _frozen_importlib. */
18
Serhiy Storchakab48af342015-02-26 15:27:57 +020019static const struct _frozen _PyImport_FrozenModules[] = {
Antoine Pitroue67f48c2012-06-19 22:29:35 +020020 {0, 0, 0} /* sentinel */
21};
22
Martin v. Löwisee365ac2012-07-28 21:55:20 +020023#ifndef MS_WINDOWS
24/* On Windows, this links with the regular pythonXY.dll, so this variable comes
25 from frozen.obj. In the Makefile, frozen.o is not linked into this executable,
26 so we define the variable here. */
Benjamin Peterson7701e6e2013-03-13 14:06:39 -050027const struct _frozen *PyImport_FrozenModules;
Martin v. Löwisee365ac2012-07-28 21:55:20 +020028#endif
29
Berker Peksagda8cef42014-11-24 23:26:08 +020030const char header[] = "/* Auto-generated by Programs/_freeze_importlib.c */";
Antoine Pitroue67f48c2012-06-19 22:29:35 +020031
32int
33main(int argc, char *argv[])
34{
Eric Snow32439d62015-05-02 19:15:18 -060035 char *inpath, *outpath, *code_name;
Christian Heimes43d82df2013-07-21 23:05:04 +020036 FILE *infile = NULL, *outfile = NULL;
Victor Stinnere134a7f2015-03-30 10:09:31 +020037 struct _Py_stat_struct status;
Antoine Pitroue67f48c2012-06-19 22:29:35 +020038 size_t text_size, data_size, n;
Christian Heimes43d82df2013-07-21 23:05:04 +020039 char *text = NULL;
Antoine Pitrou0ab5cf92012-06-25 17:32:43 +020040 unsigned char *data;
Christian Heimes43d82df2013-07-21 23:05:04 +020041 PyObject *code = NULL, *marshalled = NULL;
Eric Snow32439d62015-05-02 19:15:18 -060042 int is_bootstrap = 1;
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 }
Steve Dowera4391912016-09-06 19:09:15 -070061 text_size = (size_t)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++;
Ned Deily17cfc862016-08-19 22:12:06 -040080 Py_FrozenFlag++;
Antoine Pitroue67f48c2012-06-19 22:29:35 +020081
82 Py_SetProgramName(L"./_freeze_importlib");
83 /* Don't install importlib, since it could execute outdated bytecode. */
84 _Py_InitializeEx_Private(1, 0);
85
Eric Snow32439d62015-05-02 19:15:18 -060086 if (strstr(inpath, "_external") != NULL) {
87 is_bootstrap = 0;
88 }
89
90 code_name = is_bootstrap ?
91 "<frozen importlib._bootstrap>" :
92 "<frozen importlib._bootstrap_external>";
93 code = Py_CompileStringExFlags(text, code_name, Py_file_input, NULL, 0);
Antoine Pitroue67f48c2012-06-19 22:29:35 +020094 if (code == NULL)
95 goto error;
Christian Heimes43d82df2013-07-21 23:05:04 +020096 free(text);
97 text = NULL;
98
Antoine Pitroue67f48c2012-06-19 22:29:35 +020099 marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
Christian Heimes43d82df2013-07-21 23:05:04 +0200100 Py_CLEAR(code);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200101 if (marshalled == NULL)
102 goto error;
103
104 assert(PyBytes_CheckExact(marshalled));
Antoine Pitrou0ab5cf92012-06-25 17:32:43 +0200105 data = (unsigned char *) PyBytes_AS_STRING(marshalled);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200106 data_size = PyBytes_GET_SIZE(marshalled);
107
Martin v. Löwis96d97ec2012-07-28 20:46:52 +0200108 /* Open the file in text mode. The hg checkout should be using the eol extension,
Martin v. Löwisee365ac2012-07-28 21:55:20 +0200109 which in turn should cause the EOL style match the C library's text mode */
110 outfile = fopen(outpath, "w");
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200111 if (outfile == NULL) {
112 fprintf(stderr, "cannot open '%s' for writing\n", outpath);
Christian Heimes43d82df2013-07-21 23:05:04 +0200113 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200114 }
115 fprintf(outfile, "%s\n", header);
Eric Snow32439d62015-05-02 19:15:18 -0600116 if (is_bootstrap)
117 fprintf(outfile, "const unsigned char _Py_M__importlib[] = {\n");
118 else
119 fprintf(outfile,
120 "const unsigned char _Py_M__importlib_external[] = {\n");
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200121 for (n = 0; n < data_size; n += 16) {
122 size_t i, end = Py_MIN(n + 16, data_size);
123 fprintf(outfile, " ");
124 for (i = n; i < end; i++) {
Antoine Pitrou0ab5cf92012-06-25 17:32:43 +0200125 fprintf(outfile, "%d,", (unsigned int) data[i]);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200126 }
127 fprintf(outfile, "\n");
128 }
129 fprintf(outfile, "};\n");
130
Christian Heimes43d82df2013-07-21 23:05:04 +0200131 Py_CLEAR(marshalled);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200132
133 Py_Finalize();
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200134 if (outfile) {
135 if (ferror(outfile)) {
136 fprintf(stderr, "error when writing to '%s'\n", outpath);
Christian Heimes43d82df2013-07-21 23:05:04 +0200137 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200138 }
139 fclose(outfile);
140 }
141 return 0;
142
143error:
144 PyErr_Print();
145 Py_Finalize();
146 if (infile)
147 fclose(infile);
148 if (outfile)
149 fclose(outfile);
Christian Heimes43d82df2013-07-21 23:05:04 +0200150 if (text)
151 free(text);
152 if (marshalled)
153 Py_DECREF(marshalled);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200154 return 1;
155}