ldap_cdefs.h 9.2 KB

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