Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect apple silicon (M1) vs. intel in swift 5.0?

Tags:

swift

I make a call to ffmpeg and ffprobe. This issue I have is that Brew installs them in different locations based on architecture. On an Intel machine the binaries are installed in /usr/local/bin, but on the M1 the binaries are installed in /opt/homebrew/bin.

I'd like to do something like:

let ffprobe = Process()
if intel {
    ffprobe.executableURL = URL(fileURLWithPath: "/usr/local/bin/ffprobe")
} else {
    ffprobe.executableURL = URL(fileURLWithPath: "/opt/homebrew/bin/ffprobe")
}

I have searched for this and cannot find anything.

like image 527
SouthernYankee65 Avatar asked Oct 23 '25 14:10

SouthernYankee65


1 Answers

I got this answer from the Apple Developer Forums:

var systeminfo = utsname()
uname(&systeminfo)
let machine = withUnsafeBytes(of: &systeminfo.machine) {bufPtr->String in
    let data = Data(bufPtr)
    if let lastIndex = data.lastIndex(where: {$0 != 0}) {
        return String(data: data[0...lastIndex], encoding: .isoLatin1)!
    } else {
        return String(data: data, encoding: .isoLatin1)!
    }
}
print(machine)

Here's the link for reference Apple Developer Forum.

like image 53
SouthernYankee65 Avatar answered Oct 26 '25 04:10

SouthernYankee65



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!