genericmakestep.cpp 12 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
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
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.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
Roberto Raggi's avatar
Roberto Raggi committed
30 31 32
**
**************************************************************************/

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

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

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

using namespace GenericProjectManager;
using namespace GenericProjectManager::Internal;

59 60 61 62 63 64 65 66 67 68
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
69 70
GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent) :
    AbstractProcessStep(parent, QLatin1String(GENERIC_MS_ID))
71 72 73 74
{
    ctor();
}

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

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

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

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

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

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

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

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

    return AbstractProcessStep::init();
123 124
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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
224 225
}

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

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

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

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

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

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

258 259
void GenericMakeStepConfigWidget::updateDetails()
{
260 261 262 263 264 265 266 267 268
    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());
269 270 271
    emit updateSummary();
}

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

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

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

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

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

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

GenericMakeStepFactory::~GenericMakeStepFactory()
{
}

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

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

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

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

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

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

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

367
QString GenericMakeStepFactory::displayNameForId(const QString &id) const
Roberto Raggi's avatar
Roberto Raggi committed
368
{
369 370 371 372
    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
373
}