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?
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.
Like this:
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.
My apologies.
Iāve now posted a sample project to that page which you can just run.
https://www.ohanaware.com/blog/202032/ARM_tester.zip
Oopsā¦ Iāve added it now.
Double Oopsā¦ Iāve also added that too.
If the processor is 64-Bit or not.