genericmakestep.cpp 12.6 KB
Newer Older
Roberto Raggi's avatar
Roberto Raggi committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
Roberto Raggi's avatar
Roberto Raggi committed
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
Roberto Raggi's avatar
Roberto Raggi committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
Roberto Raggi's avatar
Roberto Raggi committed
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
Roberto Raggi's avatar
Roberto Raggi committed
28 29 30
**
**************************************************************************/

31
#include "genericmakestep.h"
Roberto Raggi's avatar
Roberto Raggi committed
32 33
#include "genericprojectconstants.h"
#include "genericproject.h"
34
#include "ui_genericmakestep.h"
35
#include "genericbuildconfiguration.h"
Roberto Raggi's avatar
Roberto Raggi committed
36

37
#include <extensionsystem/pluginmanager.h>
Tobias Hunger's avatar
Tobias Hunger committed
38
#include <projectexplorer/buildsteplist.h>
Tobias Hunger's avatar
Tobias Hunger committed
39
#include <projectexplorer/gnumakeparser.h>
Tobias Hunger's avatar
Tobias Hunger committed
40 41
#include <projectexplorer/profileinformation.h>
#include <projectexplorer/projectexplorer.h>
42
#include <projectexplorer/projectexplorerconstants.h>
Tobias Hunger's avatar
Tobias Hunger committed
43
#include <projectexplorer/toolchain.h>
44
#include <coreplugin/variablemanager.h>
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
45
#include <utils/stringutils.h>
46
#include <utils/qtcassert.h>
47
#include <utils/qtcprocess.h>
48

49 50 51 52 53
#include <QFormLayout>
#include <QGroupBox>
#include <QCheckBox>
#include <QLineEdit>
#include <QListWidget>
Roberto Raggi's avatar
Roberto Raggi committed
54 55 56 57

using namespace GenericProjectManager;
using namespace GenericProjectManager::Internal;

58 59 60 61 62 63 64 65
namespace {
const char * const GENERIC_MS_ID("GenericProjectManager.GenericMakeStep");
const char * const GENERIC_MS_DISPLAY_NAME(QT_TRANSLATE_NOOP("GenericProjectManager::Internal::GenericMakeStep",
                                                             "Make"));

const char * const BUILD_TARGETS_KEY("GenericProjectManager.GenericMakeStep.BuildTargets");
const char * const MAKE_ARGUMENTS_KEY("GenericProjectManager.GenericMakeStep.MakeArguments");
const char * const MAKE_COMMAND_KEY("GenericProjectManager.GenericMakeStep.MakeCommand");
66
const char * const CLEAN_KEY("GenericProjectManager.GenericMakeStep.Clean");
67 68
}

Tobias Hunger's avatar
Tobias Hunger committed
69
GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent) :
70
    AbstractProcessStep(parent, Core::Id(GENERIC_MS_ID)),
71
    m_clean(false)
72 73 74 75
{
    ctor();
}

76
GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent, const Core::Id id) :
77 78
    AbstractProcessStep(parent, id),
    m_clean(false)
79 80 81 82
{
    ctor();
}

Tobias Hunger's avatar
Tobias Hunger committed
83 84
GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent, GenericMakeStep *bs) :
    AbstractProcessStep(parent, bs),
85 86
    m_buildTargets(bs->m_buildTargets),
    m_makeArguments(bs->m_makeArguments),
87 88
    m_makeCommand(bs->m_makeCommand),
    m_clean(bs->m_clean)
Roberto Raggi's avatar
Roberto Raggi committed
89
{
90
    ctor();
Roberto Raggi's avatar
Roberto Raggi committed
91 92
}

93
void GenericMakeStep::ctor()
dt's avatar
Compile  
dt committed
94
{
95 96
    setDefaultDisplayName(QCoreApplication::translate("GenericProjectManager::Internal::GenericMakeStep",
                                                      GENERIC_MS_DISPLAY_NAME));
dt's avatar
Compile  
dt committed
97 98
}

99
GenericMakeStep::~GenericMakeStep()
Roberto Raggi's avatar
Roberto Raggi committed
100 101 102
{
}

dt's avatar
dt committed
103 104 105 106 107
GenericBuildConfiguration *GenericMakeStep::genericBuildConfiguration() const
{
    return static_cast<GenericBuildConfiguration *>(buildConfiguration());
}

108
bool GenericMakeStep::init()
Roberto Raggi's avatar
Roberto Raggi committed
109
{
dt's avatar
dt committed
110
    GenericBuildConfiguration *bc = genericBuildConfiguration();
111 112
    if (!bc)
        bc = static_cast<GenericBuildConfiguration *>(target()->activeBuildConfiguration());
Roberto Raggi's avatar
Roberto Raggi committed
113

114 115 116 117 118 119 120
    ProjectExplorer::ProcessParameters *pp = processParameters();
    pp->setMacroExpander(bc->macroExpander());
    pp->setWorkingDirectory(bc->buildDirectory());
    pp->setEnvironment(bc->environment());
    pp->setCommand(makeCommand());
    pp->setArguments(allArguments());

121 122 123 124 125
    // If we are cleaning, then make can fail with an error code, but that doesn't mean
    // we should stop the clean queue
    // That is mostly so that rebuild works on an already clean project
    setIgnoreReturnValue(m_clean);

126
    setOutputParser(new ProjectExplorer::GnuMakeParser());
Tobias Hunger's avatar
Tobias Hunger committed
127 128 129 130
    ProjectExplorer::ToolChain *tc =
            ProjectExplorer::ToolChainProfileInformation::toolChain(bc->target()->profile());
    if (tc)
        appendOutputParser(tc->outputParser());
131
    outputParser()->setWorkingDirectory(pp->effectiveWorkingDirectory());
Tobias Hunger's avatar
Tobias Hunger committed
132 133

    return AbstractProcessStep::init();
134 135
}

136 137 138 139 140 141 142 143 144 145
void GenericMakeStep::setClean(bool clean)
{
    m_clean = clean;
}

bool GenericMakeStep::isClean() const
{
    return m_clean;
}

146
QVariantMap GenericMakeStep::toMap() const
147
{
148 149 150 151 152
    QVariantMap map(AbstractProcessStep::toMap());

    map.insert(QLatin1String(BUILD_TARGETS_KEY), m_buildTargets);
    map.insert(QLatin1String(MAKE_ARGUMENTS_KEY), m_makeArguments);
    map.insert(QLatin1String(MAKE_COMMAND_KEY), m_makeCommand);
153
    map.insert(QLatin1String(CLEAN_KEY), m_clean);
154
    return map;
155 156
}

157
bool GenericMakeStep::fromMap(const QVariantMap &map)
158
{
159
    m_buildTargets = map.value(QLatin1String(BUILD_TARGETS_KEY)).toStringList();
160
    m_makeArguments = map.value(QLatin1String(MAKE_ARGUMENTS_KEY)).toString();
161
    m_makeCommand = map.value(QLatin1String(MAKE_COMMAND_KEY)).toString();
162
    m_clean = map.value(QLatin1String(CLEAN_KEY)).toBool();
163 164

    return BuildStep::fromMap(map);
165 166
}

167
QString GenericMakeStep::allArguments() const
168
{
169 170 171
    QString args = m_makeArguments;
    Utils::QtcProcess::addArgs(&args, m_buildTargets);
    return args;
172
}
173

174
QString GenericMakeStep::makeCommand() const
175
{
176
    QString command = m_makeCommand;
177
    if (command.isEmpty()) {
Tobias Hunger's avatar
Tobias Hunger committed
178 179 180
        ProjectExplorer::ToolChain *tc = ProjectExplorer::ToolChainProfileInformation::toolChain(target()->profile());
        if (tc)
            command = tc->makeCommand();
181 182 183 184
        else
            command = QLatin1String("make");
    }
    return command;
Roberto Raggi's avatar
Roberto Raggi committed
185 186
}

187
void GenericMakeStep::run(QFutureInterface<bool> &fi)
Roberto Raggi's avatar
Roberto Raggi committed
188 189 190 191
{
    AbstractProcessStep::run(fi);
}

192
ProjectExplorer::BuildStepConfigWidget *GenericMakeStep::createConfigWidget()
Roberto Raggi's avatar
Roberto Raggi committed
193
{
194
    return new GenericMakeStepConfigWidget(this);
Roberto Raggi's avatar
Roberto Raggi committed
195 196
}

197
bool GenericMakeStep::immutable() const
Roberto Raggi's avatar
Roberto Raggi committed
198
{
199
    return false;
Roberto Raggi's avatar
Roberto Raggi committed
200 201
}

202
bool GenericMakeStep::buildsTarget(const QString &target) const
Roberto Raggi's avatar
Roberto Raggi committed
203
{
204
    return m_buildTargets.contains(target);
Roberto Raggi's avatar
Roberto Raggi committed
205 206
}

207
void GenericMakeStep::setBuildTarget(const QString &target, bool on)
Roberto Raggi's avatar
Roberto Raggi committed
208
{
209
    QStringList old = m_buildTargets;
Roberto Raggi's avatar
Roberto Raggi committed
210
    if (on && !old.contains(target))
211
         old << target;
Roberto Raggi's avatar
Roberto Raggi committed
212
    else if(!on && old.contains(target))
213
        old.removeOne(target);
214

215
    m_buildTargets = old;
Roberto Raggi's avatar
Roberto Raggi committed
216 217 218
}

//
219
// GenericMakeStepConfigWidget
Roberto Raggi's avatar
Roberto Raggi committed
220
//
221 222

GenericMakeStepConfigWidget::GenericMakeStepConfigWidget(GenericMakeStep *makeStep)
Roberto Raggi's avatar
Roberto Raggi committed
223 224
    : m_makeStep(makeStep)
{
225 226
    m_ui = new Ui::GenericMakeStep;
    m_ui->setupUi(this);
Roberto Raggi's avatar
Roberto Raggi committed
227

228
    GenericProject *pro = static_cast<GenericProject *>(m_makeStep->target()->project());
Tobias Hunger's avatar
Tobias Hunger committed
229
    foreach (const QString &target, pro->buildTargets()) {
230
        QListWidgetItem *item = new QListWidgetItem(target, m_ui->targetsList);
Roberto Raggi's avatar
Roberto Raggi committed
231
        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
dt_'s avatar
dt_ committed
232
        item->setCheckState(m_makeStep->buildsTarget(item->text()) ? Qt::Checked : Qt::Unchecked);
Roberto Raggi's avatar
Roberto Raggi committed
233 234
    }

dt_'s avatar
dt_ committed
235 236 237 238 239
    m_ui->makeLineEdit->setText(m_makeStep->m_makeCommand);
    m_ui->makeArgumentsLineEdit->setText(m_makeStep->m_makeArguments);
    updateMakeOverrrideLabel();
    updateDetails();

240 241
    connect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)),
            this, SLOT(itemChanged(QListWidgetItem*)));
242
    connect(m_ui->makeLineEdit, SIGNAL(textEdited(QString)),
243
            this, SLOT(makeLineEditTextEdited()));
244
    connect(m_ui->makeArgumentsLineEdit, SIGNAL(textEdited(QString)),
245
            this, SLOT(makeArgumentsLineEditTextEdited()));
246 247 248 249 250

    connect(ProjectExplorer::ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
            this, SLOT(updateMakeOverrrideLabel()));
    connect(ProjectExplorer::ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
            this, SLOT(updateDetails()));
251

Tobias Hunger's avatar
Tobias Hunger committed
252
    connect(m_makeStep->target(), SIGNAL(profileChanged()),
253
            this, SLOT(updateMakeOverrrideLabel()));
Roberto Raggi's avatar
Roberto Raggi committed
254 255
}

256 257 258 259 260
GenericMakeStepConfigWidget::~GenericMakeStepConfigWidget()
{
    delete m_ui;
}

261
QString GenericMakeStepConfigWidget::displayName() const
Roberto Raggi's avatar
Roberto Raggi committed
262
{
263
    return tr("Make", "GenericMakestep display name.");
Roberto Raggi's avatar
Roberto Raggi committed
264 265
}

266 267
void GenericMakeStepConfigWidget::updateMakeOverrrideLabel()
{
268
    m_ui->makeLabel->setText(tr("Override %1:").arg(m_makeStep->makeCommand()));
269 270
}

271 272
void GenericMakeStepConfigWidget::updateDetails()
{
273
    GenericBuildConfiguration *bc = m_makeStep->genericBuildConfiguration();
274 275
    if (!bc)
        bc = static_cast<GenericBuildConfiguration *>(m_makeStep->target()->activeBuildConfiguration());
276 277 278 279 280 281 282 283

    ProjectExplorer::ProcessParameters param;
    param.setMacroExpander(bc->macroExpander());
    param.setWorkingDirectory(bc->buildDirectory());
    param.setEnvironment(bc->environment());
    param.setCommand(m_makeStep->makeCommand());
    param.setArguments(m_makeStep->allArguments());
    m_summaryText = param.summary(displayName());
284 285 286
    emit updateSummary();
}

dt's avatar
dt committed
287 288
QString GenericMakeStepConfigWidget::summaryText() const
{
289
    return m_summaryText;
dt's avatar
dt committed
290 291
}

292 293
void GenericMakeStepConfigWidget::itemChanged(QListWidgetItem *item)
{
294
    m_makeStep->setBuildTarget(item->text(), item->checkState() & Qt::Checked);
295
    updateDetails();
296 297 298 299
}

void GenericMakeStepConfigWidget::makeLineEditTextEdited()
{
300
    m_makeStep->m_makeCommand = m_ui->makeLineEdit->text();
301
    updateDetails();
302 303 304 305
}

void GenericMakeStepConfigWidget::makeArgumentsLineEditTextEdited()
{
306
    m_makeStep->m_makeArguments = m_ui->makeArgumentsLineEdit->text();
307
    updateDetails();
Roberto Raggi's avatar
Roberto Raggi committed
308 309 310
}

//
311
// GenericMakeStepFactory
Roberto Raggi's avatar
Roberto Raggi committed
312 313
//

314 315 316 317 318 319 320 321 322
GenericMakeStepFactory::GenericMakeStepFactory(QObject *parent) :
    ProjectExplorer::IBuildStepFactory(parent)
{
}

GenericMakeStepFactory::~GenericMakeStepFactory()
{
}

Tobias Hunger's avatar
Tobias Hunger committed
323
bool GenericMakeStepFactory::canCreate(ProjectExplorer::BuildStepList *parent,
324
                                       const Core::Id id) const
325
{
326
    if (parent->target()->project()->id() != Core::Id(Constants::GENERICPROJECT_ID))
327
        return false;
328
    return id == Core::Id(GENERIC_MS_ID);
329 330
}

Tobias Hunger's avatar
Tobias Hunger committed
331
ProjectExplorer::BuildStep *GenericMakeStepFactory::create(ProjectExplorer::BuildStepList *parent,
332
                                                           const Core::Id id)
333
{
Tobias Hunger's avatar
Tobias Hunger committed
334
    if (!canCreate(parent, id))
335
        return 0;
336
    GenericMakeStep *step = new GenericMakeStep(parent);
337
    if (parent->id() == Core::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN)) {
338
        step->setClean(true);
339
        step->setBuildTarget(QLatin1String("clean"), /* on = */ true);
340
    } else if (parent->id() == Core::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD)) {
341
        step->setBuildTarget(QLatin1String("all"), /* on = */ true);
342 343
    }
    return step;
344 345
}

Tobias Hunger's avatar
Tobias Hunger committed
346
bool GenericMakeStepFactory::canClone(ProjectExplorer::BuildStepList *parent,
347 348
                                      ProjectExplorer::BuildStep *source) const
{
349
    return canCreate(parent, source->id());
350 351
}

Tobias Hunger's avatar
Tobias Hunger committed
352
ProjectExplorer::BuildStep *GenericMakeStepFactory::clone(ProjectExplorer::BuildStepList *parent,
353
                                                          ProjectExplorer::BuildStep *source)
Roberto Raggi's avatar
Roberto Raggi committed
354
{
Tobias Hunger's avatar
Tobias Hunger committed
355
    if (!canClone(parent, source))
356 357 358 359
        return 0;
    GenericMakeStep *old(qobject_cast<GenericMakeStep *>(source));
    Q_ASSERT(old);
    return new GenericMakeStep(parent, old);
Roberto Raggi's avatar
Roberto Raggi committed
360 361
}

Tobias Hunger's avatar
Tobias Hunger committed
362
bool GenericMakeStepFactory::canRestore(ProjectExplorer::BuildStepList *parent,
363
                                        const QVariantMap &map) const
Roberto Raggi's avatar
Roberto Raggi committed
364
{
365
    return canCreate(parent, ProjectExplorer::idFromMap(map));
366 367
}

Tobias Hunger's avatar
Tobias Hunger committed
368
ProjectExplorer::BuildStep *GenericMakeStepFactory::restore(ProjectExplorer::BuildStepList *parent,
369
                                                            const QVariantMap &map)
370
{
Tobias Hunger's avatar
Tobias Hunger committed
371
    if (!canRestore(parent, map))
372 373 374 375 376 377
        return 0;
    GenericMakeStep *bs(new GenericMakeStep(parent));
    if (bs->fromMap(map))
        return bs;
    delete bs;
    return 0;
Roberto Raggi's avatar
Roberto Raggi committed
378 379
}

380
QList<Core::Id> GenericMakeStepFactory::availableCreationIds(ProjectExplorer::BuildStepList *parent) const
Roberto Raggi's avatar
Roberto Raggi committed
381
{
382 383 384
    if (parent->target()->project()->id() != Core::Id(Constants::GENERICPROJECT_ID))
        return QList<Core::Id>();
    return QList<Core::Id>() << Core::Id(GENERIC_MS_ID);
Roberto Raggi's avatar
Roberto Raggi committed
385 386
}

387
QString GenericMakeStepFactory::displayNameForId(const Core::Id id) const
Roberto Raggi's avatar
Roberto Raggi committed
388
{
389
    if (id == Core::Id(GENERIC_MS_ID))
390 391 392
        return QCoreApplication::translate("GenericProjectManager::Internal::GenericMakeStep",
                                           GENERIC_MS_DISPLAY_NAME);
    return QString();
Roberto Raggi's avatar
Roberto Raggi committed
393
}