Skip to content

Commit 7940973

Browse files
authored
Merge pull request #23983 from mikeash/temporarily-prefer-usrlibswift-for-interpreter-libraries-5.1
[Interpreter] Temporarily prefer /usr/lib/swift for Swift dylibs loaded by the interpreter
2 parents 9ce1795 + 92fa5ef commit 7940973

File tree

11 files changed

+63
-16
lines changed

11 files changed

+63
-16
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 SkipRuntimeLibraryImportPaths = 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

lib/Driver/DarwinToolChains.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ using namespace swift;
3838
using namespace swift::driver;
3939
using namespace llvm::opt;
4040

41+
/// The path for Swift libraries in the OS. (Duplicated from Immediate.cpp.
42+
/// Eventually we should consolidate this.)
43+
#define OS_LIBRARY_PATH "/usr/lib/swift"
44+
4145
std::string
4246
toolchains::Darwin::findProgramRelativeToSwiftImpl(StringRef name) const {
4347
StringRef swiftPath = getDriver().getSwiftProgramPath();
@@ -71,12 +75,15 @@ toolchains::Darwin::constructInvocation(const InterpretJobAction &job,
7175
const JobContext &context) const {
7276
InvocationInfo II = ToolChain::constructInvocation(job, context);
7377

78+
SmallString<128> envValue(OS_LIBRARY_PATH ":");
79+
7480
SmallString<128> runtimeLibraryPath;
7581
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, /*Shared=*/true);
82+
envValue.append(runtimeLibraryPath);
7683

7784
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_LIBRARY_PATH",
7885
":", options::OPT_L, context.Args,
79-
runtimeLibraryPath);
86+
envValue);
8087
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_FRAMEWORK_PATH",
8188
":", options::OPT_F, context.Args);
8289
// FIXME: Add options::OPT_Fsystem paths to DYLD_FRAMEWORK_PATH as well.

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 updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
@@ -68,9 +68,11 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
6868
}
6969
}
7070

71-
void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
71+
void CompilerInvocation::setRuntimeResourcePath(StringRef Path,
72+
bool IsDefault) {
7273
SearchPathOpts.RuntimeResourcePath = Path;
7374
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
75+
SearchPathOpts.RuntimeLibraryPathIsDefault = IsDefault;
7476
}
7577

7678
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
@@ -969,7 +969,8 @@ 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.RuntimeLibraryPath,
973+
Ctx.SearchPathOpts.RuntimeLibraryPathIsDefault)) {
973974
CI.getDiags().diagnose(SourceLoc(),
974975
diag::error_immediate_mode_missing_stdlib);
975976
return;

test/Driver/Inputs/print-var.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env bash
22
last_arg=${@: -1}
3-
echo ${!last_arg}
3+
echo ${!last_arg:-NO_VALUE}

test/Driver/environment-mac.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// REQUIRES: OS=macosx
2+
3+
// RUN: %swift_driver -driver-use-frontend-path %S/Inputs/print-var.sh %s DYLD_LIBRARY_PATH | %FileCheck %s --dump-input fail
4+
5+
// Pass if the variable is not set at all. Something causes the
6+
// variable to get lost in PR testing. Accept that for now. This is
7+
// a temporary solution and so this will go away soon in any case.
8+
// CHECK: {{(^/usr/lib/swift:)|(^NO_VALUE$)}}

test/Driver/options-interpreter.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515

1616

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

2020
// 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
2121
// CHECK-RESOURCE-DIR-ONLY-LINUX: # LD_LIBRARY_PATH=/RSRC/linux{{$}}
2222
// CHECK-RESOURCE-DIR-ONLY-LINUX_LAX: # LD_LIBRARY_PATH=/RSRC/linux{{$|:}}
2323

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

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

3030
// 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
31-
// CHECK-L2-ENV: # DYLD_LIBRARY_PATH={{/foo/:/bar/:[^:]+/lib/swift/macosx:/abc/$}}
31+
// CHECK-L2-ENV: # DYLD_LIBRARY_PATH={{/foo/:/bar/:(/usr/lib/swift:)?[^:]+/lib/swift/macosx:/abc/$}}
3232

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

6161
// RUN: %swift_driver -### -target x86_64-unknown-linux-gnu -L/foo/ %s | %FileCheck -check-prefix=CHECK-L-LINUX${LD_LIBRARY_PATH+_LAX} %s
6262
// CHECK-L-LINUX: # LD_LIBRARY_PATH={{/foo/:[^:]+/lib/swift/linux$}}

test/Interpreter/repl_autolinking.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
// RUN: sed -n -e '/REPL_START$/,/REPL_END$/ p' %s > %t/repl.swift
55
// 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
66
// 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
7-
// RUN: %swift -repl -I %t -L %t < %t/repl.swift 2>&1 | %FileCheck %s
7+
// RUN: DYLD_LIBRARY_PATH=/usr/lib/swift %swift -repl -I %t -L %t < %t/repl.swift 2>&1 | %FileCheck %s
8+
9+
// FIXME: remove DYLD_LIBRARY_PATH from the last command when we fix the
10+
// interpreter's runtime library lookup.
811

912
// REQUIRES: swift_repl
1013
// UNSUPPORTED: OS=linux-gnu

0 commit comments

Comments
 (0)