1 | /* |
2 | If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and |
3 | destructors, in a sane 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 | #define G_HAS_CONSTRUCTORS 1 |
32 | |
33 | /* We do some weird things to avoid the constructors being optimized |
34 | * away on VS2015 if WholeProgramOptimization is enabled. First we |
35 | * make a reference to the array from the wrapper to make sure its |
36 | * references. Then we use a pragma to make sure the wrapper function |
37 | * symbol is always included at the link stage. Also, the symbols |
38 | * need to be extern (but not dllexport), even though they are not |
39 | * really used from another object file. |
40 | */ |
41 | |
42 | /* We need to account for differences between the mangling of symbols |
43 | * for Win32 (x86) and x64 programs, as symbols on Win32 are prefixed |
44 | * with an underscore but symbols on x64 are not. |
45 | */ |
46 | #ifdef _WIN64 |
47 | #define G_MSVC_SYMBOL_PREFIX "" |
48 | #else |
49 | #define G_MSVC_SYMBOL_PREFIX "_" |
50 | #endif |
51 | |
52 | #define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX) |
53 | #define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX) |
54 | |
55 | #define G_MSVC_CTOR(_func,_sym_prefix) \ |
56 | static void _func(void); \ |
57 | extern int (* _array ## _func)(void); \ |
58 | int _func ## _wrapper(void) { _func(); g_slist_find (NULL, _array ## _func); return 0; } \ |
59 | __pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \ |
60 | __pragma(section(".CRT$XCU",read)) \ |
61 | __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper; |
62 | |
63 | #define G_MSVC_DTOR(_func,_sym_prefix) \ |
64 | static void _func(void); \ |
65 | extern int (* _array ## _func)(void); \ |
66 | int _func ## _constructor(void) { atexit (_func); g_slist_find (NULL, _array ## _func); return 0; } \ |
67 | __pragma(comment(linker,"/include:" _sym_prefix # _func "_constructor")) \ |
68 | __pragma(section(".CRT$XCU",read)) \ |
69 | __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _constructor; |
70 | |
71 | #elif defined (_MSC_VER) |
72 | |
73 | #define G_HAS_CONSTRUCTORS 1 |
74 | |
75 | /* Pre Visual studio 2008 must use #pragma section */ |
76 | #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
77 | #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
78 | |
79 | #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
80 | section(".CRT$XCU",read) |
81 | #define G_DEFINE_CONSTRUCTOR(_func) \ |
82 | static void _func(void); \ |
83 | static int _func ## _wrapper(void) { _func(); return 0; } \ |
84 | __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper; |
85 | |
86 | #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
87 | section(".CRT$XCU",read) |
88 | #define G_DEFINE_DESTRUCTOR(_func) \ |
89 | static void _func(void); \ |
90 | static int _func ## _constructor(void) { atexit (_func); return 0; } \ |
91 | __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor; |
92 | |
93 | #elif defined(__SUNPRO_C) |
94 | |
95 | /* This is not tested, but i believe it should work, based on: |
96 | * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c |
97 | */ |
98 | |
99 | #define G_HAS_CONSTRUCTORS 1 |
100 | |
101 | #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
102 | #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
103 | |
104 | #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
105 | init(_func) |
106 | #define G_DEFINE_CONSTRUCTOR(_func) \ |
107 | static void _func(void); |
108 | |
109 | #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
110 | fini(_func) |
111 | #define G_DEFINE_DESTRUCTOR(_func) \ |
112 | static void _func(void); |
113 | |
114 | #else |
115 | |
116 | /* constructors not supported for this compiler */ |
117 | |
118 | #endif |
119 | |
120 | #endif /* __GTK_DOC_IGNORE__ */ |
121 | |