toolchainmanager.cpp 10.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
** 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
** a written agreement between you and Digia.  For licensing terms and
Eike Ziller's avatar
Eike Ziller committed
13
14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
25
26
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30
31

#include "toolchainmanager.h"
Tobias Hunger's avatar
Tobias Hunger committed
32

33
#include "abi.h"
Tobias Hunger's avatar
Tobias Hunger committed
34
#include "kitinformation.h"
35
36
#include "toolchain.h"

Tobias Hunger's avatar
Tobias Hunger committed
37
38
#include <coreplugin/icore.h>

39
40
#include <extensionsystem/pluginmanager.h>

41
#include <utils/fileutils.h>
Jarek Kobus's avatar
Jarek Kobus committed
42
#include <utils/persistentsettings.h>
43
#include <utils/qtcassert.h>
44
#include <utils/algorithm.h>
Jarek Kobus's avatar
Jarek Kobus committed
45

46
47
#include <QDir>
#include <QSettings>
Tobias Hunger's avatar
Tobias Hunger committed
48

49
50
51
static const char TOOLCHAIN_DATA_KEY[] = "ToolChain.";
static const char TOOLCHAIN_COUNT_KEY[] = "ToolChain.Count";
static const char TOOLCHAIN_FILE_VERSION_KEY[] = "Version";
Tobias Hunger's avatar
Tobias Hunger committed
52
53
static const char TOOLCHAIN_FILENAME[] = "/qtcreator/toolchains.xml";
static const char LEGACY_TOOLCHAIN_FILENAME[] = "/toolChains.xml";
54

hjk's avatar
hjk committed
55
using namespace Utils;
Jarek Kobus's avatar
Jarek Kobus committed
56

hjk's avatar
hjk committed
57
static FileName settingsFileName(const QString &path)
Tobias Hunger's avatar
Tobias Hunger committed
58
{
59
    QFileInfo settingsLocation(Core::ICore::settings()->fileName());
hjk's avatar
hjk committed
60
    return FileName::fromString(settingsLocation.absolutePath() + path);
Tobias Hunger's avatar
Tobias Hunger committed
61
}
62
63
64
65
66
67
68
69
70
71
72

namespace ProjectExplorer {
namespace Internal {

// --------------------------------------------------------------------------
// ToolChainManagerPrivate
// --------------------------------------------------------------------------

class ToolChainManagerPrivate
{
public:
hjk's avatar
hjk committed
73
    ToolChainManagerPrivate() : m_writer(0) {}
Tobias Hunger's avatar
Tobias Hunger committed
74
    ~ToolChainManagerPrivate();
75

hjk's avatar
hjk committed
76
77
    QMap<QString, FileName> m_abiToDebugger;
    PersistentSettingsWriter *m_writer;
78

79
    QList<ToolChain *> m_toolChains; // prioritized List
80
81
};

Tobias Hunger's avatar
Tobias Hunger committed
82
ToolChainManagerPrivate::~ToolChainManagerPrivate()
83
84
85
86
87
{
    qDeleteAll(m_toolChains);
    m_toolChains.clear();
    delete m_writer;
}
Tobias Hunger's avatar
Tobias Hunger committed
88

hjk's avatar
hjk committed
89
90
91
static ToolChainManager *m_instance = 0;
static ToolChainManagerPrivate *d;

92
93
} // namespace Internal

hjk's avatar
hjk committed
94
95
using namespace Internal;

96
97
98
99
100
// --------------------------------------------------------------------------
// ToolChainManager
// --------------------------------------------------------------------------

ToolChainManager::ToolChainManager(QObject *parent) :
hjk's avatar
hjk committed
101
    QObject(parent)
102
103
104
{
    Q_ASSERT(!m_instance);
    m_instance = this;
Tobias Hunger's avatar
Tobias Hunger committed
105

hjk's avatar
hjk committed
106
107
    d = new ToolChainManagerPrivate;

Tobias Hunger's avatar
Tobias Hunger committed
108
109
    connect(Core::ICore::instance(), SIGNAL(saveSettingsRequested()),
            this, SLOT(saveToolChains()));
110
111
112
113
114
115
    connect(this, SIGNAL(toolChainAdded(ProjectExplorer::ToolChain*)),
            this, SIGNAL(toolChainsChanged()));
    connect(this, SIGNAL(toolChainRemoved(ProjectExplorer::ToolChain*)),
            this, SIGNAL(toolChainsChanged()));
    connect(this, SIGNAL(toolChainUpdated(ProjectExplorer::ToolChain*)),
            this, SIGNAL(toolChainsChanged()));
116
117
}

hjk's avatar
hjk committed
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
ToolChainManager::~ToolChainManager()
{
    delete d;
    m_instance = 0;
}

QObject *ToolChainManager::instance()
{
    return m_instance;
}

static QList<ToolChain *> restoreFromFile(const FileName &fileName)
{
    QList<ToolChain *> result;

    PersistentSettingsReader reader;
    if (!reader.load(fileName))
        return result;
    QVariantMap data = reader.restoreValues();

    // Check version:
    int version = data.value(QLatin1String(TOOLCHAIN_FILE_VERSION_KEY), 0).toInt();
    if (version < 1)
        return result;

    QList<ToolChainFactory *> factories = ExtensionSystem::PluginManager::getObjects<ToolChainFactory>();

145
    int count = data.value(QLatin1String(TOOLCHAIN_COUNT_KEY), 0).toInt();
hjk's avatar
hjk committed
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
    for (int i = 0; i < count; ++i) {
        const QString key = QString::fromLatin1(TOOLCHAIN_DATA_KEY) + QString::number(i);
        if (!data.contains(key))
            break;

        const QVariantMap tcMap = data.value(key).toMap();

        bool restored = false;
        foreach (ToolChainFactory *f, factories) {
            if (f->canRestore(tcMap)) {
                if (ToolChain *tc = f->restore(tcMap)) {
                    result.append(tc);
                    restored = true;
                    break;
                }
            }
        }
        if (!restored)
            qWarning("Warning: Unable to restore compiler '%s' stored in %s.",
                     qPrintable(ToolChainFactory::idFromMap(tcMap)),
                     qPrintable(fileName.toUserOutput()));
    }

    return result;
}

172
173
void ToolChainManager::restoreToolChains()
{
174
175
    QTC_ASSERT(!d->m_writer, return);
    d->m_writer =
hjk's avatar
hjk committed
176
            new PersistentSettingsWriter(settingsFileName(QLatin1String(TOOLCHAIN_FILENAME)), QLatin1String("QtCreatorToolChains"));
177

178
179
180
181
    QList<ToolChain *> tcsToRegister;
    QList<ToolChain *> tcsToCheck;

    // read all tool chains from SDK
hjk's avatar
hjk committed
182
    QFileInfo systemSettingsFile(Core::ICore::settings(QSettings::SystemScope)->fileName());
183
    QList<ToolChain *> readTcs =
hjk's avatar
hjk committed
184
            restoreFromFile(FileName::fromString(systemSettingsFile.absolutePath() + QLatin1String(TOOLCHAIN_FILENAME)));
185
186
    // make sure we mark these as autodetected!
    foreach (ToolChain *tc, readTcs)
187
        tc->setDetection(ToolChain::AutoDetection);
188
189
190
191

    tcsToRegister = readTcs; // SDK TCs are always considered to be up-to-date, so no need to
                             // recheck them.

Tobias Hunger's avatar
Tobias Hunger committed
192
193
    // read all tool chains from user file.
    // Read legacy settings once and keep them around...
hjk's avatar
hjk committed
194
    FileName fileName = settingsFileName(QLatin1String(TOOLCHAIN_FILENAME));
hjk's avatar
hjk committed
195
    if (!fileName.exists())
Tobias Hunger's avatar
Tobias Hunger committed
196
        fileName = settingsFileName(QLatin1String(LEGACY_TOOLCHAIN_FILENAME));
hjk's avatar
hjk committed
197
    readTcs = restoreFromFile(fileName);
198
199
200
201
202
203
204
205

    foreach (ToolChain *tc, readTcs) {
        if (tc->isAutoDetected())
            tcsToCheck.append(tc);
        else
            tcsToRegister.append(tc);
    }
    readTcs.clear();
206

207
208
209
210
211
212
213
214
215
    // Remove TCs configured by the SDK:
    foreach (ToolChain *tc, tcsToRegister) {
        for (int i = tcsToCheck.count() - 1; i >= 0; --i) {
            if (tcsToCheck.at(i)->id() == tc->id()) {
                delete tcsToCheck.at(i);
                tcsToCheck.removeAt(i);
            }
        }
    }
216

217
    // Then auto detect
218
    QList<ToolChain *> detectedTcs;
219
    QList<ToolChainFactory *> factories = ExtensionSystem::PluginManager::getObjects<ToolChainFactory>();
220
221
222
223
224
225
226
227
    foreach (ToolChainFactory *f, factories)
        detectedTcs.append(f->autoDetect());

    // Find/update autodetected tool chains:
    ToolChain *toStore = 0;
    foreach (ToolChain *currentDetected, detectedTcs) {
        toStore = currentDetected;

228
229
        // Check whether we had this TC stored and prefer the old one with the old id, marked
        // as auto-detection.
230
231
232
        for (int i = 0; i < tcsToCheck.count(); ++i) {
            if (*(tcsToCheck.at(i)) == *currentDetected) {
                toStore = tcsToCheck.at(i);
233
                toStore->setDetection(ToolChain::AutoDetection);
234
235
236
237
238
                tcsToCheck.removeAt(i);
                delete currentDetected;
                break;
            }
        }
239
        tcsToRegister += toStore;
240
241
    }

242
    // Keep toolchains that were not rediscovered but are still executable and delete the rest
243
    foreach (ToolChain *tc, tcsToCheck) {
244
245
        if (!tc->isValid()) {
            qWarning() << QString::fromLatin1("ToolChain \"%1\" (%2) dropped since it is not valid")
246
247
248
                          .arg(tc->displayName()).arg(tc->id());
            delete tc;
        } else {
249
            tcsToRegister += tc;
250
        }
251
    }
252
253
254
255

    // Store manual tool chains
    foreach (ToolChain *tc, tcsToRegister)
        registerToolChain(tc);
256

hjk's avatar
hjk committed
257
    emit m_instance->toolChainsLoaded();
258
259
260
261
}

void ToolChainManager::saveToolChains()
{
262
263
    QVariantMap data;
    data.insert(QLatin1String(TOOLCHAIN_FILE_VERSION_KEY), 1);
264
265

    int count = 0;
266
    foreach (ToolChain *tc, d->m_toolChains) {
267
        if (tc->isValid()) {
268
269
270
            QVariantMap tmp = tc->toMap();
            if (tmp.isEmpty())
                continue;
271
            data.insert(QString::fromLatin1(TOOLCHAIN_DATA_KEY) + QString::number(count), tmp);
272
273
274
            ++count;
        }
    }
275
276
    data.insert(QLatin1String(TOOLCHAIN_COUNT_KEY), count);
    d->m_writer->save(data, Core::ICore::mainWindow());
277
278

    // Do not save default debuggers! Those are set by the SDK!
279
280
}

hjk's avatar
hjk committed
281
QList<ToolChain *> ToolChainManager::toolChains()
282
{
283
    return d->m_toolChains;
284
285
}

hjk's avatar
hjk committed
286
QList<ToolChain *> ToolChainManager::findToolChains(const Abi &abi)
287
288
{
    QList<ToolChain *> result;
289
    foreach (ToolChain *tc, d->m_toolChains) {
290
291
292
293
294
295
296
        Abi targetAbi = tc->targetAbi();
        if (targetAbi.isCompatibleWith(abi))
            result.append(tc);
    }
    return result;
}

hjk's avatar
hjk committed
297
ToolChain *ToolChainManager::findToolChain(const QString &id)
298
{
299
300
301
    if (id.isEmpty())
        return 0;

302
    return Utils::findOrDefault(d->m_toolChains, Utils::equal(&ToolChain::id, id));
303
304
}

hjk's avatar
hjk committed
305
FileName ToolChainManager::defaultDebugger(const Abi &abi)
306
{
hjk's avatar
hjk committed
307
    return d->m_abiToDebugger.value(abi.toString());
308
309
}

hjk's avatar
hjk committed
310
bool ToolChainManager::isLoaded()
311
312
313
314
{
    return d->m_writer;
}

hjk's avatar
hjk committed
315
void ToolChainManager::notifyAboutUpdate(ToolChain *tc)
Tobias Hunger's avatar
Tobias Hunger committed
316
{
317
    if (!tc || !d->m_toolChains.contains(tc))
Tobias Hunger's avatar
Tobias Hunger committed
318
        return;
hjk's avatar
hjk committed
319
    emit m_instance->toolChainUpdated(tc);
Tobias Hunger's avatar
Tobias Hunger committed
320
321
}

322
bool ToolChainManager::registerToolChain(ToolChain *tc)
323
{
324
325
326
    QTC_ASSERT(d->m_writer, return false);

    if (!tc || d->m_toolChains.contains(tc))
327
        return true;
328
    foreach (ToolChain *current, d->m_toolChains) {
329
        if (*tc == *current && !tc->isAutoDetected())
330
            return false;
331
        QTC_ASSERT(current->id() != tc->id(), return false);
332
333
    }

334
    d->m_toolChains.append(tc);
hjk's avatar
hjk committed
335
    emit m_instance->toolChainAdded(tc);
336
    return true;
337
338
339
340
}

void ToolChainManager::deregisterToolChain(ToolChain *tc)
{
341
    if (!tc || !d->m_toolChains.contains(tc))
342
        return;
343
    d->m_toolChains.removeOne(tc);
hjk's avatar
hjk committed
344
    emit m_instance->toolChainRemoved(tc);
345
346
347
348
    delete tc;
}

} // namespace ProjectExplorer