qbsprojectmanager.cpp 8.13 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1
2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
Tobias Hunger's avatar
Tobias Hunger committed
4
5
6
7
8
9
10
11
12
** Contact: http://www.qt-project.org/legal
**
** 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
** 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.
Tobias Hunger's avatar
Tobias Hunger committed
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.
Tobias Hunger's avatar
Tobias Hunger committed
24
25
26
27
28
29
30
31
32
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "qbsprojectmanager.h"

33
#include "defaultpropertyprovider.h"
Tobias Hunger's avatar
Tobias Hunger committed
34
35
36
37
38
#include "qbslogsink.h"
#include "qbsproject.h"
#include "qbsprojectmanagerconstants.h"
#include "qbsprojectmanagerplugin.h"

Christian Kandeler's avatar
Christian Kandeler committed
39
#include <coreplugin/icore.h>
40
#include <coreplugin/messagemanager.h>
41
42
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/kit.h>
Tobias Hunger's avatar
Tobias Hunger committed
43
44
45
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/projectexplorer.h>
#include <qmljstools/qmljstoolsconstants.h>
46
47
#include <qtsupport/baseqtversion.h>
#include <qtsupport/qtkitinformation.h>
Tobias Hunger's avatar
Tobias Hunger committed
48
49
50
51

#include <QVariantMap>

#include <qbs.h>
52
#include <qtprofilesetup.h>
Tobias Hunger's avatar
Tobias Hunger committed
53

54
const QChar sep = QLatin1Char('.');
Tobias Hunger's avatar
Tobias Hunger committed
55

56
57
static QString qtcProfileGroup() { return QLatin1String("preferences.qtcreator.kit"); }
static QString qtcProfilePrefix() { return qtcProfileGroup() + sep; }
Tobias Hunger's avatar
Tobias Hunger committed
58
59
60

namespace QbsProjectManager {

61
qbs::Settings *QbsManager::m_settings = 0;
Tobias Hunger's avatar
Tobias Hunger committed
62
Internal::QbsLogSink *QbsManager::m_logSink = 0;
Tobias Hunger's avatar
Tobias Hunger committed
63

64
QbsManager::QbsManager() :
65
    m_defaultPropertyProvider(new DefaultPropertyProvider)
Tobias Hunger's avatar
Tobias Hunger committed
66
{
Christian Kandeler's avatar
Christian Kandeler committed
67
    m_settings = new qbs::Settings(Core::ICore::userResourcePath());
68

Tobias Hunger's avatar
Tobias Hunger committed
69
    setObjectName(QLatin1String("QbsProjectManager"));
70
71
72
73
74
75
76
77
    connect(ProjectExplorer::KitManager::instance(), &ProjectExplorer::KitManager::kitsLoaded, this,
            [this]() { m_kitsToBeSetupForQbs = ProjectExplorer::KitManager::kits(); } );
    connect(ProjectExplorer::KitManager::instance(), &ProjectExplorer::KitManager::kitAdded, this,
            &QbsManager::addProfileFromKit);
    connect(ProjectExplorer::KitManager::instance(), &ProjectExplorer::KitManager::kitUpdated, this,
            &QbsManager::handleKitUpdate);
    connect(ProjectExplorer::KitManager::instance(), &ProjectExplorer::KitManager::kitRemoved, this,
            &QbsManager::handleKitRemoval);
Tobias Hunger's avatar
Tobias Hunger committed
78

79
    m_logSink = new Internal::QbsLogSink(this);
80
81
82
    int level = qbs::LoggerWarning;
    const QString levelEnv = QString::fromLocal8Bit(qgetenv("QBS_LOG_LEVEL"));
    if (!levelEnv.isEmpty()) {
83
84
85
86
87
88
89
90
91
        bool ok = false;
        int tmp = levelEnv.toInt(&ok);
        if (ok) {
            if (tmp < static_cast<int>(qbs::LoggerMinLevel))
                tmp = static_cast<int>(qbs::LoggerMinLevel);
            if (tmp > static_cast<int>(qbs::LoggerMaxLevel))
                tmp = static_cast<int>(qbs::LoggerMaxLevel);
            level = tmp;
        }
92
    }
93
    m_logSink->setLogLevel(static_cast<qbs::LoggerLevel>(level));
Tobias Hunger's avatar
Tobias Hunger committed
94
95
96
97
}

QbsManager::~QbsManager()
{
98
    delete m_defaultPropertyProvider;
Tobias Hunger's avatar
Tobias Hunger committed
99
100
101
102
103
104
105
106
107
108
    delete m_settings;
}

QString QbsManager::mimeType() const
{
    return QLatin1String(QmlJSTools::Constants::QBS_MIMETYPE);
}

ProjectExplorer::Project *QbsManager::openProject(const QString &fileName, QString *errorString)
{
109
110
    if (!QFileInfo(fileName).isFile()) {
        if (errorString)
111
            *errorString = tr("Failed opening project \"%1\": Project is not a file.")
112
113
114
                .arg(fileName);
        return 0;
    }
Tobias Hunger's avatar
Tobias Hunger committed
115
116
117
118

    return new Internal::QbsProject(this, fileName);
}

119
QString QbsManager::profileForKit(ProjectExplorer::Kit *k)
Tobias Hunger's avatar
Tobias Hunger committed
120
121
122
{
    if (!k)
        return QString();
123
    updateProfileIfNecessary(k);
124
    return m_settings->value(qtcProfilePrefix() + k->id().toString()).toString();
Tobias Hunger's avatar
Tobias Hunger committed
125
126
127
128
}

void QbsManager::setProfileForKit(const QString &name, const ProjectExplorer::Kit *k)
{
129
    m_settings->setValue(qtcProfilePrefix() + k->id().toString(), name);
Tobias Hunger's avatar
Tobias Hunger committed
130
131
}

132
133
134
135
136
137
void QbsManager::updateProfileIfNecessary(ProjectExplorer::Kit *kit)
{
    if (m_kitsToBeSetupForQbs.removeOne(kit)) // kit in list <=> yes, it is necessary
        addProfileFromKit(kit);
}

Tobias Hunger's avatar
Tobias Hunger committed
138
139
void QbsManager::addProfile(const QString &name, const QVariantMap &data)
{
140
    qbs::Profile profile(name, settings());
141
142
    const QVariantMap::ConstIterator cend = data.constEnd();
    for (QVariantMap::ConstIterator it = data.constBegin(); it != cend; ++it)
143
        profile.setValue(it.key(), it.value());
Tobias Hunger's avatar
Tobias Hunger committed
144
145
}

146
147
148
149
150
151
152
void QbsManager::addQtProfileFromKit(const QString &profileName, const ProjectExplorer::Kit *k)
{
    const QtSupport::BaseQtVersion * const qt = QtSupport::QtKitInformation::qtVersion(k);
    if (!qt)
        return;

    qbs::QtEnvironment qtEnv;
153
154
155
156
157
158
    const QList<ProjectExplorer::Abi> abi = qt->qtAbis();
    if (!abi.empty()) {
        qtEnv.architecture = ProjectExplorer::Abi::toString(abi.first().architecture());
        if (abi.first().wordWidth() == 64)
            qtEnv.architecture.append(QLatin1String("_64"));
    }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    qtEnv.binaryPath = qt->binPath().toString();
    if (qt->hasDebugBuild())
        qtEnv.buildVariant << QLatin1String("debug");
    if (qt->hasReleaseBuild())
        qtEnv.buildVariant << QLatin1String("release");
    qtEnv.documentationPath = qt->docsPath().toString();
    qtEnv.includePath = qt->headerPath().toString();
    qtEnv.libraryPath = qt->libraryPath().toString();
    qtEnv.pluginPath = qt->pluginPath().toString();
    qtEnv.mkspecBasePath = qt->mkspecsPath().toString();
    qtEnv.mkspecName = qt->mkspec().toString();
    qtEnv.mkspecPath = qt->mkspecPath().toString();
    qtEnv.qtNameSpace = qt->qtNamespace();
    qtEnv.qtLibInfix = qt->qtLibInfix();
    qtEnv.qtVersion = qt->qtVersionString();
174
175
176
    qtEnv.qtMajorVersion = qt->qtVersion().majorVersion;
    qtEnv.qtMinorVersion = qt->qtVersion().minorVersion;
    qtEnv.qtPatchVersion = qt->qtVersion().patchVersion;
177
178
179
180
181
    qtEnv.frameworkBuild = qt->isFrameworkBuild();
    qtEnv.configItems = qt->configValues();
    qtEnv.qtConfigItems = qt->qtConfigValues();
    const qbs::ErrorInfo errorInfo = qbs::setupQtProfile(profileName, settings(), qtEnv);
    if (errorInfo.hasError()) {
182
        Core::MessageManager::write(tr("Failed to set up kit for Qbs: %1")
183
184
185
186
                .arg(errorInfo.toString()), Core::MessageManager::ModeSwitch);
    }
}

Tobias Hunger's avatar
Tobias Hunger committed
187
188
void QbsManager::addProfileFromKit(const ProjectExplorer::Kit *k)
{
189
    const QString name = QString::fromLatin1("qtc_") + k->fileSystemFriendlyName();
Tobias Hunger's avatar
Tobias Hunger committed
190
    setProfileForKit(name, k);
191
    addQtProfileFromKit(name, k);
Tobias Hunger's avatar
Tobias Hunger committed
192

193
194
195
196
197
198
    // set up properties:
    QVariantMap data = m_defaultPropertyProvider->properties(k, QVariantMap());
    QList<PropertyProvider *> providerList = ExtensionSystem::PluginManager::getObjects<PropertyProvider>();
    foreach (PropertyProvider *provider, providerList) {
        if (provider->canHandle(k))
            data = provider->properties(k, data);
Tobias Hunger's avatar
Tobias Hunger committed
199
200
201
202
203
    }

    addProfile(name, data);
}

204
void QbsManager::handleKitUpdate(ProjectExplorer::Kit *kit)
Tobias Hunger's avatar
Tobias Hunger committed
205
{
206
207
208
    m_kitsToBeSetupForQbs.removeOne(kit);
    addProfileFromKit(kit);
}
Tobias Hunger's avatar
Tobias Hunger committed
209

210
211
212
213
214
215
216
void QbsManager::handleKitRemoval(ProjectExplorer::Kit *kit)
{
    m_kitsToBeSetupForQbs.removeOne(kit);
    const QString key = qtcProfilePrefix() + kit->id().toString();
    const QString profileName = m_settings->value(key).toString();
    m_settings->remove(key);
    qbs::Profile(profileName, m_settings).removeProfile();
Tobias Hunger's avatar
Tobias Hunger committed
217
218
219
}

} // namespace QbsProjectManager