qmakestep.cpp 13.8 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** 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.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "qmakestep.h"
hjk's avatar
hjk committed
31

Tobias Hunger's avatar
Tobias Hunger committed
32
33
#include "qmakeparser.h"
#include "qt4buildconfiguration.h"
con's avatar
con committed
34
35
36
#include "qt4project.h"
#include "qt4projectmanagerconstants.h"
#include "qt4projectmanager.h"
Tobias Hunger's avatar
Tobias Hunger committed
37
#include "qt4target.h"
38
#include "qtversionmanager.h"
con's avatar
con committed
39
40

#include <coreplugin/icore.h>
hjk's avatar
hjk committed
41
#include <utils/qtcassert.h>
con's avatar
con committed
42

Tobias Hunger's avatar
Tobias Hunger committed
43
44
#include <QtCore/QDir>
#include <QtCore/QFile>
con's avatar
con committed
45
46
47
48
49

using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;
using namespace ProjectExplorer;

50
51
52
53
54
55
56
57
58
namespace {
const char * const QMAKE_BS_ID("QtProjectManager.QMakeBuildStep");

const char * const QMAKE_ARGUMENTS_KEY("QtProjectManager.QMakeBuildStep.QMakeArguments");
}

QMakeStep::QMakeStep(Qt4BuildConfiguration *bc) :
    AbstractProcessStep(bc, QLatin1String(QMAKE_BS_ID)),
    m_forced(false)
con's avatar
con committed
59
{
60
    ctor();
con's avatar
con committed
61
62
}

63
64
65
66
67
68
69
70
71
QMakeStep::QMakeStep(Qt4BuildConfiguration *bc, const QString &id) :
    AbstractProcessStep(bc, id),
    m_forced(false)
{
    ctor();
}

QMakeStep::QMakeStep(Qt4BuildConfiguration *bc, QMakeStep *bs) :
    AbstractProcessStep(bc, bs),
72
    m_forced(false),
dt's avatar
dt committed
73
    m_userArgs(bs->m_userArgs)
74
{
75
76
77
78
79
    ctor();
}

void QMakeStep::ctor()
{
80
    setDisplayName(tr("qmake", "QMakeStep display name."));
81
82
}

con's avatar
con committed
83
84
85
86
QMakeStep::~QMakeStep()
{
}

dt's avatar
dt committed
87
88
89
90
91
Qt4BuildConfiguration *QMakeStep::qt4BuildConfiguration() const
{
    return static_cast<Qt4BuildConfiguration *>(buildConfiguration());
}

dt's avatar
dt committed
92
QStringList QMakeStep::allArguments()
con's avatar
con committed
93
{
dt's avatar
dt committed
94
    QStringList additonalArguments = m_userArgs;
dt's avatar
dt committed
95
    Qt4BuildConfiguration *bc = qt4BuildConfiguration();
con's avatar
con committed
96
    QStringList arguments;
97
98
99
    if (bc->subNodeBuild())
        arguments << bc->subNodeBuild()->path();
    else
Tobias Hunger's avatar
Tobias Hunger committed
100
        arguments << buildConfiguration()->target()->project()->file()->fileName();
con's avatar
con committed
101
102
    arguments << "-r";

103
    if (!additonalArguments.contains("-spec"))
104
        arguments << "-spec" << bc->qtVersion()->mkspec();
105

ck's avatar
ck committed
106
#ifdef Q_OS_WIN
dt's avatar
dt committed
107
    ToolChain::ToolChainType type = bc->toolChainType();
ck's avatar
ck committed
108
109
110
111
    if (type == ToolChain::GCC_MAEMO)
        arguments << QLatin1String("-unix");
#endif

112
113
114
115
116
117
118
119
120
121
122
    // Find out what flags we pass on to qmake
    QStringList addedUserConfigArguments;
    QStringList removedUserConfigArguments;
    bc->getConfigCommandLineArguments(&addedUserConfigArguments, &removedUserConfigArguments);
    if (!removedUserConfigArguments.isEmpty()) {
        foreach (const QString &removedConfig, removedUserConfigArguments)
            arguments.append("CONFIG-=" + removedConfig);
    }
    if (!addedUserConfigArguments.isEmpty()) {
        foreach (const QString &addedConfig, addedUserConfigArguments)
            arguments.append("CONFIG+=" + addedConfig);
con's avatar
con committed
123
124
125
126
127
128
129
    }
    if (!additonalArguments.isEmpty())
        arguments << additonalArguments;

    return arguments;
}

130
bool QMakeStep::init()
con's avatar
con committed
131
{
dt's avatar
dt committed
132
    Qt4BuildConfiguration *qt4bc = qt4BuildConfiguration();
133
    const QtVersion *qtVersion = qt4bc->qtVersion();
con's avatar
con committed
134
135

    if (!qtVersion->isValid()) {
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
136
#if defined(Q_WS_MAC)
137
138
        emit addOutput(tr("<font color=\"#ff0000\">Qt version <b>%1</b> is invalid. Set a valid Qt Version in Preferences </font>\n")
                       .arg(qtVersion->displayName()));
con's avatar
con committed
139
#else
140
141
        emit addOutput(tr("<font color=\"#ff0000\">Qt version <b>%1</b> is invalid. Set valid Qt Version in Tools/Options </b></font>\n")
                       .arg(qtVersion->displayName()));
con's avatar
con committed
142
#endif
143
        emit addOutput("<font color=\"#ff0000\">" + qtVersion->invalidReason() + "</font><br>");
con's avatar
con committed
144
145
146
        return false;
    }

dt's avatar
dt committed
147
    QStringList args = allArguments();
148
149
150
151
152
    QString workingDirectory;
    if (qt4bc->subNodeBuild())
        workingDirectory = qt4bc->subNodeBuild()->buildDir();
    else
        workingDirectory = qt4bc->buildDirectory();
con's avatar
con committed
153

154
    qDebug()<<"using working directory"<<workingDirectory<<"and args"<<args;
con's avatar
con committed
155
156
    QString program = qtVersion->qmakeCommand();

Tobias Hunger's avatar
Tobias Hunger committed
157
    // Check whether we need to run qmake
158
    m_needToRunQMake = true;
dt's avatar
dt committed
159
    if (QDir(workingDirectory).exists(QLatin1String("Makefile"))) {
160
161
        QString qmakePath = QtVersionManager::findQMakeBinaryFromMakefile(workingDirectory);
        if (qtVersion->qmakeCommand() == qmakePath) {
dt's avatar
dt committed
162
            m_needToRunQMake = !qt4bc->compareToImportFrom(workingDirectory);
dt's avatar
dt committed
163
        }
con's avatar
con committed
164
165
166
167
    }

    if (m_forced) {
        m_forced = false;
168
        m_needToRunQMake = true;
con's avatar
con committed
169
170
    }

171
172
173
174
    setEnabled(m_needToRunQMake);
    setWorkingDirectory(workingDirectory);
    setCommand(program);
    setArguments(args);
175
    setEnvironment(qt4bc->environment());
176

Tobias Hunger's avatar
Tobias Hunger committed
177
178
    setOutputParser(new QMakeParser);
    return AbstractProcessStep::init();
con's avatar
con committed
179
180
181
182
}

void QMakeStep::run(QFutureInterface<bool> &fi)
{
Tobias Hunger's avatar
Tobias Hunger committed
183
    Qt4Project *pro = qt4BuildConfiguration()->qt4Target()->qt4Project();
dt's avatar
dt committed
184
    if (pro->rootProjectNode()->projectType() == ScriptTemplate) {
con's avatar
con committed
185
186
187
        fi.reportResult(true);
        return;
    }
188

189
    if (!m_needToRunQMake) {
190
        emit addOutput(tr("<font color=\"#0000ff\">Configuration unchanged, skipping qmake step.</font>"));
con's avatar
con committed
191
192
193
        fi.reportResult(true);
        return;
    }
Tobias Hunger's avatar
Tobias Hunger committed
194
    AbstractProcessStep::run(fi);
con's avatar
con committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
}

void QMakeStep::setForced(bool b)
{
    m_forced = b;
}

bool QMakeStep::forced()
{
    return m_forced;
}

ProjectExplorer::BuildStepConfigWidget *QMakeStep::createConfigWidget()
{
    return new QMakeStepConfigWidget(this);
}

bool QMakeStep::immutable() const
{
dt's avatar
dt committed
214
    return false;
con's avatar
con committed
215
216
217
218
219
220
221
222
223
224
225
226
227
}

void QMakeStep::processStartupFailed()
{
    m_forced = true;
    AbstractProcessStep::processStartupFailed();
}

bool QMakeStep::processFinished(int exitCode, QProcess::ExitStatus status)
{
    bool result = AbstractProcessStep::processFinished(exitCode, status);
    if (!result)
        m_forced = true;
228
    qt4BuildConfiguration()->emitBuildDirectoryInitialized();
con's avatar
con committed
229
230
231
    return result;
}

232
void QMakeStep::setUserArguments(const QStringList &arguments)
233
{
234
235
    if (m_userArgs == arguments)
        return;
dt's avatar
dt committed
236
    m_userArgs = arguments;
237

238
    emit userArgumentsChanged();
239
240

    qt4BuildConfiguration()->emitQMakeBuildConfigurationChanged();
241
242
}

dt's avatar
dt committed
243
QStringList QMakeStep::userArguments()
244
{
dt's avatar
dt committed
245
    return m_userArgs;
246
247
}

248
QVariantMap QMakeStep::toMap() const
249
{
250
251
252
    QVariantMap map(AbstractProcessStep::toMap());
    map.insert(QLatin1String(QMAKE_ARGUMENTS_KEY), m_userArgs);
    return map;
253
254
}

255
bool QMakeStep::fromMap(const QVariantMap &map)
256
{
257
258
259
    m_userArgs = map.value(QLatin1String(QMAKE_ARGUMENTS_KEY)).toStringList();

    return BuildStep::fromMap(map);
260
261
}

262
263
264
265
////
// QMakeStepConfigWidget
////

con's avatar
con committed
266
QMakeStepConfigWidget::QMakeStepConfigWidget(QMakeStep *step)
267
    : BuildStepConfigWidget(), m_step(step), m_ignoreChange(false)
con's avatar
con committed
268
269
270
{
    m_ui.setupUi(this);
    connect(m_ui.qmakeAdditonalArgumentsLineEdit, SIGNAL(textEdited(const QString&)),
271
272
273
            this, SLOT(qmakeArgumentsLineEdited()));
    connect(m_ui.buildConfigurationComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(buildConfigurationSelected()));
274
275
    connect(step, SIGNAL(userArgumentsChanged()),
            this, SLOT(userArgumentsChanged()));
276
    connect(step->qt4BuildConfiguration(), SIGNAL(qtVersionChanged()),
277
            this, SLOT(qtVersionChanged()));
278
279
280
281
282
283
284
285
286
287
288
289
290
    connect(step->qt4BuildConfiguration(), SIGNAL(qmakeBuildConfigurationChanged()),
            this, SLOT(qmakeBuildConfigChanged()));
}

void QMakeStepConfigWidget::init()
{
    QString qmakeArgs = ProjectExplorer::Environment::joinArgumentList(m_step->userArguments());
    m_ui.qmakeAdditonalArgumentsLineEdit->setText(qmakeArgs);

    qmakeBuildConfigChanged();

    updateSummaryLabel();
    updateEffectiveQMakeCall();
con's avatar
con committed
291
292
}

dt's avatar
dt committed
293
294
295
296
297
QString QMakeStepConfigWidget::summaryText() const
{
    return m_summaryText;
}

298
299
300
301
302
QString QMakeStepConfigWidget::displayName() const
{
    return m_step->displayName();
}

303
void QMakeStepConfigWidget::qtVersionChanged()
304
{
305
    updateSummaryLabel();
dt's avatar
dt committed
306
    updateEffectiveQMakeCall();
307
308
}

309
void QMakeStepConfigWidget::qmakeBuildConfigChanged()
dt's avatar
dt committed
310
{
311
312
313
314
315
316
317
318
    Qt4BuildConfiguration *bc = m_step->qt4BuildConfiguration();
    bool debug = bc->qmakeBuildConfiguration() & QtVersion::DebugBuild;
    m_ignoreChange = true;
    m_ui.buildConfigurationComboBox->setCurrentIndex(debug? 0 : 1);
    m_ignoreChange = false;
    updateSummaryLabel();
    updateEffectiveQMakeCall();
}
dt's avatar
dt committed
319

320
321
322
323
324
325
326
327
void QMakeStepConfigWidget::userArgumentsChanged()
{
    if (m_ignoreChange)
        return;
    QString qmakeArgs = ProjectExplorer::Environment::joinArgumentList(m_step->userArguments());
    m_ui.qmakeAdditonalArgumentsLineEdit->setText(qmakeArgs);
    updateSummaryLabel();
    updateEffectiveQMakeCall();
dt's avatar
dt committed
328
329
}

330
void QMakeStepConfigWidget::qmakeArgumentsLineEdited()
con's avatar
con committed
331
{
332
333
    m_ignoreChange = true;
    m_step->setUserArguments(
334
            ProjectExplorer::Environment::parseCombinedArgString(m_ui.qmakeAdditonalArgumentsLineEdit->text()));
335
    m_ignoreChange = false;
336

337
    updateSummaryLabel();
338
    updateEffectiveQMakeCall();
con's avatar
con committed
339
340
}

341
void QMakeStepConfigWidget::buildConfigurationSelected()
con's avatar
con committed
342
{
343
344
345
346
347
    if (m_ignoreChange)
        return;
    Qt4BuildConfiguration *bc = m_step->qt4BuildConfiguration();
    QtVersion::QmakeBuildConfigs buildConfiguration = bc->qmakeBuildConfiguration();
    if (m_ui.buildConfigurationComboBox->currentIndex() == 0) { // debug
348
        buildConfiguration = buildConfiguration | QtVersion::DebugBuild;
con's avatar
con committed
349
    } else {
350
        buildConfiguration = buildConfiguration & ~QtVersion::DebugBuild;
con's avatar
con committed
351
    }
352
353
354
    m_ignoreChange = true;
    bc->setQMakeBuildConfiguration(buildConfiguration);
    m_ignoreChange = false;
con's avatar
con committed
355

356
357
    updateSummaryLabel();
    updateEffectiveQMakeCall();
con's avatar
con committed
358
359
}

360
void QMakeStepConfigWidget::updateSummaryLabel()
361
{
362
363
364
    Qt4BuildConfiguration *qt4bc = m_step->qt4BuildConfiguration();
    const QtVersion *qtVersion = qt4bc->qtVersion();
    if (!qtVersion) {
365
        m_summaryText = tr("<b>qmake:</b> No Qt version set. Can not be run qmake.");
366
        emit updateSummary();
367
        return;
368
369
370
371
    }

    QStringList args = m_step->allArguments();
    // We don't want the full path to the .pro file
Tobias Hunger's avatar
Tobias Hunger committed
372
    const QString projectFileName = m_step->buildConfiguration()->target()->project()->file()->fileName();
373
374
375
376
377
378
    int index = args.indexOf(projectFileName);
    if (index != -1)
        args[index] = QFileInfo(projectFileName).fileName();

    // And we only use the .pro filename not the full path
    QString program = QFileInfo(qtVersion->qmakeCommand()).fileName();
379
    m_summaryText = tr("<b>qmake:</b> %1 %2").arg(program, args.join(QString(QLatin1Char(' '))));
380
    emit updateSummary();
381

382
383
384
385
}

void QMakeStepConfigWidget::updateEffectiveQMakeCall()
{
dt's avatar
dt committed
386
    Qt4BuildConfiguration *qt4bc = m_step->qt4BuildConfiguration();
387
    const QtVersion *qtVersion = qt4bc->qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
388
389
    QString program = QFileInfo(qtVersion->qmakeCommand()).fileName();
    m_ui.qmakeArgumentsEdit->setPlainText(program + QLatin1Char(' ') + ProjectExplorer::Environment::joinArgumentList(m_step->allArguments()));
con's avatar
con committed
390
391
}

dt's avatar
dt committed
392
393
394
395
////
// QMakeStepFactory
////

396
397
QMakeStepFactory::QMakeStepFactory(QObject *parent) :
    ProjectExplorer::IBuildStepFactory(parent)
dt's avatar
dt committed
398
399
400
401
402
403
404
{
}

QMakeStepFactory::~QMakeStepFactory()
{
}

405
bool QMakeStepFactory::canCreate(BuildConfiguration *parent, const QString &id) const
dt's avatar
dt committed
406
{
407
408
409
    if (!qobject_cast<Qt4BuildConfiguration *>(parent))
        return false;
    return (id == QLatin1String(QMAKE_BS_ID));
dt's avatar
dt committed
410
411
}

412
ProjectExplorer::BuildStep *QMakeStepFactory::create(BuildConfiguration *parent, const QString &id)
dt's avatar
dt committed
413
{
414
415
416
417
    if (!canCreate(parent, id))
        return 0;
    Qt4BuildConfiguration *bc(qobject_cast<Qt4BuildConfiguration *>(parent));
    Q_ASSERT(bc);
dt's avatar
dt committed
418
    return new QMakeStep(bc);
419
420
}

421
bool QMakeStepFactory::canClone(BuildConfiguration *parent, BuildStep *source) const
422
{
423
    return canCreate(parent, source->id());
dt's avatar
dt committed
424
425
}

426
ProjectExplorer::BuildStep *QMakeStepFactory::clone(ProjectExplorer::BuildConfiguration *parent, ProjectExplorer::BuildStep *source)
dt's avatar
dt committed
427
{
428
429
430
431
432
    if (!canClone(parent, source))
        return 0;
    Qt4BuildConfiguration *bc(qobject_cast<Qt4BuildConfiguration *>(parent));
    Q_ASSERT(bc);
    return new QMakeStep(bc, qobject_cast<QMakeStep *>(source));
dt's avatar
dt committed
433
434
}

435
436
437
438
439
440
441
bool QMakeStepFactory::canRestore(ProjectExplorer::BuildConfiguration *parent, const QVariantMap &map) const
{
    QString id(ProjectExplorer::idFromMap(map));
    return canCreate(parent, id);
}

ProjectExplorer::BuildStep *QMakeStepFactory::restore(ProjectExplorer::BuildConfiguration *parent, const QVariantMap &map)
dt's avatar
dt committed
442
{
443
444
445
446
447
448
449
450
451
    if (!canRestore(parent, map))
        return 0;
    Qt4BuildConfiguration *bc(qobject_cast<Qt4BuildConfiguration *>(parent));
    Q_ASSERT(bc);
    QMakeStep *bs(new QMakeStep(bc));
    if (bs->fromMap(map))
        return bs;
    delete bs;
    return 0;
dt's avatar
dt committed
452
453
}

454
455
456
457
458
459
460
QStringList QMakeStepFactory::availableCreationIds(ProjectExplorer::BuildConfiguration *parent) const
{
    if (Qt4BuildConfiguration *bc = qobject_cast<Qt4BuildConfiguration *>(parent))
        if (!bc->qmakeStep())
            return QStringList() << QLatin1String(QMAKE_BS_ID);
    return QStringList();
}
dt's avatar
dt committed
461

462
463
464
QString QMakeStepFactory::displayNameForId(const QString &id) const
{
    if (id == QLatin1String(QMAKE_BS_ID))
465
        return tr("qmake");
466
467
    return QString();
}