Skip to content

Commit b2f63aa

Browse files
authored
Merge pull request swiftlang#35067 from nkcsgexi/72230172
PrebuiltModules: being resilient to 0-specified build number
2 parents 6ec1499 + 1ba1bd9 commit b2f63aa

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

lib/Frontend/CompilerInvocation.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
8181
DiagnosticOpts.LocalizationPath = std::string(DiagnosticMessagesDir.str());
8282
}
8383

84+
static std::string
85+
getVersionedPrebuiltModulePath(Optional<llvm::VersionTuple> sdkVer,
86+
StringRef defaultPrebuiltPath) {
87+
if (!sdkVer.hasValue())
88+
return defaultPrebuiltPath.str();
89+
std::string versionStr = sdkVer->getAsString();
90+
StringRef vs = versionStr;
91+
do {
92+
SmallString<64> pathWithSDKVer = defaultPrebuiltPath;
93+
llvm::sys::path::append(pathWithSDKVer, vs);
94+
if (llvm::sys::fs::exists(pathWithSDKVer)) {
95+
return pathWithSDKVer.str().str();
96+
} else if (vs.endswith(".0")) {
97+
vs = vs.substr(0, vs.size() - 2);
98+
} else {
99+
return defaultPrebuiltPath.str();
100+
}
101+
} while(true);
102+
}
103+
84104
void CompilerInvocation::setDefaultPrebuiltCacheIfNecessary() {
85105

86106
if (!FrontendOpts.PrebuiltModuleCachePath.empty())
@@ -101,18 +121,8 @@ void CompilerInvocation::setDefaultPrebuiltCacheIfNecessary() {
101121

102122
// If the SDK version is given, we should check if SDK-versioned prebuilt
103123
// module cache is available and use it if so.
104-
if (auto ver = LangOpts.SDKVersion) {
105-
// "../macosx/prebuilt-modules"
106-
SmallString<64> defaultPrebuiltPathWithSDKVer = defaultPrebuiltPath;
107-
// "../macosx/prebuilt-modules/10.15"
108-
llvm::sys::path::append(defaultPrebuiltPathWithSDKVer, ver->getAsString());
109-
// If the versioned prebuilt module cache exists in the disk, use it.
110-
if (llvm::sys::fs::exists(defaultPrebuiltPathWithSDKVer)) {
111-
FrontendOpts.PrebuiltModuleCachePath = std::string(defaultPrebuiltPathWithSDKVer.str());
112-
return;
113-
}
114-
}
115-
FrontendOpts.PrebuiltModuleCachePath = std::string(defaultPrebuiltPath.str());
124+
FrontendOpts.PrebuiltModuleCachePath =
125+
getVersionedPrebuiltModulePath(LangOpts.SDKVersion, defaultPrebuiltPath);
116126
}
117127

118128
static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,

test/ModuleInterface/default-prebuilt-module-location-sdk-versioned.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
// 6. Make sure we installed a forwarding module in the module cache.
2525
// RUN: %{python} %S/ModuleCache/Inputs/check-is-forwarding-module.py %t/ModuleCache/PrebuiltModule-*.swiftmodule
2626

27+
// 5.1. Import this prebuilt module, but DON'T pass in -prebuilt-module-cache-path, it should use the implicit one from the SDK-versioned prebuilt module cache dir.
28+
// RUN: %target-swift-frontend -typecheck -resource-dir %t/ResourceDir -I %t %s -parse-stdlib -module-cache-path %t/ModuleCache -sdk %t -target-sdk-version 10.15.0
29+
30+
// 6.1. Make sure we installed a forwarding module in the module cache.
31+
// RUN: %{python} %S/ModuleCache/Inputs/check-is-forwarding-module.py %t/ModuleCache/PrebuiltModule-*.swiftmodule
32+
33+
// 5.2. Import this prebuilt module, but DON'T pass in -prebuilt-module-cache-path, it should use the implicit one from the SDK-versioned prebuilt module cache dir.
34+
// RUN: %target-swift-frontend -typecheck -resource-dir %t/ResourceDir -I %t %s -parse-stdlib -module-cache-path %t/ModuleCache -sdk %t -target-sdk-version 10.15.0.0
35+
36+
// 6.2. Make sure we installed a forwarding module in the module cache.
37+
// RUN: %{python} %S/ModuleCache/Inputs/check-is-forwarding-module.py %t/ModuleCache/PrebuiltModule-*.swiftmodule
38+
2739
// 7. Remove the prebuilt module from the SDK-versioned prebuilt module cache dir.
2840
// RUN: %empty-directory(%t/ResourceDir/%target-sdk-name/prebuilt-modules/10.15)
2941

@@ -33,6 +45,18 @@
3345
// 9. Make sure we built a binary module in the module cache.
3446
// RUN: not %{python} %S/ModuleCache/Inputs/check-is-forwarding-module.py %t/ModuleCache/PrebuiltModule-*.swiftmodule
3547

48+
// 8.1. Import this prebuilt module, it should not find the prebuilt module cache.
49+
// RUN: %target-swift-frontend -typecheck -resource-dir %t/ResourceDir -I %t %s -parse-stdlib -module-cache-path %t/ModuleCache -sdk %t -target-sdk-version 10.15.0
50+
51+
// 9.1. Make sure we built a binary module in the module cache.
52+
// RUN: not %{python} %S/ModuleCache/Inputs/check-is-forwarding-module.py %t/ModuleCache/PrebuiltModule-*.swiftmodule
53+
54+
// 8.2. Import this prebuilt module, it should not find the prebuilt module cache.
55+
// RUN: %target-swift-frontend -typecheck -resource-dir %t/ResourceDir -I %t %s -parse-stdlib -module-cache-path %t/ModuleCache -sdk %t -target-sdk-version 10.15.0.0
56+
57+
// 9.2. Make sure we built a binary module in the module cache.
58+
// RUN: not %{python} %S/ModuleCache/Inputs/check-is-forwarding-module.py %t/ModuleCache/PrebuiltModule-*.swiftmodule
59+
3660
import PrebuiltModule
3761

3862
func x<T>(_ x: T) {}

0 commit comments

Comments
 (0)