1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of Qt Creator.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
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
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "maemopackagecreationstep.h"
44 #include "maemoconstants.h"
45 #include "maemopackagecreationwidget.h"
46 #include "maemopackagecontents.h"
47 #include "maemotoolchain.h"
48 #include "profilewrapper.h"
50 #include <projectexplorer/projectexplorerconstants.h>
51 #include <qt4buildconfiguration.h>
52 #include <qt4project.h>
53 #include <qt4target.h>
55 #include <QtCore/QDir>
56 #include <QtCore/QFile>
57 #include <QtCore/QFileInfo>
58 #include <QtCore/QProcess>
59 #include <QtCore/QProcessEnvironment>
60 #include <QtCore/QStringBuilder>
61 #include <QtGui/QWidget>
64 const QLatin1String PackagingEnabledKey("Packaging Enabled");
65 const QLatin1String DefaultVersionNumber("0.0.1");
66 const QLatin1String VersionNumberKey("Version Number");
69 using namespace ProjectExplorer::Constants;
70 using ProjectExplorer::BuildConfiguration;
71 using ProjectExplorer::BuildStepConfigWidget;
72 using ProjectExplorer::Task;
74 namespace Qt4ProjectManager {
77 MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig)
78 : ProjectExplorer::BuildStep(buildConfig, CreatePackageId),
79 m_packageContents(new MaemoPackageContents(this)),
80 m_packagingEnabled(true),
81 m_versionString(DefaultVersionNumber)
85 MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig,
86 MaemoPackageCreationStep *other)
87 : BuildStep(buildConfig, other),
88 m_packageContents(new MaemoPackageContents(this)),
89 m_packagingEnabled(other->m_packagingEnabled),
90 m_versionString(other->m_versionString)
94 MaemoPackageCreationStep::~MaemoPackageCreationStep() {}
96 bool MaemoPackageCreationStep::init()
101 QVariantMap MaemoPackageCreationStep::toMap() const
103 QVariantMap map(ProjectExplorer::BuildStep::toMap());
104 map.insert(PackagingEnabledKey, m_packagingEnabled);
105 map.insert(VersionNumberKey, m_versionString);
109 bool MaemoPackageCreationStep::fromMap(const QVariantMap &map)
111 m_packagingEnabled = map.value(PackagingEnabledKey, true).toBool();
112 m_versionString = map.value(VersionNumberKey, DefaultVersionNumber).toString();
113 return ProjectExplorer::BuildStep::fromMap(map);
116 void MaemoPackageCreationStep::run(QFutureInterface<bool> &fi)
118 fi.reportResult(m_packagingEnabled ? createPackage() : true);
121 BuildStepConfigWidget *MaemoPackageCreationStep::createConfigWidget()
123 return new MaemoPackageCreationWidget(this);
126 bool MaemoPackageCreationStep::createPackage()
128 if (!packagingNeeded())
131 QTextCharFormat textCharFormat;
132 emit addOutput(tr("Creating package file ..."), textCharFormat);
133 QFile configFile(targetRoot() % QLatin1String("/config.sh"));
134 if (!configFile.open(QIODevice::ReadOnly)) {
135 raiseError(tr("Cannot open MADDE config file '%1'.")
136 .arg(nativePath(configFile)));
140 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
141 const QString &path = QDir::toNativeSeparators(maddeRoot() + QLatin1Char('/'));
143 const QLatin1String key("PATH");
144 QString colon = QLatin1String(":");
146 colon = QLatin1String(";");
147 env.insert(key, path % QLatin1String("bin") % colon % env.value(key));
149 env.insert(key, targetRoot() % "/bin" % colon % env.value(key));
150 env.insert(key, path % QLatin1String("madbin") % colon % env.value(key));
151 env.insert(QLatin1String("PERL5LIB"), path % QLatin1String("madlib/perl5"));
153 const QString buildDir = buildDirectory();
154 env.insert(QLatin1String("PWD"), buildDir);
156 const QRegExp envPattern(QLatin1String("([^=]+)=[\"']?([^;\"']+)[\"']? ;.*"));
159 line = configFile.readLine(200);
160 if (envPattern.exactMatch(line))
161 env.insert(envPattern.cap(1), envPattern.cap(2));
162 } while (!line.isEmpty());
165 buildProc.setProcessEnvironment(env);
166 buildProc.setWorkingDirectory(buildDir);
167 buildProc.start("cd " + buildDir);
168 buildProc.waitForFinished();
170 // cache those two since we can change the version number during packaging
171 // and might fail later to modify, copy, remove etc. the generated package
172 const QString version = versionString();
173 const QString pkgFilePath = packageFilePath();
175 if (!QFileInfo(buildDir + QLatin1String("/debian")).exists()) {
176 const QString command = QLatin1String("dh_make -s -n -p ")
177 % executableFileName().toLower() % QLatin1Char('_') % versionString();
178 if (!runCommand(buildProc, command))
181 QFile rulesFile(buildDir + QLatin1String("/debian/rules"));
182 if (!rulesFile.open(QIODevice::ReadWrite)) {
183 raiseError(tr("Packaging Error: Cannot open file '%1'.")
184 .arg(nativePath(rulesFile)));
188 QByteArray rulesContents = rulesFile.readAll();
189 rulesContents.replace("DESTDIR", "INSTALL_ROOT");
191 // Would be the right solution, but does not work (on Windows),
192 // because dpkg-genchanges doesn't know about it (and can't be told).
193 // rulesContents.replace("dh_builddeb", "dh_builddeb --destdir=.");
196 rulesFile.write(rulesContents);
197 if (rulesFile.error() != QFile::NoError) {
198 raiseError(tr("Packaging Error: Cannot write file '%1'.")
199 .arg(nativePath(rulesFile)));
205 QFile::remove(buildDir + QLatin1String("/debian/files"));
206 QFile changeLog(buildDir + QLatin1String("/debian/changelog"));
207 if (changeLog.open(QIODevice::ReadWrite)) {
208 QString content = QString::fromUtf8(changeLog.readAll());
209 content.replace(QRegExp("\\([a-zA-Z0-9_\\.]+\\)"),
210 QLatin1Char('(') % version % QLatin1Char(')'));
212 changeLog.write(content.toUtf8());
216 if (!runCommand(buildProc, "dpkg-buildpackage -nc -uc -us"))
219 // Workaround for non-working dh_builddeb --destdir=.
220 if (!QDir(buildDir).isRoot()) {
221 const QString packageFileName = QFileInfo(pkgFilePath).fileName();
222 const QString changesFileName = QFileInfo(packageFileName)
223 .completeBaseName() + QLatin1String(".changes");
224 const QString packageSourceDir = buildDir + QLatin1String("/../");
225 const QString packageSourceFilePath
226 = packageSourceDir + packageFileName;
227 const QString changesSourceFilePath
228 = packageSourceDir + changesFileName;
229 const QString changesTargetFilePath
230 = buildDir + QLatin1Char('/') + changesFileName;
231 QFile::remove(pkgFilePath);
232 QFile::remove(changesTargetFilePath);
233 if (!QFile::rename(packageSourceFilePath, pkgFilePath)
234 || !QFile::rename(changesSourceFilePath, changesTargetFilePath)) {
235 raiseError(tr("Packaging failed."),
236 tr("Could not move package files from %1 to %2.")
237 .arg(packageSourceDir, buildDir));
242 emit addOutput(tr("Package created."), textCharFormat);
243 m_packageContents->setUnModified();
247 bool MaemoPackageCreationStep::runCommand(QProcess &proc, const QString &command)
249 QTextCharFormat textCharFormat;
250 emit addOutput(tr("Package Creation: Running command '%1'.").arg(command), textCharFormat);
253 perl = maddeRoot() + QLatin1String("/bin/perl.exe ");
255 proc.start(perl + maddeRoot() % QLatin1String("/madbin/") % command);
256 if (!proc.waitForStarted()) {
257 raiseError(tr("Packaging failed."),
258 tr("Packaging error: Could not start command '%1'. Reason: %2")
259 .arg(command).arg(proc.errorString()));
262 proc.write("\n"); // For dh_make
263 proc.waitForFinished(-1);
264 if (proc.error() != QProcess::UnknownError || proc.exitCode() != 0) {
265 QString mainMessage = tr("Packaging Error: Command '%1' failed.")
267 if (proc.error() != QProcess::UnknownError)
268 mainMessage += tr(" Reason: %1").arg(proc.errorString());
270 mainMessage += tr("Exit code: %1").arg(proc.exitCode());
271 raiseError(mainMessage, mainMessage + QLatin1Char('\n')
272 + tr("Output was: ") + proc.readAllStandardError()
273 + QLatin1Char('\n') + proc.readAllStandardOutput());
279 const Qt4BuildConfiguration *MaemoPackageCreationStep::qt4BuildConfiguration() const
281 return static_cast<Qt4BuildConfiguration *>(buildConfiguration());
284 QString MaemoPackageCreationStep::localExecutableFilePath() const
286 const TargetInformation &ti = qt4BuildConfiguration()->qt4Target()
287 ->qt4Project()->rootProjectNode()->targetInformation();
290 return QDir::toNativeSeparators(QDir::cleanPath(ti.workingDir
291 + QLatin1Char('/') + executableFileName()));
294 QString MaemoPackageCreationStep::buildDirectory() const
296 const TargetInformation &ti = qt4BuildConfiguration()->qt4Target()
297 ->qt4Project()->rootProjectNode()->targetInformation();
298 return ti.valid ? ti.buildDir : QString();
301 QString MaemoPackageCreationStep::executableFileName() const
303 const Qt4Project * const project
304 = qt4BuildConfiguration()->qt4Target()->qt4Project();
305 const TargetInformation &ti
306 = project->rootProjectNode()->targetInformation();
310 return project->rootProjectNode()->projectType() == LibraryTemplate
311 ? QLatin1String("lib") + ti.target + QLatin1String(".so")
315 const MaemoToolChain *MaemoPackageCreationStep::maemoToolChain() const
317 return static_cast<MaemoToolChain *>(qt4BuildConfiguration()->toolChain());
320 QString MaemoPackageCreationStep::maddeRoot() const
322 return maemoToolChain()->maddeRoot();
325 QString MaemoPackageCreationStep::targetRoot() const
327 return maemoToolChain()->targetRoot();
330 bool MaemoPackageCreationStep::packagingNeeded() const
332 QFileInfo packageInfo(packageFilePath());
333 if (!packageInfo.exists() || m_packageContents->isModified())
336 for (int i = 0; i < m_packageContents->rowCount(); ++i) {
337 if (packageInfo.lastModified()
338 <= QFileInfo(m_packageContents->deployableAt(i).localFilePath)
346 QString MaemoPackageCreationStep::packageFilePath() const
348 return buildDirectory() % QDir::separator() % executableFileName().toLower()
349 % QLatin1Char('_') % versionString() % QLatin1String("_armel.deb");
352 QString MaemoPackageCreationStep::versionString() const
354 return m_versionString;
357 void MaemoPackageCreationStep::setVersionString(const QString &version)
359 m_versionString = version;
362 QString MaemoPackageCreationStep::nativePath(const QFile &file) const
364 return QDir::toNativeSeparators(QFileInfo(file).filePath());
367 void MaemoPackageCreationStep::raiseError(const QString &shortMsg,
368 const QString &detailedMsg)
370 QTextCharFormat textCharFormat;
371 emit addOutput(detailedMsg.isNull() ? shortMsg : detailedMsg, textCharFormat);
372 emit addTask(Task(Task::Error, shortMsg, QString(), -1,
373 TASK_CATEGORY_BUILDSYSTEM));
376 QSharedPointer<ProFileWrapper> MaemoPackageCreationStep::proFileWrapper() const
378 if (!m_proFileWrapper) {
379 const Qt4ProFileNode * const proFileNode = qt4BuildConfiguration()
380 ->qt4Target()->qt4Project()->rootProjectNode();
381 m_proFileWrapper = QSharedPointer<ProFileWrapper>(
382 new ProFileWrapper(proFileNode->path()));
385 return m_proFileWrapper;
388 const QLatin1String MaemoPackageCreationStep::CreatePackageId("Qt4ProjectManager.MaemoPackageCreationStep");
390 } // namespace Internal
391 } // namespace Qt4ProjectManager