iosconfigurations.cpp 14.6 KB
Newer Older
1
2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://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
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
16
17
**
** GNU Lesser General Public License Usage
** 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.
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27
28
29
30
31
32
33
34
35
36
37
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "iosconfigurations.h"
#include "iosconstants.h"
#include "iosdevice.h"
#include "iossimulator.h"
#include "iosprobe.h"

#include <coreplugin/icore.h>
38
#include <utils/algorithm.h>
39
40
41
42
43
44
45
46
#include <utils/qtcassert.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/devicesupport/devicemanager.h>
#include <projectexplorer/toolchainmanager.h>
#include <projectexplorer/toolchain.h>
#include <projectexplorer/gcctoolchain.h>
#include <projectexplorer/projectexplorerconstants.h>
47
#include <debugger/debuggeritemmanager.h>
48
#include <debugger/debuggeritem.h>
49
50
51
52
53
54
55
#include <debugger/debuggerkitinformation.h>
#include <qtsupport/baseqtversion.h>
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/qtversionmanager.h>
#include <qtsupport/qtversionfactory.h>

#include <QFileInfo>
56
#include <QHash>
57
#include <QList>
hjk's avatar
hjk committed
58
59
60
#include <QSettings>
#include <QStringList>
#include <QTimer>
61
62

using namespace ProjectExplorer;
hjk's avatar
hjk committed
63
using namespace QtSupport;
64
using namespace Utils;
Fawzi Mohamed's avatar
Fawzi Mohamed committed
65
using namespace Debugger;
66

67
68
69
namespace {
Q_LOGGING_CATEGORY(kitSetupLog, "qtc.ios.kitSetup")
}
70
71
72
73

namespace Ios {
namespace Internal {

hjk's avatar
hjk committed
74
75
const QLatin1String SettingsGroup("IosConfigurations");
const QLatin1String ignoreAllDevicesKey("IgnoreAllDevices");
76

77
static Core::Id deviceId(const Platform &platform)
78
{
79
80
81
82
83
84
    if (platform.name.startsWith(QLatin1String("iphoneos-")))
        return Constants::IOS_DEVICE_TYPE;
    else if (platform.name.startsWith(QLatin1String("iphonesimulator-")))
        return Constants::IOS_SIMULATOR_TYPE;
    return Core::Id();
}
hjk's avatar
hjk committed
85

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
static bool handledPlatform(const Platform &platform)
{
    // do not want platforms that
    // - are not iphone (e.g. watchos)
    // - are not base
    // - are C++11
    // - are not clang
    return deviceId(platform).isValid()
            && (platform.platformKind & Platform::BasePlatform) != 0
            && (platform.platformKind & Platform::Cxx11Support) == 0
            && platform.compilerPath.toString().contains(QLatin1String("clang"));
}

static QList<Platform> handledPlatforms()
{
    QList<Platform> platforms = IosProbe::detectPlatforms().values();
    return Utils::filtered(platforms, handledPlatform);
}

static QList<ClangToolChain *> clangToolChains(const QList<ToolChain *> &toolChains)
{
    QList<ClangToolChain *> clangToolChains;
    foreach (ToolChain *toolChain, toolChains)
        if (toolChain->typeId() == ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID)
            clangToolChains.append(static_cast<ClangToolChain *>(toolChain));
    return clangToolChains;
}

static QList<ClangToolChain *> autoDetectedIosToolChains()
{
    const QList<ClangToolChain *> toolChains = clangToolChains(ToolChainManager::toolChains());
    return Utils::filtered(toolChains, [](ClangToolChain *toolChain) {
        return toolChain->isAutoDetected()
               && toolChain->displayName().startsWith(QLatin1String("iphone")); // TODO tool chains should be marked directly
    });
}

static ClangToolChain *findToolChainForPlatform(const Platform &platform, const QList<ClangToolChain *> &toolChains)
{
    return Utils::findOrDefault(toolChains, [&platform](ClangToolChain *toolChain) {
        return platform.compilerPath == toolChain->compilerCommand()
               && platform.backendFlags == toolChain->platformCodeGenFlags()
               && platform.backendFlags == toolChain->platformLinkerFlags();
    });
}

static QHash<Platform, ClangToolChain *> findToolChains(const QList<Platform> &platforms)
{
    QHash<Platform, ClangToolChain *> platformToolChainHash;
    const QList<ClangToolChain *> toolChains = autoDetectedIosToolChains();
    foreach (const Platform &platform, platforms) {
        ClangToolChain *toolChain = findToolChainForPlatform(platform, toolChains);
        if (toolChain)
            platformToolChainHash.insert(platform, toolChain);
140
    }
141
142
143
144
145
146
    return platformToolChainHash;
}

static QHash<Abi::Architecture, QSet<BaseQtVersion *>> iosQtVersions()
{
    QHash<Abi::Architecture, QSet<BaseQtVersion *>> versions;
hjk's avatar
hjk committed
147
    foreach (BaseQtVersion *qtVersion, QtVersionManager::versions()) {
148
        if (!qtVersion->isValid() || qtVersion->type() != QLatin1String(Constants::IOSQT))
149
            continue;
150
151
152
153
154
155
156
157
158
159
160
161
        foreach (const Abi &abi, qtVersion->qtAbis())
            versions[abi.architecture()].insert(qtVersion);
    }
    return versions;
}

static void printQtVersions(const QHash<Abi::Architecture, QSet<BaseQtVersion *> > &map)
{
    foreach (const Abi::Architecture &arch, map.keys()) {
        qCDebug(kitSetupLog) << "-" << Abi::toString(arch);
        foreach (const BaseQtVersion *version, map.value(arch))
            qCDebug(kitSetupLog) << "  -" << version->displayName() << version;
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
}

static QSet<Kit *> existingAutoDetectedIosKits()
{
    return Utils::filtered(KitManager::kits(), [](Kit *kit) -> bool {
        Core::Id deviceKind = DeviceTypeKitInformation::deviceTypeId(kit);
        return kit->isAutoDetected() && (deviceKind == Constants::IOS_DEVICE_TYPE
                                         || deviceKind == Constants::IOS_SIMULATOR_TYPE);
    }).toSet();
}

static void printKits(const QSet<Kit *> &kits)
{
    foreach (const Kit *kit, kits)
        qCDebug(kitSetupLog) << "  -" << kit->displayName();
}

static void setupKit(Kit *kit, Core::Id pDeviceType, ClangToolChain *pToolchain,
                     const QVariant &debuggerId, const Utils::FileName &sdkPath, BaseQtVersion *qtVersion)
{
    kit->setIconPath(FileName::fromString(QLatin1String(Constants::IOS_SETTINGS_CATEGORY_ICON)));
    DeviceTypeKitInformation::setDeviceTypeId(kit, pDeviceType);
    ToolChainKitInformation::setToolChain(kit, pToolchain);
    QtKitInformation::setQtVersion(kit, qtVersion);
    // only replace debugger with the default one if we find an unusable one here
    // (since the user could have changed it)
    if ((!DebuggerKitInformation::debugger(kit)
            || !DebuggerKitInformation::debugger(kit)->isValid()
            || DebuggerKitInformation::debugger(kit)->engineType() != LldbEngineType)
            && debuggerId.isValid())
        DebuggerKitInformation::setDebugger(kit, debuggerId);

    kit->setMutable(DeviceKitInformation::id(), true);
    kit->setSticky(QtKitInformation::id(), true);
    kit->setSticky(ToolChainKitInformation::id(), true);
    kit->setSticky(DeviceTypeKitInformation::id(), true);
    kit->setSticky(SysRootKitInformation::id(), true);
    kit->setSticky(DebuggerKitInformation::id(), false);

    SysRootKitInformation::setSysRoot(kit, sdkPath);
}

void IosConfigurations::updateAutomaticKitList()
{
    const QList<Platform> platforms = handledPlatforms();
    qCDebug(kitSetupLog) << "Used platforms:" << platforms;
    if (!platforms.isEmpty())
        setDeveloperPath(platforms.first().developerPath);
    qCDebug(kitSetupLog) << "Developer path:" << developerPath();

    // platform name -> tool chain
    const QHash<Platform, ClangToolChain *> platformToolChainHash = findToolChains(platforms);

    const QHash<Abi::Architecture, QSet<BaseQtVersion *> > qtVersionsForArch = iosQtVersions();
    qCDebug(kitSetupLog) << "iOS Qt versions:";
    printQtVersions(qtVersionsForArch);
219

220
    const DebuggerItem *possibleDebugger = DebuggerItemManager::findByEngineType(LldbEngineType);
221
222
223
224
225
226
227
228
229
230
231
232
    const QVariant debuggerId = (possibleDebugger ? possibleDebugger->id() : QVariant());

    QSet<Kit *> existingKits = existingAutoDetectedIosKits();
    qCDebug(kitSetupLog) << "Existing auto-detected iOS kits:";
    printKits(existingKits);
    QSet<Kit *> resultingKits;
    // match existing kits and create missing kits
    foreach (const Platform &platform, platforms) {
        qCDebug(kitSetupLog) << "Guaranteeing kits for " << platform.name ;
        ClangToolChain *pToolchain = platformToolChainHash.value(platform);
        if (!pToolchain) {
            qCDebug(kitSetupLog) << "  - No tool chain found";
233
234
            continue;
        }
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
        Core::Id pDeviceType = deviceId(platform);
        QTC_ASSERT(pDeviceType.isValid(), continue);
        Abi::Architecture arch = pToolchain->targetAbi().architecture();

        QSet<BaseQtVersion *> qtVersions = qtVersionsForArch.value(arch);
        foreach (BaseQtVersion *qtVersion, qtVersions) {
            qCDebug(kitSetupLog) << "  - Qt version:" << qtVersion->displayName();
            Kit *kit = Utils::findOrDefault(existingKits, [&pDeviceType, &pToolchain, &qtVersion](const Kit *kit) {
                // we do not compare the sdk (thus automatically upgrading it in place if a
                // new Xcode is used). Change?
                return DeviceTypeKitInformation::deviceTypeId(kit) == pDeviceType
                        && ToolChainKitInformation::toolChain(kit) == pToolchain
                        && QtKitInformation::qtVersion(kit) == qtVersion;
            });
            QTC_ASSERT(!resultingKits.contains(kit), continue);
            if (kit) {
                qCDebug(kitSetupLog) << "    - Kit matches:" << kit->displayName();
                kit->blockNotification();
                setupKit(kit, pDeviceType, pToolchain, debuggerId, platform.sdkPath, qtVersion);
                kit->unblockNotification();
255
            } else {
256
257
258
259
260
261
262
263
264
                qCDebug(kitSetupLog) << "    - Setting up new kit";
                kit = new Kit;
                kit->blockNotification();
                kit->setAutoDetected(true);
                const QString baseDisplayName = tr("%1 %2").arg(platform.name, qtVersion->unexpandedDisplayName());
                kit->setUnexpandedDisplayName(baseDisplayName);
                setupKit(kit, pDeviceType, pToolchain, debuggerId, platform.sdkPath, qtVersion);
                kit->unblockNotification();
                KitManager::registerKit(kit);
265
            }
266
            resultingKits.insert(kit);
267
268
        }
    }
269
270
271
272
273
274
    // remove unused kits
    existingKits.subtract(resultingKits);
    qCDebug(kitSetupLog) << "Removing unused kits:";
    printKits(existingKits);
    foreach (Kit *kit, existingKits)
        KitManager::deregisterKit(kit);
275
276
}

hjk's avatar
hjk committed
277
278
279
static IosConfigurations *m_instance = 0;

QObject *IosConfigurations::instance()
280
{
281
282
283
    return m_instance;
}

hjk's avatar
hjk committed
284
285
286
287
288
289
void IosConfigurations::initialize()
{
    QTC_CHECK(m_instance == 0);
    m_instance = new IosConfigurations(0);
}

290
291
bool IosConfigurations::ignoreAllDevices()
{
hjk's avatar
hjk committed
292
    return m_instance->m_ignoreAllDevices;
293
294
295
296
}

void IosConfigurations::setIgnoreAllDevices(bool ignoreDevices)
{
hjk's avatar
hjk committed
297
298
299
300
    if (ignoreDevices != m_instance->m_ignoreAllDevices) {
        m_instance->m_ignoreAllDevices = ignoreDevices;
        m_instance->save();
        emit m_instance->updated();
301
302
303
304
305
    }
}

FileName IosConfigurations::developerPath()
{
hjk's avatar
hjk committed
306
    return m_instance->m_developerPath;
307
308
309
310
}

void IosConfigurations::save()
{
hjk's avatar
hjk committed
311
    QSettings *settings = Core::ICore::settings();
312
    settings->beginGroup(SettingsGroup);
313
    settings->setValue(ignoreAllDevicesKey, m_ignoreAllDevices);
314
315
316
317
318
319
320
321
322
323
324
    settings->endGroup();
}

IosConfigurations::IosConfigurations(QObject *parent)
    : QObject(parent)
{
    load();
}

void IosConfigurations::load()
{
hjk's avatar
hjk committed
325
    QSettings *settings = Core::ICore::settings();
326
    settings->beginGroup(SettingsGroup);
327
    m_ignoreAllDevices = settings->value(ignoreAllDevicesKey, false).toBool();
328
329
330
    settings->endGroup();
}

hjk's avatar
hjk committed
331
332
void IosConfigurations::updateSimulators()
{
333
334
    // currently we have just one simulator
    DeviceManager *devManager = DeviceManager::instance();
hjk's avatar
hjk committed
335
    Core::Id devId = Constants::IOS_SIMULATOR_DEVICE_ID;
336
    IDevice::ConstPtr dev = devManager->find(devId);
Fawzi Mohamed's avatar
Fawzi Mohamed committed
337
338
339
340
341
    if (dev.isNull()) {
        dev = IDevice::ConstPtr(new IosSimulator(devId));
        devManager->addDevice(dev);
    }
    IosSimulator::updateAvailableDevices();
342
343
}

344
345
void IosConfigurations::setDeveloperPath(const FileName &devPath)
{
346
    static bool hasDevPath = false;
hjk's avatar
hjk committed
347
348
349
    if (devPath != m_instance->m_developerPath) {
        m_instance->m_developerPath = devPath;
        m_instance->save();
350
351
352
353
354
        if (!hasDevPath && !devPath.isEmpty()) {
            hasDevPath = true;
            QTimer::singleShot(1000, IosDeviceManager::instance(), SLOT(monitorAvailableDevices()));
            m_instance->updateSimulators();
        }
hjk's avatar
hjk committed
355
        emit m_instance->updated();
356
357
358
    }
}

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
static ClangToolChain *createToolChain(const Platform &platform)
{
    ClangToolChain *toolChain = new ClangToolChain(ToolChain::AutoDetection);
    toolChain->setDisplayName(platform.name);
    toolChain->setPlatformCodeGenFlags(platform.backendFlags);
    toolChain->setPlatformLinkerFlags(platform.backendFlags);
    toolChain->resetToolChain(platform.compilerPath);
    return toolChain;
}

QList<ToolChain *> IosToolChainFactory::autoDetect(const QList<ToolChain *> &existingToolChains)
{
    QList<ClangToolChain *> existingClangToolChains = clangToolChains(existingToolChains);
    const QList<Platform> platforms = handledPlatforms();
    QList<ClangToolChain *> toolChains;
    toolChains.reserve(platforms.size());
    foreach (const Platform &platform, platforms) {
        ClangToolChain *toolChain = findToolChainForPlatform(platform, existingClangToolChains);
        if (!toolChain) {
378
379
            toolChain = createToolChain(platform);
            existingClangToolChains.append(toolChain);
380
        }
381
        toolChains.append(toolChain);
382
383
384
385
    }
    return Utils::transform(toolChains, [](ClangToolChain *tc) -> ToolChain * { return tc; });
}

386
387
} // namespace Internal
} // namespace Ios