Skip to content

Commit d580ce0

Browse files
authored
Merge pull request #24839 from mikeash/usrlibswift-fallback-5.1
[5.1][Interpreter] Fall back to loading Swift dylibs from /usr/lib/swift on Apple platforms.
2 parents 4031070 + f143585 commit d580ce0

File tree

13 files changed

+58
-71
lines changed

13 files changed

+58
-71
lines changed

include/swift/AST/SearchPathOptions.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,15 @@ 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;
7273

7374
/// Don't look in for compiler-provided modules.
7475
bool SkipRuntimeLibraryImportPaths = false;
75-
76-
/// Whether the runtime library path is set to the compiler-relative default.
77-
bool RuntimeLibraryPathIsDefault = true;
7876

7977
/// Return a hash code of any components from these options that should
8078
/// 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
@@ -183,7 +183,7 @@ class CompilerInvocation {
183183

184184
void setMainExecutablePath(StringRef Path);
185185

186-
void setRuntimeResourcePath(StringRef Path, bool IsDefault = false);
186+
void setRuntimeResourcePath(StringRef Path);
187187

188188
void setSDKPath(const std::string &Path);
189189

lib/Driver/DarwinToolChains.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ using namespace swift;
3939
using namespace swift::driver;
4040
using namespace llvm::opt;
4141

42-
/// The path for Swift libraries in the OS. (Duplicated from Immediate.cpp.
43-
/// Eventually we should consolidate this.)
44-
#define OS_LIBRARY_PATH "/usr/lib/swift"
45-
4642
std::string
4743
toolchains::Darwin::findProgramRelativeToSwiftImpl(StringRef name) const {
4844
StringRef swiftPath = getDriver().getSwiftProgramPath();
@@ -76,15 +72,12 @@ toolchains::Darwin::constructInvocation(const InterpretJobAction &job,
7672
const JobContext &context) const {
7773
InvocationInfo II = ToolChain::constructInvocation(job, context);
7874

79-
SmallString<128> envValue(OS_LIBRARY_PATH ":");
80-
8175
SmallString<128> runtimeLibraryPath;
8276
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, /*Shared=*/true);
83-
envValue.append(runtimeLibraryPath);
8477

8578
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_LIBRARY_PATH",
8679
":", options::OPT_L, context.Args,
87-
envValue);
80+
runtimeLibraryPath);
8881
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "DYLD_FRAMEWORK_PATH",
8982
":", options::OPT_F, context.Args);
9083
// FIXME: Add options::OPT_Fsystem paths to DYLD_FRAMEWORK_PATH as well.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 9 additions & 5 deletions
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
}
@@ -39,7 +42,7 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
3942
llvm::sys::path::remove_filename(LibPath); // Remove /swift
4043
llvm::sys::path::remove_filename(LibPath); // Remove /bin
4144
llvm::sys::path::append(LibPath, "lib", "swift");
42-
setRuntimeResourcePath(LibPath.str(), /*IsDefault=*/true);
45+
setRuntimeResourcePath(LibPath.str());
4346
}
4447

4548
/// If we haven't explicitly passed -prebuilt-module-cache-path, set it to
@@ -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.
@@ -87,11 +93,9 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
8793
}
8894
}
8995

90-
void CompilerInvocation::setRuntimeResourcePath(StringRef Path,
91-
bool IsDefault) {
96+
void CompilerInvocation::setRuntimeResourcePath(StringRef Path) {
9297
SearchPathOpts.RuntimeResourcePath = Path;
9398
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
94-
SearchPathOpts.RuntimeLibraryPathIsDefault = IsDefault;
9599
}
96100

97101
void CompilerInvocation::setTargetTriple(StringRef Triple) {

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
@@ -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: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,43 +51,34 @@
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-
5754
static void *loadRuntimeLib(StringRef runtimeLibPathWithName) {
5855
#if defined(_WIN32)
5956
return LoadLibraryA(runtimeLibPathWithName.str().c_str());
6057
#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
7058
return dlopen(runtimeLibPathWithName.str().c_str(), RTLD_LAZY | RTLD_GLOBAL);
7159
#endif
7260
}
7361

74-
static void *loadRuntimeLib(StringRef sharedLibName, StringRef runtimeLibPath) {
62+
static void *loadRuntimeLibAtPath(StringRef sharedLibName,
63+
StringRef runtimeLibPath) {
7564
// FIXME: Need error-checking.
7665
llvm::SmallString<128> Path = runtimeLibPath;
7766
llvm::sys::path::append(Path, sharedLibName);
7867
return loadRuntimeLib(Path);
7968
}
8069

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;
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;
8875
}
89-
#endif
90-
return loadRuntimeLib(LibName, runtimeLibPath);
76+
return nullptr;
77+
}
78+
79+
void *swift::immediate::loadSwiftRuntime(ArrayRef<std::string>
80+
runtimeLibPaths) {
81+
return loadRuntimeLib("libswiftCore" LTDL_SHLIB_EXT, runtimeLibPaths);
9182
}
9283

9384
static bool tryLoadLibrary(LinkLibrary linkLib,
@@ -125,9 +116,9 @@ static bool tryLoadLibrary(LinkLibrary linkLib,
125116
if (!success)
126117
success = loadRuntimeLib(stem);
127118

128-
// If that fails, try our runtime library path.
119+
// If that fails, try our runtime library paths.
129120
if (!success)
130-
success = loadRuntimeLib(stem, searchPathOpts.RuntimeLibraryPath);
121+
success = loadRuntimeLib(stem, searchPathOpts.RuntimeLibraryPaths);
131122
break;
132123
}
133124
case LibraryKind::Framework: {
@@ -260,9 +251,7 @@ int swift::RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
260251
//
261252
// This must be done here, before any library loading has been done, to avoid
262253
// racing with the static initializers in user code.
263-
auto stdlib = loadSwiftRuntime(
264-
Context.SearchPathOpts.RuntimeLibraryPath,
265-
Context.SearchPathOpts.RuntimeLibraryPathIsDefault);
254+
auto stdlib = loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPaths);
266255
if (!stdlib) {
267256
CI.getDiags().diagnose(SourceLoc(),
268257
diag::error_immediate_mode_missing_stdlib);

lib/Immediate/ImmediateImpl.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +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-
/// \param IsDefault If true, the path is the default compiler-relative path.
42-
void *loadSwiftRuntime(StringRef runtimeLibPath, bool IsDefault);
40+
/// \param runtimeLibPaths Paths to search for stdlib dylibs.
41+
void *loadSwiftRuntime(ArrayRef<std::string> runtimeLibPaths);
4342
bool tryLoadLibraries(ArrayRef<LinkLibrary> LinkLibraries,
4443
SearchPathOptions SearchPathOpts,
4544
DiagnosticEngine &Diags);

lib/Immediate/REPL.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,7 @@ class REPLEnvironment {
969969
ASTContext &Ctx = CI.getASTContext();
970970
Ctx.LangOpts.EnableAccessControl = false;
971971
if (!ParseStdlib) {
972-
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPath,
973-
Ctx.SearchPathOpts.RuntimeLibraryPathIsDefault)) {
972+
if (!loadSwiftRuntime(Ctx.SearchPathOpts.RuntimeLibraryPaths)) {
974973
CI.getDiags().diagnose(SourceLoc(),
975974
diag::error_immediate_mode_missing_stdlib);
976975
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:-NO_VALUE}
3+
echo ${!last_arg}

test/Driver/environment-mac.swift

Lines changed: 0 additions & 8 deletions
This file was deleted.

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={{(/usr/lib/swift:)?}}/RSRC/macosx{{$}}
18+
// CHECK-RESOURCE-DIR-ONLY: # DYLD_LIBRARY_PATH=/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/:(/usr/lib/swift:)?[^:]+/lib/swift/macosx$}}
25+
// CHECK-L: # DYLD_LIBRARY_PATH={{/foo/:[^:]+/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/:(/usr/lib/swift:)?[^:]+/lib/swift/macosx$}}
28+
// CHECK-L2: # DYLD_LIBRARY_PATH={{/foo/:/bar/:[^:]+/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/:(/usr/lib/swift:)?[^:]+/lib/swift/macosx:/abc/$}}
31+
// CHECK-L2-ENV: # DYLD_LIBRARY_PATH={{/foo/:/bar/:[^:]+/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/:(/usr/lib/swift:)?[^:]+/lib/swift/macosx($| )}}
59+
// CHECK-COMPLEX-DAG: DYLD_LIBRARY_PATH={{/foo2/:/bar2/:[^:]+/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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
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: 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.
7+
// RUN: %swift -repl -I %t -L %t < %t/repl.swift 2>&1 | %FileCheck %s
118

129
// REQUIRES: swift_repl
1310
// UNSUPPORTED: OS=linux-gnu

0 commit comments

Comments
 (0)