maemopackagecreationstep.cpp 13.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "maemopackagecreationstep.h"

44
#include "maemoconstants.h"
45
#include "maemodeployables.h"
46
#include "maemodeploystep.h"
ck's avatar
ck committed
47
48
49
#include "maemoglobal.h"
#include "maemopackagecreationwidget.h"
#include "maemoprofilewrapper.h"
ck's avatar
ck committed
50
#include "maemotoolchain.h"
51

52
#include <projectexplorer/projectexplorerconstants.h>
ck's avatar
ck committed
53
54
55
56
57
58
59
#include <qt4buildconfiguration.h>
#include <qt4project.h>
#include <qt4target.h>

#include <QtCore/QProcess>
#include <QtCore/QProcessEnvironment>
#include <QtCore/QStringBuilder>
60
61
#include <QtGui/QWidget>

62
63
64
65
66
namespace {
    const QLatin1String PackagingEnabledKey("Packaging Enabled");
    const QLatin1String DefaultVersionNumber("0.0.1");
    const QLatin1String VersionNumberKey("Version Number");
}
ck's avatar
ck committed
67

68
using namespace ProjectExplorer::Constants;
69
70
using ProjectExplorer::BuildConfiguration;
using ProjectExplorer::BuildStepConfigWidget;
71
using ProjectExplorer::Task;
72
73
74
75
76

namespace Qt4ProjectManager {
namespace Internal {

MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig)
77
    : ProjectExplorer::BuildStep(buildConfig, CreatePackageId),
78
79
      m_packagingEnabled(true),
      m_versionString(DefaultVersionNumber)
80
{
ck's avatar
ck committed
81
    ctor();
82
83
84
85
}

MaemoPackageCreationStep::MaemoPackageCreationStep(BuildConfiguration *buildConfig,
    MaemoPackageCreationStep *other)
86
    : BuildStep(buildConfig, other),
87
88
      m_packagingEnabled(other->m_packagingEnabled),
      m_versionString(other->m_versionString)
89
{
ck's avatar
ck committed
90
    ctor();
91
92
}

93
94
95
MaemoPackageCreationStep::~MaemoPackageCreationStep()
{
}
96

ck's avatar
ck committed
97
98
99
100
101
102
void MaemoPackageCreationStep::ctor()
{
    connect(buildConfiguration(), SIGNAL(buildDirectoryChanged()), this,
        SIGNAL(packageFilePathChanged()));
}

103
104
105
106
107
bool MaemoPackageCreationStep::init()
{
    return true;
}

ck's avatar
ck committed
108
109
110
QVariantMap MaemoPackageCreationStep::toMap() const
{
    QVariantMap map(ProjectExplorer::BuildStep::toMap());
ck's avatar
ck committed
111
    map.insert(PackagingEnabledKey, m_packagingEnabled);
112
    map.insert(VersionNumberKey, m_versionString);
113
    return map;
ck's avatar
ck committed
114
115
116
117
}

bool MaemoPackageCreationStep::fromMap(const QVariantMap &map)
{
ck's avatar
ck committed
118
    m_packagingEnabled = map.value(PackagingEnabledKey, true).toBool();
119
    m_versionString = map.value(VersionNumberKey, DefaultVersionNumber).toString();
ck's avatar
ck committed
120
121
122
    return ProjectExplorer::BuildStep::fromMap(map);
}

123
124
void MaemoPackageCreationStep::run(QFutureInterface<bool> &fi)
{
ck's avatar
ck committed
125
    fi.reportResult(m_packagingEnabled ? createPackage() : true);
126
127
128
129
130
131
132
}

BuildStepConfigWidget *MaemoPackageCreationStep::createConfigWidget()
{
    return new MaemoPackageCreationWidget(this);
}

ck's avatar
ck committed
133
134
135
136
137
bool MaemoPackageCreationStep::createPackage()
{
    if (!packagingNeeded())
        return true;

138
    emit addOutput(tr("Creating package file ..."), BuildStep::MessageOutput);
ck's avatar
ck committed
139
140
    QFile configFile(targetRoot() % QLatin1String("/config.sh"));
    if (!configFile.open(QIODevice::ReadOnly)) {
141
142
        raiseError(tr("Cannot open MADDE config file '%1'.")
                   .arg(nativePath(configFile)));
ck's avatar
ck committed
143
144
        return false;
    }
kh1's avatar
kh1 committed
145

ck's avatar
ck committed
146
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
kh1's avatar
kh1 committed
147
148
149
    const QString &path = QDir::toNativeSeparators(maddeRoot() + QLatin1Char('/'));

    const QLatin1String key("PATH");
150
    QString colon = QLatin1String(":");
kh1's avatar
kh1 committed
151
#ifdef Q_OS_WIN
152
153
    colon = QLatin1String(";");
    env.insert(key, path % QLatin1String("bin") % colon % env.value(key));
kh1's avatar
kh1 committed
154
#endif
155

ck's avatar
ck committed
156
    env.insert(key, targetRoot() % "/bin" % colon % env.value(key));
kh1's avatar
kh1 committed
157
    env.insert(key, path % QLatin1String("madbin") % colon % env.value(key));
158
159
160
161
162
163
164

    QString perlLib = QDir::fromNativeSeparators(path % QLatin1String("madlib/perl5"));
#ifdef Q_OS_WIN
    perlLib = perlLib.remove(QLatin1Char(':'));
    perlLib = perlLib.prepend(QLatin1Char('/'));
#endif
    env.insert(QLatin1String("PERL5LIB"), perlLib);
kh1's avatar
kh1 committed
165

166
    const QString buildDir = buildDirectory();
167
    env.insert(QLatin1String("PWD"), buildDir);
kh1's avatar
kh1 committed
168
169

    const QRegExp envPattern(QLatin1String("([^=]+)=[\"']?([^;\"']+)[\"']? ;.*"));
ck's avatar
ck committed
170
171
172
173
174
175
    QByteArray line;
    do {
        line = configFile.readLine(200);
        if (envPattern.exactMatch(line))
            env.insert(envPattern.cap(1), envPattern.cap(2));
    } while (!line.isEmpty());
kh1's avatar
kh1 committed
176
    
177
178
179
180
181
182
183
184
185
186

    m_buildProc.reset(new QProcess);
    connect(m_buildProc.data(), SIGNAL(readyReadStandardOutput()), this,
        SLOT(handleBuildOutput()));
    connect(m_buildProc.data(), SIGNAL(readyReadStandardError()), this,
        SLOT(handleBuildOutput()));
    m_buildProc->setProcessEnvironment(env);
    m_buildProc->setWorkingDirectory(buildDir);
    m_buildProc->start("cd " + buildDir);
    m_buildProc->waitForFinished();
ck's avatar
ck committed
187

188
189
190
191
192
    // cache those two since we can change the version number during packaging
    // and might fail later to modify, copy, remove etc. the generated package
    const QString version = versionString();
    const QString pkgFilePath = packageFilePath();

193
    if (!QFileInfo(buildDir + QLatin1String("/debian")).exists()) {
ck's avatar
ck committed
194
        const QString command = QLatin1String("dh_make -s -n -p ")
ck's avatar
ck committed
195
            % projectName() % QLatin1Char('_') % versionString();
196
        if (!runCommand(command))
ck's avatar
ck committed
197
            return false;
198

199
        QFile rulesFile(buildDir + QLatin1String("/debian/rules"));
ck's avatar
ck committed
200
        if (!rulesFile.open(QIODevice::ReadWrite)) {
201
202
            raiseError(tr("Packaging Error: Cannot open file '%1'.")
                       .arg(nativePath(rulesFile)));
ck's avatar
ck committed
203
204
205
206
207
            return false;
        }

        QByteArray rulesContents = rulesFile.readAll();
        rulesContents.replace("DESTDIR", "INSTALL_ROOT");
ck's avatar
ck committed
208
        rulesContents.replace("dh_shlibdeps", "# dh_shlibdeps");
ck's avatar
ck committed
209

210
211
212
213
        // Would be the right solution, but does not work (on Windows),
        // because dpkg-genchanges doesn't know about it (and can't be told).
        // rulesContents.replace("dh_builddeb", "dh_builddeb --destdir=.");

ck's avatar
ck committed
214
215
216
        rulesFile.resize(0);
        rulesFile.write(rulesContents);
        if (rulesFile.error() != QFile::NoError) {
217
218
            raiseError(tr("Packaging Error: Cannot write file '%1'.")
                       .arg(nativePath(rulesFile)));
ck's avatar
ck committed
219
220
221
222
            return false;
        }
    }

223
    {
224
        QFile::remove(buildDir + QLatin1String("/debian/files"));
225
226
227
228
        QFile changeLog(buildDir + QLatin1String("/debian/changelog"));
        if (changeLog.open(QIODevice::ReadWrite)) {
            QString content = QString::fromUtf8(changeLog.readAll());
            content.replace(QRegExp("\\([a-zA-Z0-9_\\.]+\\)"),
229
                QLatin1Char('(') % version % QLatin1Char(')'));
230
231
232
233
234
            changeLog.resize(0);
            changeLog.write(content.toUtf8());
        }
    }

235
    if (!runCommand(QLatin1String("dpkg-buildpackage -nc -uc -us")))
ck's avatar
ck committed
236
        return false;
237

ck's avatar
ck committed
238
239
    // Workaround for non-working dh_builddeb --destdir=.
    if (!QDir(buildDir).isRoot()) {
240
        const QString packageFileName = QFileInfo(pkgFilePath).fileName();
ck's avatar
ck committed
241
242
243
244
245
246
247
248
249
        const QString changesFileName = QFileInfo(packageFileName)
            .completeBaseName() + QLatin1String(".changes");
        const QString packageSourceDir = buildDir + QLatin1String("/../");
        const QString packageSourceFilePath
            = packageSourceDir + packageFileName;
        const QString changesSourceFilePath
            = packageSourceDir + changesFileName;
        const QString changesTargetFilePath
            = buildDir + QLatin1Char('/') + changesFileName;
250
        QFile::remove(pkgFilePath);
ck's avatar
ck committed
251
        QFile::remove(changesTargetFilePath);
252
        if (!QFile::rename(packageSourceFilePath, pkgFilePath)
ck's avatar
ck committed
253
254
255
256
            || !QFile::rename(changesSourceFilePath, changesTargetFilePath)) {
            raiseError(tr("Packaging failed."),
                tr("Could not move package files from %1 to %2.")
                .arg(packageSourceDir, buildDir));
257
258
            return false;
        }
ck's avatar
ck committed
259
260
    }

261
    emit addOutput(tr("Package created."), BuildStep::MessageOutput);
262
    deployStep()->deployables()->setUnmodified();
ck's avatar
ck committed
263
264
265
    return true;
}

266
bool MaemoPackageCreationStep::runCommand(const QString &command)
ck's avatar
ck committed
267
{
268
    emit addOutput(tr("Package Creation: Running command '%1'.").arg(command), BuildStep::MessageOutput);
269
270
271
272
    QString perl;
#ifdef Q_OS_WIN
    perl = maddeRoot() + QLatin1String("/bin/perl.exe ");
#endif
273
274
    m_buildProc->start(perl + maddeRoot() % QLatin1String("/madbin/") % command);
    if (!m_buildProc->waitForStarted()) {
275
276
        raiseError(tr("Packaging failed."),
            tr("Packaging error: Could not start command '%1'. Reason: %2")
277
            .arg(command).arg(m_buildProc->errorString()));
278
279
        return false;
    }
280
281
282
    m_buildProc->write("\n"); // For dh_make
    m_buildProc->waitForFinished(-1);
    if (m_buildProc->error() != QProcess::UnknownError || m_buildProc->exitCode() != 0) {
283
284
        QString mainMessage = tr("Packaging Error: Command '%1' failed.")
            .arg(command);
285
286
        if (m_buildProc->error() != QProcess::UnknownError)
            mainMessage += tr(" Reason: %1").arg(m_buildProc->errorString());
ck's avatar
ck committed
287
        else
288
289
            mainMessage += tr("Exit code: %1").arg(m_buildProc->exitCode());
        raiseError(mainMessage);
ck's avatar
ck committed
290
291
292
293
294
        return false;
    }
    return true;
}

295
296
297
298
void MaemoPackageCreationStep::handleBuildOutput()
{
    const QByteArray &stdOut = m_buildProc->readAllStandardOutput();
    if (!stdOut.isEmpty())
299
        emit addOutput(QString::fromLocal8Bit(stdOut), BuildStep::NormalOutput);
300
301
    const QByteArray &errorOut = m_buildProc->readAllStandardError();
    if (!errorOut.isEmpty()) {
302
        emit addOutput(QString::fromLocal8Bit(errorOut), BuildStep::ErrorOutput);
303
304
305
    }
}

ck's avatar
ck committed
306
307
308
309
310
const Qt4BuildConfiguration *MaemoPackageCreationStep::qt4BuildConfiguration() const
{
    return static_cast<Qt4BuildConfiguration *>(buildConfiguration());
}

311
312
QString MaemoPackageCreationStep::buildDirectory() const
{
ck's avatar
ck committed
313
    return qt4BuildConfiguration()->buildDirectory();
314
315
}

ck's avatar
ck committed
316
QString MaemoPackageCreationStep::projectName() const
ck's avatar
ck committed
317
{
ck's avatar
ck committed
318
319
    return qt4BuildConfiguration()->qt4Target()->qt4Project()
        ->rootProjectNode()->displayName().toLower();
ck's avatar
ck committed
320
321
322
323
324
325
326
}

const MaemoToolChain *MaemoPackageCreationStep::maemoToolChain() const
{
    return static_cast<MaemoToolChain *>(qt4BuildConfiguration()->toolChain());
}

327
328
329
330
331
332
333
334
335
MaemoDeployStep *MaemoPackageCreationStep::deployStep() const
{
    MaemoDeployStep * const deployStep
        = MaemoGlobal::buildStep<MaemoDeployStep>(buildConfiguration());
    Q_ASSERT(deployStep &&
        "Fatal error: Maemo build configuration without deploy step.");
    return deployStep;
}

ck's avatar
ck committed
336
337
338
339
340
341
342
343
344
345
346
347
QString MaemoPackageCreationStep::maddeRoot() const
{
    return maemoToolChain()->maddeRoot();
}

QString MaemoPackageCreationStep::targetRoot() const
{
    return maemoToolChain()->targetRoot();
}

bool MaemoPackageCreationStep::packagingNeeded() const
{
348
    const MaemoDeployables * const deployables = deployStep()->deployables();
349
    QFileInfo packageInfo(packageFilePath());
350
    if (!packageInfo.exists() || deployables->isModified())
351
352
        return true;

353
    const int deployableCount = deployables->deployableCount();
354
    for (int i = 0; i < deployableCount; ++i) {
355
        if (packageInfo.lastModified()
356
            <= QFileInfo(deployables->deployableAt(i).localFilePath)
357
358
359
360
361
               .lastModified())
            return true;
    }

    return false;
ck's avatar
ck committed
362
363
}

364
365
QString MaemoPackageCreationStep::packageFilePath() const
{
ck's avatar
ck committed
366
    return buildDirectory() % '/' % projectName()
367
        % QLatin1Char('_') % versionString() % QLatin1String("_armel.deb");
368
369
370
}

QString MaemoPackageCreationStep::versionString() const
371
{
372
373
374
375
376
377
    return m_versionString;
}

void MaemoPackageCreationStep::setVersionString(const QString &version)
{
    m_versionString = version;
ck's avatar
ck committed
378
    emit packageFilePathChanged();
379
380
}

381
382
383
384
385
386
387
388
QString MaemoPackageCreationStep::nativePath(const QFile &file) const
{
    return QDir::toNativeSeparators(QFileInfo(file).filePath());
}

void MaemoPackageCreationStep::raiseError(const QString &shortMsg,
                                          const QString &detailedMsg)
{
389
    emit addOutput(detailedMsg.isNull() ? shortMsg : detailedMsg, BuildStep::ErrorOutput);
390
391
392
393
    emit addTask(Task(Task::Error, shortMsg, QString(), -1,
                      TASK_CATEGORY_BUILDSYSTEM));
}

394
395
396
397
const QLatin1String MaemoPackageCreationStep::CreatePackageId("Qt4ProjectManager.MaemoPackageCreationStep");

} // namespace Internal
} // namespace Qt4ProjectManager