Detect if app is running on M1 Mac

Hi,

I’m using some optional libraries in my app that do not have support for M1 macs and I’d like to be able to skip loading them to since they’d otherwise make my app crash at some point.

Is there a way to check in my code whether or not I’m running on a M1 mac? Just going by the processor name using SystemInfo.processorType works in some cases, but is unreliable (could be “Apple M1”, “VirtualApple”, probably some more).

Thanks,
Denchi

Is your app running natively on the M1 Macs or via Rosetta?

It’s a “normal” macOS build, signed and uploaded to Steam, which I’d assume means it’s running via Rosetta (don’t have a M1 Mac myself, will have to ask my users). Actually I wasn’t even aware Unity could do native M1 builds already.

You can P/Invoke to sysctlbyname to determine if you’re running on Rosetta:

For completeness, if you built Apple silicon native or universal app, you could use
System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.

Awesome, thanks. I’ll give that a try.

Although I’m not quite sure how to access errno after the P/Invoke.

Also, is there a good place to find out more about the silicon native/universal builds? Would love to read up on that.

We introduced support for them in Unity 2020.2. It’s just a matter of choosing it in a combobox: 6974135--822422--upload_2021-3-25_10-50-3.png

We also have a megathread related to all things Apple silicon: Unity on Apple silicon and Big Sur: Known issues and workarounds

Thanks. Still having some trouble getting this to work properly since I can’t figure out how to access errno after the P/Invoke. If you happen to have any code for that or can point me in the right direction, that’d be awesome.

Will keep working on this and post here if I figure something out.

Are you using Mono or IL2CPP scripting backend?

I’m using Mono.

You have two options:

  1. Wrap sysctlbyname and errno interactions into a small dylib and P/Invoke into that.
  2. Just disregard errno. It doesn’t matter what it is: if sysctlbyname fails, just assume you’re not running on Rosetta/Apple silicon hardware. The fact that there’s an error doesn’t change the outcome of whether you want to load the plugins or not.

I would personally go with the second one.

1 Like

That’s true, hadn’t thought of that. That’ll work for me.

Thanks again!