blob: aecb1232af1674f74d44007c20da36e8675a0dbd [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 }
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
Eric Snow32439d62015-05-02 19:15:18 -060085 if (strstr(inpath, "_external") != NULL) {
86 is_bootstrap = 0;
87 }
88
89 code_name = is_bootstrap ?
90 "<frozen importlib._bootstrap>" :
91 "<frozen importlib._bootstrap_external>";
92 code = Py_CompileStringExFlags(text, code_name, Py_file_input, NULL, 0);
Antoine Pitroue67f48c2012-06-19 22:29:35 +020093 if (code == NULL)
94 goto error;
Christian Heimes43d82df2013-07-21 23:05:04 +020095 free(text);
96 text = NULL;
97
Antoine Pitroue67f48c2012-06-19 22:29:35 +020098 marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
Christian Heimes43d82df2013-07-21 23:05:04 +020099 Py_CLEAR(code);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200100 if (marshalled == NULL)
101 goto error;
102
103 assert(PyBytes_CheckExact(marshalled));
Antoine Pitrou0ab5cf92012-06-25 17:32:43 +0200104 data = (unsigned char *) PyBytes_AS_STRING(marshalled);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200105 data_size = PyBytes_GET_SIZE(marshalled);
106
Martin v. Löwis96d97ec2012-07-28 20:46:52 +0200107 /* Open the file in text mode. The hg checkout should be using the eol extension,
Martin v. Löwisee365ac2012-07-28 21:55:20 +0200108 which in turn should cause the EOL style match the C library's text mode */
109 outfile = fopen(outpath, "w");
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200110 if (outfile == NULL) {
111 fprintf(stderr, "cannot open '%s' for writing\n", outpath);
Christian Heimes43d82df2013-07-21 23:05:04 +0200112 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200113 }
114 fprintf(outfile, "%s\n", header);
Eric Snow32439d62015-05-02 19:15:18 -0600115 if (is_bootstrap)
116 fprintf(outfile, "const unsigned char _Py_M__importlib[] = {\n");
117 else
118 fprintf(outfile,
119 "const unsigned char _Py_M__importlib_external[] = {\n");
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200120 for (n = 0; n < data_size; n += 16) {
121 size_t i, end = Py_MIN(n + 16, data_size);
122 fprintf(outfile, " ");
123 for (i = n; i < end; i++) {
Antoine Pitrou0ab5cf92012-06-25 17:32:43 +0200124 fprintf(outfile, "%d,", (unsigned int) data[i]);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200125 }
126 fprintf(outfile, "\n");
127 }
128 fprintf(outfile, "};\n");
129
Christian Heimes43d82df2013-07-21 23:05:04 +0200130 Py_CLEAR(marshalled);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200131
132 Py_Finalize();
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200133 if (outfile) {
134 if (ferror(outfile)) {
135 fprintf(stderr, "error when writing to '%s'\n", outpath);
Christian Heimes43d82df2013-07-21 23:05:04 +0200136 goto error;
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200137 }
138 fclose(outfile);
139 }
140 return 0;
141
142error:
143 PyErr_Print();
144 Py_Finalize();
145 if (infile)
146 fclose(infile);
147 if (outfile)
148 fclose(outfile);
Christian Heimes43d82df2013-07-21 23:05:04 +0200149 if (text)
150 free(text);
151 if (marshalled)
152 Py_DECREF(marshalled);
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200153 return 1;
154}