cmakekitinformation.cpp 8.41 KB
Newer Older
1
2
/****************************************************************************
**
3
4
** Copyright (C) 2016 Canonical Ltd.
** Contact: https://www.qt.io/licensing/
5
6
7
8
9
10
11
**
** This file is part of Qt Creator.
**
** 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.
23
24
**
****************************************************************************/
25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "cmakekitinformation.h"
#include "cmakekitconfigwidget.h"
#include "cmaketoolmanager.h"
#include "cmaketool.h"

#include <utils/qtcassert.h>
#include <projectexplorer/task.h>
#include <projectexplorer/kit.h>
#include <projectexplorer/projectexplorerconstants.h>

#include <QDebug>

using namespace ProjectExplorer;

namespace CMakeProjectManager {
41
42
43
// --------------------------------------------------------------------
// CMakeKitInformation:
// --------------------------------------------------------------------
44

45
46
47
static Core::Id defaultCMakeToolId()
{
    CMakeTool *defaultTool = CMakeToolManager::defaultCMakeTool();
48
    return defaultTool ? defaultTool->id() : Core::Id();
49
50
}

51
52
static const char TOOL_ID[] = "CMakeProjectManager.CMakeKitInformation";

53
54
55
56
// --------------------------------------------------------------------
// CMakeKitInformation:
// --------------------------------------------------------------------

57
58
59
CMakeKitInformation::CMakeKitInformation()
{
    setObjectName(QLatin1String("CMakeKitInformation"));
60
    setId(TOOL_ID);
Orgad Shaneh's avatar
Orgad Shaneh committed
61
    setPriority(20000);
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

    //make sure the default value is set if a selected CMake is removed
    connect(CMakeToolManager::instance(), &CMakeToolManager::cmakeRemoved,
            [this]() { foreach (Kit *k, KitManager::kits()) fix(k); });

    //make sure the default value is set if a new default CMake is set
    connect(CMakeToolManager::instance(), &CMakeToolManager::defaultCMakeChanged,
            [this]() { foreach (Kit *k, KitManager::kits()) fix(k); });
}

CMakeTool *CMakeKitInformation::cmakeTool(const Kit *k)
{
    if (!k)
        return 0;

77
    const QVariant id = k->value(TOOL_ID);
78
79
80
81
82
    return CMakeToolManager::findById(Core::Id::fromSetting(id));
}

void CMakeKitInformation::setCMakeTool(Kit *k, const Core::Id id)
{
83
84
85
86
    const Core::Id toSet = id.isValid() ? id : defaultCMakeToolId();
    QTC_ASSERT(!id.isValid() || CMakeToolManager::findById(toSet), return);
    if (k)
        k->setValue(TOOL_ID, toSet.toSetting());
87
88
}

89
QVariant CMakeKitInformation::defaultValue(const Kit *k) const
90
{
91
    const Core::Id id = k ? defaultCMakeToolId() : Core::Id();
92
    return id.toSetting();
93
94
95
96
}

QList<Task> CMakeKitInformation::validate(const Kit *k) const
{
97
98
    Q_UNUSED(k);
    return QList<Task>();
99
100
101
102
103
}

void CMakeKitInformation::setup(Kit *k)
{
    CMakeTool *tool = CMakeKitInformation::cmakeTool(k);
104
105
    if (!tool)
        setCMakeTool(k, defaultCMakeToolId());
106
107
108
109
}

void CMakeKitInformation::fix(Kit *k)
{
110
    if (!CMakeKitInformation::cmakeTool(k))
111
112
113
114
115
        setup(k);
}

KitInformation::ItemList CMakeKitInformation::toUserOutput(const Kit *k) const
{
116
    const CMakeTool *const tool = cmakeTool(k);
117
118
119
120
121
122
123
124
    return ItemList() << qMakePair(tr("CMake"), tool == 0 ? tr("Unconfigured") : tool->displayName());
}

KitConfigWidget *CMakeKitInformation::createConfigWidget(Kit *k) const
{
    return new Internal::CMakeKitConfigWidget(k, this);
}

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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// --------------------------------------------------------------------
// CMakeGeneratorKitInformation:
// --------------------------------------------------------------------

static const char GENERATOR_ID[] = "CMake.GeneratorKitInformation";

CMakeGeneratorKitInformation::CMakeGeneratorKitInformation()
{
    setObjectName(QLatin1String("CMakeGeneratorKitInformation"));
    setId(GENERATOR_ID);
    setPriority(19000);
}

QString CMakeGeneratorKitInformation::generator(const Kit *k)
{
    if (!k)
        return QString();
    return k->value(GENERATOR_ID).toString();
}

void CMakeGeneratorKitInformation::setGenerator(Kit *k, const QString &generator)
{
    if (!k)
        return;
    k->setValue(GENERATOR_ID, generator);
}

QString CMakeGeneratorKitInformation::generatorArgument(const Kit *k)
{
    QString tmp = generator(k);
    if (tmp.isEmpty())
        return QString::fromLatin1("-G") + tmp;
    return tmp;
}

QVariant CMakeGeneratorKitInformation::defaultValue(const Kit *k) const
{
    CMakeTool *tool = CMakeKitInformation::cmakeTool(k);
    if (!tool)
        return QString();
    QStringList known = tool->supportedGenerators();
    auto it = std::find_if(known.constBegin(), known.constEnd(),
                           [](const QString &s) { return s == QLatin1String("CodeBlocks - Ninja"); });
    if (it == known.constEnd())
        it = std::find_if(known.constBegin(), known.constEnd(),
                          [](const QString &s) { return s == QLatin1String("CodeBlocks - Unix Makefiles"); });
    if (it == known.constEnd())
        it = std::find_if(known.constBegin(), known.constEnd(),
                          [](const QString &s) { return s == QLatin1String("CodeBlocks - NMake Makefiles"); });
    if (it == known.constEnd())
        it = known.constBegin(); // Fallback to the first generator...
    if (it != known.constEnd())
        return *it;
    return QString();
}

QList<Task> CMakeGeneratorKitInformation::validate(const Kit *k) const
{
    CMakeTool *tool = CMakeKitInformation::cmakeTool(k);
    QString generator = CMakeGeneratorKitInformation::generator(k);

    QList<Task> result;
    if (!tool) {
        if (!generator.isEmpty()) {
            result << Task(Task::Warning, tr("No CMake Tool configured, CMake generator will be ignored."),
                           Utils::FileName(), -1, Core::Id(Constants::TASK_CATEGORY_BUILDSYSTEM));
        }
    } else {
        if (!tool->isValid()) {
            result << Task(Task::Warning, tr("CMake Tool is unconfigured, CMake generator will be ignored."),
                           Utils::FileName(), -1, Core::Id(Constants::TASK_CATEGORY_BUILDSYSTEM));
        } else {
            QStringList known = tool->supportedGenerators();
            if (!known.contains(generator)) {
                result << Task(Task::Error, tr("CMake Tool does not support the configured generator."),
                               Utils::FileName(), -1, Core::Id(Constants::TASK_CATEGORY_BUILDSYSTEM));
            }
            if (!generator.startsWith(QLatin1String("CodeBlocks -"))) {
                result << Task(Task::Warning, tr("CMake generator does not generate CodeBlocks file. "
                                                 "Qt Creator will not be able to parse the CMake project."),
                               Utils::FileName(), -1, Core::Id(Constants::TASK_CATEGORY_BUILDSYSTEM));
            }
        }
    }
    return result;
}

void CMakeGeneratorKitInformation::setup(Kit *k)
{
    CMakeGeneratorKitInformation::setGenerator(k, defaultValue(k).toString());
}

void CMakeGeneratorKitInformation::fix(Kit *k)
{
    const CMakeTool *tool = CMakeKitInformation::cmakeTool(k);
    const QString generator = CMakeGeneratorKitInformation::generator(k);

    if (!tool)
        return;
    QStringList known = tool->supportedGenerators();
    if (generator.isEmpty() || !known.contains(generator))
        CMakeGeneratorKitInformation::setGenerator(k, defaultValue(k).toString());
}

KitInformation::ItemList CMakeGeneratorKitInformation::toUserOutput(const Kit *k) const
{
    const QString generator = CMakeGeneratorKitInformation::generator(k);
    return ItemList() << qMakePair(tr("CMake Generator"),
                                   generator.isEmpty() ? tr("<Use Default Generator>") : generator);
}

KitConfigWidget *CMakeGeneratorKitInformation::createConfigWidget(Kit *k) const
{
    return new Internal::CMakeGeneratorKitConfigWidget(k, this);
}

241
} // namespace CMakeProjectManager