cmakeprojectmanager.cpp 15.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) 2012 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
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

33
#include "cmakeopenprojectwizard.h"
con's avatar
con committed
34 35 36 37
#include "cmakeprojectmanager.h"
#include "cmakeprojectconstants.h"
#include "cmakeproject.h"

38
#include <utils/synchronousprocess.h>
39
#include <utils/qtcprocess.h>
40

dt's avatar
dt committed
41
#include <coreplugin/icore.h>
42
#include <coreplugin/id.h>
43 44 45
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actioncontainer.h>
con's avatar
con committed
46
#include <projectexplorer/projectexplorerconstants.h>
47
#include <projectexplorer/projectexplorer.h>
48
#include <utils/QtConcurrentTools>
49 50 51 52 53 54 55 56 57 58 59
#include <QtConcurrentRun>
#include <QCoreApplication>
#include <QSettings>
#include <QDateTime>
#include <QFormLayout>
#include <QBoxLayout>
#include <QDesktopServices>
#include <QApplication>
#include <QLabel>
#include <QGroupBox>
#include <QSpacerItem>
con's avatar
con committed
60 61 62

using namespace CMakeProjectManager::Internal;

63 64
CMakeManager::CMakeManager(CMakeSettingsPage *cmakeSettingsPage)
    : m_settingsPage(cmakeSettingsPage)
con's avatar
con committed
65
{
66
    ProjectExplorer::ProjectExplorerPlugin *projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance();
Robert Loehning's avatar
Robert Loehning committed
67 68
    connect(projectExplorer, SIGNAL(aboutToShowContextMenu(ProjectExplorer::Project*,ProjectExplorer::Node*)),
            this, SLOT(updateContextMenu(ProjectExplorer::Project*,ProjectExplorer::Node*)));
69 70

    Core::ActionContainer *mbuild =
Eike Ziller's avatar
Eike Ziller committed
71
            Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
72
    Core::ActionContainer *mproject =
Eike Ziller's avatar
Eike Ziller committed
73
            Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
74
    Core::ActionContainer *msubproject =
Eike Ziller's avatar
Eike Ziller committed
75
            Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
76

77 78
    const Core::Context projectContext(CMakeProjectManager::Constants::PROJECTCONTEXT);

Jarek Kobus's avatar
Jarek Kobus committed
79
    m_runCMakeAction = new QAction(QIcon(), tr("Run CMake"), this);
Eike Ziller's avatar
Eike Ziller committed
80 81
    Core::Command *command = Core::ActionManager::registerAction(m_runCMakeAction,
                                                                 Constants::RUNCMAKE, projectContext);
82
    command->setAttribute(Core::Command::CA_Hide);
83
    mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_DEPLOY);
84 85
    connect(m_runCMakeAction, SIGNAL(triggered()), this, SLOT(runCMake()));

Jarek Kobus's avatar
Jarek Kobus committed
86
    m_runCMakeActionContextMenu = new QAction(QIcon(), tr("Run CMake"), this);
Eike Ziller's avatar
Eike Ziller committed
87 88
    command = Core::ActionManager::registerAction(m_runCMakeActionContextMenu,
                                                  Constants::RUNCMAKECONTEXTMENU, projectContext);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    command->setAttribute(Core::Command::CA_Hide);
    mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
    msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
    connect(m_runCMakeActionContextMenu, SIGNAL(triggered()), this, SLOT(runCMakeContextMenu()));

}

void CMakeManager::updateContextMenu(ProjectExplorer::Project *project, ProjectExplorer::Node *node)
{
    Q_UNUSED(node);
    m_contextProject = project;
}

void CMakeManager::runCMake()
{
104
    runCMake(ProjectExplorer::ProjectExplorerPlugin::currentProject());
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
}

void CMakeManager::runCMakeContextMenu()
{
    runCMake(m_contextProject);
}

void CMakeManager::runCMake(ProjectExplorer::Project *project)
{
    if (!project)
        return;
    CMakeProject *cmakeProject = qobject_cast<CMakeProject *>(project);
    if (!cmakeProject)
        return;

    if (!cmakeProject->activeTarget())
        return;
    if (!cmakeProject->activeTarget()->activeBuildConfiguration())
        return;
    CMakeBuildConfiguration *bc = cmakeProject->activeTarget()->activeBuildConfiguration();
    CMakeOpenProjectWizard copw(this,
                                cmakeProject->projectDirectory(),
                                bc->buildDirectory(),
                                CMakeOpenProjectWizard::WantToUpdate,
                                bc->environment());
    if (copw.exec() == QDialog::Accepted) {
        cmakeProject->parseCMakeLists();
    }
133 134
}

135
ProjectExplorer::Project *CMakeManager::openProject(const QString &fileName, QString *errorString)
con's avatar
con committed
136
{
137
    Q_UNUSED(errorString)
Tobias Hunger's avatar
Tobias Hunger committed
138
    // TODO check whether this project is already opened
con's avatar
con committed
139 140 141 142 143 144 145
    return new CMakeProject(this, fileName);
}

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

dt's avatar
dt committed
147 148 149 150 151
QString CMakeManager::cmakeExecutable() const
{
    return m_settingsPage->cmakeExecutable();
}

152 153 154 155 156 157 158 159 160 161
bool CMakeManager::isCMakeExecutableValid() const
{
    return m_settingsPage->isCMakeExecutableValid();
}

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

162 163 164 165 166
bool CMakeManager::hasCodeBlocksMsvcGenerator() const
{
    return m_settingsPage->hasCodeBlocksMsvcGenerator();
}

167
// need to refactor this out
dt's avatar
dt committed
168 169 170
// 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
171
void CMakeManager::createXmlFile(Utils::QtcProcess *proc, const QString &arguments,
172 173
                                 const QString &sourceDirectory, const QDir &buildDirectory,
                                 const Utils::Environment &env, const QString &generator)
dt's avatar
dt committed
174 175 176 177 178 179 180 181 182
{
    // 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
    QString buildDirectoryPath = buildDirectory.absolutePath();
    buildDirectory.mkpath(buildDirectoryPath);
183
    proc->setWorkingDirectory(buildDirectoryPath);
184
    proc->setEnvironment(env);
185

186 187
    const QString srcdir = buildDirectory.exists(QLatin1String("CMakeCache.txt")) ?
                QString(QLatin1Char('.')) : sourceDirectory;
188 189 190 191 192 193
    QString args;
    Utils::QtcProcess::addArg(&args, srcdir);
    Utils::QtcProcess::addArgs(&args, arguments);
    Utils::QtcProcess::addArg(&args, generator);
    proc->setCommand(cmakeExecutable(), args);
    proc->start();
dt's avatar
dt committed
194 195
}

196 197 198
QString CMakeManager::findCbpFile(const QDir &directory)
{
    // Find the cbp file
199
    //   the cbp file is named like the project() command in the CMakeList.txt file
200 201
    //   so this method below could find the wrong cbp file, if the user changes the project()
    //   2name
202 203
    QDateTime t;
    QString file;
204
    foreach (const QString &cbpFile , directory.entryList()) {
205 206 207 208 209 210 211
        if (cbpFile.endsWith(QLatin1String(".cbp"))) {
            QFileInfo fi(directory.path() + QLatin1Char('/') + cbpFile);
            if (t.isNull() || fi.lastModified() > t) {
                file = directory.path() + QLatin1Char('/') + cbpFile;
                t = fi.lastModified();
            }
        }
212
    }
213
    return file;
214 215
}

216 217 218 219
// This code is duplicated from qtversionmanager
QString CMakeManager::qtVersionForQMake(const QString &qmakePath)
{
    QProcess qmake;
220
    qmake.start(qmakePath, QStringList(QLatin1String("--version")));
221 222 223 224 225 226 227
    if (!qmake.waitForStarted()) {
        qWarning("Cannot start '%s': %s", qPrintable(qmakePath), qPrintable(qmake.errorString()));
        return QString();
    }
    if (!qmake.waitForFinished())      {
        Utils::SynchronousProcess::stopProcess(qmake);
        qWarning("Timeout running '%s'.", qPrintable(qmakePath));
228
        return QString();
229
    }
230
    QString output = qmake.readAllStandardOutput();
231
    QRegExp regexp(QLatin1String("(QMake version|Qmake version:)[\\s]*([\\d.]*)"));
232
    regexp.indexIn(output);
233 234
    if (regexp.cap(2).startsWith(QLatin1String("2."))) {
        QRegExp regexp2(QLatin1String("Using Qt version[\\s]*([\\d\\.]*)"));
235 236 237 238 239
        regexp2.indexIn(output);
        return regexp2.cap(1);
    }
    return QString();
}
240

241 242
/////
// CMakeSettingsPage
243
////
244

245 246

CMakeSettingsPage::CMakeSettingsPage()
dt's avatar
dt committed
247
    :  m_pathchooser(0)
248
{
249 250 251 252 253 254 255
    setId(QLatin1String("Z.CMake"));
    setDisplayName(tr("CMake"));
    setCategory(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY));
    setDisplayCategory(QCoreApplication::translate("ProjectExplorer",
       ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY));
    setCategoryIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));

dt's avatar
dt committed
256 257
    m_userCmake.process = 0;
    m_pathCmake.process = 0;
258 259
    m_userCmake.hasCodeBlocksMsvcGenerator = false;
    m_pathCmake.hasCodeBlocksMsvcGenerator = false;
hjk's avatar
hjk committed
260
    QSettings *settings = Core::ICore::settings();
261
    settings->beginGroup(QLatin1String("CMakeSettings"));
dt's avatar
dt committed
262
    m_userCmake.executable = settings->value(QLatin1String("cmakeExecutable")).toString();
263
    settings->endGroup();
dt's avatar
dt committed
264 265 266 267

    updateInfo(&m_userCmake);
    m_pathCmake.executable = findCmakeExecutable();
    updateInfo(&m_pathCmake);
268 269
}

dt's avatar
dt committed
270
void CMakeSettingsPage::startProcess(CMakeValidator *cmakeValidator)
271
{
dt's avatar
dt committed
272 273 274 275 276 277 278 279
    cmakeValidator->process = new QProcess();

    if (cmakeValidator == &m_pathCmake) // ugly
        connect(cmakeValidator->process, SIGNAL(finished(int)),
                this, SLOT(userCmakeFinished()));
    else
        connect(cmakeValidator->process, SIGNAL(finished(int)),
                this, SLOT(pathCmakeFinished()));
dt's avatar
dt committed
280

dt's avatar
dt committed
281 282 283
    cmakeValidator->process->start(cmakeValidator->executable, QStringList(QLatin1String("--help")));
    cmakeValidator->process->waitForStarted();
}
dt's avatar
dt committed
284

dt's avatar
dt committed
285 286 287
void CMakeSettingsPage::userCmakeFinished()
{
    cmakeFinished(&m_userCmake);
dt's avatar
dt committed
288 289
}

dt's avatar
dt committed
290
void CMakeSettingsPage::pathCmakeFinished()
dt's avatar
dt committed
291
{
dt's avatar
dt committed
292 293 294 295 296 297
    cmakeFinished(&m_pathCmake);
}

void CMakeSettingsPage::cmakeFinished(CMakeValidator *cmakeValidator) const
{
    if (cmakeValidator->process) {
dt's avatar
dt committed
298
        cmakeValidator->process->waitForFinished();
dt's avatar
dt committed
299
        QString response = cmakeValidator->process->readAll();
300 301
        QRegExp versionRegexp(QLatin1String("^cmake version ([\\d\\.]*)"));
        versionRegexp.indexIn(response);
dt's avatar
dt committed
302

303
        //m_supportsQtCreator = response.contains(QLatin1String("QtCreator"));
dt's avatar
dt committed
304 305
        cmakeValidator->hasCodeBlocksMsvcGenerator = response.contains(QLatin1String("CodeBlocks - NMake Makefiles"));
        cmakeValidator->version = versionRegexp.cap(1);
306
        if (!(versionRegexp.capturedTexts().size() > 3))
dt's avatar
dt committed
307
            cmakeValidator->version += QLatin1Char('.') + versionRegexp.cap(3);
dt's avatar
dt committed
308

dt's avatar
dt committed
309 310
        if (cmakeValidator->version.isEmpty())
            cmakeValidator->state = CMakeValidator::INVALID;
311
        else
dt's avatar
dt committed
312
            cmakeValidator->state = CMakeValidator::VALID;
313

dt's avatar
dt committed
314 315
        cmakeValidator->process->deleteLater();
        cmakeValidator->process = 0;
316
    }
317 318
}

dt's avatar
dt committed
319
bool CMakeSettingsPage::isCMakeExecutableValid() const
dt's avatar
dt committed
320
{
dt's avatar
dt committed
321 322
    if (m_userCmake.state == CMakeValidator::RUNNING) {
        disconnect(m_userCmake.process, SIGNAL(finished(int)),
dt's avatar
dt committed
323
                   this, SLOT(userCmakeFinished()));
dt's avatar
dt committed
324 325 326 327 328 329 330 331 332
        m_userCmake.process->waitForFinished();
        // Parse the output now
        cmakeFinished(&m_userCmake);
    }

    if (m_userCmake.state == CMakeValidator::VALID)
        return true;
    if (m_pathCmake.state == CMakeValidator::RUNNING) {
        disconnect(m_userCmake.process, SIGNAL(finished(int)),
dt's avatar
dt committed
333
                   this, SLOT(pathCmakeFinished()));
dt's avatar
dt committed
334
        m_pathCmake.process->waitForFinished();
335
        // Parse the output now
dt's avatar
dt committed
336
        cmakeFinished(&m_pathCmake);
337
    }
dt's avatar
dt committed
338
    return m_pathCmake.state == CMakeValidator::VALID;
dt's avatar
dt committed
339 340
}

341
CMakeSettingsPage::~CMakeSettingsPage()
dt's avatar
dt committed
342
{
dt's avatar
dt committed
343 344 345 346 347 348
    if (m_userCmake.process)
        m_userCmake.process->waitForFinished();
    delete m_userCmake.process;
    if (m_pathCmake.process)
        m_pathCmake.process->waitForFinished();
    delete m_pathCmake.process;
349 350 351 352
}

QString CMakeSettingsPage::findCmakeExecutable() const
{
353
    Utils::Environment env = Utils::Environment::systemEnvironment();
354
    return env.searchInPath(QLatin1String("cmake"));
355 356 357 358
}

QWidget *CMakeSettingsPage::createPage(QWidget *parent)
{
359
    QWidget *outerWidget = new QWidget(parent);
360
    QFormLayout *formLayout = new QFormLayout(outerWidget);
361
    formLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
362
    m_pathchooser = new Utils::PathChooser;
363
    m_pathchooser->setExpectedKind(Utils::PathChooser::ExistingCommand);
364
    formLayout->addRow(tr("Executable:"), m_pathchooser);
365
    formLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
dt's avatar
dt committed
366
    m_pathchooser->setPath(m_userCmake.executable);
367
    return outerWidget;
368 369
}

dt's avatar
dt committed
370
void CMakeSettingsPage::updateInfo(CMakeValidator *cmakeValidator)
371
{
dt's avatar
dt committed
372
    QFileInfo fi(cmakeValidator->executable);
373 374
    if (fi.exists() && fi.isExecutable()) {
        // Run it to find out more
dt's avatar
dt committed
375 376
        cmakeValidator->state = CMakeValidator::RUNNING;
        startProcess(cmakeValidator);
377
    } else {
dt's avatar
dt committed
378
        cmakeValidator->state = CMakeValidator::INVALID;
379 380 381 382
    }
    saveSettings();
}

383 384
void CMakeSettingsPage::saveSettings() const
{
hjk's avatar
hjk committed
385
    QSettings *settings = Core::ICore::settings();
386
    settings->beginGroup(QLatin1String("CMakeSettings"));
dt's avatar
dt committed
387
    settings->setValue(QLatin1String("cmakeExecutable"), m_userCmake.executable);
388 389 390 391 392
    settings->endGroup();
}

void CMakeSettingsPage::apply()
{
393 394
    if (!m_pathchooser) // page was never shown
        return;
dt's avatar
dt committed
395
    if (m_userCmake.executable == m_pathchooser->path())
396
        return;
dt's avatar
dt committed
397 398
    m_userCmake.executable = m_pathchooser->path();
    updateInfo(&m_userCmake);
399 400 401 402 403 404 405 406 407
}

void CMakeSettingsPage::finish()
{

}

QString CMakeSettingsPage::cmakeExecutable() const
{
dt's avatar
dt committed
408 409 410 411 412 413
    if (!isCMakeExecutableValid())
        return QString();
    if (m_userCmake.state == CMakeValidator::VALID)
        return m_userCmake.executable;
    else
        return m_pathCmake.executable;
414 415
}

416
void CMakeSettingsPage::setCMakeExecutable(const QString &executable)
417
{
dt's avatar
dt committed
418
    if (m_userCmake.executable == executable)
419
        return;
dt's avatar
dt committed
420 421
    m_userCmake.executable = executable;
    updateInfo(&m_userCmake);
422 423
}

424
bool CMakeSettingsPage::hasCodeBlocksMsvcGenerator() const
425
{
dt's avatar
dt committed
426 427 428 429 430 431
    if (!isCMakeExecutableValid())
        return false;
    if (m_userCmake.state == CMakeValidator::VALID)
        return m_userCmake.hasCodeBlocksMsvcGenerator;
    else
        return m_pathCmake.hasCodeBlocksMsvcGenerator;
432
}