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 "maemoglobal.h"
46 #include "maemopackagecreationwidget.h"
47 #include "maemodeployables.h"
48 #include "maemodeploystep.h"
49 #include "maemotoolchain.h"
50 #include "profilewrapper.h"
52 #include <projectexplorer/projectexplorerconstants.h>
53 #include <qt4buildconfiguration.h>
54 #include <qt4project.h>
55 #include <qt4target.h>
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_packagingEnabled(true),
79 m_versionString(DefaultVersionNumber)
83 MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig,
84 MaemoPackageCreationStep *other)
85 : BuildStep(buildConfig, other),
86 m_packagingEnabled(other->m_packagingEnabled),
87 m_versionString(other->m_versionString)
91 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);
108 bool MaemoPackageCreationStep::fromMap(const QVariantMap &map)
110 m_packagingEnabled = map.value(PackagingEnabledKey, true).toBool();
111 m_versionString = map.value(VersionNumberKey, DefaultVersionNumber).toString();
112 return ProjectExplorer::BuildStep::fromMap(map);
115 void MaemoPackageCreationStep::run(QFutureInterface<bool> &fi)
117 fi.reportResult(m_packagingEnabled ? createPackage() : true);
120 BuildStepConfigWidget *MaemoPackageCreationStep::createConfigWidget()
122 return new MaemoPackageCreationWidget(this);
125 bool MaemoPackageCreationStep::createPackage()
127 if (!packagingNeeded())
130 emit addOutput(tr("Creating package file ..."), BuildStep::MessageOutput);
131 QFile configFile(targetRoot() % QLatin1String("/config.sh"));
132 if (!configFile.open(QIODevice::ReadOnly)) {
133 raiseError(tr("Cannot open MADDE config file '%1'.")
134 .arg(nativePath(configFile)));
138 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
139 const QString &path = QDir::toNativeSeparators(maddeRoot() + QLatin1Char('/'));
141 const QLatin1String key("PATH");
142 QString colon = QLatin1String(":");
144 colon = QLatin1String(";");
145 env.insert(key, path % QLatin1String("bin") % colon % env.value(key));
147 env.insert(key, targetRoot() % "/bin" % colon % env.value(key));
148 env.insert(key, path % QLatin1String("madbin") % colon % env.value(key));
149 env.insert(QLatin1String("PERL5LIB"), path % QLatin1String("madlib/perl5"));
151 const QString buildDir = buildDirectory();
152 env.insert(QLatin1String("PWD"), buildDir);
154 const QRegExp envPattern(QLatin1String("([^=]+)=[\"']?([^;\"']+)[\"']? ;.*"));
157 line = configFile.readLine(200);
158 if (envPattern.exactMatch(line))
159 env.insert(envPattern.cap(1), envPattern.cap(2));
160 } while (!line.isEmpty());
163 m_buildProc.reset(new QProcess);
164 connect(m_buildProc.data(), SIGNAL(readyReadStandardOutput()), this,
165 SLOT(handleBuildOutput()));
166 connect(m_buildProc.data(), SIGNAL(readyReadStandardError()), this,
167 SLOT(handleBuildOutput()));
168 m_buildProc->setProcessEnvironment(env);
169 m_buildProc->setWorkingDirectory(buildDir);
170 m_buildProc->start("cd " + buildDir);
171 m_buildProc->waitForFinished();
173 // cache those two since we can change the version number during packaging
174 // and might fail later to modify, copy, remove etc. the generated package
175 const QString version = versionString();
176 const QString pkgFilePath = packageFilePath();
178 if (!QFileInfo(buildDir + QLatin1String("/debian")).exists()) {
179 const QString command = QLatin1String("dh_make -s -n -p ")
180 % projectName() % QLatin1Char('_') % versionString();
181 if (!runCommand(command))
184 QFile rulesFile(buildDir + QLatin1String("/debian/rules"));
185 if (!rulesFile.open(QIODevice::ReadWrite)) {
186 raiseError(tr("Packaging Error: Cannot open file '%1'.")
187 .arg(nativePath(rulesFile)));
191 QByteArray rulesContents = rulesFile.readAll();
192 rulesContents.replace("DESTDIR", "INSTALL_ROOT");
194 // Would be the right solution, but does not work (on Windows),
195 // because dpkg-genchanges doesn't know about it (and can't be told).
196 // rulesContents.replace("dh_builddeb", "dh_builddeb --destdir=.");
199 rulesFile.write(rulesContents);
200 if (rulesFile.error() != QFile::NoError) {
201 raiseError(tr("Packaging Error: Cannot write file '%1'.")
202 .arg(nativePath(rulesFile)));
208 QFile::remove(buildDir + QLatin1String("/debian/files"));
209 QFile changeLog(buildDir + QLatin1String("/debian/changelog"));
210 if (changeLog.open(QIODevice::ReadWrite)) {
211 QString content = QString::fromUtf8(changeLog.readAll());
212 content.replace(QRegExp("\\([a-zA-Z0-9_\\.]+\\)"),
213 QLatin1Char('(') % version % QLatin1Char(')'));
215 changeLog.write(content.toUtf8());
219 if (!runCommand(QLatin1String("dpkg-buildpackage -nc -uc -us")))
222 // Workaround for non-working dh_builddeb --destdir=.
223 if (!QDir(buildDir).isRoot()) {
224 const QString packageFileName = QFileInfo(pkgFilePath).fileName();
225 const QString changesFileName = QFileInfo(packageFileName)
226 .completeBaseName() + QLatin1String(".changes");
227 const QString packageSourceDir = buildDir + QLatin1String("/../");
228 const QString packageSourceFilePath
229 = packageSourceDir + packageFileName;
230 const QString changesSourceFilePath
231 = packageSourceDir + changesFileName;
232 const QString changesTargetFilePath
233 = buildDir + QLatin1Char('/') + changesFileName;
234 QFile::remove(pkgFilePath);
235 QFile::remove(changesTargetFilePath);
236 if (!QFile::rename(packageSourceFilePath, pkgFilePath)
237 || !QFile::rename(changesSourceFilePath, changesTargetFilePath)) {
238 raiseError(tr("Packaging failed."),
239 tr("Could not move package files from %1 to %2.")
240 .arg(packageSourceDir, buildDir));
245 emit addOutput(tr("Package created."), BuildStep::MessageOutput);
246 deployStep()->deployables()->setUnmodified();
250 bool MaemoPackageCreationStep::runCommand(const QString &command)
252 emit addOutput(tr("Package Creation: Running command '%1'.").arg(command), BuildStep::MessageOutput);
255 perl = maddeRoot() + QLatin1String("/bin/perl.exe ");
257 m_buildProc->start(perl + maddeRoot() % QLatin1String("/madbin/") % command);
258 if (!m_buildProc->waitForStarted()) {
259 raiseError(tr("Packaging failed."),
260 tr("Packaging error: Could not start command '%1'. Reason: %2")
261 .arg(command).arg(m_buildProc->errorString()));
264 m_buildProc->write("\n"); // For dh_make
265 m_buildProc->waitForFinished(-1);
266 if (m_buildProc->error() != QProcess::UnknownError || m_buildProc->exitCode() != 0) {
267 QString mainMessage = tr("Packaging Error: Command '%1' failed.")
269 if (m_buildProc->error() != QProcess::UnknownError)
270 mainMessage += tr(" Reason: %1").arg(m_buildProc->errorString());
272 mainMessage += tr("Exit code: %1").arg(m_buildProc->exitCode());
273 raiseError(mainMessage);
279 void MaemoPackageCreationStep::handleBuildOutput()
281 const QByteArray &stdOut = m_buildProc->readAllStandardOutput();
282 if (!stdOut.isEmpty())
283 emit addOutput(QString::fromLocal8Bit(stdOut), BuildStep::NormalOutput);
284 const QByteArray &errorOut = m_buildProc->readAllStandardError();
285 if (!errorOut.isEmpty()) {
286 emit addOutput(QString::fromLocal8Bit(errorOut), BuildStep::ErrorOutput);
290 const Qt4BuildConfiguration *MaemoPackageCreationStep::qt4BuildConfiguration() const
292 return static_cast<Qt4BuildConfiguration *>(buildConfiguration());
295 QString MaemoPackageCreationStep::buildDirectory() const
297 return qt4BuildConfiguration()->buildDirectory();
300 QString MaemoPackageCreationStep::projectName() const
302 return qt4BuildConfiguration()->qt4Target()->qt4Project()
303 ->rootProjectNode()->displayName().toLower();
306 const MaemoToolChain *MaemoPackageCreationStep::maemoToolChain() const
308 return static_cast<MaemoToolChain *>(qt4BuildConfiguration()->toolChain());
311 MaemoDeployStep *MaemoPackageCreationStep::deployStep() const
313 MaemoDeployStep * const deployStep
314 = MaemoGlobal::buildStep<MaemoDeployStep>(buildConfiguration());
315 Q_ASSERT(deployStep &&
316 "Fatal error: Maemo build configuration without deploy step.");
320 QString MaemoPackageCreationStep::maddeRoot() const
322 return maemoToolChain()->maddeRoot();
325 QString MaemoPackageCreationStep::targetRoot() const
327 return maemoToolChain()->targetRoot();
330 bool MaemoPackageCreationStep::packagingNeeded() const
332 const MaemoDeployables * const deployables = deployStep()->deployables();
333 QFileInfo packageInfo(packageFilePath());
334 if (!packageInfo.exists() || deployables->isModified())
337 const int deployableCount = deployables->deployableCount();
338 for (int i = 0; i < deployableCount; ++i) {
339 if (packageInfo.lastModified()
340 <= QFileInfo(deployables->deployableAt(i).localFilePath)
348 QString MaemoPackageCreationStep::packageFilePath() const
350 return buildDirectory() % '/' % projectName()
351 % QLatin1Char('_') % versionString() % QLatin1String("_armel.deb");
354 QString MaemoPackageCreationStep::versionString() const
356 return m_versionString;
359 void MaemoPackageCreationStep::setVersionString(const QString &version)
361 m_versionString = version;
364 QString MaemoPackageCreationStep::nativePath(const QFile &file) const
366 return QDir::toNativeSeparators(QFileInfo(file).filePath());
369 void MaemoPackageCreationStep::raiseError(const QString &shortMsg,
370 const QString &detailedMsg)
372 emit addOutput(detailedMsg.isNull() ? shortMsg : detailedMsg, BuildStep::ErrorOutput);
373 emit addTask(Task(Task::Error, shortMsg, QString(), -1,
374 TASK_CATEGORY_BUILDSYSTEM));
377 const QLatin1String MaemoPackageCreationStep::CreatePackageId("Qt4ProjectManager.MaemoPackageCreationStep");
379 } // namespace Internal
380 } // namespace Qt4ProjectManager