Commit 555a0872 authored by Adrien Plazas's avatar Adrien Plazas
Browse files

main: Automatically init libhandy

Remove the public hdy_init() initialization function and replace it by
the private hdy_constructor() library constructor function. This
automatically initializes the library, at the price of a compiler
supporting that feature and of statically linking the library.
parent c981c866
......@@ -85,7 +85,6 @@ libhandy-1.so.0 libhandy-1-0 #MINVER#
hdy_header_group_new@LIBHANDY_1_0 0.0.3
hdy_header_group_remove_header_bar@LIBHANDY_1_0 0.0.4~
hdy_header_group_set_focus@LIBHANDY_1_0 0.0.3
hdy_init@LIBHANDY_1_0 0.0.5
hdy_init_public_types@LIBHANDY_1_0 0.0.5
hdy_keypad_button_get_digit@LIBHANDY_1_0 0.0.12
hdy_keypad_button_get_symbols@LIBHANDY_1_0 0.0.12
......
......@@ -64,7 +64,6 @@
<chapter id="helpers">
<title>Helpers</title>
<xi:include href="xml/hdy-version.xml"/>
<xi:include href="xml/hdy-main.xml"/>
<xi:include href="xml/hdy-string-utf8.xml"/>
<xi:include href="xml/hdy-fold.xml"/>
<xi:include href="xml/hdy-list-box.xml"/>
......
......@@ -4,6 +4,7 @@ subdir('xml')
private_headers = [
'config.h',
'gconstructorprivate.h',
'gtkprogresstrackerprivate.h',
'gtk-window-private.h',
'hdy-animation-private.h',
......
/*
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__ */
......@@ -39,7 +39,6 @@ G_BEGIN_DECLS
#include "hdy-keypad.h"
#include "hdy-leaflet.h"
#include "hdy-list-box.h"
#include "hdy-main.h"
#include "hdy-paginator.h"
#include "hdy-preferences-group.h"
#include "hdy-preferences-page.h"
......
......@@ -9,7 +9,7 @@
#error "Only <handy.h> can be included directly."
#endif
#include "hdy-main.h"
#include <glib.h>
G_BEGIN_DECLS
......
/*
* Copyright (C) 2018 Purism SPC
* Copyright (C) 2018-2020 Purism SPC
*
* SPDX-License-Identifier: LGPL-2.1+
*/
......@@ -7,44 +7,38 @@
#include "hdy-main-private.h"
#include <gio/gio.h>
#include <glib/gi18n-lib.h>
static gint hdy_initialized = FALSE;
#include "gconstructorprivate.h"
/**
* SECTION:hdy-main
* PRIVATE:hdy-main
* @short_description: Library initialization.
* @Title: hdy-main
* @stability: Private
*
* Before using the Handy libarary you should initialize it. This makes
* sure translations for the Handy library are set up properly.
*/
#if defined (G_HAS_CONSTRUCTORS)
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(hdy_constructor)
#endif
G_DEFINE_CONSTRUCTOR(hdy_constructor)
/**
* hdy_init:
* @argc: (inout) (optional): Address of the <parameter>argc</parameter>
* parameter of your main() function (or 0 if @argv is %NULL). This will be
* changed if any arguments were handled.
* @argv: (array length=argc) (inout) (nullable) (optional) (transfer none):
* Address of the <parameter>argv</parameter> parameter of main(), or %NULL.
* Any options understood by Handy are stripped before return.
*
* Call this function before using any other Handy functions in your
* GUI applications. If libhandy has already been initialized, the function will
* simply return without processing the new arguments.
* hdy_constructor:
*
* Returns: %TRUE if initialization was successful, %FALSE otherwise.
* Automatically initializes libhandy.
*/
gboolean
hdy_init (int *argc, char ***argv)
static void
hdy_constructor (void)
{
if (hdy_initialized)
return TRUE;
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
hdy_init_public_types ();
hdy_initialized = TRUE;
return TRUE;
}
#else
# error Your platform/compiler is missing constructor support
#endif
/*
* Copyright (C) 2018 Purism SPC
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#pragma once
#if !defined(_HANDY_INSIDE) && !defined(HANDY_COMPILATION)
#error "Only <handy.h> can be included directly."
#endif
#include <glib.h>
G_BEGIN_DECLS
gboolean hdy_init(int *argc, char ***argv);
G_END_DECLS
......@@ -65,7 +65,6 @@ src_headers = [
'hdy-keypad.h',
'hdy-leaflet.h',
'hdy-list-box.h',
'hdy-main.h',
'hdy-paginator.h',
'hdy-preferences-group.h',
'hdy-preferences-page.h',
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment