dist: Add extraInitFuncs option to FreezeTool

Useful for init_libpnmimagetypes in particular
This commit is contained in:
rdb 2023-02-26 11:20:00 +01:00
parent 108b317327
commit 4da2059758

View File

@ -370,6 +370,8 @@ extern PyAPI_FUNC(int) PyImport_ExtendInittab(struct _inittab *newtab);
/* Main program */ /* Main program */
EXTRA_INIT_FUNC_DECLS
int int
Py_FrozenMain(int argc, char **argv) Py_FrozenMain(int argc, char **argv)
{ {
@ -449,6 +451,8 @@ Py_FrozenMain(int argc, char **argv)
PySys_SetArgv(argc, argv); PySys_SetArgv(argc, argv);
#endif #endif
EXTRA_INIT_FUNC_CALLS
n = PyImport_ImportFrozenModule("__main__"); n = PyImport_ImportFrozenModule("__main__");
if (n == 0) if (n == 0)
Py_FatalError("__main__ not frozen"); Py_FatalError("__main__ not frozen");
@ -839,6 +843,12 @@ class Freezer:
# modules. # modules.
self.extras = [] self.extras = []
# This is a list of init functions that must be called after
# Py_Initialize(), but before importing __main__. This is a
# tuple of (return type, name). They should use C calling
# convention.
self.extraInitFuncs = []
# Set this to true if extension modules should be linked in to # Set this to true if extension modules should be linked in to
# the resulting executable. # the resulting executable.
self.linkExtensionModules = False self.linkExtensionModules = False
@ -1711,6 +1721,18 @@ class Freezer:
if compileToExe: if compileToExe:
code = self.frozenMainCode code = self.frozenMainCode
decls = ''
calls = ''
for func in self.extraInitFuncs:
if isinstance(func, str):
func = ('void', func)
decls += f'extern {func[0]} {func[1]}();\n'
calls += f' {func[1]}();\n';
code = code.replace('EXTRA_INIT_FUNC_DECLS', decls)
code = code.replace('EXTRA_INIT_FUNC_CALLS', calls)
if self.platform.startswith('win'): if self.platform.startswith('win'):
code += self.frozenDllMainCode code += self.frozenDllMainCode
initCode = self.mainInitCode % { initCode = self.mainInitCode % {