From 7dfbb74d588bb429022fe6e44a8a8d7b8ff5e7b0 Mon Sep 17 00:00:00 2001
From: Tobias Hunger <tobias.hunger@digia.com>
Date: Fri, 2 May 2014 11:44:32 +0200
Subject: [PATCH] Qbs: Honor default build directory set up in Build &
 Run->General

Change-Id: I1e7d6cc97f2e8adb5956866205493d28d79887cb
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
---
 .../qbsprojectmanager/qbsbuildconfiguration.cpp      |  9 ++++-----
 src/plugins/qbsprojectmanager/qbsproject.cpp         | 12 ++++++++----
 src/plugins/qbsprojectmanager/qbsproject.h           |  4 +++-
 3 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/src/plugins/qbsprojectmanager/qbsbuildconfiguration.cpp b/src/plugins/qbsprojectmanager/qbsbuildconfiguration.cpp
index 5e243de525a..77b40fa152a 100644
--- a/src/plugins/qbsprojectmanager/qbsbuildconfiguration.cpp
+++ b/src/plugins/qbsprojectmanager/qbsbuildconfiguration.cpp
@@ -277,18 +277,16 @@ QList<ProjectExplorer::BuildInfo *> QbsBuildConfigurationFactory::availableSetup
 {
     QList<ProjectExplorer::BuildInfo *> result;
 
-    const Utils::FileName buildDirectory = QbsProject::defaultBuildDirectory(projectPath);
-
     ProjectExplorer::BuildInfo *info = createBuildInfo(k, ProjectExplorer::BuildConfiguration::Debug);
     //: The name of the debug build configuration created by default for a qbs project.
     info->displayName = tr("Debug");
-    info->buildDirectory = buildDirectory;
+    info->buildDirectory = QbsProject::defaultBuildDirectory(projectPath, k, info->displayName);
     result << info;
 
     info = createBuildInfo(k, ProjectExplorer::BuildConfiguration::Release);
     //: The name of the release build configuration created by default for a qbs project.
     info->displayName = tr("Release");
-    info->buildDirectory = buildDirectory;
+    info->buildDirectory = QbsProject::defaultBuildDirectory(projectPath, k, info->displayName);
     result << info;
 
     return result;
@@ -311,7 +309,8 @@ ProjectExplorer::BuildConfiguration *QbsBuildConfigurationFactory::create(Projec
 
     Utils::FileName buildDir = info->buildDirectory;
     if (buildDir.isEmpty())
-        buildDir = QbsProject::defaultBuildDirectory(parent->project()->projectDirectory());
+        buildDir = QbsProject::defaultBuildDirectory(parent->project()->projectDirectory(),
+                                                     parent->kit(), info->displayName);
 
     ProjectExplorer::BuildConfiguration *bc
             = QbsBuildConfiguration::setup(parent, info->displayName, info->displayName,
diff --git a/src/plugins/qbsprojectmanager/qbsproject.cpp b/src/plugins/qbsprojectmanager/qbsproject.cpp
index 000fdcca008..317834df4c3 100644
--- a/src/plugins/qbsprojectmanager/qbsproject.cpp
+++ b/src/plugins/qbsprojectmanager/qbsproject.cpp
@@ -52,6 +52,7 @@
 #include <projectexplorer/kitinformation.h>
 #include <projectexplorer/projectexplorer.h>
 #include <projectexplorer/projectexplorerconstants.h>
+#include <projectexplorer/projectmacroexpander.h>
 #include <projectexplorer/target.h>
 #include <projectexplorer/taskhub.h>
 #include <projectexplorer/toolchain.h>
@@ -247,11 +248,14 @@ bool QbsProject::hasParseResult() const
     return qbsProject().isValid();
 }
 
-Utils::FileName QbsProject::defaultBuildDirectory(const QString &path)
+Utils::FileName QbsProject::defaultBuildDirectory(const QString &projectFilePath, const Kit *k,
+                                                  const QString &bcName)
 {
-    QFileInfo fi(path);
-    const QString buildDir = QDir(fi.canonicalPath()).absoluteFilePath(QString::fromLatin1("../%1-build").arg(fi.baseName()));
-    return FileName::fromString(QDir::cleanPath(buildDir));
+    const QString projectName = QFileInfo(projectFilePath).completeBaseName();
+    ProjectExplorer::ProjectMacroExpander expander(projectFilePath, projectName, k, bcName);
+    QString projectDir = projectDirectory(projectFilePath);
+    QString buildPath = Utils::expandMacros(Core::DocumentManager::buildDirectory(), &expander);
+    return Utils::FileName::fromString(Utils::FileUtils::resolvePath(projectDir, buildPath));
 }
 
 qbs::Project QbsProject::qbsProject() const
diff --git a/src/plugins/qbsprojectmanager/qbsproject.h b/src/plugins/qbsprojectmanager/qbsproject.h
index 0f0ab61b3c4..00fc85f0038 100644
--- a/src/plugins/qbsprojectmanager/qbsproject.h
+++ b/src/plugins/qbsprojectmanager/qbsproject.h
@@ -92,7 +92,9 @@ public:
     bool hasParseResult() const;
     void parseCurrentBuildConfiguration(bool force);
 
-    static Utils::FileName defaultBuildDirectory(const QString &path);
+    static Utils::FileName defaultBuildDirectory(const QString &projectFilePath,
+                                                 const ProjectExplorer::Kit *k,
+                                                 const QString &bcName);
 
     qbs::Project qbsProject() const;
     const qbs::ProjectData qbsProjectData() const;
-- 
GitLab