forked from qt-creator/qt-creator
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:
@@ -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) + "}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user