/* $OpenLDAP$ */1/* This work is part of OpenLDAP Software <http://www.openldap.org/>.2*3* Copyright 1998-2024 The OpenLDAP Foundation.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted only as authorized by the OpenLDAP8* Public License.9*10* A copy of this license is available in file LICENSE in the11* top-level directory of the distribution or, alternatively, at12* <http://www.OpenLDAP.org/license.html>.13*/14/* LDAP C Defines */1516#ifndef _LDAP_CDEFS_H17#define _LDAP_CDEFS_H1819#if defined(__cplusplus) || defined(c_plusplus)20# define LDAP_BEGIN_DECL extern "C" {21# define LDAP_END_DECL }22#else23# define LDAP_BEGIN_DECL /* begin declarations */24# define LDAP_END_DECL /* end declarations */25#endif2627#if !defined(LDAP_NO_PROTOTYPES) && ( defined(LDAP_NEEDS_PROTOTYPES) || \28defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus) )2930/* ANSI C or C++ */31# define LDAP_P(protos) protos32# define LDAP_CONCAT1(x,y) x ## y33# define LDAP_CONCAT(x,y) LDAP_CONCAT1(x,y)34# define LDAP_STRING(x) #x /* stringify without expanding x */35# define LDAP_XSTRING(x) LDAP_STRING(x) /* expand x, then stringify */3637#ifndef LDAP_CONST38# define LDAP_CONST const39#endif4041#else /* no prototypes */4243/* traditional C */44# define LDAP_P(protos) ()45# define LDAP_CONCAT(x,y) x/**/y46# define LDAP_STRING(x) "x"4748#ifndef LDAP_CONST49# define LDAP_CONST /* no const */50#endif5152#endif /* no prototypes */5354#if (__GNUC__) * 1000 + (__GNUC_MINOR__) >= 200655# define LDAP_GCCATTR(attrs) __attribute__(attrs)56#else57# define LDAP_GCCATTR(attrs)58#endif5960/*61* Support for Windows DLLs.62*63* When external source code includes header files for dynamic libraries,64* the external source code is "importing" DLL symbols into its resulting65* object code. On Windows, symbols imported from DLLs must be explicitly66* indicated in header files with the __declspec(dllimport) directive.67* This is not totally necessary for functions because the compiler68* (gcc or MSVC) will generate stubs when this directive is absent.69* However, this is required for imported variables.70*71* The LDAP libraries, i.e. liblber and libldap, can be built as72* static or shared, based on configuration. Just about all other source73* code in OpenLDAP use these libraries. If the LDAP libraries74* are configured as shared, 'configure' defines the LDAP_LIBS_DYNAMIC75* macro. When other source files include LDAP library headers, the76* LDAP library symbols will automatically be marked as imported. When77* the actual LDAP libraries are being built, the symbols will not78* be marked as imported because the LBER_LIBRARY or LDAP_LIBRARY macros79* will be respectively defined.80*81* Any project outside of OpenLDAP with source code wanting to use82* LDAP dynamic libraries should explicitly define LDAP_LIBS_DYNAMIC.83* This will ensure that external source code appropriately marks symbols84* that will be imported.85*86* The slapd executable, itself, can be used as a dynamic library.87* For example, if a backend module is compiled as shared, it will88* import symbols from slapd. When this happens, the slapd symbols89* must be marked as imported in header files that the backend module90* includes. Remember that slapd links with various static libraries.91* If the LDAP libraries were configured as static, their object92* code is also part of the monolithic slapd executable. Thus, when93* a backend module imports symbols from slapd, it imports symbols from94* all of the static libraries in slapd as well. Thus, the SLAP_IMPORT95* macro, when defined, will appropriately mark symbols as imported.96* This macro should be used by shared backend modules as well as any97* other external source code that imports symbols from the slapd98* executable as if it were a DLL.99*100* Note that we don't actually have to worry about using the101* __declspec(dllexport) directive anywhere. This is because both102* MSVC and Mingw provide alternate (more effective) methods for exporting103* symbols out of binaries, i.e. the use of a DEF file.104*105* NOTE ABOUT BACKENDS: Backends can be configured as static or dynamic.106* When a backend is configured as dynamic, slapd will load the backend107* explicitly and populate function pointer structures by calling108* the backend's well-known initialization function. Because of this109* procedure, slapd never implicitly imports symbols from dynamic backends.110* This makes it unnecessary to tag various backend functions with the111* __declspec(dllimport) directive. This is because neither slapd nor112* any other external binary should ever be implicitly loading a backend113* dynamic module.114*115* Backends are supposed to be self-contained. However, it appears that116* back-meta DOES implicitly import symbols from back-ldap. This means117* that the __declspec(dllimport) directive should be marked on back-ldap118* functions (in its header files) if and only if we're compiling for119* windows AND back-ldap has been configured as dynamic AND back-meta120* is the client of back-ldap. When client is slapd, there is no effect121* since slapd does not implicitly import symbols.122*123* TODO(?): Currently, back-meta nor back-ldap is supported for Mingw32.124* Thus, there's no need to worry about this right now. This is something that125* may or may not have to be addressed in the future.126*/127128/* LBER library */129#if defined(_WIN32) && \130((defined(LDAP_LIBS_DYNAMIC) && !defined(LBER_LIBRARY)) || \131(!defined(LDAP_LIBS_DYNAMIC) && defined(SLAPD_IMPORT)))132# define LBER_F(type) extern __declspec(dllimport) type133# define LBER_V(type) extern __declspec(dllimport) type134#else135# define LBER_F(type) extern type136# define LBER_V(type) extern type137#endif138139/* LDAP library */140#if defined(_WIN32) && \141((defined(LDAP_LIBS_DYNAMIC) && !defined(LDAP_LIBRARY)) || \142(!defined(LDAP_LIBS_DYNAMIC) && defined(SLAPD_IMPORT)))143# define LDAP_F(type) extern __declspec(dllimport) type144# define LDAP_V(type) extern __declspec(dllimport) type145#else146# define LDAP_F(type) extern type147# define LDAP_V(type) extern type148#endif149150/* AVL library */151#if defined(_WIN32) && defined(SLAPD_IMPORT)152# define LDAP_AVL_F(type) extern __declspec(dllimport) type153# define LDAP_AVL_V(type) extern __declspec(dllimport) type154#else155# define LDAP_AVL_F(type) extern type156# define LDAP_AVL_V(type) extern type157#endif158159/* LDIF library */160#if defined(_WIN32) && defined(SLAPD_IMPORT)161# define LDAP_LDIF_F(type) extern __declspec(dllimport) type162# define LDAP_LDIF_V(type) extern __declspec(dllimport) type163#else164# define LDAP_LDIF_F(type) extern type165# define LDAP_LDIF_V(type) extern type166#endif167168/* LUNICODE library */169#if defined(_WIN32) && defined(SLAPD_IMPORT)170# define LDAP_LUNICODE_F(type) extern __declspec(dllimport) type171# define LDAP_LUNICODE_V(type) extern __declspec(dllimport) type172#else173# define LDAP_LUNICODE_F(type) extern type174# define LDAP_LUNICODE_V(type) extern type175#endif176177/* LUTIL library */178#if defined(_WIN32) && defined(SLAPD_IMPORT)179# define LDAP_LUTIL_F(type) extern __declspec(dllimport) type180# define LDAP_LUTIL_V(type) extern __declspec(dllimport) type181#else182# define LDAP_LUTIL_F(type) extern type183# define LDAP_LUTIL_V(type) extern type184#endif185186/* REWRITE library */187#if defined(_WIN32) && defined(SLAPD_IMPORT)188# define LDAP_REWRITE_F(type) extern __declspec(dllimport) type189# define LDAP_REWRITE_V(type) extern __declspec(dllimport) type190#else191# define LDAP_REWRITE_F(type) extern type192# define LDAP_REWRITE_V(type) extern type193#endif194195/* SLAPD (as a dynamic library exporting symbols) */196#if defined(_WIN32) && defined(SLAPD_IMPORT)197# define LDAP_SLAPD_F(type) extern __declspec(dllimport) type198# define LDAP_SLAPD_V(type) extern __declspec(dllimport) type199#else200# define LDAP_SLAPD_F(type) extern type201# define LDAP_SLAPD_V(type) extern type202#endif203204/* SLAPD (as a dynamic library exporting symbols) */205#if defined(_WIN32) && defined(SLAPD_IMPORT)206# define LDAP_SLAPI_F(type) extern __declspec(dllimport) type207# define LDAP_SLAPI_V(type) extern __declspec(dllimport) type208#else209# define LDAP_SLAPI_F(type) extern type210# define LDAP_SLAPI_V(type) extern type211#endif212213/* SLAPD (as a dynamic library exporting symbols) */214#if defined(_WIN32) && defined(SLAPD_IMPORT)215# define SLAPI_F(type) extern __declspec(dllimport) type216# define SLAPI_V(type) extern __declspec(dllimport) type217#else218# define SLAPI_F(type) extern type219# define SLAPI_V(type) extern type220#endif221222/*223* C library. Mingw32 links with the dynamic C run-time library by default,224* so the explicit definition of CSTATIC will keep dllimport from225* being defined, if desired.226*227* MSVC defines the _DLL macro when the compiler is invoked with /MD or /MDd,228* which means the resulting object code will be linked with the dynamic229* C run-time library.230*231* Technically, it shouldn't be necessary to redefine any functions that232* the headers for the C library should already contain. Nevertheless, this233* is here as a safe-guard.234*235* TODO: Determine if these macros ever get expanded for Windows. If not,236* the declspec expansion can probably be removed.237*/238#if (defined(__MINGW32__) && !defined(CSTATIC)) || \239(defined(_MSC_VER) && defined(_DLL))240# define LDAP_LIBC_F(type) extern __declspec(dllimport) type241# define LDAP_LIBC_V(type) extern __declspec(dllimport) type242#else243# define LDAP_LIBC_F(type) extern type244# define LDAP_LIBC_V(type) extern type245#endif246247#endif /* _LDAP_CDEFS_H */248249250