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 **************************************************************************/
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
43 #include "cmakeopenprojectwizard.h"
44 #include "cmakeprojectmanager.h"
46 #include <utils/pathchooser.h>
47 #include <projectexplorer/toolchainmanager.h>
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>
57 using namespace CMakeProjectManager;
58 using namespace CMakeProjectManager::Internal;
62 // Start (No .user file)
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
71 CMakeOpenProjectWizard::CMakeOpenProjectWizard(CMakeManager *cmakeManager, const QString &sourceDirectory, const Utils::Environment &env)
72 : m_cmakeManager(cmakeManager),
73 m_sourceDirectory(sourceDirectory),
74 m_creatingCbpFiles(false),
79 if (hasInSourceBuild()) {
80 startid = InSourcePageId;
81 m_buildDirectory = m_sourceDirectory;
83 startid = ShadowBuildPageId;
84 QDir dir(m_sourceDirectory);
86 m_buildDirectory = dir.absolutePath() + "/qtcreator-build";
89 setPage(InSourcePageId, new InSourceBuildPage(this));
90 setPage(ShadowBuildPageId, new ShadowBuildPage(this));
91 setPage(CMakeRunPageId, new CMakeRunPage(this));
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);
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),
114 CMakeRunPage::Mode rmode;
115 if (mode == CMakeOpenProjectWizard::NeedToCreate)
116 rmode = CMakeRunPage::Recreate;
117 else if (mode == CMakeOpenProjectWizard::WantToUpdate)
118 rmode = CMakeRunPage::WantToUpdate;
120 rmode = CMakeRunPage::NeedToUpdate;
121 addPage(new CMakeRunPage(this, rmode, buildDirectory));
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),
134 m_buildDirectory = oldBuildDirectory;
135 addPage(new ShadowBuildPage(this, true));
136 addPage(new CMakeRunPage(this, CMakeRunPage::ChangeDirectory));
140 void CMakeOpenProjectWizard::init()
142 setOption(QWizard::NoBackButtonOnStartPage);
143 setWindowTitle(tr("CMake Wizard"));
146 CMakeManager *CMakeOpenProjectWizard::cmakeManager() const
148 return m_cmakeManager;
151 int CMakeOpenProjectWizard::nextId() const
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;
165 bool CMakeOpenProjectWizard::hasInSourceBuild() const
167 QFileInfo fi(m_sourceDirectory + "/CMakeCache.txt");
173 bool CMakeOpenProjectWizard::existsUpToDateXmlFile() const
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");
181 if (cbpFileInfo.lastModified() > cmakeListsFileInfo.lastModified())
187 QString CMakeOpenProjectWizard::buildDirectory() const
189 return m_buildDirectory;
192 QString CMakeOpenProjectWizard::sourceDirectory() const
194 return m_sourceDirectory;
197 void CMakeOpenProjectWizard::setBuildDirectory(const QString &directory)
199 m_buildDirectory = directory;
202 QString CMakeOpenProjectWizard::arguments() const
207 void CMakeOpenProjectWizard::setArguments(const QString &args)
212 ProjectExplorer::ToolChain *CMakeOpenProjectWizard::toolChain() const
217 void CMakeOpenProjectWizard::setToolChain(ProjectExplorer::ToolChain *tc)
223 Utils::Environment CMakeOpenProjectWizard::environment() const
225 return m_environment;
229 InSourceBuildPage::InSourceBuildPage(CMakeOpenProjectWizard *cmakeWizard)
230 : QWizardPage(cmakeWizard), m_cmakeWizard(cmakeWizard)
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"));
243 ShadowBuildPage::ShadowBuildPage(CMakeOpenProjectWizard *cmakeWizard, bool change)
244 : QWizardPage(cmakeWizard), m_cmakeWizard(cmakeWizard)
246 QFormLayout *fl = new QFormLayout;
249 QLabel *label = new QLabel(this);
250 label->setWordWrap(true);
252 label->setText(tr("Please enter the directory in which you want to build your project. "));
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"));
267 void ShadowBuildPage::buildDirectoryChanged()
269 m_cmakeWizard->setBuildDirectory(m_pc->path());
272 CMakeRunPage::CMakeRunPage(CMakeOpenProjectWizard *cmakeWizard, Mode mode, const QString &buildDirectory)
273 : QWizardPage(cmakeWizard),
274 m_cmakeWizard(cmakeWizard),
277 m_buildDirectory(buildDirectory)
282 void CMakeRunPage::initWidgets()
284 QFormLayout *fl = new QFormLayout;
287 m_descriptionLabel = new QLabel(this);
288 m_descriptionLabel->setWordWrap(true);
290 fl->addRow(m_descriptionLabel);
292 if (m_cmakeWizard->cmakeManager()->isCMakeExecutableValid()) {
293 m_cmakeExecutable = 0;
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);
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);
304 text += tr(" The path %1 is not a valid cmake.").arg(cmakeExecutable);
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);
314 // Run CMake Line (with arguments)
315 m_argumentsLineEdit = new QLineEdit(this);
316 connect(m_argumentsLineEdit,SIGNAL(returnPressed()), this, SLOT(runCMake()));
318 m_generatorComboBox = new QComboBox(this);
320 m_runCMake = new QPushButton(this);
321 m_runCMake->setText(tr("Run CMake"));
322 connect(m_runCMake, SIGNAL(clicked()), this, SLOT(runCMake()));
324 QHBoxLayout *hbox = new QHBoxLayout;
325 hbox->addWidget(m_argumentsLineEdit);
326 hbox->addWidget(m_generatorComboBox);
327 hbox->addWidget(m_runCMake);
329 fl->addRow(tr("Arguments"), hbox);
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);
339 m_exitCodeLabel = new QLabel(this);
340 m_exitCodeLabel->setVisible(false);
341 fl->addRow(m_exitCodeLabel);
343 setTitle(tr("Run CMake"));
346 void CMakeRunPage::initializePage()
348 if (m_mode == Initial) {
349 m_complete = m_cmakeWizard->existsUpToDateXmlFile();
350 m_buildDirectory = m_cmakeWizard->buildDirectory();
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));
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));
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));
383 if (m_cmakeWizard->cmakeManager()->hasCodeBlocksMsvcGenerator()) {
384 // Try to find out generator from CMakeCache file, if it exists
385 QString cachedGenerator;
387 QFile fi(m_buildDirectory + "/CMakeCache.txt");
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();
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);
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());
429 m_cmakeWizard->setToolChain(tcs.at(0));
433 void CMakeRunPage::runCMake()
435 int index = m_generatorComboBox->currentIndex();
437 ProjectExplorer::ToolChain *tc = 0;
439 tc = static_cast<ProjectExplorer::ToolChain *>(m_generatorComboBox->itemData(index).value<void *>());
442 m_cmakeWizard->setToolChain(tc);
444 tc = m_cmakeWizard->toolChain();
448 m_runCMake->setEnabled(false);
449 m_argumentsLineEdit->setEnabled(false);
450 m_generatorComboBox->setEnabled(false);
451 CMakeManager *cmakeManager = m_cmakeWizard->cmakeManager();
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");
458 generator = QLatin1String("-GCodeBlocks - NMake Makefiles");
462 Utils::Environment env = m_cmakeWizard->environment();
463 tc->addToEnvironment(env);
465 if (m_cmakeExecutable) {
466 // We asked the user for the cmake executable
467 m_cmakeWizard->cmakeManager()->setCMakeExecutable(m_cmakeExecutable->path());
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);
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."));
486 static QColor mix_colors(QColor a, QColor b)
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);
492 void CMakeRunPage::cmakeReadyReadStandardOutput()
494 QTextCursor cursor(m_output->document());
497 QFont font = m_output->font();
499 tf.setForeground(m_output->palette().color(QPalette::Text));
501 cursor.insertText(m_cmakeProcess->readAllStandardOutput(), tf);
504 void CMakeRunPage::cmakeReadyReadStandardError()
506 QTextCursor cursor(m_output->document());
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)));
515 cursor.insertText(m_cmakeProcess->readAllStandardError(), tf);
518 void CMakeRunPage::cmakeFinished()
520 m_runCMake->setEnabled(true);
521 m_argumentsLineEdit->setEnabled(true);
522 m_generatorComboBox->setEnabled(true);
524 if (m_cmakeProcess->exitCode() != 0) {
525 m_exitCodeLabel->setVisible(true);
526 m_exitCodeLabel->setText(tr("CMake exited with errors. Please check CMake output."));
529 m_exitCodeLabel->setVisible(false);
532 m_cmakeProcess->deleteLater();
534 m_cmakeWizard->setArguments(m_argumentsLineEdit->text());
535 //TODO Actually test that running cmake was finished, for setting this bool
536 emit completeChanged();
539 void CMakeRunPage::cleanupPage()
543 m_exitCodeLabel->setVisible(false);
544 emit completeChanged();
547 bool CMakeRunPage::isComplete() const