🦾 Check for Apple Silicon

This commit is contained in:
Chris Araman 2021-05-11 21:45:24 -07:00 committed by Ben Chatelain
parent 844131ff2e
commit c9378046a6
3 changed files with 51 additions and 1 deletions

View file

@ -42,7 +42,11 @@ let package = Package(
),
.target(
name: "MasKit",
dependencies: ["Commandant", "PromiseKit", "Version"],
dependencies: [
"Commandant",
"PromiseKit",
"Version",
],
swiftSettings: [
.unsafeFlags([
"-I", "Sources/PrivateFrameworks/CommerceKit",

View file

@ -14,6 +14,7 @@ protocol ExternalCommand {
var process: Process { get }
var stdout: String { get }
var stderr: String { get }
var stdoutPipe: Pipe { get }
var stderrPipe: Pipe { get }
@ -28,6 +29,11 @@ protocol ExternalCommand {
/// Common implementation
extension ExternalCommand {
var stdout: String {
let data = stdoutPipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: .utf8) ?? ""
}
var stderr: String {
let data = stderrPipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: .utf8) ?? ""

View file

@ -0,0 +1,40 @@
//
// SysCtlSystemCommand.swift
// MasKit
//
// Created by Chris Araman on 6/3/21.
// Copyright © 2021 mas-cli. All rights reserved.
//
import Foundation
/// Wrapper for the external sysctl system command.
/// https://ss64.com/osx/sysctl.html
struct SysCtlSystemCommand: ExternalCommand {
var binaryPath: String
let process = Process()
let stdoutPipe = Pipe()
let stderrPipe = Pipe()
init(binaryPath: String = "/usr/sbin/sysctl") {
self.binaryPath = binaryPath
}
static var isAppleSilicon: Bool = {
let sysctl = SysCtlSystemCommand()
do {
// Returns 1 on Apple Silicon even when run in an Intel context in Rosetta.
try sysctl.run(arguments: "-in", "hw.optional.arm64")
} catch {
fatalError("sysctl failed")
}
guard sysctl.succeeded else {
fatalError("sysctl failed")
}
return sysctl.stdout.trimmingCharacters(in: .newlines) == "1"
}()
}