Files
qt-creator/src/plugins/cmakeprojectmanager/cmakeopenprojectwizard.cpp

502 lines
18 KiB
C++
Raw Normal View History

2009-03-20 14:57:12 +01:00
/**************************************************************************
**
** This file is part of Qt Creator
**
2010-03-05 11:25:49 +01:00
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
2009-03-20 14:57:12 +01:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2009-03-20 14:57:12 +01:00
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
2009-08-14 09:30:56 +02:00
** contact the sales department at http://qt.nokia.com/contact.
2009-03-20 14:57:12 +01:00
**
**************************************************************************/
/// TODO
/// To check
/// a) with an old cmake
/// => should not show combobox always use mingw generator
/// b) with an new cmake
/// always show combo box, defaulting if there's already a existing build
2009-03-10 09:36:18 +01:00
#include "cmakeopenprojectwizard.h"
#include "cmakeprojectmanager.h"
#include <utils/pathchooser.h>
#include <projectexplorer/toolchain.h>
2009-03-10 09:36:18 +01:00
#include <QtGui/QVBoxLayout>
#include <QtGui/QFormLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QPlainTextEdit>
#include <QtCore/QDateTime>
#include <QtCore/QStringList>
2009-03-10 09:36:18 +01:00
using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
///////
// Page Flow:
// Start (No .user file)
// |
// |---> In Source Build --> Page: Tell the user about that
// |--> Already existing cbp file (and new enough) --> Page: Ready to load the project
// |--> Page: Ask for cmd options, run generator
// |---> No in source Build --> Page: Ask the user for the build directory
// |--> Already existing cbp file (and new enough) --> Page: Ready to load the project
// |--> Page: Ask for cmd options, run generator
CMakeOpenProjectWizard::CMakeOpenProjectWizard(CMakeManager *cmakeManager, const QString &sourceDirectory, const Utils::Environment &env)
2009-03-10 09:36:18 +01:00
: m_cmakeManager(cmakeManager),
m_sourceDirectory(sourceDirectory),
m_creatingCbpFiles(false),
m_environment(env)
2009-03-10 09:36:18 +01:00
{
int startid;
if (hasInSourceBuild()) {
startid = InSourcePageId;
m_buildDirectory = m_sourceDirectory;
} else {
startid = ShadowBuildPageId;
QDir dir(m_sourceDirectory);
dir.cdUp();
m_buildDirectory = dir.absolutePath() + "/qtcreator-build";
2009-03-10 09:36:18 +01:00
}
setPage(InSourcePageId, new InSourceBuildPage(this));
setPage(ShadowBuildPageId, new ShadowBuildPage(this));
setPage(CMakeRunPageId, new CMakeRunPage(this));
2010-03-31 14:48:08 +02:00
Utils::WizardProgress *wp = wizardProgress();
Utils::WizardProgressItem *inSourceItem = wp->item(InSourcePageId);
Utils::WizardProgressItem *shadowBuildItem = wp->item(ShadowBuildPageId);
Utils::WizardProgressItem *cmakeRunItem = wp->item(CMakeRunPageId);
inSourceItem->setNextItems(QList<Utils::WizardProgressItem *>() << cmakeRunItem);
shadowBuildItem->setNextItems(QList<Utils::WizardProgressItem *>() << cmakeRunItem);
2009-03-10 09:36:18 +01:00
setStartId(startid);
2009-06-10 16:03:16 +02:00
init();
2009-03-10 09:36:18 +01:00
}
CMakeOpenProjectWizard::CMakeOpenProjectWizard(CMakeManager *cmakeManager, const QString &sourceDirectory,
const QString &buildDirectory, CMakeOpenProjectWizard::Mode mode,
const Utils::Environment &env)
: m_cmakeManager(cmakeManager),
m_sourceDirectory(sourceDirectory),
m_creatingCbpFiles(true),
m_environment(env)
{
CMakeRunPage::Mode rmode;
if (mode == CMakeOpenProjectWizard::NeedToCreate)
rmode = CMakeRunPage::Recreate;
else if (mode == CMakeOpenProjectWizard::WantToUpdate)
rmode = CMakeRunPage::WantToUpdate;
else
rmode = CMakeRunPage::NeedToUpdate;
addPage(new CMakeRunPage(this, rmode, buildDirectory));
2009-06-10 16:03:16 +02:00
init();
}
CMakeOpenProjectWizard::CMakeOpenProjectWizard(CMakeManager *cmakeManager, const QString &sourceDirectory,
const QString &oldBuildDirectory,
const Utils::Environment &env)
: m_cmakeManager(cmakeManager),
m_sourceDirectory(sourceDirectory),
m_creatingCbpFiles(true),
m_environment(env)
{
m_buildDirectory = oldBuildDirectory;
addPage(new ShadowBuildPage(this, true));
addPage(new CMakeRunPage(this, CMakeRunPage::ChangeDirectory));
2009-06-10 16:03:16 +02:00
init();
}
void CMakeOpenProjectWizard::init()
{
setOption(QWizard::NoBackButtonOnStartPage);
2009-06-10 16:03:16 +02:00
setWindowTitle(tr("CMake Wizard"));
}
2009-03-10 09:36:18 +01:00
CMakeManager *CMakeOpenProjectWizard::cmakeManager() const
{
return m_cmakeManager;
}
int CMakeOpenProjectWizard::nextId() const
{
if (m_creatingCbpFiles)
return QWizard::nextId();
2009-03-10 09:36:18 +01:00
int cid = currentId();
if (cid == InSourcePageId) {
return CMakeRunPageId;
2009-03-10 09:36:18 +01:00
} else if (cid == ShadowBuildPageId) {
return CMakeRunPageId;
2009-03-10 09:36:18 +01:00
}
return -1;
}
bool CMakeOpenProjectWizard::hasInSourceBuild() const
{
QFileInfo fi(m_sourceDirectory + "/CMakeCache.txt");
if (fi.exists())
return true;
return false;
}
bool CMakeOpenProjectWizard::existsUpToDateXmlFile() const
{
QString cbpFile = CMakeManager::findCbpFile(QDir(buildDirectory()));
if (!cbpFile.isEmpty()) {
// We already have a cbp file
QFileInfo cbpFileInfo(cbpFile);
QFileInfo cmakeListsFileInfo(sourceDirectory() + "/CMakeLists.txt");
if (cbpFileInfo.lastModified() > cmakeListsFileInfo.lastModified())
return true;
}
return false;
}
QString CMakeOpenProjectWizard::buildDirectory() const
{
return m_buildDirectory;
}
QString CMakeOpenProjectWizard::sourceDirectory() const
{
return m_sourceDirectory;
}
void CMakeOpenProjectWizard::setBuildDirectory(const QString &directory)
{
m_buildDirectory = directory;
}
QString CMakeOpenProjectWizard::msvcVersion() const
{
return m_msvcVersion;
}
void CMakeOpenProjectWizard::setMsvcVersion(const QString &version)
{
m_msvcVersion = version;
}
2009-03-10 09:36:18 +01:00
QStringList CMakeOpenProjectWizard::arguments() const
{
return m_arguments;
}
void CMakeOpenProjectWizard::setArguments(const QStringList &args)
{
m_arguments = args;
}
Utils::Environment CMakeOpenProjectWizard::environment() const
{
return m_environment;
}
2009-03-10 09:36:18 +01:00
InSourceBuildPage::InSourceBuildPage(CMakeOpenProjectWizard *cmakeWizard)
: QWizardPage(cmakeWizard), m_cmakeWizard(cmakeWizard)
{
setLayout(new QVBoxLayout);
QLabel *label = new QLabel(this);
label->setWordWrap(true);
label->setText(tr("Qt Creator has detected an <b>in-source-build in %1</b> "
2009-05-07 15:34:52 +02:00
"which prevents shadow builds. Qt Creator will not allow you to change the build directory. "
"If you want a shadow build, clean your source directory and re-open the project.")
.arg(m_cmakeWizard->buildDirectory()));
2009-03-10 09:36:18 +01:00
layout()->addWidget(label);
2010-03-31 14:48:08 +02:00
setTitle(tr("Build Location"));
2009-03-10 09:36:18 +01:00
}
ShadowBuildPage::ShadowBuildPage(CMakeOpenProjectWizard *cmakeWizard, bool change)
2009-03-10 09:36:18 +01:00
: QWizardPage(cmakeWizard), m_cmakeWizard(cmakeWizard)
{
QFormLayout *fl = new QFormLayout;
this->setLayout(fl);
QLabel *label = new QLabel(this);
label->setWordWrap(true);
if (change)
label->setText(tr("Please enter the directory in which you want to build your project. "));
else
label->setText(tr("Please enter the directory in which you want to build your project. "
"Qt Creator recommends to not use the source directory for building. "
"This ensures that the source directory remains clean and enables multiple builds "
"with different settings."));
2009-03-10 09:36:18 +01:00
fl->addWidget(label);
m_pc = new Utils::PathChooser(this);
m_pc->setBaseDirectory(m_cmakeWizard->sourceDirectory());
2009-03-10 09:36:18 +01:00
m_pc->setPath(m_cmakeWizard->buildDirectory());
connect(m_pc, SIGNAL(changed(QString)), this, SLOT(buildDirectoryChanged()));
fl->addRow(tr("Build directory:"), m_pc);
2010-03-31 14:48:08 +02:00
setTitle(tr("Build Location"));
2009-03-10 09:36:18 +01:00
}
void ShadowBuildPage::buildDirectoryChanged()
{
m_cmakeWizard->setBuildDirectory(m_pc->path());
}
CMakeRunPage::CMakeRunPage(CMakeOpenProjectWizard *cmakeWizard, Mode mode, const QString &buildDirectory)
: QWizardPage(cmakeWizard),
m_cmakeWizard(cmakeWizard),
m_complete(false),
m_mode(mode),
m_buildDirectory(buildDirectory)
{
initWidgets();
}
void CMakeRunPage::initWidgets()
2009-03-10 09:36:18 +01:00
{
QFormLayout *fl = new QFormLayout;
setLayout(fl);
// Description Label
m_descriptionLabel = new QLabel(this);
m_descriptionLabel->setWordWrap(true);
fl->addRow(m_descriptionLabel);
2009-03-10 09:36:18 +01:00
if (m_cmakeWizard->cmakeManager()->isCMakeExecutableValid()) {
m_cmakeExecutable = 0;
} else {
QString text = tr("Please specify the path to the CMake executable. No CMake executable was found in the path.");
QString cmakeExecutable = m_cmakeWizard->cmakeManager()->cmakeExecutable();
if (!cmakeExecutable.isEmpty()) {
QFileInfo fi(cmakeExecutable);
if (!fi.exists())
text += tr(" The CMake executable (%1) does not exist.").arg(cmakeExecutable);
else if (!fi.isExecutable())
text += tr(" The path %1 is not a executable.").arg(cmakeExecutable);
else
text += tr(" The path %1 is not a valid CMake.").arg(cmakeExecutable);
}
fl->addRow(new QLabel(text, this));
// Show a field for the user to enter
m_cmakeExecutable = new Utils::PathChooser(this);
m_cmakeExecutable->setExpectedKind(Utils::PathChooser::ExistingCommand);
fl->addRow("CMake Executable", m_cmakeExecutable);
}
// Run CMake Line (with arguments)
2009-03-10 09:36:18 +01:00
m_argumentsLineEdit = new QLineEdit(this);
connect(m_argumentsLineEdit,SIGNAL(returnPressed()), this, SLOT(runCMake()));
2009-03-10 09:36:18 +01:00
m_generatorComboBox = new QComboBox(this);
2009-03-10 09:36:18 +01:00
m_runCMake = new QPushButton(this);
m_runCMake->setText(tr("Run CMake"));
2009-03-10 09:36:18 +01:00
connect(m_runCMake, SIGNAL(clicked()), this, SLOT(runCMake()));
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(m_argumentsLineEdit);
hbox->addWidget(m_generatorComboBox);
hbox->addWidget(m_runCMake);
fl->addRow(tr("Arguments"), hbox);
// Bottom output window
2009-03-10 09:36:18 +01:00
m_output = new QPlainTextEdit(this);
m_output->setReadOnly(true);
QSizePolicy pl = m_output->sizePolicy();
pl.setVerticalStretch(1);
m_output->setSizePolicy(pl);
2009-03-10 09:36:18 +01:00
fl->addRow(m_output);
2010-03-31 14:48:08 +02:00
setTitle(tr("Run CMake"));
2009-03-10 09:36:18 +01:00
}
void CMakeRunPage::initializePage()
{
if (m_mode == Initial) {
m_complete = m_cmakeWizard->existsUpToDateXmlFile();
m_buildDirectory = m_cmakeWizard->buildDirectory();
if (m_cmakeWizard->existsUpToDateXmlFile()) {
m_descriptionLabel->setText(
2009-07-31 16:41:12 +02:00
tr("The directory %1 already contains a cbp file, which is recent enough. "
"You can pass special arguments or change the used toolchain here and rerun CMake. "
"Or simply finish the wizard directly.").arg(m_buildDirectory));
} else {
m_descriptionLabel->setText(
tr("The directory %1 does not contain a cbp file. Qt Creator needs to create this file by running CMake. "
"Some projects require command line arguments to the initial CMake call.").arg(m_buildDirectory));
}
} else if (m_mode == CMakeRunPage::NeedToUpdate) {
m_descriptionLabel->setText(tr("The directory %1 contains an outdated .cbp file. Qt "
"Creator needs to update this file by running CMake. "
"If you want to add additional command line arguments, "
"add them below. Note that CMake remembers command "
2009-05-07 15:34:52 +02:00
"line arguments from the previous runs.").arg(m_buildDirectory));
} else if(m_mode == CMakeRunPage::Recreate) {
2009-05-07 15:34:52 +02:00
m_descriptionLabel->setText(tr("The directory %1 specified in a build-configuration, "
"does not contain a cbp file. Qt Creator needs to "
"recreate this file, by running CMake. "
"Some projects require command line arguments to "
"the initial CMake call. Note that CMake remembers command "
2009-05-07 15:34:52 +02:00
"line arguments from the previous runs.").arg(m_buildDirectory));
} else if(m_mode == CMakeRunPage::ChangeDirectory) {
m_buildDirectory = m_cmakeWizard->buildDirectory();
m_descriptionLabel->setText(tr("Qt Creator needs to run CMake in the new build directory. "
"Some projects require command line arguments to the "
"initial CMake call."));
} else if (m_mode == CMakeRunPage::WantToUpdate) {
m_descriptionLabel->setText(tr("Refreshing cbp file in %1.").arg(m_buildDirectory));
}
if (m_cmakeWizard->cmakeManager()->hasCodeBlocksMsvcGenerator()) {
m_generatorComboBox->setVisible(true);
QString cachedGenerator;
// Try to find out generator from CMakeCachhe file, if it exists
QFile fi(m_buildDirectory + "/CMakeCache.txt");
if (fi.exists()) {
// Cache exists, then read it...
if (fi.open(QIODevice::ReadOnly | QIODevice::Text)) {
while (!fi.atEnd()) {
QString line = fi.readLine();
if (line.startsWith("CMAKE_GENERATOR:INTERNAL=")) {
int splitpos = line.indexOf('=');
if (splitpos != -1) {
cachedGenerator = line.mid(splitpos + 1).trimmed();
}
break;
}
}
}
}
m_generatorComboBox->clear();
// Find out whether we have multiple msvc versions
QStringList msvcVersions = ProjectExplorer::ToolChain::availableMSVCVersions();
if (msvcVersions.isEmpty()) {
} else if (msvcVersions.count() == 1) {
m_generatorComboBox->addItem(tr("NMake Generator"), msvcVersions.first());
} else {
foreach (const QString &msvcVersion, msvcVersions)
m_generatorComboBox->addItem(tr("NMake Generator (%1)").arg(msvcVersion), msvcVersion);
}
if (cachedGenerator == "NMake Makefiles" && !msvcVersions.isEmpty()) {
m_generatorComboBox->setCurrentIndex(0);
m_cmakeWizard->setMsvcVersion(msvcVersions.first());
}
m_generatorComboBox->addItem(tr("MinGW Generator"), "mingw");
if (cachedGenerator == "MinGW Makefiles") {
m_generatorComboBox->setCurrentIndex(m_generatorComboBox->count() - 1);
m_cmakeWizard->setMsvcVersion("");
}
} else {
// No new enough cmake, simply hide the combo box
m_generatorComboBox->setVisible(false);
}
}
2009-03-10 09:36:18 +01:00
void CMakeRunPage::runCMake()
{
m_runCMake->setEnabled(false);
m_argumentsLineEdit->setEnabled(false);
QStringList arguments = Utils::Environment::parseCombinedArgString(m_argumentsLineEdit->text());
2009-03-10 09:36:18 +01:00
CMakeManager *cmakeManager = m_cmakeWizard->cmakeManager();
#ifdef Q_OS_WIN
m_cmakeWizard->setMsvcVersion(QString());
QString generator = QLatin1String("-GCodeBlocks - MinGW Makefiles");
if (m_generatorComboBox->isVisible()) {
// the combobox is shown, check which generator is selected
int index = m_generatorComboBox->currentIndex();
if (index != -1) {
QString version = m_generatorComboBox->itemData(index).toString();
if (version != "mingw") {
generator = "-GCodeBlocks - NMake Makefiles";
m_cmakeWizard->setMsvcVersion(version);
} else {
m_cmakeWizard->setMsvcVersion("");
}
}
}
#else // Q_OS_WIN
QString generator = QLatin1String("-GCodeBlocks - Unix Makefiles");
#endif
Utils::Environment env = m_cmakeWizard->environment();
if (!m_cmakeWizard->msvcVersion().isEmpty()) {
// Add the environment of that msvc version to environment
ProjectExplorer::ToolChain *tc = ProjectExplorer::ToolChain::createMSVCToolChain(m_cmakeWizard->msvcVersion(), false);
tc->addToEnvironment(env);
delete tc;
}
if (m_cmakeExecutable) {
// We asked the user for the cmake executable
m_cmakeWizard->cmakeManager()->setCMakeExecutable(m_cmakeExecutable->path());
}
m_output->clear();
if (m_cmakeWizard->cmakeManager()->isCMakeExecutableValid()) {
m_cmakeProcess = new QProcess();
connect(m_cmakeProcess, SIGNAL(readyRead()), this, SLOT(cmakeReadyRead()));
connect(m_cmakeProcess, SIGNAL(finished(int)), this, SLOT(cmakeFinished()));
cmakeManager->createXmlFile(m_cmakeProcess, arguments, m_cmakeWizard->sourceDirectory(), m_buildDirectory, env, generator);
} else {
m_runCMake->setEnabled(true);
m_argumentsLineEdit->setEnabled(true);
m_output->appendPlainText(tr("No valid CMake executable specified."));
}
2009-03-10 09:36:18 +01:00
}
void CMakeRunPage::cmakeReadyRead()
{
m_output->appendPlainText(m_cmakeProcess->readAll());
}
void CMakeRunPage::cmakeFinished()
{
m_runCMake->setEnabled(true);
m_argumentsLineEdit->setEnabled(true);
m_cmakeProcess->deleteLater();
m_cmakeProcess = 0;
m_cmakeWizard->setArguments(Utils::Environment::parseCombinedArgString(m_argumentsLineEdit->text()));
2009-03-10 09:36:18 +01:00
//TODO Actually test that running cmake was finished, for setting this bool
m_complete = true;
emit completeChanged();
}
void CMakeRunPage::cleanupPage()
{
m_output->clear();
m_complete = false;
emit completeChanged();
}
bool CMakeRunPage::isComplete() const
{
return m_complete;
}