From 791bb483fd7c1baaef4ade46c4e53c42f7adb636 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Date: Tue, 26 Oct 2010 15:39:37 +0200
Subject: [PATCH] Debugger/JS Code model: Set environment for QML tools
 correctly.

Set complete build environment (MinGW).

Reviewed-by: Christian Kamm <christian.d.kamm@nokia.com>
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>

Task-number: QTCREATORBUG-2883
(cherry picked from commit f1fb0bddb11d69355784953a6cc0364f9fa25d3e)
---
 src/libs/qmljs/qmljsmodelmanagerinterface.h         |  3 +++
 src/plugins/qmljseditor/qmljsmodelmanager.cpp       |  1 +
 src/plugins/qmlprojectmanager/qmlproject.cpp        | 11 ++++++++---
 .../qmlprojectmanager/qmlprojectruncontrol.cpp      | 11 ++++++-----
 src/plugins/qt4projectmanager/qmldumptool.cpp       |  7 +++----
 src/plugins/qt4projectmanager/qt4project.cpp        |  6 ++----
 src/plugins/qt4projectmanager/qtversionmanager.cpp  | 13 +++++++++++++
 src/plugins/qt4projectmanager/qtversionmanager.h    |  1 +
 8 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/src/libs/qmljs/qmljsmodelmanagerinterface.h b/src/libs/qmljs/qmljsmodelmanagerinterface.h
index ed39a810c3e..b7039dcacf2 100644
--- a/src/libs/qmljs/qmljsmodelmanagerinterface.h
+++ b/src/libs/qmljs/qmljsmodelmanagerinterface.h
@@ -33,6 +33,8 @@
 #include "qmljs_global.h"
 #include "qmljsdocument.h"
 
+#include <utils/environment.h>
+
 #include <QObject>
 #include <QStringList>
 #include <QSharedPointer>
@@ -77,6 +79,7 @@ public:
         QStringList sourceFiles;
         QStringList importPaths;
         QString qmlDumpPath;
+        Utils::Environment qmlDumpEnvironment;
     };
 
     class WorkingCopy
diff --git a/src/plugins/qmljseditor/qmljsmodelmanager.cpp b/src/plugins/qmljseditor/qmljsmodelmanager.cpp
index 27cf5c1ced2..74adffe02de 100644
--- a/src/plugins/qmljseditor/qmljsmodelmanager.cpp
+++ b/src/plugins/qmljseditor/qmljsmodelmanager.cpp
@@ -478,6 +478,7 @@ void ModelManager::onLoadPluginTypes(const QString &libraryPath, const QString &
         return;
 
     QProcess *process = new QProcess(this);
+    process->setEnvironment(info.qmlDumpEnvironment.toStringList());
     connect(process, SIGNAL(finished(int)), SLOT(qmlPluginTypeDumpDone(int)));
     connect(process, SIGNAL(error(QProcess::ProcessError)), SLOT(qmlPluginTypeDumpError(QProcess::ProcessError)));
     QStringList args;
diff --git a/src/plugins/qmlprojectmanager/qmlproject.cpp b/src/plugins/qmlprojectmanager/qmlproject.cpp
index e74f2683b29..d928291a7da 100644
--- a/src/plugins/qmlprojectmanager/qmlproject.cpp
+++ b/src/plugins/qmlprojectmanager/qmlproject.cpp
@@ -131,11 +131,16 @@ void QmlProject::refresh(RefreshOptions options)
     QmlJS::ModelManagerInterface::ProjectInfo pinfo(this);
     pinfo.sourceFiles = files();
     pinfo.importPaths = importPaths();
+    pinfo.qmlDumpPath = Qt4ProjectManager::QmlDumpTool::qmlDumpPath(this);
 
-    if (pinfo.qmlDumpPath.isNull()) {
-        pinfo.qmlDumpPath = Qt4ProjectManager::QmlDumpTool::qmlDumpPath(this);
+    if (activeTarget()) {
+        if (const QmlProjectRunConfiguration *runConfig =
+                qobject_cast<QmlProjectRunConfiguration*>(activeTarget()->activeRunConfiguration())) {
+            if (runConfig->qtVersion()) {
+                pinfo.qmlDumpEnvironment = runConfig->qtVersion()->qmlToolsEnvironment();
+            }
+        }
     }
-
     m_modelManager->updateProjectInfo(pinfo);
 }
 
diff --git a/src/plugins/qmlprojectmanager/qmlprojectruncontrol.cpp b/src/plugins/qmlprojectmanager/qmlprojectruncontrol.cpp
index eb0c7b37d01..c84903055ce 100644
--- a/src/plugins/qmlprojectmanager/qmlprojectruncontrol.cpp
+++ b/src/plugins/qmlprojectmanager/qmlprojectruncontrol.cpp
@@ -34,6 +34,8 @@
 #include <coreplugin/modemanager.h>
 #include <projectexplorer/projectexplorerconstants.h>
 #include <projectexplorer/applicationlauncher.h>
+#include <projectexplorer/toolchain.h>
+#include <qt4projectmanager/qtversionmanager.h>
 #include <utils/environment.h>
 #include <utils/qtcassert.h>
 
@@ -62,9 +64,8 @@ namespace Internal {
 QmlRunControl::QmlRunControl(QmlProjectRunConfiguration *runConfiguration, QString mode)
     : RunControl(runConfiguration, mode)
 {
-    Utils::Environment environment = Utils::Environment::systemEnvironment();
-
-    m_applicationLauncher.setEnvironment(environment.toStringList());
+    if (Qt4ProjectManager::QtVersion *qtVersion = runConfiguration->qtVersion())
+        m_applicationLauncher.setEnvironment(qtVersion->qmlToolsEnvironment().toStringList());
     m_applicationLauncher.setWorkingDirectory(runConfiguration->workingDirectory());
 
     if (mode == ProjectExplorer::Constants::RUNMODE) {
@@ -195,7 +196,6 @@ QWidget *QmlRunControlFactory::createConfigurationWidget(RunConfiguration *runCo
 
 ProjectExplorer::RunControl *QmlRunControlFactory::createDebugRunControl(QmlProjectRunConfiguration *runConfig)
 {
-    Utils::Environment environment = Utils::Environment::systemEnvironment();
     Debugger::DebuggerStartParameters params;
     params.startMode = Debugger::StartInternal;
     params.executable = runConfig->observerPath();
@@ -204,7 +204,8 @@ ProjectExplorer::RunControl *QmlRunControlFactory::createDebugRunControl(QmlProj
     params.processArgs = runConfig->viewerArguments();
     params.processArgs.append(QLatin1String("-qmljsdebugger=port:") + QString::number(runConfig->qmlDebugServerPort()));
     params.workingDirectory = runConfig->workingDirectory();
-    params.environment = environment.toStringList();
+    if (Qt4ProjectManager::QtVersion *qtVersion = runConfig->qtVersion())
+        params.environment = qtVersion->qmlToolsEnvironment().toStringList();
     params.displayName = runConfig->displayName();
 
     if (params.executable.isEmpty()) {
diff --git a/src/plugins/qt4projectmanager/qmldumptool.cpp b/src/plugins/qt4projectmanager/qmldumptool.cpp
index 442598850f9..13e6486c410 100644
--- a/src/plugins/qt4projectmanager/qmldumptool.cpp
+++ b/src/plugins/qt4projectmanager/qmldumptool.cpp
@@ -112,10 +112,9 @@ public slots:
 
         foreach (ProjectExplorer::Project *project, m_projectsToUpdate) {
             QmlJS::ModelManagerInterface::ProjectInfo projectInfo = modelManager->projectInfo(project);
-            if (projectInfo.qmlDumpPath.isEmpty()) {
-                projectInfo.qmlDumpPath = qmldumpPath;
-                modelManager->updateProjectInfo(projectInfo);
-            }
+            projectInfo.qmlDumpPath = qmldumpPath;
+            projectInfo.qmlDumpEnvironment = m_version.qmlToolsEnvironment();
+            modelManager->updateProjectInfo(projectInfo);
         }
     }
 
diff --git a/src/plugins/qt4projectmanager/qt4project.cpp b/src/plugins/qt4projectmanager/qt4project.cpp
index af546f26ba1..a233fb639fc 100644
--- a/src/plugins/qt4projectmanager/qt4project.cpp
+++ b/src/plugins/qt4projectmanager/qt4project.cpp
@@ -597,14 +597,12 @@ void Qt4Project::updateQmlJSCodeModel()
             const QString qtVersionImportPath = qtVersion->versionInfo().value("QT_INSTALL_IMPORTS");
             if (!qtVersionImportPath.isEmpty())
                 projectInfo.importPaths += qtVersionImportPath;
+            projectInfo.qmlDumpEnvironment = qtVersion->qmlToolsEnvironment();
+            projectInfo.qmlDumpPath = QmlDumpTool::qmlDumpPath(this);
         }
     }
     projectInfo.importPaths.removeDuplicates();
 
-    if (projectInfo.qmlDumpPath.isNull()) {
-        projectInfo.qmlDumpPath = QmlDumpTool::qmlDumpPath(this);
-    }
-
     modelManager->updateProjectInfo(projectInfo);
 }
 
diff --git a/src/plugins/qt4projectmanager/qtversionmanager.cpp b/src/plugins/qt4projectmanager/qtversionmanager.cpp
index 35622017df9..9b71b4cd0db 100644
--- a/src/plugins/qt4projectmanager/qtversionmanager.cpp
+++ b/src/plugins/qt4projectmanager/qtversionmanager.cpp
@@ -1638,6 +1638,19 @@ bool QtVersion::hasQmlObserver() const
     return m_hasQmlObserver;
 }
 
+Utils::Environment QtVersion::qmlToolsEnvironment() const
+{
+    Utils::Environment environment = Utils::Environment::systemEnvironment();
+    addToEnvironment(environment);
+
+    // add preferred toolchain, as that is how the tools are built, compare QtVersion::buildDebuggingHelperLibrary
+    QList<QSharedPointer<ProjectExplorer::ToolChain> > alltc = toolChains();
+    if (!alltc.isEmpty())
+        alltc.first().data()->addToEnvironment(environment);
+
+    return environment;
+}
+
 QString QtVersion::debuggingHelperLibrary() const
 {
     QString qtInstallData = versionInfo().value("QT_INSTALL_DATA");
diff --git a/src/plugins/qt4projectmanager/qtversionmanager.h b/src/plugins/qt4projectmanager/qtversionmanager.h
index e33e5bd3927..7f584744c70 100644
--- a/src/plugins/qt4projectmanager/qtversionmanager.h
+++ b/src/plugins/qt4projectmanager/qtversionmanager.h
@@ -119,6 +119,7 @@ public:
 
     bool hasQmlDump() const;
     bool hasQmlObserver() const;
+    Utils::Environment qmlToolsEnvironment() const;
 
     // Builds a debugging library
     // returns the output of the commands
-- 
GitLab