Skip to content

[Triple] More detailed errors if getting the triple from the compiler should fail #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions Sources/TSCUtility/Triple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,39 @@ public struct Triple: Encodable, Equatable {

/// 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")
let output = try result.utf8Output().spm_chomp()
let targetInfo = try JSON(string: output)
let tripleString: String = try targetInfo.get("target").get("triple")
return try Triple(tripleString)
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("could not determine host triple: \(error)")
#endif
#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)")
}
}
}
Expand Down