Created
May 23, 2022 14:59
-
-
Save Deco354/e94b48aede0acd50c1225c44d817b61b to your computer and use it in GitHub Desktop.
Simulator Architecture Detector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Architecture.swift | |
// TumblrSnapshotTestCase | |
// | |
// Created by Declan McKenna on 26/01/2022. | |
// | |
import Foundation | |
@objc(PSTArchitecture) public final class Architecture: NSObject { // @arnold no-tests | |
/// Check if process runs under Rosetta 2. | |
/// https://developer.apple.com/forums/thread/652667?answerId=618217022&page=1#622923022 | |
/// https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment | |
@objc public static var isRosettaEmulated: Bool { | |
#if targetEnvironment(simulator) | |
return processIsTranslated() == EMULATED_EXECUTION | |
#else | |
return false | |
#endif | |
} | |
/// Returns true on M1 macs and false on intel machines or M1 macs using Rosetta | |
public static var isArm64: Bool { | |
#if arch(arm64) | |
return true | |
#else | |
return false | |
#endif | |
} | |
/// Returns "arm64" or "x86_64" String based on architecture of current macbook | |
public static var current: String { isArm64 ? "arm64" : "x86_64" } | |
} | |
fileprivate let NATIVE_EXECUTION = Int32(0) | |
fileprivate let EMULATED_EXECUTION = Int32(1) | |
fileprivate let UNKNOWN_EXECUTION = -Int32(1) | |
private func processIsTranslated() -> Int32 { | |
let key = "sysctl.proc_translated" | |
var ret = Int32(0) | |
var size: Int = 0 | |
sysctlbyname(key, nil, &size, nil, 0) | |
let result = sysctlbyname(key, &ret, &size, nil, 0) | |
if result == -1 { | |
if errno == ENOENT { | |
return 0 | |
} | |
return -1 | |
} | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment