Skip to content

Commit c177674

Browse files
committed
Load runtime library modules from SDK
With this change, swiftc will still look for standard library and overlay modules in ../lib/swift (relative to the compiler), but if it doesn’t find them there, it will now look in usr/lib/swift in the SDK.
1 parent 46ddb2a commit c177674

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
4949
llvm::sys::path::append(LibPath, getPlatformNameForTriple(Triple));
5050
SearchPathOpts.RuntimeLibraryPath = LibPath.str();
5151

52+
// Set up the import paths containing the swiftmodules for the libraries in
53+
// RuntimeLibraryPath.
5254
SearchPathOpts.RuntimeLibraryImportPaths.clear();
5355

5456
// If this is set, we don't want any runtime import paths.
@@ -58,6 +60,12 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
5860
if (!Triple.isOSDarwin())
5961
llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple));
6062
SearchPathOpts.RuntimeLibraryImportPaths.push_back(LibPath.str());
63+
64+
if (!SearchPathOpts.SDKPath.empty()) {
65+
LibPath = SearchPathOpts.SDKPath;
66+
llvm::sys::path::append(LibPath, "usr", "lib", "swift");
67+
SearchPathOpts.RuntimeLibraryImportPaths.push_back(LibPath.str());
68+
}
6169
}
6270

6371
void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// Tests the fallback behavior for runtime library import paths. These should
2+
/// prefer the resource directory, but fall back to the SDK.
3+
4+
// Assumption: We build the standard library with the compiler, so the default
5+
// resource directory will contain a swiftmodule for the standard library.
6+
7+
// %t/good-sdk contains a loadable standard library.
8+
// RUN: %empty-directory(%t/good-sdk/usr/lib/swift)
9+
// RUN: cp -r %platform-module-dir/Swift.swiftmodule %t/good-sdk/usr/lib/swift/Swift.swiftmodule
10+
11+
// %t/bad-sdk contains an invalid standard library that cannot be loaded.
12+
// RUN: %empty-directory(%t/bad-sdk/usr/lib/swift/Swift.swiftmodule)
13+
// RUN: touch %t/bad-sdk/usr/lib/swift/Swift.swiftmodule/garbage-garbage-garbage.swiftmodule
14+
15+
// %t/empty-toolchain does not contain a standard library.
16+
// RUN: %empty-directory(%t/empty-toolchain/usr/lib/swift)
17+
18+
// FIXME: Until we have private imports, we need SwiftShims in the toolchain.
19+
// RUN: cp -r %test-resource-dir/shims %t/empty-toolchain/usr/lib/swift/shims
20+
21+
// If the compiler's resource directory does not contain a runtime swiftmodule,
22+
// we should fall back to the SDK.
23+
24+
// RUN: %empty-directory(%t/mcp)
25+
// RUN: %target-swift-frontend(mock-sdk: -sdk %t/good-sdk) -resource-dir %t/empty-toolchain/usr/lib/swift -module-cache-path %t/mcp -typecheck -verify %s
26+
27+
// If the compiler's resource directory *does* contain a runtime swiftmodule, we
28+
// should *not* use the one in the SDK. (We assume that the resource directory
29+
// built with this compiler does contain a standard library.)
30+
31+
// RUN: %empty-directory(%t/mcp)
32+
// RUN: %target-swift-frontend(mock-sdk: -sdk %t/bad-sdk) -module-cache-path %t/mcp -typecheck -verify %s
33+
34+
// If neither the resource directory nor the SDK contains a runtime swiftmodule,
35+
// loading should fail. This just proves that we aren't getting runtime imports
36+
// some other way.
37+
38+
// FIXME: We can't properly test this on a non-Darwin platform because we'll get
39+
// the same error message for "unloadable standard library" and "no standard
40+
// library". (SR-10097)
41+
// REQUIRES: objc_interop
42+
43+
// 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
46+
47+
let x: Int = 1

test/lit.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ else:
310310
resource_dir_opt = ""
311311
stdlib_resource_dir_opt = resource_dir_opt
312312
sourcekitd_framework_dir = config.swift_lib_dir
313+
config.substitutions.append( ('%test-resource-dir', test_resource_dir) )
313314
lit_config.note('Using resource dir: ' + test_resource_dir)
314315

315316
# Parse the variant triple.

0 commit comments

Comments
 (0)