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 "cmakeproject.h"
35 #include "cmakeprojectconstants.h"
36 #include "cmakeprojectnodes.h"
37 #include "cmakerunconfiguration.h"
38 #include "cmaketarget.h"
40 #include "cmakeopenprojectwizard.h"
41 #include "cmakebuildconfiguration.h"
42 #include "cmakeuicodemodelsupport.h"
44 #include <projectexplorer/projectexplorerconstants.h>
45 #include <projectexplorer/projectexplorer.h>
46 #include <projectexplorer/buildenvironmentwidget.h>
47 #include <projectexplorer/buildsteplist.h>
48 #include <projectexplorer/buildmanager.h>
49 #include <projectexplorer/toolchain.h>
50 #include <cplusplus/ModelManagerInterface.h>
51 #include <extensionsystem/pluginmanager.h>
52 #include <utils/qtcassert.h>
53 #include <coreplugin/icore.h>
54 #include <coreplugin/editormanager/editormanager.h>
56 #include <QtCore/QMap>
57 #include <QtCore/QDebug>
58 #include <QtCore/QDir>
59 #include <QtCore/QDateTime>
60 #include <QtCore/QProcess>
61 #include <QtGui/QFormLayout>
62 #include <QtGui/QMainWindow>
63 #include <QtGui/QInputDialog>
65 using namespace CMakeProjectManager;
66 using namespace CMakeProjectManager::Internal;
67 using namespace ProjectExplorer;
69 // QtCreator CMake Generator wishlist:
70 // Which make targets we need to build to get all executables
71 // What is the make we need to call
72 // What is the actual compiler executable
76 // Who sets up the environment for cl.exe ? INCLUDEPATH and so on
78 // Test for form editor (loosely coupled)
79 static inline bool isFormWindowEditor(const QObject *o)
81 return o && !qstrcmp(o->metaObject()->className(), "Designer::FormWindowEditor");
84 // Return contents of form editor (loosely coupled)
85 static inline QString formWindowEditorContents(const QObject *editor)
87 const QVariant contentV = editor->property("contents");
88 QTC_ASSERT(contentV.isValid(), return QString(); )
89 return contentV.toString();
95 CMakeProject::CMakeProject(CMakeManager *manager, const QString &fileName)
98 m_rootNode(new CMakeProjectNode(m_fileName)),
99 m_insideFileChanged(false),
102 m_file = new CMakeFile(this, fileName);
104 connect(this, SIGNAL(addedTarget(ProjectExplorer::Target*)),
105 SLOT(targetAdded(ProjectExplorer::Target*)));
108 CMakeProject::~CMakeProject()
110 // Remove CodeModel support
111 CPlusPlus::CppModelManagerInterface *modelManager
112 = CPlusPlus::CppModelManagerInterface::instance();
113 QMap<QString, CMakeUiCodeModelSupport *>::const_iterator it, end;
114 it = m_uiCodeModelSupport.constBegin();
115 end = m_uiCodeModelSupport.constEnd();
116 for (; it!=end; ++it) {
117 modelManager->removeEditorSupport(it.value());
121 m_codeModelFuture.cancel();
125 void CMakeProject::fileChanged(const QString &fileName)
128 if (!activeTarget() ||
129 !activeTarget()->activeBuildConfiguration())
132 if (m_insideFileChanged)
134 m_insideFileChanged = true;
135 changeActiveBuildConfiguration(activeTarget()->activeBuildConfiguration());
136 m_insideFileChanged = false;
139 void CMakeProject::changeActiveBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
141 if (!bc || bc->target() != activeTarget())
144 CMakeBuildConfiguration * cmakebc(qobject_cast<CMakeBuildConfiguration *>(bc));
148 // Pop up a dialog asking the user to rerun cmake
149 QFileInfo sourceFileInfo(m_fileName);
151 QString cbpFile = CMakeManager::findCbpFile(QDir(bc->buildDirectory()));
152 QFileInfo cbpFileFi(cbpFile);
153 CMakeOpenProjectWizard::Mode mode = CMakeOpenProjectWizard::Nothing;
154 if (!cbpFileFi.exists()) {
155 mode = CMakeOpenProjectWizard::NeedToCreate;
157 foreach(const QString &file, m_watchedFiles) {
158 if (QFileInfo(file).lastModified() > cbpFileFi.lastModified()) {
159 mode = CMakeOpenProjectWizard::NeedToUpdate;
165 if (mode != CMakeOpenProjectWizard::Nothing) {
166 CMakeOpenProjectWizard copw(m_manager,
167 sourceFileInfo.absolutePath(),
168 cmakebc->buildDirectory(),
170 cmakebc->environment());
172 cmakebc->setMsvcVersion(copw.msvcVersion());
178 void CMakeProject::targetAdded(ProjectExplorer::Target *t)
183 connect(t, SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
184 SLOT(changeActiveBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
187 void CMakeProject::changeBuildDirectory(CMakeBuildConfiguration *bc, const QString &newBuildDirectory)
189 bc->setBuildDirectory(newBuildDirectory);
193 QString CMakeProject::defaultBuildDirectory() const
195 return projectDirectory() + QLatin1String("/qtcreator-build");
198 bool CMakeProject::parseCMakeLists()
200 if (!activeTarget() ||
201 !activeTarget()->activeBuildConfiguration())
205 CMakeBuildConfiguration *activeBC = activeTarget()->activeBuildConfiguration();
206 QString cbpFile = CMakeManager::findCbpFile(activeBC->buildDirectory());
209 m_rootNode->setDisplayName(QFileInfo(cbpFile).completeBaseName());
210 CMakeCbpParser cbpparser;
212 //qDebug()<<"Parsing file "<<cbpFile;
213 if (!cbpparser.parseCbpFile(cbpFile)) {
215 qDebug()<<"Parsing failed";
216 // activeBC->updateToolChain(QString::null);
217 emit buildTargetsChanged();
222 // activeBC->updateToolChain(cbpparser.compilerName());
223 m_projectName = cbpparser.projectName();
224 m_rootNode->setDisplayName(cbpparser.projectName());
226 //qDebug()<<"Building Tree";
227 QList<ProjectExplorer::FileNode *> fileList = cbpparser.fileList();
228 QSet<QString> projectFiles;
229 if (cbpparser.hasCMakeFiles()) {
230 fileList.append(cbpparser.cmakeFileList());
231 foreach(const ProjectExplorer::FileNode *node, cbpparser.cmakeFileList())
232 projectFiles.insert(node->path());
234 // Manually add the CMakeLists.txt file
235 QString cmakeListTxt = projectDirectory() + "/CMakeLists.txt";
236 bool generated = false;
237 fileList.append(new ProjectExplorer::FileNode(cmakeListTxt, ProjectExplorer::ProjectFileType, generated));
238 projectFiles.insert(cmakeListTxt);
241 QSet<QString> added = projectFiles;
242 added.subtract(m_watchedFiles);
243 foreach(const QString &add, added)
244 m_watcher->addFile(add);
245 foreach(const QString &remove, m_watchedFiles.subtract(projectFiles))
246 m_watcher->removeFile(remove);
247 m_watchedFiles = projectFiles;
250 foreach (ProjectExplorer::FileNode *fn, fileList)
251 m_files.append(fn->path());
254 buildTree(m_rootNode, fileList);
256 //qDebug()<<"Adding Targets";
257 m_buildTargets = cbpparser.buildTargets();
258 // qDebug()<<"Printing targets";
259 // foreach(CMakeBuildTarget ct, m_buildTargets) {
260 // qDebug()<<ct.title<<" with executable:"<<ct.executable;
261 // qDebug()<<"WD:"<<ct.workingDirectory;
262 // qDebug()<<ct.makeCommand<<ct.makeCleanCommand;
267 // TOOD this code ain't very pretty ...
268 m_uicCommand.clear();
269 QFile cmakeCache(activeBC->buildDirectory() + "/CMakeCache.txt");
270 cmakeCache.open(QIODevice::ReadOnly);
271 while (!cmakeCache.atEnd()) {
272 QString line = cmakeCache.readLine();
273 if (line.startsWith("QT_UIC_EXECUTABLE")) {
274 if (int pos = line.indexOf('=')) {
275 m_uicCommand = line.mid(pos + 1).trimmed();
282 //qDebug()<<"Updating CodeModel";
283 createUiCodeModelSupport();
285 QStringList allIncludePaths;
286 QStringList allFrameworkPaths;
287 QList<ProjectExplorer::HeaderPath> allHeaderPaths = activeBC->toolChain()->systemHeaderPaths();
288 foreach (const ProjectExplorer::HeaderPath &headerPath, allHeaderPaths) {
289 if (headerPath.kind() == ProjectExplorer::HeaderPath::FrameworkHeaderPath)
290 allFrameworkPaths.append(headerPath.path());
292 allIncludePaths.append(headerPath.path());
294 // This explicitly adds -I. to the include paths
295 allIncludePaths.append(projectDirectory());
297 allIncludePaths.append(cbpparser.includeFiles());
298 CPlusPlus::CppModelManagerInterface *modelmanager =
299 CPlusPlus::CppModelManagerInterface::instance();
301 CPlusPlus::CppModelManagerInterface::ProjectInfo pinfo = modelmanager->projectInfo(this);
302 if (pinfo.includePaths != allIncludePaths
303 || pinfo.sourceFiles != m_files
304 || pinfo.defines != activeBC->toolChain()->predefinedMacros()
305 || pinfo.frameworkPaths != allFrameworkPaths) {
306 pinfo.includePaths = allIncludePaths;
307 // TODO we only want C++ files, not all other stuff that might be in the project
308 pinfo.sourceFiles = m_files;
309 pinfo.defines = activeBC->toolChain()->predefinedMacros(); // TODO this is to simplistic
310 pinfo.frameworkPaths = allFrameworkPaths;
311 modelmanager->updateProjectInfo(pinfo);
312 m_codeModelFuture.cancel();
313 m_codeModelFuture = modelmanager->updateSourceFiles(pinfo.sourceFiles);
317 emit buildTargetsChanged();
318 emit fileListChanged();
322 QList<CMakeBuildTarget> CMakeProject::buildTargets() const
324 return m_buildTargets;
327 QStringList CMakeProject::buildTargetTitles() const
330 foreach (const CMakeBuildTarget &ct, m_buildTargets) {
331 if (ct.executable.isEmpty())
333 if (ct.title.endsWith(QLatin1String("/fast")))
340 bool CMakeProject::hasBuildTarget(const QString &title) const
342 foreach (const CMakeBuildTarget &ct, m_buildTargets) {
343 if (ct.executable.isEmpty())
345 if (ct.title.endsWith(QLatin1String("/fast")))
347 if (ct.title == title)
353 void CMakeProject::gatherFileNodes(ProjectExplorer::FolderNode *parent, QList<ProjectExplorer::FileNode *> &list)
355 foreach(ProjectExplorer::FolderNode *folder, parent->subFolderNodes())
356 gatherFileNodes(folder, list);
357 foreach(ProjectExplorer::FileNode *file, parent->fileNodes())
361 void CMakeProject::buildTree(CMakeProjectNode *rootNode, QList<ProjectExplorer::FileNode *> newList)
364 QList<ProjectExplorer::FileNode *> oldList;
365 gatherFileNodes(rootNode, oldList);
366 qSort(oldList.begin(), oldList.end(), ProjectExplorer::ProjectNode::sortNodesByPath);
367 qSort(newList.begin(), newList.end(), ProjectExplorer::ProjectNode::sortNodesByPath);
369 // generate added and deleted list
370 QList<ProjectExplorer::FileNode *>::const_iterator oldIt = oldList.constBegin();
371 QList<ProjectExplorer::FileNode *>::const_iterator oldEnd = oldList.constEnd();
372 QList<ProjectExplorer::FileNode *>::const_iterator newIt = newList.constBegin();
373 QList<ProjectExplorer::FileNode *>::const_iterator newEnd = newList.constEnd();
375 QList<ProjectExplorer::FileNode *> added;
376 QList<ProjectExplorer::FileNode *> deleted;
379 while(oldIt != oldEnd && newIt != newEnd) {
380 if ( (*oldIt)->path() == (*newIt)->path()) {
384 } else if ((*oldIt)->path() < (*newIt)->path()) {
385 deleted.append(*oldIt);
388 added.append(*newIt);
393 while (oldIt != oldEnd) {
394 deleted.append(*oldIt);
398 while (newIt != newEnd) {
399 added.append(*newIt);
404 foreach (ProjectExplorer::FileNode *fn, added) {
405 // qDebug()<<"added"<<fn->path();
406 // Get relative path to rootNode
407 QString parentDir = QFileInfo(fn->path()).absolutePath();
408 ProjectExplorer::FolderNode *folder = findOrCreateFolder(rootNode, parentDir);
409 rootNode->addFileNodes(QList<ProjectExplorer::FileNode *>()<< fn, folder);
412 // remove old file nodes and check whether folder nodes can be removed
413 foreach (ProjectExplorer::FileNode *fn, deleted) {
414 ProjectExplorer::FolderNode *parent = fn->parentFolderNode();
415 // qDebug()<<"removed"<<fn->path();
416 rootNode->removeFileNodes(QList<ProjectExplorer::FileNode *>() << fn, parent);
417 // Check for empty parent
418 while (parent->subFolderNodes().isEmpty() && parent->fileNodes().isEmpty()) {
419 ProjectExplorer::FolderNode *grandparent = parent->parentFolderNode();
420 rootNode->removeFolderNodes(QList<ProjectExplorer::FolderNode *>() << parent, grandparent);
421 parent = grandparent;
422 if (parent == rootNode)
428 ProjectExplorer::FolderNode *CMakeProject::findOrCreateFolder(CMakeProjectNode *rootNode, QString directory)
430 QString relativePath = QDir(QFileInfo(rootNode->path()).path()).relativeFilePath(directory);
431 QStringList parts = relativePath.split(QLatin1Char('/'), QString::SkipEmptyParts);
432 ProjectExplorer::FolderNode *parent = rootNode;
433 QString path = QFileInfo(rootNode->path()).path();
434 foreach (const QString &part, parts) {
435 path += QLatin1Char('/');
437 // Find folder in subFolders
439 foreach (ProjectExplorer::FolderNode *folder, parent->subFolderNodes()) {
440 if (folder->path() == path) {
441 // yeah found something :)
448 // No FolderNode yet, so create it
449 ProjectExplorer::FolderNode *tmp = new ProjectExplorer::FolderNode(path);
450 tmp->setDisplayName(part);
451 rootNode->addFolderNodes(QList<ProjectExplorer::FolderNode *>() << tmp, parent);
458 QString CMakeProject::displayName() const
460 return m_projectName;
463 QString CMakeProject::id() const
465 return QLatin1String(Constants::CMAKEPROJECT_ID);
468 Core::IFile *CMakeProject::file() const
473 CMakeManager *CMakeProject::projectManager() const
478 CMakeTarget *CMakeProject::activeTarget() const
480 return static_cast<CMakeTarget *>(Project::activeTarget());
483 QList<ProjectExplorer::Project *> CMakeProject::dependsOn()
485 return QList<Project *>();
488 QList<ProjectExplorer::BuildConfigWidget*> CMakeProject::subConfigWidgets()
490 QList<ProjectExplorer::BuildConfigWidget*> list;
491 list << new BuildEnvironmentWidget;
495 ProjectExplorer::ProjectNode *CMakeProject::rootProjectNode() const
501 QStringList CMakeProject::files(FilesMode fileMode) const
507 bool CMakeProject::fromMap(const QVariantMap &map)
509 if (!Project::fromMap(map))
512 bool hasUserFile = activeTarget();
514 CMakeTargetFactory *factory =
515 ExtensionSystem::PluginManager::instance()->getObject<CMakeTargetFactory>();
516 CMakeTarget *t = factory->create(this, QLatin1String(DEFAULT_CMAKE_TARGET_ID));
519 Q_ASSERT(t->activeBuildConfiguration());
521 // Ask the user for where he wants to build it
522 // and the cmake command line
524 CMakeOpenProjectWizard copw(m_manager, projectDirectory(), Utils::Environment::systemEnvironment());
525 if (copw.exec() != QDialog::Accepted)
528 CMakeBuildConfiguration *bc =
529 static_cast<CMakeBuildConfiguration *>(t->buildConfigurations().at(0));
530 bc->setMsvcVersion(copw.msvcVersion());
531 if (!copw.buildDirectory().isEmpty())
532 bc->setBuildDirectory(copw.buildDirectory());
536 // We have a user file, but we could still be missing the cbp file
537 // or simply run createXml with the saved settings
538 QFileInfo sourceFileInfo(m_fileName);
539 CMakeBuildConfiguration *activeBC = activeTarget()->activeBuildConfiguration();
540 QString cbpFile = CMakeManager::findCbpFile(QDir(activeBC->buildDirectory()));
541 QFileInfo cbpFileFi(cbpFile);
543 CMakeOpenProjectWizard::Mode mode = CMakeOpenProjectWizard::Nothing;
544 if (!cbpFileFi.exists())
545 mode = CMakeOpenProjectWizard::NeedToCreate;
546 else if (cbpFileFi.lastModified() < sourceFileInfo.lastModified())
547 mode = CMakeOpenProjectWizard::NeedToUpdate;
549 if (mode != CMakeOpenProjectWizard::Nothing) {
550 CMakeOpenProjectWizard copw(m_manager,
551 sourceFileInfo.absolutePath(),
552 activeBC->buildDirectory(),
554 activeBC->environment());
555 if (copw.exec() != QDialog::Accepted)
557 activeBC->setMsvcVersion(copw.msvcVersion());
561 m_watcher = new ProjectExplorer::FileWatcher(this);
562 connect(m_watcher, SIGNAL(fileChanged(QString)), this, SLOT(fileChanged(QString)));
564 if (!parseCMakeLists()) // Gets the directory from the active buildconfiguration
567 if (!hasUserFile && hasBuildTarget("all")) {
568 MakeStep *makeStep = qobject_cast<MakeStep *>(
569 activeTarget()->activeBuildConfiguration()->stepList(ProjectExplorer::Constants::BUILDSTEPS_BUILD)->at(0));
571 makeStep->setBuildTarget("all", true);
574 foreach (Target *t, targets()) {
575 connect(t, SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
576 this, SLOT(changeActiveBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
577 connect(t, SIGNAL(environmentChanged()),
578 this, SLOT(changeEnvironment()));
581 connect(Core::EditorManager::instance(), SIGNAL(editorAboutToClose(Core::IEditor*)),
582 this, SLOT(editorAboutToClose(Core::IEditor*)));
584 connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
585 this, SLOT(editorChanged(Core::IEditor*)));
587 connect(ProjectExplorer::ProjectExplorerPlugin::instance()->buildManager(), SIGNAL(buildStateChanged(ProjectExplorer::Project*)),
588 this, SLOT(buildStateChanged(ProjectExplorer::Project*)));
593 CMakeBuildTarget CMakeProject::buildTargetForTitle(const QString &title)
595 foreach(const CMakeBuildTarget &ct, m_buildTargets)
596 if (ct.title == title)
598 return CMakeBuildTarget();
601 QString CMakeProject::uicCommand() const
606 QString CMakeProject::uiHeaderFile(const QString &uiFile)
608 QDir srcDirRoot = QDir(projectDirectory());
609 QString relativePath = srcDirRoot.relativeFilePath(uiFile);
610 QDir buildDir = QDir(activeTarget()->activeBuildConfiguration()->buildDirectory());
611 QString uiHeaderFilePath = buildDir.absoluteFilePath(relativePath);
613 QFileInfo fi(uiHeaderFilePath);
614 uiHeaderFilePath = fi.absolutePath();
615 uiHeaderFilePath += QLatin1String("/ui_");
616 uiHeaderFilePath += fi.completeBaseName();
617 uiHeaderFilePath += QLatin1String(".h");
618 return QDir::cleanPath(uiHeaderFilePath);
621 void CMakeProject::createUiCodeModelSupport()
623 // qDebug()<<"creatUiCodeModelSupport()";
624 CPlusPlus::CppModelManagerInterface *modelManager
625 = CPlusPlus::CppModelManagerInterface::instance();
628 QMap<QString, CMakeUiCodeModelSupport *> oldCodeModelSupport;
629 oldCodeModelSupport = m_uiCodeModelSupport;
630 m_uiCodeModelSupport.clear();
633 foreach (const QString &uiFile, m_files) {
634 if (uiFile.endsWith(".ui")) {
635 // UI file, not convert to
636 QString uiHeaderFilePath = uiHeaderFile(uiFile);
637 QMap<QString, CMakeUiCodeModelSupport *>::iterator it
638 = oldCodeModelSupport.find(uiFile);
639 if (it != oldCodeModelSupport.end()) {
640 // qDebug()<<"updated old codemodelsupport";
641 CMakeUiCodeModelSupport *cms = it.value();
642 cms->setFileName(uiHeaderFilePath);
643 m_uiCodeModelSupport.insert(it.key(), cms);
644 oldCodeModelSupport.erase(it);
646 // qDebug()<<"adding new codemodelsupport";
647 CMakeUiCodeModelSupport *cms = new CMakeUiCodeModelSupport(modelManager, this, uiFile, uiHeaderFilePath);
648 m_uiCodeModelSupport.insert(uiFile, cms);
649 modelManager->addEditorSupport(cms);
655 QMap<QString, CMakeUiCodeModelSupport *>::const_iterator it, end;
656 end = oldCodeModelSupport.constEnd();
657 for (it = oldCodeModelSupport.constBegin(); it!=end; ++it) {
658 modelManager->removeEditorSupport(it.value());
663 void CMakeProject::updateCodeModelSupportFromEditor(const QString &uiFileName,
664 const QString &contents)
666 const QMap<QString, CMakeUiCodeModelSupport *>::const_iterator it =
667 m_uiCodeModelSupport.constFind(uiFileName);
668 if (it != m_uiCodeModelSupport.constEnd())
669 it.value()->updateFromEditor(contents);
672 void CMakeProject::editorChanged(Core::IEditor *editor)
675 if (isFormWindowEditor(m_lastEditor)) {
676 disconnect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
678 const QString contents = formWindowEditorContents(m_lastEditor);
679 updateCodeModelSupportFromEditor(m_lastEditor->file()->fileName(), contents);
684 m_lastEditor = editor;
687 if (isFormWindowEditor(editor))
688 connect(editor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
691 void CMakeProject::editorAboutToClose(Core::IEditor *editor)
693 if (m_lastEditor == editor) {
694 // Oh no our editor is going to be closed
695 // get the content first
696 if (isFormWindowEditor(m_lastEditor)) {
697 disconnect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
699 const QString contents = formWindowEditorContents(m_lastEditor);
700 updateCodeModelSupportFromEditor(m_lastEditor->file()->fileName(), contents);
708 void CMakeProject::uiEditorContentsChanged()
710 // cast sender, get filename
711 if (!m_dirtyUic && isFormWindowEditor(sender()))
715 void CMakeProject::buildStateChanged(ProjectExplorer::Project *project)
717 if (project == this) {
718 if (!ProjectExplorer::ProjectExplorerPlugin::instance()->buildManager()->isBuilding(this)) {
719 QMap<QString, CMakeUiCodeModelSupport *>::const_iterator it, end;
720 end = m_uiCodeModelSupport.constEnd();
721 for (it = m_uiCodeModelSupport.constBegin(); it != end; ++it) {
722 it.value()->updateFromBuild();
730 CMakeFile::CMakeFile(CMakeProject *parent, QString fileName)
731 : Core::IFile(parent), m_project(parent), m_fileName(fileName)
736 bool CMakeFile::save(const QString &fileName)
738 // Once we have an texteditor open for this file, we probably do
739 // need to implement this, don't we.
744 QString CMakeFile::fileName() const
749 QString CMakeFile::defaultPath() const
754 QString CMakeFile::suggestedFileName() const
759 QString CMakeFile::mimeType() const
761 return Constants::CMAKEMIMETYPE;
765 bool CMakeFile::isModified() const
770 bool CMakeFile::isReadOnly() const
775 bool CMakeFile::isSaveAsAllowed() const
780 void CMakeFile::rename(const QString &newName)
787 Core::IFile::ReloadBehavior CMakeFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
791 return BehaviorSilent;
794 void CMakeFile::reload(ReloadFlag flag, ChangeType type)
800 CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeTarget *target)
801 : m_target(target), m_buildConfiguration(0)
803 QFormLayout *fl = new QFormLayout(this);
804 fl->setContentsMargins(20, -1, 0, -1);
805 fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
808 // TODO add action to Build menu?
809 QPushButton *runCmakeButton = new QPushButton("Run cmake");
810 connect(runCmakeButton, SIGNAL(clicked()),
811 this, SLOT(runCMake()));
812 fl->addRow(tr("Reconfigure project:"), runCmakeButton);
814 m_pathLineEdit = new QLineEdit(this);
815 m_pathLineEdit->setReadOnly(true);
817 QHBoxLayout *hbox = new QHBoxLayout();
818 hbox->addWidget(m_pathLineEdit);
820 m_changeButton = new QPushButton(this);
821 m_changeButton->setText(tr("&Change"));
822 connect(m_changeButton, SIGNAL(clicked()), this, SLOT(openChangeBuildDirectoryDialog()));
823 hbox->addWidget(m_changeButton);
825 fl->addRow("Build directory:", hbox);
828 QString CMakeBuildSettingsWidget::displayName() const
833 void CMakeBuildSettingsWidget::init(BuildConfiguration *bc)
835 m_buildConfiguration = static_cast<CMakeBuildConfiguration *>(bc);
836 m_pathLineEdit->setText(m_buildConfiguration->buildDirectory());
837 if (m_buildConfiguration->buildDirectory() == m_target->cmakeProject()->projectDirectory())
838 m_changeButton->setEnabled(false);
840 m_changeButton->setEnabled(true);
843 void CMakeBuildSettingsWidget::openChangeBuildDirectoryDialog()
845 CMakeProject *project = m_target->cmakeProject();
846 CMakeOpenProjectWizard copw(project->projectManager(),
847 project->projectDirectory(),
848 m_buildConfiguration->buildDirectory(),
849 m_buildConfiguration->environment());
850 if (copw.exec() == QDialog::Accepted) {
851 project->changeBuildDirectory(m_buildConfiguration, copw.buildDirectory());
852 m_pathLineEdit->setText(m_buildConfiguration->buildDirectory());
856 void CMakeBuildSettingsWidget::runCMake()
858 // TODO skip build directory
859 CMakeProject *project = m_target->cmakeProject();
860 CMakeOpenProjectWizard copw(project->projectManager(),
861 project->projectDirectory(),
862 m_buildConfiguration->buildDirectory(),
863 CMakeOpenProjectWizard::WantToUpdate,
864 m_buildConfiguration->environment());
865 if (copw.exec() == QDialog::Accepted) {
866 project->parseCMakeLists();
874 bool CMakeCbpParser::parseCbpFile(const QString &fileName)
877 if (fi.exists() && fi.open(QFile::ReadOnly)) {
882 if (name() == "CodeBlocks_project_file") {
883 parseCodeBlocks_project_file();
884 } else if (isStartElement()) {
885 parseUnknownElement();
889 m_includeFiles.sort();
890 m_includeFiles.removeDuplicates();
896 void CMakeCbpParser::parseCodeBlocks_project_file()
900 if (isEndElement()) {
902 } else if (name() == "Project") {
904 } else if (isStartElement()) {
905 parseUnknownElement();
910 void CMakeCbpParser::parseProject()
914 if (isEndElement()) {
916 } else if (name() == "Option") {
918 } else if (name() == "Unit") {
920 } else if (name() == "Build") {
922 } else if (isStartElement()) {
923 parseUnknownElement();
928 void CMakeCbpParser::parseBuild()
932 if (isEndElement()) {
934 } else if (name() == "Target") {
936 } else if (isStartElement()) {
937 parseUnknownElement();
942 void CMakeCbpParser::parseBuildTarget()
944 m_buildTargetType = false;
945 m_buildTarget.clear();
947 if (attributes().hasAttribute("title"))
948 m_buildTarget.title = attributes().value("title").toString();
951 if (isEndElement()) {
952 if (m_buildTargetType || m_buildTarget.title == "all" || m_buildTarget.title == "install") {
953 m_buildTargets.append(m_buildTarget);
956 } else if (name() == "Compiler") {
958 } else if (name() == "Option") {
959 parseBuildTargetOption();
960 } else if (isStartElement()) {
961 parseUnknownElement();
966 void CMakeCbpParser::parseBuildTargetOption()
968 if (attributes().hasAttribute("output")) {
969 m_buildTarget.executable = attributes().value("output").toString();
970 } else if (attributes().hasAttribute("type") && (attributes().value("type") == "1" || attributes().value("type") == "0")) {
971 m_buildTargetType = true;
972 } else if (attributes().hasAttribute("type") && (attributes().value("type") == "3" || attributes().value("type") == "2")) {
973 m_buildTargetType = true;
974 m_buildTarget.library = true;
975 } else if (attributes().hasAttribute("working_dir")) {
976 m_buildTarget.workingDirectory = attributes().value("working_dir").toString();
980 if (isEndElement()) {
982 } else if (name() == "MakeCommand") {
984 } else if (isStartElement()) {
985 parseUnknownElement();
990 QString CMakeCbpParser::projectName() const
992 return m_projectName;
995 void CMakeCbpParser::parseOption()
997 if (attributes().hasAttribute("title"))
998 m_projectName = attributes().value("title").toString();
1000 if (attributes().hasAttribute("compiler"))
1001 m_compiler = attributes().value("compiler").toString();
1005 if (isEndElement()) {
1007 } else if(isStartElement()) {
1008 parseUnknownElement();
1013 void CMakeCbpParser::parseMakeCommand()
1017 if (isEndElement()) {
1019 } else if (name() == "Build") {
1020 parseBuildTargetBuild();
1021 } else if (name() == "Clean") {
1022 parseBuildTargetClean();
1023 } else if (isStartElement()) {
1024 parseUnknownElement();
1029 void CMakeCbpParser::parseBuildTargetBuild()
1031 if (attributes().hasAttribute("command"))
1032 m_buildTarget.makeCommand = attributes().value("command").toString();
1035 if (isEndElement()) {
1037 } else if (isStartElement()) {
1038 parseUnknownElement();
1043 void CMakeCbpParser::parseBuildTargetClean()
1045 if (attributes().hasAttribute("command"))
1046 m_buildTarget.makeCleanCommand = attributes().value("command").toString();
1049 if (isEndElement()) {
1051 } else if (isStartElement()) {
1052 parseUnknownElement();
1057 void CMakeCbpParser::parseCompiler()
1061 if (isEndElement()) {
1063 } else if (name() == "Add") {
1065 } else if (isStartElement()) {
1066 parseUnknownElement();
1071 void CMakeCbpParser::parseAdd()
1073 m_includeFiles.append(attributes().value("directory").toString());
1076 if (isEndElement()) {
1078 } else if (isStartElement()) {
1079 parseUnknownElement();
1084 void CMakeCbpParser::parseUnit()
1086 //qDebug()<<stream.attributes().value("filename");
1087 QString fileName = attributes().value("filename").toString();
1088 m_parsingCmakeUnit = false;
1091 if (isEndElement()) {
1092 if (!fileName.endsWith(QLatin1String(".rule")) && !m_processedUnits.contains(fileName)) {
1093 // Now check whether we found a virtual element beneath
1094 if (m_parsingCmakeUnit) {
1095 m_cmakeFileList.append( new ProjectExplorer::FileNode(fileName, ProjectExplorer::ProjectFileType, false));
1097 bool generated = false;
1098 QString onlyFileName = QFileInfo(fileName).fileName();
1099 if ( (onlyFileName.startsWith("moc_") && onlyFileName.endsWith(".cxx"))
1100 || (onlyFileName.startsWith("ui_") && onlyFileName.endsWith(".h"))
1101 || (onlyFileName.startsWith("qrc_") && onlyFileName.endsWith(".cxx")))
1104 if (fileName.endsWith(QLatin1String(".qrc")))
1105 m_fileList.append( new ProjectExplorer::FileNode(fileName, ProjectExplorer::ResourceType, generated));
1107 m_fileList.append( new ProjectExplorer::FileNode(fileName, ProjectExplorer::SourceType, generated));
1109 m_processedUnits.insert(fileName);
1112 } else if (name() == "Option") {
1114 } else if (isStartElement()) {
1115 parseUnknownElement();
1120 void CMakeCbpParser::parseUnitOption()
1122 if (attributes().hasAttribute("virtualFolder"))
1123 m_parsingCmakeUnit = true;
1131 if (isStartElement())
1132 parseUnknownElement();
1136 void CMakeCbpParser::parseUnknownElement()
1138 Q_ASSERT(isStartElement());
1146 if (isStartElement())
1147 parseUnknownElement();
1151 QList<ProjectExplorer::FileNode *> CMakeCbpParser::fileList()
1156 QList<ProjectExplorer::FileNode *> CMakeCbpParser::cmakeFileList()
1158 return m_cmakeFileList;
1161 bool CMakeCbpParser::hasCMakeFiles()
1163 return !m_cmakeFileList.isEmpty();
1166 QStringList CMakeCbpParser::includeFiles()
1168 return m_includeFiles;
1171 QList<CMakeBuildTarget> CMakeCbpParser::buildTargets()
1173 return m_buildTargets;
1176 QString CMakeCbpParser::compilerName() const
1181 void CMakeBuildTarget::clear()
1184 makeCommand.clear();
1185 makeCleanCommand.clear();
1186 workingDirectory.clear();