-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathWindowsToolchain+LinkerSupport.swift
213 lines (179 loc) · 8.25 KB
/
WindowsToolchain+LinkerSupport.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//===--------------- WindowsToolchain+LinkerSupport.swift -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftOptions
import func TSCBasic.lookupExecutablePath
import struct TSCBasic.AbsolutePath
private func architecture(for triple: Triple) -> String {
// The concept of a "major" arch name only applies to Linux triples
guard triple.os == .linux else { return triple.archName }
// HACK: We don't wrap LLVM's ARM target architecture parsing, and we should
// definitely not try to port it. This check was only normalizing
// "armv7a/armv7r" and similar variants for armv6 to 'armv7' and
// 'armv6', so just take a brute-force approach
if triple.archName.contains("armv7") { return "armv7" }
if triple.archName.contains("armv6") { return "armv6" }
return triple.archName
}
extension WindowsToolchain {
public func addPlatformSpecificLinkerArgs(to commandLine: inout [Job.ArgTemplate],
parsedOptions: inout ParsedOptions,
linkerOutputType: LinkOutputType,
inputs: [TypedVirtualPath],
outputFile: VirtualPath,
shouldUseInputFileList: Bool,
lto: LTOKind?,
sanitizers: Set<Sanitizer>,
targetInfo: FrontendTargetInfo)
throws -> ResolvedTool {
// Special case static linking as clang cannot drive the operation.
if linkerOutputType == .staticLibrary {
let librarian: String
switch parsedOptions.getLastArgument(.useLd)?.asSingle {
case .none:
librarian = lto == nil ? "link" : "lld-link"
case .some("lld"), .some("lld.exe"), .some("lld-link"), .some("lld-link.exe"):
librarian = "lld-link"
case let .some(linker):
librarian = linker
}
commandLine.appendFlag("/LIB")
commandLine.appendFlag("/NOLOGO")
commandLine.appendFlag("/OUT:\(outputFile.name.spm_shellEscaped())")
let types: [FileType] = lto == nil ? [.object] : [.object, .llvmBitcode]
commandLine.append(contentsOf: inputs.lazy.filter { types.contains($0.type) }
.map { .path($0.file) })
return try resolvedTool(.staticLinker(lto), pathOverride: lookup(executable: librarian))
}
var cxxCompatEnabled = parsedOptions.hasArgument(.enableExperimentalCxxInterop)
if let cxxInteropMode = parsedOptions.getLastArgument(.cxxInteroperabilityMode) {
if cxxInteropMode.asSingle == "swift-5.9" {
cxxCompatEnabled = true
}
}
let clangTool: Tool = cxxCompatEnabled ? .clangxx : .clang
var clang = try getToolPath(clangTool)
let targetTriple = targetInfo.target.triple
if !targetTriple.triple.isEmpty {
commandLine.appendFlag("-target")
commandLine.appendFlag(targetTriple.triple)
}
switch linkerOutputType {
case .staticLibrary:
fatalError(".staticLibrary should not be reached")
case .dynamicLibrary:
commandLine.appendFlag("-shared")
case .executable:
break
}
if let arg = parsedOptions.getLastArgument(.toolsDirectory) {
let path = try AbsolutePath(validating: arg.asSingle)
if let tool = lookupExecutablePath(filename: executableName("clang"),
searchPaths: [path]) {
clang = tool
}
commandLine.appendFlag("-B")
commandLine.appendPath(path)
}
// Select the linker to use.
if let arg = parsedOptions.getLastArgument(.useLd) {
commandLine.appendFlag("-fuse-ld=\(arg.asSingle)")
} else if lto != nil {
commandLine.appendFlag("-fuse-ld=lld")
}
switch lto {
case .some(.llvmThin):
commandLine.appendFlag("-flto=thin")
case .some(.llvmFull):
commandLine.appendFlag("-flto=full")
case .none:
break
}
// FIXME(compnerd): render `-Xlinker /DEBUG` or `-Xlinker /DEBUG:DWARF` with
// DWARF + lld
// Rely on `-libc` to correctly identify the MSVC Runtime Library. We use
// `-nostartfiles` as that limits the difference to just the
// `-defaultlib:libcmt` which is passed unconditionally with the `clang`
// driver rather than the `clang-cl` driver.
commandLine.appendFlag("-nostartfiles")
// TODO(compnerd) investigate the proper way to port this logic over from
// the C++ driver.
// Since Windows has separate libraries per architecture, link against the
// architecture specific version of the static library.
commandLine.appendFlag(.L)
commandLine.appendPath(VirtualPath.lookup(targetInfo.runtimeLibraryImportPaths.last!.path))
if !parsedOptions.hasArgument(.nostartfiles) {
// Locate the Swift registration helper by honouring any explicit
// `-resource-dir`, `-sdk`, or the `SDKROOT` environment variable, and
// finally falling back to the target information.
let rsrc: VirtualPath
if let resourceDir = parsedOptions.getLastArgument(.resourceDir) {
rsrc = try VirtualPath(path: resourceDir.asSingle)
} else if let sdk = parsedOptions.getLastArgument(.sdk)?.asSingle ?? env["SDKROOT"], !sdk.isEmpty {
rsrc = try VirtualPath(path: AbsolutePath(validating: sdk)
.appending(components: "usr", "lib", "swift",
targetTriple.platformName() ?? "",
architecture(for: targetTriple))
.pathString)
} else {
rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
}
commandLine.appendPath(rsrc.appending(component: "swiftrt.obj"))
}
commandLine.append(contentsOf: inputs.compactMap { (input: TypedVirtualPath) -> Job.ArgTemplate? in
switch input.type {
case .object, .llvmBitcode:
return .path(input.file)
default:
return nil
}
})
for framework in parsedOptions.arguments(for: .F, .Fsystem) {
commandLine.appendFlag(framework.option == .Fsystem ? "-iframework" : "-F")
try commandLine.appendPath(VirtualPath(path: framework.argument.asSingle))
}
try commandLine.appendAllExcept(includeList: [.linkerOption],
excludeList: [.l],
from: &parsedOptions)
if let sdkPath = targetInfo.sdkPath?.path {
commandLine.appendFlag("-I")
commandLine.appendPath(VirtualPath.lookup(sdkPath))
}
if let stdlib = parsedOptions.getLastArgument(.experimentalCxxStdlib) {
commandLine.appendFlag("-stdlib=\(stdlib.asSingle)")
}
// Pass down an optimization level
if let optArg = mapOptimizationLevelToClangArg(from: &parsedOptions) {
commandLine.appendFlag(optArg)
}
// FIXME(compnerd) render asan/ubsan runtime link for executables
if parsedOptions.contains(.profileGenerate) {
commandLine.appendFlag("-Xlinker")
// FIXME(compnerd) wrap llvm::getInstrProfRuntimeHookVarName()
commandLine.appendFlag("-include:__llvm_profile_runtime")
commandLine.appendFlag("-lclang_rt.profile")
}
for option in parsedOptions.arguments(for: .Xlinker) {
commandLine.appendFlag(.Xlinker)
commandLine.appendFlag(option.argument.asSingle)
}
// TODO(compnerd) is there a separate equivalent to OPT_linker_option_group?
try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions)
if parsedOptions.contains(.v) {
commandLine.appendFlag("-v")
}
commandLine.appendFlag("-o")
commandLine.appendPath(outputFile)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
// TODO(compnerd) handle static libraries
return try resolvedTool(clangTool, pathOverride: clang)
}
}