forked from swiftlang/swift-sdk-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxRecipe.swift
303 lines (266 loc) · 9.93 KB
/
LinuxRecipe.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022-2023 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 Foundation
import Helpers
import struct SystemPackage.FilePath
public struct LinuxRecipe: SwiftSDKRecipe {
public enum TargetSwiftSource: Sendable {
case docker(baseSwiftDockerImage: String)
case localPackage(FilePath)
case remoteTarball
}
public enum HostSwiftSource: Sendable, Equatable {
case localPackage(FilePath)
case remoteTarball
case preinstalled
}
let mainTargetTriple: Triple
let mainHostTriple: Triple
let linuxDistribution: LinuxDistribution
let targetSwiftSource: TargetSwiftSource
let hostSwiftSource: HostSwiftSource
let versionsConfiguration: VersionsConfiguration
var shouldUseDocker: Bool {
if case .docker = self.targetSwiftSource {
return true
}
return false
}
public init(
targetTriple: Triple,
hostTriple: Triple,
linuxDistribution: LinuxDistribution,
swiftVersion: String,
swiftBranch: String?,
lldVersion: String,
withDocker: Bool,
fromContainerImage: String?,
hostSwiftPackagePath: String?,
targetSwiftPackagePath: String?,
includeHostToolchain: Bool = false
) throws {
let versionsConfiguration = try VersionsConfiguration(
swiftVersion: swiftVersion,
swiftBranch: swiftBranch,
lldVersion: lldVersion,
linuxDistribution: linuxDistribution,
targetTriple: targetTriple
)
let targetSwiftSource: LinuxRecipe.TargetSwiftSource
if let targetSwiftPackagePath {
targetSwiftSource = .localPackage(FilePath(targetSwiftPackagePath))
} else {
if withDocker || fromContainerImage != nil {
let imageName = fromContainerImage ?? versionsConfiguration.swiftBaseDockerImage
targetSwiftSource = .docker(baseSwiftDockerImage: imageName)
} else {
targetSwiftSource = .remoteTarball
}
}
let hostSwiftSource: HostSwiftSource
if includeHostToolchain == false {
hostSwiftSource = .preinstalled
} else if let hostSwiftPackagePath {
hostSwiftSource = .localPackage(FilePath(hostSwiftPackagePath))
} else {
hostSwiftSource = .remoteTarball
}
self.init(
mainTargetTriple: targetTriple,
mainHostTriple: hostTriple,
linuxDistribution: linuxDistribution,
targetSwiftSource: targetSwiftSource,
hostSwiftSource: hostSwiftSource,
versionsConfiguration: versionsConfiguration
)
}
public init(
mainTargetTriple: Triple,
mainHostTriple: Triple,
linuxDistribution: LinuxDistribution,
targetSwiftSource: TargetSwiftSource,
hostSwiftSource: HostSwiftSource,
versionsConfiguration: VersionsConfiguration
) {
self.mainTargetTriple = mainTargetTriple
self.mainHostTriple = mainHostTriple
self.linuxDistribution = linuxDistribution
self.targetSwiftSource = targetSwiftSource
self.hostSwiftSource = hostSwiftSource
self.versionsConfiguration = versionsConfiguration
}
public func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple) {
if self.hostSwiftSource == .preinstalled {
toolset.rootPath = nil
}
var swiftCompilerOptions = ["-Xlinker", "-R/usr/lib/swift/linux/"]
// Swift 5.9 does not handle the `-use-ld` option properly:
// https://github.com/swiftlang/swift-package-manager/issues/7222
if self.versionsConfiguration.swiftVersion.hasPrefix("5.9") {
swiftCompilerOptions += ["-Xclang-linker", "--ld-path=ld.lld"]
} else {
swiftCompilerOptions.append("-use-ld=lld")
if self.hostSwiftSource != .preinstalled {
toolset.linker = Toolset.ToolProperties(path: "ld.lld")
}
}
toolset.swiftCompiler = Toolset.ToolProperties(extraCLIOptions: swiftCompilerOptions)
toolset.cxxCompiler = Toolset.ToolProperties(extraCLIOptions: ["-lstdc++"])
toolset.librarian = Toolset.ToolProperties(path: "llvm-ar")
}
public func applyPlatformOptions(
metadata: inout SwiftSDKMetadataV4.TripleProperties,
paths: PathsConfiguration,
targetTriple: Triple
) {
var relativeSDKDir = self.sdkDirPath(paths: paths)
guard relativeSDKDir.removePrefix(paths.swiftSDKRootPath) else {
fatalError("The SDK directory path must be a subdirectory of the Swift SDK root path.")
}
metadata.swiftResourcesPath = relativeSDKDir.appending("usr/lib/swift").string
metadata.swiftStaticResourcesPath = relativeSDKDir.appending("usr/lib/swift_static").string
}
public var defaultArtifactID: String {
"""
\(self.versionsConfiguration.swiftVersion)_\(self.linuxDistribution.name.rawValue)_\(
self.linuxDistribution
.release
)_\(
self.mainTargetTriple.arch!.linuxConventionName
)
"""
}
func sdkDirPath(paths: PathsConfiguration) -> FilePath {
paths.swiftSDKRootPath
.appending("\(self.linuxDistribution.name.rawValue)-\(self.linuxDistribution.release).sdk")
}
func itemsToDownload(from artifacts: DownloadableArtifacts) -> [DownloadableArtifacts.Item] {
var items: [DownloadableArtifacts.Item] = []
if self.hostSwiftSource != .preinstalled
&& self.mainHostTriple.os != .linux
&& !self.versionsConfiguration.swiftVersion.hasPrefix("6.0") {
items.append(artifacts.hostLLVM)
}
switch self.targetSwiftSource {
case .remoteTarball:
items.append(artifacts.targetSwift)
case .docker, .localPackage: break
}
switch self.hostSwiftSource {
case .remoteTarball:
items.append(artifacts.hostSwift)
case .localPackage: break
case .preinstalled: break
}
return items
}
var hostTriples: [Triple]? {
if self.hostSwiftSource == .preinstalled {
// Swift 5.9 and 5.10 require `supportedTriples` to be set in info.json.
// FIXME: This can be removed once the SDK generator does not support 5.9/5.10 any more.
if self.versionsConfiguration.swiftVersion.hasPrefix("5.9")
|| self.versionsConfiguration.swiftVersion.hasPrefix("5.10") {
return [
Triple("x86_64-unknown-linux-gnu"),
Triple("aarch64-unknown-linux-gnu"),
Triple("x86_64-apple-macos"),
Triple("arm64-apple-macos"),
]
}
// Swift 6.0 and later can set `supportedTriples` to nil
return nil
}
return [self.mainHostTriple]
}
public func makeSwiftSDK(
generator: SwiftSDKGenerator,
engine: QueryEngine,
httpClient client: some HTTPClientProtocol
) async throws -> SwiftSDKProduct {
let sdkDirPath = self.sdkDirPath(paths: generator.pathsConfiguration)
if !generator.isIncremental {
try await generator.removeRecursively(at: sdkDirPath)
}
try await generator.createDirectoryIfNeeded(at: sdkDirPath)
var downloadableArtifacts = try DownloadableArtifacts(
hostTriple: mainHostTriple,
targetTriple: generator.targetTriple,
self.versionsConfiguration,
generator.pathsConfiguration
)
try await generator.downloadArtifacts(
client,
engine,
downloadableArtifacts: &downloadableArtifacts,
itemsToDownload: { artifacts in itemsToDownload(from: artifacts) }
)
if !self.shouldUseDocker {
guard case let .ubuntu(version) = linuxDistribution else {
throw GeneratorError
.distributionSupportsOnlyDockerGenerator(self.linuxDistribution)
}
try await generator.downloadUbuntuPackages(
client,
engine,
requiredPackages: version.requiredPackages,
versionsConfiguration: self.versionsConfiguration,
sdkDirPath: sdkDirPath
)
}
switch self.hostSwiftSource {
case let .localPackage(filePath):
try await generator.rsync(
from: filePath.appending("usr"), to: generator.pathsConfiguration.toolchainDirPath
)
case .remoteTarball:
try await generator.unpackHostSwift(
hostSwiftPackagePath: downloadableArtifacts.hostSwift.localPath
)
case .preinstalled:
break
}
switch self.targetSwiftSource {
case let .docker(baseSwiftDockerImage):
try await generator.copyTargetSwiftFromDocker(
targetDistribution: self.linuxDistribution,
baseDockerImage: baseSwiftDockerImage,
sdkDirPath: sdkDirPath
)
case let .localPackage(filePath):
try await generator.copyTargetSwift(
from: filePath.appending("usr"), sdkDirPath: sdkDirPath
)
case .remoteTarball:
try await generator.unpackTargetSwiftPackage(
targetSwiftPackagePath: downloadableArtifacts.targetSwift.localPath,
relativePathToRoot: [FilePath.Component(self.versionsConfiguration.swiftDistributionName())!],
sdkDirPath: sdkDirPath
)
}
try await generator.fixAbsoluteSymlinks(sdkDirPath: sdkDirPath)
if self.hostSwiftSource != .preinstalled {
if self.mainHostTriple.os != .linux && !self.versionsConfiguration.swiftVersion.hasPrefix("6.0") {
try await generator.prepareLLDLinker(engine, llvmArtifact: downloadableArtifacts.hostLLVM)
}
if self.versionsConfiguration.swiftVersion.hasPrefix("5.9") ||
self.versionsConfiguration.swiftVersion.hasPrefix("5.10") {
try await generator.symlinkClangHeaders()
}
let autolinkExtractPath = generator.pathsConfiguration.toolchainBinDirPath.appending("swift-autolink-extract")
if await !generator.doesFileExist(at: autolinkExtractPath) {
logGenerationStep("Fixing `swift-autolink-extract` symlink...")
try await generator.createSymlink(at: autolinkExtractPath, pointingTo: "swift")
}
}
return SwiftSDKProduct(sdkDirPath: sdkDirPath, hostTriples: self.hostTriples)
}
}