Skip to content

[Target Info] Compute resource-directory relative to the SDK on non-Darwin targets #72409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ class CompilerInvocation {
StringRef mainExecutablePath, bool shared,
llvm::SmallVectorImpl<char> &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<char> &path, bool shared);

Expand Down
29 changes: 28 additions & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> &runtimeResourcePath) {
Expand All @@ -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);
Expand Down Expand Up @@ -3552,6 +3576,9 @@ bool CompilerInvocation::parseArgs(
return true;
}

if (FrontendOpts.PrintTargetInfo)
setRuntimeResourcePath(computeRuntimeResourcePathForTargetInfo());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just fixes the narrow problem of passing the right path for the Swift core modules back to swift-driver, but I raised a larger concern in the issue that motivated this pull, swiftlang/swift-driver#1562, that the swift-frontend is not looking in a passed in -sdk for those Swift core modules.

Can this pull be broadened to fix that too, especially since @compnerd has proposed looking in -sdk primarily for those Swift core modules?

Right now, the way it works is that swift-driver queries swift-frontend for what paths to use then uses those to set -sdk and -resource-dir flags to pass back to the swift-frontend for compiling new Swift files. During compilation, swift-frontend only checks -resource-dir and adjacent to the compiler for the Swift core modules like the stdlib.

I think we should change swift-frontend to look in -sdk and adjacent to the compiler instead and stop passing -resource-dir to the swift-frontend unless one was specified to swift-driver. I noted in the linked issue that the legacy C++ Driver worked that way, ie it did not pass -resource-dir to the swift-frontend unless one was passed to the Driver first.

This would be in keeping with Saleem's plan to de-emphasize -resource-dir to look for Swift core modules and look in -sdk instead.


updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
setDefaultPrebuiltCacheIfNecessary();
setDefaultBlocklistsIfNecessary();
Expand Down