Skip to content

Commit bbdafde

Browse files
committed
[Interpreter] Fall back to loading Swift dylibs from /usr/lib/swift on Apple platforms.
Continue to load the dylibs next to the compiler if they exist. If they don't, then use the OS's dylibs. rdar://problem/47528005
1 parent 659278d commit bbdafde

File tree

7 files changed

+53
-14
lines changed

7 files changed

+53
-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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
using namespace swift;
3131
using namespace llvm::opt;
3232

33+
#if defined(__APPLE__) && defined(__MACH__)
34+
/// The path for Swift libraries in the OS.
35+
#define OS_LIBRARY_PATH "/usr/lib/swift"
36+
#endif
37+
3338
swift::CompilerInvocation::CompilerInvocation() {
3439
setTargetTriple(llvm::sys::getDefaultTargetTriple());
3540
}
@@ -66,7 +71,11 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
6671
llvm::SmallString<128> LibPath(SearchPathOpts.RuntimeResourcePath);
6772

6873
llvm::sys::path::append(LibPath, getPlatformNameForTriple(Triple));
69-
SearchPathOpts.RuntimeLibraryPath = LibPath.str();
74+
SearchPathOpts.RuntimeLibraryPaths.clear();
75+
SearchPathOpts.RuntimeLibraryPaths.push_back(LibPath.str());
76+
#if defined(__APPLE__) && defined(__MACH__)
77+
SearchPathOpts.RuntimeLibraryPaths.push_back(OS_LIBRARY_PATH);
78+
#endif
7079

7180
// Set up the import paths containing the swiftmodules for the libraries in
7281
// RuntimeLibraryPath.

lib/Frontend/ParseableInterfaceModuleLoader.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,12 @@ class ParseableInterfaceModuleLoaderImpl {
10661066
}
10671067

10681068
bool isInResourceDir(StringRef path) {
1069-
StringRef resourceDir = ctx.SearchPathOpts.RuntimeLibraryPath;
1070-
if (resourceDir.empty()) return false;
1071-
return path.startswith(resourceDir);
1069+
for (auto &RuntimeLibraryPath : ctx.SearchPathOpts.RuntimeLibraryPaths) {
1070+
if (path.startswith(RuntimeLibraryPath)) {
1071+
return true;
1072+
}
1073+
}
1074+
return false;
10721075
}
10731076

10741077
/// Finds the most appropriate .swiftmodule, whose dependencies are up to

lib/IRGen/GenType.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,23 @@ TypeConverter::TypeConverter(IRGenModule &IGM)
11841184
if (!doesPlatformUseLegacyLayouts(platformName, archName))
11851185
return;
11861186

1187-
defaultPath.append(IGM.Context.SearchPathOpts.RuntimeLibraryPath);
1187+
// Find the first runtime library path that exists.
1188+
bool found = false;
1189+
for (auto &RuntimeLibraryPath
1190+
: IGM.Context.SearchPathOpts.RuntimeLibraryPaths) {
1191+
if (llvm::sys::fs::exists(RuntimeLibraryPath)) {
1192+
defaultPath.append(RuntimeLibraryPath);
1193+
found = true;
1194+
break;
1195+
}
1196+
}
1197+
if (!found) {
1198+
auto joined = llvm::join(IGM.Context.SearchPathOpts.RuntimeLibraryPaths,
1199+
"', '");
1200+
llvm::report_fatal_error("Unable to find a runtime library path at '"
1201+
+ joined + "'");
1202+
}
1203+
11881204
llvm::sys::path::append(defaultPath, "layouts-");
11891205
defaultPath.append(archName);
11901206
defaultPath.append(".yaml");

lib/Immediate/Immediate.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,18 @@ static void *loadRuntimeLib(StringRef sharedLibName, StringRef runtimeLibPath) {
6666
return loadRuntimeLib(Path);
6767
}
6868

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

7383
static bool tryLoadLibrary(LinkLibrary linkLib,
@@ -105,9 +115,9 @@ static bool tryLoadLibrary(LinkLibrary linkLib,
105115
if (!success)
106116
success = loadRuntimeLib(stem);
107117

108-
// If that fails, try our runtime library path.
118+
// If that fails, try our runtime library paths.
109119
if (!success)
110-
success = loadRuntimeLib(stem, searchPathOpts.RuntimeLibraryPath);
120+
success = loadRuntimeLib(stem, searchPathOpts.RuntimeLibraryPaths);
111121
break;
112122
}
113123
case LibraryKind::Framework: {
@@ -240,7 +250,7 @@ int swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
240250
//
241251
// This must be done here, before any library loading has been done, to avoid
242252
// racing with the static initializers in user code.
243-
auto stdlib = loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPath);
253+
auto stdlib = loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPaths);
244254
if (!stdlib) {
245255
CI.getDiags().diagnose(SourceLoc(),
246256
diag::error_immediate_mode_missing_stdlib);

lib/Immediate/ImmediateImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ 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+
void *loadSwiftRuntime(const std::vector<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)