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 "maemopackagecreationwidget.h"
46 #include "maemopackagecontents.h"
47 #include "maemotoolchain.h"
48 #include "profilewrapper.h"
49
50 #include <projectexplorer/projectexplorerconstants.h>
51 #include <qt4buildconfiguration.h>
52 #include <qt4project.h>
53 #include <qt4target.h>
54
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>
62
63 namespace {
64     const QLatin1String PackagingEnabledKey("Packaging Enabled");
65     const QLatin1String DefaultVersionNumber("0.0.1");
66     const QLatin1String VersionNumberKey("Version Number");
67 }
68
69 using namespace ProjectExplorer::Constants;
70 using ProjectExplorer::BuildConfiguration;
71 using ProjectExplorer::BuildStepConfigWidget;
72 using ProjectExplorer::Task;
73
74 namespace Qt4ProjectManager {
75 namespace Internal {
76
77 MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig)
78     : ProjectExplorer::BuildStep(buildConfig, CreatePackageId),
79       m_packageContents(new MaemoPackageContents(this)),
80       m_packagingEnabled(true),
81       m_versionString(DefaultVersionNumber)
82 {
83 }
84
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)
91 {
92 }
93
94 MaemoPackageCreationStep::~MaemoPackageCreationStep() {}
95
96 bool MaemoPackageCreationStep::init()
97 {
98     return true;
99 }
100
101 QVariantMap MaemoPackageCreationStep::toMap() const
102 {
103     QVariantMap map(ProjectExplorer::BuildStep::toMap());
104     map.insert(PackagingEnabledKey, m_packagingEnabled);
105     map.insert(VersionNumberKey, m_versionString);
106     return map;
107 }
108
109 bool MaemoPackageCreationStep::fromMap(const QVariantMap &map)
110 {
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     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"));
152
153     const QString buildDir = buildDirectory();
154     env.insert(QLatin1String("PWD"), buildDir);
155
156     const QRegExp envPattern(QLatin1String("([^=]+)=[\"']?([^;\"']+)[\"']? ;.*"));
157     QByteArray line;
158     do {
159         line = configFile.readLine(200);
160         if (envPattern.exactMatch(line))
161             env.insert(envPattern.cap(1), envPattern.cap(2));
162     } while (!line.isEmpty());
163     
164     QProcess buildProc;
165     buildProc.setProcessEnvironment(env);
166     buildProc.setWorkingDirectory(buildDir);
167     buildProc.start("cd " + buildDir);
168     buildProc.waitForFinished();
169
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();
174
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))
179             return false;
180
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)));
185             return false;
186         }
187
188         QByteArray rulesContents = rulesFile.readAll();
189         rulesContents.replace("DESTDIR", "INSTALL_ROOT");
190
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=.");
194
195         rulesFile.resize(0);
196         rulesFile.write(rulesContents);
197         if (rulesFile.error() != QFile::NoError) {
198             raiseError(tr("Packaging Error: Cannot write file '%1'.")
199                        .arg(nativePath(rulesFile)));
200             return false;
201         }
202     }
203
204     {
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(')'));
211             changeLog.resize(0);
212             changeLog.write(content.toUtf8());
213         }
214     }
215
216     if (!runCommand(buildProc, "dpkg-buildpackage -nc -uc -us"))
217         return false;
218
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));
238             return false;
239         }
240     }
241
242     emit addOutput(tr("Package created."), textCharFormat);
243     m_packageContents->setUnModified();
244     return true;
245 }
246
247 bool MaemoPackageCreationStep::runCommand(QProcess &proc, const QString &command)
248 {
249     QTextCharFormat textCharFormat;
250     emit addOutput(tr("Package Creation: Running command '%1'.").arg(command), textCharFormat);
251     QString perl;
252 #ifdef Q_OS_WIN
253     perl = maddeRoot() + QLatin1String("/bin/perl.exe ");
254 #endif
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()));
260         return false;
261     }
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.")
266             .arg(command);
267         if (proc.error() != QProcess::UnknownError)
268             mainMessage += tr(" Reason: %1").arg(proc.errorString());
269         else
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());
274         return false;
275     }
276     return true;
277 }
278
279 const Qt4BuildConfiguration *MaemoPackageCreationStep::qt4BuildConfiguration() const
280 {
281     return static_cast<Qt4BuildConfiguration *>(buildConfiguration());
282 }
283
284 QString MaemoPackageCreationStep::localExecutableFilePath() const
285 {
286     const TargetInformation &ti = qt4BuildConfiguration()->qt4Target()
287         ->qt4Project()->rootProjectNode()->targetInformation();
288     if (!ti.valid)
289         return QString();
290     return QDir::toNativeSeparators(QDir::cleanPath(ti.workingDir
291         + QLatin1Char('/') + executableFileName()));
292 }
293
294 QString MaemoPackageCreationStep::buildDirectory() const
295 {
296     const TargetInformation &ti = qt4BuildConfiguration()->qt4Target()
297         ->qt4Project()->rootProjectNode()->targetInformation();
298     return ti.valid ? ti.buildDir : QString();
299 }
300
301 QString MaemoPackageCreationStep::executableFileName() const
302 {
303     const Qt4Project * const project
304         = qt4BuildConfiguration()->qt4Target()->qt4Project();
305     const TargetInformation &ti
306         = project->rootProjectNode()->targetInformation();
307     if (!ti.valid)
308         return QString();
309
310     return project->rootProjectNode()->projectType() == LibraryTemplate
311         ? QLatin1String("lib") + ti.target + QLatin1String(".so")
312         : ti.target;
313 }
314
315 const MaemoToolChain *MaemoPackageCreationStep::maemoToolChain() const
316 {
317     return static_cast<MaemoToolChain *>(qt4BuildConfiguration()->toolChain());
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     QFileInfo packageInfo(packageFilePath());
333     if (!packageInfo.exists() || m_packageContents->isModified())
334         return true;
335
336     for (int i = 0; i < m_packageContents->rowCount(); ++i) {
337         if (packageInfo.lastModified()
338             <= QFileInfo(m_packageContents->deployableAt(i).localFilePath)
339                .lastModified())
340             return true;
341     }
342
343     return false;
344 }
345
346 QString MaemoPackageCreationStep::packageFilePath() const
347 {
348     return buildDirectory() % QDir::separator() % executableFileName().toLower()
349         % QLatin1Char('_') % versionString() % QLatin1String("_armel.deb");
350 }
351
352 QString MaemoPackageCreationStep::versionString() const
353 {
354     return m_versionString;
355 }
356
357 void MaemoPackageCreationStep::setVersionString(const QString &version)
358 {
359     m_versionString = version;
360 }
361
362 QString MaemoPackageCreationStep::nativePath(const QFile &file) const
363 {
364     return QDir::toNativeSeparators(QFileInfo(file).filePath());
365 }
366
367 void MaemoPackageCreationStep::raiseError(const QString &shortMsg,
368                                           const QString &detailedMsg)
369 {
370     QTextCharFormat textCharFormat;
371     emit addOutput(detailedMsg.isNull() ? shortMsg : detailedMsg, textCharFormat);
372     emit addTask(Task(Task::Error, shortMsg, QString(), -1,
373                       TASK_CATEGORY_BUILDSYSTEM));
374 }
375
376 QSharedPointer<ProFileWrapper> MaemoPackageCreationStep::proFileWrapper() const
377 {
378     if (!m_proFileWrapper) {
379         const Qt4ProFileNode * const proFileNode = qt4BuildConfiguration()
380             ->qt4Target()->qt4Project()->rootProjectNode();
381         m_proFileWrapper = QSharedPointer<ProFileWrapper>(
382             new ProFileWrapper(proFileNode->path()));
383     }
384
385     return m_proFileWrapper;
386 }
387
388 const QLatin1String MaemoPackageCreationStep::CreatePackageId("Qt4ProjectManager.MaemoPackageCreationStep");
389
390 } // namespace Internal
391 } // namespace Qt4ProjectManager