Add more comments indicating what this is doing.
[metze/wireshark/wip.git] / ws_symbol_export.h
1 /*
2  * Cross platform defines for exporting symbols from shared libraries
3  *
4  * Wireshark - Network traffic analyzer
5  * By Balint Reczey <balint@balintreczey.hu>
6  * Copyright 2013 Balint Reczey
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /** Reset symbol export behavior.
24  * If you {un}define WS_BUILD_DLL on the fly you'll have to define this
25  * as well.
26  */
27 #ifdef RESET_SYMBOL_EXPORT
28
29 #ifdef SYMBOL_EXPORT_H
30 #undef SYMBOL_EXPORT_H
31 #endif
32
33 #ifdef WS_DLL_PUBLIC
34 #undef WS_DLL_PUBLIC
35 #endif
36
37 #ifdef WS_DLL_PUBLIC_DEF
38 #undef WS_DLL_PUBLIC_DEF
39 #endif
40
41 #ifdef WS_DLL_LOCAL
42 #undef WS_DLL_LOCAL
43 #endif
44
45 #endif /* RESET_SYMBOL_EXPORT */
46
47 #ifndef SYMBOL_EXPORT_H
48 #define SYMBOL_EXPORT_H
49
50 /* Originally copied from GCC Wiki at http://gcc.gnu.org/wiki/Visibility */
51 #if defined _WIN32 || defined __CYGWIN__
52   /* Compiling for Windows, so we use the Windows DLL declarations. */
53   #ifdef WS_BUILD_DLL
54     /*
55      * Building a DLL; for all definitions, we want dllexport, and
56      * (presumably so source from DLL and source from a program using the
57      * DLL can both include a header that declares APIs and exported data
58      * for the DLL), for declarations, either dllexport or dllimport will
59      * work (they mean the same thing for a declaration when building a DLL).
60      */
61     #ifdef __GNUC__
62       /* GCC */
63 #define WS_DLL_PUBLIC_DEF __attribute__ ((dllexport))
64     #else /* ! __GNUC__ */
65       /*
66        * Presumably MSVC.
67        * Note: actually gcc seems to also support this syntax.
68        */
69 #define WS_DLL_PUBLIC_DEF __declspec(dllexport)
70     #endif /* __GNUC__ */
71   #else /* WS_BUILD_DLL */
72     /*
73      * Building a program; we should only see declarations, not definitions,
74      * with WS_DLL_PUBLIC, and they all represent APIs or data imported
75      * from a DLL, so use dllimport.
76      *
77      * For functions, export shouldn't be necessary; for data, it might
78      * be necessary, e.g. if what's declared is an array whose size is
79      * not given in the declaration.
80      */
81     #ifdef __GNUC__
82       /* GCC */
83 #define WS_DLL_PUBLIC_DEF __attribute__ ((dllimport))
84     #elif ! (defined ENABLE_STATIC) /* ! __GNUC__ */
85       /*
86        * Presumably MSVC, and we're not building all-static.
87        * Note: actually gcc seems to also support this syntax.
88        */
89 #define WS_DLL_PUBLIC_DEF __declspec(dllimport)
90     #else /* ! __GNUC__  && ENABLE_STATIC */
91       /*
92        * Presumably MSVC, and we're building all-static, so we're
93        * not building any DLLs.
94        */
95 #define WS_DLL_PUBLIC_DEF
96     #endif /* __GNUC__ */
97   #endif /* WS_BUILD_DLL */
98
99   /*
100    * Symbols in a DLL are *not* exported unless they're specifically
101    * flagged as exported, so, for a non-static but non-exported
102    * symbol, we don't have to do anything.
103    */
104   #define WS_DLL_LOCAL
105 #else /* defined _WIN32 || defined __CYGWIN__ */
106   /*
107    * Compiling for UN*X, where the dllimport and dllexport stuff
108    * is neither necessary nor supported; just specify the
109    * visibility if we have a compiler that claims compatibility
110    * with GCC 4 or later.
111    */
112   #if __GNUC__ >= 4
113     /*
114      * Symbols exported from libraries.
115      */
116 #define WS_DLL_PUBLIC_DEF __attribute__ ((visibility ("default")))
117
118     /*
119      * Non-static symbols *not* exported from libraries.
120      */
121 #define WS_DLL_LOCAL  __attribute__ ((visibility ("hidden")))
122   #else /* ! __GNUC__ >= 4 */
123     /*
124      * We have no way to control visibility.
125      */
126     #define WS_DLL_PUBLIC_DEF
127     #define WS_DLL_LOCAL
128   #endif /* __GNUC__ >= 4 */
129 #endif
130
131 /*
132  * Use this for declarations; it can also be used for exported
133  * *function* definitions, but must not be used for exported *data*
134  * definitions.
135  */
136 #define WS_DLL_PUBLIC   WS_DLL_PUBLIC_DEF extern
137
138 #endif /* SYMBOL_EXPORT_H */