Skip to content

Commit 1993b4c

Browse files
authored
Merge pull request #22093 from mikeash/load-libs-from-usrlibswift
[REPL] When using the default resource directory, prefer loading Swift dylibs from /usr/lib/swift.
2 parents 81b8454 + 8358df8 commit 1993b4c

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

include/swift/AST/SearchPathOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class SearchPathOptions {
7272

7373
/// Don't look in for compiler-provided modules.
7474
bool SkipRuntimeLibraryImportPath = false;
75+
76+
/// Whether the runtime library path is set to the compiler-relative default.
77+
bool RuntimeLibraryPathIsDefault = true;
7578

7679
/// Return a hash code of any components from these options that should
7780
/// contribute to a Swift Bridging PCH hash.

include/swift/Frontend/Frontend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class CompilerInvocation {
182182

183183
void setMainExecutablePath(StringRef Path);
184184

185-
void setRuntimeResourcePath(StringRef Path);
185+
void setRuntimeResourcePath(StringRef Path, bool IsDefault = false);
186186

187187
void setSDKPath(const std::string &Path) {
188188
SearchPathOpts.SDKPath = Path;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
3939
llvm::sys::path::remove_filename(LibPath); // Remove /swift
4040
llvm::sys::path::remove_filename(LibPath); // Remove /bin
4141
llvm::sys::path::append(LibPath, "lib", "swift");
42-
setRuntimeResourcePath(LibPath.str());
42+
setRuntimeResourcePath(LibPath.str(), /*IsDefault=*/true);
4343
}
4444

4545
static void updateRuntimeLibraryPath(SearchPathOptions &SearchPathOpts,
@@ -53,9 +53,11 @@ static void updateRuntimeLibraryPath(SearchPathOptions &SearchPathOpts,
5353
SearchPathOpts.RuntimeLibraryImportPath = LibPath.str();
5454
}
5555

56-
void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
56+
void CompilerInvocation::setRuntimeResourcePath(StringRef Path,
57+
bool IsDefault) {
5758
SearchPathOpts.RuntimeResourcePath = Path;
5859
updateRuntimeLibraryPath(SearchPathOpts, LangOpts.Target);
60+
SearchPathOpts.RuntimeLibraryPathIsDefault = IsDefault;
5961
}
6062

6163
void CompilerInvocation::setTargetTriple(StringRef Triple) {

lib/Immediate/Immediate.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,22 @@
5151
using namespace swift;
5252
using namespace swift::immediate;
5353

54+
/// The path for Swift libraries in the OS.
55+
#define OS_LIBRARY_PATH "/usr/lib/swift"
56+
5457
static void *loadRuntimeLib(StringRef runtimeLibPathWithName) {
5558
#if defined(_WIN32)
5659
return LoadLibraryA(runtimeLibPathWithName.str().c_str());
5760
#else
61+
#if defined(__APPLE__) && defined(__MACH__)
62+
if (!llvm::sys::path::is_absolute(runtimeLibPathWithName)) {
63+
// Try an absolute path search for Swift in the OS first.
64+
llvm::SmallString<128> absolutePath(OS_LIBRARY_PATH);
65+
llvm::sys::path::append(absolutePath, runtimeLibPathWithName);
66+
auto result = dlopen(absolutePath.c_str(), RTLD_LAZY | RTLD_GLOBAL);
67+
if (result) return result;
68+
}
69+
#endif
5870
return dlopen(runtimeLibPathWithName.str().c_str(), RTLD_LAZY | RTLD_GLOBAL);
5971
#endif
6072
}
@@ -66,8 +78,16 @@ static void *loadRuntimeLib(StringRef sharedLibName, StringRef runtimeLibPath) {
6678
return loadRuntimeLib(Path);
6779
}
6880

69-
void *swift::immediate::loadSwiftRuntime(StringRef runtimeLibPath) {
70-
return loadRuntimeLib("libswiftCore" LTDL_SHLIB_EXT, runtimeLibPath);
81+
void *swift::immediate::loadSwiftRuntime(StringRef runtimeLibPath,
82+
bool IsDefault) {
83+
StringRef LibName = "libswiftCore" LTDL_SHLIB_EXT;
84+
#if defined(__APPLE__) && defined(__MACH__)
85+
if (IsDefault) {
86+
auto result = loadRuntimeLib(LibName);
87+
if (result) return result;
88+
}
89+
#endif
90+
return loadRuntimeLib(LibName, runtimeLibPath);
7191
}
7292

7393
static bool tryLoadLibrary(LinkLibrary linkLib,
@@ -240,7 +260,9 @@ int swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
240260
//
241261
// This must be done here, before any library loading has been done, to avoid
242262
// racing with the static initializers in user code.
243-
auto stdlib = loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPath);
263+
auto stdlib = loadSwiftRuntime(
264+
Context.SearchPathOpts.RuntimeLibraryPath,
265+
Context.SearchPathOpts.RuntimeLibraryPathIsDefault);
244266
if (!stdlib) {
245267
CI.getDiags().diagnose(SourceLoc(),
246268
diag::error_immediate_mode_missing_stdlib);

lib/Immediate/ImmediateImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ namespace immediate {
3838
/// calls or \c null if an error occurred.
3939
///
4040
/// \param runtimeLibPath Path to search for compiler-relative stdlib dylibs.
41-
void *loadSwiftRuntime(StringRef runtimeLibPath);
41+
/// \param IsDefault If true, the path is the default compiler-relative path.
42+
void *loadSwiftRuntime(StringRef runtimeLibPath, bool IsDefault);
4243
bool tryLoadLibraries(ArrayRef<LinkLibrary> LinkLibraries,
4344
SearchPathOptions SearchPathOpts,
4445
DiagnosticEngine &Diags);

lib/Immediate/REPL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,8 @@ class REPLEnvironment {
968968
ASTContext &Ctx = CI.getASTContext();
969969
Ctx.LangOpts.EnableAccessControl = false;
970970
if (!ParseStdlib) {
971-
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPath)) {
971+
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPath,
972+
Ctx.SearchPathOpts.RuntimeLibraryPathIsDefault)) {
972973
CI.getDiags().diagnose(SourceLoc(),
973974
diag::error_immediate_mode_missing_stdlib);
974975
return;

0 commit comments

Comments
 (0)