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
391
392
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qbs.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** 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-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "setupqt.h"
#include "../shared/logging/consolelogger.h"
#include <logging/translator.h>
#include <tools/architectures.h>
#include <tools/hostosinfo.h>
#include <tools/profile.h>
#include <tools/set.h>
#include <tools/settings.h>
#include <tools/stlutils.h>
#include <tools/toolchains.h>
#include <tools/version.h>
#include <QtCore/qbytearraymatcher.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qdir.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qprocess.h>
#include <QtCore/qstringlist.h>
#include <algorithm>
#include <regex>
namespace qbs {
using Internal::none_of;
using Internal::contains;
using Internal::HostOsInfo;
using Internal::Tr;
using Internal::rangeTo;
static QStringList qmakeExecutableNames()
{
const QString baseName = HostOsInfo::appendExecutableSuffix(QStringLiteral("qmake"));
QStringList lst(baseName);
if (HostOsInfo::isLinuxHost()) {
// Some distributions ship binaries called qmake-qt5 or qmake-qt4.
lst << baseName + QLatin1String("-qt5") << baseName + QLatin1String("-qt4");
}
return lst;
}
static QStringList collectQmakePaths()
{
const QStringList qmakeExeNames = qmakeExecutableNames();
QStringList qmakePaths;
QByteArray environmentPath = qgetenv("PATH");
const QList<QByteArray> environmentPaths
= environmentPath.split(HostOsInfo::pathListSeparator().toLatin1());
for (const QByteArray &path : environmentPaths) {
for (const QString &qmakeExecutableName : qmakeExeNames) {
QFileInfo pathFileInfo(QDir(QLatin1String(path)), qmakeExecutableName);
if (pathFileInfo.exists()) {
QString qmakePath = pathFileInfo.absoluteFilePath();
if (!qmakePaths.contains(qmakePath))
qmakePaths.push_back(qmakePath);
}
}
}
return qmakePaths;
}
bool SetupQt::isQMakePathValid(const QString &qmakePath)
{
QFileInfo qmakeFileInfo(qmakePath);
return qmakeFileInfo.exists() && qmakeFileInfo.isFile() && qmakeFileInfo.isExecutable();
}
std::vector<QtEnvironment> SetupQt::fetchEnvironments()
{
std::vector<QtEnvironment> qtEnvironments;
const auto qmakePaths = collectQmakePaths();
for (const QString &qmakePath : qmakePaths) {
const QtEnvironment env = fetchEnvironment(qmakePath);
if (none_of(qtEnvironments, [&env](const QtEnvironment &otherEnv) {
return env.qmakeFilePath == otherEnv.qmakeFilePath;
})) {
qtEnvironments.push_back(env);
}
}
return qtEnvironments;
}
// These functions work only for Qt from installer.
static QStringList qbsToolchainFromDirName(const QString &dir)
{
if (dir.startsWith(QLatin1String("msvc")))
return {QStringLiteral("msvc")};
if (dir.startsWith(QLatin1String("mingw")))
return {QStringLiteral("mingw"), QStringLiteral("gcc")};
if (dir.startsWith(QLatin1String("clang")))
return {QStringLiteral("clang"), QStringLiteral("llvm"), QStringLiteral("gcc")};
if (dir.startsWith(QLatin1String("gcc")))
return {QStringLiteral("gcc")};
return {};
}
static Version msvcVersionFromDirName(const QString &dir)
{
static const std::regex regexp("^msvc(\\d\\d\\d\\d).*$");
std::smatch match;
const std::string dirString = dir.toStdString();
if (!std::regex_match(dirString, match, regexp))
return Version{};
QMap<std::string, std::string> mapping{
std::make_pair("2005", "14"), std::make_pair("2008", "15"), std::make_pair("2010", "16"),
std::make_pair("2012", "17"), std::make_pair("2013", "18"), std::make_pair("2015", "19"),
std::make_pair("2017", "19.1"), std::make_pair("2019", "19.2")
};
return Version::fromString(QString::fromStdString(mapping.value(match[1].str())));
}
static QString archFromDirName(const QString &dir)
{
static const std::regex regexp("^[^_]+_(.*).*$");
std::smatch match;
const std::string dirString = dir.toStdString();
if (!std::regex_match(dirString, match, regexp))
return {};
const QString arch = QString::fromStdString(match[1]);
if (arch == QLatin1String("32"))
return QStringLiteral("x86");
if (arch == QLatin1String("64"))
return QStringLiteral("x86_64");
if (arch.contains(QLatin1String("arm64")))
return QStringLiteral("arm64");
return arch;
}
static QString platformFromDirName(const QString &dir)
{
if (dir.startsWith(QLatin1String("android")))
return QStringLiteral("android");
if (dir == QLatin1String("Boot2Qt"))
return QStringLiteral("linux");
return QString::fromStdString(HostOsInfo::hostOSIdentifier());
}
QtEnvironment SetupQt::fetchEnvironment(const QString &qmakePath)
{
QtEnvironment env;
env.qmakeFilePath = qmakePath;
QDir qtDir = QFileInfo(qmakePath).dir();
if (qtDir.dirName() == QLatin1String("bin")) {
qtDir.cdUp();
env.qbsToolchain = qbsToolchainFromDirName(qtDir.dirName());
env.msvcVersion = msvcVersionFromDirName(qtDir.dirName());
env.architecture = archFromDirName(qtDir.dirName());
if (env.msvcVersion.isValid() && env.architecture.isEmpty())
env.architecture = QStringLiteral("x86");
env.targetPlatform = platformFromDirName(qtDir.dirName());
qtDir.cdUp();
env.qtVersion = Version::fromString(qtDir.dirName());
}
return env;
}
static bool isToolchainProfile(const Profile &profile)
{
const auto actual = rangeTo<Internal::Set<QString>>(
profile.allKeys(Profile::KeySelectionRecursive));
Internal::Set<QString> expected{ QStringLiteral("qbs.toolchainType") };
if (HostOsInfo::isMacosHost())
expected.insert(QStringLiteral("qbs.targetPlatform")); // match only Xcode profiles
return Internal::Set<QString>(actual).unite(expected) == actual;
}
static bool isQtProfile(const Profile &profile)
{
if (!profile.value(QStringLiteral("moduleProviders.Qt.qmakeFilePaths")).toStringList()
.empty()) {
return true;
}
// For Profiles created with setup-qt < 5.13.
const QStringList searchPaths
= profile.value(QStringLiteral("preferences.qbsSearchPaths")).toStringList();
return Internal::any_of(searchPaths, [] (const QString &path) {
return QFileInfo(path + QStringLiteral("/modules/Qt")).isDir();
});
}
template <typename T> bool areProfilePropertiesIncompatible(const T &set1, const T &set2)
{
// Two objects are only considered incompatible if they are both non empty and compare inequal
// This logic is used for comparing target OS, toolchain lists, and architectures
return set1.size() > 0 && set2.size() > 0 && set1 != set2;
}
enum Match { MatchFull, MatchPartial, MatchNone };
static Match compatibility(const QtEnvironment &env, const Profile &toolchainProfile)
{
Match match = MatchFull;
const auto toolchainType =
toolchainProfile.value(QStringLiteral("qbs.toolchainType")).toString();
const auto toolchain = !toolchainType.isEmpty()
? canonicalToolchain(toolchainType)
: toolchainProfile.value(QStringLiteral("qbs.toolchain")).toStringList();
const auto toolchainNames = rangeTo<Internal::Set<QString>>(toolchain);
const auto qtToolchainNames = rangeTo<Internal::Set<QString>>(env.qbsToolchain);
if (areProfilePropertiesIncompatible(toolchainNames, qtToolchainNames)) {
auto intersection = toolchainNames;
intersection.intersect(qtToolchainNames);
if (!intersection.empty())
match = MatchPartial;
else
return MatchNone;
}
const auto targetPlatform = toolchainProfile.value(
QStringLiteral("qbs.targetPlatform")).toString();
if (!targetPlatform.isEmpty() && targetPlatform != env.targetPlatform)
return MatchNone;
const QString toolchainArchitecture = toolchainProfile.value(QStringLiteral("qbs.architecture"))
.toString();
if (areProfilePropertiesIncompatible(canonicalArchitecture(env.architecture),
canonicalArchitecture(toolchainArchitecture)))
return MatchNone;
if (env.msvcVersion.isValid()) {
// We want to know for sure that MSVC compiler versions match,
// because it's especially important for this toolchain
const Version compilerVersion = Version::fromString(
toolchainProfile.value(QStringLiteral("cpp.compilerVersion")).toString());
static const Version vs2017Version{19, 1};
if (env.msvcVersion >= vs2017Version) {
if (env.msvcVersion.majorVersion() != compilerVersion.majorVersion()
|| compilerVersion < vs2017Version) {
return MatchNone;
}
} else if (env.msvcVersion.majorVersion() != compilerVersion.majorVersion()
|| env.msvcVersion.minorVersion() != compilerVersion.minorVersion()) {
return MatchNone;
}
}
return match;
}
QString profileNameWithoutHostArch(const QString &profileName)
{
QString result;
int i = profileName.indexOf(QLatin1Char('-'));
if (i == -1)
return result;
++i;
int j = profileName.indexOf(QLatin1Char('_'), i);
if (j == -1)
return result;
result = profileName.mid(0, i) + profileName.mid(j + 1);
return result;
}
// "Compressing" MSVC profiles means that if MSVC2017-x64 and MSVC2017-x86_x64 fully match,
// then we drop the crosscompiling toolchain MSVC2017-x86_x64.
static void compressMsvcProfiles(QStringList &profiles)
{
auto it = std::remove_if(profiles.begin(), profiles.end(),
[&profiles] (const QString &profileName) {
int idx = profileName.indexOf(QLatin1Char('_'));
if (idx == -1)
return false;
return contains(profiles, profileNameWithoutHostArch(profileName));
});
if (it != profiles.end())
profiles.erase(it, profiles.end());
}
void SetupQt::saveToQbsSettings(const QString &qtVersionName,
const QtEnvironment &qtEnvironment,
Settings *settings)
{
const QString cleanQtVersionName = Profile::cleanName(qtVersionName);
QString msg = QCoreApplication::translate("SetupQt", "Creating profile '%1'.")
.arg(cleanQtVersionName);
std::printf("%s\n", qPrintable(msg));
Profile profile(cleanQtVersionName, settings);
profile.removeProfile();
profile.setValue(QStringLiteral("moduleProviders.Qt.qmakeFilePaths"),
QStringList(qtEnvironment.qmakeFilePath));
if (!profile.baseProfile().isEmpty())
return;
if (isToolchainProfile(profile))
return;
QStringList fullMatches;
QStringList partialMatches;
const auto profileNames = settings->profiles();
for (const QString &profileName : profileNames) {
const Profile otherProfile(profileName, settings);
if (profileName == profile.name()
|| !isToolchainProfile(otherProfile)
|| isQtProfile(otherProfile))
continue;
switch (compatibility(qtEnvironment, otherProfile)) {
case MatchFull:
fullMatches << profileName;
break;
case MatchPartial:
partialMatches << profileName;
break;
default:
break;
}
}
if (fullMatches.size() > 1)
compressMsvcProfiles(fullMatches);
QString bestMatch;
if (fullMatches.size() == 1)
bestMatch = fullMatches.front();
else if (fullMatches.empty() && partialMatches.size() == 1)
bestMatch = partialMatches.front();
if (bestMatch.isEmpty()) {
QString message = Tr::tr("You may want to set up toolchain information "
"for the generated Qt profile. ");
if (!fullMatches.empty() || !partialMatches.empty()) {
message += Tr::tr("Consider setting one of these profiles as this profile's base "
"profile: %1.").arg((fullMatches + partialMatches)
.join(QLatin1String(", ")));
}
qbsInfo() << message;
} else {
profile.setBaseProfile(bestMatch);
qbsInfo() << Tr::tr("Setting profile '%1' as the base profile for this profile.")
.arg(bestMatch);
}
}
bool SetupQt::checkIfMoreThanOneQtWithTheSameVersion(const Version &qtVersion,
const std::vector<QtEnvironment> &qtEnvironments)
{
bool foundOneVersion = false;
for (const QtEnvironment &qtEnvironment : qtEnvironments) {
if (qtEnvironment.qtVersion == qtVersion) {
if (foundOneVersion)
return true;
foundOneVersion = true;
}
}
return false;
}
} // namespace qbs
|