1 | /* |
2 | If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and |
3 | destructors, in a usable way, including e.g. on library unload. If not you're on |
4 | your own. |
5 | |
6 | Some compilers need #pragma to handle this, which does not work with macros, |
7 | so the way you need to use this is (for constructors): |
8 | |
9 | #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA |
10 | #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor) |
11 | #endif |
12 | G_DEFINE_CONSTRUCTOR(my_constructor) |
13 | static void my_constructor(void) { |
14 | ... |
15 | } |
16 | |
17 | */ |
18 | |
19 | #ifndef __GTK_DOC_IGNORE__ |
20 | |
21 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) |
22 | |
23 | #define G_HAS_CONSTRUCTORS 1 |
24 | |
25 | #define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void); |
26 | #define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void); |
27 | |
28 | #elif defined (_MSC_VER) && (_MSC_VER >= 1500) |
29 | /* Visual studio 2008 and later has _Pragma */ |
30 | |
31 | #include <stdlib.h> |
32 | |
33 | #define G_HAS_CONSTRUCTORS 1 |
34 | |
35 | /* We do some weird things to avoid the constructors being optimized |
36 | * away on VS2015 if WholeProgramOptimization is enabled. First we |
37 | * make a reference to the array from the wrapper to make sure its |
38 | * references. Then we use a pragma to make sure the wrapper function |
39 | * symbol is always included at the link stage. Also, the symbols |
40 | * need to be extern (but not dllexport), even though they are not |
41 | * really used from another object file. |
42 | */ |
43 | |
44 | /* We need to account for differences between the mangling of symbols |
45 | * for x86 and x64/ARM/ARM64 programs, as symbols on x86 are prefixed |
46 | * with an underscore but symbols on x64/ARM/ARM64 are not. |
47 | */ |
48 | #ifdef _M_IX86 |
49 | #define G_MSVC_SYMBOL_PREFIX "_" |
50 | #else |
51 | #define G_MSVC_SYMBOL_PREFIX "" |
52 | #endif |
53 | |
54 | #define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX) |
55 | #define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX) |
56 | |
57 | #define G_MSVC_CTOR(_func,_sym_prefix) \ |
58 | static void _func(void); \ |
59 | extern int (* _array ## _func)(void); \ |
60 | int _func ## _wrapper(void) { _func(); g_slist_find (NULL, _array ## _func); return 0; } \ |
61 | __pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \ |
62 | __pragma(section(".CRT$XCU",read)) \ |
63 | __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper; |
64 | |
65 | #define G_MSVC_DTOR(_func,_sym_prefix) \ |
66 | static void _func(void); \ |
67 | extern int (* _array ## _func)(void); \ |
68 | int _func ## _constructor(void) { atexit (_func); g_slist_find (NULL, _array ## _func); return 0; } \ |
69 | __pragma(comment(linker,"/include:" _sym_prefix # _func "_constructor")) \ |
70 | __pragma(section(".CRT$XCU",read)) \ |
71 | __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _constructor; |
72 | |
73 | #elif defined (_MSC_VER) |
74 | |
75 | #define G_HAS_CONSTRUCTORS 1 |
76 | |
77 | /* Pre Visual studio 2008 must use #pragma section */ |
78 | #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
79 | #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
80 | |
81 | #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
82 | section(".CRT$XCU",read) |
83 | #define G_DEFINE_CONSTRUCTOR(_func) \ |
84 | static void _func(void); \ |
85 | static int _func ## _wrapper(void) { _func(); return 0; } \ |
86 | __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper; |
87 | |
88 | #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
89 | section(".CRT$XCU",read) |
90 | #define G_DEFINE_DESTRUCTOR(_func) \ |
91 | static void _func(void); \ |
92 | static int _func ## _constructor(void) { atexit (_func); return 0; } \ |
93 | __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor; |
94 | |
95 | #elif defined(__SUNPRO_C) |
96 | |
97 | /* This is not tested, but i believe it should work, based on: |
98 | * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c |
99 | */ |
100 | |
101 | #define G_HAS_CONSTRUCTORS 1 |
102 | |
103 | #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
104 | #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
105 | |
106 | #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
107 | init(_func) |
108 | #define G_DEFINE_CONSTRUCTOR(_func) \ |
109 | static void _func(void); |
110 | |
111 | #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
112 | fini(_func) |
113 | #define G_DEFINE_DESTRUCTOR(_func) \ |
114 | static void _func(void); |
115 | |
116 | #else |
117 | |
118 | /* constructors not supported for this compiler */ |
119 | |
120 | #endif |
121 | |
122 | #endif /* __GTK_DOC_IGNORE__ */ |
123 | |