cmakeprojectmanager.cpp 10.1 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
#include "cmakeprojectmanager.h"
#include "cmakeprojectconstants.h"
#include "cmakeproject.h"

dt's avatar
dt committed
34
#include <coreplugin/icore.h>
con's avatar
con committed
35
36
#include <coreplugin/uniqueidmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
37
#include <projectexplorer/environment.h>
dt's avatar
dt committed
38
39
#include <qtconcurrent/QtConcurrentTools>
#include <QtCore/QtConcurrentRun>
40
#include <QtCore/QCoreApplication>
41
#include <QtCore/QSettings>
dt's avatar
dt committed
42
#include <QtGui/QFormLayout>
43
#include <QtGui/QBoxLayout>
44
45
#include <QtGui/QDesktopServices>
#include <QtGui/QApplication>
46
#include <QtGui/QLabel>
47
48
#include <QtGui/QGroupBox>
#include <QtGui/QSpacerItem>
con's avatar
con committed
49
50
51

using namespace CMakeProjectManager::Internal;

52
53
CMakeManager::CMakeManager(CMakeSettingsPage *cmakeSettingsPage)
    : m_settingsPage(cmakeSettingsPage)
con's avatar
con committed
54
{
55
56
    Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
    m_projectContext = uidm->uniqueIdentifier(CMakeProjectManager::Constants::PROJECTCONTEXT);
57
    m_projectLanguage = uidm->uniqueIdentifier(ProjectExplorer::Constants::LANG_CXX);
58
59
}

con's avatar
con committed
60
61
62
63
64
65
66
67
68
69
70
71
int CMakeManager::projectContext() const
{
    return m_projectContext;
}

int CMakeManager::projectLanguage() const
{
    return m_projectLanguage;
}

ProjectExplorer::Project *CMakeManager::openProject(const QString &fileName)
{
Tobias Hunger's avatar
Tobias Hunger committed
72
    // TODO check whether this project is already opened
con's avatar
con committed
73
74
75
76
77
78
79
    return new CMakeProject(this, fileName);
}

QString CMakeManager::mimeType() const
{
    return Constants::CMAKEMIMETYPE;
}
80

dt's avatar
dt committed
81
82
83
84
85
QString CMakeManager::cmakeExecutable() const
{
    return m_settingsPage->cmakeExecutable();
}

86
87
88
89
90
91
92
93
94
95
bool CMakeManager::isCMakeExecutableValid() const
{
    return m_settingsPage->isCMakeExecutableValid();
}

void CMakeManager::setCMakeExecutable(const QString &executable)
{
    m_settingsPage->setCMakeExecutable(executable);
}

96
97
98
99
100
bool CMakeManager::hasCodeBlocksMsvcGenerator() const
{
    return m_settingsPage->hasCodeBlocksMsvcGenerator();
}

dt's avatar
dt committed
101
102
103
104
// TODO need to refactor this out
// we probably want the process instead of this function
// cmakeproject then could even run the cmake process in the background, adding the files afterwards
// sounds like a plan
105
void CMakeManager::createXmlFile(QProcess *proc, const QStringList &arguments, const QString &sourceDirectory, const QDir &buildDirectory, const ProjectExplorer::Environment &env, const QString &generator)
dt's avatar
dt committed
106
107
108
109
110
111
112
113
114
115
116
{
    // We create a cbp file, only if we didn't find a cbp file in the base directory
    // Yet that can still override cbp files in subdirectories
    // And we are creating tons of files in the source directories
    // All of that is not really nice.
    // The mid term plan is to move away from the CodeBlocks Generator and use our own
    // QtCreator generator, which actually can be very similar to the CodeBlock Generator

    // TODO we need to pass on the same paremeters as the cmakestep
    QString buildDirectoryPath = buildDirectory.absolutePath();
    buildDirectory.mkpath(buildDirectoryPath);
117
118
119
    proc->setWorkingDirectory(buildDirectoryPath);
    proc->setProcessChannelMode(QProcess::MergedChannels);
    proc->setEnvironment(env.toStringList());
120

121
    const QString srcdir = buildDirectory.exists(QLatin1String("CMakeCache.txt")) ? QString(QLatin1Char('.')) : sourceDirectory;
122
    proc->start(cmakeExecutable(), QStringList() << srcdir << arguments << generator);
dt's avatar
dt committed
123
124
}

125
126
127
128
129
130
131
QString CMakeManager::findCbpFile(const QDir &directory)
{
    // Find the cbp file
    //   TODO the cbp file is named like the project() command in the CMakeList.txt file
    //   so this method below could find the wrong cbp file, if the user changes the project()
    //   2name
    foreach (const QString &cbpFile , directory.entryList()) {
132
133
        if (cbpFile.endsWith(QLatin1String(".cbp")))
            return directory.path() + QLatin1Char('/') + cbpFile;
134
    }
135
    return QString();
136
137
}

138
139
140
141
// This code is duplicated from qtversionmanager
QString CMakeManager::qtVersionForQMake(const QString &qmakePath)
{
    QProcess qmake;
142
    qmake.start(qmakePath, QStringList(QLatin1String("--version")));
143
144
145
    if (!qmake.waitForFinished())
        return false;
    QString output = qmake.readAllStandardOutput();
146
    QRegExp regexp(QLatin1String("(QMake version|Qmake version:)[\\s]*([\\d.]*)"));
147
    regexp.indexIn(output);
148
149
    if (regexp.cap(2).startsWith(QLatin1String("2."))) {
        QRegExp regexp2(QLatin1String("Using Qt version[\\s]*([\\d\\.]*)"));
150
151
152
153
154
        regexp2.indexIn(output);
        return regexp2.cap(1);
    }
    return QString();
}
155

156
157
/////
// CMakeSettingsPage
158
////
159

160
161

CMakeSettingsPage::CMakeSettingsPage()
162
    :  m_pathchooser(0), m_process(0)
163
{
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    Core::ICore *core = Core::ICore::instance();
    QSettings * settings = core->settings();
    settings->beginGroup(QLatin1String("CMakeSettings"));
    m_cmakeExecutable = settings->value(QLatin1String("cmakeExecutable")).toString();
    QFileInfo fi(m_cmakeExecutable);
    if (!fi.exists() || !fi.isExecutable())
        m_cmakeExecutable = findCmakeExecutable();
    fi.setFile(m_cmakeExecutable);
    if (fi.exists() && fi.isExecutable()) {
        // Run it to find out more
        m_state = RUNNING;
        startProcess();
    } else {
        m_state = INVALID;
    }
dt's avatar
dt committed
179

180
    settings->endGroup();
181
182
}

183
void CMakeSettingsPage::startProcess()
184
{
185
    m_process = new QProcess();
dt's avatar
dt committed
186

187
188
    connect(m_process, SIGNAL(finished(int)),
            this, SLOT(cmakeFinished()));
dt's avatar
dt committed
189

190
191
    m_process->start(m_cmakeExecutable, QStringList(QLatin1String("--help")));
    m_process->waitForStarted();
dt's avatar
dt committed
192
193
}

194
void CMakeSettingsPage::cmakeFinished()
dt's avatar
dt committed
195
{
196
197
198
199
    if (m_process) {
        QString response = m_process->readAll();
        QRegExp versionRegexp(QLatin1String("^cmake version ([\\d\\.]*)"));
        versionRegexp.indexIn(response);
dt's avatar
dt committed
200

201
202
203
204
205
        //m_supportsQtCreator = response.contains(QLatin1String("QtCreator"));
        m_hasCodeBlocksMsvcGenerator = response.contains(QLatin1String("CodeBlocks - NMake Makefiles"));
        m_version = versionRegexp.cap(1);
        if (!(versionRegexp.capturedTexts().size() > 3))
            m_version += QLatin1Char('.') + versionRegexp.cap(3);
dt's avatar
dt committed
206

207
208
209
210
211
212
213
214
        if (m_version.isEmpty())
            m_state = INVALID;
        else
            m_state = VALID;

        m_process->deleteLater();
        m_process = 0;
    }
215
216
}

217
bool CMakeSettingsPage::isCMakeExecutableValid()
dt's avatar
dt committed
218
{
219
220
221
222
223
224
225
226
    if (m_state == RUNNING) {
        disconnect(m_process, SIGNAL(finished(int)),
                   this, SLOT(cmakeFinished()));
        m_process->waitForFinished();
        // Parse the output now
        cmakeFinished();
    }
    return m_state == VALID;
dt's avatar
dt committed
227
228
}

229
CMakeSettingsPage::~CMakeSettingsPage()
dt's avatar
dt committed
230
{
231
232
233
    if (m_process)
        m_process->waitForFinished();
    delete m_process;
234
235
236
237
238
}

QString CMakeSettingsPage::findCmakeExecutable() const
{
    ProjectExplorer::Environment env = ProjectExplorer::Environment::systemEnvironment();
239
    return env.searchInPath(QLatin1String("cmake"));
240
241
}

242
243
QString CMakeSettingsPage::id() const
{
244
    return QLatin1String("Z.CMake");
245
}
246

247
QString CMakeSettingsPage::displayName() const
248
{
249
    return tr("CMake");
250
251
252
253
}

QString CMakeSettingsPage::category() const
{
254
    return QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY);
255
256
}

257
QString CMakeSettingsPage::displayCategory() const
258
{
259
260
    return QCoreApplication::translate("ProjectExplorer",
                                       ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY);
261
262
}

263
264
QIcon CMakeSettingsPage::categoryIcon() const
{
265
    return QIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));
266
267
}

268
269
QWidget *CMakeSettingsPage::createPage(QWidget *parent)
{
270
    QWidget *outerWidget = new QWidget(parent);
271
    QFormLayout *formLayout = new QFormLayout(outerWidget);
272
    m_pathchooser = new Utils::PathChooser;
273
    m_pathchooser->setExpectedKind(Utils::PathChooser::Command);
274
    formLayout->addRow(tr("Executable:"), m_pathchooser);
275
    formLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
276
    m_pathchooser->setPath(cmakeExecutable());
277
    return outerWidget;
278
279
}

280
281
282
283
284
285
286
287
288
289
290
291
292
void CMakeSettingsPage::updateInfo()
{
    QFileInfo fi(m_cmakeExecutable);
    if (fi.exists() && fi.isExecutable()) {
        // Run it to find out more
        m_state = RUNNING;
        startProcess();
    } else {
        m_state = INVALID;
    }
    saveSettings();
}

293
294
295
void CMakeSettingsPage::saveSettings() const
{
    QSettings *settings = Core::ICore::instance()->settings();
296
    settings->beginGroup(QLatin1String("CMakeSettings"));
297
    settings->setValue(QLatin1String("cmakeExecutable"), m_cmakeExecutable);
298
299
300
301
302
    settings->endGroup();
}

void CMakeSettingsPage::apply()
{
303
304
305
306
    if (m_cmakeExecutable == m_pathchooser->path())
        return;
    m_cmakeExecutable = m_pathchooser->path();
    updateInfo();
307
308
309
310
311
312
313
314
315
}

void CMakeSettingsPage::finish()
{

}

QString CMakeSettingsPage::cmakeExecutable() const
{
316
    return m_cmakeExecutable;
317
318
}

319
void CMakeSettingsPage::setCMakeExecutable(const QString &executable)
320
{
321
322
323
324
    if (m_cmakeExecutable == executable)
        return;
    m_cmakeExecutable = executable;
    updateInfo();
325
326
}

327
bool CMakeSettingsPage::hasCodeBlocksMsvcGenerator() const
328
{
329
    return m_hasCodeBlocksMsvcGenerator;
330
}