Skip to content

Commit 4a5c1a0

Browse files
authored
Merge pull request #24838 from mikeash/usrlibswift-fallback
[Interpreter] Fall back to loading Swift dylibs from /usr/lib/swift on Apple platforms.
2 parents fffaea1 + 9df513a commit 4a5c1a0

File tree

7 files changed

+48
-14
lines changed

7 files changed

+48
-14
lines changed

include/swift/AST/SearchPathOptions.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ class SearchPathOptions {
6464
/// Path to search for compiler-relative header files.
6565
std::string RuntimeResourcePath;
6666

67-
/// Path to search for compiler-relative stdlib dylibs.
68-
std::string RuntimeLibraryPath;
67+
/// Paths to search for compiler-relative stdlib dylibs, in order of
68+
/// preference.
69+
std::vector<std::string> RuntimeLibraryPaths;
6970

7071
/// Paths to search for stdlib modules. One of these will be compiler-relative.
7172
std::vector<std::string> RuntimeLibraryImportPaths;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
using namespace swift;
3131
using namespace llvm::opt;
3232

33+
/// The path for Swift libraries in the OS on Darwin.
34+
#define DARWIN_OS_LIBRARY_PATH "/usr/lib/swift"
35+
3336
swift::CompilerInvocation::CompilerInvocation() {
3437
setTargetTriple(llvm::sys::getDefaultTargetTriple());
3538
}
@@ -66,7 +69,10 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
6669
llvm::SmallString<128> LibPath(SearchPathOpts.RuntimeResourcePath);
6770

6871
llvm::sys::path::append(LibPath, getPlatformNameForTriple(Triple));
69-
SearchPathOpts.RuntimeLibraryPath = LibPath.str();
72+
SearchPathOpts.RuntimeLibraryPaths.clear();
73+
SearchPathOpts.RuntimeLibraryPaths.push_back(LibPath.str());
74+
if (Triple.isOSDarwin())
75+
SearchPathOpts.RuntimeLibraryPaths.push_back(DARWIN_OS_LIBRARY_PATH);
7076

7177
// Set up the import paths containing the swiftmodules for the libraries in
7278
// RuntimeLibraryPath.

lib/Frontend/ParseableInterfaceModuleLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ class ParseableInterfaceModuleLoaderImpl {
10661066
}
10671067

10681068
bool isInResourceDir(StringRef path) {
1069-
StringRef resourceDir = ctx.SearchPathOpts.RuntimeLibraryPath;
1069+
StringRef resourceDir = ctx.SearchPathOpts.RuntimeResourcePath;
10701070
if (resourceDir.empty()) return false;
10711071
return path.startswith(resourceDir);
10721072
}

lib/IRGen/GenType.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,23 @@ TypeConverter::TypeConverter(IRGenModule &IGM)
12271227
if (!doesPlatformUseLegacyLayouts(platformName, archName))
12281228
return;
12291229

1230-
defaultPath.append(IGM.Context.SearchPathOpts.RuntimeLibraryPath);
1230+
// Find the first runtime library path that exists.
1231+
bool found = false;
1232+
for (auto &RuntimeLibraryPath
1233+
: IGM.Context.SearchPathOpts.RuntimeLibraryPaths) {
1234+
if (llvm::sys::fs::exists(RuntimeLibraryPath)) {
1235+
defaultPath.append(RuntimeLibraryPath);
1236+
found = true;
1237+
break;
1238+
}
1239+
}
1240+
if (!found) {
1241+
auto joined = llvm::join(IGM.Context.SearchPathOpts.RuntimeLibraryPaths,
1242+
"', '");
1243+
llvm::report_fatal_error("Unable to find a runtime library path at '"
1244+
+ joined + "'");
1245+
}
1246+
12311247
llvm::sys::path::append(defaultPath, "layouts-");
12321248
defaultPath.append(archName);
12331249
defaultPath.append(".yaml");

lib/Immediate/Immediate.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,26 @@ static void *loadRuntimeLib(StringRef runtimeLibPathWithName) {
5959
#endif
6060
}
6161

62-
static void *loadRuntimeLib(StringRef sharedLibName, StringRef runtimeLibPath) {
62+
static void *loadRuntimeLibAtPath(StringRef sharedLibName,
63+
StringRef runtimeLibPath) {
6364
// FIXME: Need error-checking.
6465
llvm::SmallString<128> Path = runtimeLibPath;
6566
llvm::sys::path::append(Path, sharedLibName);
6667
return loadRuntimeLib(Path);
6768
}
6869

69-
void *swift::immediate::loadSwiftRuntime(StringRef runtimeLibPath) {
70-
return loadRuntimeLib("libswiftCore" LTDL_SHLIB_EXT, runtimeLibPath);
70+
static void *loadRuntimeLib(StringRef sharedLibName,
71+
ArrayRef<std::string> runtimeLibPaths) {
72+
for (auto &runtimeLibPath : runtimeLibPaths) {
73+
if (void *handle = loadRuntimeLibAtPath(sharedLibName, runtimeLibPath))
74+
return handle;
75+
}
76+
return nullptr;
77+
}
78+
79+
void *swift::immediate::loadSwiftRuntime(ArrayRef<std::string>
80+
runtimeLibPaths) {
81+
return loadRuntimeLib("libswiftCore" LTDL_SHLIB_EXT, runtimeLibPaths);
7182
}
7283

7384
static bool tryLoadLibrary(LinkLibrary linkLib,
@@ -105,9 +116,9 @@ static bool tryLoadLibrary(LinkLibrary linkLib,
105116
if (!success)
106117
success = loadRuntimeLib(stem);
107118

108-
// If that fails, try our runtime library path.
119+
// If that fails, try our runtime library paths.
109120
if (!success)
110-
success = loadRuntimeLib(stem, searchPathOpts.RuntimeLibraryPath);
121+
success = loadRuntimeLib(stem, searchPathOpts.RuntimeLibraryPaths);
111122
break;
112123
}
113124
case LibraryKind::Framework: {
@@ -240,7 +251,7 @@ int swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
240251
//
241252
// This must be done here, before any library loading has been done, to avoid
242253
// racing with the static initializers in user code.
243-
auto stdlib = loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPath);
254+
auto stdlib = loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPaths);
244255
if (!stdlib) {
245256
CI.getDiags().diagnose(SourceLoc(),
246257
diag::error_immediate_mode_missing_stdlib);

lib/Immediate/ImmediateImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ namespace immediate {
3737
/// Returns a handle to the runtime suitable for other \c dlsym or \c dlclose
3838
/// calls or \c null if an error occurred.
3939
///
40-
/// \param runtimeLibPath Path to search for compiler-relative stdlib dylibs.
41-
void *loadSwiftRuntime(StringRef runtimeLibPath);
40+
/// \param runtimeLibPaths Paths to search for stdlib dylibs.
41+
void *loadSwiftRuntime(ArrayRef<std::string> runtimeLibPaths);
4242
bool tryLoadLibraries(ArrayRef<LinkLibrary> LinkLibraries,
4343
SearchPathOptions SearchPathOpts,
4444
DiagnosticEngine &Diags);

lib/Immediate/REPL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ class REPLEnvironment {
969969
ASTContext &Ctx = CI.getASTContext();
970970
Ctx.LangOpts.EnableAccessControl = false;
971971
if (!ParseStdlib) {
972-
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPath)) {
972+
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPaths)) {
973973
CI.getDiags().diagnose(SourceLoc(),
974974
diag::error_immediate_mode_missing_stdlib);
975975
return;

0 commit comments

Comments
 (0)