-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathDarwinToolchain+LinkerSupport.swift
More file actions
298 lines (265 loc) · 10.5 KB
/
Copy pathDarwinToolchain+LinkerSupport.swift
File metadata and controls
298 lines (265 loc) · 10.5 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//===--------------- DarwinToolchain+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 struct TSCBasic.AbsolutePath
import struct TSCBasic.RelativePath
extension DarwinToolchain {
internal func findXcodeClangPath() throws -> AbsolutePath? {
let result = try executor.checkNonZeroExit(
args: "xcrun", "-toolchain", "default", "-f", "clang",
environment: env.legacyVars
).trimmingCharacters(in: .whitespacesAndNewlines)
return result.isEmpty ? nil : try AbsolutePath(validating: result)
}
internal func findXcodeClangLibPath(_ additionalPath: String) throws -> AbsolutePath? {
let path = try getToolPath(.swiftCompiler)
.parentDirectory // 'swift'
.parentDirectory // 'bin'
.appending(components: "lib", additionalPath)
if fileSystem.exists(path) { return path }
// If we don't have a 'lib/arc/' directory, find the "arclite" library
// relative to the Clang in the active Xcode.
if let clangPath = try? findXcodeClangPath() {
return clangPath
.parentDirectory // 'clang'
.parentDirectory // 'bin'
.appending(components: "lib", additionalPath)
}
return nil
}
internal func findARCLiteLibPath() throws -> AbsolutePath? {
return try findXcodeClangLibPath("arc")
}
/// Adds the arguments necessary to link the files from the given set of
/// options for a Darwin platform.
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 {
// Set up for linking.
let linkerTool: Tool
switch linkerOutputType {
case .dynamicLibrary:
// Same as an executable, but with the -dylib flag
linkerTool = .dynamicLinker
commandLine.appendFlag("-dynamiclib")
try addLinkInputs(shouldUseInputFileList: shouldUseInputFileList,
commandLine: &commandLine,
inputs: inputs,
linkerOutputType: linkerOutputType)
try addDynamicLinkerFlags(targetInfo: targetInfo,
parsedOptions: &parsedOptions,
commandLine: &commandLine,
sanitizers: sanitizers,
linkerOutputType: linkerOutputType,
lto: lto)
case .executable:
linkerTool = .dynamicLinker
try addLinkInputs(shouldUseInputFileList: shouldUseInputFileList,
commandLine: &commandLine,
inputs: inputs,
linkerOutputType: linkerOutputType)
try addDynamicLinkerFlags(targetInfo: targetInfo,
parsedOptions: &parsedOptions,
commandLine: &commandLine,
sanitizers: sanitizers,
linkerOutputType: linkerOutputType,
lto: lto)
case .staticLibrary:
linkerTool = .staticLinker(lto)
commandLine.appendFlag(.static)
try addLinkInputs(shouldUseInputFileList: shouldUseInputFileList,
commandLine: &commandLine,
inputs: inputs,
linkerOutputType: linkerOutputType)
}
// Add the output
commandLine.appendFlag("-o")
commandLine.appendPath(outputFile)
return try resolvedTool(linkerTool)
}
private func addLinkInputs(shouldUseInputFileList: Bool,
commandLine: inout [Job.ArgTemplate],
inputs: [TypedVirtualPath],
linkerOutputType: LinkOutputType) throws {
// inputs LinkFileList
if shouldUseInputFileList {
commandLine.appendFlag(.filelist)
var inputPaths = [VirtualPath]()
var inputModules = [VirtualPath]()
for input in inputs {
if input.type == .swiftModule && linkerOutputType != .staticLibrary {
inputModules.append(input.file)
} else if input.type == .object {
inputPaths.append(input.file)
} else if input.type == .tbd {
inputPaths.append(input.file)
} else if input.type == .llvmBitcode {
inputPaths.append(input.file)
}
}
let fileList = try VirtualPath.createUniqueFilelist(RelativePath(validating: "inputs.LinkFileList"),
.list(inputPaths))
commandLine.appendPath(fileList)
if linkerOutputType != .staticLibrary {
for module in inputModules {
commandLine.append(.joinedOptionAndPath("-Wl,-add_ast_path,", module))
}
}
// FIXME: Primary inputs need to check -index-file-path
} else {
// Add inputs.
commandLine.append(contentsOf: inputs.flatMap {
(path: TypedVirtualPath) -> [Job.ArgTemplate] in
if path.type == .swiftModule && linkerOutputType != .staticLibrary {
return [.joinedOptionAndPath("-Wl,-add_ast_path,", path.file)]
} else if path.type == .object {
return [.path(path.file)]
} else if path.type == .tbd {
return [.path(path.file)]
} else if path.type == .llvmBitcode {
return [.path(path.file)]
} else {
return []
}
})
}
}
private func addDynamicLinkerFlags(targetInfo: FrontendTargetInfo,
parsedOptions: inout ParsedOptions,
commandLine: inout [Job.ArgTemplate],
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")
}
if let arg = parsedOptions.getLastArgument(.ltoLibrary)?.asSingle {
commandLine.append(.joinedOptionAndPath("-Wl,-lto_library,", try VirtualPath(path: arg)))
}
}
if let arg = parsedOptions.getLastArgument(.useLd)?.asSingle {
commandLine.appendFlag("-fuse-ld=\(arg)")
}
if let arg = parsedOptions.getLastArgument(.ldPath)?.asSingle {
commandLine.append(.joinedOptionAndPath("--ld-path=", try VirtualPath(path: arg)))
}
let fSystemArgs = parsedOptions.arguments(for: .F, .Fsystem)
for opt in fSystemArgs {
commandLine.appendFlag(.F)
commandLine.appendPath(try VirtualPath(path: opt.argument.asSingle))
}
if parsedOptions.contains(.enableAppExtension) {
commandLine.appendFlag("-fapplication-extension")
}
// Pass down an optimization level
if let optArg = mapOptimizationLevelToClangArg(from: &parsedOptions) {
commandLine.appendFlag(optArg)
}
// 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 != .staticLibrary && !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(.sanitizeStableAbiEQ) {
commandLine.appendFlag("-fsanitize-stable-abi")
}
}
if parsedOptions.contains(.embedBitcodeMarker) {
commandLine.appendFlag("-fembed-bitcode=marker")
}
// Add the SDK path
if let sdkPath = targetInfo.sdkPath?.path {
commandLine.appendFlag("--sysroot")
commandLine.appendPath(VirtualPath.lookup(sdkPath))
}
// -link-objc-runtime also implies -fobjc-link-runtime
if parsedOptions.hasFlag(positive: .linkObjcRuntime,
negative: .noLinkObjcRuntime,
default: false) {
commandLine.appendFlag("-fobjc-link-runtime")
}
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")
commandLine.appendFlag(variantTriple.triple)
}
// On Darwin, we only support libc++.
var cxxCompatEnabled = parsedOptions.hasArgument(.enableExperimentalCxxInterop)
if let cxxInteropMode = parsedOptions.getLastArgument(.cxxInteroperabilityMode) {
if cxxInteropMode.asSingle != "off" {
cxxCompatEnabled = true
}
}
if cxxCompatEnabled {
commandLine.appendFlag("-lc++")
}
try addArgsToLinkStdlib(
to: &commandLine,
parsedOptions: &parsedOptions,
targetInfo: targetInfo,
linkerOutputType: linkerOutputType,
fileSystem: fileSystem
)
commandLine.appendFlags(mapInstrumentationTypeToClangArgs(from: &parsedOptions))
// These custom arguments should be right before the object file at the
// end.
try commandLine.appendAllExcept(
includeList: [.linkerOption],
excludeList: [.l],
from: &parsedOptions
)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
try addExtraClangLinkerArgs(to: &commandLine, parsedOptions: &parsedOptions)
}
}
private extension DarwinPlatform {
var profileLibraryNameSuffixes: [String] {
switch self {
case .macOS, .iOS(.catalyst):
return ["osx"]
case .iOS(.device):
return ["ios"]
case .iOS(.simulator):
return ["iossim", "ios"]
case .tvOS(.device):
return ["tvos"]
case .tvOS(.simulator):
return ["tvossim", "tvos"]
case .watchOS(.device):
return ["watchos"]
case .watchOS(.simulator):
return ["watchossim", "watchos"]
case .visionOS(.device):
return ["xros"]
case .visionOS(.simulator):
return ["xrossim", "xros"]
}
}
}