Enable full-static build on Windows

Glib cannot be built statically on Windows because glib, gobject and gio
modules need to perform specific initialization when DLL are loaded and
cleanup when unloaded. Those initializations and cleanups are performed
using the DllMain function which is not called with static builds.

Issue is known for a while and solutions were already proposed but never
merged (see: https://gitlab.gnome.org/GNOME/glib/-/issues/692). Last
patch is from version 2.36.x and since then the
"constructor/destructor" mechanism has been implemented and used in
other part of the system.

This patch takes back the old idea and updates it to the last version of
glib to allow static compilation on Windows.

WARNING: because DllMain doesn't exist anymore in static compilation
mode, there is no easy way of knowing when a Windows thread finishes.
This patch implements a workaround for glib threads created by calling
g_thread_new(), so all glib threads created through glib API will behave
exactly the same way in static and dynamic compilation modes.
Unfortunately, Windows threads created by using CreateThread() or
_beginthread/ex() will not work with glib TLS functions. If users need
absolutely to use a thread NOT created with glib API under Windows and
in static compilation mode, they should not use glib functions within
their thread or they may encounter memory leaks when the thread finishes.

This should not be an issue as users should use exclusively the glib API
to manipulate threads in order to be cross-platform compatible and this
would be very unlikely and cumbersome that they may mix up Windows native
threads API with glib one.

Closes #692
This commit is contained in:
Loic Le Page
2022-01-19 14:19:05 +01:00
parent 2ff2c9eb5b
commit 42c77c7ac7
10 changed files with 194 additions and 27 deletions

View File

@@ -4521,7 +4521,23 @@ gobject_init (void)
_g_signal_init ();
}
#if defined (G_OS_WIN32)
#ifdef G_PLATFORM_WIN32
void gobject_win32_init (void);
void
gobject_win32_init (void)
{
/* May be called more than once in static compilation mode */
static gboolean win32_already_init = FALSE;
if (!win32_already_init)
{
win32_already_init = TRUE;
gobject_init ();
}
}
#ifndef GLIB_STATIC_COMPILATION
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
@@ -4535,7 +4551,7 @@ DllMain (HINSTANCE hinstDLL,
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
gobject_init ();
gobject_win32_init ();
break;
default:
@@ -4546,21 +4562,55 @@ DllMain (HINSTANCE hinstDLL,
return TRUE;
}
#elif defined (G_HAS_CONSTRUCTORS)
#elif defined(G_HAS_CONSTRUCTORS) /* && G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION */
extern void glib_win32_init (void);
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gobject_init_ctor)
#endif
G_DEFINE_CONSTRUCTOR(gobject_init_ctor)
static void
gobject_init_ctor (void)
{
/* When built dynamically, module initialization is done through DllMain
* function which is called when the dynamic library is loaded by the glib
* module. So, in dynamic configuration glib is always initialized BEFORE
* gobject.
*
* When built statically, initialization mechanism relies on hooking
* functions to the CRT section directly at compilation time. As we don't
* control how each compilation unit will be built and in which order, we
* obtain the same kind of issue as the "static initialization order fiasco".
* In this case, we must ensure explicitly that glib is always well
* initialized BEFORE gobject.
*/
glib_win32_init ();
gobject_win32_init ();
}
#else /* G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION && !G_HAS_CONSTRUCTORS */
# error Your platform/compiler is missing constructor support
#endif /* GLIB_STATIC_COMPILATION */
#elif defined(G_HAS_CONSTRUCTORS) /* && !G_PLATFORM_WIN32 */
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gobject_init_ctor)
#endif
G_DEFINE_CONSTRUCTOR (gobject_init_ctor)
static void
gobject_init_ctor (void)
{
gobject_init ();
}
#else
# error Your platform/compiler is missing constructor support
#endif
#else /* !G_PLATFORM_WIN32 && !G_HAS_CONSTRUCTORS */
#error Your platform/compiler is missing constructor support
#endif /* G_PLATFORM_WIN32 */
/**
* g_type_class_add_private: