genericmakestep.cpp 11.9 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) 2010 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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
**
** 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
26
** contact the sales department at http://qt.nokia.com/contact.
Roberto Raggi's avatar
Roberto Raggi committed
27 28 29
**
**************************************************************************/

30
#include "genericmakestep.h"
Roberto Raggi's avatar
Roberto Raggi committed
31 32
#include "genericprojectconstants.h"
#include "genericproject.h"
Tobias Hunger's avatar
Tobias Hunger committed
33
#include "generictarget.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>
39
#include <projectexplorer/toolchain.h>
40
#include <projectexplorer/projectexplorer.h>
Tobias Hunger's avatar
Tobias Hunger committed
41
#include <projectexplorer/gnumakeparser.h>
42
#include <coreplugin/variablemanager.h>
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
43
#include <utils/stringutils.h>
44
#include <utils/qtcassert.h>
45
#include <utils/qtcprocess.h>
46

Roberto Raggi's avatar
Roberto Raggi committed
47 48 49 50 51 52 53 54 55
#include <QtGui/QFormLayout>
#include <QtGui/QGroupBox>
#include <QtGui/QCheckBox>
#include <QtGui/QLineEdit>
#include <QtGui/QListWidget>

using namespace GenericProjectManager;
using namespace GenericProjectManager::Internal;

56 57 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");
}

Tobias Hunger's avatar
Tobias Hunger committed
66 67
GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent) :
    AbstractProcessStep(parent, QLatin1String(GENERIC_MS_ID))
68 69 70 71
{
    ctor();
}

Tobias Hunger's avatar
Tobias Hunger committed
72 73
GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent, const QString &id) :
    AbstractProcessStep(parent, id)
74 75 76 77
{
    ctor();
}

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

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

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

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

102
bool GenericMakeStep::init()
Roberto Raggi's avatar
Roberto Raggi committed
103
{
dt's avatar
dt committed
104
    GenericBuildConfiguration *bc = genericBuildConfiguration();
Roberto Raggi's avatar
Roberto Raggi committed
105

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

    setOutputParser(new ProjectExplorer::GnuMakeParser(pp->effectiveWorkingDirectory()));
Tobias Hunger's avatar
Tobias Hunger committed
115 116
    if (bc->genericTarget()->genericProject()->toolChain())
        appendOutputParser(bc->genericTarget()->genericProject()->toolChain()->outputParser());
Tobias Hunger's avatar
Tobias Hunger committed
117 118

    return AbstractProcessStep::init();
119 120
}

121
QVariantMap GenericMakeStep::toMap() const
122
{
123 124 125 126 127 128
    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;
129 130
}

131
bool GenericMakeStep::fromMap(const QVariantMap &map)
132
{
133
    m_buildTargets = map.value(QLatin1String(BUILD_TARGETS_KEY)).toStringList();
134
    m_makeArguments = map.value(QLatin1String(MAKE_ARGUMENTS_KEY)).toString();
135 136 137
    m_makeCommand = map.value(QLatin1String(MAKE_COMMAND_KEY)).toString();

    return BuildStep::fromMap(map);
138 139
}

140
QString GenericMakeStep::allArguments() const
141
{
142 143 144
    QString args = m_makeArguments;
    Utils::QtcProcess::addArgs(&args, m_buildTargets);
    return args;
145
}
146

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

160
void GenericMakeStep::run(QFutureInterface<bool> &fi)
Roberto Raggi's avatar
Roberto Raggi committed
161 162 163 164
{
    AbstractProcessStep::run(fi);
}

165
ProjectExplorer::BuildStepConfigWidget *GenericMakeStep::createConfigWidget()
Roberto Raggi's avatar
Roberto Raggi committed
166
{
167
    return new GenericMakeStepConfigWidget(this);
Roberto Raggi's avatar
Roberto Raggi committed
168 169
}

170
bool GenericMakeStep::immutable() const
Roberto Raggi's avatar
Roberto Raggi committed
171
{
172
    return false;
Roberto Raggi's avatar
Roberto Raggi committed
173 174
}

175
bool GenericMakeStep::buildsTarget(const QString &target) const
Roberto Raggi's avatar
Roberto Raggi committed
176
{
177
    return m_buildTargets.contains(target);
Roberto Raggi's avatar
Roberto Raggi committed
178 179
}

180
void GenericMakeStep::setBuildTarget(const QString &target, bool on)
Roberto Raggi's avatar
Roberto Raggi committed
181
{
182
    QStringList old = m_buildTargets;
Roberto Raggi's avatar
Roberto Raggi committed
183
    if (on && !old.contains(target))
184
         old << target;
Roberto Raggi's avatar
Roberto Raggi committed
185
    else if(!on && old.contains(target))
186
        old.removeOne(target);
187

188
    m_buildTargets = old;
Roberto Raggi's avatar
Roberto Raggi committed
189 190 191
}

//
192
// GenericMakeStepConfigWidget
Roberto Raggi's avatar
Roberto Raggi committed
193
//
194 195

GenericMakeStepConfigWidget::GenericMakeStepConfigWidget(GenericMakeStep *makeStep)
Roberto Raggi's avatar
Roberto Raggi committed
196 197
    : m_makeStep(makeStep)
{
198 199
    m_ui = new Ui::GenericMakeStep;
    m_ui->setupUi(this);
Roberto Raggi's avatar
Roberto Raggi committed
200 201

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

209 210
    connect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)),
            this, SLOT(itemChanged(QListWidgetItem*)));
211
    connect(m_ui->makeLineEdit, SIGNAL(textEdited(QString)),
212
            this, SLOT(makeLineEditTextEdited()));
213
    connect(m_ui->makeArgumentsLineEdit, SIGNAL(textEdited(QString)),
214
            this, SLOT(makeArgumentsLineEditTextEdited()));
215 216 217 218 219

    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
220 221
}

222
QString GenericMakeStepConfigWidget::displayName() const
Roberto Raggi's avatar
Roberto Raggi committed
223
{
224
    return tr("Make", "GenericMakestep display name.");
Roberto Raggi's avatar
Roberto Raggi committed
225 226
}

227 228 229
// TODO: Label should update when tool chain is changed
void GenericMakeStepConfigWidget::updateMakeOverrrideLabel()
{
230
    m_ui->makeLabel->setText(tr("Override %1:").arg(m_makeStep->makeCommand()));
231 232
}

233
void GenericMakeStepConfigWidget::init()
Roberto Raggi's avatar
Roberto Raggi committed
234
{
235
    updateMakeOverrrideLabel();
236

237 238
    m_ui->makeLineEdit->setText(m_makeStep->m_makeCommand);
    m_ui->makeArgumentsLineEdit->setText(m_makeStep->m_makeArguments);
239 240 241 242 243 244 245

    // 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);
246
        item->setCheckState(m_makeStep->buildsTarget(item->text()) ? Qt::Checked : Qt::Unchecked);
Roberto Raggi's avatar
Roberto Raggi committed
247
    }
248

249
    updateDetails();
Roberto Raggi's avatar
Roberto Raggi committed
250
    // and connect again
251 252 253
    connect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
}

254 255
void GenericMakeStepConfigWidget::updateDetails()
{
256 257 258 259 260 261 262 263 264
    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());
265 266 267
    emit updateSummary();
}

dt's avatar
dt committed
268 269
QString GenericMakeStepConfigWidget::summaryText() const
{
270
    return m_summaryText;
dt's avatar
dt committed
271 272
}

273 274
void GenericMakeStepConfigWidget::itemChanged(QListWidgetItem *item)
{
275
    m_makeStep->setBuildTarget(item->text(), item->checkState() & Qt::Checked);
276
    updateDetails();
277 278 279 280
}

void GenericMakeStepConfigWidget::makeLineEditTextEdited()
{
281
    m_makeStep->m_makeCommand = m_ui->makeLineEdit->text();
282
    updateDetails();
283 284 285 286
}

void GenericMakeStepConfigWidget::makeArgumentsLineEditTextEdited()
{
287
    m_makeStep->m_makeArguments = m_ui->makeArgumentsLineEdit->text();
288
    updateDetails();
Roberto Raggi's avatar
Roberto Raggi committed
289 290 291
}

//
292
// GenericMakeStepFactory
Roberto Raggi's avatar
Roberto Raggi committed
293 294
//

295 296 297 298 299 300 301 302 303
GenericMakeStepFactory::GenericMakeStepFactory(QObject *parent) :
    ProjectExplorer::IBuildStepFactory(parent)
{
}

GenericMakeStepFactory::~GenericMakeStepFactory()
{
}

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

Tobias Hunger's avatar
Tobias Hunger committed
312
ProjectExplorer::BuildStep *GenericMakeStepFactory::create(ProjectExplorer::BuildStepList *parent,
313 314
                                                           const QString &id)
{
Tobias Hunger's avatar
Tobias Hunger committed
315
    if (!canCreate(parent, id))
316 317 318 319
        return 0;
    return new GenericMakeStep(parent);
}

Tobias Hunger's avatar
Tobias Hunger committed
320
bool GenericMakeStepFactory::canClone(ProjectExplorer::BuildStepList *parent,
321 322 323
                                      ProjectExplorer::BuildStep *source) const
{
    const QString id(source->id());
Tobias Hunger's avatar
Tobias Hunger committed
324
    return canCreate(parent, id);
325 326
}

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

Tobias Hunger's avatar
Tobias Hunger committed
337
bool GenericMakeStepFactory::canRestore(ProjectExplorer::BuildStepList *parent,
338
                                        const QVariantMap &map) const
Roberto Raggi's avatar
Roberto Raggi committed
339
{
340
    QString id(ProjectExplorer::idFromMap(map));
Tobias Hunger's avatar
Tobias Hunger committed
341
    return canCreate(parent, id);
342 343
}

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

Tobias Hunger's avatar
Tobias Hunger committed
356
QStringList GenericMakeStepFactory::availableCreationIds(ProjectExplorer::BuildStepList *parent) const
Roberto Raggi's avatar
Roberto Raggi committed
357
{
Tobias Hunger's avatar
Tobias Hunger committed
358
    if (parent->target()->project()->id() != QLatin1String(Constants::GENERICPROJECT_ID))
359 360
        return QStringList();
    return QStringList() << QLatin1String(GENERIC_MS_ID);
Roberto Raggi's avatar
Roberto Raggi committed
361 362
}

363
QString GenericMakeStepFactory::displayNameForId(const QString &id) const
Roberto Raggi's avatar
Roberto Raggi committed
364
{
365 366 367 368
    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
369
}