CMake: Do not crash without a supported CMake

Do not crash when a CMake < 3.7 (no server-mode and no file-api) is
used.

Change-Id: I72a4ce6bb81d4fcf3d59508c72e46f422a8a00c0
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Tobias Hunger
2020-04-22 11:18:01 +02:00
parent 7d3c6f7c02
commit f51f3c897e
4 changed files with 30 additions and 23 deletions

View File

@@ -143,6 +143,13 @@ void BuildDirManager::updateReaderType(const BuildDirParameters &p,
} }
m_reader = BuildDirReader::createReader(p); m_reader = BuildDirReader::createReader(p);
if (!m_reader) {
TaskHub::addTask(BuildSystemTask(
Task::Error,
tr("The kit does not define a valid CMake tool to parse this project with.")));
return;
}
connect(m_reader.get(), connect(m_reader.get(),
&BuildDirReader::configurationStarted, &BuildDirReader::configurationStarted,
this, this,

View File

@@ -44,13 +44,14 @@ std::unique_ptr<BuildDirReader> BuildDirReader::createReader(const BuildDirParam
CMakeTool *cmake = p.cmakeTool(); CMakeTool *cmake = p.cmakeTool();
QTC_ASSERT(p.isValid() && cmake, return {}); QTC_ASSERT(p.isValid() && cmake, return {});
switch (cmake->readerType()) { auto type = cmake->readerType();
if (!type)
return {};
switch (type.value()) {
case CMakeTool::FileApi: case CMakeTool::FileApi:
return std::make_unique<FileApiReader>(); return std::make_unique<FileApiReader>();
case CMakeTool::ServerMode: case CMakeTool::ServerMode:
return std::make_unique<ServerModeReader>(); return std::make_unique<ServerModeReader>();
default:
return {};
} }
} }

View File

@@ -69,10 +69,11 @@ static bool ignoreFileApi()
static Utils::optional<CMakeTool::ReaderType> readerTypeFromString(const QString &input) static Utils::optional<CMakeTool::ReaderType> readerTypeFromString(const QString &input)
{ {
// Do not try to be clever here, just use whatever is in the string!
if (input == READER_TYPE_SERVERMODE) if (input == READER_TYPE_SERVERMODE)
return CMakeTool::ServerMode; return CMakeTool::ServerMode;
if (input == READER_TYPE_FILEAPI) if (input == READER_TYPE_FILEAPI)
return ignoreFileApi() ? CMakeTool::ServerMode : CMakeTool::FileApi; return CMakeTool::FileApi;
return {}; return {};
} }
@@ -84,7 +85,6 @@ static QString readerTypeToString(const CMakeTool::ReaderType &type)
case CMakeTool::FileApi: case CMakeTool::FileApi:
return QString(READER_TYPE_FILEAPI); return QString(READER_TYPE_FILEAPI);
} }
return QString("<INVALID>");
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
@@ -120,9 +120,10 @@ public:
/////////////////////////// ///////////////////////////
// CMakeTool // CMakeTool
/////////////////////////// ///////////////////////////
CMakeTool::CMakeTool(Detection d, const Core::Id &id) : CMakeTool::CMakeTool(Detection d, const Core::Id &id)
m_id(id), m_isAutoDetected(d == AutoDetection), : m_id(id)
m_introspection(std::make_unique<Internal::IntrospectionData>()) , m_isAutoDetected(d == AutoDetection)
, m_introspection(std::make_unique<Internal::IntrospectionData>())
{ {
QTC_ASSERT(m_id.isValid(), m_id = Core::Id::fromString(QUuid::createUuid().toString())); QTC_ASSERT(m_id.isValid(), m_id = Core::Id::fromString(QUuid::createUuid().toString()));
} }
@@ -198,7 +199,7 @@ bool CMakeTool::isValid() const
if (!m_introspection->m_didAttemptToRun) if (!m_introspection->m_didAttemptToRun)
supportedGenerators(); supportedGenerators();
return m_introspection->m_didRun && (hasFileApi() || hasServerMode()); return m_introspection->m_didRun && readerType().has_value();
} }
Utils::SynchronousProcessResponse CMakeTool::run(const QStringList &args, int timeoutS) const Utils::SynchronousProcessResponse CMakeTool::run(const QStringList &args, int timeoutS) const
@@ -223,7 +224,7 @@ QVariantMap CMakeTool::toMap() const
data.insert(CMAKE_INFORMATION_QCH_FILE_PATH, m_qchFilePath.toString()); data.insert(CMAKE_INFORMATION_QCH_FILE_PATH, m_qchFilePath.toString());
data.insert(CMAKE_INFORMATION_AUTORUN, m_isAutoRun); data.insert(CMAKE_INFORMATION_AUTORUN, m_isAutoRun);
data.insert(CMAKE_INFORMATION_AUTO_CREATE_BUILD_DIRECTORY, m_autoCreateBuildDirectory); data.insert(CMAKE_INFORMATION_AUTO_CREATE_BUILD_DIRECTORY, m_autoCreateBuildDirectory);
if (m_readerType.has_value()) if (m_readerType)
data.insert(CMAKE_INFORMATION_READERTYPE, data.insert(CMAKE_INFORMATION_READERTYPE,
Internal::readerTypeToString(m_readerType.value())); Internal::readerTypeToString(m_readerType.value()));
data.insert(CMAKE_INFORMATION_AUTODETECTED, m_isAutoDetected); data.insert(CMAKE_INFORMATION_AUTODETECTED, m_isAutoDetected);
@@ -368,19 +369,17 @@ CMakeTool::PathMapper CMakeTool::pathMapper() const
return [](const Utils::FilePath &fn) { return fn; }; return [](const Utils::FilePath &fn) { return fn; };
} }
CMakeTool::ReaderType CMakeTool::readerType() const Utils::optional<CMakeTool::ReaderType> CMakeTool::readerType() const
{ {
if (!m_readerType.has_value()) { if (m_readerType)
// Find best possible reader type: return m_readerType; // Allow overriding the auto-detected value via .user files
if (hasFileApi()) {
if (hasServerMode() && Internal::ignoreFileApi()) // Find best possible reader type:
return ServerMode; // We were asked to fall back to server mode if (hasFileApi() && !Internal::ignoreFileApi())
return FileApi; return FileApi;
} if (hasServerMode())
if (hasServerMode()) return ServerMode;
return ServerMode; return {};
}
return m_readerType.value();
} }
Utils::FilePath CMakeTool::searchQchFile(const Utils::FilePath &executable) Utils::FilePath CMakeTool::searchQchFile(const Utils::FilePath &executable)

View File

@@ -110,7 +110,7 @@ public:
void setPathMapper(const PathMapper &includePathMapper); void setPathMapper(const PathMapper &includePathMapper);
PathMapper pathMapper() const; PathMapper pathMapper() const;
ReaderType readerType() const; Utils::optional<ReaderType> readerType() const;
static Utils::FilePath searchQchFile(const Utils::FilePath &executable); static Utils::FilePath searchQchFile(const Utils::FilePath &executable);