[gtksourceview/wip/constructor] Use a DSO constructor to init i18n
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/constructor] Use a DSO constructor to init i18n
- Date: Tue, 2 Aug 2016 04:32:40 +0000 (UTC)
commit d6199abc740d4042e1708a9d124002d52e1dee1f
Author: Sébastien Wilmet <swilmet gnome org>
Date: Tue Aug 2 05:55:12 2016 +0200
Use a DSO constructor to init i18n
When the Dynamic Shared Object is loaded, the constructor is called,
which initializes i18n.
docs/reference/Makefile.am | 1 +
gtksourceview/Makefile.am | 2 +
gtksourceview/gconstructor.h | 120 ++++++++++++++++++++++++++
gtksourceview/gtksourceview-i18n.c | 96 ---------------------
gtksourceview/gtksourceview-init.c | 164 ++++++++++++++++++++++++++++++++++++
po/POTFILES.in | 1 +
6 files changed, 288 insertions(+), 96 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 23037c6..1e85a23 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -30,6 +30,7 @@ EXTRA_HFILES = \
# e.g. IGNORE_HFILES=gtkdebug.h gtkintl.h private_code
IGNORE_HFILES = \
config.h \
+ gconstructor.h \
gtksource.h \
gtksourcebuffer-private.h \
gtksourcebufferinputstream.h \
diff --git a/gtksourceview/Makefile.am b/gtksourceview/Makefile.am
index 787132f..e304301 100644
--- a/gtksourceview/Makefile.am
+++ b/gtksourceview/Makefile.am
@@ -96,6 +96,7 @@ libgtksourceview_public_c_files = \
gtksourceview.c
libgtksourceview_private_headers = \
+ gconstructor.h \
gtksourcebuffer-private.h \
gtksourcebufferinputstream.h \
gtksourcebufferoutputstream.h \
@@ -139,6 +140,7 @@ libgtksourceview_private_c_files = \
gtksourcespacedrawer.c \
gtksourceundomanagerdefault.c \
gtksourceview-i18n.c \
+ gtksourceview-init.c \
gtksourceview-utils.c
# Split in a helper library, so the private functions can be used in unit tests.
diff --git a/gtksourceview/gconstructor.h b/gtksourceview/gconstructor.h
new file mode 100644
index 0000000..dccb031
--- /dev/null
+++ b/gtksourceview/gconstructor.h
@@ -0,0 +1,120 @@
+/*
+ If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
+ destructors, in a sane way, including e.g. on library unload. If not you're on
+ your own.
+
+ Some compilers need #pragma to handle this, which does not work with macros,
+ so the way you need to use this is (for constructors):
+
+ #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
+ #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
+ #endif
+ G_DEFINE_CONSTRUCTOR(my_constructor)
+ static void my_constructor(void) {
+ ...
+ }
+
+*/
+
+#ifndef __GTK_DOC_IGNORE__
+
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
+
+#define G_HAS_CONSTRUCTORS 1
+
+#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
+#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
+
+#elif defined (_MSC_VER) && (_MSC_VER >= 1500)
+/* Visual studio 2008 and later has _Pragma */
+
+#define G_HAS_CONSTRUCTORS 1
+
+/* We do some weird things to avoid the constructors being optimized
+ * away on VS2015 if WholeProgramOptimization is enabled. First we
+ * make a reference to the array from the wrapper to make sure its
+ * references. Then we use a pragma to make sure the wrapper function
+ * symbol is always included at the link stage. Also, the symbols
+ * need to be extern (but not dllexport), even though they are not
+ * really used from another object file.
+ */
+
+/* We need to account for differences between the mangling of symbols
+ * for Win32 (x86) and x64 programs, as symbols on Win32 are prefixed
+ * with an underscore but symbols on x64 are not.
+ */
+#ifdef _WIN64
+#define G_MSVC_SYMBOL_PREFIX ""
+#else
+#define G_MSVC_SYMBOL_PREFIX "_"
+#endif
+
+#define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX)
+#define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX)
+
+#define G_MSVC_CTOR(_func,_sym_prefix) \
+ static void _func(void); \
+ extern int (* _array ## _func)(void); \
+ int _func ## _wrapper(void) { _func(); g_slist_find (NULL, _array ## _func); return 0; } \
+ __pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \
+ __pragma(section(".CRT$XCU",read)) \
+ __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper;
+
+#define G_MSVC_DTOR(_func,_sym_prefix) \
+ static void _func(void); \
+ extern int (* _array ## _func)(void); \
+ int _func ## _constructor(void) { atexit (_func); g_slist_find (NULL, _array ## _func); return 0; } \
+ __pragma(comment(linker,"/include:" _sym_prefix # _func "_constructor")) \
+ __pragma(section(".CRT$XCU",read)) \
+ __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _constructor;
+
+#elif defined (_MSC_VER)
+
+#define G_HAS_CONSTRUCTORS 1
+
+/* Pre Visual studio 2008 must use #pragma section */
+#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
+#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
+
+#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
+ section(".CRT$XCU",read)
+#define G_DEFINE_CONSTRUCTOR(_func) \
+ static void _func(void); \
+ static int _func ## _wrapper(void) { _func(); return 0; } \
+ __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
+
+#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
+ section(".CRT$XCU",read)
+#define G_DEFINE_DESTRUCTOR(_func) \
+ static void _func(void); \
+ static int _func ## _constructor(void) { atexit (_func); return 0; } \
+ __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
+
+#elif defined(__SUNPRO_C)
+
+/* This is not tested, but i believe it should work, based on:
+ * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
+ */
+
+#define G_HAS_CONSTRUCTORS 1
+
+#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
+#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
+
+#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
+ init(_func)
+#define G_DEFINE_CONSTRUCTOR(_func) \
+ static void _func(void);
+
+#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
+ fini(_func)
+#define G_DEFINE_DESTRUCTOR(_func) \
+ static void _func(void);
+
+#else
+
+/* constructors not supported for this compiler */
+
+#endif
+
+#endif /* __GTK_DOC_IGNORE__ */
diff --git a/gtksourceview/gtksourceview-i18n.c b/gtksourceview/gtksourceview-i18n.c
index b6bbd58..dca1a65 100644
--- a/gtksourceview/gtksourceview-i18n.c
+++ b/gtksourceview/gtksourceview-i18n.c
@@ -27,108 +27,12 @@
#include "gtksourceview-i18n.h"
-#ifdef OS_OSX
-
-#include <Cocoa/Cocoa.h>
-
-static gchar *
-dirs_os_x_get_bundle_resource_dir (void)
-{
- NSAutoreleasePool *pool;
- gchar *str = NULL;
- NSString *path;
-
- pool = [[NSAutoreleasePool alloc] init];
-
- if ([[NSBundle mainBundle] bundleIdentifier] == nil)
- {
- [pool release];
- return NULL;
- }
-
- path = [[NSBundle mainBundle] resourcePath];
-
- if (!path)
- {
- [pool release];
- return NULL;
- }
-
- str = g_strdup ([path UTF8String]);
- [pool release];
- return str;
-}
-
-static gchar *
-dirs_os_x_get_locale_dir (void)
-{
- gchar *res_dir;
- gchar *ret;
-
- res_dir = dirs_os_x_get_bundle_resource_dir ();
-
- if (res_dir == NULL)
- {
- ret = g_build_filename (DATADIR, "locale", NULL);
- }
- else
- {
- ret = g_build_filename (res_dir, "share", "locale", NULL);
- g_free (res_dir);
- }
-
- return ret;
-}
-
-#endif
-
-static gchar *
-get_locale_dir (void)
-{
- gchar *locale_dir;
-
-#ifdef G_OS_WIN32
- gchar *win32_dir;
-
- win32_dir = g_win32_get_package_installation_directory_of_module (NULL);
-
- locale_dir = g_build_filename (win32_dir, "share", "locale", NULL);
-
- g_free (win32_dir);
-#elif defined (OS_OSX)
- locale_dir = dirs_os_x_get_locale_dir ();
-#else
- locale_dir = g_build_filename (DATADIR, "locale", NULL);
-#endif
-
- return locale_dir;
-}
-
-/*
- * Small hack since we don't have a proper place where
- * do gettext initialization.
- */
const gchar *
_gtksourceview_gettext (const gchar *msgid)
{
- static gboolean initialized = FALSE;
-
G_GNUC_UNUSED const char translator_credits[] = N_("translator-credits");
/* above is a dummy variable to get the string into po files */
- if (G_UNLIKELY (!initialized))
- {
- gchar *locale_dir;
-
- locale_dir = get_locale_dir ();
-
- bindtextdomain (GETTEXT_PACKAGE, locale_dir);
- g_free (locale_dir);
-
- bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
- initialized = TRUE;
- }
-
return g_dgettext (GETTEXT_PACKAGE, msgid);
}
diff --git a/gtksourceview/gtksourceview-init.c b/gtksourceview/gtksourceview-init.c
new file mode 100644
index 0000000..852b961
--- /dev/null
+++ b/gtksourceview/gtksourceview-init.c
@@ -0,0 +1,164 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- *
+ *
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2016 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/* Init i18n */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib/gi18n-lib.h>
+#include "gconstructor.h"
+
+#ifdef G_OS_WIN32
+static HMODULE gtksourceview_dll;
+#endif
+
+#ifdef OS_OSX
+#include <Cocoa/Cocoa.h>
+
+static gchar *
+dirs_os_x_get_bundle_resource_dir (void)
+{
+ NSAutoreleasePool *pool;
+ gchar *str = NULL;
+ NSString *path;
+
+ pool = [[NSAutoreleasePool alloc] init];
+
+ if ([[NSBundle mainBundle] bundleIdentifier] == nil)
+ {
+ [pool release];
+ return NULL;
+ }
+
+ path = [[NSBundle mainBundle] resourcePath];
+
+ if (!path)
+ {
+ [pool release];
+ return NULL;
+ }
+
+ str = g_strdup ([path UTF8String]);
+ [pool release];
+ return str;
+}
+
+static gchar *
+dirs_os_x_get_locale_dir (void)
+{
+ gchar *res_dir;
+ gchar *ret;
+
+ res_dir = dirs_os_x_get_bundle_resource_dir ();
+
+ if (res_dir == NULL)
+ {
+ ret = g_build_filename (DATADIR, "locale", NULL);
+ }
+ else
+ {
+ ret = g_build_filename (res_dir, "share", "locale", NULL);
+ g_free (res_dir);
+ }
+
+ return ret;
+}
+#endif /* OS_OSX */
+
+static gchar *
+get_locale_dir (void)
+{
+ gchar *locale_dir;
+
+#if defined (G_OS_WIN32)
+ gchar *win32_dir;
+
+ win32_dir = g_win32_get_package_installation_directory_of_module (gtksourceview_dll);
+
+ locale_dir = g_build_filename (win32_dir, "share", "locale", NULL);
+
+ g_free (win32_dir);
+#elif defined (OS_OSX)
+ locale_dir = dirs_os_x_get_locale_dir ();
+#else
+ locale_dir = g_build_filename (DATADIR, "locale", NULL);
+#endif
+
+ return locale_dir;
+}
+
+static void
+gtksourceview_init (void)
+{
+ gchar *locale_dir;
+
+ locale_dir = get_locale_dir ();
+ bindtextdomain (GETTEXT_PACKAGE, locale_dir);
+ g_free (locale_dir);
+
+ bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+}
+
+#if defined (G_OS_WIN32)
+
+BOOL WINAPI DllMain (HINSTANCE hinstDLL,
+ DWORD fdwReason,
+ LPVOID lpvReserved);
+
+BOOL WINAPI
+DllMain (HINSTANCE hinstDLL,
+ DWORD fdwReason,
+ LPVOID lpvReserved)
+{
+ switch (fdwReason)
+ {
+ case DLL_PROCESS_ATTACH:
+ gtksourceview_dll = hinstDLL;
+ gtksourceview_init ();
+ break;
+
+ case DLL_THREAD_DETACH:
+ default:
+ /* do nothing */
+ break;
+ }
+
+ return TRUE;
+}
+
+#elif defined (G_HAS_CONSTRUCTORS)
+
+# ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
+# pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gtksourceview_constructor)
+# endif
+G_DEFINE_CONSTRUCTOR (gtksourceview_constructor)
+
+static void
+gtksourceview_constructor (void)
+{
+ gtksourceview_init ();
+}
+
+#else
+# error Your platform/compiler is missing constructor support
+#endif
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 95fdced..ad05598 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -42,3 +42,4 @@ gtksourceview/gtksourceundomanagerdefault.c
gtksourceview/gtksourceutils.c
gtksourceview/gtksourceview.c
gtksourceview/gtksourceview-i18n.c
+gtksourceview/gtksourceview-init.c
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]