1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "qmldumptool.h"
34 #include "qt4project.h"
35 #include "qt4projectmanagerconstants.h"
36 #include "qtversionmanager.h"
37 #include "debugginghelperbuildtask.h"
38 #include <coreplugin/icore.h>
39 #include <coreplugin/progressmanager/progressmanager.h>
41 #include <projectexplorer/project.h>
42 #include <projectexplorer/projectexplorer.h>
43 #include <projectexplorer/runconfiguration.h>
44 #include <qtconcurrent/runextensions.h>
45 #include <qmljs/qmljsmodelmanagerinterface.h>
46 #include <utils/qtcassert.h>
47 #include <QtGui/QDesktopServices>
48 #include <QtCore/QCoreApplication>
49 #include <QtCore/QDir>
50 #include <QtCore/QDebug>
51 #include <QtCore/QHash>
55 using namespace Qt4ProjectManager;
56 using Qt4ProjectManager::Internal::DebuggingHelperBuildTask;
59 class QmlDumpBuildTask;
61 typedef QHash<int, QmlDumpBuildTask *> QmlDumpByVersion;
62 Q_GLOBAL_STATIC(QmlDumpByVersion, qmlDumpBuilds)
64 // A task suitable to be run by QtConcurrent to build qmldump.
65 class QmlDumpBuildTask : public QObject {
66 Q_DISABLE_COPY(QmlDumpBuildTask)
69 explicit QmlDumpBuildTask(QtVersion *version)
70 : m_buildTask(new DebuggingHelperBuildTask(version, DebuggingHelperBuildTask::QmlDump))
73 qmlDumpBuilds()->insert(version->uniqueId(), this);
75 connect(m_buildTask, SIGNAL(finished(int,QString,DebuggingHelperBuildTask::Tools)),
76 this, SLOT(finish(int,QString,DebuggingHelperBuildTask::Tools)),
77 Qt::QueuedConnection);
80 void run(QFutureInterface<void> &future)
82 m_buildTask->run(future);
85 void updateProjectWhenDone(QPointer<ProjectExplorer::Project> project, bool preferDebug)
87 foreach (const ProjectToUpdate &update, m_projectsToUpdate) {
88 if (update.project == project)
92 ProjectToUpdate update;
93 update.project = project;
94 update.preferDebug = preferDebug;
95 m_projectsToUpdate += update;
98 bool hasFailed() const
104 void finish(int qtId, const QString &output, DebuggingHelperBuildTask::Tools tools)
106 QtVersion *version = QtVersionManager::instance()->version(qtId);
108 QTC_ASSERT(tools == DebuggingHelperBuildTask::QmlDump, return);
109 QString errorMessage;
112 errorMessage = QString::fromLatin1("Qt version became invalid");
114 version->invalidateCache();
116 if (!version->hasQmlDump()) {
118 errorMessage = QString::fromLatin1("Could not build QML plugin dumping helper for %1\n"
120 arg(version->displayName(), output);
125 qWarning("%s", qPrintable(errorMessage));
128 // update qmldump path for all the project
129 QmlJS::ModelManagerInterface *modelManager = QmlJS::ModelManagerInterface::instance();
133 foreach (const ProjectToUpdate &update, m_projectsToUpdate) {
136 QmlJS::ModelManagerInterface::ProjectInfo projectInfo = modelManager->projectInfo(update.project);
137 projectInfo.qmlDumpPath = version->qmlDumpTool(update.preferDebug);
138 if (projectInfo.qmlDumpPath.isEmpty())
139 projectInfo.qmlDumpPath = version->qmlDumpTool(!update.preferDebug);
140 projectInfo.qmlDumpEnvironment = version->qmlToolsEnvironment();
141 modelManager->updateProjectInfo(projectInfo);
145 qmlDumpBuilds()->remove(qtId);
150 class ProjectToUpdate {
152 QPointer<ProjectExplorer::Project> project;
156 QList<ProjectToUpdate> m_projectsToUpdate;
157 Internal::DebuggingHelperBuildTask *m_buildTask; // deletes itself after run()
160 } // end of anonymous namespace
163 namespace Qt4ProjectManager {
165 static inline QStringList validBinaryFilenames(bool debugBuild)
167 QStringList list = QStringList()
168 << QLatin1String("qmldump.exe")
169 << QLatin1String("qmldump")
170 << QLatin1String("qmldump.app/Contents/MacOS/qmldump");
172 list.prepend(QLatin1String("debug/qmldump.exe"));
174 list.prepend(QLatin1String("release/qmldump.exe"));
178 bool QmlDumpTool::canBuild(const QtVersion *qtVersion)
180 const QString installHeaders = qtVersion->versionInfo().value("QT_INSTALL_HEADERS");
181 const QString header = installHeaders + QLatin1String("/QtDeclarative/private/qdeclarativemetatype_p.h");
182 return (qtVersion->supportsTargetId(Constants::DESKTOP_TARGET_ID)
183 || (qtVersion->supportsTargetId(Constants::QT_SIMULATOR_TARGET_ID)
184 && (qtVersion->qtVersion() > QtVersionNumber(4, 7, 1))))
185 && QFile::exists(header);
188 static QtVersion *qtVersionForProject(ProjectExplorer::Project *project)
190 if (project && project->id() == Qt4ProjectManager::Constants::QT4PROJECT_ID) {
191 Qt4Project *qt4Project = static_cast<Qt4Project*>(project);
192 if (qt4Project && qt4Project->activeTarget()
193 && qt4Project->activeTarget()->activeBuildConfiguration()) {
194 QtVersion *version = qt4Project->activeTarget()->activeBuildConfiguration()->qtVersion();
195 if (version->isValid())
201 if (project && project->id() == QLatin1String("QmlProjectManager.QmlProject")) {
202 // We cannot access the QmlProject interfaces here, therefore use the metatype system
203 if (!project->activeTarget() || !project->activeTarget()->activeRunConfiguration())
205 QVariant variant = project->activeTarget()->activeRunConfiguration()->property("qtVersionId");
206 QTC_ASSERT(variant.isValid() && variant.canConvert(QVariant::Int), return 0);
207 QtVersion *version = QtVersionManager::instance()->version(variant.toInt());
208 if (version && version->isValid())
213 // else, find any desktop or simulator Qt version that has qmldump, or
214 // - if there isn't any - one that could build it
215 QtVersion *canBuildQmlDump = 0;
216 QtVersionManager *qtVersions = QtVersionManager::instance();
217 foreach (QtVersion *version, qtVersions->validVersions()) {
218 if (version->supportsTargetId(Constants::DESKTOP_TARGET_ID)
219 || version->supportsTargetId(Constants::QT_SIMULATOR_TARGET_ID)) {
220 if (version->hasQmlDump())
223 if (!canBuildQmlDump && QmlDumpTool::canBuild(version)) {
224 canBuildQmlDump = version;
229 return canBuildQmlDump;
232 QString QmlDumpTool::toolForProject(ProjectExplorer::Project *project, bool debugDump)
234 QtVersion *version = qtVersionForProject(project);
236 QString qtInstallData = version->versionInfo().value("QT_INSTALL_DATA");
237 QString toolPath = toolByInstallData(qtInstallData, debugDump);
244 static QString sourcePath()
246 return Core::ICore::instance()->resourcePath() + QLatin1String("/qml/qmldump/");
249 static QStringList sourceFileNames()
252 files << QLatin1String("main.cpp") << QLatin1String("qmldump.pro")
253 << QLatin1String("qmlstreamwriter.cpp") << QLatin1String("qmlstreamwriter.h")
254 << QLatin1String("LICENSE.LGPL") << QLatin1String("LGPL_EXCEPTION.TXT");
256 files << QLatin1String("Info.plist");
261 QString QmlDumpTool::toolByInstallData(const QString &qtInstallData, bool debugDump)
263 if (!Core::ICore::instance())
266 const QString mainFilename = Core::ICore::instance()->resourcePath()
267 + QLatin1String("/qml/qmldump/main.cpp");
268 const QStringList directories = installDirectories(qtInstallData);
269 const QStringList binFilenames = validBinaryFilenames(debugDump);
271 return byInstallDataHelper(sourcePath(), sourceFileNames(), directories, binFilenames);
274 QStringList QmlDumpTool::locationsByInstallData(const QString &qtInstallData, bool debugDump)
278 const QStringList binFilenames = validBinaryFilenames(debugDump);
279 foreach(const QString &directory, installDirectories(qtInstallData)) {
280 if (getHelperFileInfoFor(binFilenames, directory, &fileInfo))
281 result << fileInfo.filePath();
286 bool QmlDumpTool::build(const QString &directory, const QString &makeCommand,
287 const QString &qmakeCommand, const QString &mkspec,
288 const Utils::Environment &env, const QString &targetMode,
289 const QStringList &qmakeArguments, QString *output, QString *errorMessage)
291 return buildHelper(QCoreApplication::translate("Qt4ProjectManager::QmlDumpTool", "qmldump"), QLatin1String("qmldump.pro"),
292 directory, makeCommand, qmakeCommand, mkspec, env, targetMode,
293 qmakeArguments, output, errorMessage);
296 QString QmlDumpTool::copy(const QString &qtInstallData, QString *errorMessage)
298 const QStringList directories = QmlDumpTool::installDirectories(qtInstallData);
300 // Try to find a writeable directory.
301 foreach(const QString &directory, directories) {
302 if (copyFiles(sourcePath(), sourceFileNames(), directory, errorMessage)) {
306 *errorMessage = QCoreApplication::translate("ProjectExplorer::QmlDumpTool",
307 "qmldump could not be built in any of the directories:\n- %1\n\nReason: %2")
308 .arg(directories.join(QLatin1String("\n- ")), *errorMessage);
312 QStringList QmlDumpTool::installDirectories(const QString &qtInstallData)
314 const QChar slash = QLatin1Char('/');
315 const uint hash = qHash(qtInstallData);
316 QStringList directories;
318 << (qtInstallData + QLatin1String("/qtc-qmldump/"))
319 << QDir::cleanPath((QCoreApplication::applicationDirPath() + QLatin1String("/../qtc-qmldump/") + QString::number(hash))) + slash
320 << (QDesktopServices::storageLocation(QDesktopServices::DataLocation) + QLatin1String("/qtc-qmldump/") + QString::number(hash)) + slash;
324 void QmlDumpTool::pathAndEnvironment(ProjectExplorer::Project *project, bool preferDebug,
325 QString *dumperPath, Utils::Environment *env)
329 QtVersion *version = qtVersionForProject(project);
330 if (version && !version->hasQmlDump() && QmlDumpTool::canBuild(version)) {
331 QmlDumpBuildTask *qmlDumpBuildTask = qmlDumpBuilds()->value(version->uniqueId());
332 if (qmlDumpBuildTask) {
333 if (!qmlDumpBuildTask->hasFailed())
334 qmlDumpBuildTask->updateProjectWhenDone(project, preferDebug);
336 QmlDumpBuildTask *buildTask = new QmlDumpBuildTask(version);
337 buildTask->updateProjectWhenDone(project, preferDebug);
338 QFuture<void> task = QtConcurrent::run(&QmlDumpBuildTask::run, buildTask);
339 const QString taskName = QmlDumpBuildTask::tr("Building helper");
340 Core::ICore::instance()->progressManager()->addTask(task, taskName,
341 QLatin1String("Qt4ProjectManager::BuildHelpers"));
346 path = Qt4ProjectManager::QmlDumpTool::toolForProject(project, preferDebug);
348 path = Qt4ProjectManager::QmlDumpTool::toolForProject(project, !preferDebug);
350 if (!path.isEmpty()) {
351 QFileInfo qmldumpFileInfo(path);
352 if (!qmldumpFileInfo.exists()) {
353 qWarning() << "QmlDumpTool::qmlDumpPath: qmldump executable does not exist at" << path;
355 } else if (!qmldumpFileInfo.isFile()) {
356 qWarning() << "QmlDumpTool::qmlDumpPath: " << path << " is not a file";
361 if (!path.isEmpty() && version && dumperPath && env) {
363 *env = version->qmlToolsEnvironment();
367 } // namespace Qt4ProjectManager
369 #include "qmldumptool.moc"