1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qmldumptool.h"
35 #include "qt4project.h"
36 #include "qt4projectmanagerconstants.h"
37 #include "qtversionmanager.h"
38 #include "debugginghelperbuildtask.h"
39 #include <coreplugin/icore.h>
40 #include <coreplugin/progressmanager/progressmanager.h>
42 #include <projectexplorer/project.h>
43 #include <projectexplorer/projectexplorer.h>
44 #include <projectexplorer/runconfiguration.h>
45 #include <qtconcurrent/runextensions.h>
46 #include <qmljs/qmljsmodelmanagerinterface.h>
47 #include <utils/qtcassert.h>
48 #include <QtGui/QDesktopServices>
49 #include <QtCore/QCoreApplication>
50 #include <QtCore/QDir>
51 #include <QtCore/QDebug>
52 #include <QtCore/QHash>
56 using namespace Qt4ProjectManager;
57 using Qt4ProjectManager::Internal::DebuggingHelperBuildTask;
60 class QmlDumpBuildTask;
62 typedef QHash<int, QmlDumpBuildTask *> QmlDumpByVersion;
63 Q_GLOBAL_STATIC(QmlDumpByVersion, qmlDumpBuilds)
65 // A task suitable to be run by QtConcurrent to build qmldump.
66 class QmlDumpBuildTask : public QObject {
67 Q_DISABLE_COPY(QmlDumpBuildTask)
70 explicit QmlDumpBuildTask(QtVersion *version)
71 : m_buildTask(new DebuggingHelperBuildTask(version, DebuggingHelperBuildTask::QmlDump))
74 qmlDumpBuilds()->insert(version->uniqueId(), this);
76 connect(m_buildTask, SIGNAL(finished(int,QString,DebuggingHelperBuildTask::Tools)),
77 this, SLOT(finish(int,QString,DebuggingHelperBuildTask::Tools)),
78 Qt::QueuedConnection);
81 void run(QFutureInterface<void> &future)
83 m_buildTask->run(future);
86 void updateProjectWhenDone(QPointer<ProjectExplorer::Project> project, bool preferDebug)
88 foreach (const ProjectToUpdate &update, m_projectsToUpdate) {
89 if (update.project == project)
93 ProjectToUpdate update;
94 update.project = project;
95 update.preferDebug = preferDebug;
96 m_projectsToUpdate += update;
99 bool hasFailed() const
105 void finish(int qtId, const QString &output, DebuggingHelperBuildTask::Tools tools)
107 QtVersion *version = QtVersionManager::instance()->version(qtId);
109 QTC_ASSERT(tools == DebuggingHelperBuildTask::QmlDump, return);
110 QString errorMessage;
113 errorMessage = QString::fromLatin1("Qt version became invalid");
115 version->invalidateCache();
117 if (!version->hasQmlDump()) {
119 errorMessage = QString::fromLatin1("Could not build QML plugin dumping helper for %1\n"
121 arg(version->displayName(), output);
126 qWarning("%s", qPrintable(errorMessage));
129 // update qmldump path for all the project
130 QmlJS::ModelManagerInterface *modelManager = QmlJS::ModelManagerInterface::instance();
134 foreach (const ProjectToUpdate &update, m_projectsToUpdate) {
137 QmlJS::ModelManagerInterface::ProjectInfo projectInfo = modelManager->projectInfo(update.project);
138 projectInfo.qmlDumpPath = version->qmlDumpTool(update.preferDebug);
139 if (projectInfo.qmlDumpPath.isEmpty())
140 projectInfo.qmlDumpPath = version->qmlDumpTool(!update.preferDebug);
141 projectInfo.qmlDumpEnvironment = version->qmlToolsEnvironment();
142 modelManager->updateProjectInfo(projectInfo);
146 qmlDumpBuilds()->remove(qtId);
151 class ProjectToUpdate {
153 QPointer<ProjectExplorer::Project> project;
157 QList<ProjectToUpdate> m_projectsToUpdate;
158 Internal::DebuggingHelperBuildTask *m_buildTask; // deletes itself after run()
161 } // end of anonymous namespace
164 namespace Qt4ProjectManager {
166 static inline QStringList validBinaryFilenames(bool debugBuild)
168 QStringList list = QStringList()
169 << QLatin1String("qmldump.exe")
170 << QLatin1String("qmldump")
171 << QLatin1String("qmldump.app/Contents/MacOS/qmldump");
173 list.prepend(QLatin1String("debug/qmldump.exe"));
175 list.prepend(QLatin1String("release/qmldump.exe"));
179 bool QmlDumpTool::canBuild(const QtVersion *qtVersion)
181 const QString installHeaders = qtVersion->versionInfo().value("QT_INSTALL_HEADERS");
182 const QString header = installHeaders + QLatin1String("/QtDeclarative/private/qdeclarativemetatype_p.h");
183 return (qtVersion->supportsTargetId(Constants::DESKTOP_TARGET_ID)
184 || (qtVersion->supportsTargetId(Constants::QT_SIMULATOR_TARGET_ID)
185 && (qtVersion->qtVersion() > QtVersionNumber(4, 7, 1))))
186 && QFile::exists(header);
189 static QtVersion *qtVersionForProject(ProjectExplorer::Project *project)
191 if (project && project->id() == Qt4ProjectManager::Constants::QT4PROJECT_ID) {
192 Qt4Project *qt4Project = static_cast<Qt4Project*>(project);
193 if (qt4Project && qt4Project->activeTarget()
194 && qt4Project->activeTarget()->activeBuildConfiguration()) {
195 QtVersion *version = qt4Project->activeTarget()->activeBuildConfiguration()->qtVersion();
196 if (version->isValid())
202 if (project && project->id() == QLatin1String("QmlProjectManager.QmlProject")) {
203 // We cannot access the QmlProject interfaces here, therefore use the metatype system
204 if (!project->activeTarget() || !project->activeTarget()->activeRunConfiguration())
206 QVariant variant = project->activeTarget()->activeRunConfiguration()->property("qtVersionId");
207 QTC_ASSERT(variant.isValid() && variant.canConvert(QVariant::Int), return 0);
208 QtVersion *version = QtVersionManager::instance()->version(variant.toInt());
209 if (version && version->isValid())
214 // else, find any desktop or simulator Qt version that has qmldump, or
215 // - if there isn't any - one that could build it
216 QtVersion *canBuildQmlDump = 0;
217 QtVersionManager *qtVersions = QtVersionManager::instance();
218 foreach (QtVersion *version, qtVersions->validVersions()) {
219 if (version->supportsTargetId(Constants::DESKTOP_TARGET_ID)
220 || version->supportsTargetId(Constants::QT_SIMULATOR_TARGET_ID)) {
221 if (version->hasQmlDump())
224 if (!canBuildQmlDump && QmlDumpTool::canBuild(version)) {
225 canBuildQmlDump = version;
230 return canBuildQmlDump;
233 QString QmlDumpTool::toolForProject(ProjectExplorer::Project *project, bool debugDump)
235 QtVersion *version = qtVersionForProject(project);
237 QString qtInstallData = version->versionInfo().value("QT_INSTALL_DATA");
238 QString toolPath = toolByInstallData(qtInstallData, debugDump);
245 static QString sourcePath()
247 return Core::ICore::instance()->resourcePath() + QLatin1String("/qml/qmldump/");
250 static QStringList sourceFileNames()
253 files << QLatin1String("main.cpp") << QLatin1String("qmldump.pro")
254 << QLatin1String("qmlstreamwriter.cpp") << QLatin1String("qmlstreamwriter.h")
255 << QLatin1String("LICENSE.LGPL") << QLatin1String("LGPL_EXCEPTION.TXT");
257 files << QLatin1String("Info.plist");
262 QString QmlDumpTool::toolByInstallData(const QString &qtInstallData, bool debugDump)
264 if (!Core::ICore::instance())
267 const QString mainFilename = Core::ICore::instance()->resourcePath()
268 + QLatin1String("/qml/qmldump/main.cpp");
269 const QStringList directories = installDirectories(qtInstallData);
270 const QStringList binFilenames = validBinaryFilenames(debugDump);
272 return byInstallDataHelper(sourcePath(), sourceFileNames(), directories, binFilenames);
275 QStringList QmlDumpTool::locationsByInstallData(const QString &qtInstallData, bool debugDump)
279 const QStringList binFilenames = validBinaryFilenames(debugDump);
280 foreach(const QString &directory, installDirectories(qtInstallData)) {
281 if (getHelperFileInfoFor(binFilenames, directory, &fileInfo))
282 result << fileInfo.filePath();
287 bool QmlDumpTool::build(const QString &directory, const QString &makeCommand,
288 const QString &qmakeCommand, const QString &mkspec,
289 const Utils::Environment &env, const QString &targetMode,
290 const QStringList &qmakeArguments, QString *output, QString *errorMessage)
292 return buildHelper(QCoreApplication::translate("Qt4ProjectManager::QmlDumpTool", "qmldump"), QLatin1String("qmldump.pro"),
293 directory, makeCommand, qmakeCommand, mkspec, env, targetMode,
294 qmakeArguments, output, errorMessage);
297 QString QmlDumpTool::copy(const QString &qtInstallData, QString *errorMessage)
299 const QStringList directories = QmlDumpTool::installDirectories(qtInstallData);
301 // Try to find a writeable directory.
302 foreach(const QString &directory, directories) {
303 if (copyFiles(sourcePath(), sourceFileNames(), directory, errorMessage)) {
307 *errorMessage = QCoreApplication::translate("ProjectExplorer::QmlDumpTool",
308 "qmldump could not be built in any of the directories:\n- %1\n\nReason: %2")
309 .arg(directories.join(QLatin1String("\n- ")), *errorMessage);
313 QStringList QmlDumpTool::installDirectories(const QString &qtInstallData)
315 const QChar slash = QLatin1Char('/');
316 const uint hash = qHash(qtInstallData);
317 QStringList directories;
319 << (qtInstallData + QLatin1String("/qtc-qmldump/"))
320 << QDir::cleanPath((QCoreApplication::applicationDirPath() + QLatin1String("/../qtc-qmldump/") + QString::number(hash))) + slash
321 << (QDesktopServices::storageLocation(QDesktopServices::DataLocation) + QLatin1String("/qtc-qmldump/") + QString::number(hash)) + slash;
325 void QmlDumpTool::pathAndEnvironment(ProjectExplorer::Project *project, bool preferDebug,
326 QString *dumperPath, Utils::Environment *env)
330 QtVersion *version = qtVersionForProject(project);
331 if (version && !version->hasQmlDump() && QmlDumpTool::canBuild(version)) {
332 QmlDumpBuildTask *qmlDumpBuildTask = qmlDumpBuilds()->value(version->uniqueId());
333 if (qmlDumpBuildTask) {
334 if (!qmlDumpBuildTask->hasFailed())
335 qmlDumpBuildTask->updateProjectWhenDone(project, preferDebug);
337 QmlDumpBuildTask *buildTask = new QmlDumpBuildTask(version);
338 buildTask->updateProjectWhenDone(project, preferDebug);
339 QFuture<void> task = QtConcurrent::run(&QmlDumpBuildTask::run, buildTask);
340 const QString taskName = QmlDumpBuildTask::tr("Building helper");
341 Core::ICore::instance()->progressManager()->addTask(task, taskName,
342 QLatin1String("Qt4ProjectManager::BuildHelpers"));
347 path = Qt4ProjectManager::QmlDumpTool::toolForProject(project, preferDebug);
349 path = Qt4ProjectManager::QmlDumpTool::toolForProject(project, !preferDebug);
351 if (!path.isEmpty()) {
352 QFileInfo qmldumpFileInfo(path);
353 if (!qmldumpFileInfo.exists()) {
354 qWarning() << "QmlDumpTool::qmlDumpPath: qmldump executable does not exist at" << path;
356 } else if (!qmldumpFileInfo.isFile()) {
357 qWarning() << "QmlDumpTool::qmlDumpPath: " << path << " is not a file";
362 if (!path.isEmpty() && version && dumperPath && env) {
364 *env = version->qmlToolsEnvironment();
368 } // namespace Qt4ProjectManager
370 #include "qmldumptool.moc"