OSDN Git Service

Merge remote branch 'origin/2.0'
[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 "maemoglobal.h"
46 #include "maemopackagecreationwidget.h"
47 #include "maemodeployables.h"
48 #include "maemodeploystep.h"
49 #include "maemotoolchain.h"
50 #include "profilewrapper.h"
51
52 #include <projectexplorer/projectexplorerconstants.h>
53 #include <qt4buildconfiguration.h>
54 #include <qt4project.h>
55 #include <qt4target.h>
56
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_packagingEnabled(true),
79       m_versionString(DefaultVersionNumber)
80 {
81 }
82
83 MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig,
84     MaemoPackageCreationStep *other)
85     : BuildStep(buildConfig, other),
86       m_packagingEnabled(other->m_packagingEnabled),
87       m_versionString(other->m_versionString)
88 {
89 }
90
91 MaemoPackageCreationStep::~MaemoPackageCreationStep()
92 {
93 }
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;
106 }
107
108 bool MaemoPackageCreationStep::fromMap(const QVariantMap &map)
109 {
110     m_packagingEnabled = map.value(PackagingEnabledKey, true).toBool();
111     m_versionString = map.value(VersionNumberKey, DefaultVersionNumber).toString();
112     return ProjectExplorer::BuildStep::fromMap(map);
113 }
114
115 void MaemoPackageCreationStep::run(QFutureInterface<bool> &fi)
116 {
117     fi.reportResult(m_packagingEnabled ? createPackage() : true);
118 }
119
120 BuildStepConfigWidget *MaemoPackageCreationStep::createConfigWidget()
121 {
122     return new MaemoPackageCreationWidget(this);
123 }
124
125 bool MaemoPackageCreationStep::createPackage()
126 {
127     if (!packagingNeeded())
128         return true;
129
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)));
135         return false;
136     }
137
138     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
139     const QString &path = QDir::toNativeSeparators(maddeRoot() + QLatin1Char('/'));
140
141     const QLatin1String key("PATH");
142     QString colon = QLatin1String(":");
143 #ifdef Q_OS_WIN
144     colon = QLatin1String(";");
145     env.insert(key, path % QLatin1String("bin") % colon % env.value(key));
146 #endif
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"));
150
151     const QString buildDir = buildDirectory();
152     env.insert(QLatin1String("PWD"), buildDir);
153
154     const QRegExp envPattern(QLatin1String("([^=]+)=[\"']?([^;\"']+)[\"']? ;.*"));
155     QByteArray line;
156     do {
157         line = configFile.readLine(200);
158         if (envPattern.exactMatch(line))
159             env.insert(envPattern.cap(1), envPattern.cap(2));
160     } while (!line.isEmpty());
161     
162
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();
172
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();
177
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))
182             return false;
183
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)));
188             return false;
189         }
190
191         QByteArray rulesContents = rulesFile.readAll();
192         rulesContents.replace("DESTDIR", "INSTALL_ROOT");
193
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=.");
197
198         rulesFile.resize(0);
199         rulesFile.write(rulesContents);
200         if (rulesFile.error() != QFile::NoError) {
201             raiseError(tr("Packaging Error: Cannot write file '%1'.")
202                        .arg(nativePath(rulesFile)));
203             return false;
204         }
205     }
206
207     {
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(')'));
214             changeLog.resize(0);
215             changeLog.write(content.toUtf8());
216         }
217     }
218
219     if (!runCommand(QLatin1String("dpkg-buildpackage -nc -uc -us")))
220         return false;
221
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));
241             return false;
242         }
243     }
244
245     emit addOutput(tr("Package created."), BuildStep::MessageOutput);
246     deployStep()->deployables()->setUnmodified();
247     return true;
248 }
249
250 bool MaemoPackageCreationStep::runCommand(const QString &command)
251 {
252     emit addOutput(tr("Package Creation: Running command '%1'.").arg(command), BuildStep::MessageOutput);
253     QString perl;
254 #ifdef Q_OS_WIN
255     perl = maddeRoot() + QLatin1String("/bin/perl.exe ");
256 #endif
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()));
262         return false;
263     }
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.")
268             .arg(command);
269         if (m_buildProc->error() != QProcess::UnknownError)
270             mainMessage += tr(" Reason: %1").arg(m_buildProc->errorString());
271         else
272             mainMessage += tr("Exit code: %1").arg(m_buildProc->exitCode());
273         raiseError(mainMessage);
274         return false;
275     }
276     return true;
277 }
278
279 void MaemoPackageCreationStep::handleBuildOutput()
280 {
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);
287     }
288 }
289
290 const Qt4BuildConfiguration *MaemoPackageCreationStep::qt4BuildConfiguration() const
291 {
292     return static_cast<Qt4BuildConfiguration *>(buildConfiguration());
293 }
294
295 QString MaemoPackageCreationStep::buildDirectory() const
296 {
297     return qt4BuildConfiguration()->buildDirectory();
298 }
299
300 QString MaemoPackageCreationStep::projectName() const
301 {
302     return qt4BuildConfiguration()->qt4Target()->qt4Project()
303         ->rootProjectNode()->displayName().toLower();
304 }
305
306 const MaemoToolChain *MaemoPackageCreationStep::maemoToolChain() const
307 {
308     return static_cast<MaemoToolChain *>(qt4BuildConfiguration()->toolChain());
309 }
310
311 MaemoDeployStep *MaemoPackageCreationStep::deployStep() const
312 {
313     MaemoDeployStep * const deployStep
314         = MaemoGlobal::buildStep<MaemoDeployStep>(buildConfiguration());
315     Q_ASSERT(deployStep &&
316         "Fatal error: Maemo build configuration without deploy step.");
317     return deployStep;
318 }
319
320 QString MaemoPackageCreationStep::maddeRoot() const
321 {
322     return maemoToolChain()->maddeRoot();
323 }
324
325 QString MaemoPackageCreationStep::targetRoot() const
326 {
327     return maemoToolChain()->targetRoot();
328 }
329
330 bool MaemoPackageCreationStep::packagingNeeded() const
331 {
332     const MaemoDeployables * const deployables = deployStep()->deployables();
333     QFileInfo packageInfo(packageFilePath());
334     if (!packageInfo.exists() || deployables->isModified())
335         return true;
336
337     const int deployableCount = deployables->deployableCount();
338     for (int i = 0; i < deployableCount; ++i) {
339         if (packageInfo.lastModified()
340             <= QFileInfo(deployables->deployableAt(i).localFilePath)
341                .lastModified())
342             return true;
343     }
344
345     return false;
346 }
347
348 QString MaemoPackageCreationStep::packageFilePath() const
349 {
350     return buildDirectory() % '/' % projectName()
351         % QLatin1Char('_') % versionString() % QLatin1String("_armel.deb");
352 }
353
354 QString MaemoPackageCreationStep::versionString() const
355 {
356     return m_versionString;
357 }
358
359 void MaemoPackageCreationStep::setVersionString(const QString &version)
360 {
361     m_versionString = version;
362 }
363
364 QString MaemoPackageCreationStep::nativePath(const QFile &file) const
365 {
366     return QDir::toNativeSeparators(QFileInfo(file).filePath());
367 }
368
369 void MaemoPackageCreationStep::raiseError(const QString &shortMsg,
370                                           const QString &detailedMsg)
371 {
372     emit addOutput(detailedMsg.isNull() ? shortMsg : detailedMsg, BuildStep::ErrorOutput);
373     emit addTask(Task(Task::Error, shortMsg, QString(), -1,
374                       TASK_CATEGORY_BUILDSYSTEM));
375 }
376
377 const QLatin1String MaemoPackageCreationStep::CreatePackageId("Qt4ProjectManager.MaemoPackageCreationStep");
378
379 } // namespace Internal
380 } // namespace Qt4ProjectManager