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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2023 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import enum TSCBasic.JSON
extension Triple {
public init(_ description: String) throws {
self.init(description, normalizing: false)
}
}
extension Triple {
public static let macOS = try! Self("x86_64-apple-macosx")
}
extension Triple {
public var isWasm: Bool {
[.wasm32, .wasm64].contains(self.arch)
}
public func isApple() -> Bool {
vendor == .apple
}
public func isAndroid() -> Bool {
os == .linux && environment == .android
}
public func isDarwin() -> Bool {
switch (vendor, os) {
case (.apple, .noneOS):
return false
case (.apple, _), (_, .macosx), (_, .darwin):
return true
default:
return false
}
}
public func isLinux() -> Bool {
os == .linux
}
public func isWindows() -> Bool {
os == .win32
}
public func isWASI() -> Bool {
os == .wasi
}
public func isOpenBSD() -> Bool {
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 """
\(self.archName)-\
\(self.vendorName)-\
\(self.osNameUnversioned)\(version)\
\(self.environmentName.isEmpty ? "" : "-\(self.environmentName)")
"""
}
public var tripleString: String {
self.triple
}
/// Determine the versioned host triple using the Swift compiler.
public static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) throws -> Triple {
// Call the compiler to get the target info JSON.
let compilerOutput: String
do {
let result = try AsyncProcess.popen(args: swiftCompiler.pathString, "-print-target-info")
compilerOutput = try result.utf8Output().spm_chomp()
} catch {
throw InternalError("Failed to get target info (\(error.interpolationDescription))")
}
// Parse the compiler's JSON output.
let parsedTargetInfo: JSON
do {
parsedTargetInfo = try JSON(string: compilerOutput)
} catch {
throw InternalError(
"Failed to parse target info (\(error.interpolationDescription)).\nRaw compiler output: \(compilerOutput)"
)
}
// Get the triple string from the parsed JSON.
let tripleString: String
do {
tripleString = try parsedTargetInfo.get("target").get("triple")
} catch {
throw InternalError(
"Target info does not contain a triple string (\(error.interpolationDescription)).\nTarget info: \(parsedTargetInfo)"
)
}
// Parse the triple string.
do {
return try Triple(tripleString)
} catch {
throw InternalError(
"Failed to parse triple string (\(error.interpolationDescription)).\nTriple string: \(tripleString)"
)
}
}
}
extension Triple {
/// The file prefix for dynamic libraries
public var dynamicLibraryPrefix: String {
switch os {
case .win32:
return ""
default:
return "lib"
}
}
/// The file extension for dynamic libraries (eg. `.dll`, `.so`, or `.dylib`)
public var dynamicLibraryExtension: String {
guard let os = self.os else {
fatalError("Cannot create dynamic libraries unknown os.")
}
switch os {
case _ where isDarwin():
return ".dylib"
case .linux, .openbsd:
return ".so"
case .win32:
return ".dll"
case .wasi:
return ".wasm"
default:
fatalError("Cannot create dynamic libraries for os \"\(os)\".")
}
}
public var executableExtension: String {
guard !self.isWasm else {
return ".wasm"
}
guard let os = self.os else {
return ""
}
switch os {
case _ where isDarwin():
return ""
case .linux, .openbsd:
return ""
case .win32:
return ".exe"
case .noneOS:
return ""
default:
return ""
}
}
/// The file extension for static libraries.
public var staticLibraryExtension: String {
".a"
}
/// The file extension for Foundation-style bundle.
public var nsbundleExtension: String {
switch os {
case _ where isDarwin():
return ".bundle"
default:
// See: https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/FHS%20Bundles.md
return ".resources"
}
}
/// Returns `true` if code compiled for `triple` can run on `self` value of ``Triple``.
public func isRuntimeCompatible(with triple: Triple) -> Bool {
guard self != triple else {
return true
}
if
self.arch == triple.arch &&
self.vendor == triple.vendor &&
self.os == triple.os &&
self.environment == triple.environment
{
return self.osVersion >= triple.osVersion
} else {
return false
}
}
}
extension Triple: CustomStringConvertible {
public var description: String { tripleString }
}
extension Triple: Equatable {
public static func ==(lhs: Triple, rhs: Triple) -> Bool {
lhs.arch == rhs.arch
&& lhs.vendor == rhs.vendor
&& lhs.os == rhs.os
&& lhs.environment == rhs.environment
&& lhs.osVersion == rhs.osVersion
}
}
|