Skip to content

Commit 59070a8

Browse files
committed
IRGen: rework linking against CxxStdlib
Rewrite the handling for the `CxxStdlib` implicit linking to use a slightly more functional style for filtering. Additionally, add Windows to the list providing the overlay. The Windows linking scenario is a slightly more complicated as the library names differ between static and dynamic variants to disambiguate between import libraries and static libraries. Take this into account when embedding the library name so that the linker can find the appropriate content.
1 parent e08308b commit 59070a8

11 files changed

+37
-25
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

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

test/Interop/Cxx/class/destructors-correct-abi-irgen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %swift -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s | %FileCheck %s
1+
// RUN: %swift %target-swift-flags -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s | %FileCheck %s
22

33
import Destructors
44

test/Interop/Cxx/foreign-reference/Inputs/move-only.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
#define TEST_INTEROP_CXX_FOREIGN_REFERENCE_INPUTS_MOVE_ONLY_H
33

44
#include <stdlib.h>
5-
#if defined(_WIN32)
6-
inline void *operator new(size_t, void *p) { return p; }
7-
#else
85
#include <new>
9-
#endif
106

117
#include "visibility.h"
128

test/Interop/Cxx/foreign-reference/Inputs/nullable.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
#define TEST_INTEROP_CXX_FOREIGN_REFERENCE_INPUTS_NULLABLE_H
33

44
#include <stdlib.h>
5-
#if defined(_WIN32)
6-
inline void *operator new(size_t, void *p) { return p; }
7-
#else
85
#include <new>
9-
#endif
106

117
struct __attribute__((swift_attr("import_reference")))
128
__attribute__((swift_attr("retain:immortal")))

test/Interop/Cxx/foreign-reference/Inputs/singleton.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
#define TEST_INTEROP_CXX_FOREIGN_REFERENCE_INPUTS_SINGLETON_H
33

44
#include <stdlib.h>
5-
#if defined(_WIN32)
6-
inline void *operator new(size_t, void *p) { return p; }
7-
#else
85
#include <new>
9-
#endif
106

117
#include "visibility.h"
128

test/Interop/Cxx/stdlib/use-std-pair.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
2-
//
2+
33
// REQUIRES: executable_test
44
//
55
// REQUIRES: OS=macosx || OS=linux-gnu

test/Interop/Cxx/union/anonymous-union-partly-invalid.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %swift -I %S/Inputs -enable-experimental-cxx-interop -enable-objc-interop -emit-ir %s -Xcc -fignore-exceptions | %FileCheck %s
1+
// RUN: %swift %target-swift-flags -I %S/Inputs -enable-experimental-cxx-interop -enable-objc-interop -emit-ir %s -Xcc -fignore-exceptions | %FileCheck %s
22

33
import AnonymousUnionPartlyInvalid
44

0 commit comments

Comments
 (0)