Android: Detect available Ndk platforms also for recent Ndk versions

In order to detect the list of platforms that an Ndk installation
supports, AndroidConfig::availableNdkPlatforms iterates through the
directories of the Ndk.

The directory structure of the Ndk changed in the recent versions. So
that the detection that works with Ndk 19 does not work with Ndk 23.
Also, the new directory structure is split up by Android ABI. And the
lists of supported platforms differ between ABI.

This change adds detection for the new structure, in case that the
old implementation fails to return a list. It also adds an autotest
that covers the old and new detection of supported platforms.

Fixes: QTCREATORBUG-26772
Change-Id: I6e584963f51feca0bf90c7ed3a9fdb03cb5d39e6
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
Alessandro Portale
2022-01-25 09:28:43 +01:00
parent d087fe424d
commit 49443d689e
6 changed files with 190 additions and 6 deletions

View File

@@ -77,6 +77,11 @@
#include <functional>
#include <memory>
#ifdef WITH_TESTS
# include <QTest>
# include "androidplugin.h"
#endif // WITH_TESTS
using namespace QtSupport;
using namespace ProjectExplorer;
using namespace Utils;
@@ -368,11 +373,11 @@ void AndroidConfig::parseDependenciesJson()
}
}
QVector<int> AndroidConfig::availableNdkPlatforms(const QtVersion *qtVersion) const
static QVector<int> availableNdkPlatformsLegacy(const FilePath &ndkLocation)
{
QVector<int> availableNdkPlatforms;
ndkLocation(qtVersion)
ndkLocation
.pathAppended("platforms")
.iterateDirectory(
[&availableNdkPlatforms](const FilePath &filePath) {
@@ -384,10 +389,43 @@ QVector<int> AndroidConfig::availableNdkPlatforms(const QtVersion *qtVersion) co
},
{{"android-*"}, QDir::Dirs});
Utils::sort(availableNdkPlatforms, std::greater<>());
return availableNdkPlatforms;
}
static QVector<int> availableNdkPlatformsV21Plus(const FilePath &ndkLocation, const Abis &abis,
OsType hostOs)
{
if (abis.isEmpty())
return {};
const QString abi = AndroidConfig::toolsPrefix(abis.first());
const FilePath libPath =
AndroidConfig::toolchainPathFromNdk(ndkLocation, hostOs) / "sysroot/usr/lib" / abi;
const QList<FilePath> dirEntries = libPath.dirEntries(QDir::Dirs | QDir::NoDotAndDotDot);
const QVector<int> availableNdkPlatforms =
Utils::transform(dirEntries, [](const FilePath &path) {
return path.fileName().toInt(); });
return availableNdkPlatforms;
}
static QVector<int> availableNdkPlatformsImpl(const FilePath &ndkLocation, const Abis &abis,
OsType hostOs)
{
QVector<int> result = availableNdkPlatformsLegacy(ndkLocation);
if (result.isEmpty())
result = availableNdkPlatformsV21Plus(ndkLocation, abis, hostOs);
Utils::sort(result, std::greater<>());
return result;
}
QVector<int> AndroidConfig::availableNdkPlatforms(const QtVersion *qtVersion) const
{
return availableNdkPlatformsImpl(ndkLocation(qtVersion), qtVersion->qtAbis(),
HostOsInfo::hostOs());
}
QStringList AndroidConfig::getCustomNdkList() const
{
return m_customNdkList;
@@ -513,7 +551,7 @@ FilePath AndroidConfig::avdManagerToolPath() const
return FilePath();
}
FilePath AndroidConfig::toolchainPathFromNdk(const FilePath &ndkLocation)
FilePath AndroidConfig::toolchainPathFromNdk(const FilePath &ndkLocation, OsType hostOs)
{
const FilePath tcPath = ndkLocation / "toolchains/";
FilePath toolchainPath;
@@ -525,7 +563,7 @@ FilePath AndroidConfig::toolchainPathFromNdk(const FilePath &ndkLocation)
// detect toolchain host
QStringList hostPatterns;
switch (HostOsInfo::hostOs()) {
switch (hostOs) {
case OsTypeLinux:
hostPatterns << QLatin1String("linux*");
break;
@@ -1641,4 +1679,62 @@ void AndroidConfigurations::updateAndroidDevice()
AndroidConfigurations *AndroidConfigurations::m_instance = nullptr;
#ifdef WITH_TESTS
void AndroidPlugin::testAndroidConfigAvailableNdkPlatforms_data()
{
QTest::addColumn<FilePath>("ndkPath");
QTest::addColumn<Abis>("abis");
QTest::addColumn<OsType>("hostOs");
QTest::addColumn<QVector<int> >("expectedPlatforms");
QTest::newRow("ndkLegacy")
<< FilePath::fromUserInput(":/android/tst/ndk/19.2.5345600")
<< Abis()
<< OsTypeOther
<< QVector<int>{28, 27, 26, 24, 23, 22, 21, 19, 18, 17, 16};
const FilePath ndkV21Plus = FilePath::fromUserInput(":/android/tst/ndk/23.1.7779620");
const QVector<int> abis32Bit = {31, 30, 29, 28, 27, 26, 24, 23, 22, 21, 19, 18, 17, 16};
const QVector<int> abis64Bit = {31, 30, 29, 28, 27, 26, 24, 23, 22, 21};
QTest::newRow("ndkV21Plus armeabi-v7a OsTypeWindows")
<< ndkV21Plus
<< Abis{AndroidManager::androidAbi2Abi(
ProjectExplorer::Constants::ANDROID_ABI_ARMEABI_V7A)}
<< OsTypeWindows
<< abis32Bit;
QTest::newRow("ndkV21Plus arm64-v8a OsTypeLinux")
<< ndkV21Plus
<< Abis{AndroidManager::androidAbi2Abi(
ProjectExplorer::Constants::ANDROID_ABI_ARM64_V8A)}
<< OsTypeLinux
<< abis64Bit;
QTest::newRow("ndkV21Plus x86 OsTypeMac")
<< ndkV21Plus
<< Abis{AndroidManager::androidAbi2Abi(
ProjectExplorer::Constants::ANDROID_ABI_X86)}
<< OsTypeMac
<< abis32Bit;
QTest::newRow("ndkV21Plus x86_64 OsTypeWindows")
<< ndkV21Plus
<< Abis{AndroidManager::androidAbi2Abi(
ProjectExplorer::Constants::ANDROID_ABI_X86_64)}
<< OsTypeWindows
<< abis64Bit;
}
void AndroidPlugin::testAndroidConfigAvailableNdkPlatforms()
{
QFETCH(FilePath, ndkPath);
QFETCH(Abis, abis);
QFETCH(OsType, hostOs);
QFETCH(QVector<int>, expectedPlatforms);
const QVector<int> foundPlatforms = availableNdkPlatformsImpl(ndkPath, abis, hostOs);
QCOMPARE(foundPlatforms, expectedPlatforms);
}
#endif // WITH_TESTS
} // namespace Android