OSDN Git Service

26255702256ff51aeb727a1f71e3ba148e51e687
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cmakeprojectmanager / cmakeopenprojectwizard.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
35 /// TODO
36 /// To check
37 /// a) with an old cmake
38 /// => should not show combobox always use mingw generator
39 /// b) with an new cmake
40 /// always show combo box, defaulting if there's already a existing build
41
42
43 #include "cmakeopenprojectwizard.h"
44 #include "cmakeprojectmanager.h"
45
46 #include <utils/pathchooser.h>
47 #include <projectexplorer/toolchainmanager.h>
48
49 #include <QtGui/QVBoxLayout>
50 #include <QtGui/QFormLayout>
51 #include <QtGui/QLabel>
52 #include <QtGui/QPushButton>
53 #include <QtGui/QPlainTextEdit>
54 #include <QtCore/QDateTime>
55 #include <QtCore/QStringList>
56
57 using namespace CMakeProjectManager;
58 using namespace CMakeProjectManager::Internal;
59
60 ///////
61 //  Page Flow:
62 //   Start (No .user file)
63 //    |
64 //    |---> In Source Build --> Page: Tell the user about that
65 //                               |--> Already existing cbp file (and new enough) --> Page: Ready to load the project
66 //                               |--> Page: Ask for cmd options, run generator
67 //    |---> No in source Build --> Page: Ask the user for the build directory
68 //                                   |--> Already existing cbp file (and new enough) --> Page: Ready to load the project
69 //                                   |--> Page: Ask for cmd options, run generator
70
71 CMakeOpenProjectWizard::CMakeOpenProjectWizard(CMakeManager *cmakeManager, const QString &sourceDirectory, const Utils::Environment &env)
72     : m_cmakeManager(cmakeManager),
73       m_sourceDirectory(sourceDirectory),
74       m_creatingCbpFiles(false),
75       m_environment(env),
76       m_toolChain(0)
77 {
78     int startid;
79     if (hasInSourceBuild()) {
80         startid = InSourcePageId;
81         m_buildDirectory = m_sourceDirectory;
82     } else {
83         startid = ShadowBuildPageId;
84         QDir dir(m_sourceDirectory);
85         dir.cdUp();
86         m_buildDirectory = dir.absolutePath() + "/qtcreator-build";
87     }
88
89     setPage(InSourcePageId, new InSourceBuildPage(this));
90     setPage(ShadowBuildPageId, new ShadowBuildPage(this));
91     setPage(CMakeRunPageId, new CMakeRunPage(this));
92
93     Utils::WizardProgress *wp = wizardProgress();
94     Utils::WizardProgressItem *inSourceItem = wp->item(InSourcePageId);
95     Utils::WizardProgressItem *shadowBuildItem = wp->item(ShadowBuildPageId);
96     Utils::WizardProgressItem *cmakeRunItem = wp->item(CMakeRunPageId);
97     inSourceItem->setNextItems(QList<Utils::WizardProgressItem *>() << cmakeRunItem);
98     shadowBuildItem->setNextItems(QList<Utils::WizardProgressItem *>() << cmakeRunItem);
99
100     setStartId(startid);
101     init();
102 }
103
104 CMakeOpenProjectWizard::CMakeOpenProjectWizard(CMakeManager *cmakeManager, const QString &sourceDirectory,
105                                                const QString &buildDirectory, CMakeOpenProjectWizard::Mode mode,
106                                                const Utils::Environment &env)
107     : m_cmakeManager(cmakeManager),
108       m_sourceDirectory(sourceDirectory),
109       m_creatingCbpFiles(true),
110       m_environment(env),
111       m_toolChain(0)
112 {
113
114     CMakeRunPage::Mode rmode;
115     if (mode == CMakeOpenProjectWizard::NeedToCreate)
116         rmode = CMakeRunPage::Recreate;
117     else if (mode == CMakeOpenProjectWizard::WantToUpdate)
118         rmode = CMakeRunPage::WantToUpdate;
119     else
120         rmode = CMakeRunPage::NeedToUpdate;
121     addPage(new CMakeRunPage(this, rmode, buildDirectory));
122     init();
123 }
124
125 CMakeOpenProjectWizard::CMakeOpenProjectWizard(CMakeManager *cmakeManager, const QString &sourceDirectory,
126                                                const QString &oldBuildDirectory,
127                                                const Utils::Environment &env)
128     : m_cmakeManager(cmakeManager),
129       m_sourceDirectory(sourceDirectory),
130       m_creatingCbpFiles(true),
131       m_environment(env),
132       m_toolChain(0)
133 {
134     m_buildDirectory = oldBuildDirectory;
135     addPage(new ShadowBuildPage(this, true));
136     addPage(new CMakeRunPage(this, CMakeRunPage::ChangeDirectory));
137     init();
138 }
139
140 void CMakeOpenProjectWizard::init()
141 {
142     setOption(QWizard::NoBackButtonOnStartPage);
143     setWindowTitle(tr("CMake Wizard"));
144 }
145
146 CMakeManager *CMakeOpenProjectWizard::cmakeManager() const
147 {
148     return m_cmakeManager;
149 }
150
151 int CMakeOpenProjectWizard::nextId() const
152 {
153     if (m_creatingCbpFiles)
154         return QWizard::nextId();
155     int cid = currentId();
156     if (cid == InSourcePageId) {
157         return CMakeRunPageId;
158     } else if (cid == ShadowBuildPageId) {
159         return CMakeRunPageId;
160     }
161     return -1;
162 }
163
164
165 bool CMakeOpenProjectWizard::hasInSourceBuild() const
166 {
167     QFileInfo fi(m_sourceDirectory + "/CMakeCache.txt");
168     if (fi.exists())
169         return true;
170     return false;
171 }
172
173 bool CMakeOpenProjectWizard::existsUpToDateXmlFile() const
174 {
175     QString cbpFile = CMakeManager::findCbpFile(QDir(buildDirectory()));
176     if (!cbpFile.isEmpty()) {
177         // We already have a cbp file
178         QFileInfo cbpFileInfo(cbpFile);
179         QFileInfo cmakeListsFileInfo(sourceDirectory() + "/CMakeLists.txt");
180
181         if (cbpFileInfo.lastModified() > cmakeListsFileInfo.lastModified())
182             return true;
183     }
184     return false;
185 }
186
187 QString CMakeOpenProjectWizard::buildDirectory() const
188 {
189     return m_buildDirectory;
190 }
191
192 QString CMakeOpenProjectWizard::sourceDirectory() const
193 {
194     return m_sourceDirectory;
195 }
196
197 void CMakeOpenProjectWizard::setBuildDirectory(const QString &directory)
198 {
199     m_buildDirectory = directory;
200 }
201
202 QString CMakeOpenProjectWizard::arguments() const
203 {
204     return m_arguments;
205 }
206
207 void CMakeOpenProjectWizard::setArguments(const QString &args)
208 {
209     m_arguments = args;
210 }
211
212 ProjectExplorer::ToolChain *CMakeOpenProjectWizard::toolChain() const
213 {
214     return m_toolChain;
215 }
216
217 void CMakeOpenProjectWizard::setToolChain(ProjectExplorer::ToolChain *tc)
218 {
219     m_toolChain = tc;
220 }
221
222
223 Utils::Environment CMakeOpenProjectWizard::environment() const
224 {
225     return m_environment;
226 }
227
228
229 InSourceBuildPage::InSourceBuildPage(CMakeOpenProjectWizard *cmakeWizard)
230     : QWizardPage(cmakeWizard), m_cmakeWizard(cmakeWizard)
231 {
232     setLayout(new QVBoxLayout);
233     QLabel *label = new QLabel(this);
234     label->setWordWrap(true);
235     label->setText(tr("Qt Creator has detected an <b>in-source-build in %1</b> "
236                    "which prevents shadow builds. Qt Creator will not allow you to change the build directory. "
237                    "If you want a shadow build, clean your source directory and re-open the project.")
238                    .arg(m_cmakeWizard->buildDirectory()));
239     layout()->addWidget(label);
240     setTitle(tr("Build Location"));
241 }
242
243 ShadowBuildPage::ShadowBuildPage(CMakeOpenProjectWizard *cmakeWizard, bool change)
244     : QWizardPage(cmakeWizard), m_cmakeWizard(cmakeWizard)
245 {
246     QFormLayout *fl = new QFormLayout;
247     this->setLayout(fl);
248
249     QLabel *label = new QLabel(this);
250     label->setWordWrap(true);
251     if (change)
252         label->setText(tr("Please enter the directory in which you want to build your project. "));
253     else
254         label->setText(tr("Please enter the directory in which you want to build your project. "
255                           "Qt Creator recommends to not use the source directory for building. "
256                           "This ensures that the source directory remains clean and enables multiple builds "
257                           "with different settings."));
258     fl->addWidget(label);
259     m_pc = new Utils::PathChooser(this);
260     m_pc->setBaseDirectory(m_cmakeWizard->sourceDirectory());
261     m_pc->setPath(m_cmakeWizard->buildDirectory());
262     connect(m_pc, SIGNAL(changed(QString)), this, SLOT(buildDirectoryChanged()));
263     fl->addRow(tr("Build directory:"), m_pc);
264     setTitle(tr("Build Location"));
265 }
266
267 void ShadowBuildPage::buildDirectoryChanged()
268 {
269     m_cmakeWizard->setBuildDirectory(m_pc->path());
270 }
271
272 CMakeRunPage::CMakeRunPage(CMakeOpenProjectWizard *cmakeWizard, Mode mode, const QString &buildDirectory)
273     : QWizardPage(cmakeWizard),
274       m_cmakeWizard(cmakeWizard),
275       m_complete(false),
276       m_mode(mode),
277       m_buildDirectory(buildDirectory)
278 {
279     initWidgets();
280 }
281
282 void CMakeRunPage::initWidgets()
283 {
284     QFormLayout *fl = new QFormLayout;
285     setLayout(fl);
286     // Description Label
287     m_descriptionLabel = new QLabel(this);
288     m_descriptionLabel->setWordWrap(true);
289
290     fl->addRow(m_descriptionLabel);
291
292     if (m_cmakeWizard->cmakeManager()->isCMakeExecutableValid()) {
293         m_cmakeExecutable = 0;
294     } else {
295         QString text = tr("Please specify the path to the cmake executable. No cmake executable was found in the path.");
296         QString cmakeExecutable = m_cmakeWizard->cmakeManager()->cmakeExecutable();
297         if (!cmakeExecutable.isEmpty()) {
298             QFileInfo fi(cmakeExecutable);
299             if (!fi.exists())
300                 text += tr(" The cmake executable (%1) does not exist.").arg(cmakeExecutable);
301             else if (!fi.isExecutable())
302                 text += tr(" The path %1 is not a executable.").arg(cmakeExecutable);
303             else
304                 text += tr(" The path %1 is not a valid cmake.").arg(cmakeExecutable);
305         }
306
307         fl->addRow(new QLabel(text, this));
308         // Show a field for the user to enter
309         m_cmakeExecutable = new Utils::PathChooser(this);
310         m_cmakeExecutable->setExpectedKind(Utils::PathChooser::ExistingCommand);
311         fl->addRow("cmake Executable", m_cmakeExecutable);
312     }
313
314     // Run CMake Line (with arguments)
315     m_argumentsLineEdit = new QLineEdit(this);
316     connect(m_argumentsLineEdit,SIGNAL(returnPressed()), this, SLOT(runCMake()));
317
318     m_generatorComboBox = new QComboBox(this);
319
320     m_runCMake = new QPushButton(this);
321     m_runCMake->setText(tr("Run CMake"));
322     connect(m_runCMake, SIGNAL(clicked()), this, SLOT(runCMake()));
323
324     QHBoxLayout *hbox = new QHBoxLayout;
325     hbox->addWidget(m_argumentsLineEdit);
326     hbox->addWidget(m_generatorComboBox);
327     hbox->addWidget(m_runCMake);
328
329     fl->addRow(tr("Arguments"), hbox);
330
331     // Bottom output window
332     m_output = new QPlainTextEdit(this);
333     m_output->setReadOnly(true);
334     QSizePolicy pl = m_output->sizePolicy();
335     pl.setVerticalStretch(1);
336     m_output->setSizePolicy(pl);
337     fl->addRow(m_output);
338
339     m_exitCodeLabel = new QLabel(this);
340     m_exitCodeLabel->setVisible(false);
341     fl->addRow(m_exitCodeLabel);
342
343     setTitle(tr("Run CMake"));
344 }
345
346 void CMakeRunPage::initializePage()
347 {
348     if (m_mode == Initial) {
349         m_complete = m_cmakeWizard->existsUpToDateXmlFile();
350         m_buildDirectory = m_cmakeWizard->buildDirectory();
351
352         if (m_cmakeWizard->existsUpToDateXmlFile()) {
353             m_descriptionLabel->setText(
354                     tr("The directory %1 already contains a cbp file, which is recent enough. "
355                        "You can pass special arguments or change the used tool chain here and rerun CMake. "
356                        "Or simply finish the wizard directly.").arg(m_buildDirectory));
357         } else {
358             m_descriptionLabel->setText(
359                     tr("The directory %1 does not contain a cbp file. Qt Creator needs to create this file by running CMake. "
360                        "Some projects require command line arguments to the initial CMake call.").arg(m_buildDirectory));
361         }
362     } else if (m_mode == CMakeRunPage::NeedToUpdate) {
363         m_descriptionLabel->setText(tr("The directory %1 contains an outdated .cbp file. Qt "
364                                        "Creator needs to update this file by running CMake. "
365                                        "If you want to add additional command line arguments, "
366                                        "add them below. Note that CMake remembers command "
367                                        "line arguments from the previous runs.").arg(m_buildDirectory));
368     } else if(m_mode == CMakeRunPage::Recreate) {
369         m_descriptionLabel->setText(tr("The directory %1 specified in a build-configuration, "
370                                        "does not contain a cbp file. Qt Creator needs to "
371                                        "recreate this file, by running CMake. "
372                                        "Some projects require command line arguments to "
373                                        "the initial CMake call. Note that CMake remembers command "
374                                        "line arguments from the previous runs.").arg(m_buildDirectory));
375     } else if(m_mode == CMakeRunPage::ChangeDirectory) {
376         m_buildDirectory = m_cmakeWizard->buildDirectory();
377         m_descriptionLabel->setText(tr("Qt Creator needs to run CMake in the new build directory. "
378                                        "Some projects require command line arguments to the "
379                                        "initial CMake call."));
380     } else if (m_mode == CMakeRunPage::WantToUpdate) {
381         m_descriptionLabel->setText(tr("Refreshing cbp file in %1.").arg(m_buildDirectory));
382     }
383     if (m_cmakeWizard->cmakeManager()->hasCodeBlocksMsvcGenerator()) {
384         // Try to find out generator from CMakeCache file, if it exists
385         QString cachedGenerator;
386
387         QFile fi(m_buildDirectory + "/CMakeCache.txt");
388         if (fi.exists()) {
389             // Cache exists, then read it...
390             if (fi.open(QIODevice::ReadOnly | QIODevice::Text)) {
391                 while (!fi.atEnd()) {
392                     QString line = fi.readLine();
393                     if (line.startsWith("CMAKE_GENERATOR:INTERNAL=")) {
394                         int splitpos = line.indexOf('=');
395                         if (splitpos != -1) {
396                             cachedGenerator = line.mid(splitpos + 1).trimmed();
397                         }
398                         break;
399                     }
400                 }
401             }
402         }
403
404         m_generatorComboBox->setVisible(true);
405         m_generatorComboBox->clear();
406         QList<ProjectExplorer::ToolChain *> tcs =
407                 ProjectExplorer::ToolChainManager::instance()->findToolChains(ProjectExplorer::Abi::hostAbi());
408         foreach (ProjectExplorer::ToolChain *tc, tcs) {
409             ProjectExplorer::Abi targetAbi = tc->targetAbi();
410             QVariant tcVariant = qVariantFromValue(static_cast<void *>(tc));
411             if (targetAbi.os() == ProjectExplorer::Abi::WindowsOS) {
412                 if (targetAbi.osFlavor() == ProjectExplorer::Abi::WindowsMsvc2005Flavor
413                         || targetAbi.osFlavor() == ProjectExplorer::Abi::WindowsMsvc2008Flavor
414                         || targetAbi.osFlavor() == ProjectExplorer::Abi::WindowsMsvc2010Flavor)
415                     m_generatorComboBox->addItem(tr("NMake Generator (%1)").arg(tc->displayName()), tcVariant);
416                 else if (targetAbi.osFlavor() == ProjectExplorer::Abi::WindowsMSysFlavor)
417                     m_generatorComboBox->addItem(tr("MinGW Generator (%1)").arg(tc->displayName()), tcVariant);
418                 else
419                     continue;
420             }
421         }
422     } else {
423         // No new enough cmake, simply hide the combo box
424         m_generatorComboBox->setVisible(false);
425         QList<ProjectExplorer::ToolChain *> tcs =
426                 ProjectExplorer::ToolChainManager::instance()->findToolChains(ProjectExplorer::Abi::hostAbi());
427         if (tcs.isEmpty())
428             return;
429         m_cmakeWizard->setToolChain(tcs.at(0));
430     }
431 }
432
433 void CMakeRunPage::runCMake()
434 {
435     int index = m_generatorComboBox->currentIndex();
436
437     ProjectExplorer::ToolChain *tc = 0;
438     if (index >= 0) {
439         tc = static_cast<ProjectExplorer::ToolChain *>(m_generatorComboBox->itemData(index).value<void *>());
440         if (!tc)
441             return;
442         m_cmakeWizard->setToolChain(tc);
443     } else {
444         tc = m_cmakeWizard->toolChain();
445     }
446     Q_ASSERT(tc);
447
448     m_runCMake->setEnabled(false);
449     m_argumentsLineEdit->setEnabled(false);
450     m_generatorComboBox->setEnabled(false);
451     CMakeManager *cmakeManager = m_cmakeWizard->cmakeManager();
452
453     QString generator = QLatin1String("-GCodeBlocks - Unix Makefiles");
454     if (tc->targetAbi().os() == ProjectExplorer::Abi::WindowsOS) {
455         if (tc->targetAbi().osFlavor() == ProjectExplorer::Abi::WindowsMSysFlavor)
456             generator = QLatin1String("-GCodeBlocks - MinGW Makefiles");
457         else
458             generator = QLatin1String("-GCodeBlocks - NMake Makefiles");
459     }
460
461
462     Utils::Environment env = m_cmakeWizard->environment();
463     tc->addToEnvironment(env);
464
465     if (m_cmakeExecutable) {
466         // We asked the user for the cmake executable
467         m_cmakeWizard->cmakeManager()->setCMakeExecutable(m_cmakeExecutable->path());
468     }
469
470     m_output->clear();
471
472     if (m_cmakeWizard->cmakeManager()->isCMakeExecutableValid()) {
473         m_cmakeProcess = new Utils::QtcProcess();
474         connect(m_cmakeProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(cmakeReadyReadStandardOutput()));
475         connect(m_cmakeProcess, SIGNAL(readyReadStandardError()), this, SLOT(cmakeReadyReadStandardError()));
476         connect(m_cmakeProcess, SIGNAL(finished(int)), this, SLOT(cmakeFinished()));
477         cmakeManager->createXmlFile(m_cmakeProcess, m_argumentsLineEdit->text(), m_cmakeWizard->sourceDirectory(), m_buildDirectory, env, generator);
478     } else {
479         m_runCMake->setEnabled(true);
480         m_argumentsLineEdit->setEnabled(true);
481         m_generatorComboBox->setEnabled(true);
482         m_output->appendPlainText(tr("No valid cmake executable specified."));
483     }
484 }
485
486 static QColor mix_colors(QColor a, QColor b)
487 {
488     return QColor((a.red() + 2 * b.red()) / 3, (a.green() + 2 * b.green()) / 3,
489                   (a.blue() + 2* b.blue()) / 3, (a.alpha() + 2 * b.alpha()) / 3);
490 }
491
492 void CMakeRunPage::cmakeReadyReadStandardOutput()
493 {
494     QTextCursor cursor(m_output->document());
495     QTextCharFormat tf;
496
497     QFont font = m_output->font();
498     tf.setFont(font);
499     tf.setForeground(m_output->palette().color(QPalette::Text));
500
501     cursor.insertText(m_cmakeProcess->readAllStandardOutput(), tf);
502 }
503
504 void CMakeRunPage::cmakeReadyReadStandardError()
505 {
506     QTextCursor cursor(m_output->document());
507     QTextCharFormat tf;
508
509     QFont font = m_output->font();
510     QFont boldFont = font;
511     boldFont.setBold(true);
512     tf.setFont(boldFont);
513     tf.setForeground(mix_colors(m_output->palette().color(QPalette::Text), QColor(Qt::red)));
514
515     cursor.insertText(m_cmakeProcess->readAllStandardError(), tf);
516 }
517
518 void CMakeRunPage::cmakeFinished()
519 {
520     m_runCMake->setEnabled(true);
521     m_argumentsLineEdit->setEnabled(true);
522     m_generatorComboBox->setEnabled(true);
523
524     if (m_cmakeProcess->exitCode() != 0) {
525         m_exitCodeLabel->setVisible(true);
526         m_exitCodeLabel->setText(tr("CMake exited with errors. Please check CMake output."));
527         m_complete = false;
528     } else {
529         m_exitCodeLabel->setVisible(false);
530         m_complete = true;
531     }
532     m_cmakeProcess->deleteLater();
533     m_cmakeProcess = 0;
534     m_cmakeWizard->setArguments(m_argumentsLineEdit->text());
535     //TODO Actually test that running cmake was finished, for setting this bool
536     emit completeChanged();
537 }
538
539 void CMakeRunPage::cleanupPage()
540 {
541     m_output->clear();
542     m_complete = false;
543     m_exitCodeLabel->setVisible(false);
544     emit completeChanged();
545 }
546
547 bool CMakeRunPage::isComplete() const
548 {
549     return m_complete;
550 }