summaryrefslogtreecommitdiffstats
path: root/tests/postbuild/global.h
blob: 86c6666d213db7a0eb5110eadf5509706dc19311 (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
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
// Helper functions for global test cases.

#ifndef QT_TESTS_SHARED_GLOBAL_H_INCLUDED
#define QT_TESTS_SHARED_GLOBAL_H_INCLUDED

#include <QtCore/QtCore>
#include <QtTest/QtTest>

QStringList qt_tests_shared_global_get_include_path(const QString &makeFile);
QHash<QString, QString> qt_tests_shared_global_get_modules(const QString &configFile);
QStringList qt_tests_shared_global_get_include_paths();

QHash<QString, QString> qt_tests_shared_global_get_modules(const QString &configFile)
{
    QHash<QString, QString> modules;

    QFile file(configFile);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QWARN("Can't open the config file for global.cfg.");
        return modules;
    }

    QXmlStreamReader xml;
    xml.setDevice(&file);

    while (!xml.atEnd()) {
        xml.readNext();
        if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "config") {
            xml.readNextStartElement();
            if (xml.name() == "modules") {
                while (!xml.atEnd()) {
                    xml.readNextStartElement();
                    QString modName;
                    QString qtModName;
                    if (xml.name() == "module") {
                        modName = xml.attributes().value("name").toString().simplified();
                        qtModName = xml.attributes().value("qtname").toString().simplified();
                        if (!modName.isEmpty() && !qtModName.isEmpty())
                            modules[modName] = qtModName;
                    }
                }
            }
        }
    }

    file.close();

    qDebug() << "modules keys:" << modules.keys();
    qDebug() << "modules values:" << modules.values();

    return modules;
}

QByteArray qt_tests_shared_global_get_modules_pro_lines(const QHash<QString, QString> &modules)
{
    QByteArray result;
    foreach (QString moduleName, modules.values()) {
        QByteArray module = moduleName.toLatin1();
        result += "qtHaveModule(" + module + ") {\n" +
                  "    QT += " + module + "\n" +
                  "}\n";
    }
    return result;
}

QStringList qt_tests_shared_global_get_include_paths(const QString &workDir, QHash<QString, QString> &modules)
{
    QString proFile = workDir + "/global.pro";
    QString makeFile = workDir + "/Makefile";

    QStringList incPaths;

#ifndef QT_NO_PROCESS
    QFile file(proFile);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QWARN("Can't open the pro file for global.");
        return incPaths;
    }

    QByteArray proLines = qt_tests_shared_global_get_modules_pro_lines(modules);
    file.write(proLines);
    file.flush();
    file.close();

    if (!QDir::setCurrent(workDir)) {
        QWARN("Change working dir failed.");
        return incPaths;
    }

    QString qmakeApp = "qmake";

    QStringList qmakeArgs;
    qmakeArgs << "-o"
              << "Makefile";

    QProcess proc;
    proc.start(qmakeApp, qmakeArgs, QIODevice::ReadOnly);
    if (!proc.waitForFinished(6000000)) {
        qWarning() << qmakeApp << qmakeArgs << "in" << workDir << "didn't finish" << proc.errorString();
        return incPaths;
    }
    if (proc.exitCode() != 0) {
        qWarning() << qmakeApp << qmakeArgs << "in" << workDir << "returned with" << proc.exitCode();
        qDebug() << proc.readAllStandardError();
        return incPaths;
    }

    QFile::remove(proFile);

    incPaths = qt_tests_shared_global_get_include_path(makeFile);
#ifdef Q_OS_WIN
    if (incPaths.isEmpty())
        incPaths = qt_tests_shared_global_get_include_path(makeFile + QLatin1String(".Release"));
#endif

    QFile::remove(makeFile);
#else
    Q_UNUSED(modules);
#endif // QT_NO_PROCESS

    return incPaths;
}

QStringList qt_tests_shared_global_get_include_path(const QString &makeFile)
{
    QFile file(makeFile);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return QStringList();

    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        if (line.contains("=") && line.contains("INCPATH")) {
            QString relatives = line.mid(line.indexOf("=")+1);
            QStringList list1 = relatives.split(" ");
            QStringList list2;
            for (int i = 0; i < list1.size(); ++i) {
                if (!list1.at(i).startsWith("-I"))
                    continue;
                QString rpath = list1.at(i).mid(2);
                QString apath = "-I" + QDir(rpath).absolutePath();
#ifdef Q_OS_WIN
                apath.replace('\\', '/');
#endif
                list2 << apath;
            }
            return list2;
        }
    }

    return QStringList();
}

#endif // QT_TESTS_SHARED_GLOBAL_H_INCLUDED