Skip to content

Commit 6f2fecd

Browse files
authored
Merge pull request #279 from karwa/triple-triad
[Triple] More detailed errors if getting the triple from the compiler should fail
2 parents bccbd93 + bdfc2fa commit 6f2fecd

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

Sources/TSCUtility/Triple.swift

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,39 @@ public struct Triple: Encodable, Equatable {
161161

162162
/// Determine the versioned host triple using the Swift compiler.
163163
public static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) -> Triple {
164+
// Call the compiler to get the target info JSON.
165+
let compilerOutput: String
164166
do {
165167
let result = try Process.popen(args: swiftCompiler.pathString, "-print-target-info")
166-
let output = try result.utf8Output().spm_chomp()
167-
let targetInfo = try JSON(string: output)
168-
let tripleString: String = try targetInfo.get("target").get("triple")
169-
return try Triple(tripleString)
168+
compilerOutput = try result.utf8Output().spm_chomp()
170169
} catch {
171170
// FIXME: Remove the macOS special-casing once the latest version of Xcode comes with
172171
// a Swift compiler that supports -print-target-info.
173-
#if os(macOS)
174-
return .macOS
175-
#else
176-
fatalError("could not determine host triple: \(error)")
177-
#endif
172+
#if os(macOS)
173+
return .macOS
174+
#else
175+
fatalError("Failed to get target info (\(error))")
176+
#endif
177+
}
178+
// Parse the compiler's JSON output.
179+
let parsedTargetInfo: JSON
180+
do {
181+
parsedTargetInfo = try JSON(string: compilerOutput)
182+
} catch {
183+
fatalError("Failed to parse target info (\(error)).\nRaw compiler output: \(compilerOutput)")
184+
}
185+
// Get the triple string from the parsed JSON.
186+
let tripleString: String
187+
do {
188+
tripleString = try parsedTargetInfo.get("target").get("triple")
189+
} catch {
190+
fatalError("Target info does not contain a triple string (\(error)).\nTarget info: \(parsedTargetInfo)")
191+
}
192+
// Parse the triple string.
193+
do {
194+
return try Triple(tripleString)
195+
} catch {
196+
fatalError("Failed to parse triple string (\(error)).\nTriple string: \(tripleString)")
178197
}
179198
}
180199
}

0 commit comments

Comments
 (0)