-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathXcodeBuilder.swift
385 lines (310 loc) · 12 KB
/
XcodeBuilder.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//
// XcodeBuilder.swift
// swift-create-xcframework
//
// Created by Rob Amos on 7/5/20.
//
import Build
import Foundation
import PackageModel
import TSCBasic
import TSCUtility
import Xcodeproj
struct XcodeBuilder {
// MARK: - Properties
let path: AbsolutePath
let project: Xcode.Project
let package: PackageInfo
let options: Command.Options
var buildDirectory: Foundation.URL {
self.package.projectBuildDirectory
.appendingPathComponent("build")
.absoluteURL
}
// MARK: - Initialisation
init (project: Xcode.Project, projectPath: AbsolutePath, package: PackageInfo, options: Command.Options) {
self.project = project
self.path = projectPath
self.package = package
self.options = options
}
// MARK: - Clean
func clean () throws {
let process = TSCBasic.Process (
arguments: [
"xcrun",
"xcodebuild",
"-project", self.path.pathString,
"BUILD_DIR=\(self.buildDirectory.path)",
"clean"
],
outputRedirection: .none
)
try process.launch()
let result = try process.waitUntilExit()
switch result.exitStatus {
case let .terminated(code: code):
if code != 0 {
throw Error.nonZeroExit("xcodebuild", code)
}
case let .signalled(signal: signal):
throw Error.signalExit("xcodebuild", signal)
}
}
// MARK: - Build
struct BuildResult {
let target: String
let frameworkPath: Foundation.URL
let debugSymbolsPath: Foundation.URL
}
func build (targets: [String], sdk: TargetPlatform.SDK) throws -> [String: BuildResult] {
for target in targets {
let process = TSCBasic.Process (
arguments: try self.buildCommand(target: target, sdk: sdk),
outputRedirection: .none
)
try process.launch()
let result = try process.waitUntilExit()
switch result.exitStatus {
case let .terminated(code: code):
if code != 0 {
throw Error.nonZeroExit("xcodebuild", code)
}
case let .signalled(signal: signal):
throw Error.signalExit("xcodebuild", signal)
}
}
return targets
.reduce(into: [String: BuildResult]()) { dict, name in
dict[name] = BuildResult (
target: name,
frameworkPath: self.frameworkPath(target: name, sdk: sdk),
debugSymbolsPath: self.debugSymbolsPath(target: name, sdk: sdk)
)
}
}
private func buildCommand (target: String, sdk: TargetPlatform.SDK) throws -> [String] {
var command: [String] = [
"xcrun",
"xcodebuild",
"-project", self.path.pathString,
"-configuration", self.options.configuration.xcodeConfigurationName,
"-destination", sdk.destination,
"BUILD_DIR=\(self.buildDirectory.path)",
"SKIP_INSTALL=NO"
]
if self.options.action == .archive {
command += [
"-archivePath", self.buildDirectory.appendingPathComponent(self.productName(target: target)).appendingPathComponent(sdk.archiveName).path,
]
}
// add SDK-specific build settings
if let settings = sdk.buildSettings {
for setting in settings {
command.append("\(setting.key)=\(setting.value)")
}
}
// enable evolution for the whole stack
if self.options.stackEvolution {
command.append("BUILD_LIBRARY_FOR_DISTRIBUTION=YES")
}
// add build settings provided in the invocation
self.options.xcSetting.forEach { setting in
command.append("\(setting.name)=\(setting.value)")
}
// add our targets
command += [ "-scheme", target ]
// and the command
if self.options.action == .build {
command += [ "build" ]
} else {
command += [ "archive" ]
}
return command
}
// we should probably pull this from the build output but we just make assumptions here
private func frameworkPath (target: String, sdk: TargetPlatform.SDK) -> Foundation.URL {
if self.options.action == .build {
return self.buildDirectory
.appendingPathComponent(sdk.releaseFolder)
.appendingPathComponent("\(self.productName(target: target)).framework")
.absoluteURL
} else {
return self.buildDirectory
.appendingPathComponent(self.productName(target: target))
.appendingPathComponent(sdk.archiveName)
.appendingPathComponent("Products/Library/Frameworks")
.appendingPathComponent("\(self.productName(target: target)).framework")
.absoluteURL
}
}
// MARK: - Debug Symbols
private func debugSymbolsPath (target: String, sdk: TargetPlatform.SDK) -> Foundation.URL {
return self.buildDirectory
.appendingPathComponent(sdk.releaseFolder)
}
private func dSYMPath (target: String, path: Foundation.URL) -> Foundation.URL {
return path
.appendingPathComponent("\(self.productName(target: target)).framework.dSYM")
}
private func dwarfPath (target: String, path: Foundation.URL) -> Foundation.URL {
return path
.appendingPathComponent("Contents/Resources/DWARF")
.appendingPathComponent(self.productName(target: target))
}
private func debugSymbolFiles (target: String, path: Foundation.URL) throws -> [Foundation.URL] {
// if there is no dSYM directory there is no point continuing
let dsym = self.dSYMPath(target: target, path: path)
guard FileManager.default.fileExists(atPath: dsym.absoluteURL.path) else {
return []
}
var files = [
dsym
]
// if we have a dwarf file we can inspect that to get the slice UUIDs
let dwarf = self.dwarfPath(target: target, path: dsym)
guard FileManager.default.fileExists(atPath: dwarf.absoluteURL.path) else {
return files
}
// get the UUID of the slices in the DWARF
let identifiers = try self.binarySliceIdentifiers(file: dwarf)
// They should be bcsymbolmap files in the debug dir
for identifier in identifiers {
let file = "\(identifier.uuidString.uppercased()).bcsymbolmap"
files.append(path.appendingPathComponent(file))
}
return files
}
private func binarySliceIdentifiers (file: Foundation.URL) throws -> [UUID] {
let command = [
"xcrun",
"dwarfdump",
"--uuid",
file.absoluteURL.path
]
let process = TSCBasic.Process (
arguments: command,
outputRedirection: .collect
)
try process.launch()
let result = try process.waitUntilExit()
switch result.exitStatus {
case let .terminated(code: code):
if code != 0 {
throw Error.nonZeroExit("dwarfdump", code)
}
case let .signalled(signal: signal):
throw Error.signalExit("dwarfdump", signal)
}
switch result.output {
case let .success(output):
guard let string = String(bytes: output, encoding: .utf8) else {
return []
}
return try string.sliceIdentifiers()
case let .failure(error):
throw Error.errorThrown("dwarfdump", error)
}
}
// MARK: - Merging
func merge (target: String, buildResults: [BuildResult]) throws -> Foundation.URL {
let outputPath = self.xcframeworkPath(target: target)
// try to remove it if its already there, otherwise we're going to get errors
try? FileManager.default.removeItem(at: outputPath)
let process = TSCBasic.Process (
arguments: try self.mergeCommand(outputPath: outputPath, buildResults: buildResults),
outputRedirection: .none
)
try process.launch()
let result = try process.waitUntilExit()
switch result.exitStatus {
case let .terminated(code: code):
if code != 0 {
throw Error.nonZeroExit("xcodebuild", code)
}
case let .signalled(signal: signal):
throw Error.signalExit("xcodebuild", signal)
}
return outputPath
}
private func mergeCommand (outputPath: Foundation.URL, buildResults: [BuildResult]) throws -> [String] {
var command: [String] = [
"xcrun",
"xcodebuild",
"-create-xcframework"
]
// add our frameworks and any debugging symbols
command += try buildResults.flatMap { result -> [String] in
var args = [ "-framework", result.frameworkPath.absoluteURL.path ]
if self.package.options.debugSymbols {
let symbolFiles = try self.debugSymbolFiles(target: result.target, path: result.debugSymbolsPath)
for file in symbolFiles {
if FileManager.default.fileExists(atPath: file.absoluteURL.path) {
args += [ "-debug-symbols", file.absoluteURL.path ]
}
}
}
return args
}
if self.package.options.allowInternalDistribution {
command += [ "-allow-internal-distribution" ]
}
// and the output
command += [ "-output", outputPath.path ]
return command
}
private func xcframeworkPath (target: String) -> Foundation.URL {
return URL(fileURLWithPath: self.options.output)
.appendingPathComponent("\(self.productName(target: target)).xcframework")
}
private func productName (target: String) -> String {
// Xcode replaces any non-alphanumeric characters in the target with an underscore
// https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c
return target
.replacingOccurrences(of: "[^0-9a-zA-Z]", with: "_", options: .regularExpression)
.replacingOccurrences(of: "^[0-9]", with: "_", options: .regularExpression)
}
// MARK: - Errors
enum Error: Swift.Error, LocalizedError {
case nonZeroExit(String, Int32)
case signalExit(String, Int32)
case errorThrown(String, Swift.Error)
var errorDescription: String? {
switch self {
case let .nonZeroExit(command, code):
return "\(command) exited with a non-zero code: \(code)"
case let .signalExit(command, signal):
return "\(command) exited due to signal: \(signal)"
case let .errorThrown(command, error):
return "\(command) returned unexpected error: \(error)"
}
}
}
}
// MARK: - Helper Extensions
extension BuildConfiguration {
var xcodeConfigurationName: String {
switch self {
case .debug: return "Debug"
case .release: return "Release"
}
}
}
private extension String {
func sliceIdentifiers() throws -> [UUID] {
let regex = try NSRegularExpression(pattern: #"^UUID: ([a-zA-Z0-9\-]+)"#, options: .anchorsMatchLines)
let matches = regex.matches(in: self, options: [], range: NSRange(location: 0, length: self.count))
guard matches.isEmpty == false else {
return []
}
return matches
.compactMap { match in
let nsrange = match.range(at: 1)
guard let range = Range(nsrange, in: self) else {
return nil
}
return String(self[range])
}
.compactMap(UUID.init(uuidString:))
}
}