CMakeProjectManager: Use CMAKE_HOME_DIRECTORY as source directory

The CMakeCache.txt file has CMAKE_HOME_DIRECTORY pointing to the
project source directory used to configure the project.

When importing a cmake build the CMAKE_HOME_DIRECTORY from
CMakeCache.txt might not point to the same CMakeLists.txt that was
opened as a project.

qt-cmake-standalone-test from Qt6 uses a CMake template project which
does a add_subdirectory with the test source directory, which will not
work if opened standalone.

Normally this is a user error though, so ask the user if this was
intended, before actually importing the build.

Task-number: QTBUG-88776
Change-Id: Ifdd5e1d1cb8a1ef9955d22493eba3a1a55dc689f
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Cristian Adam
2020-12-02 19:16:40 +01:00
parent ac378fbe2a
commit 707a3cfaf3
15 changed files with 104 additions and 22 deletions

View File

@@ -28,8 +28,11 @@
#include "cmakebuildconfiguration.h"
#include "cmakebuildsystem.h"
#include "cmakekitinformation.h"
#include "cmakeprojectconstants.h"
#include "cmaketoolmanager.h"
#include <coreplugin/messagemanager.h>
#include <projectexplorer/buildinfo.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/projectexplorerconstants.h>
@@ -58,6 +61,7 @@ struct DirectoryData
// Project Stuff:
QByteArray cmakeBuildType;
FilePath buildDirectory;
FilePath cmakeHomeDirectory;
// Kit Stuff
FilePath cmakeBinary;
@@ -266,7 +270,8 @@ static QVector<ToolChainDescription> extractToolChainsFromCache(const CMakeConfi
return result;
}
QList<void *> CMakeProjectImporter::examineDirectory(const FilePath &importPath) const
QList<void *> CMakeProjectImporter::examineDirectory(const FilePath &importPath,
QString *warningMessage) const
{
qCInfo(cmInputLog) << "Examining directory:" << importPath.toUserOutput();
const FilePath cacheFile = importPath.pathAppended("CMakeCache.txt");
@@ -282,18 +287,22 @@ QList<void *> CMakeProjectImporter::examineDirectory(const FilePath &importPath)
qCDebug(cmInputLog) << "Failed to read configuration from" << cacheFile << errorMessage;
return { };
}
const auto homeDir = FilePath::fromUserInput(
QString::fromUtf8(
CMakeConfigItem::valueOf("CMAKE_HOME_DIRECTORY", config)))
.canonicalPath();
auto data = std::make_unique<DirectoryData>();
data->cmakeHomeDirectory = FilePath::fromUserInput(
QString::fromUtf8(
CMakeConfigItem::valueOf("CMAKE_HOME_DIRECTORY", config)))
.canonicalPath();
const FilePath canonicalProjectDirectory = projectDirectory().canonicalPath();
if (homeDir != canonicalProjectDirectory) {
qCDebug(cmInputLog) << "Wrong source directory:" << homeDir.toUserOutput()
<< "expected:" << canonicalProjectDirectory.toUserOutput();
return { };
if (data->cmakeHomeDirectory != canonicalProjectDirectory) {
*warningMessage = tr("Unexpected source directory \"%1\", expected \"%2\". "
"This can be correct in some situations, for example when "
"importing a standalone Qt test, but usually this is an error. "
"Import the build anyway?")
.arg(data->cmakeHomeDirectory.toUserOutput(),
canonicalProjectDirectory.toUserOutput());
}
auto data = std::make_unique<DirectoryData>();
data->buildDirectory = importPath;
data->cmakeBuildType = CMakeConfigItem::valueOf("CMAKE_BUILD_TYPE", config);
@@ -395,6 +404,10 @@ const QList<BuildInfo> CMakeProjectImporter::buildInfoList(void *directoryData)
info.buildDirectory = data->buildDirectory;
info.displayName = info.typeName;
QVariantMap config;
config.insert(Constants::CMAKE_HOME_DIR, data->cmakeHomeDirectory.toString());
info.extraInfo = config;
qCDebug(cmInputLog) << "BuildInfo configured.";
return {info};
}