Skip to content

[Interpreter] Temporarily prefer /usr/lib/swift for Swift dylibs loaded by the interpreter #23983

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
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 SkipRuntimeLibraryImportPaths = 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);

Expand Down
9 changes: 8 additions & 1 deletion lib/Driver/DarwinToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ using namespace swift;
using namespace swift::driver;
using namespace llvm::opt;

/// The path for Swift libraries in the OS. (Duplicated from Immediate.cpp.
/// Eventually we should consolidate this.)
#define OS_LIBRARY_PATH "/usr/lib/swift"

std::string
toolchains::Darwin::findProgramRelativeToSwiftImpl(StringRef name) const {
StringRef swiftPath = getDriver().getSwiftProgramPath();
Expand Down Expand Up @@ -71,12 +75,15 @@ toolchains::Darwin::constructInvocation(const InterpretJobAction &job,
const JobContext &context) const {
InvocationInfo II = ToolChain::constructInvocation(job, context);

SmallString<128> envValue(OS_LIBRARY_PATH ":");

SmallString<128> runtimeLibraryPath;
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, /*Shared=*/true);
envValue.append(runtimeLibraryPath);

addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_LIBRARY_PATH",
":", options::OPT_L, context.Args,
runtimeLibraryPath);
envValue);
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_FRAMEWORK_PATH",
":", options::OPT_F, context.Args);
// FIXME: Add options::OPT_Fsystem paths to DYLD_FRAMEWORK_PATH as well.
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 updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
Expand Down Expand Up @@ -68,9 +68,11 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
}
}

void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
void CompilerInvocation::setRuntimeResourcePath(StringRef Path,
bool IsDefault) {
SearchPathOpts.RuntimeResourcePath = Path;
updateRuntimeLibraryPaths(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;
}
#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 @@ -969,7 +969,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
2 changes: 1 addition & 1 deletion test/Driver/Inputs/print-var.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
last_arg=${@: -1}
echo ${!last_arg}
echo ${!last_arg:-NO_VALUE}
8 changes: 8 additions & 0 deletions test/Driver/environment-mac.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// REQUIRES: OS=macosx

// RUN: %swift_driver -driver-use-frontend-path %S/Inputs/print-var.sh %s DYLD_LIBRARY_PATH | %FileCheck %s --dump-input fail

// Pass if the variable is not set at all. Something causes the
// variable to get lost in PR testing. Accept that for now. This is
// a temporary solution and so this will go away soon in any case.
// CHECK: {{(^/usr/lib/swift:)|(^NO_VALUE$)}}
10 changes: 5 additions & 5 deletions test/Driver/options-interpreter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@


// RUN: %swift_driver -### -target x86_64-apple-macosx10.9 -resource-dir /RSRC/ %s | %FileCheck -check-prefix=CHECK-RESOURCE-DIR-ONLY %s
// CHECK-RESOURCE-DIR-ONLY: # DYLD_LIBRARY_PATH=/RSRC/macosx{{$}}
// CHECK-RESOURCE-DIR-ONLY: # DYLD_LIBRARY_PATH={{(/usr/lib/swift:)?}}/RSRC/macosx{{$}}

// RUN: %swift_driver -### -target x86_64-unknown-linux-gnu -resource-dir /RSRC/ %s | %FileCheck -check-prefix=CHECK-RESOURCE-DIR-ONLY-LINUX${LD_LIBRARY_PATH+_LAX} %s
// CHECK-RESOURCE-DIR-ONLY-LINUX: # LD_LIBRARY_PATH=/RSRC/linux{{$}}
// CHECK-RESOURCE-DIR-ONLY-LINUX_LAX: # LD_LIBRARY_PATH=/RSRC/linux{{$|:}}

// RUN: %swift_driver -### -target x86_64-apple-macosx10.9 -L/foo/ %s | %FileCheck -check-prefix=CHECK-L %s
// CHECK-L: # DYLD_LIBRARY_PATH={{/foo/:[^:]+/lib/swift/macosx$}}
// CHECK-L: # DYLD_LIBRARY_PATH={{/foo/:(/usr/lib/swift:)?[^:]+/lib/swift/macosx$}}

// RUN: %swift_driver -### -target x86_64-apple-macosx10.9 -L/foo/ -L/bar/ %s | %FileCheck -check-prefix=CHECK-L2 %s
// CHECK-L2: # DYLD_LIBRARY_PATH={{/foo/:/bar/:[^:]+/lib/swift/macosx$}}
// CHECK-L2: # DYLD_LIBRARY_PATH={{/foo/:/bar/:(/usr/lib/swift:)?[^:]+/lib/swift/macosx$}}

// RUN: env DYLD_LIBRARY_PATH=/abc/ %swift_driver_plain -### -target x86_64-apple-macosx10.9 -L/foo/ -L/bar/ %s | %FileCheck -check-prefix=CHECK-L2-ENV %s
// CHECK-L2-ENV: # DYLD_LIBRARY_PATH={{/foo/:/bar/:[^:]+/lib/swift/macosx:/abc/$}}
// CHECK-L2-ENV: # DYLD_LIBRARY_PATH={{/foo/:/bar/:(/usr/lib/swift:)?[^:]+/lib/swift/macosx:/abc/$}}

// RUN: %swift_driver -### -target x86_64-apple-macosx10.9 %s | %FileCheck -check-prefix=CHECK-NO-FRAMEWORKS %s
// RUN: env DYLD_FRAMEWORK_PATH=/abc/ %swift_driver_plain -### -target x86_64-apple-macosx10.9 %s | %FileCheck -check-prefix=CHECK-NO-FRAMEWORKS %s
Expand Down Expand Up @@ -56,7 +56,7 @@
// CHECK-COMPLEX: -F /bar/
// CHECK-COMPLEX: #
// CHECK-COMPLEX-DAG: DYLD_FRAMEWORK_PATH=/foo/:/bar/:/abc/{{$| }}
// CHECK-COMPLEX-DAG: DYLD_LIBRARY_PATH={{/foo2/:/bar2/:[^:]+/lib/swift/macosx($| )}}
// CHECK-COMPLEX-DAG: DYLD_LIBRARY_PATH={{/foo2/:/bar2/:(/usr/lib/swift:)?[^:]+/lib/swift/macosx($| )}}

// RUN: %swift_driver -### -target x86_64-unknown-linux-gnu -L/foo/ %s | %FileCheck -check-prefix=CHECK-L-LINUX${LD_LIBRARY_PATH+_LAX} %s
// CHECK-L-LINUX: # LD_LIBRARY_PATH={{/foo/:[^:]+/lib/swift/linux$}}
Expand Down
5 changes: 4 additions & 1 deletion test/Interpreter/repl_autolinking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
// RUN: sed -n -e '/REPL_START$/,/REPL_END$/ p' %s > %t/repl.swift
// RUN: %target-swiftc_driver -emit-library %t/a.swift -I %t -L %t -emit-module-path %t/ModuleA.swiftmodule -autolink-force-load -module-link-name ModuleA -module-name ModuleA -o %t/libModuleA.dylib
// RUN: %target-swiftc_driver -emit-library %t/b.swift -I %t -L %t -emit-module-path %t/ModuleB.swiftmodule -autolink-force-load -module-link-name ModuleB -module-name ModuleB -o %t/libModuleB.dylib
// RUN: %swift -repl -I %t -L %t < %t/repl.swift 2>&1 | %FileCheck %s
// RUN: DYLD_LIBRARY_PATH=/usr/lib/swift %swift -repl -I %t -L %t < %t/repl.swift 2>&1 | %FileCheck %s

// FIXME: remove DYLD_LIBRARY_PATH from the last command when we fix the
// interpreter's runtime library lookup.

// REQUIRES: swift_repl
// UNSUPPORTED: OS=linux-gnu
Expand Down