Add workarounds for running under Rosetta on macOS

When Qt Creator is built as an Intel binary, and runs on
an Apple Silicon (ARM) Mac, it will be run via the Rosetta
translation layer. This means any process spawned by QtC,
including qmake, CMake, and lldb, will launch as x86_64
binaries as well.

For qmake and CMake, this affects their default choice of
build architecture, resulting in x86_64 builds of user
applications. We want to produce native arm64 apps, even
if Qt Creator itself isn't one, so we explicitly detect
the situation, and if Qt has an arm64 slice, we default
to arm64 builds.

The logic of adding CONFIG+=x86_64 to the qmake step has
been disabled, as the assumption that a single qmake run
and build will produce only a single architecture does no
longer hold. The corresponding logic in Qt was removed
in 2015 (qtbase f58e95f098c8d78a5f2db7729606126fe093cbdf).

In the case of lldb, running it as an x86_64 binary fails
to attach to the running application. We work around this
by using the 'arch' tool to explicitly launch it as an
arm64 binary. This works for debugging both arm64 and
x86_64 applications.

Change-Id: I65cc0f600223990f25c76cef18d927895e551260
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Tor Arne Vestbø
2021-06-22 00:11:58 +02:00
parent 9cd97e940b
commit a71d725e46
9 changed files with 140 additions and 63 deletions

View File

@@ -51,6 +51,8 @@
#include <qtsupport/qtversionmanager.h>
#include <qtsupport/qtsupportconstants.h>
#include <ios/iosconstants.h>
#include <utils/algorithm.h>
#include <utils/hostosinfo.h>
#include <utils/layoutbuilder.h>
@@ -609,6 +611,13 @@ void QMakeStep::separateDebugInfoChanged()
askForRebuild(tr("Separate Debug Information"));
}
static bool isIos(const Kit *k)
{
const Id deviceType = DeviceTypeKitAspect::deviceTypeId(k);
return deviceType == Ios::Constants::IOS_DEVICE_TYPE
|| deviceType == Ios::Constants::IOS_SIMULATOR_TYPE;
}
void QMakeStep::abisChanged()
{
m_selectedAbis.clear();
@@ -618,20 +627,42 @@ void QMakeStep::abisChanged()
m_selectedAbis << item->text();
}
if (isAndroidKit()) {
const QString prefix = "ANDROID_ABIS=";
QStringList args = m_extraArgs;
for (auto it = args.begin(); it != args.end(); ++it) {
if (it->startsWith(prefix)) {
args.erase(it);
break;
if (BaseQtVersion *qtVersion = QtKitAspect::qtVersion(target()->kit())) {
if (qtVersion->hasAbi(Abi::LinuxOS, Abi::AndroidLinuxFlavor)) {
const QString prefix = "ANDROID_ABIS=";
QStringList args = m_extraArgs;
for (auto it = args.begin(); it != args.end(); ++it) {
if (it->startsWith(prefix)) {
args.erase(it);
break;
}
}
}
if (!m_selectedAbis.isEmpty())
args << prefix + '"' + m_selectedAbis.join(' ') + '"';
setExtraArguments(args);
if (!m_selectedAbis.isEmpty())
args << prefix + '"' + m_selectedAbis.join(' ') + '"';
setExtraArguments(args);
buildSystem()->setProperty(Android::Constants::ANDROID_ABIS, m_selectedAbis);
buildSystem()->setProperty(Android::Constants::ANDROID_ABIS, m_selectedAbis);
} else if (qtVersion->hasAbi(Abi::DarwinOS) && !isIos(target()->kit())) {
const QString prefix = "QMAKE_APPLE_DEVICE_ARCHS=";
QStringList args = m_extraArgs;
for (auto it = args.begin(); it != args.end(); ++it) {
if (it->startsWith(prefix)) {
args.erase(it);
break;
}
}
QStringList archs;
for (const QString &selectedAbi : qAsConst(m_selectedAbis)) {
const auto abi = Abi::abiFromTargetTriplet(selectedAbi);
if (abi.architecture() == Abi::X86Architecture)
archs << "x86_64";
else if (abi.architecture() == Abi::ArmArchitecture)
archs << "arm64";
}
if (!archs.isEmpty())
args << prefix + '"' + archs.join(' ') + '"';
setExtraArguments(args);
}
}
updateAbiWidgets();
@@ -668,18 +699,6 @@ void QMakeStep::askForRebuild(const QString &title)
question->show();
}
bool QMakeStep::isAndroidKit() const
{
BaseQtVersion *qtVersion = QtKitAspect::qtVersion(target()->kit());
if (!qtVersion)
return false;
const Abis abis = qtVersion->qtAbis();
return Utils::anyOf(abis, [](const Abi &abi) {
return abi.osFlavor() == Abi::OSFlavor::AndroidLinuxFlavor;
});
}
void QMakeStep::updateAbiWidgets()
{
if (!abisLabel)
@@ -698,15 +717,23 @@ void QMakeStep::updateAbiWidgets()
abisListWidget->clear();
QStringList selectedAbis = m_selectedAbis;
if (selectedAbis.isEmpty() && isAndroidKit()) {
// Prefer ARM for Android, prefer 32bit.
for (const Abi &abi : abis) {
if (abi.param() == ProjectExplorer::Constants::ANDROID_ABI_ARMEABI_V7A)
selectedAbis.append(abi.param());
}
if (selectedAbis.isEmpty()) {
if (selectedAbis.isEmpty()) {
if (qtVersion->hasAbi(Abi::LinuxOS, Abi::AndroidLinuxFlavor)) {
// Prefer ARM for Android, prefer 32bit.
for (const Abi &abi : abis) {
if (abi.param() == ProjectExplorer::Constants::ANDROID_ABI_ARM64_V8A)
if (abi.param() == ProjectExplorer::Constants::ANDROID_ABI_ARMEABI_V7A)
selectedAbis.append(abi.param());
}
if (selectedAbis.isEmpty()) {
for (const Abi &abi : abis) {
if (abi.param() == ProjectExplorer::Constants::ANDROID_ABI_ARM64_V8A)
selectedAbis.append(abi.param());
}
}
} else if (qtVersion->hasAbi(Abi::DarwinOS) && !isIos(target()->kit()) && HostOsInfo::isRunningUnderRosetta()) {
// Automatically select arm64 when running under Rosetta
for (const Abi &abi : abis) {
if (abi.architecture() == Abi::ArmArchitecture)
selectedAbis.append(abi.param());
}
}
@@ -788,14 +815,6 @@ QMakeStepConfig::OsType QMakeStepConfig::osTypeFor(const Abi &targetAbi, const B
QStringList QMakeStepConfig::toArguments() const
{
QStringList arguments;
if (archConfig == X86)
arguments << "CONFIG+=x86";
else if (archConfig == X86_64)
arguments << "CONFIG+=x86_64";
else if (archConfig == PowerPC)
arguments << "CONFIG+=ppc";
else if (archConfig == PowerPC64)
arguments << "CONFIG+=ppc64";
// TODO: make that depend on the actual Qt version that is used
if (osType == IphoneSimulator)