1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "cmakeopenprojectwizard.h"
35 #include "cmakeprojectmanager.h"
36 #include "cmakeprojectconstants.h"
37 #include "cmakeproject.h"
39 #include <utils/synchronousprocess.h>
40 #include <utils/qtcprocess.h>
42 #include <coreplugin/icore.h>
43 #include <coreplugin/uniqueidmanager.h>
44 #include <coreplugin/actionmanager/actionmanager.h>
45 #include <coreplugin/actionmanager/command.h>
46 #include <coreplugin/actionmanager/actioncontainer.h>
47 #include <projectexplorer/projectexplorerconstants.h>
48 #include <projectexplorer/projectexplorer.h>
49 #include <qtconcurrent/QtConcurrentTools>
50 #include <QtCore/QtConcurrentRun>
51 #include <QtCore/QCoreApplication>
52 #include <QtCore/QSettings>
53 #include <QtGui/QFormLayout>
54 #include <QtGui/QBoxLayout>
55 #include <QtGui/QDesktopServices>
56 #include <QtGui/QApplication>
57 #include <QtGui/QLabel>
58 #include <QtGui/QGroupBox>
59 #include <QtGui/QSpacerItem>
61 using namespace CMakeProjectManager::Internal;
63 CMakeManager::CMakeManager(CMakeSettingsPage *cmakeSettingsPage)
64 : m_settingsPage(cmakeSettingsPage)
66 m_projectContext = Core::Context(CMakeProjectManager::Constants::PROJECTCONTEXT);
67 m_projectLanguage = Core::Context(ProjectExplorer::Constants::LANG_CXX);
69 ProjectExplorer::ProjectExplorerPlugin *projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance();
70 connect(projectExplorer, SIGNAL(aboutToShowContextMenu(ProjectExplorer::Project*, ProjectExplorer::Node*)),
71 this, SLOT(updateContextMenu(ProjectExplorer::Project*, ProjectExplorer::Node*)));
73 Core::ActionManager *am = Core::ICore::instance()->actionManager();
75 Core::ActionContainer *mbuild =
76 am->actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
77 Core::ActionContainer *mproject =
78 am->actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
79 Core::ActionContainer *msubproject =
80 am->actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
82 m_runCMakeAction = new QAction(QIcon(), tr("Run CMake"), this);
83 Core::Command *command = am->registerAction(m_runCMakeAction, Constants::RUNCMAKE, m_projectContext);
84 command->setAttribute(Core::Command::CA_Hide);
85 mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_PROJECT);
86 connect(m_runCMakeAction, SIGNAL(triggered()), this, SLOT(runCMake()));
88 m_runCMakeActionContextMenu = new QAction(QIcon(), tr("Run CMake"), this);
89 command = am->registerAction(m_runCMakeActionContextMenu, Constants::RUNCMAKECONTEXTMENU, m_projectContext);
90 command->setAttribute(Core::Command::CA_Hide);
91 mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
92 msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
93 connect(m_runCMakeActionContextMenu, SIGNAL(triggered()), this, SLOT(runCMakeContextMenu()));
97 void CMakeManager::updateContextMenu(ProjectExplorer::Project *project, ProjectExplorer::Node *node)
100 m_contextProject = project;
103 void CMakeManager::runCMake()
105 runCMake(ProjectExplorer::ProjectExplorerPlugin::instance()->currentProject());
108 void CMakeManager::runCMakeContextMenu()
110 runCMake(m_contextProject);
113 void CMakeManager::runCMake(ProjectExplorer::Project *project)
117 CMakeProject *cmakeProject = qobject_cast<CMakeProject *>(project);
121 if (!cmakeProject->activeTarget())
123 if (!cmakeProject->activeTarget()->activeBuildConfiguration())
125 CMakeBuildConfiguration *bc = cmakeProject->activeTarget()->activeBuildConfiguration();
126 CMakeOpenProjectWizard copw(this,
127 cmakeProject->projectDirectory(),
128 bc->buildDirectory(),
129 CMakeOpenProjectWizard::WantToUpdate,
131 if (copw.exec() == QDialog::Accepted) {
132 cmakeProject->parseCMakeLists();
136 Core::Context CMakeManager::projectContext() const
138 return m_projectContext;
141 Core::Context CMakeManager::projectLanguage() const
143 return m_projectLanguage;
146 ProjectExplorer::Project *CMakeManager::openProject(const QString &fileName)
148 // TODO check whether this project is already opened
149 return new CMakeProject(this, fileName);
152 QString CMakeManager::mimeType() const
154 return Constants::CMAKEMIMETYPE;
157 QString CMakeManager::cmakeExecutable() const
159 return m_settingsPage->cmakeExecutable();
162 bool CMakeManager::isCMakeExecutableValid() const
164 return m_settingsPage->isCMakeExecutableValid();
167 void CMakeManager::setCMakeExecutable(const QString &executable)
169 m_settingsPage->setCMakeExecutable(executable);
172 bool CMakeManager::hasCodeBlocksMsvcGenerator() const
174 return m_settingsPage->hasCodeBlocksMsvcGenerator();
177 // TODO need to refactor this out
178 // we probably want the process instead of this function
179 // cmakeproject then could even run the cmake process in the background, adding the files afterwards
180 // sounds like a plan
181 void CMakeManager::createXmlFile(Utils::QtcProcess *proc, const QString &arguments,
182 const QString &sourceDirectory, const QDir &buildDirectory,
183 const Utils::Environment &env, const QString &generator)
185 // We create a cbp file, only if we didn't find a cbp file in the base directory
186 // Yet that can still override cbp files in subdirectories
187 // And we are creating tons of files in the source directories
188 // All of that is not really nice.
189 // The mid term plan is to move away from the CodeBlocks Generator and use our own
190 // QtCreator generator, which actually can be very similar to the CodeBlock Generator
192 // TODO we need to pass on the same paremeters as the cmakestep
193 QString buildDirectoryPath = buildDirectory.absolutePath();
194 buildDirectory.mkpath(buildDirectoryPath);
195 proc->setWorkingDirectory(buildDirectoryPath);
196 proc->setEnvironment(env);
198 const QString srcdir = buildDirectory.exists(QLatin1String("CMakeCache.txt")) ?
199 QString(QLatin1Char('.')) : sourceDirectory;
201 Utils::QtcProcess::addArg(&args, srcdir);
202 Utils::QtcProcess::addArgs(&args, arguments);
203 Utils::QtcProcess::addArg(&args, generator);
204 proc->setCommand(cmakeExecutable(), args);
208 QString CMakeManager::findCbpFile(const QDir &directory)
211 // TODO the cbp file is named like the project() command in the CMakeList.txt file
212 // so this method below could find the wrong cbp file, if the user changes the project()
214 foreach (const QString &cbpFile , directory.entryList()) {
215 if (cbpFile.endsWith(QLatin1String(".cbp")))
216 return directory.path() + QLatin1Char('/') + cbpFile;
221 // This code is duplicated from qtversionmanager
222 QString CMakeManager::qtVersionForQMake(const QString &qmakePath)
225 qmake.start(qmakePath, QStringList(QLatin1String("--version")));
226 if (!qmake.waitForStarted()) {
227 qWarning("Cannot start '%s': %s", qPrintable(qmakePath), qPrintable(qmake.errorString()));
230 if (!qmake.waitForFinished()) {
231 Utils::SynchronousProcess::stopProcess(qmake);
232 qWarning("Timeout running '%s'.", qPrintable(qmakePath));
235 QString output = qmake.readAllStandardOutput();
236 QRegExp regexp(QLatin1String("(QMake version|Qmake version:)[\\s]*([\\d.]*)"));
237 regexp.indexIn(output);
238 if (regexp.cap(2).startsWith(QLatin1String("2."))) {
239 QRegExp regexp2(QLatin1String("Using Qt version[\\s]*([\\d\\.]*)"));
240 regexp2.indexIn(output);
241 return regexp2.cap(1);
251 CMakeSettingsPage::CMakeSettingsPage()
254 m_userCmake.process = 0;
255 m_pathCmake.process = 0;
256 m_userCmake.hasCodeBlocksMsvcGenerator = false;
257 m_pathCmake.hasCodeBlocksMsvcGenerator = false;
258 Core::ICore *core = Core::ICore::instance();
259 QSettings * settings = core->settings();
260 settings->beginGroup(QLatin1String("CMakeSettings"));
261 m_userCmake.executable = settings->value(QLatin1String("cmakeExecutable")).toString();
262 settings->endGroup();
264 updateInfo(&m_userCmake);
265 m_pathCmake.executable = findCmakeExecutable();
266 updateInfo(&m_pathCmake);
269 void CMakeSettingsPage::startProcess(CMakeValidator *cmakeValidator)
271 cmakeValidator->process = new QProcess();
273 if (cmakeValidator == &m_pathCmake) // ugly
274 connect(cmakeValidator->process, SIGNAL(finished(int)),
275 this, SLOT(userCmakeFinished()));
277 connect(cmakeValidator->process, SIGNAL(finished(int)),
278 this, SLOT(pathCmakeFinished()));
280 cmakeValidator->process->start(cmakeValidator->executable, QStringList(QLatin1String("--help")));
281 cmakeValidator->process->waitForStarted();
284 void CMakeSettingsPage::userCmakeFinished()
286 cmakeFinished(&m_userCmake);
289 void CMakeSettingsPage::pathCmakeFinished()
291 cmakeFinished(&m_pathCmake);
294 void CMakeSettingsPage::cmakeFinished(CMakeValidator *cmakeValidator) const
296 if (cmakeValidator->process) {
297 cmakeValidator->process->waitForFinished();
298 QString response = cmakeValidator->process->readAll();
299 QRegExp versionRegexp(QLatin1String("^cmake version ([\\d\\.]*)"));
300 versionRegexp.indexIn(response);
302 //m_supportsQtCreator = response.contains(QLatin1String("QtCreator"));
303 cmakeValidator->hasCodeBlocksMsvcGenerator = response.contains(QLatin1String("CodeBlocks - NMake Makefiles"));
304 cmakeValidator->version = versionRegexp.cap(1);
305 if (!(versionRegexp.capturedTexts().size() > 3))
306 cmakeValidator->version += QLatin1Char('.') + versionRegexp.cap(3);
308 if (cmakeValidator->version.isEmpty())
309 cmakeValidator->state = CMakeValidator::INVALID;
311 cmakeValidator->state = CMakeValidator::VALID;
313 cmakeValidator->process->deleteLater();
314 cmakeValidator->process = 0;
318 bool CMakeSettingsPage::isCMakeExecutableValid() const
320 if (m_userCmake.state == CMakeValidator::RUNNING) {
321 disconnect(m_userCmake.process, SIGNAL(finished(int)),
322 this, SLOT(cmakeFinished()));
323 m_userCmake.process->waitForFinished();
324 // Parse the output now
325 cmakeFinished(&m_userCmake);
328 if (m_userCmake.state == CMakeValidator::VALID)
330 if (m_pathCmake.state == CMakeValidator::RUNNING) {
331 disconnect(m_userCmake.process, SIGNAL(finished(int)),
332 this, SLOT(cmakeFinished()));
333 m_pathCmake.process->waitForFinished();
334 // Parse the output now
335 cmakeFinished(&m_pathCmake);
337 return m_pathCmake.state == CMakeValidator::VALID;
340 CMakeSettingsPage::~CMakeSettingsPage()
342 if (m_userCmake.process)
343 m_userCmake.process->waitForFinished();
344 delete m_userCmake.process;
345 if (m_pathCmake.process)
346 m_pathCmake.process->waitForFinished();
347 delete m_pathCmake.process;
350 QString CMakeSettingsPage::findCmakeExecutable() const
352 Utils::Environment env = Utils::Environment::systemEnvironment();
353 return env.searchInPath(QLatin1String("cmake"));
356 QString CMakeSettingsPage::id() const
358 return QLatin1String("Z.CMake");
361 QString CMakeSettingsPage::displayName() const
366 QString CMakeSettingsPage::category() const
368 return QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY);
371 QString CMakeSettingsPage::displayCategory() const
373 return QCoreApplication::translate("ProjectExplorer",
374 ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY);
377 QIcon CMakeSettingsPage::categoryIcon() const
379 return QIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));
382 QWidget *CMakeSettingsPage::createPage(QWidget *parent)
384 QWidget *outerWidget = new QWidget(parent);
385 QFormLayout *formLayout = new QFormLayout(outerWidget);
386 m_pathchooser = new Utils::PathChooser;
387 m_pathchooser->setExpectedKind(Utils::PathChooser::ExistingCommand);
388 formLayout->addRow(tr("Executable:"), m_pathchooser);
389 formLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
390 m_pathchooser->setPath(m_userCmake.executable);
394 void CMakeSettingsPage::updateInfo(CMakeValidator *cmakeValidator)
396 QFileInfo fi(cmakeValidator->executable);
397 if (fi.exists() && fi.isExecutable()) {
398 // Run it to find out more
399 cmakeValidator->state = CMakeValidator::RUNNING;
400 startProcess(cmakeValidator);
402 cmakeValidator->state = CMakeValidator::INVALID;
407 void CMakeSettingsPage::saveSettings() const
409 QSettings *settings = Core::ICore::instance()->settings();
410 settings->beginGroup(QLatin1String("CMakeSettings"));
411 settings->setValue(QLatin1String("cmakeExecutable"), m_userCmake.executable);
412 settings->endGroup();
415 void CMakeSettingsPage::apply()
417 if (!m_pathchooser) // page was never shown
419 if (m_userCmake.executable == m_pathchooser->path())
421 m_userCmake.executable = m_pathchooser->path();
422 updateInfo(&m_userCmake);
425 void CMakeSettingsPage::finish()
430 QString CMakeSettingsPage::cmakeExecutable() const
432 if (!isCMakeExecutableValid())
434 if (m_userCmake.state == CMakeValidator::VALID)
435 return m_userCmake.executable;
437 return m_pathCmake.executable;
440 void CMakeSettingsPage::setCMakeExecutable(const QString &executable)
442 if (m_userCmake.executable == executable)
444 m_userCmake.executable = executable;
445 updateInfo(&m_userCmake);
448 bool CMakeSettingsPage::hasCodeBlocksMsvcGenerator() const
450 if (!isCMakeExecutableValid())
452 if (m_userCmake.state == CMakeValidator::VALID)
453 return m_userCmake.hasCodeBlocksMsvcGenerator;
455 return m_pathCmake.hasCodeBlocksMsvcGenerator;