Skip to content
Snippets Groups Projects
buildmanager.cpp 17.1 KiB
Newer Older
/**************************************************************************
con's avatar
con committed
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
**
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
**
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
**
**************************************************************************/
hjk's avatar
hjk committed

con's avatar
con committed
#include "buildmanager.h"
hjk's avatar
hjk committed

#include "buildprogress.h"
con's avatar
con committed
#include "buildstep.h"
#include "compileoutputwindow.h"
#include "projectexplorerconstants.h"
hjk's avatar
hjk committed
#include "projectexplorer.h"
#include "projectexplorersettings.h"
Tobias Hunger's avatar
Tobias Hunger committed
#include "target.h"
hjk's avatar
hjk committed
#include "taskwindow.h"
#include "buildconfiguration.h"
con's avatar
con committed

#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
con's avatar
con committed
#include <coreplugin/progressmanager/futureprogress.h>
#include <projectexplorer/session.h>
hjk's avatar
hjk committed
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>
con's avatar
con committed

#include <QtCore/QDir>
#include <QtCore/QTimer>
con's avatar
con committed
#include <QtGui/QHeaderView>
#include <QtGui/QIcon>
#include <QtGui/QLabel>
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
con's avatar
con committed

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

static inline QString msgProgress(int n, int total)
{
Friedemann Kleint's avatar
Friedemann Kleint committed
    return BuildManager::tr("Finished %1 of %n build steps", 0, n).arg(total);
con's avatar
con committed
BuildManager::BuildManager(ProjectExplorerPlugin *parent)
    : QObject(parent)
    , m_running(false)
    , m_previousBuildStepProject(0)
    , m_canceling(false)
    , m_maxProgress(0)
    , m_progressFutureInterface(0)
{
    ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
    m_projectExplorerPlugin = parent;

    connect(&m_watcher, SIGNAL(finished()),
            this, SLOT(nextBuildQueue()));

    connect(&m_watcher, SIGNAL(progressValueChanged(int)),
            this, SLOT(progressChanged()));
    connect(&m_watcher, SIGNAL(progressRangeChanged(int, int)),
            this, SLOT(progressChanged()));

    connect(parent->session(), SIGNAL(aboutToRemoveProject(ProjectExplorer::Project *)),
            this, SLOT(aboutToRemoveProject(ProjectExplorer::Project *)));

con's avatar
con committed
    m_outputWindow = new CompileOutputWindow(this);
    pm->addObject(m_outputWindow);

    m_taskWindow = new TaskWindow;
    pm->addObject(m_taskWindow);

    m_taskWindow->addCategory(Constants::TASK_CATEGORY_COMPILE, tr("Compile", "Category for compiler isses listened under 'Build Issues'"));
    m_taskWindow->addCategory(Constants::TASK_CATEGORY_BUILDSYSTEM, tr("Build System", "Category for build system isses listened under 'Build Issues'"));
con's avatar
con committed
    connect(m_taskWindow, SIGNAL(tasksChanged()),
            this, SLOT(updateTaskCount()));
con's avatar
con committed

dt's avatar
dt committed
    connect(m_taskWindow, SIGNAL(tasksCleared()),
            this,SIGNAL(tasksCleared()));

con's avatar
con committed
    connect(&m_progressWatcher, SIGNAL(canceled()),
            this, SLOT(cancel()));
    connect(&m_progressWatcher, SIGNAL(finished()),
            this, SLOT(finish()));
con's avatar
con committed
}

BuildManager::~BuildManager()
{
    cancel();
    ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();

    pm->removeObject(m_taskWindow);
    delete m_taskWindow;

    pm->removeObject(m_outputWindow);
    delete m_outputWindow;
}

void BuildManager::aboutToRemoveProject(ProjectExplorer::Project *p)
{
    QHash<Project *, int>::iterator it = m_activeBuildSteps.find(p);
    QHash<Project *, int>::iterator end = m_activeBuildSteps.end();
    if (it != end && *it > 0) {
        // We are building the project that's about to be removed.
        // We cancel the whole queue, which isn't the nicest thing to do
        // but a safe thing.
        cancel();
    }
}

con's avatar
con committed
bool BuildManager::isBuilding() const
{
    // we are building even if we are not running yet
    return !m_buildQueue.isEmpty() || m_running;
}

void BuildManager::cancel()
{
    if (m_running) {
        m_canceling = true;
        m_watcher.cancel();
        m_watcher.waitForFinished();

        // The cancel message is added to the output window via a single shot timer
        // since the canceling is likely to have generated new addToOutputWindow signals
        // which are waiting in the event queue to be processed
        // (And we want those to be before the cancel message.)
        QTimer::singleShot(0, this, SLOT(emitCancelMessage()));

        disconnect(m_currentBuildStep, SIGNAL(addTask(ProjectExplorer::Task)),
                   this, SLOT(addToTaskWindow(ProjectExplorer::Task)));
        disconnect(m_currentBuildStep, SIGNAL(addOutput(QString, QTextCharFormat)),
                   this, SLOT(addToOutputWindow(QString, QTextCharFormat)));
Tobias Hunger's avatar
Tobias Hunger committed
        decrementActiveBuildSteps(m_currentBuildStep->buildConfiguration()->target()->project());
con's avatar
con committed

        m_progressFutureInterface->setProgressValueAndText(m_progress*100, "Build canceled"); //TODO NBS fix in qtconcurrent
con's avatar
con committed
        clearBuildQueue();
    }
    return;
}

void BuildManager::updateTaskCount()
{
    Core::ProgressManager *progressManager = Core::ICore::instance()->progressManager();
    int errors = m_taskWindow->errorTaskCount();
    if (errors > 0) {
        progressManager->setApplicationLabel(QString("%1").arg(errors));
    } else {
        progressManager->setApplicationLabel("");
    }
    emit tasksChanged();
}

void BuildManager::finish()
{
    QApplication::alert(Core::ICore::instance()->mainWindow(), 3000);
}

con's avatar
con committed
void BuildManager::emitCancelMessage()
{
    QTextCharFormat textCharFormat;
    textCharFormat.setForeground(Qt::red);
    emit addToOutputWindow(tr("Canceled build."), textCharFormat);
con's avatar
con committed
}

void BuildManager::clearBuildQueue()
{
    foreach (BuildStep *bs, m_buildQueue) {
Tobias Hunger's avatar
Tobias Hunger committed
        decrementActiveBuildSteps(bs->buildConfiguration()->target()->project());
        disconnect(bs, SIGNAL(addTask(ProjectExplorer::Task)),
                   this, SLOT(addToTaskWindow(ProjectExplorer::Task)));
        disconnect(bs, SIGNAL(addOutput(QString, QTextCharFormat)),
                   this, SLOT(addToOutputWindow(QString, QTextCharFormat)));
con's avatar
con committed

    m_buildQueue.clear();
    m_running = false;
    m_previousBuildStepProject = 0;
    m_currentBuildStep = 0;
con's avatar
con committed

    m_progressFutureInterface->reportCanceled();
    m_progressFutureInterface->reportFinished();
    m_progressWatcher.setFuture(QFuture<void>());
con's avatar
con committed
    delete m_progressFutureInterface;
    m_progressFutureInterface = 0;
    m_maxProgress = 0;

    emit buildQueueFinished(false);
}


void BuildManager::toggleOutputWindow()
{
    m_outputWindow->toggle(false);
}

void BuildManager::showTaskWindow()
{
    m_taskWindow->popup(false);
}

void BuildManager::toggleTaskWindow()
{
    m_taskWindow->toggle(false);
}

bool BuildManager::tasksAvailable() const
{
    return m_taskWindow->taskCount() > 0;
con's avatar
con committed
}

void BuildManager::gotoTaskWindow()
{
    m_taskWindow->popup(true);
}

void BuildManager::startBuildQueue()
{
    if (m_buildQueue.isEmpty()) {
        emit buildQueueFinished(true);
con's avatar
con committed
    if (!m_running) {
        // Progress Reporting
        Core::ProgressManager *progressManager = Core::ICore::instance()->progressManager();
con's avatar
con committed
        m_progressFutureInterface = new QFutureInterface<void>;
        m_progressWatcher.setFuture(m_progressFutureInterface->future());
        m_outputWindow->clearContents();
        m_taskWindow->clearTasks(Constants::TASK_CATEGORY_COMPILE);
        m_taskWindow->clearTasks(Constants::TASK_CATEGORY_BUILDSYSTEM);
        progressManager->setApplicationLabel("");
con's avatar
con committed
        Core::FutureProgress *progress = progressManager->addTask(m_progressFutureInterface->future(),
              tr("Build"),
              Constants::TASK_BUILD,
              Core::ProgressManager::KeepOnFinish | Core::ProgressManager::ShowInApplicationIcon);
con's avatar
con committed
        connect(progress, SIGNAL(clicked()), this, SLOT(showBuildResults()));
        progress->setWidget(new BuildProgress(m_taskWindow));
        m_progress = 0;
        m_progressFutureInterface->setProgressRange(0, m_maxProgress * 100);
con's avatar
con committed

        m_running = true;
        m_canceling = false;
        m_progressFutureInterface->reportStarted();
        nextStep();
    } else {
        // Already running
        m_progressFutureInterface->setProgressRange(0, m_maxProgress * 100);
        m_progressFutureInterface->setProgressValueAndText(m_progress*100, msgProgress(m_progress, m_maxProgress));
con's avatar
con committed
    }
}

void BuildManager::showBuildResults()
{
    if (m_taskWindow->taskCount() != 0)
con's avatar
con committed
        toggleTaskWindow();
    else
        toggleOutputWindow();
    //toggleTaskWindow();
}

void BuildManager::addToTaskWindow(const ProjectExplorer::Task &task)
con's avatar
con committed
{
    m_taskWindow->addTask(task);
dt's avatar
dt committed
    emit taskAdded(task);
con's avatar
con committed
}

void BuildManager::addToOutputWindow(const QString &string, const QTextCharFormat &format)
con's avatar
con committed
{
    m_outputWindow->appendText(string, format);
con's avatar
con committed
}

void BuildManager::nextBuildQueue()
{
    if (m_canceling)
        return;

    disconnect(m_currentBuildStep, SIGNAL(addTask(ProjectExplorer::Task)),
               this, SLOT(addToTaskWindow(ProjectExplorer::Task)));
    disconnect(m_currentBuildStep, SIGNAL(addOutput(QString, QTextCharFormat)),
               this, SLOT(addToOutputWindow(QString, QTextCharFormat)));
con's avatar
con committed

    ++m_progress;
    m_progressFutureInterface->setProgressValueAndText(m_progress*100, msgProgress(m_progress, m_maxProgress));
Tobias Hunger's avatar
Tobias Hunger committed
    decrementActiveBuildSteps(m_currentBuildStep->buildConfiguration()->target()->project());
con's avatar
con committed

    bool result = m_watcher.result();
    if (!result) {
        // Build Failure
        const QString projectName = m_currentBuildStep->buildConfiguration()->target()->project()->displayName();
        const QString targetName = m_currentBuildStep->buildConfiguration()->target()->displayName();
        QTextCharFormat textCharFormat;
        textCharFormat.setForeground(Qt::red);
        addToOutputWindow(tr("Error while building project %1 (target: %2)").arg(projectName, targetName), textCharFormat);
        addToOutputWindow(tr("When executing build step '%1'").arg(m_currentBuildStep->displayName()), textCharFormat);
con's avatar
con committed
        // NBS TODO fix in qtconcurrent
        m_progressFutureInterface->setProgressValueAndText(m_progress*100, tr("Error while building project %1 (target: %2)").arg(projectName, targetName));
con's avatar
con committed
    }

    if (result)
        nextStep();
    else
        clearBuildQueue();
}

void BuildManager::progressChanged()
{
    if (!m_progressFutureInterface)
        return;
    int range = m_watcher.progressMaximum() - m_watcher.progressMinimum();
    if (range != 0) {
        int percent = (m_watcher.progressValue() - m_watcher.progressMinimum()) * 100 / range;
        m_progressFutureInterface->setProgressValue(m_progress * 100 + percent);
    }
}

con's avatar
con committed
void BuildManager::nextStep()
{
    if (!m_buildQueue.empty()) {
        m_currentBuildStep = m_buildQueue.front();
        m_buildQueue.pop_front();

Tobias Hunger's avatar
Tobias Hunger committed
        if (m_currentBuildStep->buildConfiguration()->target()->project() != m_previousBuildStepProject) {
dt's avatar
dt committed
            const QString projectName = m_currentBuildStep->buildConfiguration()->target()->project()->displayName();
            QTextCharFormat textCharFormat;
            textCharFormat.setFontWeight(QFont::Bold);
dt's avatar
dt committed
            addToOutputWindow(tr("Running build steps for project %1...")
                              .arg(projectName), textCharFormat);
Tobias Hunger's avatar
Tobias Hunger committed
            m_previousBuildStepProject = m_currentBuildStep->buildConfiguration()->target()->project();
con's avatar
con committed
        }
        m_watcher.setFuture(QtConcurrent::run(&BuildStep::run, m_currentBuildStep));
    } else {
        m_running = false;
        m_previousBuildStepProject = 0;
con's avatar
con committed
        m_progressFutureInterface->reportFinished();
        m_progressWatcher.setFuture(QFuture<void>());
        m_currentBuildStep = 0;
con's avatar
con committed
        delete m_progressFutureInterface;
        m_progressFutureInterface = 0;
        m_maxProgress = 0;
        emit buildQueueFinished(true);
    }
}

dt's avatar
dt committed
bool BuildManager::buildQueueAppend(QList<BuildStep *> steps)
con's avatar
con committed
{
dt's avatar
dt committed
    int count = steps.size();
    bool init = true;
    int i = 0;
    for (; i < count; ++i) {
        BuildStep *bs = steps.at(i);
        connect(bs, SIGNAL(addTask(ProjectExplorer::Task)),
                this, SLOT(addToTaskWindow(ProjectExplorer::Task)));
        connect(bs, SIGNAL(addOutput(QString, QTextCharFormat)),
                this, SLOT(addToOutputWindow(QString, QTextCharFormat)));
dt's avatar
dt committed
        init = bs->init();
        if (!init)
            break;
    }
dt's avatar
dt committed
        BuildStep *bs = steps.at(i);

        // cleaning up
        // print something for the user
        const QString projectName = bs->buildConfiguration()->target()->project()->displayName();
        const QString targetName = bs->buildConfiguration()->target()->displayName();
        QTextCharFormat textCharFormat;
        textCharFormat.setForeground(Qt::red);
        addToOutputWindow(tr("Error while building project %1 (target: %2)").arg(projectName, targetName), textCharFormat);
        addToOutputWindow(tr("When executing build step '%1'").arg(bs->displayName()), textCharFormat);
dt's avatar
dt committed

        // disconnect the buildsteps again
        for (int j = 0; j <= i; ++j) {
            BuildStep *bs = steps.at(j);
            disconnect(bs, SIGNAL(addTask(ProjectExplorer::Task)),
                       this, SLOT(addToTaskWindow(ProjectExplorer::Task)));
            disconnect(bs, SIGNAL(addOutput(QString, QTextCharFormat)),
                       this, SLOT(addToOutputWindow(QString, QTextCharFormat)));
dt's avatar
dt committed
        }
dt's avatar
dt committed
    // Everthing init() well
    for (i = 0; i < count; ++i) {
        ++m_maxProgress;
        m_buildQueue.append(steps.at(i));
        incrementActiveBuildSteps(steps.at(i)->buildConfiguration()->target()->project());
    }
con's avatar
con committed
}

dt's avatar
dt committed
void BuildManager::buildProjects(const QList<BuildConfiguration *> &configurations)
con's avatar
con committed
{
dt's avatar
dt committed
    QList<BuildStep *> steps;
    foreach(BuildConfiguration *bc, configurations)
        steps.append(bc->steps(Build));
dt's avatar
dt committed

    bool success = buildQueueAppend(steps);
    if (!success) {
        m_outputWindow->popup(false);
        return;
con's avatar
con committed
    }
    if (ProjectExplorerPlugin::instance()->projectExplorerSettings().showCompilerOutput)
        m_outputWindow->popup(false);
con's avatar
con committed
}

dt's avatar
dt committed
void BuildManager::cleanProjects(const QList<BuildConfiguration *> &configurations)
con's avatar
con committed
{
dt's avatar
dt committed
    QList<BuildStep *> steps;
    foreach(BuildConfiguration *bc, configurations)
        steps.append(bc->steps(Clean));
dt's avatar
dt committed

    bool success = buildQueueAppend(steps);
    if (!success) {
        m_outputWindow->popup(false);
        return;
con's avatar
con committed
    }
    if (ProjectExplorerPlugin::instance()->projectExplorerSettings().showCompilerOutput)
        m_outputWindow->popup(false);
con's avatar
con committed
}

dt's avatar
dt committed
void BuildManager::buildProject(BuildConfiguration *configuration)
con's avatar
con committed
{
dt's avatar
dt committed
    buildProjects(QList<BuildConfiguration *>() << configuration);
con's avatar
con committed
}

dt's avatar
dt committed
void BuildManager::cleanProject(BuildConfiguration *configuration)
con's avatar
con committed
{
dt's avatar
dt committed
    cleanProjects(QList<BuildConfiguration *>() << configuration);
con's avatar
con committed
}

void BuildManager::appendStep(BuildStep *step)
con's avatar
con committed
{
dt's avatar
dt committed
    bool success = buildQueueAppend(QList<BuildStep *>() << step);
    if (!success) {
        m_outputWindow->popup(false);
        return;
    }
    if (ProjectExplorerPlugin::instance()->projectExplorerSettings().showCompilerOutput)
        m_outputWindow->popup(false);
con's avatar
con committed
    startBuildQueue();
}

bool BuildManager::isBuilding(Project *pro)
{
    QHash<Project *, int>::iterator it = m_activeBuildSteps.find(pro);
    QHash<Project *, int>::iterator end = m_activeBuildSteps.end();
    if (it == end || *it == 0)
        return false;
    else
        return true;
}

bool BuildManager::isBuilding(BuildStep *step)
{
    return (m_currentBuildStep == step) || m_buildQueue.contains(step);
}

con's avatar
con committed
void BuildManager::incrementActiveBuildSteps(Project *pro)
{
    QHash<Project *, int>::iterator it = m_activeBuildSteps.find(pro);
    QHash<Project *, int>::iterator end = m_activeBuildSteps.end();
    if (it == end) {
        m_activeBuildSteps.insert(pro, 1);
        emit buildStateChanged(pro);
    } else if (*it == 0) {
        ++*it;
        emit buildStateChanged(pro);
    } else {
        ++*it;
    }
}

void BuildManager::decrementActiveBuildSteps(Project *pro)
{
    QHash<Project *, int>::iterator it = m_activeBuildSteps.find(pro);
    QHash<Project *, int>::iterator end = m_activeBuildSteps.end();
    if (it == end) {
        Q_ASSERT(false && "BuildManager m_activeBuildSteps says project is not building, but apparently a build step was still in the queue.");
con's avatar
con committed
    } else if (*it == 1) {
        --*it;
        emit buildStateChanged(pro);
    } else {
        --*it;
    }
}