Skip to content

Switch to using clang as linker on darwin #1111

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: 40 additions & 214 deletions Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,153 +47,6 @@ 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 @@ -213,7 +66,7 @@ extension DarwinToolchain {
case .dynamicLibrary:
// Same as an executable, but with the -dylib flag
linkerTool = .dynamicLinker
commandLine.appendFlag("-dylib")
commandLine.appendFlag("-dynamiclib")
addLinkInputs(shouldUseInputFileList: shouldUseInputFileList,
commandLine: &commandLine,
inputs: inputs,
Expand Down Expand Up @@ -280,8 +133,7 @@ extension DarwinToolchain {
commandLine.appendPath(fileList)
if linkerOutputType != .staticLibrary {
for module in inputModules {
commandLine.append(.flag("-add_ast_path"))
commandLine.append(.path(module))
commandLine.append(.joinedOptionAndPath("-Wl,-add_ast_path,", module))
}
}

Expand All @@ -291,7 +143,7 @@ extension DarwinToolchain {
commandLine.append(contentsOf: inputs.flatMap {
(path: TypedVirtualPath) -> [Job.ArgTemplate] in
if path.type == .swiftModule && linkerOutputType != .staticLibrary {
return [.flag("-add_ast_path"), .path(path.file)]
return [.joinedOptionAndPath("-Wl,-add_ast_path,", path.file)]
} else if path.type == .object {
return [.path(path.file)]
} else if path.type == .tbd {
Expand All @@ -311,39 +163,16 @@ extension DarwinToolchain {
sanitizers: Set<Sanitizer>,
linkerOutputType: LinkOutputType,
lto: LTOKind?) throws {
// 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 let lto = lto {
switch lto {
case .llvmFull:
commandLine.appendFlag("-flto=full")
case .llvmThin:
commandLine.appendFlag("-flto=thin")
}

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

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

if parsedOptions.contains(.enableAppExtension) {
commandLine.appendFlag("-application_extension")
commandLine.appendFlag("-fapplication-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.
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 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)")
}

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

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

commandLine.appendFlags(
"-fobjc-link-runtime",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keith, what was the reason for always adding this flag?
It appears it does not match previous behavior and causes clang to always import Foundation, whether it is needed or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, I don't recall since it has been a while, but I agree I don't see where it clearly came from. I checked clang as well in case I was trying to mirror that and don't see it there either. I suppose I could have pulled it from a -v invocation of what this was doing before, but if that flag is present there I don't see where it's coming from.

Note that it does look like https://github.com/apple/llvm-project/blob/c10fcd3e640b3736a7b2442f00db8feca674648c/clang/lib/Driver/ToolChains/Darwin.cpp#L745-L746 relies on this being passed, so maybe it was required for that case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really should be gated on -enable-objc-interop I think. -lSystem is also rather odd. This should no longer be needed as we now rely on clang as the driver which will add -lSystem as appropriate.

"-lobjc",
"-lSystem"
)

commandLine.appendFlag("-arch")
commandLine.appendFlag(targetTriple.archName)
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)")
}

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

try addProfileGenerationArgs(
to: &commandLine,
parsedOptions: &parsedOptions,
targetInfo: targetInfo
)

let targetVariantTriple = targetInfo.targetVariant?.triple
addDeploymentTargetArgs(
to: &commandLine,
targetTriple: targetTriple,
targetVariantTriple: targetVariantTriple,
sdkPath: targetInfo.sdkPath?.path
)
if parsedOptions.hasArgument(.profileGenerate) {
commandLine.appendFlag("-fprofile-generate")
}

// These custom arguments should be right before the object file at the
// end.
Expand All @@ -427,7 +247,13 @@ extension DarwinToolchain {
from: &parsedOptions
)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
try commandLine.appendAllArguments(.Xlinker, from: &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)
}
}

Expand Down
13 changes: 0 additions & 13 deletions Sources/SwiftDriver/Jobs/Toolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ 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: "ld")
return try lookup(executable: "clang")
case .staticLinker:
return try lookup(executable: "libtool")
case .dsymutil:
Expand Down
12 changes: 5 additions & 7 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())),
"-syslibroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", "-lSystem", "-arch", .flag(hostTriple.archName),
"--sysroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", "-lSystem", .flag("--target=\(hostTriple.triple)"),
"-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",
"-rpath", "/usr/lib/swift", "-o",
.path(.relative(RelativePath("main"))),
],
inputs: [
Expand Down Expand Up @@ -249,12 +249,10 @@ final class JobExecutorTests: XCTestCase {
tool: try toolchain.resolvedTool(.dynamicLinker),
commandLine: [
.path(.temporary(RelativePath("main.o"))),
.path(.absolute(try toolchain.clangRT.get())),
"-syslibroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", "-lSystem", "-arch", .flag(hostTriple.archName),
"--sysroot", .path(.absolute(try toolchain.sdk.get())),
"-lobjc", "-lSystem", .flag("--target=\(hostTriple.triple)"),
"-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