buildconfiguration.cpp 15.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16
17
18
19
20
21
22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
hjk's avatar
hjk committed
25

con's avatar
con committed
26
27
#include "buildconfiguration.h"

28
#include "buildinfo.h"
Tobias Hunger's avatar
Tobias Hunger committed
29
30
#include "buildsteplist.h"
#include "projectexplorer.h"
Tobias Hunger's avatar
Tobias Hunger committed
31
#include "kitmanager.h"
Tobias Hunger's avatar
Tobias Hunger committed
32
#include "target.h"
33
#include "project.h"
34
#include "kit.h"
Tobias Hunger's avatar
Tobias Hunger committed
35

36
#include <projectexplorer/buildenvironmentwidget.h>
37
38
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/projectexplorerconstants.h>
39
#include <projectexplorer/projectmacroexpander.h>
40
#include <projectexplorer/target.h>
Tobias Hunger's avatar
Tobias Hunger committed
41
#include <extensionsystem/pluginmanager.h>
42
#include <coreplugin/idocument.h>
43

44
#include <utils/qtcassert.h>
45
#include <utils/macroexpander.h>
46
#include <utils/algorithm.h>
47
48
#include <utils/mimetypes/mimetype.h>
#include <utils/mimetypes/mimedatabase.h>
49

Tobias Hunger's avatar
Tobias Hunger committed
50
51
#include <QDebug>

52
53
54
55
static const char BUILD_STEP_LIST_COUNT[] = "ProjectExplorer.BuildConfiguration.BuildStepListCount";
static const char BUILD_STEP_LIST_PREFIX[] = "ProjectExplorer.BuildConfiguration.BuildStepList.";
static const char CLEAR_SYSTEM_ENVIRONMENT_KEY[] = "ProjectExplorer.BuildConfiguration.ClearSystemEnvironment";
static const char USER_ENVIRONMENT_CHANGES_KEY[] = "ProjectExplorer.BuildConfiguration.UserEnvironmentChanges";
56
static const char BUILDDIRECTORY_KEY[] = "ProjectExplorer.BuildConfiguration.BuildDirectory";
con's avatar
con committed
57

58
namespace ProjectExplorer {
59

60
61
BuildConfiguration::BuildConfiguration(Target *target, Core::Id id)
    : ProjectConfiguration(target, id)
62
63
64
65
{
    Utils::MacroExpander *expander = macroExpander();
    expander->setDisplayName(tr("Build Settings"));
    expander->setAccumulating(true);
66
    expander->registerSubProvider([target] { return target->macroExpander(); });
67
68
69
70

    expander->registerVariable("buildDir", tr("Build directory"),
            [this] { return buildDirectory().toUserOutput(); });

71
    expander->registerVariable(Constants::VAR_CURRENTBUILD_NAME, tr("Name of current build"),
72
            [this] { return displayName(); }, false);
73
74
75
76

    expander->registerPrefix(Constants::VAR_CURRENTBUILD_ENV,
                             tr("Variables in the current build environment"),
                             [this](const QString &var) { return environment().value(var); });
77
78
79
80
81
82

    updateCacheAndEmitEnvironmentChanged();
    connect(target, &Target::kitChanged,
            this, &BuildConfiguration::handleKitUpdate);
    connect(this, &BuildConfiguration::environmentChanged,
            this, &BuildConfiguration::emitBuildDirectoryChanged);
83
84
}

85
86
Utils::FileName BuildConfiguration::buildDirectory() const
{
87
    const QString path = macroExpander()->expand(QDir::cleanPath(environment().expandVariables(m_buildDirectory.toString())));
88
    return Utils::FileName::fromString(QDir::cleanPath(QDir(target()->project()->projectDirectory().toString()).absoluteFilePath(path)));
89
90
91
92
93
94
95
96
97
}

Utils::FileName BuildConfiguration::rawBuildDirectory() const
{
    return m_buildDirectory;
}

void BuildConfiguration::setBuildDirectory(const Utils::FileName &dir)
{
98
99
    if (dir == m_buildDirectory)
        return;
100
101
102
103
    m_buildDirectory = dir;
    emitBuildDirectoryChanged();
}

104
105
106
107
108
109
110
111
112
113
void BuildConfiguration::initialize(const BuildInfo *info)
{
    setDisplayName(info->displayName);
    setDefaultDisplayName(info->displayName);
    setBuildDirectory(info->buildDirectory);

    m_stepLists.append(new BuildStepList(this, Constants::BUILDSTEPS_BUILD));
    m_stepLists.append(new BuildStepList(this, Constants::BUILDSTEPS_CLEAN));
}

114
QList<NamedWidget *> BuildConfiguration::createSubConfigWidgets()
115
{
116
    return QList<NamedWidget *>() << new BuildEnvironmentWidget(this);
117
118
}

119
QList<Core::Id> BuildConfiguration::knownStepLists() const
120
{
121
    return Utils::transform(m_stepLists, &BuildStepList::id);
Tobias Hunger's avatar
Tobias Hunger committed
122
123
}

124
BuildStepList *BuildConfiguration::stepList(Core::Id id) const
Tobias Hunger's avatar
Tobias Hunger committed
125
{
126
    return Utils::findOrDefault(m_stepLists, Utils::equal(&BuildStepList::id, id));
con's avatar
con committed
127
128
}

129
QVariantMap BuildConfiguration::toMap() const
con's avatar
con committed
130
{
131
    QVariantMap map(ProjectConfiguration::toMap());
132
    map.insert(QLatin1String(CLEAR_SYSTEM_ENVIRONMENT_KEY), m_clearSystemEnvironment);
133
    map.insert(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY), Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
134
    map.insert(QLatin1String(BUILDDIRECTORY_KEY), m_buildDirectory.toString());
con's avatar
con committed
135

Tobias Hunger's avatar
Tobias Hunger committed
136
137
    map.insert(QLatin1String(BUILD_STEP_LIST_COUNT), m_stepLists.count());
    for (int i = 0; i < m_stepLists.count(); ++i)
Tobias Hunger's avatar
Tobias Hunger committed
138
        map.insert(QLatin1String(BUILD_STEP_LIST_PREFIX) + QString::number(i), m_stepLists.at(i)->toMap());
con's avatar
con committed
139

Tobias Hunger's avatar
Tobias Hunger committed
140
    return map;
Tobias Hunger's avatar
Tobias Hunger committed
141
}
142
143

bool BuildConfiguration::fromMap(const QVariantMap &map)
con's avatar
con committed
144
{
145
    m_clearSystemEnvironment = map.value(QLatin1String(CLEAR_SYSTEM_ENVIRONMENT_KEY)).toBool();
146
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(map.value(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY)).toStringList());
147
    m_buildDirectory = Utils::FileName::fromString(map.value(QLatin1String(BUILDDIRECTORY_KEY)).toString());
148

149
    updateCacheAndEmitEnvironmentChanged();
150

Tobias Hunger's avatar
Tobias Hunger committed
151
152
    qDeleteAll(m_stepLists);
    m_stepLists.clear();
153

Tobias Hunger's avatar
Tobias Hunger committed
154
155
    int maxI = map.value(QLatin1String(BUILD_STEP_LIST_COUNT), 0).toInt();
    for (int i = 0; i < maxI; ++i) {
Tobias Hunger's avatar
Tobias Hunger committed
156
        QVariantMap data = map.value(QLatin1String(BUILD_STEP_LIST_PREFIX) + QString::number(i)).toMap();
Tobias Hunger's avatar
Tobias Hunger committed
157
158
159
160
        if (data.isEmpty()) {
            qWarning() << "No data for build step list" << i << "found!";
            continue;
        }
161
        auto list = new BuildStepList(this, idFromMap(data));
hjk's avatar
hjk committed
162
        if (!list->fromMap(data)) {
Tobias Hunger's avatar
Tobias Hunger committed
163
164
165
166
167
168
            qWarning() << "Failed to restore build step list" << i;
            delete list;
            return false;
        }
        m_stepLists.append(list);
    }
169

170
    // We currently assume there to be at least a clean and build list!
171
172
    QTC_CHECK(knownStepLists().contains(Core::Id(Constants::BUILDSTEPS_BUILD)));
    QTC_CHECK(knownStepLists().contains(Core::Id(Constants::BUILDSTEPS_CLEAN)));
173

174
    return ProjectConfiguration::fromMap(map);
175
176
}

177
void BuildConfiguration::updateCacheAndEmitEnvironmentChanged()
178
{
179
180
181
182
183
    Utils::Environment env = baseEnvironment();
    env.modify(userEnvironmentChanges());
    if (env == m_cachedEnvironment)
        return;
    m_cachedEnvironment = env;
184
    emit environmentChanged(); // might trigger buildDirectoryChanged signal!
185
186
}

187
void BuildConfiguration::handleKitUpdate()
188
{
189
    updateCacheAndEmitEnvironmentChanged();
190
191
}

192
193
194
195
196
197
198
199
void BuildConfiguration::emitBuildDirectoryChanged()
{
    if (buildDirectory() != m_lastEmmitedBuildDirectory) {
        m_lastEmmitedBuildDirectory = buildDirectory();
        emit buildDirectoryChanged();
    }
}

Tobias Hunger's avatar
Tobias Hunger committed
200
Target *BuildConfiguration::target() const
201
{
Tobias Hunger's avatar
Tobias Hunger committed
202
    return static_cast<Target *>(parent());
203
204
}

205
206
207
208
209
Project *BuildConfiguration::project() const
{
    return target()->project();
}

210
Utils::Environment BuildConfiguration::baseEnvironment() const
211
{
212
    Utils::Environment result;
213
    if (useSystemEnvironment())
214
        result = Utils::Environment::systemEnvironment();
215
    addToEnvironment(result);
216
    target()->kit()->addToEnvironment(result);
217
    return result;
218
219
220
221
222
223
224
225
226
227
}

QString BuildConfiguration::baseEnvironmentText() const
{
    if (useSystemEnvironment())
        return tr("System Environment");
    else
        return tr("Clean Environment");
}

228
Utils::Environment BuildConfiguration::environment() const
229
{
230
    return m_cachedEnvironment;
231
232
233
234
235
236
237
}

void BuildConfiguration::setUseSystemEnvironment(bool b)
{
    if (useSystemEnvironment() == b)
        return;
    m_clearSystemEnvironment = !b;
238
    updateCacheAndEmitEnvironmentChanged();
239
240
}

241
242
243
244
245
void BuildConfiguration::addToEnvironment(Utils::Environment &env) const
{
    Q_UNUSED(env);
}

246
247
248
249
250
bool BuildConfiguration::useSystemEnvironment() const
{
    return !m_clearSystemEnvironment;
}

251
QList<Utils::EnvironmentItem> BuildConfiguration::userEnvironmentChanges() const
252
253
254
255
{
    return m_userEnvironmentChanges;
}

256
void BuildConfiguration::setUserEnvironmentChanges(const QList<Utils::EnvironmentItem> &diff)
257
258
259
260
{
    if (m_userEnvironmentChanges == diff)
        return;
    m_userEnvironmentChanges = diff;
261
    updateCacheAndEmitEnvironmentChanged();
262
263
}

264
265
266
267
268
bool BuildConfiguration::isEnabled() const
{
    return true;
}

dt_'s avatar
dt_ committed
269
270
271
272
273
QString BuildConfiguration::disabledReason() const
{
    return QString();
}

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
QString BuildConfiguration::buildTypeName(BuildConfiguration::BuildType type)
{
    switch (type) {
    case ProjectExplorer::BuildConfiguration::Debug:
        return QLatin1String("debug");
    case ProjectExplorer::BuildConfiguration::Profile:
        return QLatin1String("profile");
    case ProjectExplorer::BuildConfiguration::Release:
        return QLatin1String("release");
    case ProjectExplorer::BuildConfiguration::Unknown: // fallthrough
    default:
        return QLatin1String("unknown");
    }
}

289
290
291
292
293
bool BuildConfiguration::isActive() const
{
    return target()->isActive() && target()->activeBuildConfiguration() == this;
}

294
295
296
297
298
299
/*!
 * Helper function that prepends the directory containing the C++ toolchain to
 * PATH. This is used to in build configurations targeting broken build systems
 * to provide hints about which compiler to use.
 */
void BuildConfiguration::prependCompilerPathToEnvironment(Utils::Environment &env) const
300
301
302
303
304
{
    return prependCompilerPathToEnvironment(target()->kit(), env);
}

void BuildConfiguration::prependCompilerPathToEnvironment(Kit *k, Utils::Environment &env)
305
306
{
    const ToolChain *tc
307
            = ToolChainKitInformation::toolChain(k, ProjectExplorer::Constants::CXX_LANGUAGE_ID);
308
309
310
311
312
313
314
315
316

    if (!tc)
        return;

    const Utils::FileName compilerDir = tc->compilerCommand().parentDir();
    if (!compilerDir.isEmpty())
        env.prependOrSetPath(compilerDir.toString());
}

317
318
319
///
// IBuildConfigurationFactory
///
320

321
322
323
324
int IBuildConfigurationFactory::priority(const Target *parent) const
{
    return canHandle(parent) ? 0 : -1;
}
325

326
327
328
329
330
331
332
int IBuildConfigurationFactory::priority(const Kit *k, const QString &projectPath) const
{
    QTC_ASSERT(!m_supportedProjectMimeTypeName.isEmpty(), return -1);
    if (k && Utils::mimeTypeForFile(projectPath).matchesName(m_supportedProjectMimeTypeName))
        return 0;
    return -1;
}
333

334
// restore
Tobias Hunger's avatar
Tobias Hunger committed
335
336
IBuildConfigurationFactory *IBuildConfigurationFactory::find(Target *parent, const QVariantMap &map)
{
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
    QList<IBuildConfigurationFactory *> factories
            = ExtensionSystem::PluginManager::getObjects<IBuildConfigurationFactory>(
                [&parent, map](IBuildConfigurationFactory *factory) {
                    return factory->canRestore(parent, map);
                });

    IBuildConfigurationFactory *factory = 0;
    int priority = -1;
    foreach (IBuildConfigurationFactory *i, factories) {
        int iPriority = i->priority(parent);
        if (iPriority > priority) {
            factory = i;
            priority = iPriority;
        }
    }
    return factory;
Tobias Hunger's avatar
Tobias Hunger committed
353
354
}

355
// setup
356
IBuildConfigurationFactory *IBuildConfigurationFactory::find(const Kit *k, const QString &projectPath)
357
358
359
{
    QList<IBuildConfigurationFactory *> factories
            = ExtensionSystem::PluginManager::instance()->getObjects<IBuildConfigurationFactory>();
360
361
362
363
364
365
366
367
368
369
    IBuildConfigurationFactory *factory = 0;
    int priority = -1;
    foreach (IBuildConfigurationFactory *i, factories) {
        int iPriority = i->priority(k, projectPath);
        if (iPriority > priority) {
            factory = i;
            priority = iPriority;
        }
    }
    return factory;
370
371
}

372
// create
Tobias Hunger's avatar
Tobias Hunger committed
373
374
375
IBuildConfigurationFactory * IBuildConfigurationFactory::find(Target *parent)
{
    QList<IBuildConfigurationFactory *> factories
376
377
378
379
380
381
382
383
384
385
386
            = ExtensionSystem::PluginManager::instance()->getObjects<IBuildConfigurationFactory>();
    IBuildConfigurationFactory *factory = 0;
    int priority = -1;
    foreach (IBuildConfigurationFactory *i, factories) {
        int iPriority = i->priority(parent);
        if (iPriority > priority) {
            factory = i;
            priority = iPriority;
        }
    }
    return factory;
Tobias Hunger's avatar
Tobias Hunger committed
387
388
}

389
390
391
// clone
IBuildConfigurationFactory *IBuildConfigurationFactory::find(Target *parent, BuildConfiguration *bc)
{
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
    QList<IBuildConfigurationFactory *> factories
            = ExtensionSystem::PluginManager::getObjects<IBuildConfigurationFactory>(
                [&parent, &bc](IBuildConfigurationFactory *factory) {
                    return factory->canClone(parent, bc);
                });

    IBuildConfigurationFactory *factory = 0;
    int priority = -1;
    foreach (IBuildConfigurationFactory *i, factories) {
        int iPriority = i->priority(parent);
        if (iPriority > priority) {
            factory = i;
            priority = iPriority;
        }
    }
    return factory;
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
436
437
438
439
440
441
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
484
485
486
487
488
489

void IBuildConfigurationFactory::setSupportedProjectType(Core::Id id)
{
    m_supportedProjectType = id;
}

void IBuildConfigurationFactory::setSupportedProjectMimeTypeName(const QString &mimeTypeName)
{
    m_supportedProjectMimeTypeName = mimeTypeName;
}

void IBuildConfigurationFactory::setSupportedTargetDeviceTypes(const QList<Core::Id> &ids)
{
    m_supportedTargetDeviceTypes = ids;
}

bool IBuildConfigurationFactory::canHandle(const Target *target) const
{
    if (m_supportedProjectType.isValid() && m_supportedProjectType != target->project()->id())
        return false;

    if (!target->project()->supportsKit(target->kit()))
        return false;

    if (!m_supportedTargetDeviceTypes.isEmpty())
        if (!m_supportedTargetDeviceTypes.contains(
                    DeviceTypeKitInformation::deviceTypeId(target->kit())))
            return false;

    return true;
}

BuildConfiguration *IBuildConfigurationFactory::create(Target *parent, const BuildInfo *info) const
{
    if (!canHandle(parent))
        return nullptr;
    QTC_ASSERT(m_creator, return nullptr);
    BuildConfiguration *bc = m_creator(parent);
    if (!bc)
        return nullptr;
    bc->initialize(info);
    return bc;
}

bool IBuildConfigurationFactory::canClone(const Target *parent, BuildConfiguration *product) const
{
    if (!canHandle(parent))
        return false;
    const Core::Id id = product->id();
    return id == m_buildConfigId;
}

BuildConfiguration *IBuildConfigurationFactory::restore(Target *parent, const QVariantMap &map)
{
    if (!canRestore(parent, map))
        return nullptr;
    QTC_ASSERT(m_creator, return nullptr);
    BuildConfiguration *bc = m_creator(parent);
    QTC_ASSERT(bc, return nullptr);
    if (!bc->fromMap(map)) {
        delete bc;
        bc = nullptr;
    }
    return bc;
}

bool IBuildConfigurationFactory::canRestore(const Target *parent, const QVariantMap &map) const
{
    if (!canHandle(parent))
        return false;
    const Core::Id id = idFromMap(map);
    return id.name().startsWith(m_buildConfigId.name());
}

BuildConfiguration *IBuildConfigurationFactory::clone(Target *parent, BuildConfiguration *product)
{
    QTC_ASSERT(m_creator, return nullptr);
    if (!canClone(parent, product))
        return nullptr;
    BuildConfiguration *bc = m_creator(parent);
    QVariantMap data = product->toMap();
490
491
492
493
    if (!bc->fromMap(data)) {
        delete bc;
        bc = nullptr;
    }
494
495
496
    return bc;
}

497
} // namespace ProjectExplorer