Skip to content

Commit 505c462

Browse files
committed
Fix cross-compilation for the Windows stdlib
Disable SwiftPrivate, remove -Wl,-z,defs, and use the VFS overlay for compiling the runtime.
1 parent 7ab3426 commit 505c462

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ function(_add_swift_library_single target name)
734734
swift_windows_generate_sdk_vfs_overlay(SWIFTLIB_SINGLE_VFS_OVERLAY_FLAGS)
735735
foreach(flag ${SWIFTLIB_SINGLE_VFS_OVERLAY_FLAGS})
736736
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xcc;${flag})
737+
list(APPEND SWIFTLIB_SINGLE_C_COMPILE_FLAGS ${flag})
737738
endforeach()
738739
foreach(directory ${SWIFTLIB_INCLUDE})
739740
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xfrontend;-I${directory})
@@ -1474,6 +1475,11 @@ function(add_swift_library name)
14741475
continue()
14751476
endif()
14761477

1478+
# TODO: Currently SwiftPrivate runs into linker problems on Windows. See SR-6489.
1479+
if ("${sdk}" STREQUAL "WINDOWS" AND "${name}" MATCHES "SwiftPrivate")
1480+
continue()
1481+
endif()
1482+
14771483
set(THIN_INPUT_TARGETS)
14781484

14791485
# For each architecture supported by this SDK
@@ -1613,6 +1619,18 @@ function(add_swift_library name)
16131619
endif()
16141620
endif()
16151621

1622+
# We unconditionally removed "-z,defs" from CMAKE_SHARED_LINKER_FLAGS in
1623+
# swift_common_standalone_build_config_llvm within SwiftSharedCMakeConfig.cmake,
1624+
# where it was added by a call to HandleLLVMOptions.
1625+
#
1626+
# Rather than applying it to all targets and libraries, we here add it back to
1627+
# supported targets and libraries only.
1628+
# Windows doesn't support the -z,defs flag and RemoteMirror needs to build
1629+
# with undefined symbols.
1630+
if(NOT "${sdk}" STREQUAL "WINDOWS" AND NOT "${name}" STREQUAL "swiftRemoteMirror")
1631+
list(APPEND swiftlib_link_flags_all "-Wl,-z,defs")
1632+
endif()
1633+
16161634
# Add this library variant.
16171635
_add_swift_library_single(
16181636
${VARIANT_NAME}

cmake/modules/SwiftSharedCMakeConfig.cmake

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,16 @@ macro(swift_common_standalone_build_config_llvm product is_cross_compiling)
7878
include(AddSwiftTableGen) # This imports TableGen from LLVM.
7979
include(HandleLLVMOptions)
8080

81-
# HACK: this ugly tweaking is to prevent the propagation of the flag from LLVM
82-
# into swift. The use of this flag pollutes all targets, and we are not able
83-
# to remove it on a per-target basis which breaks cross-compilation.
81+
# HACK: Windows doesn't support -z,defs as a linker flag.
82+
#
83+
# Normally, LLVM would only add it as an option for non-Windows systems;
84+
# however, due to the custom scheme Swift uses for cross-compilation, the
85+
# CMAKE_SHARED_LINKER_FLAGS are determined based on the host system and
86+
# then applied to all targets.
87+
#
88+
# To work around this, we unconditionally remove the flag here and then
89+
# selectively add it to the per-target link flags; this is currently done
90+
# in add_swift_library within AddSwift.cmake.
8491
string(REGEX REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
8592

8693
set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")

stdlib/public/SwiftRemoteMirror/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# HACK: Force this library to build with undefined symbols.
2-
string(REGEX REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
3-
41
# libswiftRemoteMirror.dylib should not have runtime dependencies; it's
52
# always built as a shared library.
63
if(SWIFT_BUILD_DYNAMIC_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)

0 commit comments

Comments
 (0)