|
Packit |
79f644 |
/*
|
|
Packit |
79f644 |
If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
|
|
Packit |
79f644 |
destructors, in a sane way, including e.g. on library unload. If not you're on
|
|
Packit |
79f644 |
your own.
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
Some compilers need #pragma to handle this, which does not work with macros,
|
|
Packit |
79f644 |
so the way you need to use this is (for constructors):
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
|
|
Packit |
79f644 |
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
|
|
Packit |
79f644 |
#endif
|
|
Packit |
79f644 |
G_DEFINE_CONSTRUCTOR(my_constructor)
|
|
Packit |
79f644 |
static void my_constructor(void) {
|
|
Packit |
79f644 |
...
|
|
Packit |
79f644 |
}
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
*/
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#ifndef __GTK_DOC_IGNORE__
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_HAS_CONSTRUCTORS 1
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
|
|
Packit |
79f644 |
#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#elif defined (_MSC_VER) && (_MSC_VER >= 1500)
|
|
Packit |
79f644 |
/* Visual studio 2008 and later has _Pragma */
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_HAS_CONSTRUCTORS 1
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
/* We do some weird things to avoid the constructors being optimized
|
|
Packit |
79f644 |
* away on VS2015 if WholeProgramOptimization is enabled. First we
|
|
Packit |
79f644 |
* make a reference to the array from the wrapper to make sure its
|
|
Packit |
79f644 |
* references. Then we use a pragma to make sure the wrapper function
|
|
Packit |
79f644 |
* symbol is always included at the link stage. Also, the symbols
|
|
Packit |
79f644 |
* need to be extern (but not dllexport), even though they are not
|
|
Packit |
79f644 |
* really used from another object file.
|
|
Packit |
79f644 |
*/
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
/* We need to account for differences between the mangling of symbols
|
|
Packit |
79f644 |
* for Win32 (x86) and x64 programs, as symbols on Win32 are prefixed
|
|
Packit |
79f644 |
* with an underscore but symbols on x64 are not.
|
|
Packit |
79f644 |
*/
|
|
Packit |
79f644 |
#ifdef _WIN64
|
|
Packit |
79f644 |
#define G_MSVC_SYMBOL_PREFIX ""
|
|
Packit |
79f644 |
#else
|
|
Packit |
79f644 |
#define G_MSVC_SYMBOL_PREFIX "_"
|
|
Packit |
79f644 |
#endif
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX)
|
|
Packit |
79f644 |
#define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX)
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_MSVC_CTOR(_func,_sym_prefix) \
|
|
Packit |
79f644 |
static void _func(void); \
|
|
Packit |
79f644 |
extern int (* _array ## _func)(void); \
|
|
Packit |
79f644 |
int _func ## _wrapper(void) { _func(); g_slist_find (NULL, _array ## _func); return 0; } \
|
|
Packit |
79f644 |
__pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \
|
|
Packit |
79f644 |
__pragma(section(".CRT$XCU",read)) \
|
|
Packit |
79f644 |
__declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper;
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_MSVC_DTOR(_func,_sym_prefix) \
|
|
Packit |
79f644 |
static void _func(void); \
|
|
Packit |
79f644 |
extern int (* _array ## _func)(void); \
|
|
Packit |
79f644 |
int _func ## _constructor(void) { atexit (_func); g_slist_find (NULL, _array ## _func); return 0; } \
|
|
Packit |
79f644 |
__pragma(comment(linker,"/include:" _sym_prefix # _func "_constructor")) \
|
|
Packit |
79f644 |
__pragma(section(".CRT$XCU",read)) \
|
|
Packit |
79f644 |
__declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _constructor;
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#elif defined (_MSC_VER)
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_HAS_CONSTRUCTORS 1
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
/* Pre Visual studio 2008 must use #pragma section */
|
|
Packit |
79f644 |
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
|
|
Packit |
79f644 |
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
|
|
Packit |
79f644 |
section(".CRT$XCU",read)
|
|
Packit |
79f644 |
#define G_DEFINE_CONSTRUCTOR(_func) \
|
|
Packit |
79f644 |
static void _func(void); \
|
|
Packit |
79f644 |
static int _func ## _wrapper(void) { _func(); return 0; } \
|
|
Packit |
79f644 |
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
|
|
Packit |
79f644 |
section(".CRT$XCU",read)
|
|
Packit |
79f644 |
#define G_DEFINE_DESTRUCTOR(_func) \
|
|
Packit |
79f644 |
static void _func(void); \
|
|
Packit |
79f644 |
static int _func ## _constructor(void) { atexit (_func); return 0; } \
|
|
Packit |
79f644 |
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#elif defined(__SUNPRO_C)
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
/* This is not tested, but i believe it should work, based on:
|
|
Packit |
79f644 |
* http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
|
|
Packit |
79f644 |
*/
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_HAS_CONSTRUCTORS 1
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
|
|
Packit |
79f644 |
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
|
|
Packit |
79f644 |
init(_func)
|
|
Packit |
79f644 |
#define G_DEFINE_CONSTRUCTOR(_func) \
|
|
Packit |
79f644 |
static void _func(void);
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
|
|
Packit |
79f644 |
fini(_func)
|
|
Packit |
79f644 |
#define G_DEFINE_DESTRUCTOR(_func) \
|
|
Packit |
79f644 |
static void _func(void);
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#else
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
/* constructors not supported for this compiler */
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#endif
|
|
Packit |
79f644 |
|
|
Packit |
79f644 |
#endif /* __GTK_DOC_IGNORE__ */
|