summaryrefslogtreecommitdiffstats
path: root/src/buildrun.cpp
blob: 41417afb68a8f68a2750f783b01a2451a59f6cec (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QTestLib project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "buildrun.h"
#include "benchmark.h"


int runExecutable(const QString &workdir, const QString &executable, const QStringList &arguments = QStringList())
{
    QProcess p;
    p.setProcessChannelMode(QProcess::ForwardedChannels);
    if (workdir != QString())
        p.setWorkingDirectory(workdir);
    
    if (arguments == QStringList())
        p.start(executable);
    else
        p.start(executable, arguments);

    if (p.waitForFinished(-1) == false) {
        qDebug() << "run" << executable << "failed" << p.error();
        return -1;
    }
  //  qDebug() << p.exitCode();
    return p.exitCode();
}

RunStatus runExecutableEx(const QString &workdir, const QString &executable, const QStringList &arguments = QStringList())
{
    QProcess p;
    RunStatus status;
//    qDebug() << workdir << executable << arguments;
    p.setProcessChannelMode(QProcess::MergedChannels);
//    QDir previousWoringDirectory = QDir::current();

    if (workdir != QString())
     //   QDir::setCurrent(workdir);
        p.setWorkingDirectory(workdir);
    
    if (arguments == QStringList())
        p.start(executable);
    else
        p.start(executable, arguments);

    if (p.waitForFinished(-1) == false) {
        status.error = RunStatus::NotFoundError;
        status.output = "Running " + executable.toAscii() + " failed with error string: " + p.errorString().toAscii();
//        QDir::setCurrent(previousWoringDirectory.path());
        return status;
    }
    status.output = p.readAll();
    int code = p.exitCode();
    if (code != 0) {
//        qDebug() << "fail",    
        status.error = RunStatus::RuntimeError;
    }

//    QDir::setCurrent(previousWoringDirectory.path());
    return status;
}



QByteArray pipeExecutable(const QString &workdir, const QString &executable, const QStringList &arguments = QStringList())
{
    QProcess p;
    p.setProcessChannelMode(QProcess::MergedChannels);
    if (workdir != QString())
        p.setWorkingDirectory(workdir);
    p.start(executable, arguments);
    if (p.waitForFinished(-1) == false) {
        qDebug() << "run" << executable << "failed" << p.error();
    }
    return p.readAll();
}

// returns false on timeout
bool forwardExecutable(const QString &workdir, const QString &executable, int timeout, const QStringList &arguments, const QString &pathAddition)
{
    QProcess p;
    p.setProcessChannelMode(QProcess::ForwardedChannels);

    if (pathAddition != QString()) {
        QStringList env = QProcess::systemEnvironment();
        env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=" + pathAddition + ";\\1");
        p.setEnvironment(env);
    }
    
    if (workdir != QString())
        p.setWorkingDirectory(workdir);
    p.start(executable, arguments);

    if (p.waitForStarted(500) == false) {
        qDebug() << QString("run " + executable + " failed to start");
        return true;
    }

    if (p.waitForFinished(timeout) == false) {
        if (p.error() == QProcess::Timedout) {
            p.kill();
            p.waitForFinished(-1);
            return false;
        }
    }

    return true;
}

bool runBenchmark(const QString &qtDir, const QString &workdir, const QString &executable, int timeout, const QStringList &arguments)
{
    QString qtDirCopy = qtDir;
    if (qtDirCopy.isEmpty() == false)
        qtDirCopy += "/bin";
    return forwardExecutable(workdir, executable, timeout, arguments, qtDirCopy);
}

QHash<QString, QString> systemEnvitonment()
{
    QHash<QString, QString> keyValues;
    foreach(QString line, QProcess().systemEnvironment()) {
        QStringList parts = line.split("=");
        keyValues.insert(parts.at(0), parts.at(1));
    }
    return keyValues;
}


RunStatus qmake(const QString &path, const QString &qmakeBinaryPath, const QString &target)
{
    QStringList arguments;
    if (target != QString()) {
        arguments += "-after";
        arguments += "TARGET=" + target;
    }

    return runExecutableEx(path, qmakeBinaryPath, arguments);
}

RunStatus runMake(const QString &path, const QString &argument)
{
//    qDebug() << "make in" << path << "args" << argument;
    QStringList arguments;
    if (argument != QString())
        arguments.append(argument);

#ifdef Q_WS_WIN
    // ### do this the right way :)

    QStringList possibleMakePaths = QStringList() 
        << "C:/Programfiler/Microsoft Visual Studio 8/VC/bin/nmake.exe"
        << "C:/Program Files/Microsoft Visual Studio 8/VC/bin/nmake.exe"
    ;
    
    foreach (QString nmake, possibleMakePaths) {
        if (QFile::exists(nmake))
            return runExecutableEx(path, nmake, QStringList() << argument);
    }
    RunStatus status;
    status.error = RunStatus::NotFoundError;
    status.output = "Cold not find qmake";
    return status;
 #else
    return (runExecutableEx(path, "/usr/bin/make", arguments));
#endif
}

RunStatus buildTest(const QString &path, const QString &qmakePath, const QString &target)
{
//    qDebug() << "building test in path" << path  << "with qmake" << qmakePath;

    RunStatus status = qmake(path, qmakePath, target);
   if (status.ok() == false)
        return status;

    runMake(path, "clean");

    return runMake(path);
}

QByteArray runTest(const QString &path, const QString &executable, const QString &arg)
{
    QStringList args;
    if (arg != QString())
        args.append(arg);

    return runTest(path, executable, args);
}

QByteArray runTest(const QString &path, const QString &executable, const QStringList &args)
{
    qDebug() << "running test" << path << executable;
    return pipeExecutable(path, path + "/" + executable, args);
}

QByteArray pipeP4sync(const QString &path)
{
    return pipeExecutable(path, "p4", QStringList() << QString("sync") << QString("..."));
}

void p4sync(const QString &path)
{
    pipeExecutable(path, "p4", QStringList() << QString("sync") << QString("..."));
}

RunStatus buildBenchmark(const QString &benchmarkPath, const QString &qtPath)
{
    Benchmark b;
    return b.buildBenchmark(qtPath, benchmarkPath, QString());
}

void runBenchmark(const QString &benchmarkPath, const QStringList &options)
{
    QString partialName = QDir(benchmarkPath).dirName();
    QString testExecutable = findExecutable(benchmarkPath, partialName);
    QString testExecutablePath = benchmarkPath + "/" + testExecutable;
    runBenchmark(QString(), benchmarkPath, testExecutablePath, -1 , options);
}