OSDN Git Service

Fix package building after version number change.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qt-maemo / maemopackagecreationstep.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of Qt Creator.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "maemopackagecreationstep.h"
43
44 #include "maemoconstants.h"
45 #include "maemopackagecreationwidget.h"
46 #include "maemopackagecontents.h"
47 #include "maemotoolchain.h"
48
49 #include <projectexplorer/projectexplorerconstants.h>
50 #include <qt4buildconfiguration.h>
51 #include <qt4project.h>
52 #include <qt4target.h>
53
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>
61
62 namespace {
63     const QLatin1String PackagingEnabledKey("Packaging Enabled");
64     const QLatin1String DefaultVersionNumber("0.0.1");
65     const QLatin1String VersionNumberKey("Version Number");
66 }
67
68 using namespace ProjectExplorer::Constants;
69 using ProjectExplorer::BuildConfiguration;
70 using ProjectExplorer::BuildStepConfigWidget;
71 using ProjectExplorer::Task;
72
73 namespace Qt4ProjectManager {
74 namespace Internal {
75
76 MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig)
77     : ProjectExplorer::BuildStep(buildConfig, CreatePackageId),
78       m_packageContents(new MaemoPackageContents(this)),
79       m_packagingEnabled(true),
80       m_versionString(DefaultVersionNumber)
81 {
82 }
83
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)
90 {
91 }
92
93 MaemoPackageCreationStep::~MaemoPackageCreationStep() {}
94
95 bool MaemoPackageCreationStep::init()
96 {
97     return true;
98 }
99
100 QVariantMap MaemoPackageCreationStep::toMap() const
101 {
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());
106 }
107
108 bool MaemoPackageCreationStep::fromMap(const QVariantMap &map)
109 {
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);
114 }
115
116 void MaemoPackageCreationStep::run(QFutureInterface<bool> &fi)
117 {
118     fi.reportResult(m_packagingEnabled ? createPackage() : true);
119 }
120
121 BuildStepConfigWidget *MaemoPackageCreationStep::createConfigWidget()
122 {
123     return new MaemoPackageCreationWidget(this);
124 }
125
126 bool MaemoPackageCreationStep::createPackage()
127 {
128     if (!packagingNeeded())
129         return true;
130
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)));
137         return false;
138     }
139
140     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
141     const QString &path = QDir::toNativeSeparators(maddeRoot() + QLatin1Char('/'));
142
143     const QLatin1String key("PATH");
144     QString colon = QLatin1String(":");
145 #ifdef Q_OS_WIN
146     colon = QLatin1String(";");
147     env.insert(key, path % QLatin1String("bin") % colon % env.value(key));
148 #endif
149
150     env.insert(key, targetRoot() % "/bin" % colon % env.value(key));
151     env.insert(key, path % QLatin1String("madbin") % colon % env.value(key));
152
153     QString perlLib = QDir::fromNativeSeparators(path % QLatin1String("madlib/perl5"));
154 #ifdef Q_OS_WIN
155     perlLib = perlLib.remove(QLatin1Char(':'));
156     perlLib = perlLib.prepend(QLatin1Char('/'));
157 #endif
158     env.insert(QLatin1String("PERL5LIB"), perlLib);
159
160     const QString buildDir = buildDirectory();
161     env.insert(QLatin1String("PWD"), buildDir);
162
163     const QRegExp envPattern(QLatin1String("([^=]+)=[\"']?([^;\"']+)[\"']? ;.*"));
164     QByteArray line;
165     do {
166         line = configFile.readLine(200);
167         if (envPattern.exactMatch(line))
168             env.insert(envPattern.cap(1), envPattern.cap(2));
169     } while (!line.isEmpty());
170     
171
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();
181
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))
186             return false;
187
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)));
192             return false;
193         }
194
195         QByteArray rulesContents = rulesFile.readAll();
196         rulesContents.replace("DESTDIR", "INSTALL_ROOT");
197
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=.");
201
202         rulesFile.resize(0);
203         rulesFile.write(rulesContents);
204         if (rulesFile.error() != QFile::NoError) {
205             raiseError(tr("Packaging Error: Cannot write file '%1'.")
206                        .arg(nativePath(rulesFile)));
207             return false;
208         }
209     }
210
211     {
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(')'));
218             changeLog.resize(0);
219             changeLog.write(content.toUtf8());
220         }
221     }
222
223     if (!runCommand(QLatin1String("dpkg-buildpackage -nc -uc -us")))
224         return false;
225
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));
245             return false;
246         }
247     }
248
249     emit addOutput(tr("Package created."), textCharFormat);
250     m_packageContents->setUnModified();
251     return true;
252 }
253
254 bool MaemoPackageCreationStep::runCommand(const QString &command)
255 {
256     QTextCharFormat textCharFormat;
257     emit addOutput(tr("Package Creation: Running command '%1'.").arg(command), textCharFormat);
258     QString perl;
259 #ifdef Q_OS_WIN
260     perl = maddeRoot() + QLatin1String("/bin/perl.exe ");
261 #endif
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()));
267         return false;
268     }
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.")
273             .arg(command);
274         if (m_buildProc->error() != QProcess::UnknownError)
275             mainMessage += tr(" Reason: %1").arg(m_buildProc->errorString());
276         else
277             mainMessage += tr("Exit code: %1").arg(m_buildProc->exitCode());
278         raiseError(mainMessage);
279         return false;
280     }
281     return true;
282 }
283
284 void MaemoPackageCreationStep::handleBuildOutput()
285 {
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);
294     }
295 }
296
297 const Qt4BuildConfiguration *MaemoPackageCreationStep::qt4BuildConfiguration() const
298 {
299     return static_cast<Qt4BuildConfiguration *>(buildConfiguration());
300 }
301
302 QString MaemoPackageCreationStep::localExecutableFilePath() const
303 {
304     const TargetInformation &ti = qt4BuildConfiguration()->qt4Target()
305         ->qt4Project()->rootProjectNode()->targetInformation();
306     if (!ti.valid)
307         return QString();
308
309     return QDir::toNativeSeparators(QDir::cleanPath(ti.workingDir
310         + QLatin1Char('/') + ti.target));
311 }
312
313 QString MaemoPackageCreationStep::buildDirectory() const
314 {
315     const TargetInformation &ti = qt4BuildConfiguration()->qt4Target()
316         ->qt4Project()->rootProjectNode()->targetInformation();
317     return ti.valid ? ti.buildDir : QString();
318 }
319
320 QString MaemoPackageCreationStep::executableFileName() const
321 {
322     return QFileInfo(localExecutableFilePath()).fileName();
323 }
324
325 const MaemoToolChain *MaemoPackageCreationStep::maemoToolChain() const
326 {
327     return static_cast<MaemoToolChain *>(qt4BuildConfiguration()->toolChain());
328 }
329
330 QString MaemoPackageCreationStep::maddeRoot() const
331 {
332     return maemoToolChain()->maddeRoot();
333 }
334
335 QString MaemoPackageCreationStep::targetRoot() const
336 {
337     return maemoToolChain()->targetRoot();
338 }
339
340 bool MaemoPackageCreationStep::packagingNeeded() const
341 {
342     QFileInfo packageInfo(packageFilePath());
343     if (!packageInfo.exists() || m_packageContents->isModified())
344         return true;
345
346     for (int i = 0; i < m_packageContents->rowCount(); ++i) {
347         if (packageInfo.lastModified()
348             <= QFileInfo(m_packageContents->deployableAt(i).localFilePath)
349                .lastModified())
350             return true;
351     }
352
353     return false;
354 }
355
356 QString MaemoPackageCreationStep::packageFilePath() const
357 {
358     return buildDirectory() % QDir::separator() % executableFileName().toLower()
359         % QLatin1Char('_') % versionString() % QLatin1String("_armel.deb");
360 }
361
362 QString MaemoPackageCreationStep::versionString() const
363 {
364     return m_versionString;
365 }
366
367 void MaemoPackageCreationStep::setVersionString(const QString &version)
368 {
369     m_versionString = version;
370 }
371
372 QString MaemoPackageCreationStep::nativePath(const QFile &file) const
373 {
374     return QDir::toNativeSeparators(QFileInfo(file).filePath());
375 }
376
377 void MaemoPackageCreationStep::raiseError(const QString &shortMsg,
378                                           const QString &detailedMsg)
379 {
380     QTextCharFormat textCharFormat;
381     emit addOutput(detailedMsg.isNull() ? shortMsg : detailedMsg, textCharFormat);
382     emit addTask(Task(Task::Error, shortMsg, QString(), -1,
383                       TASK_CATEGORY_BUILDSYSTEM));
384 }
385
386 const QLatin1String MaemoPackageCreationStep::CreatePackageId("Qt4ProjectManager.MaemoPackageCreationStep");
387
388 } // namespace Internal
389 } // namespace Qt4ProjectManager