Skip to content

Commit 3d174c3

Browse files
committed
stdlib: be more DLL friendly on Windows
When the standard library is built dynamically on COFF targets, the public interfaces must be decorated in order to generate a proper DLL which can be confused by the dependent libraries. When the exported interface is used, it must be indirectly addressed. This can be done manually in code or the MS extension of `__declspec(dllimport)` may be used to indicate to the compiler that this symbol be addressed indirectly. This permits building more pieces of the standard library dynamically on Windows.
1 parent 5cf6006 commit 3d174c3

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,19 @@ function(add_swift_library name)
12371237
elseif("${sdk}" STREQUAL "WATCHOS" OR "${sdk}" STREQUAL "WATCHOS_SIMULATOR")
12381238
list(APPEND swiftlib_swift_compile_flags_all
12391239
${SWIFTLIB_SWIFT_COMPILE_FLAGS_WATCHOS})
1240+
elseif("${sdk}" STREQUAL "WINDOWS")
1241+
# FIXME(SR2005) static and shared are not mutually exclusive; however
1242+
# since we do a single build of the sources, this doesnt work for
1243+
# building both simultaneously. Effecitvely, only shared builds are
1244+
# supported on windows currently.
1245+
if(SWIFTLIB_SHARED)
1246+
list(APPEND swiftlib_swift_compile_flags_all -D_USRDLL)
1247+
if(SWIFTLIB_IS_STDLIB_CORE)
1248+
list(APPEND swiftlib_swift_compile_flags_all -DswiftCore_EXPORTS)
1249+
endif()
1250+
elseif(SWIFTLIB_STATIC)
1251+
list(APPEND swiftlib_swift_compile_flags_all -D_LIB)
1252+
endif()
12401253
endif()
12411254

12421255
# Add this library variant.

stdlib/public/SwiftShims/Visibility.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@
6363
# if defined(__CYGWIN__)
6464
# define SWIFT_RUNTIME_EXPORT
6565
# else
66-
# if defined(__SWIFT_CURRENT_DYLIB)
66+
# if defined(swiftCore_EXPORTS)
6767
# define SWIFT_RUNTIME_EXPORT __declspec(dllexport)
6868
# else
69-
# define SWIFT_RUNTIME_EXPORT
69+
# define SWIFT_RUNTIME_EXPORT __declspec(dllimport)
7070
# endif
7171
# endif
7272
#endif

stdlib/public/runtime/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ add_swift_library(swiftRuntime OBJECT_LIBRARY TARGET_LIBRARY
6767
${swift_runtime_sources}
6868
${swift_runtime_objc_sources}
6969
${swift_runtime_leaks_sources}
70-
C_COMPILE_FLAGS ${swift_runtime_compile_flags}
70+
C_COMPILE_FLAGS ${swift_runtime_compile_flags} -DswiftCore_EXPORTS
7171
INSTALL_IN_COMPONENT never_install)
7272

7373
set(ELFISH_SDKS)

stdlib/public/stubs/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ add_swift_library(swiftStdlibStubs OBJECT_LIBRARY TARGET_LIBRARY
2222
UnicodeExtendedGraphemeClusters.cpp.gyb
2323
${swift_stubs_objc_sources}
2424
${swift_stubs_unicode_normalization_sources}
25-
C_COMPILE_FLAGS ${SWIFT_CORE_CXX_FLAGS}
25+
C_COMPILE_FLAGS ${SWIFT_CORE_CXX_FLAGS} -DswiftCore_EXPORTS
2626
LINK_LIBRARIES ${swift_stubs_link_libraries}
2727
INSTALL_IN_COMPONENT stdlib)
2828

0 commit comments

Comments
 (0)