qbscleanstep.cpp 10.4 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Tobias Hunger's avatar
Tobias Hunger committed
5 6 7 8 9 10 11
**
** 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
12 13 14
** 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.
Tobias Hunger's avatar
Tobias Hunger committed
15
**
16 17 18 19 20 21 22
** 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.
Tobias Hunger's avatar
Tobias Hunger committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36
**
****************************************************************************/

#include "qbscleanstep.h"

#include "qbsbuildconfiguration.h"
#include "qbsproject.h"
#include "qbsprojectmanagerconstants.h"

#include "ui_qbscleanstepconfigwidget.h"

#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/kit.h>
#include <projectexplorer/projectexplorerconstants.h>
37
#include <projectexplorer/buildmanager.h>
Tobias Hunger's avatar
Tobias Hunger committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <projectexplorer/target.h>
#include <utils/qtcassert.h>

static const char QBS_DRY_RUN[] = "Qbs.DryRun";
static const char QBS_KEEP_GOING[] = "Qbs.DryKeepGoing";

// --------------------------------------------------------------------
// Constants:
// --------------------------------------------------------------------

namespace QbsProjectManager {
namespace Internal {

// --------------------------------------------------------------------
// QbsCleanStep:
// --------------------------------------------------------------------

QbsCleanStep::QbsCleanStep(ProjectExplorer::BuildStepList *bsl) :
    ProjectExplorer::BuildStep(bsl, Core::Id(Constants::QBS_CLEANSTEP_ID)),
57
    m_job(0), m_showCompilerOutput(true), m_parser(0)
Tobias Hunger's avatar
Tobias Hunger committed
58
{
Tobias Hunger's avatar
Tobias Hunger committed
59
    setDisplayName(tr("Qbs Clean"));
Tobias Hunger's avatar
Tobias Hunger committed
60 61 62 63
}

QbsCleanStep::QbsCleanStep(ProjectExplorer::BuildStepList *bsl, const QbsCleanStep *other) :
    ProjectExplorer::BuildStep(bsl, Core::Id(Constants::QBS_CLEANSTEP_ID)),
64
    m_qbsCleanOptions(other->m_qbsCleanOptions), m_job(0),
Tobias Hunger's avatar
Tobias Hunger committed
65 66 67 68 69 70
    m_showCompilerOutput(other->m_showCompilerOutput), m_parser(0)
{ }

QbsCleanStep::~QbsCleanStep()
{
    cancel();
71 72 73 74
    if (m_job) {
        m_job->deleteLater();
        m_job = 0;
    }
Tobias Hunger's avatar
Tobias Hunger committed
75 76
}

77
bool QbsCleanStep::init(QList<const BuildStep *> &earlierSteps)
Tobias Hunger's avatar
Tobias Hunger committed
78
{
79
    Q_UNUSED(earlierSteps);
Tobias Hunger's avatar
Tobias Hunger committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    if (static_cast<QbsProject *>(project())->isParsing() || m_job)
        return false;

    QbsBuildConfiguration *bc = static_cast<QbsBuildConfiguration *>(buildConfiguration());
    if (!bc)
        bc = static_cast<QbsBuildConfiguration *>(target()->activeBuildConfiguration());

    if (!bc)
        return false;

    return true;
}

void QbsCleanStep::run(QFutureInterface<bool> &fi)
{
    m_fi = &fi;

    QbsProject *pro = static_cast<QbsProject *>(project());
98
    qbs::CleanOptions options(m_qbsCleanOptions);
Tobias Hunger's avatar
Tobias Hunger committed
99

100
    m_job = pro->clean(options);
Tobias Hunger's avatar
Tobias Hunger committed
101 102

    if (!m_job) {
103
        reportRunResult(*m_fi, false);
Tobias Hunger's avatar
Tobias Hunger committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        return;
    }

    m_progressBase = 0;

    connect(m_job, SIGNAL(finished(bool,qbs::AbstractJob*)), this, SLOT(cleaningDone(bool)));
    connect(m_job, SIGNAL(taskStarted(QString,int,qbs::AbstractJob*)),
            this, SLOT(handleTaskStarted(QString,int)));
    connect(m_job, SIGNAL(taskProgress(int,qbs::AbstractJob*)),
            this, SLOT(handleProgress(int)));
}

ProjectExplorer::BuildStepConfigWidget *QbsCleanStep::createConfigWidget()
{
    return new QbsCleanStepConfigWidget(this);
}

bool QbsCleanStep::runInGuiThread() const
{
    return true;
}

void QbsCleanStep::cancel()
{
    if (m_job)
        m_job->cancel();
}

bool QbsCleanStep::dryRun() const
{
Christian Kandeler's avatar
Christian Kandeler committed
134
    return m_qbsCleanOptions.dryRun();
Tobias Hunger's avatar
Tobias Hunger committed
135 136 137 138
}

bool QbsCleanStep::keepGoing() const
{
Christian Kandeler's avatar
Christian Kandeler committed
139
    return m_qbsCleanOptions.keepGoing();
Tobias Hunger's avatar
Tobias Hunger committed
140 141 142 143
}

int QbsCleanStep::maxJobs() const
{
144
    return 1;
Tobias Hunger's avatar
Tobias Hunger committed
145 146 147 148 149 150 151 152
}


bool QbsCleanStep::fromMap(const QVariantMap &map)
{
    if (!ProjectExplorer::BuildStep::fromMap(map))
        return false;

Christian Kandeler's avatar
Christian Kandeler committed
153 154
    m_qbsCleanOptions.setDryRun(map.value(QLatin1String(QBS_DRY_RUN)).toBool());
    m_qbsCleanOptions.setKeepGoing(map.value(QLatin1String(QBS_KEEP_GOING)).toBool());
Tobias Hunger's avatar
Tobias Hunger committed
155 156 157 158 159 160 161

    return true;
}

QVariantMap QbsCleanStep::toMap() const
{
    QVariantMap map = ProjectExplorer::BuildStep::toMap();
Christian Kandeler's avatar
Christian Kandeler committed
162 163
    map.insert(QLatin1String(QBS_DRY_RUN), m_qbsCleanOptions.dryRun());
    map.insert(QLatin1String(QBS_KEEP_GOING), m_qbsCleanOptions.keepGoing());
Tobias Hunger's avatar
Tobias Hunger committed
164 165 166 167 168 169 170

    return map;
}

void QbsCleanStep::cleaningDone(bool success)
{
    // Report errors:
171 172
    foreach (const qbs::ErrorItem &item, m_job->error().items()) {
        createTaskAndOutput(ProjectExplorer::Task::Error, item.description(),
Christian Kandeler's avatar
Christian Kandeler committed
173
                            item.codeLocation().filePath(), item.codeLocation().line());
Tobias Hunger's avatar
Tobias Hunger committed
174 175 176
    }

    QTC_ASSERT(m_fi, return);
177
    reportRunResult(*m_fi, success);
Tobias Hunger's avatar
Tobias Hunger committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    m_fi = 0; // do not delete, it is not ours
    m_job->deleteLater();
    m_job = 0;
}

void QbsCleanStep::handleTaskStarted(const QString &desciption, int max)
{
    Q_UNUSED(desciption);
    QTC_ASSERT(m_fi, return);
    m_progressBase = m_fi->progressValue();
    m_fi->setProgressRange(0, m_progressBase + max);
}

void QbsCleanStep::handleProgress(int value)
{
    QTC_ASSERT(m_fi, return);
    m_fi->setProgressValue(m_progressBase + value);
}

void QbsCleanStep::createTaskAndOutput(ProjectExplorer::Task::TaskType type, const QString &message, const QString &file, int line)
{
199 200 201 202
    ProjectExplorer::Task task = ProjectExplorer::Task(type, message,
                                                       Utils::FileName::fromString(file), line,
                                                       ProjectExplorer::Constants::TASK_CATEGORY_COMPILE);
    emit addTask(task, 1);
Tobias Hunger's avatar
Tobias Hunger committed
203 204 205 206 207
    emit addOutput(message, NormalOutput);
}

void QbsCleanStep::setDryRun(bool dr)
{
Christian Kandeler's avatar
Christian Kandeler committed
208
    if (m_qbsCleanOptions.dryRun() == dr)
Tobias Hunger's avatar
Tobias Hunger committed
209
        return;
Christian Kandeler's avatar
Christian Kandeler committed
210
    m_qbsCleanOptions.setDryRun(dr);
Tobias Hunger's avatar
Tobias Hunger committed
211 212 213 214 215
    emit changed();
}

void QbsCleanStep::setKeepGoing(bool kg)
{
Christian Kandeler's avatar
Christian Kandeler committed
216
    if (m_qbsCleanOptions.keepGoing() == kg)
Tobias Hunger's avatar
Tobias Hunger committed
217
        return;
Christian Kandeler's avatar
Christian Kandeler committed
218
    m_qbsCleanOptions.setKeepGoing(kg);
Tobias Hunger's avatar
Tobias Hunger committed
219 220 221 222 223
    emit changed();
}

void QbsCleanStep::setMaxJobs(int jobcount)
{
224
    Q_UNUSED(jobcount); // TODO: Remove all job count-related stuff.
Tobias Hunger's avatar
Tobias Hunger committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    emit changed();
}


// --------------------------------------------------------------------
// QbsCleanStepConfigWidget:
// --------------------------------------------------------------------

QbsCleanStepConfigWidget::QbsCleanStepConfigWidget(QbsCleanStep *step) :
    m_step(step)
{
    connect(m_step, SIGNAL(displayNameChanged()), this, SLOT(updateState()));
    connect(m_step, SIGNAL(changed()), this, SLOT(updateState()));

    setContentsMargins(0, 0, 0, 0);

    m_ui = new Ui::QbsCleanStepConfigWidget;
    m_ui->setupUi(this);

    connect(m_ui->dryRunCheckBox, SIGNAL(toggled(bool)), this, SLOT(changeDryRun(bool)));
    connect(m_ui->keepGoingCheckBox, SIGNAL(toggled(bool)), this, SLOT(changeKeepGoing(bool)));

    updateState();
}

Orgad Shaneh's avatar
Orgad Shaneh committed
250 251 252 253 254
QbsCleanStepConfigWidget::~QbsCleanStepConfigWidget()
{
    delete m_ui;
}

Tobias Hunger's avatar
Tobias Hunger committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
QString QbsCleanStepConfigWidget::summaryText() const
{
    return m_summary;
}

QString QbsCleanStepConfigWidget::displayName() const
{
    return m_step->displayName();
}

void QbsCleanStepConfigWidget::updateState()
{
    m_ui->dryRunCheckBox->setChecked(m_step->dryRun());
    m_ui->keepGoingCheckBox->setChecked(m_step->keepGoing());

270
    QString command = QbsBuildConfiguration::equivalentCommandLine(m_step);
271
    m_ui->commandLineTextEdit->setPlainText(command);
Tobias Hunger's avatar
Tobias Hunger committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

    QString summary = tr("<b>Qbs:</b> %1").arg(command);
    if (m_summary !=  summary) {
        m_summary = summary;
        emit updateSummary();
    }
}

void QbsCleanStepConfigWidget::changeDryRun(bool dr)
{
    m_step->setDryRun(dr);
}

void QbsCleanStepConfigWidget::changeKeepGoing(bool kg)
{
    m_step->setKeepGoing(kg);
}

void QbsCleanStepConfigWidget::changeJobCount(int count)
{
    m_step->setMaxJobs(count);
}

// --------------------------------------------------------------------
// QbsCleanStepFactory:
// --------------------------------------------------------------------

QbsCleanStepFactory::QbsCleanStepFactory(QObject *parent) :
    ProjectExplorer::IBuildStepFactory(parent)
{ }

QList<Core::Id> QbsCleanStepFactory::availableCreationIds(ProjectExplorer::BuildStepList *parent) const
{
    if (parent->id() == ProjectExplorer::Constants::BUILDSTEPS_CLEAN
            && qobject_cast<QbsBuildConfiguration *>(parent->parent()))
        return QList<Core::Id>() << Core::Id(Constants::QBS_CLEANSTEP_ID);
    return QList<Core::Id>();
}

311
QString QbsCleanStepFactory::displayNameForId(Core::Id id) const
Tobias Hunger's avatar
Tobias Hunger committed
312 313
{
    if (id == Core::Id(Constants::QBS_CLEANSTEP_ID))
Tobias Hunger's avatar
Tobias Hunger committed
314
        return tr("Qbs Clean");
Tobias Hunger's avatar
Tobias Hunger committed
315 316 317
    return QString();
}

318
bool QbsCleanStepFactory::canCreate(ProjectExplorer::BuildStepList *parent, Core::Id id) const
Tobias Hunger's avatar
Tobias Hunger committed
319 320 321 322 323 324 325
{
    if (parent->id() != Core::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN)
            || !qobject_cast<QbsBuildConfiguration *>(parent->parent()))
        return false;
    return id == Core::Id(Constants::QBS_CLEANSTEP_ID);
}

326
ProjectExplorer::BuildStep *QbsCleanStepFactory::create(ProjectExplorer::BuildStepList *parent, Core::Id id)
Tobias Hunger's avatar
Tobias Hunger committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
{
    if (!canCreate(parent, id))
        return 0;
    return new QbsCleanStep(parent);
}

bool QbsCleanStepFactory::canRestore(ProjectExplorer::BuildStepList *parent, const QVariantMap &map) const
{
    return canCreate(parent, ProjectExplorer::idFromMap(map));
}

ProjectExplorer::BuildStep *QbsCleanStepFactory::restore(ProjectExplorer::BuildStepList *parent, const QVariantMap &map)
{
    if (!canRestore(parent, map))
        return 0;
    QbsCleanStep *bs = new QbsCleanStep(parent);
    if (!bs->fromMap(map)) {
        delete bs;
        return 0;
    }
    return bs;
}

bool QbsCleanStepFactory::canClone(ProjectExplorer::BuildStepList *parent, ProjectExplorer::BuildStep *product) const
{
    return canCreate(parent, product->id());
}

ProjectExplorer::BuildStep *QbsCleanStepFactory::clone(ProjectExplorer::BuildStepList *parent, ProjectExplorer::BuildStep *product)
{
    if (!canClone(parent, product))
        return 0;
    return new QbsCleanStep(parent, static_cast<QbsCleanStep *>(product));
}

} // namespace Internal
} // namespace QbsProjectManager