Skip to content

Commit 1040116

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 6076221 commit 1040116

11 files changed

+37
-27
lines changed

lib/IRGen/GenDecl.cpp

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

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

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 & 3 deletions
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
import StdlibUnittest
@@ -15,7 +15,6 @@ StdPairTestSuite.test("StdPairInts.init") {
1515
expectEqual(pi.second, 2)
1616
}
1717

18-
#if !os(Windows) // FIXME: enable once swiftCxxStdlib is built on Windows (https://github.com/apple/swift/issues/67649)
1918
StdPairTestSuite.test("StdPairStrings.init") {
2019
let ps = PairStrings(first: std.string(), second: std.string())
2120
expectEqual(ps.first, std.string())
@@ -25,7 +24,6 @@ StdPairTestSuite.test("StdPairStrings.init") {
2524
expectEqual(ps2.first, std.string("abc"))
2625
expectEqual(ps2.second, std.string("123"))
2726
}
28-
#endif
2927

3028
StdPairTestSuite.test("StdPair.elements") {
3129
var pi = getIntPair()

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

test/Interop/lit.local.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ if is_cf_options_interop_updated:
2828
config.available_features.add('cxx-interop-fixed-cf_options')
2929

3030
if get_target_os() in ['windows-msvc']:
31+
import os
3132
config.substitutions.insert(0, ('%target-abi', 'WIN'))
3233
# Clang should build object files with link settings equivalent to -libc MD
3334
# when building for the MSVC target.
3435
clang_opt = clang_compile_opt + '-D_MT -D_DLL -Xclang --dependent-lib=msvcrt -Xclang --dependent-lib=oldnames '
36+
config.substitutions.insert(0, ('%target-swift-flags', '-vfsoverlay {}'.format(os.path.join(config.swift_obj_root,
37+
'stdlib',
38+
'windows-vfs-overlay.yaml'))))
3539
else:
3640
# FIXME(compnerd) do all the targets we currently support use SysV ABI?
3741
config.substitutions.insert(0, ('%target-abi', 'SYSV'))
42+
config.substitutions.insert(0, ('%target-swift-flags', ''))
3843

3944
# Enable C++ interop when compiling Swift sources.
4045
config.substitutions.insert(0, ('%target-interop-build-swift',

0 commit comments

Comments
 (0)