qtkitinformation.cpp 8.09 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Tobias Hunger's avatar
Tobias Hunger committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Tobias Hunger's avatar
Tobias Hunger committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Tobias Hunger's avatar
Tobias Hunger 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.
Tobias Hunger's avatar
Tobias Hunger committed
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.
Tobias Hunger's avatar
Tobias Hunger committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
Tobias Hunger's avatar
Tobias Hunger committed
25

Tobias Hunger's avatar
Tobias Hunger committed
26
#include "qtkitinformation.h"
Tobias Hunger's avatar
Tobias Hunger committed
27

Samuel Gaist's avatar
Samuel Gaist committed
28 29
#include <QRegExp>

Tobias Hunger's avatar
Tobias Hunger committed
30
#include "qtkitconfigwidget.h"
31
#include "qtsupportconstants.h"
Tobias Hunger's avatar
Tobias Hunger committed
32
#include "qtversionmanager.h"
33
#include "qtparser.h"
Tobias Hunger's avatar
Tobias Hunger committed
34

35 36 37
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/task.h>

38
#include <utils/algorithm.h>
39
#include <utils/buildablehelperlibrary.h>
40
#include <utils/macroexpander.h>
41
#include <utils/qtcassert.h>
Tobias Hunger's avatar
Tobias Hunger committed
42

43
using namespace ProjectExplorer;
44
using namespace Utils;
45

Tobias Hunger's avatar
Tobias Hunger committed
46
namespace QtSupport {
47

Tobias Hunger's avatar
Tobias Hunger committed
48
QtKitInformation::QtKitInformation()
Tobias Hunger's avatar
Tobias Hunger committed
49
{
Tobias Hunger's avatar
Tobias Hunger committed
50
    setObjectName(QLatin1String("QtKitInformation"));
51
    setId(QtKitInformation::id());
52 53
    setPriority(26000);

54 55
    connect(KitManager::instance(), &KitManager::kitsLoaded,
            this, &QtKitInformation::kitsWereLoaded);
Tobias Hunger's avatar
Tobias Hunger committed
56 57
}

58
QVariant QtKitInformation::defaultValue(const Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
59
{
60
    Q_UNUSED(k);
Tobias Hunger's avatar
Tobias Hunger committed
61 62

    // find "Qt in PATH":
63 64
    BaseQtVersion *result = QtVersionManager::version(equal(&BaseQtVersion::autodetectionSource,
                                                            QString::fromLatin1("PATH")));
65 66 67 68
    if (result)
        return result->uniqueId();

    // Use *any* desktop Qt:
69 70
    result = QtVersionManager::version(equal(&BaseQtVersion::type,
                                             QString::fromLatin1(QtSupport::Constants::DESKTOPQT)));
71
    return result ? result->uniqueId() : -1;
Tobias Hunger's avatar
Tobias Hunger committed
72 73
}

74
QList<ProjectExplorer::Task> QtKitInformation::validate(const ProjectExplorer::Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
75
{
76
    QTC_ASSERT(QtVersionManager::isLoaded(), return { });
77 78
    BaseQtVersion *version = qtVersion(k);
    if (!version)
79 80
    return { };

81
    return version->validateKit(k);
Tobias Hunger's avatar
Tobias Hunger committed
82 83
}

84 85
void QtKitInformation::fix(ProjectExplorer::Kit *k)
{
hjk's avatar
hjk committed
86
    QTC_ASSERT(QtVersionManager::isLoaded(), return);
87
    BaseQtVersion *version = qtVersion(k);
88
    if (!version && qtVersionId(k) >= 0) {
89
        qWarning("Qt version is no longer known, removing from kit \"%s\".", qPrintable(k->displayName()));
90
        setQtVersionId(k, -1);
91
    }
92 93
}

94
ProjectExplorer::KitConfigWidget *QtKitInformation::createConfigWidget(ProjectExplorer::Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
95
{
96
    return new Internal::QtKitConfigWidget(k, this);
Tobias Hunger's avatar
Tobias Hunger committed
97 98
}

99
QString QtKitInformation::displayNamePostfix(const ProjectExplorer::Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
100
{
101
    BaseQtVersion *version = qtVersion(k);
Tobias Hunger's avatar
Tobias Hunger committed
102 103 104
    return version ? version->displayName() : QString();
}

Tobias Hunger's avatar
Tobias Hunger committed
105
ProjectExplorer::KitInformation::ItemList
106
QtKitInformation::toUserOutput(const ProjectExplorer::Kit *k) const
Tobias Hunger's avatar
Tobias Hunger committed
107
{
108
    BaseQtVersion *version = qtVersion(k);
Tobias Hunger's avatar
Tobias Hunger committed
109 110 111
    return ItemList() << qMakePair(tr("Qt version"), version ? version->displayName() : tr("None"));
}

112
void QtKitInformation::addToEnvironment(const ProjectExplorer::Kit *k, Utils::Environment &env) const
Tobias Hunger's avatar
Tobias Hunger committed
113
{
114
    BaseQtVersion *version = qtVersion(k);
Tobias Hunger's avatar
Tobias Hunger committed
115
    if (version)
116
        version->addToEnvironment(k, env);
Tobias Hunger's avatar
Tobias Hunger committed
117 118
}

119 120 121 122 123 124 125
ProjectExplorer::IOutputParser *QtKitInformation::createOutputParser(const ProjectExplorer::Kit *k) const
{
    if (qtVersion(k))
        return new QtParser;
    return 0;
}

hjk's avatar
hjk committed
126
void QtKitInformation::addToMacroExpander(Kit *kit, MacroExpander *expander) const
127
{
hjk's avatar
hjk committed
128
    expander->registerSubProvider(
129
                [kit]() -> MacroExpander * {
hjk's avatar
hjk committed
130 131 132 133
                    BaseQtVersion *version = qtVersion(kit);
                    return version ? version->macroExpander() : 0;
                });

hjk's avatar
hjk committed
134
    expander->registerVariable("Qt:Name", tr("Name of Qt Version"),
135
                [kit]() -> QString {
hjk's avatar
hjk committed
136 137 138
                   BaseQtVersion *version = qtVersion(kit);
                   return version ? version->displayName() : tr("unknown");
                });
139
    expander->registerVariable("Qt:qmakeExecutable", tr("Path to the qmake executable"),
140
                [kit]() -> QString {
141 142 143
                    BaseQtVersion *version = qtVersion(kit);
                    return version ? version->qmakeCommand().toString() : QString();
                });
144 145
}

146 147 148 149 150
Core::Id QtKitInformation::id()
{
    return "QtSupport.QtInformation";
}

151
int QtKitInformation::qtVersionId(const ProjectExplorer::Kit *k)
Tobias Hunger's avatar
Tobias Hunger committed
152
{
153
    if (!k)
Tobias Hunger's avatar
Tobias Hunger committed
154
        return -1;
155 156

    int id = -1;
157
    QVariant data = k->value(QtKitInformation::id(), -1);
158 159 160 161 162 163 164
    if (data.type() == QVariant::Int) {
        bool ok;
        id = data.toInt(&ok);
        if (!ok)
            id = -1;
    } else {
        QString source = data.toString();
165 166
        BaseQtVersion *v = QtVersionManager::version([source](const BaseQtVersion *v) { return v->autodetectionSource() == source; });
        if (v)
167 168
            id = v->uniqueId();
    }
Tobias Hunger's avatar
Tobias Hunger committed
169 170 171
    return id;
}

172
void QtKitInformation::setQtVersionId(ProjectExplorer::Kit *k, const int id)
Tobias Hunger's avatar
Tobias Hunger committed
173
{
174
    k->setValue(QtKitInformation::id(), id);
Tobias Hunger's avatar
Tobias Hunger committed
175 176
}

177
BaseQtVersion *QtKitInformation::qtVersion(const ProjectExplorer::Kit *k)
Tobias Hunger's avatar
Tobias Hunger committed
178
{
hjk's avatar
hjk committed
179
    return QtVersionManager::version(qtVersionId(k));
Tobias Hunger's avatar
Tobias Hunger committed
180 181
}

182
void QtKitInformation::setQtVersion(ProjectExplorer::Kit *k, const BaseQtVersion *v)
Tobias Hunger's avatar
Tobias Hunger committed
183 184
{
    if (!v)
185
        setQtVersionId(k, -1);
Tobias Hunger's avatar
Tobias Hunger committed
186
    else
187
        setQtVersionId(k, v->uniqueId());
Tobias Hunger's avatar
Tobias Hunger committed
188 189
}

190 191 192 193 194 195
void QtKitInformation::qtVersionsChanged(const QList<int> &addedIds,
                                         const QList<int> &removedIds,
                                         const QList<int> &changedIds)
{
    Q_UNUSED(addedIds);
    Q_UNUSED(removedIds);
196 197 198
    foreach (ProjectExplorer::Kit *k, ProjectExplorer::KitManager::kits()) {
        if (changedIds.contains(qtVersionId(k))) {
            k->validate(); // Qt version may have become (in)valid
199
            notifyAboutUpdate(k);
200 201
        }
    }
202 203
}

204 205
void QtKitInformation::kitsWereLoaded()
{
206
    foreach (ProjectExplorer::Kit *k, ProjectExplorer::KitManager::kits())
207 208
        fix(k);

209 210
    connect(QtVersionManager::instance(), &QtVersionManager::qtVersionsChanged,
            this, &QtKitInformation::qtVersionsChanged);
211 212
}

213
Kit::Predicate QtKitInformation::platformPredicate(Core::Id platform)
Tobias Hunger's avatar
Tobias Hunger committed
214
{
215
    return [platform](const Kit *kit) -> bool {
216
        BaseQtVersion *version = QtKitInformation::qtVersion(kit);
217
        return version && version->targetDeviceTypes().contains(platform);
218
    };
Tobias Hunger's avatar
Tobias Hunger committed
219 220
}

221 222 223
Kit::Predicate QtKitInformation::qtVersionPredicate(const QSet<Core::Id> &required,
                                                    const QtVersionNumber &min,
                                                    const QtVersionNumber &max)
Tobias Hunger's avatar
Tobias Hunger committed
224
{
225
    return [required, min, max](const Kit *kit) -> bool {
226 227 228 229 230 231 232 233 234
        BaseQtVersion *version = QtKitInformation::qtVersion(kit);
        if (!version)
            return false;
        QtVersionNumber current = version->qtVersion();
        if (min.majorVersion > -1 && current < min)
            return false;
        if (max.majorVersion > -1 && current > max)
            return false;
        return version->availableFeatures().contains(required);
235
    };
Tobias Hunger's avatar
Tobias Hunger committed
236 237
}

238
QSet<Core::Id> QtKitInformation::supportedPlatforms(const Kit *k) const
239 240
{
    BaseQtVersion *version = QtKitInformation::qtVersion(k);
241
    return version ? version->targetDeviceTypes() : QSet<Core::Id>();
242 243
}

244
QSet<Core::Id> QtKitInformation::availableFeatures(const Kit *k) const
245 246
{
    BaseQtVersion *version = QtKitInformation::qtVersion(k);
247
    return version ? version->availableFeatures() : QSet<Core::Id>();
248 249
}

Tobias Hunger's avatar
Tobias Hunger committed
250
} // namespace QtSupport