Bri Bri
Bri Bri

Reputation: 2220

How to get the true macOS version in a x86_64 app built with an outdated macOS SDK running on an Apple Silicon mac?

I'm working on an app that, due to restrictions that are beyond my control, is being built as an x86_64 app using an outdated macOS SDK (in this case the macOS 10.15 SDK).

It runs fine on an Apple Silicon mac using Rosetta 2, but the app needs to get the current version of macOS, and the available APIs lie to it under these conditions, consistently reporting the version is 10.16 when really it should be macOS 11.x or 12.x.

In other circumstances where the system API lies, I've been able to get the real info using sysctl, but in this case calling sysctlbyname("kern.osproductversion", ...) still lies to my app and reports 10.16.

How can I get the true version of macOS under these circumstances?

Upvotes: 0

Views: 88

Answers (1)

Bri Bri
Bri Bri

Reputation: 2220

The only solution I've found so far is to launch the sysctl command line utility as a separate process and use it to query the system version. Here's a C++ implementation using Qt:

QProcess *p = new QProcess;
p->setProgram("/usr/sbin/sysctl");
p->setArguments({"-b", "kern.osproductversion"});
p->start();

if (!p->waitForFinished(2000)) {
    // handle unlikely error here
}

QByteArray systemVersion = p->readAllStandardOutput();

On the bright side, this only ever needs to be executed once as the system version presumably won't change over the course of a process's life. Nonetheless I'll leave this question open in case someone has a better solution.

Upvotes: 1

Related Questions