Skip to content

Commit 82d2483

Browse files
committed
Add Swift stdlib to dyld library search path for swift-run and swift-test
<rdar://problem/46390701>
1 parent 035b0c0 commit 82d2483

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

Sources/Commands/SwiftRunTool.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import PackageModel
1616

1717
import func POSIX.chdir
1818
import func POSIX.getcwd
19+
import func POSIX.setenv
1920

2021
/// An enumeration of the errors that can be generated by the run tool.
2122
private enum RunError: Swift.Error {
@@ -194,6 +195,15 @@ public class SwiftRunTool: SwiftTool<RunToolOptions> {
194195
}
195196

196197
let pathRelativeToWorkingDirectory = excutablePath.relative(to: originalWorkingDirectory)
198+
199+
#if os(macOS)
200+
// Insert path to Swift stdlib in library search path.
201+
// It is fine to just taint our env like this because we don't expect to
202+
// return from the following exec call.
203+
let dyldLibPath = try getToolchain().makeDyldLibPath(withExistingValue: Process.env["DYLD_LIBRARY_PATH"])
204+
try setenv("DYLD_LIBRARY_PATH", value: dyldLibPath)
205+
#endif
206+
197207
try exec(path: excutablePath.asString, args: [pathRelativeToWorkingDirectory.asString] + arguments)
198208
}
199209

Sources/Commands/SwiftTestTool.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,11 @@ fileprivate func constructTestEnvironment(
955955
env["LLVM_PROFILE_FILE"] = codecovProfile.asString
956956
}
957957

958+
#if os(macOS)
959+
// Add path to Swift stdlib.
960+
env["DYLD_LIBRARY_PATH"] = toolchain.makeDyldLibPath(withExistingValue: env["DYLD_LIBRARY_PATH"])
961+
#endif
962+
958963
#if !os(macOS)
959964
return env
960965
#else

Sources/Workspace/UserToolchain.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ public final class UserToolchain: Toolchain {
6565
return swiftCompiler.parentDirectory.appending(component: "swift")
6666
}
6767

68+
/// Path containing the macOS Swift stdlib.
69+
var macosSwiftStdlib: AbsolutePath {
70+
return resolveSymlinks(swiftCompiler).appending(RelativePath("../../lib/swift/macosx"))
71+
}
72+
73+
public func makeDyldLibPath(withExistingValue value: String? = nil) -> String {
74+
var dyldLibPath = ""
75+
if let dyldLib = value, !dyldLib.isEmpty {
76+
dyldLibPath = dyldLib + ":"
77+
}
78+
dyldLibPath += macosSwiftStdlib.asString
79+
return dyldLibPath
80+
}
81+
6882
/// Path to the xctest utility.
6983
///
7084
/// This is only present on macOS.

Utilities/bootstrap

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -747,13 +747,13 @@ def get_swiftc_path():
747747
except:
748748
error("unable to find 'swiftc' tool for bootstrap build")
749749

750-
def delete_rpath(rpath, binary):
750+
def delete_rpath(rpath, binary, allowFailing=False):
751751
if platform.system() == 'Darwin':
752752
cmd = ["install_name_tool", "-delete_rpath", rpath, binary]
753753
note("removing RPATH from %s: %s" % (
754754
binary, ' '.join(cmd)))
755755
result = subprocess.call(cmd)
756-
if result != 0:
756+
if not allowFailing and result != 0:
757757
error("command failed with exit status %d" % (result,))
758758
else:
759759
error("unable to remove RPATHs on this platform")
@@ -789,10 +789,8 @@ def installBinary(binary_path, install_path, swiftc_path, add_rpaths=[], delete_
789789
if platform.system() == 'Darwin':
790790
installed_path = os.path.join(
791791
install_path, os.path.basename(binary_path))
792-
stdlib_path = os.path.normpath(
793-
os.path.join(os.path.dirname(os.path.realpath(swiftc_path)), "..",
794-
"lib", "swift", "macosx"))
795-
delete_rpath(stdlib_path, installed_path)
792+
stdlib_path = swift_macos_stdlib_path(swiftc_path)
793+
delete_rpath(stdlib_path, installed_path, True)
796794

797795
# Remove additional RPATHs, if requested.
798796
for rpath in delete_rpaths:
@@ -802,6 +800,8 @@ def installBinary(binary_path, install_path, swiftc_path, add_rpaths=[], delete_
802800
for rpath in add_rpaths:
803801
add_rpath(rpath, installed_path)
804802

803+
def swift_macos_stdlib_path(swiftc):
804+
return os.path.normpath(os.path.join(os.path.dirname(os.path.realpath(swiftc)), "..", "lib", "swift", "macosx"))
805805

806806
def llbuild_import_paths(args):
807807
if args.llbuild_link_framework:
@@ -1232,6 +1232,13 @@ def main():
12321232
]
12331233
if args.sysroot:
12341234
env_cmd.append("SYSROOT=" + args.sysroot)
1235+
1236+
# Look for stdlib libraries using dyld library path on macOS.
1237+
if platform.system() == 'Darwin':
1238+
stdlib_path = swift_macos_stdlib_path(args.swiftc_path)
1239+
build_flags.extend(["-Xlinker", "-rpath", "-Xlinker", stdlib_path])
1240+
env_cmd.append("DYLD_LIBRARY_PATH=" + stdlib_path)
1241+
12351242
cmd = env_cmd + [bootstrapped_product] + build_flags
12361243

12371244
# Always build tests in stage2.

0 commit comments

Comments
 (0)