Skip to content

Commit 1ce02ad

Browse files
authored
Merge pull request #67782 from compnerd/59-cxxstdlib
[5.9] Enable CxxStdlib on Windows
2 parents 19edc9c + 69483da commit 1ce02ad

18 files changed

+129
-51
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,32 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
490490
if (!getSwiftModule()->getName().is("Cxx"))
491491
this->addLinkLibrary(LinkLibrary("swiftCxx", LibraryKind::Library));
492492

493-
// Only link with CxxStdlib on platforms where the overlay is available.
494-
// Do not try to link CxxStdlib with itself.
495-
if ((target.isOSDarwin() || (target.isOSLinux() && !target.isAndroid())) &&
496-
!getSwiftModule()->getName().is("Cxx") &&
497-
!getSwiftModule()->getName().is("CxxStdlib") &&
498-
!getSwiftModule()->getName().is("std")) {
499-
this->addLinkLibrary(LinkLibrary("swiftCxxStdlib", LibraryKind::Library));
493+
// Do not try to link CxxStdlib with the C++ standard library, Cxx or
494+
// itself.
495+
if (llvm::none_of(llvm::ArrayRef{"Cxx", "CxxStdlib", "std"},
496+
[M = getSwiftModule()->getName().str()](StringRef Name) {
497+
return M == Name;
498+
})) {
499+
// Only link with CxxStdlib on platforms where the overlay is available.
500+
switch (target.getOS()) {
501+
case llvm::Triple::Linux:
502+
if (!target.isAndroid())
503+
this->addLinkLibrary(LinkLibrary("swiftCxxStdlib",
504+
LibraryKind::Library));
505+
break;
506+
case llvm::Triple::Win32: {
507+
bool isStatic = Context.getModuleByName("CxxStdlib")->isStaticLibrary();
508+
this->addLinkLibrary(
509+
LinkLibrary(isStatic ? "libswiftCxxStdlib" : "swiftCxxStdlib",
510+
LibraryKind::Library));
511+
break;
512+
}
513+
default:
514+
if (target.isOSDarwin())
515+
this->addLinkLibrary(LinkLibrary("swiftCxxStdlib",
516+
LibraryKind::Library));
517+
break;
518+
}
500519
}
501520
}
502521

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ void Serializer::writeBlockInfoBlock() {
837837
BLOCK_RECORD(options_block, SDK_PATH);
838838
BLOCK_RECORD(options_block, XCC);
839839
BLOCK_RECORD(options_block, IS_SIB);
840+
BLOCK_RECORD(options_block, IS_STATIC_LIBRARY);
840841
BLOCK_RECORD(options_block, IS_TESTABLE);
841842
BLOCK_RECORD(options_block, ARE_PRIVATE_IMPORTS_ENABLED);
842843
BLOCK_RECORD(options_block, RESILIENCE_STRATEGY);

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,10 @@ function(_add_target_variant_link_flags)
491491
# options. This causes conflicts.
492492
list(APPEND result "-nostdlib")
493493
endif()
494-
swift_windows_lib_for_arch(${LFLAGS_ARCH} ${LFLAGS_ARCH}_LIB)
495-
list(APPEND library_search_directories ${${LFLAGS_ARCH}_LIB})
494+
if(NOT CMAKE_HOST_SYSTEM MATCHES Windows)
495+
swift_windows_lib_for_arch(${LFLAGS_ARCH} ${LFLAGS_ARCH}_LIB)
496+
list(APPEND library_search_directories ${${LFLAGS_ARCH}_LIB})
497+
endif()
496498

497499
# NOTE(compnerd) workaround incorrectly extensioned import libraries from
498500
# the Windows SDK on case sensitive file systems.
@@ -894,10 +896,12 @@ function(add_swift_target_library_single target name)
894896
endif()
895897
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS
896898
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}")
897-
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE)
898-
foreach(directory ${SWIFTLIB_INCLUDE})
899-
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xcc;-isystem;-Xcc;${directory})
900-
endforeach()
899+
if(NOT CMAKE_HOST_SYSTEM MATCHES Windows)
900+
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE)
901+
foreach(directory ${SWIFTLIB_INCLUDE})
902+
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xcc;-isystem;-Xcc;${directory})
903+
endforeach()
904+
endif()
901905
if("${SWIFTLIB_SINGLE_ARCHITECTURE}" MATCHES arm)
902906
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xcc;-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE)
903907
endif()
@@ -1059,9 +1063,15 @@ function(add_swift_target_library_single target name)
10591063
SWIFT_INLINE_NAMESPACE=__runtime)
10601064

10611065
if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS")
1062-
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE)
1063-
target_include_directories("${target}" SYSTEM PRIVATE
1064-
${SWIFTLIB_INCLUDE})
1066+
if(NOT CMAKE_HOST_SYSTEM MATCHES Windows)
1067+
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE)
1068+
target_include_directories("${target}" SYSTEM PRIVATE
1069+
${SWIFTLIB_INCLUDE})
1070+
endif()
1071+
if(libkind STREQUAL STATIC)
1072+
set_property(TARGET "${target}" PROPERTY
1073+
PREFIX lib)
1074+
endif()
10651075
endif()
10661076

10671077
if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS" AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
@@ -1370,10 +1380,12 @@ function(add_swift_target_library_single target name)
13701380

13711381
# Set compilation and link flags.
13721382
if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS)
1373-
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE}
1374-
${SWIFTLIB_SINGLE_ARCHITECTURE}_INCLUDE)
1375-
target_include_directories(${target} SYSTEM PRIVATE
1376-
${${SWIFTLIB_SINGLE_ARCHITECTURE}_INCLUDE})
1383+
if(NOT CMAKE_HOST_SYSTEM MATCHES Windows)
1384+
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE}
1385+
${SWIFTLIB_SINGLE_ARCHITECTURE}_INCLUDE)
1386+
target_include_directories(${target} SYSTEM PRIVATE
1387+
${${SWIFTLIB_SINGLE_ARCHITECTURE}_INCLUDE})
1388+
endif()
13771389

13781390
if(NOT ${CMAKE_C_COMPILER_ID} STREQUAL MSVC)
13791391
swift_windows_get_sdk_vfs_overlay(SWIFTLIB_SINGLE_VFS_OVERLAY)
@@ -2614,10 +2626,12 @@ function(_add_swift_target_executable_single name)
26142626
llvm_update_compile_flags("${name}")
26152627

26162628
if(SWIFTEXE_SINGLE_SDK STREQUAL WINDOWS)
2617-
swift_windows_include_for_arch(${SWIFTEXE_SINGLE_ARCHITECTURE}
2618-
${SWIFTEXE_SINGLE_ARCHITECTURE}_INCLUDE)
2619-
target_include_directories(${name} SYSTEM PRIVATE
2620-
${${SWIFTEXE_SINGLE_ARCHITECTURE}_INCLUDE})
2629+
if(NOT CMAKE_HOST_SYSTEM MATCHES Windows)
2630+
swift_windows_include_for_arch(${SWIFTEXE_SINGLE_ARCHITECTURE}
2631+
${SWIFTEXE_SINGLE_ARCHITECTURE}_INCLUDE)
2632+
target_include_directories(${name} SYSTEM PRIVATE
2633+
${${SWIFTEXE_SINGLE_ARCHITECTURE}_INCLUDE})
2634+
endif()
26212635
if(NOT ${CMAKE_C_COMPILER_ID} STREQUAL MSVC)
26222636
# MSVC doesn't support -Xclang. We don't need to manually specify
26232637
# the dependent libraries as `cl` does so.

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ function(_compile_swift_files
453453
MACCATALYST_BUILD_FLAVOR "${maccatalyst_build_flavor}"
454454
)
455455

456+
if(SWIFTFILE_STATIC)
457+
list(APPEND swift_flags -static)
458+
endif()
459+
456460
# Determine the subdirectory where the binary should be placed.
457461
set(library_subdir_sdk "${SWIFTFILE_SDK}")
458462
if(maccatalyst_build_flavor STREQUAL "ios-like")

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ add_swift_target_library(swiftCxxStdlib STATIC NO_LINK_NAME IS_STDLIB IS_SWIFT_O
1515
${SWIFT_SDK_LINUX_CXX_OVERLAY_SWIFT_COMPILE_FLAGS}
1616

1717
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
18-
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX
18+
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX WINDOWS
1919
INSTALL_IN_COMPONENT compiler
2020
INSTALL_WITH_SHARED
2121
DEPENDS libstdcxx-modulemap libcxxshim_modulemap)

stdlib/public/Platform/ucrt.modulemap

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ module ucrt [system] {
3535
export *
3636
}
3737

38+
module float {
39+
header "float.h"
40+
export *
41+
}
42+
3843
module inttypes {
3944
header "inttypes.h"
4045
export *
@@ -102,6 +107,11 @@ module ucrt [system] {
102107
export *
103108
}
104109

110+
module timeb {
111+
header "sys/timeb.h"
112+
export *
113+
}
114+
105115
module types {
106116
header "sys/types.h"
107117
export *
@@ -120,8 +130,8 @@ module ucrt [system] {
120130
export *
121131
}
122132

123-
module process {
124-
header "process.h"
133+
module dos {
134+
header "dos.h"
125135
export *
126136
}
127137

@@ -130,6 +140,21 @@ module ucrt [system] {
130140
export *
131141
}
132142

143+
module minmax {
144+
header "minmax.h"
145+
export *
146+
}
147+
148+
module multibyte {
149+
header "mbstring.h"
150+
export *
151+
}
152+
153+
module process {
154+
header "process.h"
155+
export *
156+
}
157+
133158
module wchar {
134159
header "wchar.h"
135160
export *
@@ -153,11 +178,21 @@ module corecrt [system] {
153178
export *
154179
}
155180

181+
module malloc {
182+
header "corecrt_malloc.h"
183+
export *
184+
}
185+
156186
module math {
157187
header "corecrt_math.h"
158188
export *
159189
}
160190

191+
module share {
192+
header "corecrt_share.h"
193+
export *
194+
}
195+
161196
module stdio {
162197
header "corecrt_wstdio.h"
163198
export *

stdlib/public/Platform/vcruntime.modulemap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ module vcruntime [system] {
105105
export *
106106
}
107107

108-
explicit module stdint {
108+
module stdint {
109109
header "stdint.h"
110110
export *
111111
}
@@ -667,7 +667,7 @@ module std [system] {
667667
}
668668

669669
module unordered_set {
670-
requires unordered_set
670+
requires cplusplus11
671671
header "unordered_set"
672672
export *
673673
}
@@ -702,13 +702,13 @@ module std [system] {
702702
module _Private [system] {
703703
requires cplusplus
704704

705-
explicit module xmemory {
706-
header "xmemory"
705+
explicit module xhash {
706+
header "xhash"
707707
export *
708708
}
709709

710-
explicit module xstddef {
711-
header "xstddef"
710+
explicit module xmemory {
711+
header "xmemory"
712712
export *
713713
}
714714

test/Interop/Cxx/class/constructors-copy-irgen-windows.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Target-specific tests for C++ copy constructor code generation.
22

3-
// RUN: %swift -module-name MySwift -target x86_64-unknown-windows-msvc -dump-clang-diagnostics -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s -parse-stdlib -parse-as-library -disable-legacy-type-info | %FileCheck %s -check-prefix=MICROSOFT_X64
3+
// RUN: %swift -module-name MySwift -target x86_64-unknown-windows-msvc %target-swift-flags -dump-clang-diagnostics -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s -parse-stdlib -parse-as-library -disable-legacy-type-info | %FileCheck %s -check-prefix=MICROSOFT_X64
44

55
// REQUIRES: OS=windows-msvc
66
// REQUIRES: CPU=x86_64

test/Interop/Cxx/class/constructors-irgen-windows.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Target-specific tests for C++ constructor call code generation.
22

3-
// RUN: %swift -module-name MySwift -target x86_64-unknown-windows-msvc -dump-clang-diagnostics -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s -parse-stdlib -parse-as-library -disable-legacy-type-info | %FileCheck %s -check-prefix=MICROSOFT_X64
3+
// RUN: %swift -module-name MySwift -target x86_64-unknown-windows-msvc %target-swift-flags -dump-clang-diagnostics -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s -parse-stdlib -parse-as-library -disable-legacy-type-info | %FileCheck %s -check-prefix=MICROSOFT_X64
44

55
// REQUIRES: OS=windows-msvc
66
// REQUIRES: CPU=x86_64

test/Interop/Cxx/class/copy-move-assignment-irgen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s -Xcc -fignore-exceptions -O | %FileCheck %s
1+
// RUN: %target-swift-frontend -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s -Xcc -fignore-exceptions -O | %FileCheck %s
22

33
import CopyMoveAssignment
44

0 commit comments

Comments
 (0)