summaryrefslogtreecommitdiffstats
path: root/tests/auto/qmake/testcompiler.cpp
blob: bfc8905fa9ddad4b349c3fbddca1363ff817f78c (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $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 either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** 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.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 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 the GNU General Public License version 3.0 requirements will be
** met: 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.
** $QT_END_LICENSE$
**
****************************************************************************/


#include "testcompiler.h"
#include <stdlib.h>
#include <qapplication.h>

#ifdef QT3_SUPPORT

#include <q3process.h>
#include <qtimer.h>
#ifdef Q_OS_WIN32
#   include <windows.h>
#endif

#include <QtTest/QtTest>

#undef SHOW_QDEBUG
#undef SHOW_COMPLETENESS

QString targetName( BuildType buildMode, const QString& target, const QString& version )
{
    QString targetName = target;

#if defined (Q_OS_WIN32)
    switch (buildMode)
    {
    case Exe: // app
	targetName.append(".exe");
	break;
    case Dll: // dll
	if (version != "") {
	    QStringList ver = QStringList::split(".", version);
	    targetName.append(ver.first());
	}
        targetName.append(".dll");
	break;
    case Lib: // lib
#ifdef Q_CC_GNU
        targetName.prepend("lib");
        targetName.append(".a");
#else
	targetName.append(".lib");
#endif
	break;
    case Plain:
        // no conversion
        break;
    }
#elif defined( Q_OS_MAC )
    switch (buildMode)
    {
    case Exe: // app
	targetName += ".app/Contents/MacOS/" + target.section('/', -1);
	break;
    case Dll: // dll
	targetName.prepend("lib");
        targetName.append("." + version + ".dylib");
	break;
    case Lib: // lib
	targetName.prepend("lib");
	targetName.append(".a");
	break;
    case Plain:
        // no conversion
        break;
    }
#else
    switch (buildMode)
    {
    case Exe: // app
	break;
    case Dll: // dll
	targetName.prepend("lib");
#if defined (Q_OS_HPUX) && !defined (__ia64)
        targetName.append(".sl");
#elif defined (Q_OS_AIX)
        targetName.append(".a");
#else
        targetName.append(".so");
#endif
	break;
    case Lib: // lib
	targetName.prepend("lib");
	targetName.append(".a");
	break;
    case Plain:
        // no conversion
        break;
    }
#endif
    return targetName;
}



TestCompiler::TestCompiler()
{
    exit_ok = FALSE;
    childProc = 0;
    setBaseCommands( "", "", FALSE );
}

TestCompiler::~TestCompiler()
{
    if (childProc)
	delete childProc;
}

bool TestCompiler::runChild( bool showOutput, QStringList argList, QStringList *envList )
{
    //qDebug() << "executing" << argList;
    exit_ok = FALSE;
    if (childProc)
	delete childProc;

    child_show = showOutput;
    if ( showOutput ) {

	QString S = argList.join(" ");
	addMakeResult( S );
    }

    childProc = new Q3Process(argList, this, argList.join(" ").latin1());
    Q_ASSERT(childProc);

    connect(childProc,SIGNAL(readyReadStdout()),this,SLOT(childHasData()));
    connect(childProc,SIGNAL(processExited()),this,SLOT(childReady()));

    if (!childProc->start( envList )) {

	addMakeResult( "Error executing '" + argList[0] + "'." );
	childReady();
	return FALSE;
    }

    while (childProc != 0 && childProc->isRunning()) {
	qApp->processEvents();
    }

    childReady();

    return exit_ok;
}

void TestCompiler::childReady()
{
    if (childProc != 0) {
	childHasData();

	QString S;
	int pos;
	while (childProc->canReadLineStderr()) {
	    S = childProc->readLineStderr();
	    do {
		pos = S.find("\t");
		if (pos >= 0) {
		    S.remove(pos,1);
		    S.insert(pos,"    ");
		}
	    } while (pos >= 0);

	    if (child_show)
		addMakeResult( S );
	}

	exit_ok = childProc->normalExit() && childProc->exitStatus() == 0;
	delete childProc;
    }
    childProc = 0;
}

void TestCompiler::childHasData()
{
    QString S;
    int pos;
    while (childProc->canReadLineStderr()) {

	S = childProc->readLineStderr();
	do {
	    pos = S.find("\t");
	    if (pos >= 0) {
		S.remove(pos,1);
		S.insert(pos,"    ");
	    }

	} while (pos >= 0);

        if ( S.startsWith("Project MESSAGE: FAILED") )
	    QTest::qFail( S, __FILE__, __LINE__ );
        else if ( S.startsWith("Project MESSAGE: SKIPPED") )
            QTest::qSkip( S, QTest::SkipSingle, __FILE__, __LINE__ );
	else if (child_show)
	    addMakeResult( S );
    }
}

void TestCompiler::setBaseCommands( QString makeCmd, QString qmakeCmd, bool qwsMode )
{
    qws_mode = qwsMode;
    make_cmd = makeCmd;

    // not sure if i need this, but it doesn't hurt
    if (make_cmd.startsWith("\""))
        make_cmd = make_cmd.remove(0,1);
    if (make_cmd.endsWith("\""))
        make_cmd = make_cmd.remove(make_cmd.length()-1,1);

    qmake_cmd = qmakeCmd;
    // also not sure if i need this, but it doesn't hurt...
    if(qmake_cmd.length() >= 2 && (qmake_cmd.at(0) == '"' || qmake_cmd.at(0) == '\'') && qmake_cmd.at(qmake_cmd.length()-1) == qmake_cmd.at(0))
        qmake_cmd = qmake_cmd.mid(1, qmake_cmd.length()-2);
}

bool TestCompiler::cleanAll( const QString &workPath, const QString &destPath, const QString &exeName, const QString &exeExt )
{
    QDir D(workPath);
    if (!D.exists()) {

        addMakeResult( "Directory '" + workPath + "' doesn't exist" );
        return FALSE;
    }

    D.setCurrent(workPath);
    // must delete at least the executable file to be able to easily and safely
    // verify that the compilation was a success.
    D.remove( destPath + "/" + exeName + exeExt );
    D.remove( workPath + "/Makefile");
    QFileInfo Fi( workPath + "/Makefile");
    if (Fi.exists()) {

	// Run make clean
	QStringList args;
        args = QStringList::split( " ", make_cmd );
	args.append("clean");

	return runChild( FALSE, args, 0 );
    }

    return TRUE;
}

bool TestCompiler::makeClean( const QString &workPath )
{
    QDir D;
    if (!D.exists(workPath)) {

        addMakeResult( "Directory '" + workPath + "' doesn't exist" );
        return FALSE;
    }

    D.setCurrent(workPath);
    QFileInfo Fi( workPath + "/Makefile");
    if (Fi.exists()) {

	// Run make clean
	QStringList args;
        args = QStringList::split( " ", make_cmd );
	args.append("clean");

	return runChild( FALSE, args, 0 );
    }

    return TRUE;
}

bool TestCompiler::makeDistClean( const QString &workPath )
{
    QDir D;
    if (!D.exists(workPath)) {
        addMakeResult( "Directory '" + workPath + "' doesn't exist" );
        return FALSE;
    }

    D.setCurrent(workPath);
    QFileInfo Fi( workPath + "/Makefile");
    if (Fi.exists()) {
	// Run make distclean
	QStringList args;
        args = QStringList::split( " ", make_cmd );
	args.append("distclean");

	return runChild( FALSE, args, 0 );
    }

    return TRUE;

}

bool TestCompiler::qmake( const QString &workDir, const QString &proName, const QString &buildDir )
{
    // Now start qmake and generate the makefile

    QDir D( workDir );
    // Make sure we start in the right directory
    D.setCurrent( workDir );

    if (D.exists("Makefile"))
	D.remove("Makefile");

    QStringList args;
    args = QStringList::split( " ", qmake_cmd );

    QString project_fname = workDir + "/" + proName + ".pro";
    QString makefile_fname = (buildDir.isNull()?QString():(buildDir + "/")) + "Makefile";

    args.append( project_fname );
    args.append( "-o" );
    args.append( makefile_fname );

    return runChild( TRUE, args, 0 );
}

bool TestCompiler::make( const QString &workPath, const QString &target )
{
    QDir D;
    D.setCurrent( workPath );

    QStringList args;
    args = QStringList::split( " ", make_cmd );
    if ( make_cmd.lower().find("nmake") >= 0)
	args.append("/NOLOGO");
    if ( !target.isNull() )
        args.append(target);

    return runChild( TRUE, args, 0 );
}

bool TestCompiler::exists( const QString &destDir, const QString &exeName, BuildType buildType, const QString &version )
{
    QFileInfo f(destDir + "/" + targetName(buildType, exeName, version));
    return f.exists();
}

void TestCompiler::addMakeResult( const QString &result )
{
    make_result.append( result );
}

bool TestCompiler::removeMakefile( const QString &workPath )
{
    QDir D;
    D.setCurrent( workPath );
    if ( D.exists( "Makefile" ) )
	return D.remove( "Makefile" );
    else
	return TRUE;
}

#endif //QT3_SUPPORT