qmakestep.cpp 16.7 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
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).
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

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

41
42
#include "qt-s60/s60manager.h"

con's avatar
con committed
43
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
44
#include <utils/qtcassert.h>
con's avatar
con committed
45

Tobias Hunger's avatar
Tobias Hunger committed
46
47
#include <QtCore/QDir>
#include <QtCore/QFile>
con's avatar
con committed
48
49
50
51
52

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

53
54
55
56
57
58
59
60
61
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
62
{
63
    ctor();
con's avatar
con committed
64
65
}

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

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

void QMakeStep::ctor()
{
83
    setDisplayName(tr("qmake", "QMakeStep display name."));
84
85
}

con's avatar
con committed
86
87
88
89
QMakeStep::~QMakeStep()
{
}

dt's avatar
dt committed
90
91
92
93
94
Qt4BuildConfiguration *QMakeStep::qt4BuildConfiguration() const
{
    return static_cast<Qt4BuildConfiguration *>(buildConfiguration());
}

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

106
    if (!additonalArguments.contains("-spec"))
107
        arguments << "-spec" << bc->qtVersion()->mkspec();
108

109
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
dt's avatar
dt committed
110
    ToolChain::ToolChainType type = bc->toolChainType();
ck's avatar
ck committed
111
112
113
114
    if (type == ToolChain::GCC_MAEMO)
        arguments << QLatin1String("-unix");
#endif

115
116
117
118
119
120
121
122
123
124
125
    // 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
126
127
128
129
130
131
132
    }
    if (!additonalArguments.isEmpty())
        arguments << additonalArguments;

    return arguments;
}

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

    if (!qtVersion->isValid()) {
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
139
#if defined(Q_WS_MAC)
140
141
        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
142
#else
143
144
        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
145
#endif
146
        emit addOutput("<font color=\"#ff0000\">" + qtVersion->invalidReason() + "</font><br>");
con's avatar
con committed
147
148
149
        return false;
    }

dt's avatar
dt committed
150
    QStringList args = allArguments();
151
152
153
154
155
    QString workingDirectory;
    if (qt4bc->subNodeBuild())
        workingDirectory = qt4bc->subNodeBuild()->buildDir();
    else
        workingDirectory = qt4bc->buildDirectory();
con's avatar
con committed
156
157
158

    QString program = qtVersion->qmakeCommand();

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

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

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

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

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

191
192
193
194
195
196
197
198
    // Warn on common error conditions:
    if (qt4BuildConfiguration()->qt4Target()->id() == Constants::S60_DEVICE_TARGET_ID ||
        qt4BuildConfiguration()->qt4Target()->id() == Constants::S60_EMULATOR_TARGET_ID)
    {
        QtVersion *qtVersion = qt4BuildConfiguration()->qtVersion();

        const QString projectDir = QDir(qt4BuildConfiguration()->qt4Target()->qt4Project()->projectDirectory()).absolutePath();
        const QString epocRootDir = QDir(Internal::S60Manager::instance()->deviceForQtVersion(qtVersion).epocRoot).absolutePath();
199
        QFileInfo cppheader(epocRootDir + QLatin1String("/epoc32/include/stdapis/string.h"));
200
201
#if defined (Q_OS_WIN)
        // Report an error if project- and epoc directory are on different drives:
202
        if (!epocRootDir.startsWith(projectDir.left(3), Qt::CaseInsensitive)) {
203
204
205
206
207
208
209
            addTask(Task(Task::Error,
                         tr("The Symbian SDK and the project sources must reside on the same drive."),
                         QString(), -1, ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
            fi.reportResult(false);
            return;
        }
#endif
210
        // Report an error if EPOC root is not set:
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
        if (epocRootDir.isEmpty() || !QDir(epocRootDir).exists()) {
            addTask(Task(Task::Error,
                         tr("The Symbian SDK was not found for Qt version %1.").arg(qtVersion->displayName()),
                         QString(), -1, ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
            fi.reportResult(false);
            return;
        }
        if (!cppheader.exists()) {
            addTask(Task(Task::Error,
                         tr("The \"Open C/C++ plugin\" is not installed in the Symbian SDK or the Symbian SDK path is misconfigured for Qt version %1.").arg(qtVersion->displayName()),
                         QString(), -1, ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
            fi.reportResult(false);
            return;
        }
        // Warn of strange characters in project name:
226
227
228
229
230
231
        QString projectPath = projectDir;
#if defined (Q_OS_WIN)
        if (projectPath.at(1) == QChar(':') && projectPath.at(0).toUpper() >= QChar('A') && projectPath.at(0).toUpper() <= QChar('Z'))
            projectPath = projectPath.mid(2);
#endif
        if (projectPath.contains(QRegExp("[^a-zA-Z0-9./]"))) {
232
233
234
235
236
237
            addTask(Task(Task::Warning,
                         tr("The Symbian toolchain does not handle special characters in a project path well."),
                         QString(), -1, ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
        }
    }

238
    if (!m_needToRunQMake) {
239
        emit addOutput(tr("<font color=\"#0000ff\">Configuration unchanged, skipping qmake step.</font>"));
con's avatar
con committed
240
241
242
        fi.reportResult(true);
        return;
    }
243

Tobias Hunger's avatar
Tobias Hunger committed
244
    AbstractProcessStep::run(fi);
con's avatar
con committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
}

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
264
    return false;
con's avatar
con committed
265
266
267
268
269
270
271
272
}

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

273
bool QMakeStep::processSucceeded(int exitCode, QProcess::ExitStatus status)
con's avatar
con committed
274
{
275
    bool result = AbstractProcessStep::processSucceeded(exitCode, status);
con's avatar
con committed
276
277
    if (!result)
        m_forced = true;
278
    qt4BuildConfiguration()->emitBuildDirectoryInitialized();
con's avatar
con committed
279
280
281
    return result;
}

282
void QMakeStep::setUserArguments(const QStringList &arguments)
283
{
284
285
    if (m_userArgs == arguments)
        return;
dt's avatar
dt committed
286
    m_userArgs = arguments;
287

288
    emit userArgumentsChanged();
289
290

    qt4BuildConfiguration()->emitQMakeBuildConfigurationChanged();
291
292
}

dt's avatar
dt committed
293
QStringList QMakeStep::userArguments()
294
{
dt's avatar
dt committed
295
    return m_userArgs;
296
297
}

298
QVariantMap QMakeStep::toMap() const
299
{
300
301
302
    QVariantMap map(AbstractProcessStep::toMap());
    map.insert(QLatin1String(QMAKE_ARGUMENTS_KEY), m_userArgs);
    return map;
303
304
}

305
bool QMakeStep::fromMap(const QVariantMap &map)
306
{
307
308
309
    m_userArgs = map.value(QLatin1String(QMAKE_ARGUMENTS_KEY)).toStringList();

    return BuildStep::fromMap(map);
310
311
}

312
313
314
315
////
// QMakeStepConfigWidget
////

con's avatar
con committed
316
QMakeStepConfigWidget::QMakeStepConfigWidget(QMakeStep *step)
317
    : BuildStepConfigWidget(), m_step(step), m_ignoreChange(false)
con's avatar
con committed
318
319
320
{
    m_ui.setupUi(this);
    connect(m_ui.qmakeAdditonalArgumentsLineEdit, SIGNAL(textEdited(const QString&)),
321
322
323
            this, SLOT(qmakeArgumentsLineEdited()));
    connect(m_ui.buildConfigurationComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(buildConfigurationSelected()));
324
325
    connect(step, SIGNAL(userArgumentsChanged()),
            this, SLOT(userArgumentsChanged()));
326
    connect(step->qt4BuildConfiguration(), SIGNAL(qtVersionChanged()),
327
            this, SLOT(qtVersionChanged()));
328
329
330
331
332
333
334
335
336
337
338
339
340
    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
341
342
}

dt's avatar
dt committed
343
344
345
346
347
QString QMakeStepConfigWidget::summaryText() const
{
    return m_summaryText;
}

348
349
350
351
352
QString QMakeStepConfigWidget::displayName() const
{
    return m_step->displayName();
}

353
void QMakeStepConfigWidget::qtVersionChanged()
354
{
355
    updateSummaryLabel();
dt's avatar
dt committed
356
    updateEffectiveQMakeCall();
357
358
}

359
void QMakeStepConfigWidget::qmakeBuildConfigChanged()
dt's avatar
dt committed
360
{
361
362
363
364
365
366
367
368
    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
369

370
371
372
373
374
375
376
377
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
378
379
}

380
void QMakeStepConfigWidget::qmakeArgumentsLineEdited()
con's avatar
con committed
381
{
382
383
    m_ignoreChange = true;
    m_step->setUserArguments(
384
            ProjectExplorer::Environment::parseCombinedArgString(m_ui.qmakeAdditonalArgumentsLineEdit->text()));
385
    m_ignoreChange = false;
386

387
    updateSummaryLabel();
388
    updateEffectiveQMakeCall();
con's avatar
con committed
389
390
}

391
void QMakeStepConfigWidget::buildConfigurationSelected()
con's avatar
con committed
392
{
393
394
395
396
397
    if (m_ignoreChange)
        return;
    Qt4BuildConfiguration *bc = m_step->qt4BuildConfiguration();
    QtVersion::QmakeBuildConfigs buildConfiguration = bc->qmakeBuildConfiguration();
    if (m_ui.buildConfigurationComboBox->currentIndex() == 0) { // debug
398
        buildConfiguration = buildConfiguration | QtVersion::DebugBuild;
con's avatar
con committed
399
    } else {
400
        buildConfiguration = buildConfiguration & ~QtVersion::DebugBuild;
con's avatar
con committed
401
    }
402
403
404
    m_ignoreChange = true;
    bc->setQMakeBuildConfiguration(buildConfiguration);
    m_ignoreChange = false;
con's avatar
con committed
405

406
407
    updateSummaryLabel();
    updateEffectiveQMakeCall();
con's avatar
con committed
408
409
}

410
void QMakeStepConfigWidget::updateSummaryLabel()
411
{
412
413
414
    Qt4BuildConfiguration *qt4bc = m_step->qt4BuildConfiguration();
    const QtVersion *qtVersion = qt4bc->qtVersion();
    if (!qtVersion) {
Friedemann Kleint's avatar
Friedemann Kleint committed
415
        m_summaryText = tr("<b>qmake:</b> No Qt version set. Cannot run qmake.");
416
        emit updateSummary();
417
        return;
418
419
420
421
    }

    QStringList args = m_step->allArguments();
    // We don't want the full path to the .pro file
Tobias Hunger's avatar
Tobias Hunger committed
422
    const QString projectFileName = m_step->buildConfiguration()->target()->project()->file()->fileName();
423
424
425
426
427
428
    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();
429
    m_summaryText = tr("<b>qmake:</b> %1 %2").arg(program, args.join(QString(QLatin1Char(' '))));
430
    emit updateSummary();
431

432
433
434
435
}

void QMakeStepConfigWidget::updateEffectiveQMakeCall()
{
dt's avatar
dt committed
436
    Qt4BuildConfiguration *qt4bc = m_step->qt4BuildConfiguration();
437
    const QtVersion *qtVersion = qt4bc->qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
438
439
    QString program = QFileInfo(qtVersion->qmakeCommand()).fileName();
    m_ui.qmakeArgumentsEdit->setPlainText(program + QLatin1Char(' ') + ProjectExplorer::Environment::joinArgumentList(m_step->allArguments()));
con's avatar
con committed
440
441
}

dt's avatar
dt committed
442
443
444
445
////
// QMakeStepFactory
////

446
447
QMakeStepFactory::QMakeStepFactory(QObject *parent) :
    ProjectExplorer::IBuildStepFactory(parent)
dt's avatar
dt committed
448
449
450
451
452
453
454
{
}

QMakeStepFactory::~QMakeStepFactory()
{
}

455
bool QMakeStepFactory::canCreate(BuildConfiguration *parent, ProjectExplorer::StepType type, const QString &id) const
dt's avatar
dt committed
456
{
457
458
    if (type != ProjectExplorer::Build)
        return false;
459
460
461
    if (!qobject_cast<Qt4BuildConfiguration *>(parent))
        return false;
    return (id == QLatin1String(QMAKE_BS_ID));
dt's avatar
dt committed
462
463
}

464
ProjectExplorer::BuildStep *QMakeStepFactory::create(BuildConfiguration *parent, ProjectExplorer::StepType type,const QString &id)
dt's avatar
dt committed
465
{
466
    if (!canCreate(parent, type, id))
467
468
469
        return 0;
    Qt4BuildConfiguration *bc(qobject_cast<Qt4BuildConfiguration *>(parent));
    Q_ASSERT(bc);
dt's avatar
dt committed
470
    return new QMakeStep(bc);
471
472
}

473
bool QMakeStepFactory::canClone(BuildConfiguration *parent, ProjectExplorer::StepType type, BuildStep *source) const
474
{
475
    return canCreate(parent, type, source->id());
dt's avatar
dt committed
476
477
}

478
ProjectExplorer::BuildStep *QMakeStepFactory::clone(ProjectExplorer::BuildConfiguration *parent, ProjectExplorer::StepType type, ProjectExplorer::BuildStep *source)
dt's avatar
dt committed
479
{
480
    if (!canClone(parent, type, source))
481
482
483
484
        return 0;
    Qt4BuildConfiguration *bc(qobject_cast<Qt4BuildConfiguration *>(parent));
    Q_ASSERT(bc);
    return new QMakeStep(bc, qobject_cast<QMakeStep *>(source));
dt's avatar
dt committed
485
486
}

487
bool QMakeStepFactory::canRestore(ProjectExplorer::BuildConfiguration *parent, ProjectExplorer::StepType type, const QVariantMap &map) const
488
489
{
    QString id(ProjectExplorer::idFromMap(map));
490
    return canCreate(parent, type, id);
491
492
}

493
ProjectExplorer::BuildStep *QMakeStepFactory::restore(ProjectExplorer::BuildConfiguration *parent, ProjectExplorer::StepType type, const QVariantMap &map)
dt's avatar
dt committed
494
{
495
    if (!canRestore(parent, type, map))
496
497
498
499
500
501
502
503
        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
504
505
}

506
QStringList QMakeStepFactory::availableCreationIds(ProjectExplorer::BuildConfiguration *parent, ProjectExplorer::StepType type) const
507
{
508
509
510
511
    if (type == Build)
        if (Qt4BuildConfiguration *bc = qobject_cast<Qt4BuildConfiguration *>(parent))
            if (!bc->qmakeStep())
                return QStringList() << QLatin1String(QMAKE_BS_ID);
512
513
    return QStringList();
}
dt's avatar
dt committed
514

515
516
517
QString QMakeStepFactory::displayNameForId(const QString &id) const
{
    if (id == QLatin1String(QMAKE_BS_ID))
518
        return tr("qmake");
519
520
    return QString();
}