PrebuiltModules: being resilient to 0-specified build number

swift-driver passes down an SDK version number with a non-existing build number as 0.
The compiler should be resilient to this so we can locate prebuilt module cache.

rdar://72230172
This commit is contained in:
Xi Ge
2020-12-11 13:48:09 -08:00
parent f4e74f7907
commit 1ba1bd93b2
2 changed files with 46 additions and 12 deletions

View File

@@ -81,6 +81,26 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
DiagnosticOpts.LocalizationPath = std::string(DiagnosticMessagesDir.str());
}
static std::string
getVersionedPrebuiltModulePath(Optional<llvm::VersionTuple> sdkVer,
StringRef defaultPrebuiltPath) {
if (!sdkVer.hasValue())
return defaultPrebuiltPath.str();
std::string versionStr = sdkVer->getAsString();
StringRef vs = versionStr;
do {
SmallString<64> pathWithSDKVer = defaultPrebuiltPath;
llvm::sys::path::append(pathWithSDKVer, vs);
if (llvm::sys::fs::exists(pathWithSDKVer)) {
return pathWithSDKVer.str().str();
} else if (vs.endswith(".0")) {
vs = vs.substr(0, vs.size() - 2);
} else {
return defaultPrebuiltPath.str();
}
} while(true);
}
void CompilerInvocation::setDefaultPrebuiltCacheIfNecessary() {
if (!FrontendOpts.PrebuiltModuleCachePath.empty())
@@ -101,18 +121,8 @@ void CompilerInvocation::setDefaultPrebuiltCacheIfNecessary() {
// If the SDK version is given, we should check if SDK-versioned prebuilt
// module cache is available and use it if so.
if (auto ver = LangOpts.SDKVersion) {
// "../macosx/prebuilt-modules"
SmallString<64> defaultPrebuiltPathWithSDKVer = defaultPrebuiltPath;
// "../macosx/prebuilt-modules/10.15"
llvm::sys::path::append(defaultPrebuiltPathWithSDKVer, ver->getAsString());
// If the versioned prebuilt module cache exists in the disk, use it.
if (llvm::sys::fs::exists(defaultPrebuiltPathWithSDKVer)) {
FrontendOpts.PrebuiltModuleCachePath = std::string(defaultPrebuiltPathWithSDKVer.str());
return;
}
}
FrontendOpts.PrebuiltModuleCachePath = std::string(defaultPrebuiltPath.str());
FrontendOpts.PrebuiltModuleCachePath =
getVersionedPrebuiltModulePath(LangOpts.SDKVersion, defaultPrebuiltPath);
}
static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,