[glib: 5/7] Enable full-static build on Windows




commit 42c77c7ac7c19461775b5210d88e8dcfa52c2d5c
Author: Loic Le Page <llepage fluendo com>
Date:   Wed Jan 19 14:19:05 2022 +0100

    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

 gio/giomodule.c                       | 40 +++++++++++++++++++++--
 gio/tests/modules/symbol-visibility.h | 14 +++++---
 glib/glib-init.c                      | 52 ++++++++++++++++++++++++++----
 glib/glibconfig.h.in                  |  2 ++
 glib/gthread-win32.c                  | 24 +++++++++++++-
 glib/tests/private.c                  |  8 +++++
 gobject/gtype.c                       | 60 ++++++++++++++++++++++++++++++++---
 meson.build                           |  6 ++--
 subprojects/zlib.wrap                 |  9 +++---
 tests/module-test.c                   |  4 +++
 10 files changed, 193 insertions(+), 26 deletions(-)
---
diff --git a/gio/giomodule.c b/gio/giomodule.c
index 90af75118..1e202d3ee 100644
--- a/gio/giomodule.c
+++ b/gio/giomodule.c
@@ -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)
diff --git a/gio/tests/modules/symbol-visibility.h b/gio/tests/modules/symbol-visibility.h
index f9f8826ce..e83894444 100644
--- a/gio/tests/modules/symbol-visibility.h
+++ b/gio/tests/modules/symbol-visibility.h
@@ -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
diff --git a/glib/glib-init.c b/glib/glib-init.c
index 22be89108..e7b4984e6 100644
--- a/glib/glib-init.c
+++ b/glib/glib-init.c
@@ -343,19 +343,27 @@ glib_init (void)
 #ifdef G_PLATFORM_WIN32
 
 HMODULE glib_dll = NULL;
+void glib_win32_init (void);
 
-static void
+void
 glib_win32_init (void)
 {
-  g_crash_handler_win32_init ();
+  /* May be called more than once in static compilation mode */
+  static gboolean win32_already_init = FALSE;
+  if (!win32_already_init)
+    {
+      win32_already_init = TRUE;
+
+      g_crash_handler_win32_init ();
 #ifdef THREADS_WIN32
-  g_thread_win32_init ();
+      g_thread_win32_init ();
 #endif
 
-  g_clock_win32_init ();
-  glib_init ();
-  /* must go after glib_init */
-  g_console_win32_init ();
+      g_clock_win32_init ();
+      glib_init ();
+      /* must go after glib_init */
+      g_console_win32_init ();
+    }
 }
 
 static void
@@ -368,6 +376,8 @@ glib_win32_deinit (gboolean detach_thread)
   g_crash_handler_win32_deinit ();
 }
 
+#ifndef GLIB_STATIC_COMPILATION
+
 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
                      DWORD     fdwReason,
                      LPVOID    lpvReserved);
@@ -402,6 +412,34 @@ DllMain (HINSTANCE hinstDLL,
   return TRUE;
 }
 
+#elif defined(G_HAS_CONSTRUCTORS) /* && G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION */
+#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
+#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
+#endif
+#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
+#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(glib_init_dtor)
+#endif
+
+G_DEFINE_CONSTRUCTOR (glib_init_ctor)
+
+static void
+glib_init_ctor (void)
+{
+  glib_win32_init ();
+}
+
+G_DEFINE_DESTRUCTOR (glib_init_dtor)
+
+static void
+glib_init_dtor (void)
+{
+  glib_win32_deinit (FALSE);
+}
+
+#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
diff --git a/glib/glibconfig.h.in b/glib/glibconfig.h.in
index 873cb0314..e66748d95 100644
--- a/glib/glibconfig.h.in
+++ b/glib/glibconfig.h.in
@@ -20,6 +20,8 @@
 
 #mesondefine GLIB_STATIC_COMPILATION
 #mesondefine GOBJECT_STATIC_COMPILATION
+#mesondefine G_INTL_STATIC_COMPILATION
+#mesondefine FFI_STATIC_BUILD
 
 G_BEGIN_DECLS
 
diff --git a/glib/gthread-win32.c b/glib/gthread-win32.c
index 4cac46d6b..e7a4cde7b 100644
--- a/glib/gthread-win32.c
+++ b/glib/gthread-win32.c
@@ -422,6 +422,28 @@ g_system_thread_free (GRealThread *thread)
 void
 g_system_thread_exit (void)
 {
+  /* In static compilation, DllMain doesn't exist and so DLL_THREAD_DETACH
+   * case is never called and thread destroy notifications are not triggered.
+   * To ensure that notifications are correctly triggered in static
+   * compilation mode, we call directly the "detach" function here right
+   * before terminating the thread.
+   * As all win32 threads initialized through the glib API are run through
+   * the same proxy function g_thread_win32_proxy() which calls systematically
+   * g_system_thread_exit() when finishing, we obtain the same behavior as
+   * with dynamic compilation.
+   *
+   * WARNING: unfortunately this mechanism cannot work with threads created
+   * directly from the Windows API using CreateThread() or _beginthread/ex().
+   * It only works with threads created by using the glib API with
+   * g_system_thread_new(). 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.
+   */
+#ifdef GLIB_STATIC_COMPILATION
+  g_thread_win32_thread_detach ();
+#endif
+
   _endthreadex (0);
 }
 
@@ -610,7 +632,7 @@ SetThreadName (DWORD  dwThreadID,
 typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
                                                  PCWSTR lpThreadDescription);
 static pSetThreadDescription SetThreadDescriptionFunc = NULL;
-HMODULE kernel32_module = NULL;
+static HMODULE kernel32_module = NULL;
 
 static gboolean
 g_thread_win32_load_library (void)
diff --git a/glib/tests/private.c b/glib/tests/private.c
index 5e89b35e2..ec256d8de 100644
--- a/glib/tests/private.c
+++ b/glib/tests/private.c
@@ -146,6 +146,14 @@ test_private3 (void)
     thread = (HANDLE) _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
     WaitForSingleObject (thread, INFINITE);
     CloseHandle (thread);
+
+    /* FIXME: with static compilation on Windows this test will fail because
+     * it is mixing up glib threads with Microsoft native thread API. See
+     * comment in gthread-win32.c for g_system_thread_exit() implementation.
+     * Fix is not straightforward, possible solution could be to use FLS
+     * functions (instead of TLS) as proposed in
+     * https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1655
+     */
   }
 #else
   {
diff --git a/gobject/gtype.c b/gobject/gtype.c
index 26ec30b7b..071429934 100644
--- a/gobject/gtype.c
+++ b/gobject/gtype.c
@@ -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,22 +4562,56 @@ 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)
 {
-  gobject_init ();
+  /* 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
+#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 /* !G_PLATFORM_WIN32 && !G_HAS_CONSTRUCTORS */
+#error Your platform/compiler is missing constructor support
+#endif /* G_PLATFORM_WIN32 */
+
 /**
  * g_type_class_add_private:
  * @g_class: (type GObject.TypeClass): class structure for an instantiatable
diff --git a/meson.build b/meson.build
index 082ab630f..299f832f8 100644
--- a/meson.build
+++ b/meson.build
@@ -214,8 +214,10 @@ if get_option('default_library') != 'static'
 endif
 
 if get_option('default_library') == 'static'
-    glibconfig_conf.set('GLIB_STATIC_COMPILATION', '1')
-    glibconfig_conf.set('GOBJECT_STATIC_COMPILATION', '1')
+  glibconfig_conf.set('GLIB_STATIC_COMPILATION', '1')
+  glibconfig_conf.set('GOBJECT_STATIC_COMPILATION', '1')
+  glibconfig_conf.set('G_INTL_STATIC_COMPILATION', '1')
+  glibconfig_conf.set('FFI_STATIC_BUILD', '1')
 endif
 
 # Cygwin glib port maintainers made it clear
diff --git a/subprojects/zlib.wrap b/subprojects/zlib.wrap
index ff9ab6680..c49c1d90c 100644
--- a/subprojects/zlib.wrap
+++ b/subprojects/zlib.wrap
@@ -1,10 +1,11 @@
 [wrap-file]
 directory = zlib-1.2.11
-
 source_url = https://zlib.net/fossils/zlib-1.2.11.tar.gz
 source_filename = zlib-1.2.11.tar.gz
 source_hash = c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1
+patch_filename = zlib_1.2.11-6_patch.zip
+patch_url = https://wrapdb.mesonbuild.com/v2/zlib_1.2.11-6/get_patch
+patch_hash = f7c24c5698ce787294910ad431f94088102d35ddaf88542d04add1e54afa9212
 
-patch_url = https://github.com/mesonbuild/zlib/releases/download/1.2.11-3/zlib.zip
-patch_filename = zlib-1.2.11-3-wrap.zip
-patch_hash = f07dc491ab3d05daf00632a0591e2ae61b470615b5b73bcf9b3f061fff65cff0
+[provide]
+zlib = zlib_dep
diff --git a/tests/module-test.c b/tests/module-test.c
index 62473d29d..a74bc74d4 100644
--- a/tests/module-test.c
+++ b/tests/module-test.c
@@ -101,8 +101,12 @@ main (int    argc,
   if (!module_self)
     g_error ("error: %s", g_module_error ());
 
+    /* On Windows static compilation mode, glib API symbols are not
+     * exported dynamically by definition. */
+#if !defined(G_PLATFORM_WIN32) || !defined(GLIB_STATIC_COMPILATION)
   if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
     g_error ("error: %s", g_module_error ());
+#endif
 
   module_a = g_module_open_full (plugin_a, G_MODULE_BIND_LAZY, &error);
   g_assert_no_error (error);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]