Skip to content

Commit bed2f0f

Browse files
committed
dispatch: split DISPATCH_EXPORT and prepare for static linking
This change accomplishes two items: 1. splits out a new `DISPATCH_EXTERN` macro to decorate with `extern` or `extern "C"` based on the compilation mode (C vs C++). 2. enables a new configuration macro (`dispatch_STATIC`) that allows building with libdispatch for static linking. The default remains dynamic linking. The motivation for `DISPATCH_EXTERN` is primarily being able to concisely define `DISPATCH_EXPORT`. This follows the naming scheme that most other Apple frameworks use (e.g. UIKit, Foundation, etc). The change to introduce support for static linking is motivated by Windows. With the gradual roll out of static linking of the runtime, it would be convenient to be able to statically link dispatch into a fully sealed Swift program. Doing so requires building libdispatch without the `__declspec(dllexport)` and `__declspec(dllimport)` attributes on Windows. Similarly, it would be unfortunate to have dispatch's ABI be subsumed by a client library and have it participate in dynamic linking. The user is responsible for defining `dispatch_STATIC` when using a static copy of libdispatch. This allows the default to remain dynamic linking (which is the preferred style on Darwin and Windows).
1 parent 8b61977 commit bed2f0f

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

dispatch/base.h

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,31 @@
178178
#endif
179179
#endif
180180

181-
#if defined(_WIN32)
182-
#if defined(__DISPATCH_BUILDING_DISPATCH__)
183-
#if defined(__cplusplus)
184-
#define DISPATCH_EXPORT extern "C" __declspec(dllexport)
185-
#else
186-
#define DISPATCH_EXPORT extern __declspec(dllexport)
187-
#endif
188-
#else
189181
#if defined(__cplusplus)
190-
#define DISPATCH_EXPORT extern "C" __declspec(dllimport)
182+
# define DISPATCH_EXTERN extern "C"
191183
#else
192-
#define DISPATCH_EXPORT extern __declspec(dllimport)
193-
#endif
184+
# define DISPATCH_EXTERN extern
194185
#endif
186+
187+
#if defined(_WIN32)
188+
# if defined(dispatch_STATIC)
189+
# define DISPATCH_EXPORT DISPATCH_EXTERN
190+
# endif
191+
# else
192+
# if defined(__DISPATCH_BUILDING_DISPATCH__) || defined(dispatch_EXPORTS)
193+
# define DISPATCH_EXPORT DISPATCH_EXTERN __declspec(dllexport)
194+
# else
195+
# define DISPATCH_EXPORT DISPATCH_EXTERN __declspec(dllimport)
196+
# endif
197+
# endif
195198
#elif __GNUC__
196-
#define DISPATCH_EXPORT extern __attribute__((visibility("default")))
199+
# if defined(dispatch_STATIC)
200+
# define DISPATCH_EXPORT DISPATCH_EXTERN __attribute__((visibility("hidden")))
201+
# else
202+
# define DISPATCH_EXPORT DISPATCH_EXTERN __attribute__((visibility("default")))
203+
# endif
197204
#else
198-
#define DISPATCH_EXPORT extern
205+
# define DISPATCH_EXPORT extern
199206
#endif
200207

201208
#if __GNUC__

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ elseif(ANDROID)
104104
target_compile_options(dispatch PRIVATE
105105
-U_GNU_SOURCE)
106106
endif()
107+
if(NOT BUILD_SHARED_LIBS)
108+
target_compile_definitions(dispatch PUBLIC
109+
dispatch_STATIC)
110+
endif()
107111
if(DISPATCH_ENABLE_ASSERTS)
108112
target_compile_definitions(dispatch PRIVATE
109113
DISPATCH_DEBUG=1)

0 commit comments

Comments
 (0)