2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2013-01-15 13:58:06 +01:00
|
|
|
|
|
|
|
|
#include "hostosinfo.h"
|
|
|
|
|
|
2017-08-18 14:32:39 +02:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
|
|
|
|
|
#if !defined(QT_NO_OPENGL) && defined(QT_GUI_LIB)
|
2015-09-29 16:43:42 +02:00
|
|
|
#include <QOpenGLContext>
|
2017-08-18 14:32:39 +02:00
|
|
|
#endif
|
2015-09-29 16:43:42 +02:00
|
|
|
|
2022-06-17 11:58:14 +02:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
#include <sys/sysinfo.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-01-15 13:58:06 +01:00
|
|
|
#ifdef Q_OS_WIN
|
2013-08-12 15:09:57 +02:00
|
|
|
#include <qt_windows.h>
|
2013-01-17 11:56:39 +01:00
|
|
|
#endif
|
2013-01-15 13:58:06 +01:00
|
|
|
|
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>
2021-06-22 00:11:58 +02:00
|
|
|
#ifdef Q_OS_MACOS
|
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-01-15 13:58:06 +01:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2015-08-12 16:01:19 +02:00
|
|
|
Qt::CaseSensitivity HostOsInfo::m_overrideFileNameCaseSensitivity = Qt::CaseSensitive;
|
|
|
|
|
bool HostOsInfo::m_useOverrideFileNameCaseSensitivity = false;
|
|
|
|
|
|
2013-01-17 11:56:39 +01:00
|
|
|
#ifdef Q_OS_WIN
|
2015-07-22 23:32:12 +03:00
|
|
|
static WORD hostProcessorArchitecture()
|
|
|
|
|
{
|
2013-01-15 13:58:06 +01:00
|
|
|
SYSTEM_INFO info;
|
|
|
|
|
GetNativeSystemInfo(&info);
|
2015-07-22 23:32:12 +03:00
|
|
|
return info.wProcessorArchitecture;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
HostOsInfo::HostArchitecture HostOsInfo::hostArchitecture()
|
|
|
|
|
{
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
static const WORD processorArchitecture = hostProcessorArchitecture();
|
|
|
|
|
switch (processorArchitecture) {
|
2013-01-15 13:58:06 +01:00
|
|
|
case PROCESSOR_ARCHITECTURE_AMD64:
|
|
|
|
|
return HostOsInfo::HostArchitectureAMD64;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_INTEL:
|
|
|
|
|
return HostOsInfo::HostArchitectureX86;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_IA64:
|
|
|
|
|
return HostOsInfo::HostArchitectureItanium;
|
|
|
|
|
case PROCESSOR_ARCHITECTURE_ARM:
|
|
|
|
|
return HostOsInfo::HostArchitectureArm;
|
2022-06-20 13:18:28 +02:00
|
|
|
case PROCESSOR_ARCHITECTURE_ARM64:
|
|
|
|
|
return HostOsInfo::HostArchitectureArm64;
|
2013-01-15 13:58:06 +01:00
|
|
|
default:
|
2013-01-17 13:41:12 +01:00
|
|
|
return HostOsInfo::HostArchitectureUnknown;
|
2013-01-15 13:58:06 +01:00
|
|
|
}
|
2013-01-17 11:56:39 +01:00
|
|
|
#else
|
|
|
|
|
return HostOsInfo::HostArchitectureUnknown;
|
2013-01-15 13:58:06 +01:00
|
|
|
#endif
|
2013-01-17 11:56:39 +01:00
|
|
|
}
|
2015-08-12 16:01:19 +02:00
|
|
|
|
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>
2021-06-22 00:11:58 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-12 16:01:19 +02:00
|
|
|
void HostOsInfo::setOverrideFileNameCaseSensitivity(Qt::CaseSensitivity sensitivity)
|
|
|
|
|
{
|
|
|
|
|
m_useOverrideFileNameCaseSensitivity = true;
|
|
|
|
|
m_overrideFileNameCaseSensitivity = sensitivity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HostOsInfo::unsetOverrideFileNameCaseSensitivity()
|
|
|
|
|
{
|
|
|
|
|
m_useOverrideFileNameCaseSensitivity = false;
|
|
|
|
|
}
|
2015-09-29 16:43:42 +02:00
|
|
|
|
|
|
|
|
bool HostOsInfo::canCreateOpenGLContext(QString *errorMessage)
|
|
|
|
|
{
|
2017-08-18 14:32:39 +02:00
|
|
|
#if defined(QT_NO_OPENGL) || !defined(QT_GUI_LIB)
|
2017-02-10 21:50:37 +01:00
|
|
|
Q_UNUSED(errorMessage)
|
|
|
|
|
return false;
|
|
|
|
|
#else
|
2015-09-29 16:43:42 +02:00
|
|
|
static const bool canCreate = QOpenGLContext().create();
|
|
|
|
|
if (!canCreate)
|
2017-08-18 14:32:39 +02:00
|
|
|
*errorMessage = QCoreApplication::translate("Utils::HostOsInfo",
|
|
|
|
|
"Cannot create OpenGL context.");
|
2015-09-29 16:43:42 +02:00
|
|
|
return canCreate;
|
2017-02-10 21:50:37 +01:00
|
|
|
#endif
|
2015-09-29 16:43:42 +02:00
|
|
|
}
|
2022-06-17 11:58:14 +02:00
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
std::optional<quint64> HostOsInfo::totalMemoryInstalledInBytes()
|
2022-06-17 11:58:14 +02:00
|
|
|
{
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
struct sysinfo info;
|
|
|
|
|
if (sysinfo(&info) == -1)
|
|
|
|
|
return {};
|
|
|
|
|
return info.totalram;
|
|
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
|
MEMORYSTATUSEX statex;
|
|
|
|
|
statex.dwLength = sizeof statex;
|
|
|
|
|
if (!GlobalMemoryStatusEx(&statex))
|
|
|
|
|
return {};
|
|
|
|
|
return statex.ullTotalPhys;
|
|
|
|
|
#elif defined(Q_OS_MACOS)
|
|
|
|
|
int mib[] = {CTL_HW, HW_MEMSIZE};
|
|
|
|
|
int64_t ram;
|
|
|
|
|
size_t length = sizeof(int64_t);
|
|
|
|
|
if (sysctl(mib, 2, &ram, &length, nullptr, 0) == -1)
|
|
|
|
|
return {};
|
|
|
|
|
return ram;
|
|
|
|
|
#endif
|
|
|
|
|
return {};
|
|
|
|
|
}
|