genericmakestep.cpp 12.1 KB
Newer Older
Roberto Raggi's avatar
Roberto Raggi committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
Roberto Raggi's avatar
Roberto Raggi committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
Roberto Raggi's avatar
Roberto Raggi committed
8
**
con's avatar
con committed
9
** No Commercial Usage
Roberto Raggi's avatar
Roberto Raggi committed
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
Roberto Raggi's avatar
Roberto Raggi committed
15 16 17 18 19 20 21 22 23 24
**
** 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.
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
Roberto Raggi's avatar
Roberto Raggi committed
31 32 33
**
**************************************************************************/

34
#include "genericmakestep.h"
Roberto Raggi's avatar
Roberto Raggi committed
35 36
#include "genericprojectconstants.h"
#include "genericproject.h"
Tobias Hunger's avatar
Tobias Hunger committed
37
#include "generictarget.h"
38
#include "ui_genericmakestep.h"
39
#include "genericbuildconfiguration.h"
Roberto Raggi's avatar
Roberto Raggi committed
40

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

Roberto Raggi's avatar
Roberto Raggi committed
51 52 53 54 55 56 57 58 59
#include <QtGui/QFormLayout>
#include <QtGui/QGroupBox>
#include <QtGui/QCheckBox>
#include <QtGui/QLineEdit>
#include <QtGui/QListWidget>

using namespace GenericProjectManager;
using namespace GenericProjectManager::Internal;

60 61 62 63 64 65 66 67 68 69
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");
}

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

Tobias Hunger's avatar
Tobias Hunger committed
76 77
GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent, const QString &id) :
    AbstractProcessStep(parent, id)
78 79 80 81
{
    ctor();
}

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

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

97
GenericMakeStep::~GenericMakeStep()
Roberto Raggi's avatar
Roberto Raggi committed
98 99 100
{
}

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

106
bool GenericMakeStep::init()
Roberto Raggi's avatar
Roberto Raggi committed
107
{
dt's avatar
dt committed
108
    GenericBuildConfiguration *bc = genericBuildConfiguration();
Roberto Raggi's avatar
Roberto Raggi committed
109

110
    setEnabled(true);
111 112 113 114 115 116 117
    ProjectExplorer::ProcessParameters *pp = processParameters();
    pp->setMacroExpander(bc->macroExpander());
    pp->setWorkingDirectory(bc->buildDirectory());
    pp->setEnvironment(bc->environment());
    pp->setCommand(makeCommand());
    pp->setArguments(allArguments());

118
    setOutputParser(new ProjectExplorer::GnuMakeParser());
Tobias Hunger's avatar
Tobias Hunger committed
119 120
    if (bc->genericTarget()->genericProject()->toolChain())
        appendOutputParser(bc->genericTarget()->genericProject()->toolChain()->outputParser());
121
    outputParser()->setWorkingDirectory(pp->effectiveWorkingDirectory());
Tobias Hunger's avatar
Tobias Hunger committed
122 123

    return AbstractProcessStep::init();
124 125
}

126
QVariantMap GenericMakeStep::toMap() const
127
{
128 129 130 131 132 133
    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);
    return map;
134 135
}

136
bool GenericMakeStep::fromMap(const QVariantMap &map)
137
{
138
    m_buildTargets = map.value(QLatin1String(BUILD_TARGETS_KEY)).toStringList();
139
    m_makeArguments = map.value(QLatin1String(MAKE_ARGUMENTS_KEY)).toString();
140 141 142
    m_makeCommand = map.value(QLatin1String(MAKE_COMMAND_KEY)).toString();

    return BuildStep::fromMap(map);
143 144
}

145
QString GenericMakeStep::allArguments() const
146
{
147 148 149
    QString args = m_makeArguments;
    Utils::QtcProcess::addArgs(&args, m_buildTargets);
    return args;
150
}
151

152
QString GenericMakeStep::makeCommand() const
153
{
154
    QString command = m_makeCommand;
155
    if (command.isEmpty()) {
Tobias Hunger's avatar
Tobias Hunger committed
156
        GenericProject *pro = genericBuildConfiguration()->genericTarget()->genericProject();
dt's avatar
dt committed
157
        if (ProjectExplorer::ToolChain *toolChain = pro->toolChain())
158 159 160 161 162
            command = toolChain->makeCommand();
        else
            command = QLatin1String("make");
    }
    return command;
Roberto Raggi's avatar
Roberto Raggi committed
163 164
}

165
void GenericMakeStep::run(QFutureInterface<bool> &fi)
Roberto Raggi's avatar
Roberto Raggi committed
166 167 168 169
{
    AbstractProcessStep::run(fi);
}

170
ProjectExplorer::BuildStepConfigWidget *GenericMakeStep::createConfigWidget()
Roberto Raggi's avatar
Roberto Raggi committed
171
{
172
    return new GenericMakeStepConfigWidget(this);
Roberto Raggi's avatar
Roberto Raggi committed
173 174
}

175
bool GenericMakeStep::immutable() const
Roberto Raggi's avatar
Roberto Raggi committed
176
{
177
    return false;
Roberto Raggi's avatar
Roberto Raggi committed
178 179
}

180
bool GenericMakeStep::buildsTarget(const QString &target) const
Roberto Raggi's avatar
Roberto Raggi committed
181
{
182
    return m_buildTargets.contains(target);
Roberto Raggi's avatar
Roberto Raggi committed
183 184
}

185
void GenericMakeStep::setBuildTarget(const QString &target, bool on)
Roberto Raggi's avatar
Roberto Raggi committed
186
{
187
    QStringList old = m_buildTargets;
Roberto Raggi's avatar
Roberto Raggi committed
188
    if (on && !old.contains(target))
189
         old << target;
Roberto Raggi's avatar
Roberto Raggi committed
190
    else if(!on && old.contains(target))
191
        old.removeOne(target);
192

193
    m_buildTargets = old;
Roberto Raggi's avatar
Roberto Raggi committed
194 195 196
}

//
197
// GenericMakeStepConfigWidget
Roberto Raggi's avatar
Roberto Raggi committed
198
//
199 200

GenericMakeStepConfigWidget::GenericMakeStepConfigWidget(GenericMakeStep *makeStep)
Roberto Raggi's avatar
Roberto Raggi committed
201 202
    : m_makeStep(makeStep)
{
203 204
    m_ui = new Ui::GenericMakeStep;
    m_ui->setupUi(this);
Roberto Raggi's avatar
Roberto Raggi committed
205 206

    // TODO update this list also on rescans of the GenericLists.txt
Tobias Hunger's avatar
Tobias Hunger committed
207 208
    GenericProject *pro = m_makeStep->genericBuildConfiguration()->genericTarget()->genericProject();
    foreach (const QString &target, pro->buildTargets()) {
209
        QListWidgetItem *item = new QListWidgetItem(target, m_ui->targetsList);
Roberto Raggi's avatar
Roberto Raggi committed
210 211 212 213
        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
        item->setCheckState(Qt::Unchecked);
    }

214 215
    connect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)),
            this, SLOT(itemChanged(QListWidgetItem*)));
216
    connect(m_ui->makeLineEdit, SIGNAL(textEdited(QString)),
217
            this, SLOT(makeLineEditTextEdited()));
218
    connect(m_ui->makeArgumentsLineEdit, SIGNAL(textEdited(QString)),
219
            this, SLOT(makeArgumentsLineEditTextEdited()));
220 221 222 223 224

    connect(ProjectExplorer::ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
            this, SLOT(updateMakeOverrrideLabel()));
    connect(ProjectExplorer::ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
            this, SLOT(updateDetails()));
Roberto Raggi's avatar
Roberto Raggi committed
225 226
}

227
QString GenericMakeStepConfigWidget::displayName() const
Roberto Raggi's avatar
Roberto Raggi committed
228
{
229
    return tr("Make", "GenericMakestep display name.");
Roberto Raggi's avatar
Roberto Raggi committed
230 231
}

232 233 234
// TODO: Label should update when tool chain is changed
void GenericMakeStepConfigWidget::updateMakeOverrrideLabel()
{
235
    m_ui->makeLabel->setText(tr("Override %1:").arg(m_makeStep->makeCommand()));
236 237
}

238
void GenericMakeStepConfigWidget::init()
Roberto Raggi's avatar
Roberto Raggi committed
239
{
240
    updateMakeOverrrideLabel();
241

242 243
    m_ui->makeLineEdit->setText(m_makeStep->m_makeCommand);
    m_ui->makeArgumentsLineEdit->setText(m_makeStep->m_makeArguments);
244 245 246 247 248 249 250

    // Disconnect to make the changes to the items
    disconnect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));

    int count = m_ui->targetsList->count();
    for (int i = 0; i < count; ++i) {
        QListWidgetItem *item = m_ui->targetsList->item(i);
251
        item->setCheckState(m_makeStep->buildsTarget(item->text()) ? Qt::Checked : Qt::Unchecked);
Roberto Raggi's avatar
Roberto Raggi committed
252
    }
253

254
    updateDetails();
Roberto Raggi's avatar
Roberto Raggi committed
255
    // and connect again
256 257 258
    connect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
}

259 260
void GenericMakeStepConfigWidget::updateDetails()
{
261 262 263 264 265 266 267 268 269
    GenericBuildConfiguration *bc = m_makeStep->genericBuildConfiguration();

    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());
270 271 272
    emit updateSummary();
}

dt's avatar
dt committed
273 274
QString GenericMakeStepConfigWidget::summaryText() const
{
275
    return m_summaryText;
dt's avatar
dt committed
276 277
}

278 279
void GenericMakeStepConfigWidget::itemChanged(QListWidgetItem *item)
{
280
    m_makeStep->setBuildTarget(item->text(), item->checkState() & Qt::Checked);
281
    updateDetails();
282 283 284 285
}

void GenericMakeStepConfigWidget::makeLineEditTextEdited()
{
286
    m_makeStep->m_makeCommand = m_ui->makeLineEdit->text();
287
    updateDetails();
288 289 290 291
}

void GenericMakeStepConfigWidget::makeArgumentsLineEditTextEdited()
{
292
    m_makeStep->m_makeArguments = m_ui->makeArgumentsLineEdit->text();
293
    updateDetails();
Roberto Raggi's avatar
Roberto Raggi committed
294 295 296
}

//
297
// GenericMakeStepFactory
Roberto Raggi's avatar
Roberto Raggi committed
298 299
//

300 301 302 303 304 305 306 307 308
GenericMakeStepFactory::GenericMakeStepFactory(QObject *parent) :
    ProjectExplorer::IBuildStepFactory(parent)
{
}

GenericMakeStepFactory::~GenericMakeStepFactory()
{
}

Tobias Hunger's avatar
Tobias Hunger committed
309
bool GenericMakeStepFactory::canCreate(ProjectExplorer::BuildStepList *parent,
310
                                       const QString &id) const
311
{
Tobias Hunger's avatar
Tobias Hunger committed
312
    if (parent->target()->project()->id() != QLatin1String(Constants::GENERICPROJECT_ID))
313 314 315 316
        return false;
    return id == QLatin1String(GENERIC_MS_ID);
}

Tobias Hunger's avatar
Tobias Hunger committed
317
ProjectExplorer::BuildStep *GenericMakeStepFactory::create(ProjectExplorer::BuildStepList *parent,
318 319
                                                           const QString &id)
{
Tobias Hunger's avatar
Tobias Hunger committed
320
    if (!canCreate(parent, id))
321 322 323 324
        return 0;
    return new GenericMakeStep(parent);
}

Tobias Hunger's avatar
Tobias Hunger committed
325
bool GenericMakeStepFactory::canClone(ProjectExplorer::BuildStepList *parent,
326 327 328
                                      ProjectExplorer::BuildStep *source) const
{
    const QString id(source->id());
Tobias Hunger's avatar
Tobias Hunger committed
329
    return canCreate(parent, id);
330 331
}

Tobias Hunger's avatar
Tobias Hunger committed
332
ProjectExplorer::BuildStep *GenericMakeStepFactory::clone(ProjectExplorer::BuildStepList *parent,
333
                                                          ProjectExplorer::BuildStep *source)
Roberto Raggi's avatar
Roberto Raggi committed
334
{
Tobias Hunger's avatar
Tobias Hunger committed
335
    if (!canClone(parent, source))
336 337 338 339
        return 0;
    GenericMakeStep *old(qobject_cast<GenericMakeStep *>(source));
    Q_ASSERT(old);
    return new GenericMakeStep(parent, old);
Roberto Raggi's avatar
Roberto Raggi committed
340 341
}

Tobias Hunger's avatar
Tobias Hunger committed
342
bool GenericMakeStepFactory::canRestore(ProjectExplorer::BuildStepList *parent,
343
                                        const QVariantMap &map) const
Roberto Raggi's avatar
Roberto Raggi committed
344
{
345
    QString id(ProjectExplorer::idFromMap(map));
Tobias Hunger's avatar
Tobias Hunger committed
346
    return canCreate(parent, id);
347 348
}

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

Tobias Hunger's avatar
Tobias Hunger committed
361
QStringList GenericMakeStepFactory::availableCreationIds(ProjectExplorer::BuildStepList *parent) const
Roberto Raggi's avatar
Roberto Raggi committed
362
{
Tobias Hunger's avatar
Tobias Hunger committed
363
    if (parent->target()->project()->id() != QLatin1String(Constants::GENERICPROJECT_ID))
364 365
        return QStringList();
    return QStringList() << QLatin1String(GENERIC_MS_ID);
Roberto Raggi's avatar
Roberto Raggi committed
366 367
}

368
QString GenericMakeStepFactory::displayNameForId(const QString &id) const
Roberto Raggi's avatar
Roberto Raggi committed
369
{
370 371 372 373
    if (id == QLatin1String(GENERIC_MS_ID))
        return QCoreApplication::translate("GenericProjectManager::Internal::GenericMakeStep",
                                           GENERIC_MS_DISPLAY_NAME);
    return QString();
Roberto Raggi's avatar
Roberto Raggi committed
374
}