Skip to content

[REPL] When using the default resource directory, prefer loading Swift dylibs from /usr/lib/swift. #22093

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

Merged
Merged
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
3 changes: 3 additions & 0 deletions include/swift/AST/SearchPathOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class SearchPathOptions {

/// Don't look in for compiler-provided modules.
bool SkipRuntimeLibraryImportPath = false;

/// Whether the runtime library path is set to the compiler-relative default.
bool RuntimeLibraryPathIsDefault = true;

/// Return a hash code of any components from these options that should
/// contribute to a Swift Bridging PCH hash.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class CompilerInvocation {

void setMainExecutablePath(StringRef Path);

void setRuntimeResourcePath(StringRef Path);
void setRuntimeResourcePath(StringRef Path, bool IsDefault = false);

void setSDKPath(const std::string &Path) {
SearchPathOpts.SDKPath = Path;
Expand Down
6 changes: 4 additions & 2 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
llvm::sys::path::remove_filename(LibPath); // Remove /swift
llvm::sys::path::remove_filename(LibPath); // Remove /bin
llvm::sys::path::append(LibPath, "lib", "swift");
setRuntimeResourcePath(LibPath.str());
setRuntimeResourcePath(LibPath.str(), /*IsDefault=*/true);
}

static void updateRuntimeLibraryPath(SearchPathOptions &SearchPathOpts,
Expand All @@ -53,9 +53,11 @@ static void updateRuntimeLibraryPath(SearchPathOptions &SearchPathOpts,
SearchPathOpts.RuntimeLibraryImportPath = LibPath.str();
}

void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
void CompilerInvocation::setRuntimeResourcePath(StringRef Path,
bool IsDefault) {
SearchPathOpts.RuntimeResourcePath = Path;
updateRuntimeLibraryPath(SearchPathOpts, LangOpts.Target);
SearchPathOpts.RuntimeLibraryPathIsDefault = IsDefault;
}

void CompilerInvocation::setTargetTriple(StringRef Triple) {
Expand Down
28 changes: 25 additions & 3 deletions lib/Immediate/Immediate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,22 @@
using namespace swift;
using namespace swift::immediate;

/// The path for Swift libraries in the OS.
#define OS_LIBRARY_PATH "/usr/lib/swift"

static void *loadRuntimeLib(StringRef runtimeLibPathWithName) {
#if defined(_WIN32)
return LoadLibraryA(runtimeLibPathWithName.str().c_str());
#else
#if defined(__APPLE__) && defined(__MACH__)
if (!llvm::sys::path::is_absolute(runtimeLibPathWithName)) {
// Try an absolute path search for Swift in the OS first.
llvm::SmallString<128> absolutePath(OS_LIBRARY_PATH);
llvm::sys::path::append(absolutePath, runtimeLibPathWithName);
auto result = dlopen(absolutePath.c_str(), RTLD_LAZY | RTLD_GLOBAL);
if (result) return result;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to be 100% correct here, we still want to search the normal dlopen paths first, then /usr/lib/swift, then the toolchain. Maybe it's better to modify DarwinToolChains.cpp, which just sets DYLD_LIBRARY_PATH up front.

Or maybe it doesn't matter, because there's not going to be anything in /usr/lib/swift/ that conflicts with someone's own library. Hopefully.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we're OK, as /usr/lib/swift will only contain the stuff that would be bundled with the compiler.

#endif
return dlopen(runtimeLibPathWithName.str().c_str(), RTLD_LAZY | RTLD_GLOBAL);
#endif
}
Expand All @@ -66,8 +78,16 @@ static void *loadRuntimeLib(StringRef sharedLibName, StringRef runtimeLibPath) {
return loadRuntimeLib(Path);
}

void *swift::immediate::loadSwiftRuntime(StringRef runtimeLibPath) {
return loadRuntimeLib("libswiftCore" LTDL_SHLIB_EXT, runtimeLibPath);
void *swift::immediate::loadSwiftRuntime(StringRef runtimeLibPath,
bool IsDefault) {
StringRef LibName = "libswiftCore" LTDL_SHLIB_EXT;
#if defined(__APPLE__) && defined(__MACH__)
if (IsDefault) {
auto result = loadRuntimeLib(LibName);
if (result) return result;
}
#endif
return loadRuntimeLib(LibName, runtimeLibPath);
}

static bool tryLoadLibrary(LinkLibrary linkLib,
Expand Down Expand Up @@ -240,7 +260,9 @@ int swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
//
// This must be done here, before any library loading has been done, to avoid
// racing with the static initializers in user code.
auto stdlib = loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPath);
auto stdlib = loadSwiftRuntime(
Context.SearchPathOpts.RuntimeLibraryPath,
Context.SearchPathOpts.RuntimeLibraryPathIsDefault);
if (!stdlib) {
CI.getDiags().diagnose(SourceLoc(),
diag::error_immediate_mode_missing_stdlib);
Expand Down
3 changes: 2 additions & 1 deletion lib/Immediate/ImmediateImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ namespace immediate {
/// calls or \c null if an error occurred.
///
/// \param runtimeLibPath Path to search for compiler-relative stdlib dylibs.
void *loadSwiftRuntime(StringRef runtimeLibPath);
/// \param IsDefault If true, the path is the default compiler-relative path.
void *loadSwiftRuntime(StringRef runtimeLibPath, bool IsDefault);
bool tryLoadLibraries(ArrayRef<LinkLibrary> LinkLibraries,
SearchPathOptions SearchPathOpts,
DiagnosticEngine &Diags);
Expand Down
3 changes: 2 additions & 1 deletion lib/Immediate/REPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,8 @@ class REPLEnvironment {
ASTContext &Ctx = CI.getASTContext();
Ctx.LangOpts.EnableAccessControl = false;
if (!ParseStdlib) {
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPath)) {
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPath,
Ctx.SearchPathOpts.RuntimeLibraryPathIsDefault)) {
CI.getDiags().diagnose(SourceLoc(),
diag::error_immediate_mode_missing_stdlib);
return;
Expand Down