Skip to content

Commit 64b3d88

Browse files
committed
[Driver] Always link compiler_rt on Darwin
Turns out it's needed for normal builtins that can appear in inlinable functions, including Objective-C's @available. Clang always links it unconditionally, so so should Swift. Note that this does mean you have to build compiler_rt to get a successful test run on Apple platforms. That was always true if you wanted the sanitizer tests to work, though. rdar://problem/41911599
1 parent b8ed087 commit 64b3d88

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

lib/Driver/DarwinToolChains.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,29 @@ toolchains::Darwin::constructInvocation(const InterpretJobAction &job,
8484
}
8585

8686
static StringRef
87-
getDarwinLibraryNameSuffixForTriple(const llvm::Triple &triple) {
87+
getDarwinLibraryNameSuffixForTriple(const llvm::Triple &triple,
88+
bool distinguishSimulator = true) {
8889
switch (getDarwinPlatformKind(triple)) {
8990
case DarwinPlatformKind::MacOS:
9091
return "osx";
92+
case DarwinPlatformKind::IPhoneOSSimulator:
93+
if (distinguishSimulator)
94+
return "iossim";
95+
LLVM_FALLTHROUGH;
9196
case DarwinPlatformKind::IPhoneOS:
9297
return "ios";
93-
case DarwinPlatformKind::IPhoneOSSimulator:
94-
return "iossim";
98+
case DarwinPlatformKind::TvOSSimulator:
99+
if (distinguishSimulator)
100+
return "tvossim";
101+
LLVM_FALLTHROUGH;
95102
case DarwinPlatformKind::TvOS:
96103
return "tvos";
97-
case DarwinPlatformKind::TvOSSimulator:
98-
return "tvossim";
104+
case DarwinPlatformKind::WatchOSSimulator:
105+
if (distinguishSimulator)
106+
return "watchossim";
107+
LLVM_FALLTHROUGH;
99108
case DarwinPlatformKind::WatchOS:
100109
return "watchos";
101-
case DarwinPlatformKind::WatchOSSimulator:
102-
return "watchossim";
103110
}
104111
llvm_unreachable("Unsupported Darwin platform");
105112
}
@@ -261,6 +268,13 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
261268

262269
assert(Triple.isOSDarwin());
263270

271+
// Always link the regular compiler_rt.
272+
addLinkRuntimeLib(context.Args, Arguments,
273+
(Twine("libclang_rt.") +
274+
getDarwinLibraryNameSuffixForTriple(Triple,
275+
/*simulator*/false) +
276+
".a").str());
277+
264278
// FIXME: If we used Clang as a linker instead of going straight to ld,
265279
// we wouldn't have to replicate Clang's logic here.
266280
bool wantsObjCRuntime = false;

test/Driver/linker.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// CHECK: -o [[OBJECTFILE:.*]]
7878

7979
// CHECK-NEXT: bin/ld{{"? }}
80+
// CHECK-DAG: lib/darwin/libclang_rt.osx.a
8081
// CHECK-DAG: [[OBJECTFILE]]
8182
// CHECK-DAG: -L [[STDLIB_PATH:[^ ]+/lib/swift/macosx]]
8283
// CHECK-DAG: -rpath [[STDLIB_PATH]]
@@ -87,7 +88,7 @@
8788

8889
// SIMPLE: bin/ld{{"? }}
8990
// SIMPLE-NOT: -syslibroot
90-
// SIMPLE-DAG: -macosx_version_min 10.{{[0-9]+}}.{{[0-9]+}}
91+
// SIMPLE: -macosx_version_min 10.{{[0-9]+}}.{{[0-9]+}}
9192
// SIMPLE-NOT: -syslibroot
9293
// SIMPLE: -o linker
9394

@@ -97,6 +98,7 @@
9798

9899
// SIMPLE_STATIC-NEXT: bin/ld{{"? }}
99100
// SIMPLE_STATIC: [[OBJECTFILE]]
101+
// SIMPLE_STATIC: lib/darwin/libclang_rt.osx.a
100102
// SIMPLE_STATIC: -lobjc
101103
// SIMPLE_STATIC: -lSystem
102104
// SIMPLE_STATIC: -arch x86_64
@@ -118,6 +120,7 @@
118120
// IOS_SIMPLE-DAG: -lSystem
119121
// IOS_SIMPLE-DAG: -arch x86_64
120122
// IOS_SIMPLE-DAG: -ios_simulator_version_min 7.1.{{[0-9]+}}
123+
// IOS_SIMPLE-DAG: lib/darwin/libclang_rt.ios.a
121124
// IOS_SIMPLE: -o linker
122125

123126

@@ -130,6 +133,7 @@
130133
// tvOS_SIMPLE-DAG: -lSystem
131134
// tvOS_SIMPLE-DAG: -arch x86_64
132135
// tvOS_SIMPLE-DAG: -tvos_simulator_version_min 9.0.{{[0-9]+}}
136+
// tvOS_SIMPLE-DAG: lib/darwin/libclang_rt.tvos.a
133137
// tvOS_SIMPLE: -o linker
134138

135139

@@ -142,6 +146,7 @@
142146
// watchOS_SIMPLE-DAG: -lSystem
143147
// watchOS_SIMPLE-DAG: -arch i386
144148
// watchOS_SIMPLE-DAG: -watchos_simulator_version_min 2.0.{{[0-9]+}}
149+
// watchOS_SIMPLE-DAG: lib/darwin/libclang_rt.watchos.a
145150
// watchOS_SIMPLE: -o linker
146151

147152

@@ -312,11 +317,11 @@
312317

313318

314319
// FILELIST: bin/ld{{"? }}
315-
// FILELIST-NOT: .o
320+
// FILELIST-NOT: .o{{"? }}
316321
// FILELIST: -filelist {{"?[^-]}}
317-
// FILELIST-NOT: .o
318-
// FILELIST: /a.o
319-
// FILELIST-NOT: .o
322+
// FILELIST-NOT: .o{{"? }}
323+
// FILELIST: /a.o{{"? }}
324+
// FILELIST-NOT: .o{{"? }}
320325
// FILELIST: -o linker
321326

322327

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdbool.h>
2+
3+
static inline bool testNewOS() {
4+
if (__builtin_available(macOS 10.12, *)) {
5+
return true;
6+
} else {
7+
return false;
8+
}
9+
}

validation-test/Driver/clang_rt.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-build-swift -emit-executable %s -import-objc-header %S/Inputs/clang_rt-helper.h -o %t
2+
3+
// Just make sure we can build and link successfully.
4+
if testNewOS() {
5+
print("new!")
6+
} else {
7+
print("old...")
8+
}

0 commit comments

Comments
 (0)