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"
49 #include <projectexplorer/projectexplorerconstants.h>
50 #include <qt4buildconfiguration.h>
51 #include <qt4project.h>
52 #include <qt4target.h>
54 #include <QtCore/QDir>
55 #include <QtCore/QFile>
56 #include <QtCore/QFileInfo>
57 #include <QtCore/QProcess>
58 #include <QtCore/QProcessEnvironment>
59 #include <QtCore/QStringBuilder>
60 #include <QtGui/QWidget>
63 const QLatin1String PackagingEnabledKey("Packaging Enabled");
64 const QLatin1String DefaultVersionNumber("0.0.1");
65 const QLatin1String VersionNumberKey("Version Number");
68 using namespace ProjectExplorer::Constants;
69 using ProjectExplorer::BuildConfiguration;
70 using ProjectExplorer::BuildStepConfigWidget;
71 using ProjectExplorer::Task;
73 namespace Qt4ProjectManager {
76 MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig)
77 : ProjectExplorer::BuildStep(buildConfig, CreatePackageId),
78 m_packageContents(new MaemoPackageContents(this)),
79 m_packagingEnabled(true),
80 m_versionString(DefaultVersionNumber)
84 MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig,
85 MaemoPackageCreationStep *other)
86 : BuildStep(buildConfig, other),
87 m_packageContents(new MaemoPackageContents(this)),
88 m_packagingEnabled(other->m_packagingEnabled),
89 m_versionString(other->m_versionString)
93 MaemoPackageCreationStep::~MaemoPackageCreationStep() {}
95 bool MaemoPackageCreationStep::init()
100 QVariantMap MaemoPackageCreationStep::toMap() const
102 QVariantMap map(ProjectExplorer::BuildStep::toMap());
103 map.insert(PackagingEnabledKey, m_packagingEnabled);
104 map.insert(VersionNumberKey, m_versionString);
105 return map.unite(m_packageContents->toMap());
108 bool MaemoPackageCreationStep::fromMap(const QVariantMap &map)
110 m_packageContents->fromMap(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));
150 env.insert(key, targetRoot() % "/bin" % colon % env.value(key));
151 env.insert(key, path % QLatin1String("madbin") % colon % env.value(key));
153 QString perlLib = QDir::fromNativeSeparators(path % QLatin1String("madlib/perl5"));
155 perlLib = perlLib.remove(QLatin1Char(':'));
156 perlLib = perlLib.prepend(QLatin1Char('/'));
158 env.insert(QLatin1String("PERL5LIB"), perlLib);
160 const QString buildDir = buildDirectory();
161 env.insert(QLatin1String("PWD"), buildDir);
163 const QRegExp envPattern(QLatin1String("([^=]+)=[\"']?([^;\"']+)[\"']? ;.*"));
166 line = configFile.readLine(200);
167 if (envPattern.exactMatch(line))
168 env.insert(envPattern.cap(1), envPattern.cap(2));
169 } while (!line.isEmpty());
172 m_buildProc.reset(new QProcess);
173 connect(m_buildProc.data(), SIGNAL(readyReadStandardOutput()), this,
174 SLOT(handleBuildOutput()));
175 connect(m_buildProc.data(), SIGNAL(readyReadStandardError()), this,
176 SLOT(handleBuildOutput()));
177 m_buildProc->setProcessEnvironment(env);
178 m_buildProc->setWorkingDirectory(buildDir);
179 m_buildProc->start("cd " + buildDir);
180 m_buildProc->waitForFinished();
182 if (!QFileInfo(buildDir + QLatin1String("/debian")).exists()) {
183 const QString command = QLatin1String("dh_make -s -n -p ")
184 % executableFileName().toLower() % QLatin1Char('_') % versionString();
185 if (!runCommand(command))
188 QFile rulesFile(buildDir + QLatin1String("/debian/rules"));
189 if (!rulesFile.open(QIODevice::ReadWrite)) {
190 raiseError(tr("Packaging Error: Cannot open file '%1'.")
191 .arg(nativePath(rulesFile)));
195 QByteArray rulesContents = rulesFile.readAll();
196 rulesContents.replace("DESTDIR", "INSTALL_ROOT");
198 // Would be the right solution, but does not work (on Windows),
199 // because dpkg-genchanges doesn't know about it (and can't be told).
200 // rulesContents.replace("dh_builddeb", "dh_builddeb --destdir=.");
203 rulesFile.write(rulesContents);
204 if (rulesFile.error() != QFile::NoError) {
205 raiseError(tr("Packaging Error: Cannot write file '%1'.")
206 .arg(nativePath(rulesFile)));
212 QFile::remove(buildDir + QLatin1String("/debian/files"));
213 QFile changeLog(buildDir + QLatin1String("/debian/changelog"));
214 if (changeLog.open(QIODevice::ReadWrite)) {
215 QString content = QString::fromUtf8(changeLog.readAll());
216 content.replace(QRegExp("\\([a-zA-Z0-9_\\.]+\\)"),
217 QLatin1Char('(') % versionString() % QLatin1Char(')'));
219 changeLog.write(content.toUtf8());
223 if (!runCommand(QLatin1String("dpkg-buildpackage -nc -uc -us")))
226 // Workaround for non-working dh_builddeb --destdir=.
227 if (!QDir(buildDir).isRoot()) {
228 const QString packageFileName = QFileInfo(packageFilePath()).fileName();
229 const QString changesFileName = QFileInfo(packageFileName)
230 .completeBaseName() + QLatin1String(".changes");
231 const QString packageSourceDir = buildDir + QLatin1String("/../");
232 const QString packageSourceFilePath
233 = packageSourceDir + packageFileName;
234 const QString changesSourceFilePath
235 = packageSourceDir + changesFileName;
236 const QString changesTargetFilePath
237 = buildDir + QLatin1Char('/') + changesFileName;
238 QFile::remove(packageFilePath());
239 QFile::remove(changesTargetFilePath);
240 if (!QFile::rename(packageSourceFilePath, packageFilePath())
241 || !QFile::rename(changesSourceFilePath, changesTargetFilePath)) {
242 raiseError(tr("Packaging failed."),
243 tr("Could not move package files from %1 to %2.")
244 .arg(packageSourceDir, buildDir));
249 emit addOutput(tr("Package created."), textCharFormat);
250 m_packageContents->setUnModified();
254 bool MaemoPackageCreationStep::runCommand(const QString &command)
256 QTextCharFormat textCharFormat;
257 emit addOutput(tr("Package Creation: Running command '%1'.").arg(command), textCharFormat);
260 perl = maddeRoot() + QLatin1String("/bin/perl.exe ");
262 m_buildProc->start(perl + maddeRoot() % QLatin1String("/madbin/") % command);
263 if (!m_buildProc->waitForStarted()) {
264 raiseError(tr("Packaging failed."),
265 tr("Packaging error: Could not start command '%1'. Reason: %2")
266 .arg(command).arg(m_buildProc->errorString()));
269 m_buildProc->write("\n"); // For dh_make
270 m_buildProc->waitForFinished(-1);
271 if (m_buildProc->error() != QProcess::UnknownError || m_buildProc->exitCode() != 0) {
272 QString mainMessage = tr("Packaging Error: Command '%1' failed.")
274 if (m_buildProc->error() != QProcess::UnknownError)
275 mainMessage += tr(" Reason: %1").arg(m_buildProc->errorString());
277 mainMessage += tr("Exit code: %1").arg(m_buildProc->exitCode());
278 raiseError(mainMessage);
284 void MaemoPackageCreationStep::handleBuildOutput()
286 const QByteArray &stdOut = m_buildProc->readAllStandardOutput();
287 QTextCharFormat textCharFormat;
288 if (!stdOut.isEmpty())
289 emit addOutput(QString::fromLocal8Bit(stdOut), textCharFormat);
290 const QByteArray &errorOut = m_buildProc->readAllStandardError();
291 if (!errorOut.isEmpty()) {
292 textCharFormat.setForeground(QBrush(QColor("red")));
293 emit addOutput(QString::fromLocal8Bit(errorOut), textCharFormat);
297 const Qt4BuildConfiguration *MaemoPackageCreationStep::qt4BuildConfiguration() const
299 return static_cast<Qt4BuildConfiguration *>(buildConfiguration());
302 QString MaemoPackageCreationStep::localExecutableFilePath() const
304 const TargetInformation &ti = qt4BuildConfiguration()->qt4Target()
305 ->qt4Project()->rootProjectNode()->targetInformation();
309 return QDir::toNativeSeparators(QDir::cleanPath(ti.workingDir
310 + QLatin1Char('/') + ti.target));
313 QString MaemoPackageCreationStep::buildDirectory() const
315 const TargetInformation &ti = qt4BuildConfiguration()->qt4Target()
316 ->qt4Project()->rootProjectNode()->targetInformation();
317 return ti.valid ? ti.buildDir : QString();
320 QString MaemoPackageCreationStep::executableFileName() const
322 return QFileInfo(localExecutableFilePath()).fileName();
325 const MaemoToolChain *MaemoPackageCreationStep::maemoToolChain() const
327 return static_cast<MaemoToolChain *>(qt4BuildConfiguration()->toolChain());
330 QString MaemoPackageCreationStep::maddeRoot() const
332 return maemoToolChain()->maddeRoot();
335 QString MaemoPackageCreationStep::targetRoot() const
337 return maemoToolChain()->targetRoot();
340 bool MaemoPackageCreationStep::packagingNeeded() const
342 QFileInfo packageInfo(packageFilePath());
343 if (!packageInfo.exists() || m_packageContents->isModified())
346 for (int i = 0; i < m_packageContents->rowCount(); ++i) {
347 if (packageInfo.lastModified()
348 <= QFileInfo(m_packageContents->deployableAt(i).localFilePath)
356 QString MaemoPackageCreationStep::packageFilePath() const
358 return buildDirectory() % QDir::separator() % executableFileName().toLower()
359 % QLatin1Char('_') % versionString() % QLatin1String("_armel.deb");
362 QString MaemoPackageCreationStep::versionString() const
364 return m_versionString;
367 void MaemoPackageCreationStep::setVersionString(const QString &version)
369 m_versionString = version;
372 QString MaemoPackageCreationStep::nativePath(const QFile &file) const
374 return QDir::toNativeSeparators(QFileInfo(file).filePath());
377 void MaemoPackageCreationStep::raiseError(const QString &shortMsg,
378 const QString &detailedMsg)
380 QTextCharFormat textCharFormat;
381 emit addOutput(detailedMsg.isNull() ? shortMsg : detailedMsg, textCharFormat);
382 emit addTask(Task(Task::Error, shortMsg, QString(), -1,
383 TASK_CATEGORY_BUILDSYSTEM));
386 const QLatin1String MaemoPackageCreationStep::CreatePackageId("Qt4ProjectManager.MaemoPackageCreationStep");
388 } // namespace Internal
389 } // namespace Qt4ProjectManager