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
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import protocol Foundation.CustomNSError
import var Foundation.NSLocalizedDescriptionKey
import TSCBasic
/// Triple - Helper class for working with Destination.target values
///
/// Used for parsing values such as x86_64-apple-macosx10.10 into
/// set of enums. For os/arch/abi based conditions in build plan.
///
/// @see Destination.target
/// @see https://github.com/apple/swift-llvm/blob/stable/include/llvm/ADT/Triple.h
///
@available(*, deprecated, message: "use `Basics.Triple` if you're libSwiftPM client, create application-specific `Triple` type otherwise")
public struct Triple: Encodable, Equatable, Sendable {
public let tripleString: String
public let arch: Arch
public let vendor: Vendor
public let os: OS
public let abi: ABI
public let osVersion: String?
public let abiVersion: String?
public enum Error: Swift.Error {
case badFormat(triple: String)
case unknownArch(arch: String)
case unknownOS(os: String)
}
public enum Arch: String, Encodable, Sendable {
case x86_64
case x86_64h
case i686
case powerpc
case powerpc64le
case s390x
case aarch64
case amd64
case armv7
case armv6
case armv5
case arm
case arm64
case arm64e
case wasm32
case riscv64
case mips
case mipsel
case mips64
case mips64el
}
public enum Vendor: String, Encodable, Sendable {
case unknown
case apple
}
public enum OS: String, Encodable, CaseIterable, Sendable {
case darwin
case macOS = "macosx"
case linux
case windows
case wasi
case openbsd
}
public enum ABI: Encodable, Equatable, RawRepresentable, Sendable {
case unknown
case android
case other(name: String)
public init?(rawValue: String) {
if rawValue.hasPrefix(ABI.android.rawValue) {
self = .android
} else if let version = rawValue.firstIndex(where: { $0.isNumber }) {
self = .other(name: String(rawValue[..<version]))
} else {
self = .other(name: rawValue)
}
}
public var rawValue: String {
switch self {
case .android: return "android"
case .other(let name): return name
case .unknown: return "unknown"
}
}
public static func ==(lhs: ABI, rhs: ABI) -> Bool {
switch (lhs, rhs) {
case (.unknown, .unknown):
return true
case (.android, .android):
return true
case let (.other(lhsName), .other(rhsName)):
return lhsName == rhsName
default:
return false
}
}
}
public init(_ string: String) throws {
let components = string.split(separator: "-").map(String.init)
guard components.count == 3 || components.count == 4 else {
throw Error.badFormat(triple: string)
}
guard let arch = Arch(rawValue: components[0]) else {
throw Error.unknownArch(arch: components[0])
}
let vendor = Vendor(rawValue: components[1]) ?? .unknown
guard let os = Triple.parseOS(components[2]) else {
throw Error.unknownOS(os: components[2])
}
let osVersion = Triple.parseVersion(components[2])
let abi = components.count > 3 ? Triple.ABI(rawValue: components[3]) : nil
let abiVersion = components.count > 3 ? Triple.parseVersion(components[3]) : nil
self.tripleString = string
self.arch = arch
self.vendor = vendor
self.os = os
self.osVersion = osVersion
self.abi = abi ?? .unknown
self.abiVersion = abiVersion
}
fileprivate static func parseOS(_ string: String) -> OS? {
var candidates = OS.allCases.map{ (name: $0.rawValue, value: $0) }
// LLVM target triples support this alternate spelling as well.
candidates.append((name: "macos", value: .macOS))
return candidates.first(where: { string.hasPrefix($0.name) })?.value
}
fileprivate static func parseVersion(_ string: String) -> String? {
let candidate = String(string.drop(while: { $0.isLetter }))
if candidate != string && !candidate.isEmpty {
return candidate
}
return nil
}
public func isAndroid() -> Bool {
return os == .linux && abi == .android
}
public func isDarwin() -> Bool {
return vendor == .apple || os == .macOS || os == .darwin
}
public func isLinux() -> Bool {
return os == .linux
}
public func isWindows() -> Bool {
return os == .windows
}
public func isWASI() -> Bool {
return os == .wasi
}
public func isOpenBSD() -> Bool {
return os == .openbsd
}
/// Returns the triple string for the given platform version.
///
/// This is currently meant for Apple platforms only.
public func tripleString(forPlatformVersion version: String) -> String {
precondition(isDarwin())
return String(self.tripleString.dropLast(self.osVersion?.count ?? 0)) + version
}
public static let macOS = try! Triple("x86_64-apple-macosx")
/// Determine the versioned host triple using the Swift compiler.
public static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) -> Triple {
// Call the compiler to get the target info JSON.
let compilerOutput: String
do {
let result = try Process.popen(args: swiftCompiler.pathString, "-print-target-info")
compilerOutput = try result.utf8Output().spm_chomp()
} catch {
// FIXME: Remove the macOS special-casing once the latest version of Xcode comes with
// a Swift compiler that supports -print-target-info.
#if os(macOS)
return .macOS
#else
fatalError("Failed to get target info (\(error))")
#endif
}
// Parse the compiler's JSON output.
let parsedTargetInfo: JSON
do {
parsedTargetInfo = try JSON(string: compilerOutput)
} catch {
fatalError("Failed to parse target info (\(error)).\nRaw compiler output: \(compilerOutput)")
}
// Get the triple string from the parsed JSON.
let tripleString: String
do {
tripleString = try parsedTargetInfo.get("target").get("triple")
} catch {
fatalError("Target info does not contain a triple string (\(error)).\nTarget info: \(parsedTargetInfo)")
}
// Parse the triple string.
do {
return try Triple(tripleString)
} catch {
fatalError("Failed to parse triple string (\(error)).\nTriple string: \(tripleString)")
}
}
public static func ==(lhs: Triple, rhs: Triple) -> Bool {
return lhs.arch == rhs.arch && lhs.vendor == rhs.vendor && lhs.os == rhs.os && lhs.abi == rhs.abi && lhs.osVersion == rhs.osVersion && lhs.abiVersion == rhs.abiVersion
}
}
@available(*, deprecated)
extension Triple {
/// The file prefix for dynamic libraries
public var dynamicLibraryPrefix: String {
switch os {
case .windows:
return ""
default:
return "lib"
}
}
/// The file extension for dynamic libraries (eg. `.dll`, `.so`, or `.dylib`)
public var dynamicLibraryExtension: String {
switch os {
case .darwin, .macOS:
return ".dylib"
case .linux, .openbsd:
return ".so"
case .windows:
return ".dll"
case .wasi:
return ".wasm"
}
}
public var executableExtension: String {
switch os {
case .darwin, .macOS:
return ""
case .linux, .openbsd:
return ""
case .wasi:
return ".wasm"
case .windows:
return ".exe"
}
}
/// The file extension for static libraries.
public var staticLibraryExtension: String {
return ".a"
}
/// The file extension for Foundation-style bundle.
public var nsbundleExtension: String {
switch os {
case .darwin, .macOS:
return ".bundle"
default:
// See: https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/FHS%20Bundles.md
return ".resources"
}
}
}
@available(*, deprecated)
extension Triple.Error: CustomNSError {
public var errorUserInfo: [String : Any] {
return [NSLocalizedDescriptionKey: "\(self)"]
}
}
|