QtSupport: Re-use ToolchainManager::isBetterToolchain()

... and upstream the additional check.

Change-Id: I6443987174f39e0953238cf2e8e07b4b1d8129bd
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2024-08-08 15:47:24 +02:00
parent b6299edf1f
commit 7c28f17481
2 changed files with 24 additions and 20 deletions

View File

@@ -11,10 +11,11 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <utils/algorithm.h>
#include <utils/environment.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/persistentsettings.h> #include <utils/persistentsettings.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <nanotrace/nanotrace.h> #include <nanotrace/nanotrace.h>
@@ -362,6 +363,26 @@ bool ToolchainManager::isBetterToolchain(
if (b2IsCcache) if (b2IsCcache)
return false; return false;
// Hack to prefer a tool chain from PATH (e.g. autodetected) over other matches.
// This improves the situation a bit if a cross-compilation tool chain has the
// same ABI as the host.
if (!bundle1.get(&Toolchain::compilerCommand).needsDevice()) {
const FilePaths envPathVar = Environment::systemEnvironment().path();
const auto toolchainIsInPath = [&envPathVar](const ToolchainBundle &b) {
return Utils::contains(b.toolchains(), [&envPathVar](const Toolchain *tc) {
return envPathVar.contains(tc->compilerCommand().parentDir());
});
};
const bool tc1IsInPath = toolchainIsInPath(bundle1);
const bool tc2IsInPath = toolchainIsInPath(bundle2);
if (tc1IsInPath) {
if (!tc2IsInPath)
return true;
} else if (tc2IsInPath) {
return false;
}
}
return path1.size() < path2.size(); return path1.size() < path2.size();
} }

View File

@@ -247,10 +247,7 @@ void QtKitAspectFactory::fix(Kit *k)
return; return;
// Prefer exact matches. // Prefer exact matches.
// TODO: We should probably prefer the compiler with the highest version number instead, sort(bundles, [version](const ToolchainBundle &b1, const ToolchainBundle &b2) {
// but this information is currently not exposed by the Toolchain class.
const FilePaths envPathVar = Environment::systemEnvironment().path();
sort(bundles, [version, &envPathVar](const ToolchainBundle &b1, const ToolchainBundle &b2) {
const QVector<Abi> &qtAbis = version->qtAbis(); const QVector<Abi> &qtAbis = version->qtAbis();
const bool tc1ExactMatch = qtAbis.contains(b1.targetAbi()); const bool tc1ExactMatch = qtAbis.contains(b1.targetAbi());
const bool tc2ExactMatch = qtAbis.contains(b2.targetAbi()); const bool tc2ExactMatch = qtAbis.contains(b2.targetAbi());
@@ -270,21 +267,7 @@ void QtKitAspectFactory::fix(Kit *k)
return false; return false;
} }
const int prio1 = b1.get(&Toolchain::priority); return ToolchainManager::isBetterToolchain(b1, b2);
const int prio2 = b2.get(&Toolchain::priority);
if (prio1 > prio2)
return true;
if (prio1 < prio2)
return false;
// Hack to prefer a tool chain from PATH (e.g. autodetected) over other matches.
// This improves the situation a bit if a cross-compilation tool chain has the
// same ABI as the host.
const bool tc1IsInPath = envPathVar.contains(
b1.compilerCommand(CXX_LANGUAGE_ID).parentDir());
const bool tc2IsInPath = envPathVar.contains(
b2.compilerCommand(CXX_LANGUAGE_ID).parentDir());
return tc1IsInPath && !tc2IsInPath;
}); });
// TODO: Why is this not done during sorting? // TODO: Why is this not done during sorting?