|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | + */ |
| 10 | + |
| 11 | +import TSCBasic |
| 12 | + |
| 13 | +/// Triple - Helper class for working with Destination.target values |
| 14 | +/// |
| 15 | +/// Used for parsing values such as x86_64-apple-macosx10.10 into |
| 16 | +/// set of enums. For os/arch/abi based conditions in build plan. |
| 17 | +/// |
| 18 | +/// @see Destination.target |
| 19 | +/// @see https://github.com/apple/swift-llvm/blob/stable/include/llvm/ADT/Triple.h |
| 20 | +/// |
| 21 | +public struct Triple: Encodable, Equatable { |
| 22 | + public let tripleString: String |
| 23 | + |
| 24 | + public let arch: Arch |
| 25 | + public let vendor: Vendor |
| 26 | + public let os: OS |
| 27 | + public let abi: ABI |
| 28 | + |
| 29 | + public enum Error: Swift.Error { |
| 30 | + case badFormat |
| 31 | + case unknownArch |
| 32 | + case unknownOS |
| 33 | + } |
| 34 | + |
| 35 | + public enum Arch: String, Encodable { |
| 36 | + case x86_64 |
| 37 | + case x86_64h |
| 38 | + case i686 |
| 39 | + case powerpc64le |
| 40 | + case s390x |
| 41 | + case aarch64 |
| 42 | + case armv7 |
| 43 | + case arm |
| 44 | + case arm64 |
| 45 | + case arm64e |
| 46 | + case wasm32 |
| 47 | + } |
| 48 | + |
| 49 | + public enum Vendor: String, Encodable { |
| 50 | + case unknown |
| 51 | + case apple |
| 52 | + } |
| 53 | + |
| 54 | + public enum OS: String, Encodable, CaseIterable { |
| 55 | + case darwin |
| 56 | + case macOS = "macosx" |
| 57 | + case linux |
| 58 | + case windows |
| 59 | + case wasi |
| 60 | + } |
| 61 | + |
| 62 | + public enum ABI: String, Encodable { |
| 63 | + case unknown |
| 64 | + case android |
| 65 | + } |
| 66 | + |
| 67 | + public init(_ string: String) throws { |
| 68 | + let components = string.split(separator: "-").map(String.init) |
| 69 | + |
| 70 | + guard components.count == 3 || components.count == 4 else { |
| 71 | + throw Error.badFormat |
| 72 | + } |
| 73 | + |
| 74 | + guard let arch = Arch(rawValue: components[0]) else { |
| 75 | + throw Error.unknownArch |
| 76 | + } |
| 77 | + |
| 78 | + let vendor = Vendor(rawValue: components[1]) ?? .unknown |
| 79 | + |
| 80 | + guard let os = Triple.parseOS(components[2]) else { |
| 81 | + throw Error.unknownOS |
| 82 | + } |
| 83 | + |
| 84 | + let abi = components.count > 3 ? Triple.parseABI(components[3]) : nil |
| 85 | + |
| 86 | + self.tripleString = string |
| 87 | + self.arch = arch |
| 88 | + self.vendor = vendor |
| 89 | + self.os = os |
| 90 | + self.abi = abi ?? .unknown |
| 91 | + } |
| 92 | + |
| 93 | + fileprivate static func parseOS(_ string: String) -> OS? { |
| 94 | + for candidate in OS.allCases where string.hasPrefix(candidate.rawValue) { |
| 95 | + return candidate |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + fileprivate static func parseABI(_ string: String) -> ABI? { |
| 102 | + if string.hasPrefix(ABI.android.rawValue) { |
| 103 | + return ABI.android |
| 104 | + } |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + public func isAndroid() -> Bool { |
| 109 | + return os == .linux && abi == .android |
| 110 | + } |
| 111 | + |
| 112 | + public func isDarwin() -> Bool { |
| 113 | + return vendor == .apple || os == .macOS || os == .darwin |
| 114 | + } |
| 115 | + |
| 116 | + public func isLinux() -> Bool { |
| 117 | + return os == .linux |
| 118 | + } |
| 119 | + |
| 120 | + public func isWindows() -> Bool { |
| 121 | + return os == .windows |
| 122 | + } |
| 123 | + |
| 124 | + public func isWASI() -> Bool { |
| 125 | + return os == .wasi |
| 126 | + } |
| 127 | + |
| 128 | + /// Returns the triple string for the given platform version. |
| 129 | + /// |
| 130 | + /// This is currently meant for Apple platforms only. |
| 131 | + public func tripleString(forPlatformVersion version: String) -> String { |
| 132 | + precondition(isDarwin()) |
| 133 | + return self.tripleString + version |
| 134 | + } |
| 135 | + |
| 136 | + public static let macOS = try! Triple("x86_64-apple-macosx") |
| 137 | + |
| 138 | + /// Determine the host triple using the Swift compiler. |
| 139 | + public static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) -> Triple { |
| 140 | + do { |
| 141 | + let result = try Process.popen(args: swiftCompiler.pathString, "-print-target-info") |
| 142 | + let output = try result.utf8Output().spm_chomp() |
| 143 | + let targetInfo = try JSON(string: output) |
| 144 | + let tripleString: String = try targetInfo.get("target").get("unversionedTriple") |
| 145 | + return try Triple(tripleString) |
| 146 | + } catch { |
| 147 | + // FIXME: Remove the macOS special-casing once the latest version of Xcode comes with |
| 148 | + // a Swift compiler that supports -print-target-info. |
| 149 | + #if os(macOS) |
| 150 | + return .macOS |
| 151 | + #else |
| 152 | + fatalError("could not determine host triple: \(error)") |
| 153 | + #endif |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +extension Triple { |
| 159 | + /// The file prefix for dynamcic libraries |
| 160 | + public var dynamicLibraryPrefix: String { |
| 161 | + switch os { |
| 162 | + case .windows: |
| 163 | + return "" |
| 164 | + default: |
| 165 | + return "lib" |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /// The file extension for dynamic libraries (eg. `.dll`, `.so`, or `.dylib`) |
| 170 | + public var dynamicLibraryExtension: String { |
| 171 | + switch os { |
| 172 | + case .darwin, .macOS: |
| 173 | + return ".dylib" |
| 174 | + case .linux: |
| 175 | + return ".so" |
| 176 | + case .windows: |
| 177 | + return ".dll" |
| 178 | + case .wasi: |
| 179 | + fatalError("WebAssembly/WASI doesn't support dynamic library yet") |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + public var executableExtension: String { |
| 184 | + switch os { |
| 185 | + case .darwin, .macOS: |
| 186 | + return "" |
| 187 | + case .linux: |
| 188 | + return "" |
| 189 | + case .wasi: |
| 190 | + return "" |
| 191 | + case .windows: |
| 192 | + return ".exe" |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /// The file extension for static libraries. |
| 197 | + public var staticLibraryExtension: String { |
| 198 | + return ".a" |
| 199 | + } |
| 200 | + |
| 201 | + /// The file extension for Foundation-style bundle. |
| 202 | + public var nsbundleExtension: String { |
| 203 | + switch os { |
| 204 | + case .darwin, .macOS: |
| 205 | + return ".bundle" |
| 206 | + default: |
| 207 | + // See: https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/FHS%20Bundles.md |
| 208 | + return ".resources" |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments