OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cmakeprojectmanager / cmakeprojectmanager.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "cmakeprojectmanager.h"
35 #include "cmakeprojectconstants.h"
36 #include "cmakeproject.h"
37
38 #include <utils/synchronousprocess.h>
39 #include <utils/qtcprocess.h>
40
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>
55
56 using namespace CMakeProjectManager::Internal;
57
58 CMakeManager::CMakeManager(CMakeSettingsPage *cmakeSettingsPage)
59     : m_settingsPage(cmakeSettingsPage)
60 {
61     m_projectContext = Core::Context(CMakeProjectManager::Constants::PROJECTCONTEXT);
62     m_projectLanguage = Core::Context(ProjectExplorer::Constants::LANG_CXX);
63 }
64
65 Core::Context CMakeManager::projectContext() const
66 {
67     return m_projectContext;
68 }
69
70 Core::Context CMakeManager::projectLanguage() const
71 {
72     return m_projectLanguage;
73 }
74
75 ProjectExplorer::Project *CMakeManager::openProject(const QString &fileName)
76 {
77     // TODO check whether this project is already opened
78     return new CMakeProject(this, fileName);
79 }
80
81 QString CMakeManager::mimeType() const
82 {
83     return Constants::CMAKEMIMETYPE;
84 }
85
86 QString CMakeManager::cmakeExecutable() const
87 {
88     return m_settingsPage->cmakeExecutable();
89 }
90
91 bool CMakeManager::isCMakeExecutableValid() const
92 {
93     return m_settingsPage->isCMakeExecutableValid();
94 }
95
96 void CMakeManager::setCMakeExecutable(const QString &executable)
97 {
98     m_settingsPage->setCMakeExecutable(executable);
99 }
100
101 bool CMakeManager::hasCodeBlocksMsvcGenerator() const
102 {
103     return m_settingsPage->hasCodeBlocksMsvcGenerator();
104 }
105
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)
113 {
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
120
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);
126
127     const QString srcdir = buildDirectory.exists(QLatin1String("CMakeCache.txt")) ?
128                 QString(QLatin1Char('.')) : sourceDirectory;
129     QString args;
130     Utils::QtcProcess::addArg(&args, srcdir);
131     Utils::QtcProcess::addArgs(&args, arguments);
132     Utils::QtcProcess::addArg(&args, generator);
133     proc->setCommand(cmakeExecutable(), args);
134     proc->start();
135 }
136
137 QString CMakeManager::findCbpFile(const QDir &directory)
138 {
139     // Find the cbp file
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()
142     //   2name
143     foreach (const QString &cbpFile , directory.entryList()) {
144         if (cbpFile.endsWith(QLatin1String(".cbp")))
145             return directory.path() + QLatin1Char('/') + cbpFile;
146     }
147     return QString();
148 }
149
150 // This code is duplicated from qtversionmanager
151 QString CMakeManager::qtVersionForQMake(const QString &qmakePath)
152 {
153     QProcess qmake;
154     qmake.start(qmakePath, QStringList(QLatin1String("--version")));
155     if (!qmake.waitForStarted()) {
156         qWarning("Cannot start '%s': %s", qPrintable(qmakePath), qPrintable(qmake.errorString()));
157         return QString();
158     }
159     if (!qmake.waitForFinished())      {
160         Utils::SynchronousProcess::stopProcess(qmake);
161         qWarning("Timeout running '%s'.", qPrintable(qmakePath));
162         return QString();
163     }
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);
171     }
172     return QString();
173 }
174
175 /////
176 // CMakeSettingsPage
177 ////
178
179
180 CMakeSettingsPage::CMakeSettingsPage()
181     :  m_pathchooser(0), m_process(0)
182 {
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
193         m_state = RUNNING;
194         startProcess();
195     } else {
196         m_state = INVALID;
197     }
198
199     settings->endGroup();
200 }
201
202 void CMakeSettingsPage::startProcess()
203 {
204     m_process = new QProcess();
205
206     connect(m_process, SIGNAL(finished(int)),
207             this, SLOT(cmakeFinished()));
208
209     m_process->start(m_cmakeExecutable, QStringList(QLatin1String("--help")));
210     m_process->waitForStarted();
211 }
212
213 void CMakeSettingsPage::cmakeFinished()
214 {
215     if (m_process) {
216         QString response = m_process->readAll();
217         QRegExp versionRegexp(QLatin1String("^cmake version ([\\d\\.]*)"));
218         versionRegexp.indexIn(response);
219
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);
225
226         if (m_version.isEmpty())
227             m_state = INVALID;
228         else
229             m_state = VALID;
230
231         m_process->deleteLater();
232         m_process = 0;
233     }
234 }
235
236 bool CMakeSettingsPage::isCMakeExecutableValid()
237 {
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
243         cmakeFinished();
244     }
245     return m_state == VALID;
246 }
247
248 CMakeSettingsPage::~CMakeSettingsPage()
249 {
250     if (m_process)
251         m_process->waitForFinished();
252     delete m_process;
253 }
254
255 QString CMakeSettingsPage::findCmakeExecutable() const
256 {
257     Utils::Environment env = Utils::Environment::systemEnvironment();
258     return env.searchInPath(QLatin1String("cmake"));
259 }
260
261 QString CMakeSettingsPage::id() const
262 {
263     return QLatin1String("Z.CMake");
264 }
265
266 QString CMakeSettingsPage::displayName() const
267 {
268     return tr("CMake");
269 }
270
271 QString CMakeSettingsPage::category() const
272 {
273     return QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY);
274 }
275
276 QString CMakeSettingsPage::displayCategory() const
277 {
278     return QCoreApplication::translate("ProjectExplorer",
279                                        ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY);
280 }
281
282 QIcon CMakeSettingsPage::categoryIcon() const
283 {
284     return QIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));
285 }
286
287 QWidget *CMakeSettingsPage::createPage(QWidget *parent)
288 {
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());
296     return outerWidget;
297 }
298
299 void CMakeSettingsPage::updateInfo()
300 {
301     QFileInfo fi(m_cmakeExecutable);
302     if (fi.exists() && fi.isExecutable()) {
303         // Run it to find out more
304         m_state = RUNNING;
305         startProcess();
306     } else {
307         m_state = INVALID;
308     }
309     saveSettings();
310 }
311
312 void CMakeSettingsPage::saveSettings() const
313 {
314     QSettings *settings = Core::ICore::instance()->settings();
315     settings->beginGroup(QLatin1String("CMakeSettings"));
316     settings->setValue(QLatin1String("cmakeExecutable"), m_cmakeExecutable);
317     settings->endGroup();
318 }
319
320 void CMakeSettingsPage::apply()
321 {
322     if (!m_pathchooser) // page was never shown
323         return;
324     if (m_cmakeExecutable == m_pathchooser->path())
325         return;
326     m_cmakeExecutable = m_pathchooser->path();
327     updateInfo();
328 }
329
330 void CMakeSettingsPage::finish()
331 {
332
333 }
334
335 QString CMakeSettingsPage::cmakeExecutable() const
336 {
337     return m_cmakeExecutable;
338 }
339
340 void CMakeSettingsPage::setCMakeExecutable(const QString &executable)
341 {
342     if (m_cmakeExecutable == executable)
343         return;
344     m_cmakeExecutable = executable;
345     updateInfo();
346 }
347
348 bool CMakeSettingsPage::hasCodeBlocksMsvcGenerator() const
349 {
350     return m_hasCodeBlocksMsvcGenerator;
351 }