Skip to content

[Interpreter] Fall back to loading Swift dylibs from /usr/lib/swift on Apple platforms. #24838

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 2 commits into from
Jul 2, 2019
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
5 changes: 3 additions & 2 deletions include/swift/AST/SearchPathOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ class SearchPathOptions {
/// Path to search for compiler-relative header files.
std::string RuntimeResourcePath;

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

/// Paths to search for stdlib modules. One of these will be compiler-relative.
std::vector<std::string> RuntimeLibraryImportPaths;
Expand Down
8 changes: 7 additions & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
using namespace swift;
using namespace llvm::opt;

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

swift::CompilerInvocation::CompilerInvocation() {
setTargetTriple(llvm::sys::getDefaultTargetTriple());
}
Expand Down Expand Up @@ -66,7 +69,10 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
llvm::SmallString<128> LibPath(SearchPathOpts.RuntimeResourcePath);

llvm::sys::path::append(LibPath, getPlatformNameForTriple(Triple));
SearchPathOpts.RuntimeLibraryPath = LibPath.str();
SearchPathOpts.RuntimeLibraryPaths.clear();
SearchPathOpts.RuntimeLibraryPaths.push_back(LibPath.str());
if (Triple.isOSDarwin())
SearchPathOpts.RuntimeLibraryPaths.push_back(DARWIN_OS_LIBRARY_PATH);

// Set up the import paths containing the swiftmodules for the libraries in
// RuntimeLibraryPath.
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/ParseableInterfaceModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ class ParseableInterfaceModuleLoaderImpl {
}

bool isInResourceDir(StringRef path) {
StringRef resourceDir = ctx.SearchPathOpts.RuntimeLibraryPath;
StringRef resourceDir = ctx.SearchPathOpts.RuntimeResourcePath;
if (resourceDir.empty()) return false;
return path.startswith(resourceDir);
}
Expand Down
18 changes: 17 additions & 1 deletion lib/IRGen/GenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,23 @@ TypeConverter::TypeConverter(IRGenModule &IGM)
if (!doesPlatformUseLegacyLayouts(platformName, archName))
return;

defaultPath.append(IGM.Context.SearchPathOpts.RuntimeLibraryPath);
// Find the first runtime library path that exists.
bool found = false;
for (auto &RuntimeLibraryPath
: IGM.Context.SearchPathOpts.RuntimeLibraryPaths) {
if (llvm::sys::fs::exists(RuntimeLibraryPath)) {
defaultPath.append(RuntimeLibraryPath);
found = true;
break;
}
}
if (!found) {
auto joined = llvm::join(IGM.Context.SearchPathOpts.RuntimeLibraryPaths,
"', '");
llvm::report_fatal_error("Unable to find a runtime library path at '"
+ joined + "'");
}

llvm::sys::path::append(defaultPath, "layouts-");
defaultPath.append(archName);
defaultPath.append(".yaml");
Expand Down
23 changes: 17 additions & 6 deletions lib/Immediate/Immediate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,26 @@ static void *loadRuntimeLib(StringRef runtimeLibPathWithName) {
#endif
}

static void *loadRuntimeLib(StringRef sharedLibName, StringRef runtimeLibPath) {
static void *loadRuntimeLibAtPath(StringRef sharedLibName,
StringRef runtimeLibPath) {
// FIXME: Need error-checking.
llvm::SmallString<128> Path = runtimeLibPath;
llvm::sys::path::append(Path, sharedLibName);
return loadRuntimeLib(Path);
}

void *swift::immediate::loadSwiftRuntime(StringRef runtimeLibPath) {
return loadRuntimeLib("libswiftCore" LTDL_SHLIB_EXT, runtimeLibPath);
static void *loadRuntimeLib(StringRef sharedLibName,
ArrayRef<std::string> runtimeLibPaths) {
for (auto &runtimeLibPath : runtimeLibPaths) {
if (void *handle = loadRuntimeLibAtPath(sharedLibName, runtimeLibPath))
return handle;
}
return nullptr;
}

void *swift::immediate::loadSwiftRuntime(ArrayRef<std::string>
runtimeLibPaths) {
return loadRuntimeLib("libswiftCore" LTDL_SHLIB_EXT, runtimeLibPaths);
}

static bool tryLoadLibrary(LinkLibrary linkLib,
Expand Down Expand Up @@ -105,9 +116,9 @@ static bool tryLoadLibrary(LinkLibrary linkLib,
if (!success)
success = loadRuntimeLib(stem);

// If that fails, try our runtime library path.
// If that fails, try our runtime library paths.
if (!success)
success = loadRuntimeLib(stem, searchPathOpts.RuntimeLibraryPath);
success = loadRuntimeLib(stem, searchPathOpts.RuntimeLibraryPaths);
break;
}
case LibraryKind::Framework: {
Expand Down Expand Up @@ -240,7 +251,7 @@ 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.RuntimeLibraryPaths);
if (!stdlib) {
CI.getDiags().diagnose(SourceLoc(),
diag::error_immediate_mode_missing_stdlib);
Expand Down
4 changes: 2 additions & 2 deletions lib/Immediate/ImmediateImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ namespace immediate {
/// Returns a handle to the runtime suitable for other \c dlsym or \c dlclose
/// calls or \c null if an error occurred.
///
/// \param runtimeLibPath Path to search for compiler-relative stdlib dylibs.
void *loadSwiftRuntime(StringRef runtimeLibPath);
/// \param runtimeLibPaths Paths to search for stdlib dylibs.
void *loadSwiftRuntime(ArrayRef<std::string> runtimeLibPaths);
bool tryLoadLibraries(ArrayRef<LinkLibrary> LinkLibraries,
SearchPathOptions SearchPathOpts,
DiagnosticEngine &Diags);
Expand Down
2 changes: 1 addition & 1 deletion lib/Immediate/REPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ class REPLEnvironment {
ASTContext &Ctx = CI.getASTContext();
Ctx.LangOpts.EnableAccessControl = false;
if (!ParseStdlib) {
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPath)) {
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPaths)) {
CI.getDiags().diagnose(SourceLoc(),
diag::error_immediate_mode_missing_stdlib);
return;
Expand Down