1/* $NetBSD: ldap_cdefs.h,v 1.1.1.6 2018/02/06 01:53:05 christos Exp $ */
2
3/* $OpenLDAP$ */
4/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 *
6 * Copyright 1998-2017 The OpenLDAP Foundation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17/* LDAP C Defines */
18
19#ifndef _LDAP_CDEFS_H
20#define _LDAP_CDEFS_H
21
22#if defined(__cplusplus) || defined(c_plusplus)
23# define LDAP_BEGIN_DECL extern "C" {
24# define LDAP_END_DECL }
25#else
26# define LDAP_BEGIN_DECL /* begin declarations */
27# define LDAP_END_DECL /* end declarations */
28#endif
29
30#if !defined(LDAP_NO_PROTOTYPES) && ( defined(LDAP_NEEDS_PROTOTYPES) || \
31 defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus) )
32
33 /* ANSI C or C++ */
34# define LDAP_P(protos) protos
35# define LDAP_CONCAT1(x,y) x ## y
36# define LDAP_CONCAT(x,y) LDAP_CONCAT1(x,y)
37# define LDAP_STRING(x) #x /* stringify without expanding x */
38# define LDAP_XSTRING(x) LDAP_STRING(x) /* expand x, then stringify */
39
40#ifndef LDAP_CONST
41# define LDAP_CONST const
42#endif
43
44#else /* no prototypes */
45
46 /* traditional C */
47# define LDAP_P(protos) ()
48# define LDAP_CONCAT(x,y) x/**/y
49# define LDAP_STRING(x) "x"
50
51#ifndef LDAP_CONST
52# define LDAP_CONST /* no const */
53#endif
54
55#endif /* no prototypes */
56
57#if (__GNUC__) * 1000 + (__GNUC_MINOR__) >= 2006
58# define LDAP_GCCATTR(attrs) __attribute__(attrs)
59#else
60# define LDAP_GCCATTR(attrs)
61#endif
62
63/*
64 * Support for Windows DLLs.
65 *
66 * When external source code includes header files for dynamic libraries,
67 * the external source code is "importing" DLL symbols into its resulting
68 * object code. On Windows, symbols imported from DLLs must be explicitly
69 * indicated in header files with the __declspec(dllimport) directive.
70 * This is not totally necessary for functions because the compiler
71 * (gcc or MSVC) will generate stubs when this directive is absent.
72 * However, this is required for imported variables.
73 *
74 * The LDAP libraries, i.e. liblber and libldap, can be built as
75 * static or shared, based on configuration. Just about all other source
76 * code in OpenLDAP use these libraries. If the LDAP libraries
77 * are configured as shared, 'configure' defines the LDAP_LIBS_DYNAMIC
78 * macro. When other source files include LDAP library headers, the
79 * LDAP library symbols will automatically be marked as imported. When
80 * the actual LDAP libraries are being built, the symbols will not
81 * be marked as imported because the LBER_LIBRARY or LDAP_LIBRARY macros
82 * will be respectively defined.
83 *
84 * Any project outside of OpenLDAP with source code wanting to use
85 * LDAP dynamic libraries should explicitly define LDAP_LIBS_DYNAMIC.
86 * This will ensure that external source code appropriately marks symbols
87 * that will be imported.
88 *
89 * The slapd executable, itself, can be used as a dynamic library.
90 * For example, if a backend module is compiled as shared, it will
91 * import symbols from slapd. When this happens, the slapd symbols
92 * must be marked as imported in header files that the backend module
93 * includes. Remember that slapd links with various static libraries.
94 * If the LDAP libraries were configured as static, their object
95 * code is also part of the monolithic slapd executable. Thus, when
96 * a backend module imports symbols from slapd, it imports symbols from
97 * all of the static libraries in slapd as well. Thus, the SLAP_IMPORT
98 * macro, when defined, will appropriately mark symbols as imported.
99 * This macro should be used by shared backend modules as well as any
100 * other external source code that imports symbols from the slapd
101 * executable as if it were a DLL.
102 *
103 * Note that we don't actually have to worry about using the
104 * __declspec(dllexport) directive anywhere. This is because both
105 * MSVC and Mingw provide alternate (more effective) methods for exporting
106 * symbols out of binaries, i.e. the use of a DEF file.
107 *
108 * NOTE ABOUT BACKENDS: Backends can be configured as static or dynamic.
109 * When a backend is configured as dynamic, slapd will load the backend
110 * explicitly and populate function pointer structures by calling
111 * the backend's well-known initialization function. Because of this
112 * procedure, slapd never implicitly imports symbols from dynamic backends.
113 * This makes it unnecessary to tag various backend functions with the
114 * __declspec(dllimport) directive. This is because neither slapd nor
115 * any other external binary should ever be implicitly loading a backend
116 * dynamic module.
117 *
118 * Backends are supposed to be self-contained. However, it appears that
119 * back-meta DOES implicitly import symbols from back-ldap. This means
120 * that the __declspec(dllimport) directive should be marked on back-ldap
121 * functions (in its header files) if and only if we're compiling for
122 * windows AND back-ldap has been configured as dynamic AND back-meta
123 * is the client of back-ldap. When client is slapd, there is no effect
124 * since slapd does not implicitly import symbols.
125 *
126 * TODO(?): Currently, back-meta nor back-ldap is supported for Mingw32.
127 * Thus, there's no need to worry about this right now. This is something that
128 * may or may not have to be addressed in the future.
129 */
130
131/* LBER library */
132#if defined(_WIN32) && \
133 ((defined(LDAP_LIBS_DYNAMIC) && !defined(LBER_LIBRARY)) || \
134 (!defined(LDAP_LIBS_DYNAMIC) && defined(SLAPD_IMPORT)))
135# define LBER_F(type) extern __declspec(dllimport) type
136# define LBER_V(type) extern __declspec(dllimport) type
137#else
138# define LBER_F(type) extern type
139# define LBER_V(type) extern type
140#endif
141
142/* LDAP library */
143#if defined(_WIN32) && \
144 ((defined(LDAP_LIBS_DYNAMIC) && !defined(LDAP_LIBRARY)) || \
145 (!defined(LDAP_LIBS_DYNAMIC) && defined(SLAPD_IMPORT)))
146# define LDAP_F(type) extern __declspec(dllimport) type
147# define LDAP_V(type) extern __declspec(dllimport) type
148#else
149# define LDAP_F(type) extern type
150# define LDAP_V(type) extern type
151#endif
152
153/* AVL library */
154#if defined(_WIN32) && defined(SLAPD_IMPORT)
155# define LDAP_AVL_F(type) extern __declspec(dllimport) type
156# define LDAP_AVL_V(type) extern __declspec(dllimport) type
157#else
158# define LDAP_AVL_F(type) extern type
159# define LDAP_AVL_V(type) extern type
160#endif
161
162/* LDIF library */
163#if defined(_WIN32) && defined(SLAPD_IMPORT)
164# define LDAP_LDIF_F(type) extern __declspec(dllimport) type
165# define LDAP_LDIF_V(type) extern __declspec(dllimport) type
166#else
167# define LDAP_LDIF_F(type) extern type
168# define LDAP_LDIF_V(type) extern type
169#endif
170
171/* LUNICODE library */
172#if defined(_WIN32) && defined(SLAPD_IMPORT)
173# define LDAP_LUNICODE_F(type) extern __declspec(dllimport) type
174# define LDAP_LUNICODE_V(type) extern __declspec(dllimport) type
175#else
176# define LDAP_LUNICODE_F(type) extern type
177# define LDAP_LUNICODE_V(type) extern type
178#endif
179
180/* LUTIL library */
181#if defined(_WIN32) && defined(SLAPD_IMPORT)
182# define LDAP_LUTIL_F(type) extern __declspec(dllimport) type
183# define LDAP_LUTIL_V(type) extern __declspec(dllimport) type
184#else
185# define LDAP_LUTIL_F(type) extern type
186# define LDAP_LUTIL_V(type) extern type
187#endif
188
189/* REWRITE library */
190#if defined(_WIN32) && defined(SLAPD_IMPORT)
191# define LDAP_REWRITE_F(type) extern __declspec(dllimport) type
192# define LDAP_REWRITE_V(type) extern __declspec(dllimport) type
193#else
194# define LDAP_REWRITE_F(type) extern type
195# define LDAP_REWRITE_V(type) extern type
196#endif
197
198/* SLAPD (as a dynamic library exporting symbols) */
199#if defined(_WIN32) && defined(SLAPD_IMPORT)
200# define LDAP_SLAPD_F(type) extern __declspec(dllimport) type
201# define LDAP_SLAPD_V(type) extern __declspec(dllimport) type
202#else
203# define LDAP_SLAPD_F(type) extern type
204# define LDAP_SLAPD_V(type) extern type
205#endif
206
207/* SLAPD (as a dynamic library exporting symbols) */
208#if defined(_WIN32) && defined(SLAPD_IMPORT)
209# define LDAP_SLAPI_F(type) extern __declspec(dllimport) type
210# define LDAP_SLAPI_V(type) extern __declspec(dllimport) type
211#else
212# define LDAP_SLAPI_F(type) extern type
213# define LDAP_SLAPI_V(type) extern type
214#endif
215
216/* SLAPD (as a dynamic library exporting symbols) */
217#if defined(_WIN32) && defined(SLAPD_IMPORT)
218# define SLAPI_F(type) extern __declspec(dllimport) type
219# define SLAPI_V(type) extern __declspec(dllimport) type
220#else
221# define SLAPI_F(type) extern type
222# define SLAPI_V(type) extern type
223#endif
224
225/*
226 * C library. Mingw32 links with the dynamic C run-time library by default,
227 * so the explicit definition of CSTATIC will keep dllimport from
228 * being defined, if desired.
229 *
230 * MSVC defines the _DLL macro when the compiler is invoked with /MD or /MDd,
231 * which means the resulting object code will be linked with the dynamic
232 * C run-time library.
233 *
234 * Technically, it shouldn't be necessary to redefine any functions that
235 * the headers for the C library should already contain. Nevertheless, this
236 * is here as a safe-guard.
237 *
238 * TODO: Determine if these macros ever get expanded for Windows. If not,
239 * the declspec expansion can probably be removed.
240 */
241#if (defined(__MINGW32__) && !defined(CSTATIC)) || \
242 (defined(_MSC_VER) && defined(_DLL))
243# define LDAP_LIBC_F(type) extern __declspec(dllimport) type
244# define LDAP_LIBC_V(type) extern __declspec(dllimport) type
245#else
246# define LDAP_LIBC_F(type) extern type
247# define LDAP_LIBC_V(type) extern type
248#endif
249
250#endif /* _LDAP_CDEFS_H */
251