|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 | import TSCBasic
|
| 13 | +import TSCUtility |
| 14 | +import Foundation |
13 | 15 | import SwiftOptions
|
14 | 16 |
|
15 | 17 | /// Toolchain for Darwin-based platforms, such as macOS and iOS.
|
@@ -205,6 +207,100 @@ public final class DarwinToolchain: Toolchain {
|
205 | 207 | }
|
206 | 208 | }
|
207 | 209 | }
|
| 210 | + |
| 211 | + private struct DarwinSDKInfo: Decodable { |
| 212 | + enum CodingKeys: String, CodingKey { |
| 213 | + case version = "Version" |
| 214 | + case versionMap = "VersionMap" |
| 215 | + } |
| 216 | + |
| 217 | + struct VersionMap: Decodable { |
| 218 | + enum CodingKeys: String, CodingKey { |
| 219 | + case macOSToCatalystMapping = "macOS_iOSMac" |
| 220 | + } |
| 221 | + |
| 222 | + var macOSToCatalystMapping: [Version: Version] |
| 223 | + |
| 224 | + init(from decoder: Decoder) throws { |
| 225 | + let keyedContainer = try decoder.container(keyedBy: CodingKeys.self) |
| 226 | + |
| 227 | + let mappingDict = try keyedContainer.decode([String: String].self, forKey: .macOSToCatalystMapping) |
| 228 | + self.macOSToCatalystMapping = [:] |
| 229 | + try mappingDict.forEach { key, value in |
| 230 | + guard let newKey = Version(potentiallyIncompleteVersionString: key) else { |
| 231 | + throw DecodingError.dataCorruptedError(forKey: .macOSToCatalystMapping, |
| 232 | + in: keyedContainer, |
| 233 | + debugDescription: "Malformed version string") |
| 234 | + } |
| 235 | + guard let newValue = Version(potentiallyIncompleteVersionString: value) else { |
| 236 | + throw DecodingError.dataCorruptedError(forKey: .macOSToCatalystMapping, |
| 237 | + in: keyedContainer, |
| 238 | + debugDescription: "Malformed version string") |
| 239 | + } |
| 240 | + self.macOSToCatalystMapping[newKey] = newValue |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + var version: Version |
| 246 | + var versionMap: VersionMap |
| 247 | + |
| 248 | + init(from decoder: Decoder) throws { |
| 249 | + let keyedContainer = try decoder.container(keyedBy: CodingKeys.self) |
| 250 | + |
| 251 | + let versionString = try keyedContainer.decode(String.self, forKey: .version) |
| 252 | + guard let version = Version(potentiallyIncompleteVersionString: versionString) else { |
| 253 | + throw DecodingError.dataCorruptedError(forKey: .version, |
| 254 | + in: keyedContainer, |
| 255 | + debugDescription: "Malformed version string") |
| 256 | + } |
| 257 | + self.version = version |
| 258 | + self.versionMap = try keyedContainer.decode(VersionMap.self, forKey: .versionMap) |
| 259 | + } |
| 260 | + |
| 261 | + func sdkVersion(for triple: Triple) -> Version { |
| 262 | + if triple.isMacCatalyst { |
| 263 | + // For the Mac Catalyst environment, we have a macOS SDK with a macOS |
| 264 | + // SDK version. Map that to the corresponding iOS version number to pass |
| 265 | + // down to the linker. |
| 266 | + return versionMap.macOSToCatalystMapping[version.withoutBuildNumbers] ?? Version(0, 0, 0) |
| 267 | + } |
| 268 | + return version |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + // SDK info is computed lazily. This should not generally be accessed directly. |
| 273 | + private var _sdkInfo: DarwinSDKInfo? = nil |
| 274 | + |
| 275 | + private func getTargetSDKInfo(sdkPath: VirtualPath) -> DarwinSDKInfo? { |
| 276 | + if let info = _sdkInfo { |
| 277 | + return info |
| 278 | + } else { |
| 279 | + let sdkSettingsPath = sdkPath.appending(component: "SDKSettings.json") |
| 280 | + guard let contents = try? fileSystem.readFileContents(sdkSettingsPath) else { return nil } |
| 281 | + guard let sdkInfo = try? JSONDecoder().decode(DarwinSDKInfo.self, |
| 282 | + from: Data(contents.contents)) else { return nil } |
| 283 | + self._sdkInfo = sdkInfo |
| 284 | + return sdkInfo |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + public func addPlatformSpecificCommonFrontendOptions( |
| 289 | + commandLine: inout [Job.ArgTemplate], |
| 290 | + inputs: inout [TypedVirtualPath], |
| 291 | + frontendTargetInfo: FrontendTargetInfo |
| 292 | + ) throws { |
| 293 | + guard let sdkPath = try frontendTargetInfo.paths.sdkPath.map(VirtualPath.init(path:)), |
| 294 | + let sdkInfo = getTargetSDKInfo(sdkPath: sdkPath) else { return } |
| 295 | + |
| 296 | + commandLine.append(.flag("-target-sdk-version")) |
| 297 | + commandLine.append(.flag(sdkInfo.sdkVersion(for: frontendTargetInfo.target.triple).description)) |
| 298 | + |
| 299 | + if let targetVariantTriple = frontendTargetInfo.targetVariant?.triple { |
| 300 | + commandLine.append(.flag("-target-variant-sdk-version")) |
| 301 | + commandLine.append(.flag(sdkInfo.sdkVersion(for: targetVariantTriple).description)) |
| 302 | + } |
| 303 | + } |
208 | 304 | }
|
209 | 305 |
|
210 | 306 | extension Diagnostic.Message {
|
|
0 commit comments