qt4runconfiguration.cpp 16.8 KB
Newer Older
hjk's avatar
hjk committed
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
hjk's avatar
hjk committed
9
** Commercial Usage
10
**
hjk's avatar
hjk committed
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
**
hjk's avatar
hjk committed
16
** GNU Lesser General Public License Usage
17
**
hjk's avatar
hjk committed
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
**
hjk's avatar
hjk committed
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
hjk's avatar
hjk committed
28
**************************************************************************/
hjk's avatar
hjk committed
29

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

hjk's avatar
hjk committed
32
#include "makestep.h"
con's avatar
con committed
33
34
#include "profilereader.h"
#include "qt4nodes.h"
hjk's avatar
hjk committed
35
#include "qt4project.h"
con's avatar
con committed
36
37
38
39
40

#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/variablemanager.h>
#include <projectexplorer/buildstep.h>
hjk's avatar
hjk committed
41
#include <utils/qtcassert.h>
con's avatar
con committed
42
43
44

#include <QtGui/QFormLayout>
#include <QtGui/QInputDialog>
45
#include <QtGui/QLabel>
con's avatar
con committed
46
47
48
49
50
51
52
53

using namespace Qt4ProjectManager::Internal;
using namespace Qt4ProjectManager;
using ProjectExplorer::ApplicationRunConfiguration;
using ProjectExplorer::PersistentSettingsReader;
using ProjectExplorer::PersistentSettingsWriter;

Qt4RunConfiguration::Qt4RunConfiguration(Qt4Project *pro, QString proFilePath)
54
55
56
57
58
59
    : ApplicationRunConfiguration(pro),
      m_proFilePath(proFilePath),
      m_userSetName(false),
      m_configWidget(0),
      m_executableLabel(0),
      m_workingDirectoryLabel(0)
con's avatar
con committed
60
61
62
63
64
65
{
    setName(tr("Qt4RunConfiguration"));
    if (!m_proFilePath.isEmpty()) {
        updateCachedValues();
        setName(QFileInfo(m_proFilePath).baseName());
    }
66
67
68
69
    connect(pro, SIGNAL(activeBuildConfigurationChanged()),
            this, SIGNAL(effectiveExecutableChanged()));
    connect(pro, SIGNAL(activeBuildConfigurationChanged()),
            this, SIGNAL(effectiveWorkingDirectoryChanged()));
con's avatar
con committed
70
71
72
73
}

Qt4RunConfiguration::~Qt4RunConfiguration()
{
74

con's avatar
con committed
75
76
77
78
79
80
81
}

QString Qt4RunConfiguration::type() const
{
    return "Qt4ProjectManager.Qt4RunConfiguration";
}

82
83
84
85
86
87
88

//////
/// Qt4RunConfigurationWidget
/////

Qt4RunConfigurationWidget::Qt4RunConfigurationWidget(Qt4RunConfiguration *qt4RunConfiguration, QWidget *parent)
    : QWidget(parent), m_qt4RunConfiguration(qt4RunConfiguration), m_ignoreChange(false)
con's avatar
con committed
89
{
90
    QFormLayout *toplayout = new QFormLayout(this);
con's avatar
con committed
91
92
93
    toplayout->setMargin(0);

    QLabel *nameLabel = new QLabel(tr("Name:"));
94
95
96
    m_nameLineEdit = new QLineEdit(m_qt4RunConfiguration->name());
    nameLabel->setBuddy(m_nameLineEdit);
    toplayout->addRow(nameLabel, m_nameLineEdit);
con's avatar
con committed
97

98
99
    m_executableLabel = new QLabel(m_qt4RunConfiguration->executable());
    toplayout->addRow(tr("Executable:"), m_executableLabel);
con's avatar
con committed
100

101
102
    m_workingDirectoryLabel = new QLabel(m_qt4RunConfiguration->workingDirectory());
    toplayout->addRow(tr("Working Directory:"), m_workingDirectoryLabel);
con's avatar
con committed
103
104

    QLabel *argumentsLabel = new QLabel(tr("&Arguments:"));
105
106
107
    m_argumentsLineEdit = new QLineEdit(ProjectExplorer::Environment::joinArgumentList(qt4RunConfiguration->commandLineArguments()));
    argumentsLabel->setBuddy(m_argumentsLineEdit);
    toplayout->addRow(argumentsLabel, m_argumentsLineEdit);
con's avatar
con committed
108

109
    connect(m_argumentsLineEdit, SIGNAL(textEdited(const QString&)),
con's avatar
con committed
110
111
            this, SLOT(setCommandLineArguments(const QString&)));

112
    connect(m_nameLineEdit, SIGNAL(textEdited(const QString&)),
con's avatar
con committed
113
114
            this, SLOT(nameEdited(const QString&)));

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    connect(qt4RunConfiguration, SIGNAL(commandLineArgumentsChanged(QString)),
            this, SLOT(commandLineArgumentsChanged(QString)));
    connect(qt4RunConfiguration, SIGNAL(nameChanged(QString)),
            this, SLOT(nameChanged(QString)));

    connect(qt4RunConfiguration, SIGNAL(effectiveExecutableChanged()),
            this, SLOT(effectiveExecutableChanged()));
    connect(qt4RunConfiguration, SIGNAL(effectiveWorkingDirectoryChanged()),
            this, SLOT(effectiveWorkingDirectoryChanged()));

}

void Qt4RunConfigurationWidget::setCommandLineArguments(const QString &args)
{
    m_ignoreChange = true;
    m_qt4RunConfiguration->setCommandLineArguments(args);
    m_ignoreChange = false;
}

void Qt4RunConfigurationWidget::nameEdited(const QString &name)
{
    m_ignoreChange = true;
    m_qt4RunConfiguration->nameEdited(name);
    m_ignoreChange = false;
}

void Qt4RunConfigurationWidget::commandLineArgumentsChanged(const QString &args)
{
    if (!m_ignoreChange)
        m_argumentsLineEdit->setText(args);
}

void Qt4RunConfigurationWidget::nameChanged(const QString &name)
{
    if (!m_ignoreChange)
        m_nameLineEdit->setText(name);
}

void Qt4RunConfigurationWidget::effectiveExecutableChanged()
{
    m_executableLabel->setText(m_qt4RunConfiguration->executable());
}

void Qt4RunConfigurationWidget::effectiveWorkingDirectoryChanged()
{
    m_workingDirectoryLabel->setText(m_qt4RunConfiguration->workingDirectory());
}

////// TODO c&p above

QWidget *Qt4RunConfiguration::configurationWidget()
{
    return new Qt4RunConfigurationWidget(this, 0);
con's avatar
con committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
}

void Qt4RunConfiguration::save(PersistentSettingsWriter &writer) const
{
    writer.saveValue("CommandLineArguments", m_commandLineArguments);
    writer.saveValue("ProFile", m_proFilePath);
    writer.saveValue("UserSetName", m_userSetName);
    ApplicationRunConfiguration::save(writer);
}

void Qt4RunConfiguration::restore(const PersistentSettingsReader &reader)
{
    ApplicationRunConfiguration::restore(reader);
    m_commandLineArguments = reader.restoreValue("CommandLineArguments").toStringList();
    m_proFilePath = reader.restoreValue("ProFile").toString();
    m_userSetName = reader.restoreValue("UserSetName").toBool();
    if (!m_proFilePath.isEmpty()) {
        updateCachedValues();
        if (!m_userSetName)
            setName(QFileInfo(m_proFilePath).baseName());
    }
}

QString Qt4RunConfiguration::executable() const
{
    return resolveVariables(project()->activeBuildConfiguration(), m_executable);
}

ApplicationRunConfiguration::RunMode Qt4RunConfiguration::runMode() const
{
    return m_runMode;
}

QString Qt4RunConfiguration::workingDirectory() const
{
    return resolveVariables(project()->activeBuildConfiguration(), m_workingDir);
}

QStringList Qt4RunConfiguration::commandLineArguments() const
{
    return m_commandLineArguments;
}

ProjectExplorer::Environment Qt4RunConfiguration::environment() const
{
    Qt4Project *pro = qobject_cast<Qt4Project *>(project());
dt's avatar
dt committed
214
    Q_ASSERT(pro);
con's avatar
con committed
215
216
217
218
219
220
    return pro->environment(pro->activeBuildConfiguration());
}

void Qt4RunConfiguration::setCommandLineArguments(const QString &argumentsString)
{
    m_commandLineArguments = ProjectExplorer::Environment::parseCombinedArgString(argumentsString);
221
    emit commandLineArgumentsChanged(argumentsString);
con's avatar
con committed
222
223
224
225
226
227
228
229
230
231
232
}

void Qt4RunConfiguration::nameEdited(const QString &name)
{
    if (name == "") {
        setName(tr("Qt4RunConfiguration"));
        m_userSetName = false;
    } else {
        setName(name);
        m_userSetName = true;
    }
233
    emit nameChanged(name);
con's avatar
con committed
234
235
236
237
238
239
240
241
242
243
244
245
}

QString Qt4RunConfiguration::proFilePath() const
{
    return m_proFilePath;
}

// and needs to be reloaded.
// Check wheter it is
void Qt4RunConfiguration::updateCachedValues()
{
    ProFileReader *reader = static_cast<Qt4Project *>(project())->createProFileReader();
246
    reader->setCumulative(false);
con's avatar
con committed
247
248
    if (!reader->readProFile(m_proFilePath)) {
        delete reader;
249
        Core::ICore::instance()->messageManager()->printToOutputPane(QString("Could not parse %1. The Qt4 run configuration %2 can not be started.").arg(m_proFilePath).arg(name()));
con's avatar
con committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
        return;
    }

    QString destDir;

    if (reader->contains("DESTDIR")) {
        // TODO Can return different destdirs for different scopes!
        destDir = reader->value("DESTDIR");
        if (QDir::isRelativePath(destDir)) {
            destDir = "${BASEDIR}" + QLatin1Char('/') + destDir;
        }
    } else {
        destDir = "${BASEDIR}";
#if defined(Q_OS_WIN)
        if (!reader->contains("DESTDIR"))
            destDir += QLatin1Char('/') + "${QMAKE_BUILDCONFIG}";
#endif
    }

#if defined (Q_OS_MAC)
    if (!reader->values("-CONFIG").contains("app_bundle")) {
        destDir += QLatin1Char('/')
                   + "${QMAKE_TARGET}"
                   + QLatin1String(".app/Contents/MacOS");
    }
#endif
    m_workingDir = destDir;
    m_executable = destDir + QLatin1Char('/') + "${QMAKE_TARGET}";

#if defined (Q_OS_WIN)
    m_executable += QLatin1String(".exe");
#endif

    m_targets = reader->values(QLatin1String("TARGET"));

    m_srcDir = QFileInfo(m_proFilePath).path();
    const QStringList config = reader->values(QLatin1String("CONFIG"));
    m_runMode = ProjectExplorer::ApplicationRunConfiguration::Gui;

    delete reader;
290
291
292

    emit effectiveExecutableChanged();
    emit effectiveWorkingDirectoryChanged();
con's avatar
con committed
293
294
295
296
297
298
299
300
301
}

QString Qt4RunConfiguration::resolveVariables(const QString &buildConfiguration, const QString& in) const
{
    detectQtShadowBuild(buildConfiguration);
    
    QString relSubDir = QFileInfo(project()->file()->fileName()).absoluteDir().relativeFilePath(m_srcDir);
    QString baseDir = QDir(project()->buildDirectory(buildConfiguration)).absoluteFilePath(relSubDir);

302
    Core::VariableManager *vm = Core::ICore::instance()->variableManager();
con's avatar
con committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    if (!vm)
        return QString();
    QString dest;
    bool found = false;
    vm->insert("QMAKE_BUILDCONFIG", qmakeBuildConfigFromBuildConfiguration(buildConfiguration));
    vm->insert("BASEDIR", baseDir);


    /*
      TODO This is a hack to detect correct target (there might be different targets in
      different scopes)
    */

    // This code also works for workingDirectory,
    // since directories are executable.
    foreach (const QString &target, m_targets) {
        dest = in;
        vm->insert("QMAKE_TARGET", target);
        dest = QDir::cleanPath(vm->resolve(dest));
        vm->remove("QMAKE_TARGET");
        QFileInfo fi(dest);
        if (fi.exists() && (fi.isExecutable() || dest.endsWith(".js"))) {
            found = true;
            break;
        }
    }
    vm->remove("BASEDIR");
    vm->remove("QMAKE_BUILDCONFIG");
    if (found)
        return dest;
    else
        return QString();
}

/* This function tries to find out wheter qmake/make will put the binary in "/debug/" or in "/release/"
   That is this function is strictly only for windows.
   We look wheter make gets an explicit parameter "debug" or "release"
   That works because if we have either debug or release there then it is surely a
   debug_and_release buildconfiguration and thus we are put in a subdirectory.

   Now if there is no explicit debug or release parameter, then we need to look at what qmake's CONFIG
   value is, if it is not debug_and_release, we don't care and return ""
   otherwise we look at wheter the default is debug or not

   Note: When fixing this function consider those cases
       qmake CONFIG+=debug_and_release CONFIG+=debug
       make release
    => we should return release

        qmake CONFIG+=debug_and_release CONFIG+=debug
        make
    => we should return debug

        qmake CONFIG-=debug_and_release CONFIG+=debug
        make
    => we should return "", since the executable is not put in a subdirectory

   Not a function to be proud of
*/
QString Qt4RunConfiguration::qmakeBuildConfigFromBuildConfiguration(const QString &buildConfigurationName) const
{
    MakeStep *ms = qobject_cast<Qt4Project *>(project())->makeStep();
    QStringList makeargs = ms->value(buildConfigurationName, "makeargs").toStringList();
    if (makeargs.contains("debug"))
        return "debug";
hjk's avatar
hjk committed
368
    else if (makeargs.contains("release"))
con's avatar
con committed
369
370
371
372
373
374
375
        return "release";

    // Oh we don't have an explicit make argument
    QMakeStep *qs = qobject_cast<Qt4Project *>(project())->qmakeStep();
    QVariant qmakeBuildConfiguration = qs->value(buildConfigurationName, "buildConfiguration");
    if (qmakeBuildConfiguration.isValid()) {
        QtVersion::QmakeBuildConfig projectBuildConfiguration = QtVersion::QmakeBuildConfig(qmakeBuildConfiguration.toInt());
376
377
378
379
        if (projectBuildConfiguration & QtVersion::DebugBuild)
            return "debug";
        else
            return "release";
con's avatar
con committed
380
    } else {
381
        // Old style always CONFIG+=debug_and_release
con's avatar
con committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
        if (qobject_cast<Qt4Project *>(project())->qtVersion(buildConfigurationName)->defaultBuildConfig() & QtVersion::DebugBuild)
            return "debug";
        else
            return "release";
    }

    // enable us to infer the right string
    return "";
}

/*!
  Handle special case were a subproject of the qt directory is opened, and
  qt was configured to be built as a shadow build -> also build in the sub-
  project in the correct shadow build directory.
  */
void Qt4RunConfiguration::detectQtShadowBuild(const QString &buildConfiguration) const
{
    if (project()->activeBuildConfiguration() == buildConfiguration)
        return;

    const QString currentQtDir = static_cast<Qt4Project *>(project())->qtDir(buildConfiguration);
    const QString qtSourceDir = static_cast<Qt4Project *>(project())->qtVersion(buildConfiguration)->sourcePath();

    // if the project is a sub-project of Qt and Qt was shadow-built then automatically
    // adjust the build directory of the sub-project.
    if (project()->file()->fileName().startsWith(qtSourceDir) && qtSourceDir != currentQtDir) {
        project()->setValue(buildConfiguration, "useShadowBuild", true);
        QString buildDir = QFileInfo(project()->file()->fileName()).absolutePath();
        buildDir.replace(qtSourceDir, currentQtDir);
        project()->setValue(buildConfiguration, "buildDirectory", buildDir);
        project()->setValue(buildConfiguration, "autoShadowBuild", true);
    }
}


///
/// Qt4RunConfigurationFactory
/// This class is used to restore run settings (saved in .user files)
///

Qt4RunConfigurationFactory::Qt4RunConfigurationFactory()
{
}

Qt4RunConfigurationFactory::~Qt4RunConfigurationFactory()
{
}

// used to recreate the runConfigurations when restoring settings
bool Qt4RunConfigurationFactory::canCreate(const QString &type) const
{
    return type == "Qt4ProjectManager.Qt4RunConfiguration";
}

hjk's avatar
hjk committed
436
437
QSharedPointer<ProjectExplorer::RunConfiguration> Qt4RunConfigurationFactory::create
    (ProjectExplorer::Project *project, const QString &type)
con's avatar
con committed
438
439
{
    Qt4Project *p = qobject_cast<Qt4Project *>(project);
dt's avatar
dt committed
440
441
    Q_ASSERT(p);
    Q_ASSERT(type == "Qt4ProjectManager.Qt4RunConfiguration");
con's avatar
con committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
    // The right path is set in restoreSettings
    QSharedPointer<ProjectExplorer::RunConfiguration> rc(new Qt4RunConfiguration(p, QString::null));
    return rc;
}

QStringList Qt4RunConfigurationFactory::canCreate(ProjectExplorer::Project *pro) const
{
    Qt4Project *qt4project = qobject_cast<Qt4Project *>(pro);
    if (qt4project)
        return QStringList();
    else
        return QStringList();
}

QString Qt4RunConfigurationFactory::nameForType(const QString &type) const
{
    Q_UNUSED(type);
    return "Run Qt4 application";
}

///
/// Qt4RunConfigurationFactoryUser
/// This class is used to create new RunConfiguration from the runsettings page
///

Qt4RunConfigurationFactoryUser::Qt4RunConfigurationFactoryUser()
{
}

Qt4RunConfigurationFactoryUser::~Qt4RunConfigurationFactoryUser()
{
}

bool Qt4RunConfigurationFactoryUser::canCreate(const QString &type) const
{
    Q_UNUSED(type);
    return false;
}

QSharedPointer<ProjectExplorer::RunConfiguration> Qt4RunConfigurationFactoryUser::create(ProjectExplorer::Project *project, const QString &type)
{
    Qt4Project *p = qobject_cast<Qt4Project *>(project);
dt's avatar
dt committed
484
    Q_ASSERT(p);
con's avatar
con committed
485
486
487
488
489
490
491
492
493
494
495

    QString fileName = type.mid(QString("Qt4RunConfiguration.").size());
    return QSharedPointer<ProjectExplorer::RunConfiguration>(new Qt4RunConfiguration(p, fileName));
}

QStringList Qt4RunConfigurationFactoryUser::canCreate(ProjectExplorer::Project *pro) const
{
    Qt4Project *qt4project = qobject_cast<Qt4Project *>(pro);
    if (qt4project) {
        QStringList applicationProFiles;
        QList<Qt4ProFileNode *> list = qt4project->applicationProFiles();
hjk's avatar
hjk committed
496
        foreach (Qt4ProFileNode * node, list) {
con's avatar
con committed
497
498
499
500
501
502
503
504
505
506
507
508
509
            applicationProFiles.append("Qt4RunConfiguration." + node->path());
        }
        return applicationProFiles;
    } else {
        return QStringList();
    }
}

QString Qt4RunConfigurationFactoryUser::nameForType(const QString &type) const
{
    QString fileName = type.mid(QString("Qt4RunConfiguration.").size());
    return QFileInfo(fileName).baseName();
}