CMake: Don't overwrite existing builds when changing build dir

When changing a build configuration's build directory to a directory
with an existing build (i.e. CMakeCache.txt), do not overwrite it, but
just parse it (running CMake as needed to generate the fileapi
response). Do not ask funny questions in that case.

This solves the pattern that you want to use some preconfigured build
but configured the right kit already with the default parameters.
Switching to a different build directory should simply never "kill"
an existing build in any case (and running CMake with the
"initial arguments" basically kills the existing build in the
not-so-uncommon case).

Change-Id: I54ca1f14d72a11a3bfe5b09ce340b28be321f80e
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Eike Ziller
2020-11-25 17:56:03 +01:00
parent 1b477cbadb
commit 3ef279450b
2 changed files with 18 additions and 9 deletions

View File

@@ -111,7 +111,7 @@ CMakeBuildConfiguration::CMakeBuildConfiguration(Target *target, Utils::Id id)
if (oldDir.isEmpty()) if (oldDir.isEmpty())
return newDir; return newDir;
if (QDir(oldDir).exists("CMakeCache.txt")) { if (QDir(oldDir).exists("CMakeCache.txt") && !QDir(newDir).exists("CMakeCache.txt")) {
if (QMessageBox::information(nullptr, if (QMessageBox::information(nullptr,
tr("Changing Build Directory"), tr("Changing Build Directory"),
tr("Change the build directory and start with a " tr("Change the build directory and start with a "

View File

@@ -766,14 +766,23 @@ void CMakeBuildSystem::wireUpConnections()
setParametersAndRequestParse(BuildDirParameters(cmakeBuildConfiguration()), setParametersAndRequestParse(BuildDirParameters(cmakeBuildConfiguration()),
CMakeBuildSystem::REPARSE_FORCE_CMAKE_RUN); CMakeBuildSystem::REPARSE_FORCE_CMAKE_RUN);
}); });
connect(cmakeBuildConfiguration(), &CMakeBuildConfiguration::buildDirectoryChanged, this, [this]() { connect(cmakeBuildConfiguration(),
// The build directory of our BC has changed: &CMakeBuildConfiguration::buildDirectoryChanged,
// Run with initial arguments! this,
qCDebug(cmakeBuildSystemLog) << "Requesting parse due to build directory change"; [this]() {
setParametersAndRequestParse(BuildDirParameters(cmakeBuildConfiguration()), // The build directory of our BC has changed:
CMakeBuildSystem::REPARSE_FORCE_INITIAL_CONFIGURATION // Does the directory contain a CMakeCache ? Existing build, just parse
| CMakeBuildSystem::REPARSE_FORCE_CMAKE_RUN); // No CMakeCache? Run with initial arguments!
}); qCDebug(cmakeBuildSystemLog) << "Requesting parse due to build directory change";
const BuildDirParameters parameters(cmakeBuildConfiguration());
const bool hasCMakeCache = QFile::exists(
(parameters.buildDirectory / "CMakeCache.txt").toString());
const auto options = ReparseParameters(
hasCMakeCache
? REPARSE_DEFAULT
: (REPARSE_FORCE_INITIAL_CONFIGURATION | REPARSE_FORCE_CMAKE_RUN));
setParametersAndRequestParse(BuildDirParameters(cmakeBuildConfiguration()), options);
});
connect(project(), &Project::projectFileIsDirty, this, [this]() { connect(project(), &Project::projectFileIsDirty, this, [this]() {
if (cmakeBuildConfiguration()->isActive() && !isParsing()) { if (cmakeBuildConfiguration()->isActive() && !isParsing()) {