From 23c3f61d1a7fd62b15f73d50e682f1b812422e36 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@digia.com>
Date: Fri, 28 Sep 2012 10:13:56 +0200
Subject: [PATCH] Add Autodetection for system cdb/gdb for kits added by
 sdktool.

Introduced special 'auto' value for debugger binary.

Change-Id: I5be4fa148ce9ded06826a7dda647c5ba402dc70a
Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
---
 .../debugger/debuggerkitinformation.cpp       | 21 ++++++++++++++++++-
 src/tools/sdktool/README.txt                  |  1 +
 src/tools/sdktool/addkitoperation.cpp         |  6 ++++++
 src/tools/sdktool/addkitoperation.h           |  2 ++
 4 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/src/plugins/debugger/debuggerkitinformation.cpp b/src/plugins/debugger/debuggerkitinformation.cpp
index 5b86f09c323..f497e2b19ed 100644
--- a/src/plugins/debugger/debuggerkitinformation.cpp
+++ b/src/plugins/debugger/debuggerkitinformation.cpp
@@ -296,8 +296,27 @@ DebuggerKitInformation::DebuggerItem DebuggerKitInformation::variantToItem(const
     }
     QTC_ASSERT(v.type() == QVariant::Map, return result);
     const QVariantMap vmap = v.toMap();
-    result.binary = Utils::FileName::fromString(vmap.value(QLatin1String(binaryKeyC)).toString());
     result.engineType = static_cast<DebuggerEngineType>(vmap.value(QLatin1String(engineTypeKeyC)).toInt());
+    QString binary = vmap.value(QLatin1String(binaryKeyC)).toString();
+    // Check for special 'auto' entry for binary written by the sdktool during
+    // installation. Try to autodetect.
+    if (binary == QLatin1String("auto")) {
+        binary.clear();
+        switch (result.engineType) {
+        case Debugger::GdbEngineType: // Auto-detect system gdb on Unix
+            if (Abi::hostAbi().os() != Abi::WindowsOS)
+                binary = Environment::systemEnvironment().searchInPath(QLatin1String("gdb"));
+            break;
+        case Debugger::CdbEngineType: { // Auto-detect system CDB on Windows.
+             const QPair<QString, QString> cdbs = autoDetectCdbDebugger();
+             binary = cdbs.second.isEmpty() ? cdbs.first : cdbs.second;
+        }
+            break;
+        default:
+            break;
+        }
+    }
+    result.binary = Utils::FileName::fromString(binary);
     return result;
 }
 
diff --git a/src/tools/sdktool/README.txt b/src/tools/sdktool/README.txt
index be6f2225ff3..9e21e73cbbd 100644
--- a/src/tools/sdktool/README.txt
+++ b/src/tools/sdktool/README.txt
@@ -119,6 +119,7 @@ Add a Kit using the newly set up tool chain and Qt version:
 Tricky parts:
   - debuggerengine is the integer used in the enum Debugger::DebuggerEngineType
     The most important type is 1 for GDB.
+  - debugger can be a absolute path or the value: 'auto'
 
   - devicetype is the string returned IDevice::type()
 
diff --git a/src/tools/sdktool/addkitoperation.cpp b/src/tools/sdktool/addkitoperation.cpp
index 3f8f659dd37..4fd5104efb7 100644
--- a/src/tools/sdktool/addkitoperation.cpp
+++ b/src/tools/sdktool/addkitoperation.cpp
@@ -63,6 +63,12 @@ static char TOOLCHAIN[] = "PE.Profile.ToolChain";
 static char MKSPEC[] = "QtPM4.mkSpecInformation";
 static char QT[] = "QtSupport.QtInformation";
 
+AddKitOperation::AddKitOperation()
+    : m_debuggerEngine(0)
+    , m_debugger(QLatin1String("auto"))
+{
+}
+
 QString AddKitOperation::name() const
 {
     return QLatin1String("addKit");
diff --git a/src/tools/sdktool/addkitoperation.h b/src/tools/sdktool/addkitoperation.h
index e526bcb6044..e1910674b32 100644
--- a/src/tools/sdktool/addkitoperation.h
+++ b/src/tools/sdktool/addkitoperation.h
@@ -38,6 +38,8 @@
 class AddKitOperation : public Operation
 {
 public:
+    AddKitOperation();
+
     QString name() const;
     QString helpText() const;
     QString argumentsHelpText() const;
-- 
GitLab