Skip to content

Commit 450aba7

Browse files
authored
Search for SwiftShims in SDK (#23287)
…but only if the compiler resource directory doesn’t have them. These will move to the SDK soon.
1 parent d01e80d commit 450aba7

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
424424
});
425425
}
426426

427+
// If there are no shims in the resource dir, add a search path in the SDK.
428+
SmallString<128> shimsPath(searchPathOpts.RuntimeResourcePath);
429+
llvm::sys::path::append(shimsPath, "shims");
430+
if (!llvm::sys::fs::exists(shimsPath)) {
431+
shimsPath = searchPathOpts.SDKPath;
432+
llvm::sys::path::append(shimsPath, "usr", "lib", "swift", "shims");
433+
invocationArgStrs.insert(invocationArgStrs.end(), {
434+
"-isystem", shimsPath.str()
435+
});
436+
}
437+
427438
// Construct the invocation arguments for the current target.
428439
// Add target-independent options first.
429440
invocationArgStrs.insert(invocationArgStrs.end(), {

test/Serialization/runtime-import-from-sdk.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@
55
// resource directory will contain a swiftmodule for the standard library.
66

77
// %t/good-sdk contains a loadable standard library.
8+
// RUN: %empty-directory(%t/good-sdk)
89
// RUN: %empty-directory(%t/good-sdk/usr/lib/swift)
910
// RUN: cp -r %platform-module-dir/Swift.swiftmodule %t/good-sdk/usr/lib/swift/Swift.swiftmodule
1011

1112
// %t/bad-sdk contains an invalid standard library that cannot be loaded.
13+
// RUN: %empty-directory(%t/bad-sdk)
1214
// RUN: %empty-directory(%t/bad-sdk/usr/lib/swift/Swift.swiftmodule)
1315
// RUN: touch %t/bad-sdk/usr/lib/swift/Swift.swiftmodule/garbage-garbage-garbage.swiftmodule
1416

1517
// %t/empty-toolchain does not contain a standard library.
18+
// RUN: %empty-directory(%t/empty-toolchain)
1619
// RUN: %empty-directory(%t/empty-toolchain/usr/lib/swift)
1720

1821
// FIXME: Until we have private imports, we need SwiftShims in the toolchain.
1922
// RUN: cp -r %test-resource-dir/shims %t/empty-toolchain/usr/lib/swift/shims
2023

24+
// %t/really-empty-toolchain does not contain a standard library or SwiftShims.
25+
// RUN: %empty-directory(%t/really-empty-toolchain)
26+
// RUN: %empty-directory(%t/really-empty-toolchain/usr/lib/swift)
27+
2128
// If the compiler's resource directory does not contain a runtime swiftmodule,
2229
// we should fall back to the SDK.
2330

@@ -34,14 +41,26 @@
3441
// If neither the resource directory nor the SDK contains a runtime swiftmodule,
3542
// loading should fail. This just proves that we aren't getting runtime imports
3643
// some other way.
44+
//
45+
// We also check that ClangImporter noticed SwiftShims in the toolchain and
46+
// didn't add a -isystem flag to look in the SDK.
3747

3848
// FIXME: We can't properly test this on a non-Darwin platform because we'll get
3949
// the same error message for "unloadable standard library" and "no standard
4050
// library". (SR-10097)
4151
// REQUIRES: objc_interop
4252

4353
// RUN: %empty-directory(%t/mcp)
44-
// RUN: not %target-swift-frontend(mock-sdk: -sdk %t/bad-sdk) -resource-dir %t/empty-toolchain/usr/lib/swift -module-cache-path %t/mcp -typecheck %s 2>&1 | %FileCheck %s
45-
// CHECK: error: could not find module 'Swift' for target '{{.*}}'; found: garbage-garbage-garbage
54+
// RUN: not %target-swift-frontend(mock-sdk: -sdk %t/bad-sdk) -resource-dir %t/empty-toolchain/usr/lib/swift -module-cache-path %t/mcp -typecheck %s -dump-clang-diagnostics 2>&1 | %FileCheck --check-prefix CHECK-EMPTY %s
55+
// CHECK-EMPTY-NOT: '-isystem' '{{.*}}/bad-sdk/usr/lib/swift/shims'
56+
// CHECK-EMPTY: error: could not find module 'Swift' for target '{{.*}}'; found: garbage-garbage-garbage
57+
58+
// Check that, when the toolchain *doesn't* have SwiftShims in it, ClagImporter
59+
// *does* add a -I flag to look in the SDK.
60+
61+
// RUN: %empty-directory(%t/mcp)
62+
// RUN: not %target-swift-frontend(mock-sdk: -sdk %t/bad-sdk) -resource-dir %t/really-empty-toolchain/usr/lib/swift -module-cache-path %t/mcp -typecheck %s -dump-clang-diagnostics 2>&1 | %FileCheck --check-prefix CHECK-REALLY-EMPTY %s
63+
// CHECK-REALLY-EMPTY: '-isystem' '{{.*}}/bad-sdk/usr/lib/swift/shims'
64+
// CHECK-REALLY-EMPTY: error: could not find module 'Swift' for target '{{.*}}'; found: garbage-garbage-garbage
4665

4766
let x: Int = 1

0 commit comments

Comments
 (0)