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

@@ -129,6 +129,8 @@ CMakeBuildConfiguration::CMakeBuildConfiguration(Target *target, Utils::Id id)
auto initialCMakeArgumentsAspect = addAspect<InitialCMakeArgumentsAspect>();
initialCMakeArgumentsAspect->setMacroExpanderProvider([this]{ return macroExpander(); });
addAspect<SourceDirectoryAspect>();
appendInitialBuildStep(Constants::CMAKE_BUILD_STEP_ID);
appendInitialCleanStep(Constants::CMAKE_BUILD_STEP_ID);
@@ -190,6 +192,11 @@ CMakeBuildConfiguration::CMakeBuildConfiguration(Target *target, Utils::Id id)
info.buildType));
}
if (info.extraInfo.isValid()) {
setSourceDirectory(FilePath::fromVariant(
info.extraInfo.value<QVariantMap>().value(Constants::CMAKE_HOME_DIR)));
}
setInitialCMakeArguments(initialArgs);
});
@@ -486,6 +493,16 @@ void CMakeBuildConfiguration::runCMakeWithExtraArguments()
m_buildSystem->runCMakeWithExtraArguments();
}
void CMakeBuildConfiguration::setSourceDirectory(const FilePath &path)
{
aspect<SourceDirectoryAspect>()->setValue(path.toString());
}
Utils::FilePath CMakeBuildConfiguration::sourceDirectory() const
{
return Utils::FilePath::fromString(aspect<SourceDirectoryAspect>()->value());
}
// ----------------------------------------------------------------------
// - InitialCMakeParametersAspect:
// ----------------------------------------------------------------------
@@ -497,5 +514,14 @@ InitialCMakeArgumentsAspect::InitialCMakeArgumentsAspect()
setDisplayStyle(TextEditDisplay);
}
// -----------------------------------------------------------------------------
// SourceDirectoryAspect:
// -----------------------------------------------------------------------------
SourceDirectoryAspect::SourceDirectoryAspect()
{
// Will not be displayed, only persisted
setSettingsKey("CMake.Source.Directory");
}
} // namespace Internal
} // namespace CMakeProjectManager