I need to write some if/then logic that requires knowing which CPU is being used, Intel or Apple Silicon. Is there a method or property, either native Xojo or MBS, that will tell me this?
See IsTranslated function in SystemInformationMBS class to detect emulation.
You could wrap some code in
#if TargetARM #EndIf
That is assuming you are building as Universal.
Thank you both. Yes, it’s a Universal build. It seems that the isTranslated method has the advantage that if the user has forced the app to run in Rosetta 2 it will be seen as running in Intel (note, that’s my interpretation of the docs, I haven’t tested it yet).
#if is for deciding at compile time, what goes into Intel vs. ARM app.
IsTranslated lets you decide at runtime to whether your Intel app runs in emulation or natively.
Two different things.
I created this method based on a Shell. It works whether the app itself is translated or not.
Function IsAppleARM() As Boolean #if not TargetMacOS then return false #else dim sh as new Shell sh.Execute "/usr/sbin/sysctl -a | grep 'brand_string'" if sh.ErrorCode <> 0 then dim err as new RuntimeException err.Message = "Could not run sysctl" err.ErrorNumber = sh.ErrorCode raise err end if dim result as string = sh.Result.DefineEncoding( Encodings.UTF8 ) return result.InStr( "Apple" ) <> 0 #endif End Function
(Note: This intentionally uses API 1 calls as it is in freeware meant to work in older versions too.)
Edit: Removed “M1” from the search string based on Christian’s suggestion below.
You’d better not check for a particular text string there.
Next processor will be named differently.
Good point. Looking for “Apple” will be sufficient.
I wrote an article on this back in August 2020.
The docs for IsARM need to be updated:
If we someday produce ARM plugins for Mac, this may also return true.
Stupid question: should IsARM be true on my MacBookAir M1? Using Xojo 2020r2 and MBS from November 2020.
@Sam Rowlands: The code for Gestalt is missing something.
Thomas code is missing the declaration of sizeInfo. What does CPU_ARCH_ABI64 tell me?
Onwards to Kem.
I’ll update it.
For Apple Silicon, it returns true.
I just tested and isARM doesn’t return true on AS. That’s why I had to fiddle around with the other methods.
IsARM doesn’t return true if you run in an Intel app.
Then it’s not the droid I’m looking for because I needed to check if the computer was ARM or not. Anyways, I’ve got a solution.
If your app runs in Rosetta, it will return true for isTranslated.
If TargetMacOS Then If SystemInformationMBS.isARM Then MsgBox "Running ARM version on ARM CPU." Elseif SystemInformationMBS.IsTranslated Then MsgBox "Running Intel version on ARM CPU." Else MsgBox "Running Intel version on Intel CPU." End If End If
<-- Suddenly recalls the feeling of sitting outside the principal’s office…
Well, your code worked fine.
I’ve now posted a sample project to that page which you can just run.
Oops… I’ve added it now.
Double Oops… I’ve also added that too.
If the processor is 64-Bit or not.