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

@@ -68,6 +68,10 @@
#include <AvailabilityMacros.h>
#endif
#define __GLIB_H_INSIDE__
#include "gconstructor.h"
#undef __GLIB_H_INSIDE__
/**
* SECTION:giomodule
* @short_description: Loadable GIO Modules
@@ -1103,7 +1107,7 @@ extern GType _g_win32_network_monitor_get_type (void);
static HMODULE gio_dll = NULL;
#ifdef DLL_EXPORT
#ifndef GLIB_STATIC_COMPILATION
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
@@ -1123,8 +1127,40 @@ DllMain (HINSTANCE hinstDLL,
return TRUE;
}
#elif defined(G_HAS_CONSTRUCTORS) /* && G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION */
extern void glib_win32_init (void);
extern void gobject_win32_init (void);
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(giomodule_init_ctor)
#endif
G_DEFINE_CONSTRUCTOR (giomodule_init_ctor)
static void
giomodule_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 AFTER loading gobject. So, in dynamic configuration glib and
* gobject are always initialized BEFORE gio.
*
* 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 and gobject are always
* well initialized BEFORE gio.
*/
glib_win32_init ();
gobject_win32_init ();
gio_win32_appinfo_init (FALSE);
}
#else /* G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION && !G_HAS_CONSTRUCTORS */
#error Your platform/compiler is missing constructor support
#endif /* GLIB_STATIC_COMPILATION */
void *
_g_io_win32_get_module (void)
{
@@ -1136,7 +1172,7 @@ _g_io_win32_get_module (void)
return gio_dll;
}
#endif
#endif /* G_PLATFORM_WIN32 */
void
_g_io_modules_ensure_extension_points_registered (void)

View File

@@ -3,11 +3,15 @@
/* This is the same check that's done in configure to create config.h */
#ifdef _WIN32
# ifdef _MSC_VER
# define GLIB_TEST_EXPORT_SYMBOL __declspec(dllexport) extern
# else
# define GLIB_TEST_EXPORT_SYMBOL __attribute__((visibility("default"))) __declspec(dllexport) extern
# endif
#ifdef GLIB_STATIC_COMPILATION
#define GLIB_TEST_EXPORT_SYMBOL extern
#else
#ifdef _MSC_VER
#define GLIB_TEST_EXPORT_SYMBOL __declspec(dllexport) extern
#else
#define GLIB_TEST_EXPORT_SYMBOL __attribute__ ((visibility ("default"))) __declspec(dllexport) extern
#endif
#endif
/* Matches GCC and Clang */
#elif defined(__GNUC__) && (__GNUC__ >= 4)
# define GLIB_TEST_EXPORT_SYMBOL __attribute__((visibility("default"))) extern