Utils: Make FilePath::refersToExe(...) return the found item

... and use in on the CMake side.

Change-Id: Ib215ebc4f87beb67b6a302d0c42e7b955a2fa5b7
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
hjk
2022-11-28 11:43:18 +01:00
parent 9e0ed9eeac
commit a146403596
5 changed files with 26 additions and 20 deletions

View File

@@ -250,12 +250,14 @@ QByteArray DeviceFileAccess::fileId(const FilePath &filePath) const
return {};
}
bool DeviceFileAccess::refersToExecutableFile(
std::optional<FilePath> DeviceFileAccess::refersToExecutableFile(
const FilePath &filePath,
FilePath::MatchScope matchScope) const
{
Q_UNUSED(matchScope)
return isExecutableFile(filePath);
if (isExecutableFile(filePath))
return filePath;
return {};
}
void DeviceFileAccess::asyncFileContents(
@@ -301,38 +303,42 @@ bool DesktopDeviceFileAccess::isExecutableFile(const FilePath &filePath) const
return fi.isExecutable() && !fi.isDir();
}
static bool isWindowsExecutableHelper(const FilePath &filePath, const QStringView suffix)
static std::optional<FilePath> isWindowsExecutableHelper
(const FilePath &filePath, const QStringView suffix)
{
const QFileInfo fi(filePath.path().append(suffix));
return fi.isExecutable() && !fi.isDir();
if (!fi.isExecutable() || fi.isDir())
return {};
return filePath.withNewPath(FileUtils::normalizedPathName(fi.filePath()));
}
bool DesktopDeviceFileAccess::refersToExecutableFile(
std::optional<FilePath> DesktopDeviceFileAccess::refersToExecutableFile(
const FilePath &filePath,
FilePath::MatchScope matchScope) const
{
if (isExecutableFile(filePath))
return true;
return filePath;
if (HostOsInfo::isWindowsHost()) {
if (matchScope == FilePath::WithExeSuffix || matchScope == FilePath::WithExeOrBatSuffix) {
if (isWindowsExecutableHelper(filePath, u".exe"))
return true;
if (auto res = isWindowsExecutableHelper(filePath, u".exe"))
return res;
}
if (matchScope == FilePath::WithBatSuffix || matchScope == FilePath::WithExeOrBatSuffix) {
if (isWindowsExecutableHelper(filePath, u".bat"))
return true;
if (auto res = isWindowsExecutableHelper(filePath, u".bat"))
return res;
}
if (matchScope == FilePath::WithAnySuffix) {
// That's usually .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH,
static const QStringList exts = qtcEnvironmentVariable("PATHEXT").split(';');
static const QStringList exts = qtcEnvironmentVariable("PATHEXT").toLower().split(';');
for (const QString &ext : exts) {
if (isWindowsExecutableHelper(filePath, ext))
return true;
if (auto res = isWindowsExecutableHelper(filePath, ext))
return res;
}
}
}
return false;
return {};
}
bool DesktopDeviceFileAccess::isReadableFile(const FilePath &filePath) const

View File

@@ -47,7 +47,7 @@ protected:
virtual qint64 bytesAvailable(const FilePath &filePath) const;
virtual QByteArray fileId(const FilePath &filePath) const;
virtual bool refersToExecutableFile(
virtual std::optional<FilePath> refersToExecutableFile(
const FilePath &filePath,
FilePath::MatchScope matchScope) const;
@@ -118,7 +118,7 @@ protected:
qint64 bytesAvailable(const FilePath &filePath) const override;
QByteArray fileId(const FilePath &filePath) const override;
bool refersToExecutableFile(
std::optional<FilePath> refersToExecutableFile(
const FilePath &filePath,
FilePath::MatchScope matchScope) const override;

View File

@@ -419,7 +419,7 @@ bool FilePath::isExecutableFile() const
///
/// This is equivalent to \c isExecutableFile() in general.
/// On Windows, it will check appending various suffixes, too.
bool FilePath::refersToExecutableFile(MatchScope matchScope) const
std::optional<FilePath> FilePath::refersToExecutableFile(MatchScope matchScope) const
{
return fileAccess()->refersToExecutableFile(*this, matchScope);
}

View File

@@ -184,7 +184,7 @@ public:
enum MatchScope { ExactMatchOnly, WithExeSuffix, WithBatSuffix,
WithExeOrBatSuffix, WithAnySuffix };
bool refersToExecutableFile(MatchScope considerScript) const;
std::optional<FilePath> refersToExecutableFile(MatchScope considerScript) const;
// makes sure that capitalization of directories is canonical
// on Windows and macOS. This is rarely needed.

View File

@@ -70,8 +70,8 @@ static std::vector<std::unique_ptr<CMakeTool>> autoDetectCMakeTools()
if (base.isEmpty())
continue;
const FilePath suspect = base / "cmake";
if (suspect.refersToExecutableFile(FilePath::WithAnySuffix))
suspects << suspect;
if (std::optional<FilePath> foundExe = suspect.refersToExecutableFile(FilePath::WithAnySuffix))
suspects << *foundExe;
}
std::vector<std::unique_ptr<CMakeTool>> found;