File: qmakejob.cpp

package info (click to toggle)
kdevelop 4%3A5.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 52,544 kB
  • sloc: cpp: 254,897; python: 3,380; sh: 1,271; ansic: 657; xml: 221; php: 95; makefile: 36; lisp: 13; sed: 12
file content (159 lines) | stat: -rw-r--r-- 5,448 bytes parent folder | download | duplicates (2)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/***************************************************************************
 *   This file is part of KDevelop                                         *
 *   Copyright 2010 Milian Wolff <mail@milianw.de>                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "qmakejob.h"
#include <QDir>
#include <QProcess>
#include <KLocalizedString>
#include <KShell>
#include <util/processlinemaker.h>
#include <outputview/outputmodel.h>
#include <outputview/ioutputview.h>
#include <debug.h>

using namespace KDevelop;

QMakeJob::QMakeJob(QString srcDir, QString buildDir, QObject* parent)
    : OutputJob(parent)
    , m_srcDir(std::move(srcDir))
    , m_buildDir(std::move(buildDir))
    , m_qmakePath(QStringLiteral("qmake"))
    , m_buildType(0)
    , m_process(nullptr)
    , m_model(nullptr)

{
    setCapabilities(Killable);
    setStandardToolView(IOutputView::RunView);
    setBehaviours(IOutputView::AllowUserClose | IOutputView::AutoScroll);

    setObjectName(i18n("Run QMake in %1", m_buildDir));
}

QMakeJob::~QMakeJob()
{
}

void QMakeJob::setQMakePath(const QString& path)
{
    m_qmakePath = path;
}

void QMakeJob::setInstallPrefix(const QString& prefix)
{
    m_installPrefix = prefix;
}

void QMakeJob::setBuildType(int comboboxSelectedIndex)
{
    m_buildType = comboboxSelectedIndex;
}

void QMakeJob::setExtraArguments(const QString& args)
{
    m_extraArguments = args;
}

void QMakeJob::start()
{
    static const char* const BUILD_TYPES[] = { "debug", "build", "(don't specify)" };

    m_model = new OutputModel;
    setModel(m_model);

    startOutput();

    QStringList args;
    if (m_buildType < 2)
        args << QLatin1String("CONFIG+=") + QLatin1String(BUILD_TYPES[m_buildType]);
    if (!m_installPrefix.isEmpty())
        args << QLatin1String("target.path=") + m_installPrefix;
    if (!m_extraArguments.isEmpty()) {
        KShell::Errors err;
        QStringList tmp = KShell::splitArgs(m_extraArguments, KShell::TildeExpand | KShell::AbortOnMeta, &err);
        if (err == KShell::NoError) {
            args += tmp;
        } else {
            qCWarning(KDEV_QMAKE) << "Ignoring qmake Extra arguments";
            if (err == KShell::BadQuoting) {
                qCWarning(KDEV_QMAKE) << "QMake arguments badly quoted:" << m_extraArguments;
            } else {
                qCWarning(KDEV_QMAKE) << "QMake arguments had meta character:" << m_extraArguments;
            }
        }
    }
    args << QStringLiteral("-r") << m_srcDir;

    m_model->appendLine(m_buildDir + QLatin1String(": ") + args.join(QLatin1Char(' ')));

    QDir build(m_buildDir);
    if (!build.exists()) {
        build.mkpath(build.absolutePath());
    }

    m_process = new QProcess(this);
    m_process->setWorkingDirectory(m_buildDir);
    m_process->setProgram(m_qmakePath);
    m_process->setArguments(args);
    m_process->setProcessChannelMode(QProcess::MergedChannels);
    auto lineMaker = new KDevelop::ProcessLineMaker(m_process, this);

    connect(lineMaker, &ProcessLineMaker::receivedStdoutLines, m_model, &OutputModel::appendLines);
    connect(lineMaker, &ProcessLineMaker::receivedStderrLines, m_model, &OutputModel::appendLines);
    connect(m_process, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
    connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(processFinished(int,QProcess::ExitStatus)));

    m_process->start();
}

bool QMakeJob::doKill()
{
    if (!m_process) {
        return true;
    }

    m_process->kill();
    return m_process->state() == m_process->NotRunning;
}

QString QMakeJob::errorString() const
{
    return m_process->errorString();
}

void QMakeJob::processError(QProcess::ProcessError error)
{
    m_model->appendLine(errorString());
    setError(error);
    emitResult();
}

void QMakeJob::processFinished(int exitCode, QProcess::ExitStatus status)
{
    if (status == QProcess::NormalExit) {
        m_model->appendLine(i18n("*** Exited with return code: %1 ***", exitCode));
    } else if (error() == KJob::KilledJobError) {
        m_model->appendLine(i18n("*** Process aborted ***"));
    } else {
        m_model->appendLine(i18n("*** Crashed with return code: %1 ***", exitCode));
    }

    emitResult();
}