Skip to content

Revert "Switch to using clang as linker on darwin" #1142

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
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
254 changes: 214 additions & 40 deletions Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,153 @@ extension DarwinToolchain {
return try findXcodeClangLibPath("arc")
}

internal func addLTOLibArgs(to commandLine: inout [Job.ArgTemplate]) throws {
if let path = try findXcodeClangLibPath("libLTO.dylib") {
commandLine.appendFlag("-lto_library")
commandLine.appendPath(path)
}
}

func addLinkRuntimeLibraryRPath(
to commandLine: inout [Job.ArgTemplate],
parsedOptions: inout ParsedOptions,
targetInfo: FrontendTargetInfo,
darwinLibName: String
) throws {
// Adding the rpaths might negatively interact when other rpaths are involved,
// so we should make sure we add the rpaths last, after all user-specified
// rpaths. This is currently true from this place, but we need to be
// careful if this function is ever called before user's rpaths are emitted.
assert(darwinLibName.hasSuffix(".dylib"), "must be a dynamic library")

// Add @executable_path to rpath to support having the dylib copied with
// the executable.
commandLine.appendFlag("-rpath")
commandLine.appendFlag("@executable_path")

// Add the path to the resource dir to rpath to support using the dylib
// from the default location without copying.


let clangPath = try clangLibraryPath(
for: targetInfo,
parsedOptions: &parsedOptions)
commandLine.appendFlag("-rpath")
commandLine.appendPath(clangPath)
}

func addLinkSanitizerLibArgsForDarwin(
to commandLine: inout [Job.ArgTemplate],
parsedOptions: inout ParsedOptions,
targetInfo: FrontendTargetInfo,
sanitizer: Sanitizer,
isShared: Bool
) throws {
// Sanitizer runtime libraries requires C++.
commandLine.appendFlag("-lc++")
// Add explicit dependency on -lc++abi, as -lc++ doesn't re-export
// all RTTI-related symbols that are used.
commandLine.appendFlag("-lc++abi")

let sanitizerName = try runtimeLibraryName(
for: sanitizer,
targetTriple: targetInfo.target.triple,
isShared: isShared
)
try addLinkRuntimeLibrary(
named: sanitizerName,
to: &commandLine,
for: targetInfo,
parsedOptions: &parsedOptions
)

if isShared {
try addLinkRuntimeLibraryRPath(
to: &commandLine,
parsedOptions: &parsedOptions,
targetInfo: targetInfo,
darwinLibName: sanitizerName
)
}
}

private func addProfileGenerationArgs(
to commandLine: inout [Job.ArgTemplate],
parsedOptions: inout ParsedOptions,
targetInfo: FrontendTargetInfo
) throws {
guard parsedOptions.hasArgument(.profileGenerate) else { return }
let clangPath = try clangLibraryPath(for: targetInfo,
parsedOptions: &parsedOptions)

for runtime in targetInfo.target.triple.darwinPlatform!.profileLibraryNameSuffixes {
let clangRTPath = clangPath
.appending(component: "libclang_rt.profile_\(runtime).a")
commandLine.appendPath(clangRTPath)
}
}

private func addPlatformVersionArg(to commandLine: inout [Job.ArgTemplate],
for triple: Triple, sdkPath: VirtualPath.Handle?) {
assert(triple.isDarwin)
let platformName = triple.darwinPlatform!.linkerPlatformName
let platformVersion = triple.darwinLinkerPlatformVersion
let sdkVersion: Version
if let sdkPath = sdkPath,
let sdkInfo = getTargetSDKInfo(sdkPath: sdkPath) {
sdkVersion = sdkInfo.sdkVersion(for: triple)
} else {
sdkVersion = Version(0, 0, 0)
}

commandLine.append(.flag("-platform_version"))
commandLine.append(.flag(platformName))
commandLine.append(.flag(platformVersion.description))
commandLine.append(.flag(sdkVersion.description))
}

private func addDeploymentTargetArgs(
to commandLine: inout [Job.ArgTemplate],
targetTriple: Triple,
targetVariantTriple: Triple?,
sdkPath: VirtualPath.Handle?
) {
addPlatformVersionArg(to: &commandLine, for: targetTriple, sdkPath: sdkPath)
if let variantTriple = targetVariantTriple {
assert(targetTriple.isValidForZipperingWithTriple(variantTriple))
addPlatformVersionArg(to: &commandLine, for: variantTriple,
sdkPath: sdkPath)
}
}

private func addArgsToLinkARCLite(
to commandLine: inout [Job.ArgTemplate],
parsedOptions: inout ParsedOptions,
targetTriple: Triple
) throws {
guard parsedOptions.hasFlag(
positive: .linkObjcRuntime,
negative: .noLinkObjcRuntime,
default: !targetTriple.supports(.nativeARC)
) else {
return
}

guard let arcLiteLibPath = try findARCLiteLibPath(),
let platformName = targetTriple.platformName() else {
return
}
let fullLibPath = arcLiteLibPath
.appending(components: "libarclite_\(platformName).a")

commandLine.appendFlag("-force_load")
commandLine.appendPath(fullLibPath)

// Arclite depends on CoreFoundation.
commandLine.appendFlag(.framework)
commandLine.appendFlag("CoreFoundation")
}

/// Adds the arguments necessary to link the files from the given set of
/// options for a Darwin platform.
public func addPlatformSpecificLinkerArgs(
Expand All @@ -66,7 +213,7 @@ extension DarwinToolchain {
case .dynamicLibrary:
// Same as an executable, but with the -dylib flag
linkerTool = .dynamicLinker
commandLine.appendFlag("-dynamiclib")
commandLine.appendFlag("-dylib")
addLinkInputs(shouldUseInputFileList: shouldUseInputFileList,
commandLine: &commandLine,
inputs: inputs,
Expand Down Expand Up @@ -133,7 +280,8 @@ extension DarwinToolchain {
commandLine.appendPath(fileList)
if linkerOutputType != .staticLibrary {
for module in inputModules {
commandLine.append(.joinedOptionAndPath("-Wl,-add_ast_path,", module))
commandLine.append(.flag("-add_ast_path"))
commandLine.append(.path(module))
}
}

Expand All @@ -143,7 +291,7 @@ extension DarwinToolchain {
commandLine.append(contentsOf: inputs.flatMap {
(path: TypedVirtualPath) -> [Job.ArgTemplate] in
if path.type == .swiftModule && linkerOutputType != .staticLibrary {
return [.joinedOptionAndPath("-Wl,-add_ast_path,", path.file)]
return [.flag("-add_ast_path"), .path(path.file)]
} else if path.type == .object {
return [.path(path.file)]
} else if path.type == .tbd {
Expand All @@ -163,16 +311,39 @@ extension DarwinToolchain {
sanitizers: Set<Sanitizer>,
linkerOutputType: LinkOutputType,
lto: LTOKind?) throws {
if let lto = lto {
switch lto {
case .llvmFull:
commandLine.appendFlag("-flto=full")
case .llvmThin:
commandLine.appendFlag("-flto=thin")
}
// FIXME: If we used Clang as a linker instead of going straight to ld,
// we wouldn't have to replicate a bunch of Clang's logic here.

// Always link the regular compiler_rt if it's present. Note that the
// regular libclang_rt.a uses a fat binary for device and simulator; this is
// not true for all compiler_rt build products.
//
// Note: Normally we'd just add this unconditionally, but it's valid to build
// Swift and use it as a linker without building compiler_rt.
let targetTriple = targetInfo.target.triple
let darwinPlatformSuffix =
targetTriple.darwinPlatform!.with(.device)!.libraryNameSuffix
let compilerRTPath =
try clangLibraryPath(
for: targetInfo,
parsedOptions: &parsedOptions)
.appending(component: "libclang_rt.\(darwinPlatformSuffix).a")
if try fileSystem.exists(compilerRTPath) {
commandLine.append(.path(compilerRTPath))
}

try addArgsToLinkARCLite(
to: &commandLine,
parsedOptions: &parsedOptions,
targetTriple: targetTriple
)

if lto != nil {
if let arg = parsedOptions.getLastArgument(.ltoLibrary)?.asSingle {
commandLine.append(.joinedOptionAndPath("-Wl,-lto_library,", try VirtualPath(path: arg)))
commandLine.appendFlag("-lto_library")
commandLine.appendPath(try VirtualPath(path: arg))
} else {
try addLTOLibArgs(to: &commandLine)
}
}

Expand All @@ -183,44 +354,43 @@ extension DarwinToolchain {
}

if parsedOptions.contains(.enableAppExtension) {
commandLine.appendFlag("-fapplication-extension")
commandLine.appendFlag("-application_extension")
}

// Linking sanitizers will add rpaths, which might negatively interact when
// other rpaths are involved, so we should make sure we add the rpaths after
// all user-specified rpaths.
if linkerOutputType == .executable && !sanitizers.isEmpty {
let sanitizerNames = sanitizers
.map { $0.rawValue }
.sorted() // Sort so we get a stable, testable order
.joined(separator: ",")
commandLine.appendFlag("-fsanitize=\(sanitizerNames)")
for sanitizer in sanitizers {
if sanitizer == .fuzzer {
guard linkerOutputType == .executable else { continue }
}
try addLinkSanitizerLibArgsForDarwin(
to: &commandLine,
parsedOptions: &parsedOptions,
targetInfo: targetInfo,
sanitizer: sanitizer,
isShared: sanitizer != .fuzzer
)
}

if parsedOptions.contains(.embedBitcode) {
commandLine.appendFlag("-fembed-bitcode")
} else if parsedOptions.contains(.embedBitcodeMarker) {
commandLine.appendFlag("-fembed-bitcode=marker")
if parsedOptions.contains(.embedBitcode) ||
parsedOptions.contains(.embedBitcodeMarker) {
commandLine.appendFlag("-bitcode_bundle")
}

// Add the SDK path
if let sdkPath = targetInfo.sdkPath?.path {
commandLine.appendFlag("--sysroot")
commandLine.appendFlag("-syslibroot")
commandLine.appendPath(VirtualPath.lookup(sdkPath))
}

commandLine.appendFlags(
"-fobjc-link-runtime",
"-lobjc",
"-lSystem"
)

let targetTriple = targetInfo.target.triple
commandLine.appendFlag("--target=\(targetTriple.triple)")
if let variantTriple = targetInfo.targetVariant?.triple {
assert(targetTriple.isValidForZipperingWithTriple(variantTriple))
commandLine.appendFlag("-darwin-target-variant=\(variantTriple.triple)")
}
commandLine.appendFlag("-arch")
commandLine.appendFlag(targetTriple.archName)

// On Darwin, we only support libc++.
if parsedOptions.contains(.enableExperimentalCxxInterop) {
Expand All @@ -235,9 +405,19 @@ extension DarwinToolchain {
fileSystem: fileSystem
)

if parsedOptions.hasArgument(.profileGenerate) {
commandLine.appendFlag("-fprofile-generate")
}
try addProfileGenerationArgs(
to: &commandLine,
parsedOptions: &parsedOptions,
targetInfo: targetInfo
)

let targetVariantTriple = targetInfo.targetVariant?.triple
addDeploymentTargetArgs(
to: &commandLine,
targetTriple: targetTriple,
targetVariantTriple: targetVariantTriple,
sdkPath: targetInfo.sdkPath?.path
)

// These custom arguments should be right before the object file at the
// end.
Expand All @@ -247,13 +427,7 @@ extension DarwinToolchain {
from: &parsedOptions
)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
// Because we invoke `clang` as the linker executable, we must still
// use `-Xlinker` for linker-specific arguments.
for linkerOpt in parsedOptions.arguments(for: .Xlinker) {
commandLine.appendFlag(.Xlinker)
commandLine.appendFlag(linkerOpt.argument.asSingle)
}
try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions)
try commandLine.appendAllArguments(.Xlinker, from: &parsedOptions)
}
}

Expand Down
13 changes: 13 additions & 0 deletions Sources/SwiftDriver/Jobs/Toolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ extension Toolchain {
return result
}

func addLinkRuntimeLibrary(
named name: String,
to commandLine: inout [Job.ArgTemplate],
for targetInfo: FrontendTargetInfo,
parsedOptions: inout ParsedOptions
) throws {
let path = try clangLibraryPath(
for: targetInfo,
parsedOptions: &parsedOptions)
.appending(component: name)
commandLine.appendPath(path)
}

func runtimeLibraryExists(
for sanitizer: Sanitizer,
targetInfo: FrontendTargetInfo,
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Toolchains/DarwinToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public final class DarwinToolchain: Toolchain {
case .swiftCompiler:
return try lookup(executable: "swift-frontend")
case .dynamicLinker:
return try lookup(executable: "clang")
return try lookup(executable: "ld")
case .staticLinker:
return try lookup(executable: "libtool")
case .dsymutil:
Expand Down
12 changes: 7 additions & 5 deletions Tests/SwiftDriverTests/JobExecutorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ final class JobExecutorTests: XCTestCase {
.path(.temporary(RelativePath("foo.o"))),
.path(.temporary(RelativePath("main.o"))),
.path(.absolute(try toolchain.clangRT.get())),
"--sysroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", "-lSystem", .flag("--target=\(hostTriple.triple)"),
"-syslibroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", "-lSystem", "-arch", .flag(hostTriple.archName),
"-L", .path(.absolute(try toolchain.resourcesDirectory.get())),
"-L", .path(.absolute(try toolchain.sdkStdlib(sdk: toolchain.sdk.get()))),
"-rpath", "/usr/lib/swift", "-o",
"-rpath", "/usr/lib/swift", "-macosx_version_min", "10.14.0", "-o",
.path(.relative(RelativePath("main"))),
],
inputs: [
Expand Down Expand Up @@ -249,10 +249,12 @@ final class JobExecutorTests: XCTestCase {
tool: try toolchain.resolvedTool(.dynamicLinker),
commandLine: [
.path(.temporary(RelativePath("main.o"))),
"--sysroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", "-lSystem", .flag("--target=\(hostTriple.triple)"),
.path(.absolute(try toolchain.clangRT.get())),
"-syslibroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", "-lSystem", "-arch", .flag(hostTriple.archName),
"-L", .path(.absolute(try toolchain.resourcesDirectory.get())),
"-L", .path(.absolute(try toolchain.sdkStdlib(sdk: toolchain.sdk.get()))),
"-rpath", "/usr/lib/swift", "-macosx_version_min", "10.14.0",
"-o", .path(.absolute(exec)),
],
inputs: [
Expand Down
Loading