This file is part of MXE. See LICENSE.md for licensing information.

Contains ad hoc patches for cross building.

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: v1993 <v19930312@gmail.com>
Date: Fri, 15 Jun 2012 15:25:01 +0200
Subject: [PATCH 1/5] Fix static builds

Don't declare DllMain, use constructors from gconstructor.h instead.

A possibly better approach to this problem is to use TLS callbacks. See https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1655 for its status.

diff --git a/gio/giomodule.c b/gio/giomodule.c
index 1111111..2222222 100644
--- a/gio/giomodule.c
+++ b/gio/giomodule.c
@@ -54,6 +54,9 @@
 #ifdef G_OS_WIN32
 #include "gregistrysettingsbackend.h"
 #include "giowin32-priv.h"
+#ifndef DLL_EXPORT
+#include <glib/gconstructor.h>
+#endif
 #endif
 #include <glib/gstdio.h>
 
@@ -1102,8 +1105,7 @@ extern GType _g_win32_network_monitor_get_type (void);
 
 static HMODULE gio_dll = NULL;
 
-#ifdef DLL_EXPORT
-
+#if defined (DLL_EXPORT)
 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
                      DWORD     fdwReason,
                      LPVOID    lpvReserved);
@@ -1121,7 +1123,19 @@ DllMain (HINSTANCE hinstDLL,
 
   return TRUE;
 }
+#elif defined (G_HAS_CONSTRUCTORS)
+#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
+#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gio_win32_ctor)
+#endif
+G_DEFINE_CONSTRUCTOR(gio_win32_ctor)
 
+static void
+gio_win32_ctor(void)
+{
+  gio_win32_appinfo_init (FALSE);
+}
+#else
+# error Your platform/compiler is missing constructor support
 #endif
 
 void *
diff --git a/glib/glib-init.c b/glib/glib-init.c
index 1111111..2222222 100644
--- a/glib/glib-init.c
+++ b/glib/glib-init.c
@@ -334,20 +334,32 @@ glib_init (void)
 
   glib_inited = TRUE;
 
+#ifdef G_OS_WIN32
+  g_crash_handler_win32_init ();
+  g_clock_win32_init ();
+#ifdef THREADS_WIN32
+  g_thread_win32_init ();
+#endif
+#endif
+
   g_messages_prefixed_init ();
   g_debug_init ();
   g_quark_init ();
   g_error_init ();
+
+#ifdef G_OS_WIN32
+  g_console_win32_init ();
+#endif
 }
 
-#if defined (G_OS_WIN32)
+#ifdef G_OS_WIN32
+HMODULE glib_dll;
+#endif
 
+#if defined (G_OS_WIN32) && defined (DLL_EXPORT)
 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
                      DWORD     fdwReason,
                      LPVOID    lpvReserved);
-
-HMODULE glib_dll;
-
 BOOL WINAPI
 DllMain (HINSTANCE hinstDLL,
          DWORD     fdwReason,
@@ -357,14 +369,7 @@ DllMain (HINSTANCE hinstDLL,
     {
     case DLL_PROCESS_ATTACH:
       glib_dll = hinstDLL;
-      g_crash_handler_win32_init ();
-      g_clock_win32_init ();
-#ifdef THREADS_WIN32
-      g_thread_win32_init ();
-#endif
       glib_init ();
-      /* must go after glib_init */
-      g_console_win32_init ();
       break;
 
     case DLL_THREAD_DETACH:
diff --git a/gobject/gtype.c b/gobject/gtype.c
index 1111111..2222222 100644
--- a/gobject/gtype.c
+++ b/gobject/gtype.c
@@ -4521,7 +4521,7 @@ gobject_init (void)
   _g_signal_init ();
 }
 
-#if defined (G_OS_WIN32)
+#if defined (G_OS_WIN32) && defined (DLL_EXPORT)
 
 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
                      DWORD     fdwReason,

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: aquiles2k <aj@elane2k.com>
Date: Wed, 6 Apr 2016 22:39:53 +0300
Subject: [PATCH 2/5] fix error "won't overwrite defined macro" on OSX

See https://github.com/mxe/mxe/issues/1281

diff --git a/m4macros/glib-gettext.m4 b/m4macros/glib-gettext.m4
index 1111111..2222222 100644
--- a/m4macros/glib-gettext.m4
+++ b/m4macros/glib-gettext.m4
@@ -39,8 +39,8 @@ dnl We go to great lengths to make sure that aclocal won't
 dnl try to pull in the installed version of these macros
 dnl when running aclocal in the glib directory.
 dnl
-m4_copy([AC_DEFUN],[glib_DEFUN])
-m4_copy([AC_REQUIRE],[glib_REQUIRE])
+m4_copy_force([AC_DEFUN],[glib_DEFUN])
+m4_copy_force([AC_REQUIRE],[glib_REQUIRE])
 dnl
 dnl At the end, if we're not within glib, we'll define the public
 dnl definitions in terms of our private definitions.

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: v1993 <v19930312@gmail.com>
Date: Wed, 8 Dec 2021 20:08:30 +0300
Subject: [PATCH 3/5] Fix gio linking error for some cases

Happens with autotools and direct GCC call, meson doesn't have it.

diff --git a/gio/meson.build b/gio/meson.build
index 1111111..2222222 100644
--- a/gio/meson.build
+++ b/gio/meson.build
@@ -835,8 +835,15 @@ libgio_dep = declare_dependency(link_with : libgio,
   dependencies : [libgmodule_dep, libgobject_dep, gioenumtypes_dep],
   include_directories : [gioinc])
 
+# Link against libuuid to prevent linker error in certain cases
+windows_ldflags = []
+
+if host_system == 'windows'
+  windows_ldflags += ['-luuid']
+endif
+
 pkg.generate(libgio,
-  libraries_private : [osx_ldflags],
+  libraries_private : [osx_ldflags, windows_ldflags],
   requires : ['glib-2.0', 'gobject-2.0'],
   variables : ['datadir=' + join_paths('${prefix}', get_option('datadir')),
                'schemasdir=' + join_paths('${datadir}', schemas_subdir),

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: v1993 <v19930312@gmail.com>
Date: Wed, 8 Dec 2021 20:45:32 +0300
Subject: [PATCH 4/5] Allow different bindir setting for pkg-config files


diff --git a/gio/meson.build b/gio/meson.build
index 1111111..2222222 100644
--- a/gio/meson.build
+++ b/gio/meson.build
@@ -847,7 +847,7 @@ pkg.generate(libgio,
   requires : ['glib-2.0', 'gobject-2.0'],
   variables : ['datadir=' + join_paths('${prefix}', get_option('datadir')),
                'schemasdir=' + join_paths('${datadir}', schemas_subdir),
-               'bindir=' + join_paths('${prefix}', get_option('bindir')),
+               'bindir=' + join_paths('${prefix}', get_option('runtime_bindir')),
                'giomoduledir=' + pkgconfig_giomodulesdir,
                'gio=' + join_paths('${bindir}', 'gio'),
                'gio_querymodules=' + join_paths('${bindir}', 'gio-querymodules'),
diff --git a/glib/meson.build b/glib/meson.build
index 1111111..2222222 100644
--- a/glib/meson.build
+++ b/glib/meson.build
@@ -383,7 +383,7 @@ pkg.generate(libglib,
   libraries_private : [osx_ldflags, win32_ldflags],
   subdirs : ['glib-2.0'],
   extra_cflags : ['-I${libdir}/glib-2.0/include'] + win32_cflags,
-  variables : ['bindir=' + join_paths('${prefix}', get_option('bindir')),
+  variables : ['bindir=' + join_paths('${prefix}', get_option('runtime_bindir')),
                'glib_genmarshal=' + join_paths('${bindir}', 'glib-genmarshal'),
                'gobject_query=' + join_paths('${bindir}', 'gobject-query'),
                'glib_mkenums=' + join_paths('${bindir}', 'glib-mkenums')],
diff --git a/meson_options.txt b/meson_options.txt
index 1111111..2222222 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -121,4 +121,9 @@ option('glib_checks',
 option('libelf',
        type : 'feature',
        value : 'auto',
-       description : 'Enable support for listing and extracting from ELF resource files with gresource tool')
\ No newline at end of file
+       description : 'Enable support for listing and extracting from ELF resource files with gresource tool')
+
+option('runtime_bindir',
+       type : 'string',
+       value : 'bin',
+       description : 'Alternate bindir value, useful for cross-compilation,')

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Hans Petter Jansson <hpj@hpjansson.org>
Date: Fri, 8 Jul 2022 22:48:57 +0200
Subject: [PATCH 5/5] Add -luuid to win32 dependencies

Needed to prevent certain linking issues on Windows.

This will also have the effect of adding it to the Libs.private line
in the installed pkgconfig.

diff --git a/meson.build b/meson.build
index 1111111..2222222 100644
--- a/meson.build
+++ b/meson.build
@@ -2282,7 +2282,7 @@ if host_system == 'windows' and cc.get_id() != 'msvc' and cc.get_id() != 'clang-
   add_project_arguments(win32_cflags, language : 'c')
 
   # Win32 API libs, used only by libglib and exposed in glib-2.0.pc
-  win32_ldflags = ['-lws2_32', '-lole32', '-lwinmm', '-lshlwapi']
+  win32_ldflags = ['-lws2_32', '-lole32', '-lwinmm', '-lshlwapi', '-luuid']
 elif host_system == 'cygwin'
   win32_ldflags = ['-luser32', '-lkernel32']
 endif
