Skip to content

[CMake] Windows stdlib cross-compilation fixes #13140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ function(_add_swift_library_single target name)
swift_windows_generate_sdk_vfs_overlay(SWIFTLIB_SINGLE_VFS_OVERLAY_FLAGS)
foreach(flag ${SWIFTLIB_SINGLE_VFS_OVERLAY_FLAGS})
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xcc;${flag})
list(APPEND SWIFTLIB_SINGLE_C_COMPILE_FLAGS ${flag})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows is case-insensitive, so when you're cross-compiling from a case-sensitive filesystem a number of header paths don't resolve (since the Windows headers are very loose about case and the spellings vary between files).

The VFS overlay is a means to map between the differently-cased forms of the headers (e.g. windows.h to Windows.h). Previously, it wasn't passing the argument to use the VFS overlay to the C/C++ runtime sources correctly (so it was working for Swift import Msvcrt only).

endforeach()
foreach(directory ${SWIFTLIB_INCLUDE})
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xfrontend;-I${directory})
Expand Down Expand Up @@ -1613,6 +1614,19 @@ function(add_swift_library name)
endif()
endif()

# We unconditionally removed "-z,defs" from CMAKE_SHARED_LINKER_FLAGS in
# swift_common_standalone_build_config_llvm within SwiftSharedCMakeConfig.cmake,
# where it was added by a call to HandleLLVMOptions.
#
# Rather than applying it to all targets and libraries, we here add it back to
# supported targets and libraries only.
# This is needed for ELF targets only; however, RemoteMirror needs to build
# with undefined symbols.
if("${SWIFT_SDK_${LFLAGS_SDK}_OBJECT_FORMAT}" STREQUAL "ELF"
AND NOT "${name}" STREQUAL "swiftRemoteMirror")
list(APPEND swiftlib_link_flags_all "-Wl,-z,defs")
endif()

# Add this library variant.
_add_swift_library_single(
${VARIANT_NAME}
Expand Down
14 changes: 11 additions & 3 deletions cmake/modules/SwiftSharedCMakeConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,17 @@ macro(swift_common_standalone_build_config_llvm product is_cross_compiling)
include(AddSwiftTableGen) # This imports TableGen from LLVM.
include(HandleLLVMOptions)

# HACK: this ugly tweaking is to prevent the propagation of the flag from LLVM
# into swift. The use of this flag pollutes all targets, and we are not able
# to remove it on a per-target basis which breaks cross-compilation.
# HACK: Not all targets support -z,defs as a linker flag.
#
# Normally, LLVM would only add it as an option for known ELF targets;
# however, due to the custom scheme Swift uses for cross-compilation, the
# CMAKE_SHARED_LINKER_FLAGS are determined based on the host system and
# then applied to all targets. This causes issues in cross-compiling to
# Windows from a Linux host.
#
# To work around this, we unconditionally remove the flag here and then
# selectively add it to the per-target link flags; this is currently done
# in add_swift_library within AddSwift.cmake.
string(REGEX REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")

set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
Expand Down
3 changes: 0 additions & 3 deletions stdlib/public/SwiftRemoteMirror/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# HACK: Force this library to build with undefined symbols.
string(REGEX REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")

# libswiftRemoteMirror.dylib should not have runtime dependencies; it's
# always built as a shared library.
if(SWIFT_BUILD_DYNAMIC_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)
Expand Down