| 1 |
/* |
| 2 |
* Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
* |
| 4 |
* Distributed under terms of the GPL3 license. |
| 5 |
*/ |
| 6 |
|
| 7 |
#pragma once |
| 8 |
#define PY_SSIZE_T_CLEAN |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdbool.h> |
| 11 |
#include <time.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <stdarg.h> |
| 14 |
#ifdef _WIN32 |
| 15 |
#include <string.h> |
| 16 |
#define PATH_MAX MAX_PATH |
| 17 |
#else |
| 18 |
#include <strings.h> |
| 19 |
#endif |
| 20 |
#include <errno.h> |
| 21 |
#include <Python.h> |
| 22 |
#ifdef __APPLE__ |
| 23 |
#include <os/log.h> |
| 24 |
#endif |
| 25 |
#include <bypy-freeze.h> |
| 26 |
|
| 27 |
|
| 28 |
static void |
| 29 |
pre_initialize_interpreter(bool is_gui_app) { |
| 30 |
bypy_pre_initialize_interpreter(is_gui_app); |
| 31 |
} |
| 32 |
|
| 33 |
#define decode_char_buf(src, dest) { \ |
| 34 |
size_t tsz; \ |
| 35 |
wchar_t* t__ = Py_DecodeLocale(src, &tsz); \ |
| 36 |
if (!t__) fatal("Failed to decode path: %s", src); \ |
| 37 |
if (tsz > sizeof(dest) - 1) tsz = sizeof(dest) - 1; \ |
| 38 |
memcpy(dest, t__, tsz * sizeof(wchar_t)); \ |
| 39 |
dest[tsz] = 0; \ |
| 40 |
PyMem_RawFree(t__); \ |
| 41 |
} |
| 42 |
|
| 43 |
#define MAX_SYS_PATHS 3 |
| 44 |
|
| 45 |
typedef struct { |
| 46 |
int argc; |
| 47 |
wchar_t exe_path[PATH_MAX], python_home_path[PATH_MAX], python_lib_path[PATH_MAX]; |
| 48 |
wchar_t extensions_path[PATH_MAX], resources_path[PATH_MAX], executables_path[PATH_MAX]; |
| 49 |
#ifdef __APPLE__ |
| 50 |
wchar_t bundle_resource_path[PATH_MAX], frameworks_path[PATH_MAX]; |
| 51 |
#elif defined(_WIN32) |
| 52 |
wchar_t app_dir[PATH_MAX]; |
| 53 |
#endif |
| 54 |
const wchar_t *basename, *module, *function; |
| 55 |
#ifdef _WIN32 |
| 56 |
wchar_t* const *argv; |
| 57 |
#else |
| 58 |
char* const *argv; |
| 59 |
#endif |
| 60 |
} InterpreterData; |
| 61 |
|
| 62 |
static InterpreterData interpreter_data = {0}; |
| 63 |
|
| 64 |
static void |
| 65 |
run_interpreter() { |
| 66 |
bypy_initialize_interpreter( |
| 67 |
interpreter_data.exe_path, interpreter_data.python_home_path, L"site", interpreter_data.extensions_path, |
| 68 |
interpreter_data.argc, interpreter_data.argv); |
| 69 |
set_sys_bool("gui_app", use_os_log); |
| 70 |
set_sys_bool("frozen", true); |
| 71 |
set_sys_string("calibre_basename", interpreter_data.basename); |
| 72 |
set_sys_string("calibre_module", interpreter_data.module); |
| 73 |
set_sys_string("calibre_function", interpreter_data.function); |
| 74 |
set_sys_string("extensions_location", interpreter_data.extensions_path); |
| 75 |
set_sys_string("resources_location", interpreter_data.resources_path); |
| 76 |
set_sys_string("executables_location", interpreter_data.executables_path); |
| 77 |
#ifdef __APPLE__ |
| 78 |
set_sys_string("resourcepath", interpreter_data.bundle_resource_path); |
| 79 |
set_sys_string("frameworks_dir", interpreter_data.frameworks_path); |
| 80 |
set_sys_bool("new_app_bundle", true); |
| 81 |
#elif defined(_WIN32) |
| 82 |
set_sys_string("app_dir", interpreter_data.app_dir); |
| 83 |
set_sys_bool("new_app_layout", true); |
| 84 |
#else |
| 85 |
set_sys_string("frozen_path", interpreter_data.executables_path); |
| 86 |
#endif |
| 87 |
|
| 88 |
int ret = bypy_run_interpreter(); |
| 89 |
exit(ret); |
| 90 |
} |