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