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
|
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** 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 http://www.qt.io/terms-conditions. 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, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "abstractmsvctoolchain.h"
#include "msvcparser.h"
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorersettings.h>
#include <utils/qtcprocess.h>
#include <utils/synchronousprocess.h>
#include <QDir>
#include <QTemporaryFile>
enum { debug = 0 };
namespace ProjectExplorer {
namespace Internal {
AbstractMsvcToolChain::AbstractMsvcToolChain(Core::Id typeId,
Detection d,
const Abi &abi,
const QString& vcvarsBat) :
ToolChain(typeId, d),
m_lastEnvironment(Utils::Environment::systemEnvironment()),
m_abi(abi),
m_vcvarsBat(vcvarsBat)
{
Q_ASSERT(abi.os() == Abi::WindowsOS);
Q_ASSERT(abi.binaryFormat() == Abi::PEFormat);
Q_ASSERT(abi.osFlavor() != Abi::WindowsMSysFlavor);
Q_ASSERT(!m_vcvarsBat.isEmpty());
}
AbstractMsvcToolChain::AbstractMsvcToolChain(Core::Id typeId, Detection d) :
ToolChain(typeId, d),
m_lastEnvironment(Utils::Environment::systemEnvironment())
{ }
Abi AbstractMsvcToolChain::targetAbi() const
{
return m_abi;
}
bool AbstractMsvcToolChain::isValid() const
{
return !m_vcvarsBat.isEmpty();
}
QByteArray AbstractMsvcToolChain::predefinedMacros(const QStringList &cxxflags) const
{
if (m_predefinedMacros.isEmpty()) {
Utils::Environment env(m_lastEnvironment);
addToEnvironment(env);
m_predefinedMacros = msvcPredefinedMacros(cxxflags, env);
}
return m_predefinedMacros;
}
ToolChain::CompilerFlags AbstractMsvcToolChain::compilerFlags(const QStringList &cxxflags) const
{
CompilerFlags flags(MicrosoftExtensions);
if (cxxflags.contains(QLatin1String("/openmp")))
flags |= OpenMP;
// see http://msdn.microsoft.com/en-us/library/0k0w269d%28v=vs.71%29.aspx
if (cxxflags.contains(QLatin1String("/Za")))
flags &= ~MicrosoftExtensions;
if (m_abi.osFlavor() == Abi::WindowsMsvc2010Flavor
|| m_abi.osFlavor() == Abi::WindowsMsvc2012Flavor
|| m_abi.osFlavor() == Abi::WindowsMsvc2013Flavor
|| m_abi.osFlavor() == Abi::WindowsMsvc2015Flavor)
flags |= StandardCxx11;
return flags;
}
/**
* Converts MSVC warning flags to clang flags.
* @see http://msdn.microsoft.com/en-us/library/thxezb7y.aspx
*/
AbstractMsvcToolChain::WarningFlags AbstractMsvcToolChain::warningFlags(const QStringList &cflags) const
{
WarningFlags flags;
foreach (QString flag, cflags) {
if (!flag.isEmpty() && flag[0] == QLatin1Char('-'))
flag[0] = QLatin1Char('/');
if (flag == QLatin1String("/WX"))
flags |= WarningsAsErrors;
else if (flag == QLatin1String("/W0") || flag == QLatin1String("/w"))
inferWarningsForLevel(0, flags);
else if (flag == QLatin1String("/W1"))
inferWarningsForLevel(1, flags);
else if (flag == QLatin1String("/W2"))
inferWarningsForLevel(2, flags);
else if (flag == QLatin1String("/W3") || flag == QLatin1String("/W4") || flag == QLatin1String("/Wall"))
inferWarningsForLevel(3, flags);
WarningFlagAdder add(flag, flags);
if (add.triggered())
continue;
// http://msdn.microsoft.com/en-us/library/ay4h0tc9.aspx
add(4263, WarnOverloadedVirtual);
// http://msdn.microsoft.com/en-us/library/ytxde1x7.aspx
add(4230, WarnIgnoredQualfiers);
// not exact match, http://msdn.microsoft.com/en-us/library/0hx5ckb0.aspx
add(4258, WarnHiddenLocals);
// http://msdn.microsoft.com/en-us/library/wzxffy8c.aspx
add(4265, WarnNonVirtualDestructor);
// http://msdn.microsoft.com/en-us/library/y92ktdf2%28v=vs.90%29.aspx
add(4018, WarnSignedComparison);
// http://msdn.microsoft.com/en-us/library/w099eeey%28v=vs.90%29.aspx
add(4068, WarnUnknownPragma);
// http://msdn.microsoft.com/en-us/library/26kb9fy0%28v=vs.80%29.aspx
add(4100, WarnUnusedParams);
// http://msdn.microsoft.com/en-us/library/c733d5h9%28v=vs.90%29.aspx
add(4101, WarnUnusedLocals);
// http://msdn.microsoft.com/en-us/library/xb1db44s%28v=vs.90%29.aspx
add(4189, WarnUnusedLocals);
// http://msdn.microsoft.com/en-us/library/ttcz0bys%28v=vs.90%29.aspx
add(4996, WarnDeprecated);
}
return flags;
}
QList<HeaderPath> AbstractMsvcToolChain::systemHeaderPaths(const QStringList &cxxflags, const Utils::FileName &sysRoot) const
{
Q_UNUSED(cxxflags);
Q_UNUSED(sysRoot);
if (m_headerPaths.isEmpty()) {
Utils::Environment env(m_lastEnvironment);
addToEnvironment(env);
foreach (const QString &path, env.value(QLatin1String("INCLUDE")).split(QLatin1Char(';')))
m_headerPaths.append(HeaderPath(path, HeaderPath::GlobalHeaderPath));
}
return m_headerPaths;
}
void AbstractMsvcToolChain::addToEnvironment(Utils::Environment &env) const
{
// We cache the full environment (incoming + modifications by setup script).
if (!m_resultEnvironment.size() || env != m_lastEnvironment) {
if (debug)
qDebug() << "addToEnvironment: " << displayName();
m_lastEnvironment = env;
m_resultEnvironment = readEnvironmentSetting(env);
}
env = m_resultEnvironment;
}
QString AbstractMsvcToolChain::makeCommand(const Utils::Environment &environment) const
{
bool useJom = ProjectExplorerPlugin::projectExplorerSettings().useJom;
const QString jom = QLatin1String("jom.exe");
const QString nmake = QLatin1String("nmake.exe");
Utils::FileName tmp;
if (useJom) {
tmp = environment.searchInPath(jom, QStringList()
<< QCoreApplication::applicationDirPath());
if (!tmp.isEmpty())
return tmp.toString();
}
tmp = environment.searchInPath(nmake);
if (!tmp.isEmpty())
return tmp.toString();
// Nothing found :(
return useJom ? jom : nmake;
}
Utils::FileName AbstractMsvcToolChain::compilerCommand() const
{
Utils::Environment env = Utils::Environment::systemEnvironment();
addToEnvironment(env);
return env.searchInPath(QLatin1String("cl.exe"));
}
IOutputParser *AbstractMsvcToolChain::outputParser() const
{
return new MsvcParser;
}
bool AbstractMsvcToolChain::canClone() const
{
return true;
}
QByteArray AbstractMsvcToolChain::msvcPredefinedMacros(const QStringList cxxflags,
const Utils::Environment& env) const
{
Q_UNUSED(cxxflags);
Q_UNUSED(env);
static const QByteArray predefinedMacros(
"#define __MSVCRT__\n"
"#define __w64\n"
"#define __int64 long long\n"
"#define __int32 long\n"
"#define __int16 short\n"
"#define __int8 char\n"
"#define __ptr32\n"
"#define __ptr64\n");
return predefinedMacros;
}
bool AbstractMsvcToolChain::generateEnvironmentSettings(Utils::Environment &env,
const QString &batchFile,
const QString &batchArgs,
QMap<QString, QString> &envPairs)
{
const QByteArray marker = "####################\r\n";
// Create a temporary file name for the output. Use a temporary file here
// as I don't know another way to do this in Qt...
// Note, can't just use a QTemporaryFile all the way through as it remains open
// internally so it can't be streamed to later.
// Create a batch file to create and save the env settings
Utils::TempFileSaver saver(QDir::tempPath() + QLatin1String("/XXXXXX.bat"));
QByteArray call = "call ";
call += Utils::QtcProcess::quoteArg(batchFile).toLocal8Bit();
if (!batchArgs.isEmpty()) {
call += ' ';
call += batchArgs.toLocal8Bit();
}
saver.write(call + "\r\n");
saver.write("@echo " + marker);
saver.write("set\r\n");
saver.write("@echo " + marker);
if (!saver.finalize()) {
qWarning("%s: %s", Q_FUNC_INFO, qPrintable(saver.errorString()));
return false;
}
Utils::QtcProcess run;
// As of WinSDK 7.1, there is logic preventing the path from being set
// correctly if "ORIGINALPATH" is already set. That can cause problems
// if Creator is launched within a session set up by setenv.cmd.
env.unset(QLatin1String("ORIGINALPATH"));
run.setEnvironment(env);
Utils::FileName cmdPath = Utils::FileName::fromUserInput(QString::fromLocal8Bit(qgetenv("COMSPEC")));
if (cmdPath.isEmpty())
cmdPath = env.searchInPath(QLatin1String("cmd.exe"));
// Windows SDK setup scripts require command line switches for environment expansion.
QString cmdArguments = QLatin1String(" /E:ON /V:ON /c \"");
cmdArguments += QDir::toNativeSeparators(saver.fileName());
cmdArguments += QLatin1Char('"');
run.setCommand(cmdPath.toString(), cmdArguments);
if (debug)
qDebug() << "readEnvironmentSetting: " << call << cmdPath << cmdArguments
<< " Env: " << env.size();
run.start();
if (!run.waitForStarted()) {
qWarning("%s: Unable to run '%s': %s", Q_FUNC_INFO, qPrintable(batchFile),
qPrintable(run.errorString()));
return false;
}
if (!run.waitForFinished()) {
qWarning("%s: Timeout running '%s'", Q_FUNC_INFO, qPrintable(batchFile));
Utils::SynchronousProcess::stopProcess(run);
return false;
}
// The SDK/MSVC scripts do not return exit codes != 0. Check on stdout.
QByteArray stdOut = run.readAllStandardOutput();
if (!stdOut.isEmpty() && (stdOut.contains("Unknown") || stdOut.contains("Error")))
qWarning("%s: '%s' reports:\n%s", Q_FUNC_INFO, call.constData(), stdOut.constData());
//
// Now parse the file to get the environment settings
int start = stdOut.indexOf(marker);
if (start == -1) {
qWarning("Could not find start marker in stdout output.");
return false;
}
stdOut = stdOut.mid(start + marker.size());
int end = stdOut.indexOf(marker);
if (end == -1) {
qWarning("Could not find end marker in stdout output.");
return false;
}
stdOut = stdOut.left(end);
QStringList lines = QString::fromLocal8Bit(stdOut).split(QLatin1String("\r\n"));
QRegExp regexp(QLatin1String("(\\w*)=(.*)"));
foreach (const QString &line, lines) {
if (regexp.exactMatch(line)) {
const QString varName = regexp.cap(1);
const QString varValue = regexp.cap(2);
if (!varValue.isEmpty())
envPairs.insert(varName, varValue);
}
}
return true;
}
/**
* Infers warnings settings on warning level set
* @see http://msdn.microsoft.com/en-us/library/23k5d385.aspx
*/
void AbstractMsvcToolChain::inferWarningsForLevel(int warningLevel, WarningFlags &flags)
{
// reset all except unrelated flag
flags = flags & WarningsAsErrors;
if (warningLevel >= 1)
flags |= WarningFlags(WarningsDefault | WarnIgnoredQualfiers | WarnHiddenLocals | WarnUnknownPragma);
if (warningLevel >= 2)
flags |= WarningsAll;
if (warningLevel >= 3) {
flags |= WarningFlags(WarningsExtra | WarnNonVirtualDestructor | WarnSignedComparison
| WarnUnusedLocals | WarnDeprecated);
}
if (warningLevel >= 4)
flags |= WarnUnusedParams;
}
bool AbstractMsvcToolChain::operator ==(const ToolChain &other) const
{
if (!ToolChain::operator ==(other))
return false;
const AbstractMsvcToolChain *msvcTc = static_cast<const AbstractMsvcToolChain *>(&other);
return targetAbi() == msvcTc->targetAbi()
&& m_vcvarsBat == msvcTc->m_vcvarsBat;
}
AbstractMsvcToolChain::WarningFlagAdder::WarningFlagAdder(const QString &flag,
ToolChain::WarningFlags &flags) :
m_flags(flags),
m_triggered(false)
{
if (flag.startsWith(QLatin1String("-wd"))) {
m_doesEnable = false;
} else if (flag.startsWith(QLatin1String("-w"))) {
m_doesEnable = true;
} else {
m_triggered = true;
return;
}
bool ok = false;
if (m_doesEnable)
m_warningCode = flag.mid(2).toInt(&ok);
else
m_warningCode = flag.mid(3).toInt(&ok);
if (!ok)
m_triggered = true;
}
void AbstractMsvcToolChain::WarningFlagAdder::operator ()(int warningCode, ToolChain::WarningFlags flagsSet)
{
if (m_triggered)
return;
if (warningCode == m_warningCode)
{
m_triggered = true;
if (m_doesEnable)
m_flags |= flagsSet;
else
m_flags &= ~flagsSet;
}
}
void AbstractMsvcToolChain::WarningFlagAdder::operator ()(int warningCode, ToolChain::WarningFlag flag)
{
(*this)(warningCode, WarningFlags(flag));
}
bool AbstractMsvcToolChain::WarningFlagAdder::triggered() const
{
return m_triggered;
}
} // namespace Internal
} // namespace ProjectExplorer
|