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 "cmakeprojectmanager.h"
35 #include "cmakeprojectconstants.h"
36 #include "cmakeproject.h"
38 #include <utils/synchronousprocess.h>
39 #include <utils/qtcprocess.h>
41 #include <coreplugin/icore.h>
42 #include <coreplugin/uniqueidmanager.h>
43 #include <projectexplorer/projectexplorerconstants.h>
44 #include <qtconcurrent/QtConcurrentTools>
45 #include <QtCore/QtConcurrentRun>
46 #include <QtCore/QCoreApplication>
47 #include <QtCore/QSettings>
48 #include <QtGui/QFormLayout>
49 #include <QtGui/QBoxLayout>
50 #include <QtGui/QDesktopServices>
51 #include <QtGui/QApplication>
52 #include <QtGui/QLabel>
53 #include <QtGui/QGroupBox>
54 #include <QtGui/QSpacerItem>
56 using namespace CMakeProjectManager::Internal;
58 CMakeManager::CMakeManager(CMakeSettingsPage *cmakeSettingsPage)
59 : m_settingsPage(cmakeSettingsPage)
61 m_projectContext = Core::Context(CMakeProjectManager::Constants::PROJECTCONTEXT);
62 m_projectLanguage = Core::Context(ProjectExplorer::Constants::LANG_CXX);
65 Core::Context CMakeManager::projectContext() const
67 return m_projectContext;
70 Core::Context CMakeManager::projectLanguage() const
72 return m_projectLanguage;
75 ProjectExplorer::Project *CMakeManager::openProject(const QString &fileName)
77 // TODO check whether this project is already opened
78 return new CMakeProject(this, fileName);
81 QString CMakeManager::mimeType() const
83 return Constants::CMAKEMIMETYPE;
86 QString CMakeManager::cmakeExecutable() const
88 return m_settingsPage->cmakeExecutable();
91 bool CMakeManager::isCMakeExecutableValid() const
93 return m_settingsPage->isCMakeExecutableValid();
96 void CMakeManager::setCMakeExecutable(const QString &executable)
98 m_settingsPage->setCMakeExecutable(executable);
101 bool CMakeManager::hasCodeBlocksMsvcGenerator() const
103 return m_settingsPage->hasCodeBlocksMsvcGenerator();
106 // TODO need to refactor this out
107 // we probably want the process instead of this function
108 // cmakeproject then could even run the cmake process in the background, adding the files afterwards
109 // sounds like a plan
110 void CMakeManager::createXmlFile(Utils::QtcProcess *proc, const QString &arguments,
111 const QString &sourceDirectory, const QDir &buildDirectory,
112 const Utils::Environment &env, const QString &generator)
114 // We create a cbp file, only if we didn't find a cbp file in the base directory
115 // Yet that can still override cbp files in subdirectories
116 // And we are creating tons of files in the source directories
117 // All of that is not really nice.
118 // The mid term plan is to move away from the CodeBlocks Generator and use our own
119 // QtCreator generator, which actually can be very similar to the CodeBlock Generator
121 // TODO we need to pass on the same paremeters as the cmakestep
122 QString buildDirectoryPath = buildDirectory.absolutePath();
123 buildDirectory.mkpath(buildDirectoryPath);
124 proc->setWorkingDirectory(buildDirectoryPath);
125 proc->setEnvironment(env);
127 const QString srcdir = buildDirectory.exists(QLatin1String("CMakeCache.txt")) ?
128 QString(QLatin1Char('.')) : sourceDirectory;
130 Utils::QtcProcess::addArg(&args, srcdir);
131 Utils::QtcProcess::addArgs(&args, arguments);
132 Utils::QtcProcess::addArg(&args, generator);
133 proc->setCommand(cmakeExecutable(), args);
137 QString CMakeManager::findCbpFile(const QDir &directory)
140 // TODO the cbp file is named like the project() command in the CMakeList.txt file
141 // so this method below could find the wrong cbp file, if the user changes the project()
143 foreach (const QString &cbpFile , directory.entryList()) {
144 if (cbpFile.endsWith(QLatin1String(".cbp")))
145 return directory.path() + QLatin1Char('/') + cbpFile;
150 // This code is duplicated from qtversionmanager
151 QString CMakeManager::qtVersionForQMake(const QString &qmakePath)
154 qmake.start(qmakePath, QStringList(QLatin1String("--version")));
155 if (!qmake.waitForStarted()) {
156 qWarning("Cannot start '%s': %s", qPrintable(qmakePath), qPrintable(qmake.errorString()));
159 if (!qmake.waitForFinished()) {
160 Utils::SynchronousProcess::stopProcess(qmake);
161 qWarning("Timeout running '%s'.", qPrintable(qmakePath));
164 QString output = qmake.readAllStandardOutput();
165 QRegExp regexp(QLatin1String("(QMake version|Qmake version:)[\\s]*([\\d.]*)"));
166 regexp.indexIn(output);
167 if (regexp.cap(2).startsWith(QLatin1String("2."))) {
168 QRegExp regexp2(QLatin1String("Using Qt version[\\s]*([\\d\\.]*)"));
169 regexp2.indexIn(output);
170 return regexp2.cap(1);
180 CMakeSettingsPage::CMakeSettingsPage()
181 : m_pathchooser(0), m_process(0)
183 Core::ICore *core = Core::ICore::instance();
184 QSettings * settings = core->settings();
185 settings->beginGroup(QLatin1String("CMakeSettings"));
186 m_cmakeExecutable = settings->value(QLatin1String("cmakeExecutable")).toString();
187 QFileInfo fi(m_cmakeExecutable);
188 if (!fi.exists() || !fi.isExecutable())
189 m_cmakeExecutable = findCmakeExecutable();
190 fi.setFile(m_cmakeExecutable);
191 if (fi.exists() && fi.isExecutable()) {
192 // Run it to find out more
199 settings->endGroup();
202 void CMakeSettingsPage::startProcess()
204 m_process = new QProcess();
206 connect(m_process, SIGNAL(finished(int)),
207 this, SLOT(cmakeFinished()));
209 m_process->start(m_cmakeExecutable, QStringList(QLatin1String("--help")));
210 m_process->waitForStarted();
213 void CMakeSettingsPage::cmakeFinished()
216 QString response = m_process->readAll();
217 QRegExp versionRegexp(QLatin1String("^cmake version ([\\d\\.]*)"));
218 versionRegexp.indexIn(response);
220 //m_supportsQtCreator = response.contains(QLatin1String("QtCreator"));
221 m_hasCodeBlocksMsvcGenerator = response.contains(QLatin1String("CodeBlocks - NMake Makefiles"));
222 m_version = versionRegexp.cap(1);
223 if (!(versionRegexp.capturedTexts().size() > 3))
224 m_version += QLatin1Char('.') + versionRegexp.cap(3);
226 if (m_version.isEmpty())
231 m_process->deleteLater();
236 bool CMakeSettingsPage::isCMakeExecutableValid()
238 if (m_state == RUNNING) {
239 disconnect(m_process, SIGNAL(finished(int)),
240 this, SLOT(cmakeFinished()));
241 m_process->waitForFinished();
242 // Parse the output now
245 return m_state == VALID;
248 CMakeSettingsPage::~CMakeSettingsPage()
251 m_process->waitForFinished();
255 QString CMakeSettingsPage::findCmakeExecutable() const
257 Utils::Environment env = Utils::Environment::systemEnvironment();
258 return env.searchInPath(QLatin1String("cmake"));
261 QString CMakeSettingsPage::id() const
263 return QLatin1String("Z.CMake");
266 QString CMakeSettingsPage::displayName() const
271 QString CMakeSettingsPage::category() const
273 return QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY);
276 QString CMakeSettingsPage::displayCategory() const
278 return QCoreApplication::translate("ProjectExplorer",
279 ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY);
282 QIcon CMakeSettingsPage::categoryIcon() const
284 return QIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));
287 QWidget *CMakeSettingsPage::createPage(QWidget *parent)
289 QWidget *outerWidget = new QWidget(parent);
290 QFormLayout *formLayout = new QFormLayout(outerWidget);
291 m_pathchooser = new Utils::PathChooser;
292 m_pathchooser->setExpectedKind(Utils::PathChooser::ExistingCommand);
293 formLayout->addRow(tr("Executable:"), m_pathchooser);
294 formLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
295 m_pathchooser->setPath(cmakeExecutable());
299 void CMakeSettingsPage::updateInfo()
301 QFileInfo fi(m_cmakeExecutable);
302 if (fi.exists() && fi.isExecutable()) {
303 // Run it to find out more
312 void CMakeSettingsPage::saveSettings() const
314 QSettings *settings = Core::ICore::instance()->settings();
315 settings->beginGroup(QLatin1String("CMakeSettings"));
316 settings->setValue(QLatin1String("cmakeExecutable"), m_cmakeExecutable);
317 settings->endGroup();
320 void CMakeSettingsPage::apply()
322 if (!m_pathchooser) // page was never shown
324 if (m_cmakeExecutable == m_pathchooser->path())
326 m_cmakeExecutable = m_pathchooser->path();
330 void CMakeSettingsPage::finish()
335 QString CMakeSettingsPage::cmakeExecutable() const
337 return m_cmakeExecutable;
340 void CMakeSettingsPage::setCMakeExecutable(const QString &executable)
342 if (m_cmakeExecutable == executable)
344 m_cmakeExecutable = executable;
348 bool CMakeSettingsPage::hasCodeBlocksMsvcGenerator() const
350 return m_hasCodeBlocksMsvcGenerator;