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
|
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Build Suite.
**
** 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 Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://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 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "qtprofilesetup.h"
#include "qtmoduleinfo.h"
#include <logging/translator.h>
#include <tools/error.h>
#include <tools/fileinfo.h>
#include <tools/profile.h>
#include <tools/scripttools.h>
#include <tools/settings.h>
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QLibrary>
#include <QRegExp>
#include <QTextStream>
namespace qbs {
using namespace Internal;
template <class T>
QByteArray utf8JSLiteral(T t)
{
return toJSLiteral(t).toUtf8();
}
static QString pathToJSLiteral(const QString &path)
{
return toJSLiteral(QDir::fromNativeSeparators(path));
}
static QString defaultQpaPlugin(const Profile &profile, const QtModuleInfo &module,
const QtEnvironment &qtEnv)
{
if (qtEnv.qtMajorVersion < 5)
return QString();
QFile qConfigPri(qtEnv.mkspecBasePath + QLatin1String("/qconfig.pri"));
if (!qConfigPri.open(QIODevice::ReadOnly)) {
throw ErrorInfo(Tr::tr("Setting up Qt profile '%1' failed: Cannot open "
"file '%2' (%3).")
.arg(profile.name(), qConfigPri.fileName(), qConfigPri.errorString()));
}
const QList<QByteArray> lines = qConfigPri.readAll().split('\n');
const QByteArray magicString = "QT_DEFAULT_QPA_PLUGIN =";
foreach (const QByteArray &line, lines) {
const QByteArray simplifiedLine = line.simplified();
if (simplifiedLine.startsWith(magicString))
return QString::fromLatin1(simplifiedLine.mid(magicString.count()).trimmed());
}
if (module.isStaticLibrary)
qDebug("Warning: Could not determine default QPA plugin for static Qt.");
return QString();
}
static void replaceSpecialValues(QByteArray *content, const Profile &profile,
const QtModuleInfo &module, const QtEnvironment &qtEnvironment)
{
content->replace("@name@", utf8JSLiteral(module.moduleNameWithoutPrefix()));
content->replace("@has_library@", utf8JSLiteral(module.hasLibrary));
content->replace("@dependencies@", utf8JSLiteral(module.dependencies));
content->replace("@includes@", utf8JSLiteral(module.includePaths));
content->replace("@staticLibsDebug@", utf8JSLiteral(module.staticLibrariesDebug));
content->replace("@staticLibsRelease@", utf8JSLiteral(module.staticLibrariesRelease));
content->replace("@dynamicLibsDebug@", utf8JSLiteral(module.dynamicLibrariesDebug));
content->replace("@dynamicLibsRelease@", utf8JSLiteral(module.dynamicLibrariesRelease));
content->replace("@linkerFlagsDebug@", utf8JSLiteral(module.linkerFlagsDebug));
content->replace("@linkerFlagsRelease@", utf8JSLiteral(module.linkerFlagsRelease));
content->replace("@libraryPaths@", utf8JSLiteral(module.libraryPaths));
content->replace("@frameworkPathsDebug@", utf8JSLiteral(module.frameworkPathsDebug));
content->replace("@frameworkPathsRelease@", utf8JSLiteral(module.frameworkPathsRelease));
content->replace("@frameworksDebug@", utf8JSLiteral(module.frameworksDebug));
content->replace("@frameworksRelease@", utf8JSLiteral(module.frameworksRelease));
content->replace("@libFilePathDebug@", utf8JSLiteral(module.libFilePathDebug));
content->replace("@libFilePathRelease@", utf8JSLiteral(module.libFilePathRelease));
content->replace("@libNameForLinkerDebug@",
utf8JSLiteral(module.libNameForLinker(qtEnvironment, true)));
content->replace("@libNameForLinkerRelease@",
utf8JSLiteral(module.libNameForLinker(qtEnvironment, false)));
QByteArray propertiesString;
QByteArray compilerDefines = utf8JSLiteral(module.compilerDefines);
if (module.qbsName == QLatin1String("declarative")
|| module.qbsName == QLatin1String("quick")) {
const QByteArray debugMacro = module.qbsName == QLatin1String("declarative")
|| qtEnvironment.qtMajorVersion < 5
? "QT_DECLARATIVE_DEBUG" : "QT_QML_DEBUG";
const QString indent = QLatin1String(" ");
QTextStream s(&propertiesString);
s << "property bool qmlDebugging: false" << endl;
s << indent << "property string qmlPath";
if (qtEnvironment.qmlPath.isEmpty())
s << endl;
else
s << ": " << pathToJSLiteral(qtEnvironment.qmlPath) << endl;
s << indent << "property string qmlImportsPath: "
<< pathToJSLiteral(qtEnvironment.qmlImportPath);
const QByteArray baIndent(4, ' ');
compilerDefines = "{\n"
+ baIndent + baIndent + "var result = " + compilerDefines + ";\n"
+ baIndent + baIndent + "if (qmlDebugging)\n"
+ baIndent + baIndent + baIndent + "result.push(\"" + debugMacro + "\");\n"
+ baIndent + baIndent + "return result;\n"
+ baIndent + "}";
}
content->replace("@defines@", compilerDefines);
if (module.qbsName == QLatin1String("gui")) {
content->replace("@defaultQpaPlugin@",
utf8JSLiteral(defaultQpaPlugin(profile, module, qtEnvironment)));
}
if (module.isStaticLibrary) {
if (!propertiesString.isEmpty())
propertiesString += "\n ";
propertiesString += "isStaticLibrary: true";
}
if (module.isPlugin)
content->replace("@className@", utf8JSLiteral(module.pluginData.className));
content->replace("@special_properties@", propertiesString);
}
static void copyTemplateFile(const QString &fileName, const QString &targetDirectory,
const Profile &profile, const QtEnvironment &qtEnv, QStringList *allFiles,
const QtModuleInfo *module = 0)
{
if (!QDir::root().mkpath(targetDirectory)) {
throw ErrorInfo(Internal::Tr::tr("Setting up Qt profile '%1' failed: "
"Cannot create directory '%2'.")
.arg(profile.name(), targetDirectory));
}
QFile sourceFile(QLatin1String(":/templates/") + fileName);
if (!sourceFile.open(QIODevice::ReadOnly)) {
throw ErrorInfo(Internal::Tr::tr("Setting up Qt profile '%1' failed: "
"Cannot open '%1' (%2).").arg(sourceFile.fileName(), sourceFile.errorString()));
}
QByteArray newContent = sourceFile.readAll();
if (module)
replaceSpecialValues(&newContent, profile, *module, qtEnv);
sourceFile.close();
const QString targetPath = targetDirectory + QLatin1Char('/') + fileName;
allFiles->append(QFileInfo(targetPath).absoluteFilePath());
QFile targetFile(targetPath);
if (targetFile.open(QIODevice::ReadOnly)) {
if (newContent == targetFile.readAll()) // No need to overwrite anything in this case.
return;
targetFile.close();
}
if (!targetFile.open(QIODevice::WriteOnly)) {
throw ErrorInfo(Internal::Tr::tr("Setting up Qt profile '%1' failed: "
"Cannot open '%1' (%2).").arg(targetFile.fileName(), targetFile.errorString()));
}
targetFile.resize(0);
targetFile.write(newContent);
}
static void createModules(Profile &profile, Settings *settings,
const QtEnvironment &qtEnvironment)
{
const QList<QtModuleInfo> modules = qtEnvironment.qtMajorVersion < 5
? allQt4Modules(qtEnvironment)
: allQt5Modules(profile, qtEnvironment);
const QString profileBaseDir = QString::fromLocal8Bit("%1/qbs/profiles/%2")
.arg(QFileInfo(settings->fileName()).dir().absolutePath(), profile.name());
const QString qbsQtModuleBaseDir = profileBaseDir + QLatin1String("/modules/Qt");
QStringList allFiles;
copyTemplateFile(QLatin1String("QtModule.qbs"), qbsQtModuleBaseDir, profile, qtEnvironment,
&allFiles);
copyTemplateFile(QLatin1String("QtPlugin.qbs"), qbsQtModuleBaseDir, profile, qtEnvironment,
&allFiles);
foreach (const QtModuleInfo &module, modules) {
const QString qbsQtModuleDir = qbsQtModuleBaseDir + QLatin1Char('/') + module.qbsName;
QString moduleTemplateFileName;
if (module.qbsName == QLatin1String("core")) {
moduleTemplateFileName = QLatin1String("core.qbs");
copyTemplateFile(QLatin1String("moc.js"), qbsQtModuleDir, profile, qtEnvironment,
&allFiles);
} else if (module.qbsName == QLatin1String("gui")) {
moduleTemplateFileName = QLatin1String("gui.qbs");
} else if (module.qbsName == QLatin1String("phonon")) {
moduleTemplateFileName = QLatin1String("phonon.qbs");
} else if (module.isPlugin) {
moduleTemplateFileName = QLatin1String("plugin.qbs");
} else {
moduleTemplateFileName = QLatin1String("module.qbs");
}
copyTemplateFile(moduleTemplateFileName, qbsQtModuleDir, profile, qtEnvironment, &allFiles,
&module);
}
QDirIterator dit(qbsQtModuleBaseDir, QDirIterator::Subdirectories);
while (dit.hasNext()) {
dit.next();
const QFileInfo &fi = dit.fileInfo();
if (!fi.isFile())
continue;
const QString filePath = fi.absoluteFilePath();
if (!allFiles.contains(filePath) && !QFile::remove(filePath))
qDebug("Warning: Failed to remove outdated file '%s'.", qPrintable(filePath));
}
profile.setValue(QLatin1String("preferences.qbsSearchPaths"), profileBaseDir);
}
static QString guessMinimumWindowsVersion(const QtEnvironment &qt)
{
if (qt.mkspecName.startsWith("winrt-"))
return QLatin1String("6.2");
if (!qt.mkspecName.startsWith("win32-"))
return QString();
if (qt.architecture == QLatin1String("x86_64")
|| qt.architecture == QLatin1String("ia64")) {
return QLatin1String("5.2");
}
QRegExp rex(QLatin1String("^win32-msvc(\\d+)$"));
if (rex.exactMatch(qt.mkspecName)) {
int msvcVersion = rex.cap(1).toInt();
if (msvcVersion < 2012)
return QLatin1String("5.0");
else
return QLatin1String("5.1");
}
return qt.qtMajorVersion < 5 ? QLatin1String("5.0") : QLatin1String("5.1");
}
static bool checkForStaticBuild(const QtEnvironment &qt)
{
if (qt.qtMajorVersion >= 5)
return qt.qtConfigItems.contains(QLatin1String("static"));
if (qt.frameworkBuild)
return false; // there are no Qt4 static frameworks
QDir libdir(qt.libraryPath);
const QStringList coreLibFiles
= libdir.entryList(QStringList(QLatin1String("*Core*")), QDir::Files);
if (coreLibFiles.isEmpty())
throw ErrorInfo(Internal::Tr::tr("Could not determine whether Qt is a static build."));
foreach (const QString &fileName, coreLibFiles) {
if (QLibrary::isLibrary(qt.libraryPath + QLatin1Char('/') + fileName))
return false;
}
return true;
}
void doSetupQtProfile(const QString &profileName, Settings *settings,
const QtEnvironment &_qtEnvironment)
{
QtEnvironment qtEnvironment = _qtEnvironment;
const bool staticBuild = checkForStaticBuild(qtEnvironment);
// determine whether user apps require C++11
if (qtEnvironment.qtConfigItems.contains(QLatin1String("c++11")) && staticBuild)
qtEnvironment.configItems.append(QLatin1String("c++11"));
Profile profile(profileName, settings);
profile.removeProfile();
const QString settingsTemplate(QLatin1String("Qt.core.%1"));
profile.setValue(settingsTemplate.arg("config"), qtEnvironment.configItems);
profile.setValue(settingsTemplate.arg("qtConfig"), qtEnvironment.qtConfigItems);
profile.setValue(settingsTemplate.arg("binPath"), qtEnvironment.binaryPath);
profile.setValue(settingsTemplate.arg("libPath"), qtEnvironment.libraryPath);
profile.setValue(settingsTemplate.arg("pluginPath"), qtEnvironment.pluginPath);
profile.setValue(settingsTemplate.arg("incPath"), qtEnvironment.includePath);
profile.setValue(settingsTemplate.arg("mkspecPath"), qtEnvironment.mkspecPath);
profile.setValue(settingsTemplate.arg("docPath"), qtEnvironment.documentationPath);
profile.setValue(settingsTemplate.arg("version"), qtEnvironment.qtVersion);
profile.setValue(settingsTemplate.arg("libInfix"), qtEnvironment.qtLibInfix);
profile.setValue(settingsTemplate.arg("buildVariant"), qtEnvironment.buildVariant); // TODO: Remove in 1.5
profile.setValue(settingsTemplate.arg("availableBuildVariants"), qtEnvironment.buildVariant);
profile.setValue(settingsTemplate.arg(QLatin1String("staticBuild")), staticBuild);
// Set the minimum operating system versions appropriate for this Qt version
const QString windowsVersion = guessMinimumWindowsVersion(qtEnvironment);
QString osxVersion, iosVersion, androidVersion;
if (qtEnvironment.mkspecPath.contains("macx")) {
profile.setValue(settingsTemplate.arg("frameworkBuild"), qtEnvironment.frameworkBuild);
if (qtEnvironment.qtMajorVersion >= 5) {
osxVersion = QLatin1String("10.6");
} else if (qtEnvironment.qtMajorVersion == 4 && qtEnvironment.qtMinorVersion >= 6) {
QDir qconfigDir;
if (qtEnvironment.frameworkBuild) {
qconfigDir.setPath(qtEnvironment.libraryPath);
qconfigDir.cd("QtCore.framework/Headers");
} else {
qconfigDir.setPath(qtEnvironment.includePath);
qconfigDir.cd("Qt");
}
QFile qconfig(qconfigDir.absoluteFilePath("qconfig.h"));
if (qconfig.open(QIODevice::ReadOnly)) {
bool qtCocoaBuild = false;
QTextStream ts(&qconfig);
QString line;
do {
line = ts.readLine();
if (QRegExp(QLatin1String("\\s*#define\\s+QT_MAC_USE_COCOA\\s+1\\s*"),
Qt::CaseSensitive).exactMatch(line)) {
qtCocoaBuild = true;
break;
}
} while (!line.isNull());
if (ts.status() == QTextStream::Ok)
osxVersion = qtCocoaBuild ? QLatin1String("10.5") : QLatin1String("10.4");
}
if (osxVersion.isEmpty()) {
throw ErrorInfo(Internal::Tr::tr("Error reading qconfig.h; could not determine "
"whether Qt is using Cocoa or Carbon"));
}
}
if (qtEnvironment.configItems.contains("c++11"))
osxVersion = QLatin1String("10.7");
}
if (qtEnvironment.mkspecPath.contains("ios") && qtEnvironment.qtMajorVersion >= 5)
iosVersion = QLatin1String("5.0");
if (qtEnvironment.mkspecPath.contains("android")) {
if (qtEnvironment.qtMajorVersion >= 5)
androidVersion = QLatin1String("2.3");
else if (qtEnvironment.qtMajorVersion == 4 && qtEnvironment.qtMinorVersion >= 8)
androidVersion = QLatin1String("1.6"); // Necessitas
}
// ### TODO: wince, winphone, blackberry
if (!windowsVersion.isEmpty())
profile.setValue(QLatin1String("cpp.minimumWindowsVersion"), windowsVersion);
if (!osxVersion.isEmpty())
profile.setValue(QLatin1String("cpp.minimumOsxVersion"), osxVersion);
if (!iosVersion.isEmpty())
profile.setValue(QLatin1String("cpp.minimumIosVersion"), iosVersion);
if (!androidVersion.isEmpty())
profile.setValue(QLatin1String("cpp.minimumAndroidVersion"), androidVersion);
createModules(profile, settings, qtEnvironment);
}
ErrorInfo setupQtProfile(const QString &profileName, Settings *settings,
const QtEnvironment &qtEnvironment)
{
try {
doSetupQtProfile(profileName, settings, qtEnvironment);
return ErrorInfo();
} catch (const ErrorInfo &e) {
return e;
}
}
} // namespace qbs
|