Skip to content

Commit

Permalink
Add Swift stdlib to dyld library search path for swift-run and swift-…
Browse files Browse the repository at this point in the history
…test

<rdar://problem/46390701>
  • Loading branch information
aciidgh committed Jan 10, 2019
1 parent 035b0c0 commit 82d2483
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Sources/Commands/SwiftRunTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import PackageModel

import func POSIX.chdir
import func POSIX.getcwd
import func POSIX.setenv

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

let pathRelativeToWorkingDirectory = excutablePath.relative(to: originalWorkingDirectory)

#if os(macOS)
// Insert path to Swift stdlib in library search path.
// It is fine to just taint our env like this because we don't expect to
// return from the following exec call.
let dyldLibPath = try getToolchain().makeDyldLibPath(withExistingValue: Process.env["DYLD_LIBRARY_PATH"])
try setenv("DYLD_LIBRARY_PATH", value: dyldLibPath)
#endif

try exec(path: excutablePath.asString, args: [pathRelativeToWorkingDirectory.asString] + arguments)
}

Expand Down
5 changes: 5 additions & 0 deletions Sources/Commands/SwiftTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,11 @@ fileprivate func constructTestEnvironment(
env["LLVM_PROFILE_FILE"] = codecovProfile.asString
}

#if os(macOS)
// Add path to Swift stdlib.
env["DYLD_LIBRARY_PATH"] = toolchain.makeDyldLibPath(withExistingValue: env["DYLD_LIBRARY_PATH"])
#endif

#if !os(macOS)
return env
#else
Expand Down
14 changes: 14 additions & 0 deletions Sources/Workspace/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ public final class UserToolchain: Toolchain {
return swiftCompiler.parentDirectory.appending(component: "swift")
}

/// Path containing the macOS Swift stdlib.
var macosSwiftStdlib: AbsolutePath {
return resolveSymlinks(swiftCompiler).appending(RelativePath("../../lib/swift/macosx"))
}

public func makeDyldLibPath(withExistingValue value: String? = nil) -> String {
var dyldLibPath = ""
if let dyldLib = value, !dyldLib.isEmpty {
dyldLibPath = dyldLib + ":"
}
dyldLibPath += macosSwiftStdlib.asString
return dyldLibPath
}

/// Path to the xctest utility.
///
/// This is only present on macOS.
Expand Down
19 changes: 13 additions & 6 deletions Utilities/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -747,13 +747,13 @@ def get_swiftc_path():
except:
error("unable to find 'swiftc' tool for bootstrap build")

def delete_rpath(rpath, binary):
def delete_rpath(rpath, binary, allowFailing=False):
if platform.system() == 'Darwin':
cmd = ["install_name_tool", "-delete_rpath", rpath, binary]
note("removing RPATH from %s: %s" % (
binary, ' '.join(cmd)))
result = subprocess.call(cmd)
if result != 0:
if not allowFailing and result != 0:
error("command failed with exit status %d" % (result,))
else:
error("unable to remove RPATHs on this platform")
Expand Down Expand Up @@ -789,10 +789,8 @@ def installBinary(binary_path, install_path, swiftc_path, add_rpaths=[], delete_
if platform.system() == 'Darwin':
installed_path = os.path.join(
install_path, os.path.basename(binary_path))
stdlib_path = os.path.normpath(
os.path.join(os.path.dirname(os.path.realpath(swiftc_path)), "..",
"lib", "swift", "macosx"))
delete_rpath(stdlib_path, installed_path)
stdlib_path = swift_macos_stdlib_path(swiftc_path)
delete_rpath(stdlib_path, installed_path, True)

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

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

def llbuild_import_paths(args):
if args.llbuild_link_framework:
Expand Down Expand Up @@ -1232,6 +1232,13 @@ def main():
]
if args.sysroot:
env_cmd.append("SYSROOT=" + args.sysroot)

# Look for stdlib libraries using dyld library path on macOS.
if platform.system() == 'Darwin':
stdlib_path = swift_macos_stdlib_path(args.swiftc_path)
build_flags.extend(["-Xlinker", "-rpath", "-Xlinker", stdlib_path])
env_cmd.append("DYLD_LIBRARY_PATH=" + stdlib_path)

cmd = env_cmd + [bootstrapped_product] + build_flags

# Always build tests in stage2.
Expand Down

0 comments on commit 82d2483

Please sign in to comment.