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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include "cmakeconfigitem.h"
#include <projectexplorer/kit.h>
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/macroexpander.h>
#include <utils/qtcassert.h>
#include <QFile>
#include <QIODevice>
using namespace Utils;
namespace CMakeProjectManager {
// --------------------------------------------------------------------
// CMakeConfigItem:
// --------------------------------------------------------------------
CMakeConfigItem::CMakeConfigItem() = default;
CMakeConfigItem::CMakeConfigItem(const QByteArray &k, Type t,
const QByteArray &d, const QByteArray &v,
const QStringList &s) :
key(k), type(t), value(v), documentation(d), values(s)
{ }
CMakeConfigItem::CMakeConfigItem(const QByteArray &k, Type t, const QByteArray &v) :
key(k), type(t), value(v)
{ }
CMakeConfigItem::CMakeConfigItem(const QByteArray &k, const QByteArray &v) :
key(k), value(v)
{ }
QByteArray CMakeConfig::valueOf(const QByteArray &key) const
{
for (auto it = constBegin(); it != constEnd(); ++it) {
if (it->key == key)
return it->value;
}
return QByteArray();
}
QString CMakeConfig::stringValueOf(const QByteArray &key) const
{
return QString::fromUtf8(valueOf(key));
}
FilePath CMakeConfig::filePathValueOf(const QByteArray &key) const
{
return FilePath::fromUtf8(valueOf(key));
}
QString CMakeConfig::expandedValueOf(const ProjectExplorer::Kit *k, const QByteArray &key) const
{
for (auto it = constBegin(); it != constEnd(); ++it) {
if (it->key == key)
return it->expandedValue(k);
}
return QString();
}
static QString between(const QString::ConstIterator it1, const QString::ConstIterator it2)
{
QString result;
for (auto it = it1; it != it2; ++it)
result.append(*it);
return result;
}
QStringList CMakeConfigItem::cmakeSplitValue(const QString &in, bool keepEmpty)
{
QStringList newArgs;
if (in.isEmpty())
return newArgs;
int squareNesting = 0;
QString newArg;
auto last = in.constBegin();
for (auto c = in.constBegin(); c != in.constEnd(); ++c) {
switch (c->unicode()) {
case '\\': {
auto next = c + 1;
if (next != in.constEnd() && *next == ';') {
newArg.append(between(last, c));
last = next;
c = next;
}
} break;
case '[': {
++squareNesting;
} break;
case ']': {
--squareNesting;
} break;
case ';': {
// Break the string here if we are not nested inside square
// brackets.
if (squareNesting == 0) {
newArg.append(between(last, c));
// Skip over the semicolon
last = c + 1;
if (!newArg.isEmpty() || keepEmpty) {
// Add the last argument if the string is not empty.
newArgs.append(newArg);
newArg.clear();
}
}
} break;
default: {
// Just append this character.
} break;
}
}
newArg.append(between(last, in.constEnd()));
if (!newArg.isEmpty() || keepEmpty) {
// Add the last argument if the string is not empty.
newArgs.append(newArg);
}
return newArgs;
}
CMakeConfigItem::Type CMakeConfigItem::typeStringToType(const QByteArray &type)
{
if (type == "BOOL")
return CMakeConfigItem::BOOL;
if (type == "STRING")
return CMakeConfigItem::STRING;
if (type == "FILEPATH")
return CMakeConfigItem::FILEPATH;
if (type == "PATH")
return CMakeConfigItem::PATH;
if (type == "STATIC")
return CMakeConfigItem::STATIC;
if (type == "INTERNAL")
return CMakeConfigItem::INTERNAL;
return CMakeConfigItem::UNINITIALIZED;
}
QString CMakeConfigItem::typeToTypeString(const CMakeConfigItem::Type t)
{
switch (t) {
case CMakeProjectManager::CMakeConfigItem::FILEPATH:
return {"FILEPATH"};
case CMakeProjectManager::CMakeConfigItem::PATH:
return {"PATH"};
case CMakeProjectManager::CMakeConfigItem::STRING:
return {"STRING"};
case CMakeProjectManager::CMakeConfigItem::INTERNAL:
return {"INTERNAL"};
case CMakeProjectManager::CMakeConfigItem::STATIC:
return {"STATIC"};
case CMakeProjectManager::CMakeConfigItem::BOOL:
return {"BOOL"};
case CMakeProjectManager::CMakeConfigItem::UNINITIALIZED:
return {"UNINITIALIZED"};
}
QTC_CHECK(false);
return {};
}
Utils::optional<bool> CMakeConfigItem::toBool(const QString &value)
{
// Taken from CMakes if(<constant>) documentation:
// "Named boolean constants are case-insensitive."
const QString v = value.toUpper();
bool isInt = false;
v.toInt(&isInt);
// "False if the constant is 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, the empty string, or ends in the suffix -NOTFOUND."
if (v == "0" || v == "OFF" || v == "NO" || v == "FALSE" || v == "N" || v == "IGNORE" || v == "NOTFOUND" || v == "" || v.endsWith("-NOTFOUND"))
return false;
// "True if the constant is 1, ON, YES, TRUE, Y, or a non-zero number."
if (v == "1" || v == "ON" || v == "YES" || v == "TRUE" || v == "Y" || isInt)
return true;
return {};
}
QString CMakeConfigItem::expandedValue(const ProjectExplorer::Kit *k) const
{
return expandedValue(k->macroExpander());
}
QString CMakeConfigItem::expandedValue(const Utils::MacroExpander *expander) const
{
return expander ? expander->expand(QString::fromUtf8(value)) : QString::fromUtf8(value);
}
bool CMakeConfigItem::less(const CMakeConfigItem &a, const CMakeConfigItem &b)
{
return a.key < b.key;
}
CMakeConfigItem CMakeConfigItem::fromString(const QString &s)
{
// Strip comments (only at start of line!):
int commentStart = s.count();
for (int i = 0; i < s.count(); ++i) {
const QChar c = s.at(i);
if (c == ' ' || c == '\t')
continue;
else if ((c == '#')
|| (c == '/' && i < s.count() - 1 && s.at(i + 1) == '/')) {
commentStart = i;
break;
} else {
break;
}
}
const QString line = s.mid(0, commentStart);
// Split up line:
int firstPos = -1;
int colonPos = -1;
int equalPos = -1;
for (int i = 0; i < line.count(); ++i) {
const QChar c = s.at(i);
if (firstPos < 0 && !c.isSpace()) {
firstPos = i;
}
if (c == QLatin1Char(':')) {
if (colonPos > 0)
break;
colonPos = i;
continue;
}
if (c == QLatin1Char('=')) {
equalPos = i;
break;
}
}
QString key;
QString type;
QString value;
if (equalPos >= 0) {
key = line.mid(firstPos, ((colonPos >= 0) ? colonPos : equalPos) - firstPos);
type = (colonPos < 0) ? QString() : line.mid(colonPos + 1, equalPos - colonPos - 1);
value = line.mid(equalPos + 1);
}
// Fill in item:
CMakeConfigItem item;
if (!key.isEmpty()) {
item.key = key.toUtf8();
item.type = CMakeConfigItem::typeStringToType(type.toUtf8());
item.value = value.toUtf8();
}
return item;
}
static QByteArray trimCMakeCacheLine(const QByteArray &in) {
int start = 0;
while (start < in.count() && (in.at(start) == ' ' || in.at(start) == '\t'))
++start;
return in.mid(start, in.count() - start - 1);
}
static QByteArrayList splitCMakeCacheLine(const QByteArray &line) {
const int colonPos = line.indexOf(':');
if (colonPos < 0)
return QByteArrayList();
const int equalPos = line.indexOf('=', colonPos + 1);
if (equalPos < colonPos)
return QByteArrayList();
return QByteArrayList() << line.mid(0, colonPos)
<< line.mid(colonPos + 1, equalPos - colonPos - 1)
<< line.mid(equalPos + 1);
}
static CMakeConfigItem setItemFromString(const QString &input)
{
return CMakeConfigItem::fromString(input);
}
static CMakeConfigItem unsetItemFromString(const QString &input)
{
CMakeConfigItem item(input.toUtf8(), QByteArray());
item.isUnset = true;
return item;
}
CMakeConfig CMakeConfig::fromArguments(const QStringList &list, QStringList &unknownOptions)
{
CMakeConfig result;
bool inSet = false;
bool inUnset = false;
for (const QString &i : list) {
if (inSet) {
inSet = false;
result.append(setItemFromString(i));
continue;
}
if (inUnset) {
inUnset = false;
result.append(unsetItemFromString(i));
continue;
}
if (i == "-U") {
inUnset = true;
continue;
}
if (i == "-D") {
inSet = true;
continue;
}
if (i.startsWith("-U")) {
result.append(unsetItemFromString(i.mid(2)));
continue;
}
if (i.startsWith("-D")) {
result.append(setItemFromString(i.mid(2)));
continue;
}
unknownOptions.append(i);
}
return Utils::filtered(result, [](const CMakeConfigItem &item) { return !item.key.isEmpty(); });
}
CMakeConfig CMakeConfig::fromFile(const Utils::FilePath &cacheFile, QString *errorMessage)
{
CMakeConfig result;
QFile cache(cacheFile.toString());
if (!cache.open(QIODevice::ReadOnly | QIODevice::Text)) {
if (errorMessage)
*errorMessage = QCoreApplication::translate("CMakeProjectManager::CMakeConfigItem", "Failed to open %1 for reading.")
.arg(cacheFile.toUserOutput());
return CMakeConfig();
}
QSet<QByteArray> advancedSet;
QMap<QByteArray, QByteArray> valuesMap;
QByteArray documentation;
while (!cache.atEnd()) {
const QByteArray line = trimCMakeCacheLine(cache.readLine());
if (line.isEmpty() || line.startsWith('#'))
continue;
if (line.startsWith("//")) {
documentation = line.mid(2);
continue;
}
const QByteArrayList pieces = splitCMakeCacheLine(line);
if (pieces.isEmpty())
continue;
QTC_ASSERT(pieces.count() == 3, continue);
const QByteArray key = pieces.at(0);
const QByteArray type = pieces.at(1);
const QByteArray value = pieces.at(2);
if (key.endsWith("-ADVANCED") && value == "1") {
advancedSet.insert(key.left(key.count() - 9 /* "-ADVANCED" */));
} else if (key.endsWith("-STRINGS") && CMakeConfigItem::typeStringToType(type) == CMakeConfigItem::INTERNAL) {
valuesMap[key.left(key.count() - 8) /* "-STRINGS" */] = value;
} else {
CMakeConfigItem::Type t = CMakeConfigItem::typeStringToType(type);
result << CMakeConfigItem(key, t, documentation, value);
}
}
// Set advanced flags:
for (int i = 0; i < result.count(); ++i) {
CMakeConfigItem &item = result[i];
item.isAdvanced = advancedSet.contains(item.key);
if (valuesMap.contains(item.key)) {
item.values = CMakeConfigItem::cmakeSplitValue(QString::fromUtf8(valuesMap[item.key]));
} else if (item.key == "CMAKE_BUILD_TYPE") {
// WA for known options
item.values << "" << "Debug" << "Release" << "MinSizeRel" << "RelWithDebInfo";
}
}
Utils::sort(result, &CMakeConfigItem::less);
return result;
}
QString CMakeConfigItem::toString(const Utils::MacroExpander *expander) const
{
if (key.isEmpty() || type == CMakeProjectManager::CMakeConfigItem::STATIC)
return QString();
if (isUnset)
return "unset " + QString::fromUtf8(key);
QString typeStr;
switch (type)
{
case CMakeProjectManager::CMakeConfigItem::FILEPATH:
typeStr = QLatin1String("FILEPATH");
break;
case CMakeProjectManager::CMakeConfigItem::PATH:
typeStr = QLatin1String("PATH");
break;
case CMakeProjectManager::CMakeConfigItem::BOOL:
typeStr = QLatin1String("BOOL");
break;
case CMakeProjectManager::CMakeConfigItem::INTERNAL:
typeStr = QLatin1String("INTERNAL");
break;
case CMakeProjectManager::CMakeConfigItem::UNINITIALIZED:
typeStr = QLatin1String("UNINITIALIZED");
break;
case CMakeProjectManager::CMakeConfigItem::STRING:
default:
typeStr = QLatin1String("STRING");
break;
}
const QString expandedValue
= expander ? expander->expand(QString::fromUtf8(value)) : QString::fromUtf8(value);
return QString::fromUtf8(key) + QLatin1Char(':') + typeStr + QLatin1Char('=') + expandedValue;
}
QString CMakeConfigItem::toArgument() const
{
return toArgument(nullptr);
}
QString CMakeConfigItem::toArgument(const Utils::MacroExpander *expander) const
{
if (isUnset)
return "-U" + QString::fromUtf8(key);
return "-D" + toString(expander);
}
QString CMakeConfigItem::toCMakeSetLine(const Utils::MacroExpander *expander) const
{
if (isUnset) {
return QString("unset(\"%1\" CACHE)").arg(QString::fromUtf8(key));
}
return QString("set(\"%1\" \"%2\" CACHE \"%3\" \"%4\" FORCE)")
.arg(QString::fromUtf8(key))
.arg(expandedValue(expander))
.arg(typeToTypeString(type))
.arg(QString::fromUtf8(documentation));
}
bool CMakeConfigItem::operator==(const CMakeConfigItem &o) const
{
// type, isAdvanced and documentation do not matter for a match!
return o.key == key && o.value == value && o.isUnset == isUnset && o.isInitial == isInitial;
}
size_t qHash(const CMakeConfigItem &it)
{
return ::qHash(it.key) ^ ::qHash(it.value) ^ ::qHash(it.isUnset) ^ ::qHash(it.isInitial);
}
#if WITH_TESTS
} // namespace CMakeProjectManager
#include "cmakeprojectplugin.h"
#include <QTest>
namespace CMakeProjectManager {
namespace Internal {
void CMakeProjectPlugin::testCMakeSplitValue_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<bool>("keepEmpty");
QTest::addColumn<QStringList>("expectedOutput");
// negative tests
QTest::newRow("Empty input")
<< "" << false << QStringList();
QTest::newRow("Empty input, keep empty")
<< "" << true << QStringList();
QTest::newRow("single path")
<< "C:/something" << false << QStringList({"C:/something"});
QTest::newRow("single path, keep empty")
<< "C:/something" << true << QStringList({"C:/something"});
QTest::newRow(";single path")
<< ";C:/something" << false << QStringList({"C:/something"});
QTest::newRow(";single path, keep empty")
<< ";C:/something" << true << QStringList({"", "C:/something"});
QTest::newRow("single path;")
<< "C:/something;" << false << QStringList({"C:/something"});
QTest::newRow("single path;, keep empty")
<< "C:/something;" << true << QStringList({"C:/something", ""});
QTest::newRow("single path\\;")
<< "C:/something\\;" << false << QStringList({"C:/something;"});
QTest::newRow("single path\\;, keep empty")
<< "C:/something\\;" << true << QStringList({"C:/something;"});
QTest::newRow("single path\\;;second path")
<< "C:/something\\;;/second/path" << false << QStringList({"C:/something;", "/second/path"});
QTest::newRow("single path\\;;second path, keep empty")
<< "C:/something\\;;/second/path" << true << QStringList({"C:/something;", "/second/path"});
QTest::newRow("single path;;second path")
<< "C:/something;;/second/path" << false << QStringList({"C:/something", "/second/path"});
QTest::newRow("single path;;second path, keep empty")
<< "C:/something;;/second/path" << true << QStringList({"C:/something", "", "/second/path"});
}
void CMakeProjectPlugin::testCMakeSplitValue()
{
QFETCH(QString, input);
QFETCH(bool, keepEmpty);
QFETCH(QStringList, expectedOutput);
const QStringList realOutput = CMakeConfigItem::cmakeSplitValue(input, keepEmpty);
QCOMPARE(expectedOutput, realOutput);
}
} // namespace Internal
#endif
} // namespace CMakeProjectManager
|