From 97dc1d4ab47e98952deca8a3a0624466219b83ef Mon Sep 17 00:00:00 2001
From: Daniel Teske <daniel.teske@nokia.com>
Date: Wed, 16 Nov 2011 14:15:54 +0100
Subject: [PATCH] More robustness in reading .cbp files

Instead of choosing the first one, choose the most recently modified one.
Also smallish fix to the title if no .cbp file exists

Task-Number: QTCREATORBUG-6553
Task-Number: QTCREATORBUG-6532
Change-Id: I34e2d7ca6d8242356e92099ed29ca0f0a4a11574
Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
---
 src/plugins/cmakeprojectmanager/cmakeproject.cpp   |  5 +++++
 .../cmakeprojectmanager/cmakeprojectmanager.cpp    | 14 +++++++++++---
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/plugins/cmakeprojectmanager/cmakeproject.cpp b/src/plugins/cmakeprojectmanager/cmakeproject.cpp
index 596df19c836..59537b8c872 100644
--- a/src/plugins/cmakeprojectmanager/cmakeproject.cpp
+++ b/src/plugins/cmakeprojectmanager/cmakeproject.cpp
@@ -204,6 +204,11 @@ bool CMakeProject::parseCMakeLists()
     CMakeBuildConfiguration *activeBC = activeTarget()->activeBuildConfiguration();
     QString cbpFile = CMakeManager::findCbpFile(activeBC->buildDirectory());
 
+    if (cbpFile.isEmpty()) {
+        emit buildTargetsChanged();
+        return false;
+    }
+
     // setFolderName
     m_rootNode->setDisplayName(QFileInfo(cbpFile).completeBaseName());
     CMakeCbpParser cbpparser;
diff --git a/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp b/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp
index 76064be1e05..a9df983f546 100644
--- a/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp
+++ b/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp
@@ -49,6 +49,7 @@
 #include <QtCore/QtConcurrentRun>
 #include <QtCore/QCoreApplication>
 #include <QtCore/QSettings>
+#include <QtCore/QDateTime>
 #include <QtGui/QFormLayout>
 #include <QtGui/QBoxLayout>
 #include <QtGui/QDesktopServices>
@@ -200,11 +201,18 @@ QString CMakeManager::findCbpFile(const QDir &directory)
     //   TODO the cbp file is named like the project() command in the CMakeList.txt file
     //   so this method below could find the wrong cbp file, if the user changes the project()
     //   2name
+    QDateTime t;
+    QString file;
     foreach (const QString &cbpFile , directory.entryList()) {
-        if (cbpFile.endsWith(QLatin1String(".cbp")))
-            return directory.path() + QLatin1Char('/') + cbpFile;
+        if (cbpFile.endsWith(QLatin1String(".cbp"))) {
+            QFileInfo fi(directory.path() + QLatin1Char('/') + cbpFile);
+            if (t.isNull() || fi.lastModified() > t) {
+                file = directory.path() + QLatin1Char('/') + cbpFile;
+                t = fi.lastModified();
+            }
+        }
     }
-    return QString();
+    return file;
 }
 
 // This code is duplicated from qtversionmanager
-- 
GitLab