diff --git a/include/swift/Frontend/Frontend.h b/include/swift/Frontend/Frontend.h index e60e228fe3e28..1b24e396fd791 100644 --- a/include/swift/Frontend/Frontend.h +++ b/include/swift/Frontend/Frontend.h @@ -262,6 +262,10 @@ class CompilerInvocation { StringRef mainExecutablePath, bool shared, llvm::SmallVectorImpl &runtimeResourcePath); + /// Computes the runtime resource path relative to the given Swift + /// executable. + std::string computeRuntimeResourcePathForTargetInfo(); + /// Appends `lib/swift[_static]` to the given path static void appendSwiftLibDir(llvm::SmallVectorImpl &path, bool shared); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 7da1b01653e76..b3e5f7c4be4cb 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -63,6 +63,28 @@ getVersionTuple(const llvm::Triple &triple) { return triple.getOSVersion(); } +std::string CompilerInvocation::computeRuntimeResourcePathForTargetInfo() { + const auto &frontendOpts = getFrontendOptions(); + const auto &searchPathOpts = getSearchPathOptions(); + const auto &langOpts = getLangOptions(); + SmallString<128> resourceDirPath; + if (!searchPathOpts.RuntimeResourcePath.empty()) { + resourceDirPath = searchPathOpts.RuntimeResourcePath; + } else if (!langOpts.Target.isOSDarwin() && + !searchPathOpts.getSDKPath().empty()) { + StringRef value = searchPathOpts.getSDKPath(); + resourceDirPath.append(value.begin(), value.end()); + llvm::sys::path::append(resourceDirPath, "usr"); + CompilerInvocation::appendSwiftLibDir(resourceDirPath, + frontendOpts.UseSharedResourceFolder); + } else { + CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(frontendOpts.MainExecutablePath, + frontendOpts.UseSharedResourceFolder, + resourceDirPath); + } + return resourceDirPath.str().str(); +} + void CompilerInvocation::computeRuntimeResourcePathFromExecutablePath( StringRef mainExecutablePath, bool shared, llvm::SmallVectorImpl &runtimeResourcePath) { @@ -84,7 +106,9 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) { llvm::SmallString<128> LibPath; computeRuntimeResourcePathFromExecutablePath( Path, FrontendOpts.UseSharedResourceFolder, LibPath); - setRuntimeResourcePath(LibPath.str()); + // Target info query computes the resource path wholesale + if (!FrontendOpts.PrintTargetInfo) + setRuntimeResourcePath(LibPath.str()); llvm::SmallString<128> clangPath(Path); llvm::sys::path::remove_filename(clangPath); @@ -3552,6 +3576,9 @@ bool CompilerInvocation::parseArgs( return true; } + if (FrontendOpts.PrintTargetInfo) + setRuntimeResourcePath(computeRuntimeResourcePathForTargetInfo()); + updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts); setDefaultPrebuiltCacheIfNecessary(); setDefaultBlocklistsIfNecessary();