Get Python compiler working

It turns out we needed a file length field.
This commit is contained in:
Justine Tunney 2021-08-17 12:13:41 -07:00
parent 38aece42ba
commit cabb0a7ede
6 changed files with 6370 additions and 41 deletions

View file

@ -26,8 +26,13 @@
* @asyncsignalsafe
*/
int __zipos_fstat(const struct ZiposHandle *h, struct stat *st) {
ZTRACE("__zipos_fstat(%`'.*s)",
ZIP_CFILE_NAMESIZE(__zipos_get()->map + h->cfile),
ZIP_CFILE_NAME(__zipos_get()->map + h->cfile));
return __zipos_stat_impl(__zipos_get(), h->cfile, st);
int rc;
if (!(rc = __zipos_stat_impl(__zipos_get(), h->cfile, st))) {
ZTRACE("__zipos_fstat(%`'.*s) → %ld",
ZIP_CFILE_NAMESIZE(__zipos_get()->map + h->cfile),
ZIP_CFILE_NAME(__zipos_get()->map + h->cfile), st->st_size);
return 0;
} else {
return rc;
}
}

View file

@ -466,7 +466,7 @@ search_for_exec_prefix(wchar_t *argv0_path, wchar_t *home,
static void
calculate_path(void)
{
#if 1
#if 0
static wchar_t delimiter[2] = {DELIM, '\0'};
static wchar_t separator[2] = {SEP, '\0'};
/* ignore PYTHONPATH/PYTHONHOME for now */
@ -543,9 +543,9 @@ calculate_path(void)
wcsncpy(prefix,
L"third_party/python/Lib",
MAXPATHLEN);
/* wcsncpy(prefix, */
/* L"zip!third_party/python/Lib/", */
/* MAXPATHLEN); */
wcsncpy(prefix,
L"zip!.python",
MAXPATHLEN);
/* Avoid absolute path for exec_prefix */
wcsncpy(exec_prefix, L"build/lib.linux-x86_64-3.6", MAXPATHLEN);
wcsncpy(package_path, L"Lib/site-packages", MAXPATHLEN);
@ -610,7 +610,7 @@ calculate_path(void)
// printf("%ls\n", buf);
#else
module_search_path = L"zip!.python/";
module_search_path = L"third_party/python/Lib/";
/* module_search_path = L"third_party/python/Lib/"; */
#endif
}

View file

@ -1556,6 +1556,9 @@ static int _PyCodecRegistry_Init(void)
interp->codec_error_registry == NULL)
Py_FatalError("can't initialize codec registry");
/*
* XXX: terrible design!
*/
mod = PyImport_ImportModuleNoBlock("encodings");
if (mod == NULL) {
return -1;

View file

@ -605,6 +605,8 @@ ARTIFACTS = set()
def MakeDirs(d):
if d + "/" not in ARTIFACTS:
if os.path.basename(d) != '__pycache__':
return
ARTIFACTS.add(d + "/")
if d == "third_party/python/Lib":
print()
@ -643,24 +645,10 @@ for f in SAUCES:
"\t\to/$(MODE)/%s\n"
"\t@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) $(OUTPUT_OPTION) $<") % (c, c))
ARTIFACTS.add(c)
elif os.path.isfile(f):
d = os.path.dirname(f)
MakeDirs(d)
b = os.path.basename(f)
c = "%s/%s" % (d, b)
print()
print(("o/$(MODE)/%s:\t\\\n"
"\t\t%s\t\\\n"
"\t\to/$(MODE)/%s/\n"
"\t@cp -f $< $@") % (c, f, d))
print(("o/$(MODE)/%s.zip.o:\t\\\n"
"\t\to/$(MODE)/%s\n"
"\t@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) $(OUTPUT_OPTION) $<") % (c, c))
ARTIFACTS.add(c)
else:
elif os.path.basename(f) == '__pycache__':
MakeDirs(f)
ARTIFACTS.add(f + "/")
print()
print("THIRD_PARTY_PYTHON_STDLIB_PY_OBJS =\t\\")
print("THIRD_PARTY_PYTHON_STDLIB_PYC_OBJS =\t\\")
print("\t" + "\t\\\n\t".join(sorted("o/$(MODE)/%s.zip.o" % (x) for x in ARTIFACTS)))

View file

@ -87,6 +87,21 @@ char *inpath;
char *outpath;
bool optimize;
char *
StripComponents(const char *path, int n)
{
const char *p;
while (n-- > 0) {
for (p = path; *p; ++p) {
if (*p == '/') {
path = p + 1;
break;
}
}
}
return (char *)path;
}
void
GetOpts(int argc, char *argv[])
{
@ -131,7 +146,7 @@ main(int argc, char *argv[])
ssize_t rc;
size_t i, n;
struct stat st;
char *s, *p, m[8];
char *s, *p, m[12];
PyObject *code, *marshalled;
GetOpts(argc, argv);
marshalled = 0;
@ -144,7 +159,7 @@ main(int argc, char *argv[])
Py_FrozenFlag++;
Py_SetProgramName(gc(utf8toutf32(argv[0], -1, 0)));
_Py_InitializeEx_Private(1, 0);
name = gc(xasprintf("zip!%s", inpath));
name = gc(xjoinpaths(".python", StripComponents(inpath, 3)));
code = Py_CompileStringExFlags(p, name, Py_file_input, NULL, 0);
if (!code) goto error;
marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
@ -157,6 +172,7 @@ main(int argc, char *argv[])
WRITE16LE(m+0, 3379); /* Python 3.6rc1 */
WRITE16LE(m+2, READ16LE("\r\n"));
WRITE32LE(m+4, st.st_mtim.tv_sec); /* tsk tsk y2038 */
WRITE32LE(m+8, n);
CHECK_EQ(sizeof(m), write(fd, m, sizeof(m)));
for (i = 0; i < n; i += rc) {
CHECK_NE(-1, (rc = write(fd, p + i, n - i)));

File diff suppressed because it is too large Load diff