From b1b83360e1dcdc013852185b30800ec5f10f88e0 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 13 May 2025 10:50:16 -0700 Subject: [PATCH 1/2] dispatch: extract `DISPATCH_EXTERN` from the `DISPATCH_EXPORT` macro This extracts a new `DISPATCH_EXTERN` macro from the existing definition of `DISPATCH_EXPORT`. This follows the standard style of many of Darwin's frameworks. Extracting and re-using this macro allows us to make the definition of `DISPATCH_EXPORT` more concise. --- dispatch/base.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/dispatch/base.h b/dispatch/base.h index ce65f05f6..6f83a4edb 100644 --- a/dispatch/base.h +++ b/dispatch/base.h @@ -178,24 +178,24 @@ #endif #endif +#ifndef DISPATCH_EXTERN +# if defined(__cplusplus) +# define DISPATCH_EXTERN extern "C" +# else +# define DISPATCH_EXTERN extern +# endif +#endif + #if defined(_WIN32) #if defined(__DISPATCH_BUILDING_DISPATCH__) -#if defined(__cplusplus) -#define DISPATCH_EXPORT extern "C" __declspec(dllexport) +#define DISPATCH_EXPORT DISPATCH_EXTERN __declspec(dllexport) #else -#define DISPATCH_EXPORT extern __declspec(dllexport) -#endif -#else -#if defined(__cplusplus) -#define DISPATCH_EXPORT extern "C" __declspec(dllimport) -#else -#define DISPATCH_EXPORT extern __declspec(dllimport) -#endif +#define DISPATCH_EXPORT DISPATCH_EXTERN __declspec(dllimport) #endif #elif __GNUC__ -#define DISPATCH_EXPORT extern __attribute__((visibility("default"))) +#define DISPATCH_EXPORT DISPATCH_EXTERN __attribute__((visibility("default"))) #else -#define DISPATCH_EXPORT extern +#define DISPATCH_EXPORT DISPATCH_EXTERN #endif #if __GNUC__ From e1fb2099d6d82abd70f7f93e9f999aae09347985 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 13 May 2025 10:54:36 -0700 Subject: [PATCH 2/2] dispatch: introduce `dispatch_STATIC` and honour `dispatch_EXPORT` When building with CMake, `dispatch_EXPORT` is automatically defined when building a shared library. We should additionally rely on `dispatch_STATIC` to indicate that we are building a static library. When building statically, we should ensure that the ABI of dispatch does not participate in dynamic linking as otherwise any consuming library would vend the dispatch ABI. Particularly for Linux, this would be problematic as the symbol resolution is ill-defined and can end up with multiple incompatible definitions. Co-authored-by: Evan Wilde --- dispatch/base.h | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/dispatch/base.h b/dispatch/base.h index 6f83a4edb..8e7728525 100644 --- a/dispatch/base.h +++ b/dispatch/base.h @@ -186,16 +186,24 @@ # endif #endif -#if defined(_WIN32) -#if defined(__DISPATCH_BUILDING_DISPATCH__) -#define DISPATCH_EXPORT DISPATCH_EXTERN __declspec(dllexport) -#else -#define DISPATCH_EXPORT DISPATCH_EXTERN __declspec(dllimport) -#endif -#elif __GNUC__ -#define DISPATCH_EXPORT DISPATCH_EXTERN __attribute__((visibility("default"))) +#if defined(dispatch_STATIC) +# if __GNUC__ +# define DISPATCH_EXPORT DISPATCH_EXTERN __attribute__((__visibility__("hidden"))) +# else +# define DISPATCH_EXPORT DISPATCH_EXTERN +# endif #else -#define DISPATCH_EXPORT DISPATCH_EXTERN +# if defined(_WIN32) +# if defined(dispatch_EXPORT) || defined(__DISPATCH_BUILDING_DISPATCH__) +# define DISPATCH_EXPORT DISPATCH_EXTERN __declspec(dllexport) +# else +# define DISPATCH_EXPORT DISPATCH_EXTERN __declspec(dllimport) +# endif +# elif __GNUC__ +# define DISPATCH_EXPORT DISPATCH_EXTERN __attribute__((__visibility__("default"))) +# else +# define DISPATCH_EXPORT DISPATCH_EXTERN +# endif #endif #if __GNUC__