Any way to detect Rosetta2 availability?

I found a thread here to detect if an app is running under Rosetta2 on an M1 Mac. But is there a way to find if translation is available?

Check the presence of the file /Library/Apple/usr/share/rosetta/rosetta

Please confirm this info.

1 Like

To what end though? If Rosetta is not on the machine and you try to run an Intel app, it’ll either fail or ask the user to install Rosetta and then run the app.

5 Likes

Thank you! Looks good!

1 Like

Easy to explain. I maintain an xplatform desktop app that uses a lot of console helpers addressing measurement instruments using different APIs, some not yet UB. I don‘t want users to run into errors but inform them before.

1 Like

In which case, you check for the error code that’s returned. It will definitely not be zero if the app can’t run.

So why not include your own helper app that NEEDS it installed and quickly returns a status code either way – then call that when your program launches? Would give you a quick and easy way to test at app launch.

I saw on !NN under “M1 and Rosetta” postand might provide an alternative way to detect everything about the CPU and Rosetta on you Intel/M1 Mac.

It is SWIFT code, but should be able to be translated to an Xojo Declare

Thank you, Richard!
How good TOF exists. Otherwise those Pros banned for lifetime couldn’t share their knowledge anymore.

So here the IMHO cleaner solution, translated from Swift. Maybe someone could check it; I do not own an M1 Mac

Public Function isRosettaInstalled() As Boolean
  #If TargetMacOS
    #If TargetARM
      Declare Function sysctlbyname Lib "/usr/lib/libSystem.dylib" (name As CString, ByRef len As Int32, ByRef Size As Integer, newptr As Ptr, newlen As Integer) As Int32
      var len As Int32
      var Size As Integer = 4
      var res As Int32 = sysctlbyname("sysctl.proc_translated", len, Size, Nil, 0)
      If res = -1 Then
        Return False
      Else 
        Return len = -1 
      End If
    #Else
      Return False
    #EndIf
  #EndIf
End Function

Hm…
about-the-rosetta-translation-environment states:

On Apple silicon, a universal binary may run either natively or as a translated binary. The system runs the native version whenever possible, but the user might opt to run the code using Rosetta to support older plug-ins.

So this does not check for an installed Rosetta but if the app does currently run with Intel code on an ARM Mac.
From your question I guess you want to know if Rosetta is installed or not…

How about looking for the Rosetta process?
how-to-tell-if-m1-mac-has-rosetta-installed states:

Internally rosetta is known as OAH. If it returns a process id you know rosetta is installed.

So calling

/usr/bin/pgrep oahd

does either return the PID when Rosetta is installed or nothing.

Yes, thank you. I had not seen the documentation.
So, new code:

#If TargetMacOS
  #If TargetARM
    var sh As New shell
    sh.Execute("Echo $(/usr/bin/pgrep -q oahd && echo True || echo False)")
    Return Boolean.FromString(sh.Result)
  #EndIf
#EndIf
Exception
Return false

Why the additional “echo”? You can use

#If TargetMacOS
  #If TargetARM
    Var sh As New shell
    sh.Execute("/usr/bin/pgrep -q oahd && echo True || echo False")
    Return Boolean.FromString(sh.Result)
  #EndIf
#EndIf
Exception
  Return False

Oh sure. That came from my tests and was not removed. Thanks!