builddirmanager.cpp 21.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "builddirmanager.h"
27
#include "cmakebuildconfiguration.h"
28
#include "cmakekitinformation.h"
29
#include "cmakeparser.h"
30 31 32 33
#include "cmakeprojectmanager.h"
#include "cmaketool.h"

#include <coreplugin/messagemanager.h>
34
#include <coreplugin/progressmanager/progressmanager.h>
35
#include <projectexplorer/kit.h>
36
#include <projectexplorer/project.h>
37 38
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectnodes.h>
39
#include <projectexplorer/target.h>
40 41 42
#include <projectexplorer/taskhub.h>

#include <utils/algorithm.h>
43
#include <utils/fileutils.h>
44
#include <utils/qtcassert.h>
45
#include <utils/qtcprocess.h>
46 47 48
#include <utils/synchronousprocess.h>

#include <QDateTime>
Tobias Hunger's avatar
Tobias Hunger committed
49
#include <QFile>
50 51 52 53 54 55 56 57 58 59 60 61 62
#include <QFileInfo>
#include <QFileSystemWatcher>
#include <QRegularExpression>
#include <QSet>
#include <QTemporaryDir>

// --------------------------------------------------------------------
// Helper:
// --------------------------------------------------------------------

namespace CMakeProjectManager {
namespace Internal {

63 64
static QStringList toArguments(const CMakeConfig &config, const ProjectExplorer::Kit *k) {
    return Utils::transform(config, [k](const CMakeConfigItem &i) -> QString {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
                                 QString a = QString::fromLatin1("-D");
                                 a.append(QString::fromUtf8(i.key));
                                 switch (i.type) {
                                 case CMakeConfigItem::FILEPATH:
                                     a.append(QLatin1String(":FILEPATH="));
                                     break;
                                 case CMakeConfigItem::PATH:
                                     a.append(QLatin1String(":PATH="));
                                     break;
                                 case CMakeConfigItem::BOOL:
                                     a.append(QLatin1String(":BOOL="));
                                     break;
                                 case CMakeConfigItem::STRING:
                                     a.append(QLatin1String(":STRING="));
                                     break;
                                 case CMakeConfigItem::INTERNAL:
                                     a.append(QLatin1String(":INTERNAL="));
                                     break;
                                 }
84
                                 a.append(i.expandedValue(k));
85 86 87 88 89 90 91 92 93

                                 return a;
                             });
}

// --------------------------------------------------------------------
// BuildDirManager:
// --------------------------------------------------------------------

94 95
BuildDirManager::BuildDirManager(const CMakeBuildConfiguration *bc) :
    m_buildConfiguration(bc),
96 97
    m_watcher(new QFileSystemWatcher(this))
{
98 99
    QTC_ASSERT(bc, return);
    m_projectName = sourceDirectory().fileName();
100

101
    m_reparseTimer.setSingleShot(true);
Tobias Hunger's avatar
Tobias Hunger committed
102
    m_reparseTimer.setInterval(5000);
103
    connect(&m_reparseTimer, &QTimer::timeout, this, &BuildDirManager::parse);
104

105
    connect(m_watcher, &QFileSystemWatcher::fileChanged, this, [this]() {
106
        if (!isParsing())
107
            m_reparseTimer.start();
108 109 110 111 112
    });
}

BuildDirManager::~BuildDirManager()
{
113
    stopProcess();
114
    resetData();
115
    delete m_tempDir;
116 117 118 119 120 121 122 123 124 125 126 127
}

const ProjectExplorer::Kit *BuildDirManager::kit() const
{
    return m_buildConfiguration->target()->kit();
}

const Utils::FileName BuildDirManager::buildDirectory() const
{
    return m_buildConfiguration->buildDirectory();
}

128 129 130 131 132 133 134 135 136 137
const Utils::FileName BuildDirManager::workDirectory() const
{
    const Utils::FileName bdir = buildDirectory();
    if (bdir.exists())
        return bdir;
    if (m_tempDir)
        return Utils::FileName::fromString(m_tempDir->path());
    return bdir;
}

138 139 140
const Utils::FileName BuildDirManager::sourceDirectory() const
{
    return m_buildConfiguration->target()->project()->projectDirectory();
141 142
}

Tobias Hunger's avatar
Tobias Hunger committed
143
const CMakeConfig BuildDirManager::intendedConfiguration() const
144 145 146 147 148
{
    return m_buildConfiguration->cmakeConfiguration();
}

bool BuildDirManager::isParsing() const
149 150 151 152 153 154 155 156
{
    if (m_cmakeProcess)
        return m_cmakeProcess->state() != QProcess::NotRunning;
    return false;
}

void BuildDirManager::forceReparse()
{
157 158 159
    if (m_buildConfiguration->target()->activeBuildConfiguration() != m_buildConfiguration)
        return;

160
    stopProcess();
161

162 163
    CMakeTool *tool = CMakeKitInformation::cmakeTool(kit());
    const QString generator = CMakeGeneratorKitInformation::generator(kit());
164 165 166 167

    QTC_ASSERT(tool, return);
    QTC_ASSERT(!generator.isEmpty(), return);

Tobias Hunger's avatar
Tobias Hunger committed
168
    startCMake(tool, generator, intendedConfiguration());
169 170
}

171
void BuildDirManager::resetData()
172
{
173 174
    m_hasData = false;

Tobias Hunger's avatar
Tobias Hunger committed
175
    m_cmakeCache.clear();
176 177 178 179 180 181 182 183 184
    m_projectName.clear();
    m_buildTargets.clear();
    m_watchedFiles.clear();
    qDeleteAll(m_files);
    m_files.clear();

    const QStringList watchedFiles = m_watcher->files();
    if (!watchedFiles.isEmpty())
        m_watcher->removePaths(watchedFiles);
185 186
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
bool BuildDirManager::persistCMakeState()
{
    if (!m_tempDir)
        return false;

    QDir dir(buildDirectory().toString());
    dir.mkpath(buildDirectory().toString());

    delete m_tempDir;
    m_tempDir = nullptr;

    parse();
    return true;
}

202 203
void BuildDirManager::parse()
{
204 205
    CMakeTool *tool = CMakeKitInformation::cmakeTool(kit());
    const QString generator = CMakeGeneratorKitInformation::generator(kit());
206 207 208 209 210

    QTC_ASSERT(tool, return);
    QTC_ASSERT(!generator.isEmpty(), return);

    // Pop up a dialog asking the user to rerun cmake
211
    QString cbpFile = CMakeManager::findCbpFile(QDir(workDirectory().toString()));
212 213 214 215
    QFileInfo cbpFileFi(cbpFile);

    if (!cbpFileFi.exists()) {
        // Initial create:
Tobias Hunger's avatar
Tobias Hunger committed
216
        startCMake(tool, generator, intendedConfiguration());
217 218 219
        return;
    }

220 221
    const bool mustUpdate = m_watchedFiles.isEmpty()
            || Utils::anyOf(m_watchedFiles, [&cbpFileFi](const Utils::FileName &f) {
222 223 224 225 226 227
                  return f.toFileInfo().lastModified() > cbpFileFi.lastModified();
              });
    if (mustUpdate) {
        startCMake(tool, generator, CMakeConfig());
    } else {
        extractData();
228
        m_hasData = true;
229 230 231 232
        emit dataAvailable();
    }
}

233 234
void BuildDirManager::clearCache()
{
235 236
    auto cmakeCache = Utils::FileName(workDirectory()).appendPath(QLatin1String("CMakeCache.txt"));
    auto cmakeFiles = Utils::FileName(workDirectory()).appendPath(QLatin1String("CMakeFiles"));
237 238 239 240 241 242 243 244 245 246 247

    const bool mustCleanUp = cmakeCache.exists() || cmakeFiles.exists();
    if (!mustCleanUp)
        return;

    Utils::FileUtils::removeRecursively(cmakeCache);
    Utils::FileUtils::removeRecursively(cmakeFiles);

    forceReparse();
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
bool BuildDirManager::isProjectFile(const Utils::FileName &fileName) const
{
    return m_watchedFiles.contains(fileName);
}

QString BuildDirManager::projectName() const
{
    return m_projectName;
}

QList<CMakeBuildTarget> BuildDirManager::buildTargets() const
{
    return m_buildTargets;
}

263
QList<ProjectExplorer::FileNode *> BuildDirManager::files()
264 265 266 267
{
    return m_files;
}

268 269 270 271 272
void BuildDirManager::clearFiles()
{
    m_files.clear();
}

Tobias Hunger's avatar
Tobias Hunger committed
273
CMakeConfig BuildDirManager::parsedConfiguration() const
Tobias Hunger's avatar
Tobias Hunger committed
274
{
Tobias Hunger's avatar
Tobias Hunger committed
275 276 277 278 279 280 281 282 283 284 285 286 287
    if (m_cmakeCache.isEmpty()) {
        Utils::FileName cacheFile = workDirectory();
        cacheFile.appendPath(QLatin1String("CMakeCache.txt"));
        QString errorMessage;
        m_cmakeCache = parseConfiguration(cacheFile, &errorMessage);
        if (!errorMessage.isEmpty())
            emit errorOccured(errorMessage);
        const Utils::FileName sourceOfBuildDir
                = Utils::FileName::fromUtf8(CMakeConfigItem::valueOf("CMAKE_HOME_DIRECTORY", m_cmakeCache));
        if (sourceOfBuildDir != sourceDirectory()) // Use case-insensitive compare where appropriate
            emit errorOccured(tr("The build directory is not for %1").arg(sourceDirectory().toUserOutput()));
    }
    return m_cmakeCache;
Tobias Hunger's avatar
Tobias Hunger committed
288 289
}

290 291
void BuildDirManager::stopProcess()
{
292 293
    if (!m_cmakeProcess)
        return;
294

295 296 297 298 299 300 301 302 303
    m_cmakeProcess->disconnect();

    if (m_cmakeProcess->state() == QProcess::Running) {
        m_cmakeProcess->terminate();
        if (!m_cmakeProcess->waitForFinished(500))
            m_cmakeProcess->kill();
    }

    cleanUpProcess();
304

305 306
    if (!m_future)
      return;
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    m_future->reportCanceled();
    m_future->reportFinished();
    delete m_future;
    m_future = nullptr;
}

void BuildDirManager::cleanUpProcess()
{
    if (!m_cmakeProcess)
        return;

    QTC_ASSERT(m_cmakeProcess->state() == QProcess::NotRunning, return);

    m_cmakeProcess->disconnect();

    if (m_cmakeProcess->state() == QProcess::Running) {
        m_cmakeProcess->terminate();
        if (!m_cmakeProcess->waitForFinished(500))
            m_cmakeProcess->kill();
326
    }
327 328 329 330 331 332 333
    delete m_cmakeProcess;
    m_cmakeProcess = nullptr;

    // Delete issue parser:
    m_parser->flush();
    delete m_parser;
    m_parser = nullptr;
334 335
}

336 337 338
void BuildDirManager::extractData()
{
    const Utils::FileName topCMake
339
            = Utils::FileName::fromString(sourceDirectory().toString() + QLatin1String("/CMakeLists.txt"));
340

341 342 343
    resetData();

    m_projectName = sourceDirectory().fileName();
344 345 346 347
    m_files.append(new ProjectExplorer::FileNode(topCMake, ProjectExplorer::ProjectFileType, false));
    m_watchedFiles.insert(topCMake);

    // Find cbp file
348
    QString cbpFile = CMakeManager::findCbpFile(workDirectory().toString());
349 350 351 352 353 354 355 356
    if (cbpFile.isEmpty())
        return;

    m_watcher->addPath(cbpFile);

    // setFolderName
    CMakeCbpParser cbpparser;
    // Parsing
357
    if (!cbpparser.parseCbpFile(kit(), cbpFile, sourceDirectory().toString()))
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
        return;

    m_projectName = cbpparser.projectName();

    m_files = cbpparser.fileList();
    QSet<Utils::FileName> projectFiles;
    if (cbpparser.hasCMakeFiles()) {
        m_files.append(cbpparser.cmakeFileList());
        foreach (const ProjectExplorer::FileNode *node, cbpparser.cmakeFileList())
            projectFiles.insert(node->filePath());
    } else {
        m_files.append(new ProjectExplorer::FileNode(topCMake, ProjectExplorer::ProjectFileType, false));
        projectFiles.insert(topCMake);
    }

    m_watchedFiles = projectFiles;
374 375 376
    const QStringList toWatch
            = Utils::transform(m_watchedFiles.toList(), [](const Utils::FileName &fn) { return fn.toString(); });
    m_watcher->addPaths(toWatch);
377 378 379 380 381 382 383 384

    m_buildTargets = cbpparser.buildTargets();
}

void BuildDirManager::startCMake(CMakeTool *tool, const QString &generator,
                                 const CMakeConfig &config)
{
    QTC_ASSERT(tool && tool->isValid(), return);
385

386
    QTC_ASSERT(!m_cmakeProcess, return);
387
    QTC_ASSERT(!m_parser, return);
388
    QTC_ASSERT(!m_future, return);
389

390 391 392 393 394 395 396 397 398
    // Find a directory to set up into:
    if (!buildDirectory().exists()) {
        if (!m_tempDir)
            m_tempDir = new QTemporaryDir(QDir::tempPath() + QLatin1String("/qtc-cmake-XXXXXX"));
        QTC_ASSERT(m_tempDir->isValid(), return);
    }

    // Make sure work directory exists:
    QTC_ASSERT(workDirectory().exists(), return);
399

400
    m_parser = new CMakeParser;
401
    QDir source = QDir(sourceDirectory().toString());
402 403 404 405 406 407 408 409 410 411 412
    connect(m_parser, &ProjectExplorer::IOutputParser::addTask, m_parser,
            [source](const ProjectExplorer::Task &task) {
                if (task.file.isEmpty() || task.file.toFileInfo().isAbsolute()) {
                    ProjectExplorer::TaskHub::addTask(task);
                } else {
                    ProjectExplorer::Task t = task;
                    t.file = Utils::FileName::fromString(source.absoluteFilePath(task.file.toString()));
                    ProjectExplorer::TaskHub::addTask(t);
                }
            });

413 414
    // Always use the sourceDir: If we are triggered because the build directory is getting deleted
    // then we are racing against CMakeCache.txt also getting deleted.
415
    const QString srcDir = sourceDirectory().toString();
416 417

    m_cmakeProcess = new Utils::QtcProcess(this);
418
    m_cmakeProcess->setWorkingDirectory(workDirectory().toString());
419
    m_cmakeProcess->setEnvironment(m_buildConfiguration->environment());
420 421 422

    connect(m_cmakeProcess, &QProcess::readyReadStandardOutput,
            this, &BuildDirManager::processCMakeOutput);
423 424
    connect(m_cmakeProcess, &QProcess::readyReadStandardError,
            this, &BuildDirManager::processCMakeError);
425 426 427 428 429 430 431
    connect(m_cmakeProcess, static_cast<void(QProcess::*)(int,  QProcess::ExitStatus)>(&QProcess::finished),
            this, &BuildDirManager::cmakeFinished);

    QString args;
    Utils::QtcProcess::addArg(&args, srcDir);
    if (!generator.isEmpty())
        Utils::QtcProcess::addArg(&args, QString::fromLatin1("-G%1").arg(generator));
432
    Utils::QtcProcess::addArgs(&args, toArguments(config, kit()));
433 434 435

    ProjectExplorer::TaskHub::clearTasks(ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM);

436
    Core::MessageManager::write(tr("Running \"%1 %2\" in %3.")
437 438
                                .arg(tool->cmakeExecutable().toUserOutput())
                                .arg(args)
439
                                .arg(workDirectory().toUserOutput()));
440 441 442 443

    m_future = new QFutureInterface<void>();
    m_future->setProgressRange(0, 1);
    Core::ProgressManager::addTask(m_future->future(),
444
                                   tr("Configuring \"%1\"").arg(m_buildConfiguration->target()->project()->displayName()),
445 446
                                   "CMake.Configure");

447 448
    m_cmakeProcess->setCommand(tool->cmakeExecutable().toString(), args);
    m_cmakeProcess->start();
449
    emit configurationStarted();
450 451 452 453 454 455 456
}

void BuildDirManager::cmakeFinished(int code, QProcess::ExitStatus status)
{
    QTC_ASSERT(m_cmakeProcess, return);

    // process rest of the output:
457 458
    processCMakeOutput();
    processCMakeError();
459

460
    cleanUpProcess();
461 462 463

    extractData(); // try even if cmake failed...

464
    QString msg;
465
    if (status != QProcess::NormalExit)
466
        msg = tr("*** cmake process crashed!");
467
    else if (code != 0)
468 469 470 471 472 473
        msg = tr("*** cmake process exited with exit code %1.").arg(code);

    if (!msg.isEmpty()) {
        Core::MessageManager::write(msg);
        ProjectExplorer::TaskHub::addTask(ProjectExplorer::Task::Error, msg,
                                          ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM);
474 475 476
        m_future->reportCanceled();
    } else {
        m_future->setProgressValue(1);
477 478
    }

479 480
    m_future->reportFinished();
    delete m_future;
481
    m_future = nullptr;
482

483
    m_hasData = true;
484 485 486
    emit dataAvailable();
}

487 488 489 490 491 492 493 494 495 496 497 498 499
static QString lineSplit(const QString &rest, const QByteArray &array, std::function<void(const QString &)> f)
{
    QString tmp = rest + Utils::SynchronousProcess::normalizeNewlines(QString::fromLocal8Bit(array));
    int start = 0;
    int end = tmp.indexOf(QLatin1Char('\n'), start);
    while (end >= 0) {
        f(tmp.mid(start, end - start));
        start = end + 1;
        end = tmp.indexOf(QLatin1Char('\n'), start);
    }
    return tmp.mid(start);
}

500 501
void BuildDirManager::processCMakeOutput()
{
502 503
    static QString rest;
    rest = lineSplit(rest, m_cmakeProcess->readAllStandardOutput(), [this](const QString &s) { Core::MessageManager::write(s); });
504 505
}

506
void BuildDirManager::processCMakeError()
507
{
508 509 510 511 512
    static QString rest;
    rest = lineSplit(rest, m_cmakeProcess->readAllStandardError(), [this](const QString &s) {
        m_parser->stdError(s);
        Core::MessageManager::write(s);
    });
513 514
}

Tobias Hunger's avatar
Tobias Hunger committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
static QByteArray trimCMakeCacheLine(const QByteArray &in) {
    int start = 0;
    while (start < in.count() && (in.at(start) == ' ' || in.at(start) == '\t'))
        ++start;

    return in.mid(start, in.count() - start - 1);
}

static QByteArrayList splitCMakeCacheLine(const QByteArray &line) {
    const int colonPos = line.indexOf(':');
    if (colonPos < 0)
        return QByteArrayList();

    const int equalPos = line.indexOf('=', colonPos + 1);
    if (equalPos < colonPos)
        return QByteArrayList();

    return QByteArrayList() << line.mid(0, colonPos)
                            << line.mid(colonPos + 1, equalPos - colonPos - 1)
                            << line.mid(equalPos + 1);
}

static CMakeConfigItem::Type fromByteArray(const QByteArray &type) {
    if (type == "BOOL")
        return CMakeConfigItem::BOOL;
    if (type == "STRING")
        return CMakeConfigItem::STRING;
    if (type == "FILEPATH")
        return CMakeConfigItem::FILEPATH;
    if (type == "PATH")
        return CMakeConfigItem::PATH;
    QTC_CHECK(type == "INTERNAL" || type == "STATIC");

    return CMakeConfigItem::INTERNAL;
}

551 552
CMakeConfig BuildDirManager::parseConfiguration(const Utils::FileName &cacheFile,
                                                QString *errorMessage)
Tobias Hunger's avatar
Tobias Hunger committed
553 554
{
    CMakeConfig result;
555 556 557 558
    QFile cache(cacheFile.toString());
    if (!cache.open(QIODevice::ReadOnly | QIODevice::Text)) {
        if (errorMessage)
            *errorMessage = tr("Failed to open %1 for reading.").arg(cacheFile.toUserOutput());
Tobias Hunger's avatar
Tobias Hunger committed
559
        return CMakeConfig();
560
    }
Tobias Hunger's avatar
Tobias Hunger committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

    QSet<QByteArray> advancedSet;
    QByteArray documentation;
    while (!cache.atEnd()) {
        const QByteArray line = trimCMakeCacheLine(cache.readLine());

        if (line.isEmpty() || line.startsWith('#'))
            continue;

        if (line.startsWith("//")) {
            documentation = line.mid(2);
            continue;
        }

        const QByteArrayList pieces = splitCMakeCacheLine(line);
        if (pieces.isEmpty())
            continue;

        QTC_ASSERT(pieces.count() == 3, continue);
        const QByteArray key = pieces.at(0);
        const QByteArray type = pieces.at(1);
        const QByteArray value = pieces.at(2);

        if (key.endsWith("-ADVANCED") && value == "1") {
            advancedSet.insert(key.left(key.count() - 9 /* "-ADVANCED" */));
        } else {
            CMakeConfigItem::Type t = fromByteArray(type);
588
            result << CMakeConfigItem(key, t, documentation, value);
Tobias Hunger's avatar
Tobias Hunger committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602
        }
    }

    // Set advanced flags:
    for (int i = 0; i < result.count(); ++i) {
        CMakeConfigItem &item = result[i];
        item.isAdvanced = advancedSet.contains(item.key);
    }

    Utils::sort(result, CMakeConfigItem::sortOperator());

    return result;
}

603 604 605 606 607 608
void BuildDirManager::maybeForceReparse()
{
    const QByteArray GENERATOR_KEY = "CMAKE_GENERATOR";
    const QByteArray EXTRA_GENERATOR_KEY = "CMAKE_EXTRA_GENERATOR";
    const QByteArray CMAKE_COMMAND_KEY = "CMAKE_COMMAND";

609
    const QByteArrayList criticalKeys
610
            = QByteArrayList() << GENERATOR_KEY << CMAKE_COMMAND_KEY;
611

612 613
    if (!m_hasData) {
        forceReparse();
614
        return;
615
    }
616 617 618

    const CMakeConfig currentConfig = parsedConfiguration();

619 620
    const CMakeTool *tool = CMakeKitInformation::cmakeTool(kit());
    QTC_ASSERT(tool, return); // No cmake... we should not have ended up here in the first place
621 622 623 624 625 626
    const QString kitGenerator = CMakeGeneratorKitInformation::generator(kit());
    int pos = kitGenerator.lastIndexOf(QLatin1String(" - "));
    const QString extraKitGenerator = (pos > 0) ? kitGenerator.left(pos) : QString();
    const QString mainKitGenerator = (pos > 0) ? kitGenerator.mid(pos + 3) : kitGenerator;
    CMakeConfig targetConfig = m_buildConfiguration->cmakeConfiguration();
    targetConfig.append(CMakeConfigItem(GENERATOR_KEY, CMakeConfigItem::INTERNAL,
Tobias Hunger's avatar
Tobias Hunger committed
627
                                        QByteArray(), mainKitGenerator.toUtf8()));
628 629
    if (!extraKitGenerator.isEmpty())
        targetConfig.append(CMakeConfigItem(EXTRA_GENERATOR_KEY, CMakeConfigItem::INTERNAL,
630 631 632
                                            QByteArray(), extraKitGenerator.toUtf8()));
    targetConfig.append(CMakeConfigItem(CMAKE_COMMAND_KEY, CMakeConfigItem::INTERNAL,
                                        QByteArray(), tool->cmakeExecutable().toUserOutput().toUtf8()));
633 634
    Utils::sort(targetConfig, CMakeConfigItem::sortOperator());

635
    bool mustReparse = false;
636 637
    auto ccit = currentConfig.constBegin();
    auto kcit = targetConfig.constBegin();
638

639 640
    while (ccit != currentConfig.constEnd() && kcit != targetConfig.constEnd()) {
        if (ccit->key == kcit->key) {
641 642 643 644 645 646 647
            if (ccit->value != kcit->value) {
                if (criticalKeys.contains(kcit->key)) {
                        clearCache();
                        return;
                }
                mustReparse = true;
            }
648 649 650
            ++ccit;
            ++kcit;
        } else {
651
            if (ccit->key < kcit->key) {
652
                ++ccit;
653 654 655 656
            } else {
                ++kcit;
                mustReparse = true;
            }
657 658 659
        }
    }

660 661 662 663 664 665
    // If we have keys that do not exist yet, then reparse.
    //
    // The critical keys *must* be set in cmake configuration, so those were already
    // handled above.
    if (mustReparse || kcit != targetConfig.constEnd())
        forceReparse();
666 667
}

668 669
} // namespace Internal
} // namespace CMakeProjectManager