Skip to content

Commit d466883

Browse files
committed
[Driver] Only link to compiler_rt if present for the target platform
Tweak the tests to check this correctly.
1 parent 64b3d88 commit d466883

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

lib/Driver/DarwinToolChains.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,23 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
268268

269269
assert(Triple.isOSDarwin());
270270

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-
278271
// FIXME: If we used Clang as a linker instead of going straight to ld,
279-
// we wouldn't have to replicate Clang's logic here.
272+
// we wouldn't have to replicate a bunch of Clang's logic here.
273+
274+
// Always link the regular compiler_rt if it's present.
275+
//
276+
// Note: Normally we'd just add this unconditionally, but it's valid to build
277+
// Swift and use it as a linker without building compiler_rt.
278+
SmallString<128> CompilerRTPath;
279+
getClangLibraryPath(context.Args, CompilerRTPath);
280+
llvm::sys::path::append(
281+
CompilerRTPath,
282+
Twine("libclang_rt.") +
283+
getDarwinLibraryNameSuffixForTriple(Triple, /*simulator*/false) +
284+
".a");
285+
if (llvm::sys::fs::exists(CompilerRTPath))
286+
Arguments.push_back(context.Args.MakeArgString(CompilerRTPath));
287+
280288
bool wantsObjCRuntime = false;
281289
if (Triple.isiOS())
282290
wantsObjCRuntime = Triple.isOSVersionLT(9);
@@ -389,9 +397,8 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
389397
}
390398

391399
if (context.Args.hasArg(options::OPT_profile_generate)) {
392-
SmallString<128> LibProfile(RuntimeLibPath);
393-
llvm::sys::path::remove_filename(LibProfile); // remove platform name
394-
llvm::sys::path::append(LibProfile, "clang", "lib", "darwin");
400+
SmallString<128> LibProfile;
401+
getClangLibraryPath(context.Args, LibProfile);
395402

396403
StringRef RT;
397404
if (Triple.isiOS()) {

test/Driver/linker-clang_rt.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// REQUIRES: OS=macosx
2+
// Note: This is really about the /host/ environment, but since there are RUN
3+
// lines for multiple targets anyway it doesn't make a huge difference.
4+
5+
// We use hard links to make sure the Swift driver really thinks it's been
6+
// moved.
7+
8+
// RUN: rm -rf %t
9+
// RUN: %empty-directory(%t/bin)
10+
// RUN: %hardlink-or-copy(from: %swift_driver_plain, to: %t/bin/swiftc)
11+
// RUN: %empty-directory(%t/lib/clang/darwin/)
12+
13+
// RUN: %t/bin/swiftc -driver-print-jobs -target x86_64-apple-macosx10.9 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix CHECK-NO-RUNTIME %s
14+
15+
// RUN: touch %t/lib/clang/darwin/libclang_rt.osx.a %t/lib/clang/darwin/libclang_rt.ios.a %t/lib/clang/darwin/libclang_rt.tvos.a %t/lib/clang/darwin/libclang_rt.watchos.a
16+
17+
// RUN: %t/bin/swiftc -driver-print-jobs -target x86_64-apple-macosx10.9 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix MACOS %s
18+
19+
// RUN: %t/bin/swiftc -driver-print-jobs -target i386-apple-ios7 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix IOS %s
20+
// RUN: %t/bin/swiftc -driver-print-jobs -target x86_64-apple-ios7 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix IOS %s
21+
// RUN: %t/bin/swiftc -driver-print-jobs -target armv7s-apple-ios7 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix IOS %s
22+
// RUN: %t/bin/swiftc -driver-print-jobs -target arm64-apple-ios7 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix IOS %s
23+
24+
// RUN: %t/bin/swiftc -driver-print-jobs -target x86_64-apple-ios9 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix TVOS %s
25+
// RUN: %t/bin/swiftc -driver-print-jobs -target arm64-apple-tvos9 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix TVOS %s
26+
27+
// RUN: %t/bin/swiftc -driver-print-jobs -target i386-apple-watchos2 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix WATCHOS %s
28+
// RUN: %t/bin/swiftc -driver-print-jobs -target armv7k-apple-watchos2 %S/../Inputs/empty.swift | %FileCheck -check-prefix CHECK -check-prefix WATCHOS %s
29+
30+
// CHECK: bin/ld{{"? }}
31+
// CHECK-NO-RUNTIME-NOT: libclang_rt
32+
// CHECK-MACOS-SAME: {{[^ ]+/lib/clang/darwin/libclang_rt.osx.a}}
33+
// CHECK-IOS-SAME: {{[^ ]+/lib/clang/darwin/libclang_rt.ios.a}}
34+
// CHECK-TVOS-SAME: {{[^ ]+/lib/clang/darwin/libclang_rt.tvos.a}}
35+
// CHECK-WATCHOS-SAME: {{[^ ]+/lib/clang/darwin/libclang_rt.watchos.a}}
36+
// CHECK-SAME: -o {{[^ ]+}}

test/Driver/linker.swift

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

7979
// CHECK-NEXT: bin/ld{{"? }}
80-
// CHECK-DAG: lib/darwin/libclang_rt.osx.a
8180
// CHECK-DAG: [[OBJECTFILE]]
8281
// CHECK-DAG: -L [[STDLIB_PATH:[^ ]+/lib/swift/macosx]]
8382
// CHECK-DAG: -rpath [[STDLIB_PATH]]
@@ -98,7 +97,6 @@
9897

9998
// SIMPLE_STATIC-NEXT: bin/ld{{"? }}
10099
// SIMPLE_STATIC: [[OBJECTFILE]]
101-
// SIMPLE_STATIC: lib/darwin/libclang_rt.osx.a
102100
// SIMPLE_STATIC: -lobjc
103101
// SIMPLE_STATIC: -lSystem
104102
// SIMPLE_STATIC: -arch x86_64
@@ -120,7 +118,6 @@
120118
// IOS_SIMPLE-DAG: -lSystem
121119
// IOS_SIMPLE-DAG: -arch x86_64
122120
// IOS_SIMPLE-DAG: -ios_simulator_version_min 7.1.{{[0-9]+}}
123-
// IOS_SIMPLE-DAG: lib/darwin/libclang_rt.ios.a
124121
// IOS_SIMPLE: -o linker
125122

126123

@@ -133,7 +130,6 @@
133130
// tvOS_SIMPLE-DAG: -lSystem
134131
// tvOS_SIMPLE-DAG: -arch x86_64
135132
// tvOS_SIMPLE-DAG: -tvos_simulator_version_min 9.0.{{[0-9]+}}
136-
// tvOS_SIMPLE-DAG: lib/darwin/libclang_rt.tvos.a
137133
// tvOS_SIMPLE: -o linker
138134

139135

@@ -146,7 +142,6 @@
146142
// watchOS_SIMPLE-DAG: -lSystem
147143
// watchOS_SIMPLE-DAG: -arch i386
148144
// watchOS_SIMPLE-DAG: -watchos_simulator_version_min 2.0.{{[0-9]+}}
149-
// watchOS_SIMPLE-DAG: lib/darwin/libclang_rt.watchos.a
150145
// watchOS_SIMPLE: -o linker
151146

152147

test/lit.cfg

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,8 +945,9 @@ def source_compiler_rt_libs(path):
945945
if lib.startswith('libclang_rt.')
946946
and config.compiler_rt_platform in lib])
947947

948-
source_compiler_rt_libs(make_path(test_resource_dir, 'clang', 'lib',
949-
platform.system().lower()))
948+
compiler_rt_dir = make_path(test_resource_dir, 'clang', 'lib',
949+
platform.system().lower())
950+
source_compiler_rt_libs(compiler_rt_dir)
950951

951952
def check_runtime_libs(features_to_check):
952953
for lib in config.compiler_rt_libs:
@@ -967,6 +968,17 @@ if run_ptrsize != "32":
967968

968969
check_runtime_libs(runtime_libs)
969970

971+
# From https://stackoverflow.com/a/2393022
972+
def strip_right(text, suffix):
973+
if not text.endswith(suffix):
974+
return text
975+
return text[:len(text)-len(suffix)]
976+
977+
base_runtime_lib_name = (
978+
'libclang_rt.' + strip_right(config.compiler_rt_platform, 'sim') + '.a')
979+
if os.path.exists(make_path(compiler_rt_dir, base_runtime_lib_name)):
980+
config.available_features.add('c_runtime')
981+
970982
if not getattr(config, 'target_run_simple_swift', None):
971983
config.target_run_simple_swift = (
972984
'%%empty-directory(%%t) && '

validation-test/Driver/clang_rt.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %target-build-swift -emit-executable %s -import-objc-header %S/Inputs/clang_rt-helper.h -o %t
22

3+
// REQUIRES: c_runtime
4+
35
// Just make sure we can build and link successfully.
6+
47
if testNewOS() {
58
print("new!")
69
} else {

0 commit comments

Comments
 (0)