mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-25 09:48:09 +02:00
Get the new bypy based freezing process working with linux builds
This commit is contained in:
132
launcher.c
132
launcher.c
@@ -21,8 +21,8 @@
|
||||
#endif
|
||||
#include <Python.h>
|
||||
#include <wchar.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MAX_ARGC 1024
|
||||
#ifndef KITTY_LIB_PATH
|
||||
#define KITTY_LIB_PATH "../.."
|
||||
#endif
|
||||
@@ -41,78 +41,83 @@ safe_realpath(const char* src, char *buf, size_t buf_sz) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline void
|
||||
set_xoptions(const wchar_t *exe_dir, const char *lc_ctype, bool from_source) {
|
||||
static inline bool
|
||||
set_xoptions(const char *exe_dir_c, const char *lc_ctype, bool from_source) {
|
||||
wchar_t *exe_dir = Py_DecodeLocale(exe_dir_c, NULL);
|
||||
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir: %s\n", exe_dir_c); return false; }
|
||||
wchar_t buf[PATH_MAX+1] = {0};
|
||||
swprintf(buf, PATH_MAX, L"bundle_exe_dir=%ls", exe_dir);
|
||||
PySys_AddXOption(buf);
|
||||
PyMem_RawFree(exe_dir);
|
||||
if (from_source) PySys_AddXOption(L"kitty_from_source=1");
|
||||
if (lc_ctype) {
|
||||
swprintf(buf, PATH_MAX, L"lc_ctype_before_python=%s", lc_ctype);
|
||||
PySys_AddXOption(buf);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef FOR_BUNDLE
|
||||
static int
|
||||
run_embedded(const char* exe_dir_, const char *libpath, int argc, wchar_t **argv, const char *lc_ctype) {
|
||||
int num;
|
||||
Py_NoSiteFlag = 1;
|
||||
Py_FrozenFlag = 1;
|
||||
Py_IgnoreEnvironmentFlag = 1;
|
||||
Py_DontWriteBytecodeFlag = 1;
|
||||
Py_NoUserSiteDirectory = 1;
|
||||
Py_IsolatedFlag = 1;
|
||||
Py_SetProgramName(L"kitty");
|
||||
typedef struct {
|
||||
const char *exe, *exe_dir, *lc_ctype, *lib_dir;
|
||||
char **argv;
|
||||
int argc;
|
||||
} RunData;
|
||||
|
||||
int ret = 1;
|
||||
wchar_t *exe_dir = Py_DecodeLocale(exe_dir_, NULL);
|
||||
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir\n"); return 1; }
|
||||
wchar_t stdlib[PATH_MAX+1] = {0};
|
||||
#ifdef FOR_BUNDLE
|
||||
#include <bypy-freeze.h>
|
||||
|
||||
static int
|
||||
run_embedded(const RunData run_data) {
|
||||
bypy_pre_initialize_interpreter(false);
|
||||
wchar_t extensions_dir[PATH_MAX+1] = {0}, python_home[PATH_MAX+1] = {0};
|
||||
#ifdef __APPLE__
|
||||
const char *python_relpath = "../Resources/Python/lib";
|
||||
#else
|
||||
const char *python_relpath = "../" KITTY_LIB_DIR_NAME;
|
||||
#endif
|
||||
num = swprintf(stdlib, PATH_MAX, L"%ls/%s/python%s:%ls/%s/python%s/lib-dynload:%ls/%s/python%s/site-packages",
|
||||
exe_dir, python_relpath, PYVER,
|
||||
exe_dir, python_relpath, PYVER,
|
||||
exe_dir, python_relpath, PYVER
|
||||
);
|
||||
if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to python stdlib\n"); return 1; }
|
||||
Py_SetPath(stdlib);
|
||||
Py_Initialize();
|
||||
set_xoptions(exe_dir, lc_ctype, false);
|
||||
PyMem_RawFree(exe_dir);
|
||||
PySys_SetArgvEx(argc - 1, argv + 1, 0);
|
||||
PySys_SetObject("frozen", Py_True);
|
||||
PyObject *kitty = PyUnicode_FromString(libpath);
|
||||
if (kitty == NULL) { fprintf(stderr, "Failed to allocate python kitty lib object\n"); goto end; }
|
||||
PyObject *runpy = PyImport_ImportModule("runpy");
|
||||
if (runpy == NULL) { PyErr_Print(); fprintf(stderr, "Unable to import runpy\n"); Py_CLEAR(kitty); goto end; }
|
||||
PyObject *run_name = PyUnicode_FromString("__main__");
|
||||
if (run_name == NULL) { fprintf(stderr, "Failed to allocate run_name\n"); goto end; }
|
||||
PyObject *res = PyObject_CallMethod(runpy, "run_path", "OOO", kitty, Py_None, run_name);
|
||||
Py_CLEAR(runpy); Py_CLEAR(kitty); Py_CLEAR(run_name);
|
||||
if (res == NULL) PyErr_Print();
|
||||
else { ret = 0; Py_CLEAR(res); }
|
||||
end:
|
||||
if (Py_FinalizeEx() < 0) ret = 120;
|
||||
return ret;
|
||||
int num = swprintf(extensions_dir, PATH_MAX, L"%s/%s/kitty-extensions", run_data.exe_dir, python_relpath);
|
||||
if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to extensions_dir: %s/%s\n", run_data.exe_dir, python_relpath); return 1; }
|
||||
num = swprintf(python_home, PATH_MAX, L"%s/%s/python%s", run_data.exe_dir, python_relpath, PYVER);
|
||||
if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to python home: %s/%s\n", run_data.exe_dir, python_relpath); return 1; }
|
||||
bypy_initialize_interpreter(L"kitty", python_home, L"kitty_main", extensions_dir, run_data.argc, run_data.argv);
|
||||
if (!set_xoptions(run_data.exe_dir, run_data.lc_ctype, false)) return 1;
|
||||
set_sys_bool("frozen", true);
|
||||
set_sys_string("kitty_extensions_dir", extensions_dir);
|
||||
return bypy_run_interpreter();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static int
|
||||
run_embedded(const char* exe_dir_, const char *libpath, int argc, wchar_t **argv, const char *lc_ctype) {
|
||||
(void)libpath;
|
||||
wchar_t *exe_dir = Py_DecodeLocale(exe_dir_, NULL);
|
||||
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir: %s\n", exe_dir_); return 1; }
|
||||
free_argv(wchar_t **argv) {
|
||||
wchar_t **p = argv;
|
||||
while (*p) { PyMem_RawFree(*p); p++; }
|
||||
free(argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
run_embedded(const RunData run_data) {
|
||||
bool from_source = false;
|
||||
#ifdef FROM_SOURCE
|
||||
from_source = true;
|
||||
#endif
|
||||
set_xoptions(exe_dir, lc_ctype, from_source);
|
||||
PyMem_RawFree(exe_dir);
|
||||
return Py_Main(argc, argv);
|
||||
if (!set_xoptions(run_data.exe_dir, run_data.lc_ctype, from_source)) return 1;
|
||||
int argc = run_data.argc + 1;
|
||||
wchar_t **argv = calloc(argc, sizeof(wchar_t*));
|
||||
if (!argv) { fprintf(stderr, "Out of memory creating argv\n"); return 1; }
|
||||
argv[0] = Py_DecodeLocale(run_data.exe, NULL);
|
||||
if (!argv[0]) { fprintf(stderr, "Failed to decode path to exe\n"); return free_argv(argv); }
|
||||
argv[1] = Py_DecodeLocale(run_data.lib_dir, NULL);
|
||||
if (!argv[1]) { fprintf(stderr, "Failed to decode path to lib_dir\n"); return free_argv(argv); }
|
||||
for (int i=1; i < run_data.argc; i++) {
|
||||
argv[i+1] = Py_DecodeLocale(run_data.argv[i], NULL);
|
||||
if (!argv[i+1]) { fprintf(stderr, "Failed to decode the command line argument: %s\n", run_data.argv[i]); return free_argv(argv); }
|
||||
}
|
||||
int ret = Py_Main(argc, argv);
|
||||
// we cannot free argv properly as Py_Main odifies it
|
||||
free(argv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -180,41 +185,26 @@ read_exe_path(char *exe, size_t buf_sz) {
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char exe[PATH_MAX+1] = {0};
|
||||
char exe_dir_buf[PATH_MAX+1] = {0};
|
||||
const char *lc_ctype = NULL;
|
||||
#ifdef __APPLE__
|
||||
lc_ctype = getenv("LC_CTYPE");
|
||||
#endif
|
||||
if (!read_exe_path(exe, sizeof(exe))) return 1;
|
||||
|
||||
char *exe_dir = dirname(exe);
|
||||
int num, num_args, i, ret=0;
|
||||
strncpy(exe_dir_buf, exe, sizeof(exe_dir_buf));
|
||||
char *exe_dir = dirname(exe_dir_buf);
|
||||
int num, ret=0;
|
||||
char lib[PATH_MAX+1] = {0};
|
||||
char *final_argv[MAX_ARGC + 1] = {0};
|
||||
wchar_t *argvw[MAX_ARGC + 1] = {0};
|
||||
num = snprintf(lib, PATH_MAX, "%s/%s", exe_dir, KITTY_LIB_PATH);
|
||||
|
||||
if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to kitty lib\n"); return 1; }
|
||||
final_argv[0] = exe;
|
||||
final_argv[1] = lib;
|
||||
for (i = 1, num_args=2; i < argc && i + 1 <= MAX_ARGC; i++) {
|
||||
final_argv[i+1] = argv[i];
|
||||
num_args++;
|
||||
}
|
||||
#if PY_VERSION_HEX >= 0x03070000
|
||||
// Always use UTF-8 mode, see https://github.com/kovidgoyal/kitty/issues/924
|
||||
Py_UTF8Mode = 1;
|
||||
#endif
|
||||
for (i = 0; i < num_args; i++) {
|
||||
argvw[i] = Py_DecodeLocale(final_argv[i], NULL);
|
||||
if (argvw[i] == NULL) {
|
||||
fprintf(stderr, "Fatal error: cannot decode argv[%d]\n", i);
|
||||
ret = 1; goto end;
|
||||
}
|
||||
}
|
||||
if (lc_ctype) lc_ctype = strdup(lc_ctype);
|
||||
ret = run_embedded(exe_dir, lib, num_args, argvw, lc_ctype);
|
||||
RunData run_data = {.exe = exe, .exe_dir = exe_dir, .lib_dir = lib, .argc = argc, .argv = argv, .lc_ctype = lc_ctype};
|
||||
ret = run_embedded(run_data);
|
||||
if (lc_ctype) free((void*)lc_ctype);
|
||||
end:
|
||||
for (i = 0; i < num_args; i++) { if(argvw[i]) PyMem_RawFree(argvw[i]); }
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user