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

@@ -35,6 +35,10 @@
#include <qt_windows.h>
#endif
#ifdef Q_OS_MACOS
#include <sys/sysctl.h>
#endif
using namespace Utils;
Qt::CaseSensitivity HostOsInfo::m_overrideFileNameCaseSensitivity = Qt::CaseSensitive;
@@ -70,6 +74,17 @@ HostOsInfo::HostArchitecture HostOsInfo::hostArchitecture()
#endif
}
bool HostOsInfo::isRunningUnderRosetta()
{
#ifdef Q_OS_MACOS
int translated = 0;
auto size = sizeof(translated);
if (sysctlbyname("sysctl.proc_translated", &translated, &size, nullptr, 0) == 0)
return translated;
#endif
return false;
}
void HostOsInfo::setOverrideFileNameCaseSensitivity(Qt::CaseSensitivity sensitivity)
{
m_useOverrideFileNameCaseSensitivity = true;

View File

@@ -73,6 +73,8 @@ public:
#endif
}
static bool isRunningUnderRosetta();
static QString withExecutableSuffix(const QString &executable)
{
return OsSpecificAspects::withExecutableSuffix(hostOs(), executable);

View File

@@ -98,6 +98,7 @@ static Q_LOGGING_CATEGORY(cmakeBuildConfigurationLog, "qtc.cmake.bc", QtWarningM
const char CONFIGURATION_KEY[] = "CMake.Configuration";
const char DEVELOPMENT_TEAM_FLAG[] = "Ios:DevelopmentTeam:Flag";
const char PROVISIONING_PROFILE_FLAG[] = "Ios:ProvisioningProfile:Flag";
const char CMAKE_OSX_ARCHITECTURES_FLAG[] = "CMAKE_OSX_ARCHITECTURES:DefaultFlag";
const char CMAKE_QT6_TOOLCHAIN_FILE_ARG[] =
"-DCMAKE_TOOLCHAIN_FILE:PATH=%{Qt:QT_INSTALL_PREFIX}/lib/cmake/Qt6/qt.toolchain.cmake";
@@ -890,6 +891,21 @@ CMakeBuildConfiguration::CMakeBuildConfiguration(Target *target, Id id)
return QString();
});
macroExpander()->registerVariable(CMAKE_OSX_ARCHITECTURES_FLAG,
tr("The CMake flag for the architecture on macOS"),
[target] {
if (HostOsInfo::isRunningUnderRosetta()) {
if (auto *qt = QtSupport::QtKitAspect::qtVersion(target->kit())) {
const Abis abis = qt->qtAbis();
for (const Abi &abi : abis) {
if (abi.architecture() == Abi::ArmArchitecture)
return QLatin1String("-DCMAKE_OSX_ARCHITECTURES=arm64");
}
}
}
return QLatin1String();
});
addAspect<SourceDirectoryAspect>();
addAspect<BuildTypeAspect>();
@@ -939,27 +955,33 @@ CMakeBuildConfiguration::CMakeBuildConfiguration(Target *target, Id id)
}
}
if (isIos(k)) {
QtSupport::BaseQtVersion *qt = QtSupport::QtKitAspect::qtVersion(k);
if (qt && qt->qtVersion().majorVersion >= 6) {
// TODO it would be better if we could set
// CMAKE_SYSTEM_NAME=iOS and CMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=YES
// and build with "cmake --build . -- -arch <arch>" instead of setting the architecture
// and sysroot in the CMake configuration, but that currently doesn't work with Qt/CMake
// https://gitlab.kitware.com/cmake/cmake/-/issues/21276
const Id deviceType = DeviceTypeKitAspect::deviceTypeId(k);
// TODO the architectures are probably not correct with Apple Silicon in the mix...
const QString architecture = deviceType == Ios::Constants::IOS_DEVICE_TYPE
? QLatin1String("arm64")
: QLatin1String("x86_64");
const QString sysroot = deviceType == Ios::Constants::IOS_DEVICE_TYPE
? QLatin1String("iphoneos")
: QLatin1String("iphonesimulator");
initialArgs.append(CMAKE_QT6_TOOLCHAIN_FILE_ARG);
initialArgs.append("-DCMAKE_OSX_ARCHITECTURES:STRING=" + architecture);
initialArgs.append("-DCMAKE_OSX_SYSROOT:STRING=" + sysroot);
initialArgs.append("%{" + QLatin1String(DEVELOPMENT_TEAM_FLAG) + "}");
initialArgs.append("%{" + QLatin1String(PROVISIONING_PROFILE_FLAG) + "}");
const IDevice::ConstPtr device = DeviceKitAspect::device(k);
if (device->osType() == Utils::OsTypeMac) {
if (isIos(k)) {
QtSupport::BaseQtVersion *qt = QtSupport::QtKitAspect::qtVersion(k);
if (qt && qt->qtVersion().majorVersion >= 6) {
// TODO it would be better if we could set
// CMAKE_SYSTEM_NAME=iOS and CMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=YES
// and build with "cmake --build . -- -arch <arch>" instead of setting the architecture
// and sysroot in the CMake configuration, but that currently doesn't work with Qt/CMake
// https://gitlab.kitware.com/cmake/cmake/-/issues/21276
const Id deviceType = DeviceTypeKitAspect::deviceTypeId(k);
// TODO the architectures are probably not correct with Apple Silicon in the mix...
const QString architecture = deviceType == Ios::Constants::IOS_DEVICE_TYPE
? QLatin1String("arm64")
: QLatin1String("x86_64");
const QString sysroot = deviceType == Ios::Constants::IOS_DEVICE_TYPE
? QLatin1String("iphoneos")
: QLatin1String("iphonesimulator");
initialArgs.append(CMAKE_QT6_TOOLCHAIN_FILE_ARG);
initialArgs.append("-DCMAKE_OSX_ARCHITECTURES:STRING=" + architecture);
initialArgs.append("-DCMAKE_OSX_SYSROOT:STRING=" + sysroot);
initialArgs.append("%{" + QLatin1String(DEVELOPMENT_TEAM_FLAG) + "}");
initialArgs.append("%{" + QLatin1String(PROVISIONING_PROFILE_FLAG) + "}");
}
} else {
// macOS
initialArgs.append("%{" + QLatin1String(CMAKE_OSX_ARCHITECTURES_FLAG) + "}");
}
}

View File

@@ -43,6 +43,7 @@
#include <projectexplorer/projectexplorersettings.h>
#include <projectexplorer/task.h>
#include <projectexplorer/toolchain.h>
#include <projectexplorer/devicesupport/idevice.h>
#include <qtsupport/baseqtversion.h>
#include <qtsupport/qtkitinformation.h>

View File

@@ -216,7 +216,11 @@ void LldbEngine::setupEngine()
if (QFileInfo(runParameters().debugger.workingDirectory).isDir())
m_lldbProc.setWorkingDirectory(runParameters().debugger.workingDirectory);
m_lldbProc.setCommand(CommandLine(lldbCmd));
if (HostOsInfo::isRunningUnderRosetta())
m_lldbProc.setCommand(CommandLine("/usr/bin/arch", {"-arm64", lldbCmd.toString()}));
else
m_lldbProc.setCommand(CommandLine(lldbCmd));
m_lldbProc.start();
if (!m_lldbProc.waitForStarted()) {

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)

View File

@@ -185,7 +185,6 @@ private:
void updateAbiWidgets();
void updateEffectiveQMakeCall();
bool isAndroidKit() const;
Utils::CommandLine m_qmakeCommand;
Utils::CommandLine m_makeCommand;

View File

@@ -840,6 +840,20 @@ Abis BaseQtVersion::detectQtAbis() const
return qtAbisFromLibrary(d->qtCorePaths());
}
bool BaseQtVersion::hasAbi(ProjectExplorer::Abi::OS os, ProjectExplorer::Abi::OSFlavor flavor) const
{
const Abis abis = qtAbis();
return Utils::anyOf(abis, [&](const Abi &abi) {
if (abi.os() != os)
return false;
if (flavor == Abi::UnknownFlavor)
return true;
return abi.osFlavor() == flavor;
});
}
bool BaseQtVersion::equals(BaseQtVersion *other)
{
if (d->m_qmakeCommand != other->d->m_qmakeCommand)

View File

@@ -119,6 +119,7 @@ public:
virtual QString toHtml(bool verbose) const;
ProjectExplorer::Abis qtAbis() const;
bool hasAbi(ProjectExplorer::Abi::OS, ProjectExplorer::Abi::OSFlavor flavor = ProjectExplorer::Abi::UnknownFlavor) const;
void applyProperties(QMakeGlobals *qmakeGlobals) const;
virtual void addToEnvironment(const ProjectExplorer::Kit *k, Utils::Environment &env) const;