genericmakestep.cpp 11.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

hjk's avatar
hjk committed
49 50
using namespace Core;
using namespace ProjectExplorer;
Roberto Raggi's avatar
Roberto Raggi committed
51

hjk's avatar
hjk committed
52 53
namespace GenericProjectManager {
namespace Internal {
Roberto Raggi's avatar
Roberto Raggi committed
54

hjk's avatar
hjk committed
55 56 57
const char GENERIC_MS_ID[] = "GenericProjectManager.GenericMakeStep";
const char GENERIC_MS_DISPLAY_NAME[] = QT_TRANSLATE_NOOP("GenericProjectManager::Internal::GenericMakeStep",
                                                     "Make");
58

hjk's avatar
hjk committed
59 60 61 62
const char BUILD_TARGETS_KEY[] = "GenericProjectManager.GenericMakeStep.BuildTargets";
const char MAKE_ARGUMENTS_KEY[] = "GenericProjectManager.GenericMakeStep.MakeArguments";
const char MAKE_COMMAND_KEY[] = "GenericProjectManager.GenericMakeStep.MakeCommand";
const char CLEAN_KEY[] = "GenericProjectManager.GenericMakeStep.Clean";
63

hjk's avatar
hjk committed
64 65
GenericMakeStep::GenericMakeStep(BuildStepList *parent) :
    AbstractProcessStep(parent, Id(GENERIC_MS_ID)),
66
    m_clean(false)
67 68 69 70
{
    ctor();
}

hjk's avatar
hjk committed
71
GenericMakeStep::GenericMakeStep(BuildStepList *parent, const Id id) :
72 73
    AbstractProcessStep(parent, id),
    m_clean(false)
74 75 76 77
{
    ctor();
}

hjk's avatar
hjk committed
78
GenericMakeStep::GenericMakeStep(BuildStepList *parent, GenericMakeStep *bs) :
Tobias Hunger's avatar
Tobias Hunger committed
79
    AbstractProcessStep(parent, bs),
80 81
    m_buildTargets(bs->m_buildTargets),
    m_makeArguments(bs->m_makeArguments),
82 83
    m_makeCommand(bs->m_makeCommand),
    m_clean(bs->m_clean)
Roberto Raggi's avatar
Roberto Raggi committed
84
{
85
    ctor();
Roberto Raggi's avatar
Roberto Raggi committed
86 87
}

88
void GenericMakeStep::ctor()
dt's avatar
Compile  
dt committed
89
{
90 91
    setDefaultDisplayName(QCoreApplication::translate("GenericProjectManager::Internal::GenericMakeStep",
                                                      GENERIC_MS_DISPLAY_NAME));
dt's avatar
Compile  
dt committed
92 93
}

94
GenericMakeStep::~GenericMakeStep()
Roberto Raggi's avatar
Roberto Raggi committed
95 96 97
{
}

dt's avatar
dt committed
98 99 100 101 102
GenericBuildConfiguration *GenericMakeStep::genericBuildConfiguration() const
{
    return static_cast<GenericBuildConfiguration *>(buildConfiguration());
}

103
bool GenericMakeStep::init()
Roberto Raggi's avatar
Roberto Raggi committed
104
{
dt's avatar
dt committed
105
    GenericBuildConfiguration *bc = genericBuildConfiguration();
106 107
    if (!bc)
        bc = static_cast<GenericBuildConfiguration *>(target()->activeBuildConfiguration());
Roberto Raggi's avatar
Roberto Raggi committed
108

hjk's avatar
hjk committed
109
    ProcessParameters *pp = processParameters();
110 111 112 113 114 115
    pp->setMacroExpander(bc->macroExpander());
    pp->setWorkingDirectory(bc->buildDirectory());
    pp->setEnvironment(bc->environment());
    pp->setCommand(makeCommand());
    pp->setArguments(allArguments());

116 117 118 119 120
    // 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);

hjk's avatar
hjk committed
121 122
    setOutputParser(new GnuMakeParser());
    ToolChain *tc = ToolChainProfileInformation::toolChain(bc->target()->profile());
Tobias Hunger's avatar
Tobias Hunger committed
123 124
    if (tc)
        appendOutputParser(tc->outputParser());
125
    outputParser()->setWorkingDirectory(pp->effectiveWorkingDirectory());
Tobias Hunger's avatar
Tobias Hunger committed
126 127

    return AbstractProcessStep::init();
128 129
}

130 131 132 133 134 135 136 137 138 139
void GenericMakeStep::setClean(bool clean)
{
    m_clean = clean;
}

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

140
QVariantMap GenericMakeStep::toMap() const
141
{
142 143 144 145 146
    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);
147
    map.insert(QLatin1String(CLEAN_KEY), m_clean);
148
    return map;
149 150
}

151
bool GenericMakeStep::fromMap(const QVariantMap &map)
152
{
153
    m_buildTargets = map.value(QLatin1String(BUILD_TARGETS_KEY)).toStringList();
154
    m_makeArguments = map.value(QLatin1String(MAKE_ARGUMENTS_KEY)).toString();
155
    m_makeCommand = map.value(QLatin1String(MAKE_COMMAND_KEY)).toString();
156
    m_clean = map.value(QLatin1String(CLEAN_KEY)).toBool();
157 158

    return BuildStep::fromMap(map);
159 160
}

161
QString GenericMakeStep::allArguments() const
162
{
163 164 165
    QString args = m_makeArguments;
    Utils::QtcProcess::addArgs(&args, m_buildTargets);
    return args;
166
}
167

168
QString GenericMakeStep::makeCommand() const
169
{
170
    QString command = m_makeCommand;
171
    if (command.isEmpty()) {
hjk's avatar
hjk committed
172
        ToolChain *tc = ToolChainProfileInformation::toolChain(target()->profile());
Tobias Hunger's avatar
Tobias Hunger committed
173 174
        if (tc)
            command = tc->makeCommand();
175 176 177 178
        else
            command = QLatin1String("make");
    }
    return command;
Roberto Raggi's avatar
Roberto Raggi committed
179 180
}

181
void GenericMakeStep::run(QFutureInterface<bool> &fi)
Roberto Raggi's avatar
Roberto Raggi committed
182 183 184 185
{
    AbstractProcessStep::run(fi);
}

hjk's avatar
hjk committed
186
BuildStepConfigWidget *GenericMakeStep::createConfigWidget()
Roberto Raggi's avatar
Roberto Raggi committed
187
{
188
    return new GenericMakeStepConfigWidget(this);
Roberto Raggi's avatar
Roberto Raggi committed
189 190
}

191
bool GenericMakeStep::immutable() const
Roberto Raggi's avatar
Roberto Raggi committed
192
{
193
    return false;
Roberto Raggi's avatar
Roberto Raggi committed
194 195
}

196
bool GenericMakeStep::buildsTarget(const QString &target) const
Roberto Raggi's avatar
Roberto Raggi committed
197
{
198
    return m_buildTargets.contains(target);
Roberto Raggi's avatar
Roberto Raggi committed
199 200
}

201
void GenericMakeStep::setBuildTarget(const QString &target, bool on)
Roberto Raggi's avatar
Roberto Raggi committed
202
{
203
    QStringList old = m_buildTargets;
Roberto Raggi's avatar
Roberto Raggi committed
204
    if (on && !old.contains(target))
205
         old << target;
Roberto Raggi's avatar
Roberto Raggi committed
206
    else if(!on && old.contains(target))
207
        old.removeOne(target);
208

209
    m_buildTargets = old;
Roberto Raggi's avatar
Roberto Raggi committed
210 211 212
}

//
213
// GenericMakeStepConfigWidget
Roberto Raggi's avatar
Roberto Raggi committed
214
//
215 216

GenericMakeStepConfigWidget::GenericMakeStepConfigWidget(GenericMakeStep *makeStep)
Roberto Raggi's avatar
Roberto Raggi committed
217 218
    : m_makeStep(makeStep)
{
219 220
    m_ui = new Ui::GenericMakeStep;
    m_ui->setupUi(this);
Roberto Raggi's avatar
Roberto Raggi committed
221

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

dt_'s avatar
dt_ committed
229 230 231 232 233
    m_ui->makeLineEdit->setText(m_makeStep->m_makeCommand);
    m_ui->makeArgumentsLineEdit->setText(m_makeStep->m_makeArguments);
    updateMakeOverrrideLabel();
    updateDetails();

234 235
    connect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)),
            this, SLOT(itemChanged(QListWidgetItem*)));
236
    connect(m_ui->makeLineEdit, SIGNAL(textEdited(QString)),
237
            this, SLOT(makeLineEditTextEdited()));
238
    connect(m_ui->makeArgumentsLineEdit, SIGNAL(textEdited(QString)),
239
            this, SLOT(makeArgumentsLineEditTextEdited()));
240

hjk's avatar
hjk committed
241
    connect(ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
242
            this, SLOT(updateMakeOverrrideLabel()));
hjk's avatar
hjk committed
243
    connect(ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
244
            this, SLOT(updateDetails()));
245

Tobias Hunger's avatar
Tobias Hunger committed
246
    connect(m_makeStep->target(), SIGNAL(profileChanged()),
247
            this, SLOT(updateMakeOverrrideLabel()));
Roberto Raggi's avatar
Roberto Raggi committed
248 249
}

250 251 252 253 254
GenericMakeStepConfigWidget::~GenericMakeStepConfigWidget()
{
    delete m_ui;
}

255
QString GenericMakeStepConfigWidget::displayName() const
Roberto Raggi's avatar
Roberto Raggi committed
256
{
257
    return tr("Make", "GenericMakestep display name.");
Roberto Raggi's avatar
Roberto Raggi committed
258 259
}

260 261
void GenericMakeStepConfigWidget::updateMakeOverrrideLabel()
{
262
    m_ui->makeLabel->setText(tr("Override %1:").arg(m_makeStep->makeCommand()));
263 264
}

265 266
void GenericMakeStepConfigWidget::updateDetails()
{
267
    GenericBuildConfiguration *bc = m_makeStep->genericBuildConfiguration();
268 269
    if (!bc)
        bc = static_cast<GenericBuildConfiguration *>(m_makeStep->target()->activeBuildConfiguration());
270

hjk's avatar
hjk committed
271
    ProcessParameters param;
272 273 274 275 276 277
    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());
278 279 280
    emit updateSummary();
}

dt's avatar
dt committed
281 282
QString GenericMakeStepConfigWidget::summaryText() const
{
283
    return m_summaryText;
dt's avatar
dt committed
284 285
}

286 287
void GenericMakeStepConfigWidget::itemChanged(QListWidgetItem *item)
{
288
    m_makeStep->setBuildTarget(item->text(), item->checkState() & Qt::Checked);
289
    updateDetails();
290 291 292 293
}

void GenericMakeStepConfigWidget::makeLineEditTextEdited()
{
294
    m_makeStep->m_makeCommand = m_ui->makeLineEdit->text();
295
    updateDetails();
296 297 298 299
}

void GenericMakeStepConfigWidget::makeArgumentsLineEditTextEdited()
{
300
    m_makeStep->m_makeArguments = m_ui->makeArgumentsLineEdit->text();
301
    updateDetails();
Roberto Raggi's avatar
Roberto Raggi committed
302 303 304
}

//
305
// GenericMakeStepFactory
Roberto Raggi's avatar
Roberto Raggi committed
306 307
//

308
GenericMakeStepFactory::GenericMakeStepFactory(QObject *parent) :
hjk's avatar
hjk committed
309
    IBuildStepFactory(parent)
310 311 312
{
}

hjk's avatar
hjk committed
313
bool GenericMakeStepFactory::canCreate(BuildStepList *parent, const Id id) const
314
{
315 316 317
    if (parent->target()->project()->id() == Constants::GENERICPROJECT_ID)
        return id == GENERIC_MS_ID;
    return false;
318 319
}

hjk's avatar
hjk committed
320
BuildStep *GenericMakeStepFactory::create(BuildStepList *parent, const Id id)
321
{
Tobias Hunger's avatar
Tobias Hunger committed
322
    if (!canCreate(parent, id))
323
        return 0;
324
    GenericMakeStep *step = new GenericMakeStep(parent);
325
    if (parent->id() == ProjectExplorer::Constants::BUILDSTEPS_CLEAN) {
326
        step->setClean(true);
327
        step->setBuildTarget(QLatin1String("clean"), /* on = */ true);
328
    } else if (parent->id() == ProjectExplorer::Constants::BUILDSTEPS_BUILD) {
329
        step->setBuildTarget(QLatin1String("all"), /* on = */ true);
330 331
    }
    return step;
332 333
}

hjk's avatar
hjk committed
334
bool GenericMakeStepFactory::canClone(BuildStepList *parent, BuildStep *source) const
335
{
336
    return canCreate(parent, source->id());
337 338
}

hjk's avatar
hjk committed
339
BuildStep *GenericMakeStepFactory::clone(BuildStepList *parent, BuildStep *source)
Roberto Raggi's avatar
Roberto Raggi committed
340
{
Tobias Hunger's avatar
Tobias Hunger committed
341
    if (!canClone(parent, source))
342 343 344 345
        return 0;
    GenericMakeStep *old(qobject_cast<GenericMakeStep *>(source));
    Q_ASSERT(old);
    return new GenericMakeStep(parent, old);
Roberto Raggi's avatar
Roberto Raggi committed
346 347
}

hjk's avatar
hjk committed
348
bool GenericMakeStepFactory::canRestore(BuildStepList *parent, const QVariantMap &map) const
Roberto Raggi's avatar
Roberto Raggi committed
349
{
hjk's avatar
hjk committed
350
    return canCreate(parent, idFromMap(map));
351 352
}

hjk's avatar
hjk committed
353
BuildStep *GenericMakeStepFactory::restore(BuildStepList *parent, const QVariantMap &map)
354
{
Tobias Hunger's avatar
Tobias Hunger committed
355
    if (!canRestore(parent, map))
356 357 358 359 360 361
        return 0;
    GenericMakeStep *bs(new GenericMakeStep(parent));
    if (bs->fromMap(map))
        return bs;
    delete bs;
    return 0;
Roberto Raggi's avatar
Roberto Raggi committed
362 363
}

hjk's avatar
hjk committed
364
QList<Id> GenericMakeStepFactory::availableCreationIds(BuildStepList *parent) const
Roberto Raggi's avatar
Roberto Raggi committed
365
{
366
    if (parent->target()->project()->id() == Constants::GENERICPROJECT_ID)
hjk's avatar
hjk committed
367 368
        return QList<Id>() << Id(GENERIC_MS_ID);
    return QList<Id>();
Roberto Raggi's avatar
Roberto Raggi committed
369 370
}

hjk's avatar
hjk committed
371
QString GenericMakeStepFactory::displayNameForId(const Id id) const
Roberto Raggi's avatar
Roberto Raggi committed
372
{
373
    if (id == GENERIC_MS_ID)
374 375 376
        return QCoreApplication::translate("GenericProjectManager::Internal::GenericMakeStep",
                                           GENERIC_MS_DISPLAY_NAME);
    return QString();
Roberto Raggi's avatar
Roberto Raggi committed
377
}
hjk's avatar
hjk committed
378 379 380

} // namespace Internal
} // namespace GenericProjectManager