msvctoolchain.cpp 26.9 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: 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
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30

#include "msvctoolchain.h"
31

32 33 34 35
#include "msvcparser.h"
#include "projectexplorerconstants.h"

#include <utils/synchronousprocess.h>
36
#include <utils/winutils.h>
SteveKing's avatar
SteveKing committed
37
#include <utils/qtcassert.h>
Daniel Teske's avatar
Daniel Teske committed
38
#include <utils/hostosinfo.h>
39

40
#include <QDir>
41
#include <QFileInfo>
42 43
#include <QProcess>
#include <QSettings>
44 45

#include <QLabel>
46
#include <QFormLayout>
47

48 49 50 51
#define KEY_ROOT "ProjectExplorer.MsvcToolChain."
static const char varsBatKeyC[] = KEY_ROOT"VarsBat";
static const char varsBatArgKeyC[] = KEY_ROOT"VarsBatArg";
static const char supportedAbiKeyC[] = KEY_ROOT"SupportedAbi";
52

Friedemann Kleint's avatar
Friedemann Kleint committed
53 54
enum { debug = 0 };

55 56 57 58 59 60 61 62 63 64
namespace ProjectExplorer {
namespace Internal {

// --------------------------------------------------------------------------
// Helpers:
// --------------------------------------------------------------------------

static QString platformName(MsvcToolChain::Platform t)
{
    switch (t) {
65
    case MsvcToolChain::x86:
66 67 68
        return QLatin1String(" (x86)");
    case MsvcToolChain::amd64:
        return QLatin1String(" (amd64)");
69 70
    case MsvcToolChain::ia64:
        return QLatin1String(" (ia64)");
Daniel Teske's avatar
Daniel Teske committed
71 72
    case MsvcToolChain::arm:
        return QLatin1String(" (arm)");
73 74 75 76
    }
    return QString();
}

77 78 79 80 81 82 83 84
static Abi findAbiOfMsvc(MsvcToolChain::Type type, MsvcToolChain::Platform platform, const QString &version)
{
    Abi::Architecture arch = Abi::X86Architecture;
    Abi::OSFlavor flavor = Abi::UnknownFlavor;
    int wordWidth = 64;

    switch (platform)
    {
85
    case ProjectExplorer::Internal::MsvcToolChain::x86:
86 87 88 89 90 91
        wordWidth = 32;
        break;
    case ProjectExplorer::Internal::MsvcToolChain::ia64:
        arch = Abi::ItaniumArchitecture;
        break;
    case ProjectExplorer::Internal::MsvcToolChain::amd64:
Daniel Teske's avatar
Daniel Teske committed
92 93 94 95
        break;
    case ProjectExplorer::Internal::MsvcToolChain::arm:
        arch = Abi::ArmArchitecture;
        wordWidth = 32;
96 97 98 99 100
        break;
    };

    QString msvcVersionString = version;
    if (type == MsvcToolChain::WindowsSDK) {
Daniel Teske's avatar
Daniel Teske committed
101
        if (version == QLatin1String("v7.0") || version.startsWith(QLatin1String("6.")))
102
            msvcVersionString = QLatin1String("9.0");
Daniel Teske's avatar
Daniel Teske committed
103 104
        else if (version == QLatin1String("v7.0A") || version == QLatin1String("v7.1"))
            msvcVersionString = QLatin1String("10.0");
105
    }
Yuchen Deng's avatar
Yuchen Deng committed
106 107 108
    if (msvcVersionString.startsWith(QLatin1String("12.")))
        flavor = Abi::WindowsMsvc2013Flavor;
    else if (msvcVersionString.startsWith(QLatin1String("11.")))
Tobias Hunger's avatar
Tobias Hunger committed
109 110
        flavor = Abi::WindowsMsvc2012Flavor;
    else if (msvcVersionString.startsWith(QLatin1String("10.")))
111
        flavor = Abi::WindowsMsvc2010Flavor;
112
    else if (msvcVersionString.startsWith(QLatin1String("9.")))
113 114 115
        flavor = Abi::WindowsMsvc2008Flavor;
    else
        flavor = Abi::WindowsMsvc2005Flavor;
Tobias Hunger's avatar
Tobias Hunger committed
116 117 118 119 120
    const Abi result = Abi(arch, Abi::WindowsOS, flavor, Abi::PEFormat, wordWidth);
    if (!result.isValid())
        qWarning("Unable to completely determine the ABI of MSVC version %s (%s).",
                 qPrintable(version), qPrintable(result.toString()));
    return result;
121 122
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
static QString generateDisplayName(const QString &name,
                                   MsvcToolChain::Type t,
                                   MsvcToolChain::Platform p)
{
    if (t == MsvcToolChain::WindowsSDK) {
        QString sdkName = name;
        sdkName += platformName(p);
        return sdkName;
    }
    // Comes as "9.0" from the registry
    QString vcName = QLatin1String("Microsoft Visual C++ Compiler ");
    vcName += name;
    vcName += platformName(p);
    return vcName;
}

static QByteArray msvcCompilationFile()
{
    static const char* macros[] = {"_ATL_VER", "_CHAR_UNSIGNED", "__CLR_VER",
                                   "__cplusplus_cli", "__COUNTER__", "__cplusplus",
                                   "_CPPLIB_VER", "_CPPRTTI", "_CPPUNWIND",
                                   "_DEBUG", "_DLL", "__FUNCDNAME__",
145 146 147 148 149 150 151
                                   "__FUNCSIG__", "__FUNCTION__", "_INTEGRAL_MAX_BITS",
                                   "_M_ALPHA", "_M_AAMD64", "_M_CEE", "_M_CEE_PURE",
                                   "_M_CEE_SAFE", "_M_IX86", "_M_IA64",
                                   "_M_IX86_FP", "_M_MPPC", "_M_MRX000",
                                   "_M_PPC", "_M_X64", "_MANAGED",
                                   "_MFC_VER", "_MSC_BUILD", "_MSC_EXTENSIONS",
                                   "_MSC_FULL_VER", "_MSC_VER", "__MSVC_RUNTIME_CHECKS",
152 153
                                   "_MT", "_NATIVE_WCHAR_T_DEFINED", "_OPENMP",
                                   "_VC_NODEFAULTLIB", "_WCHAR_T_DEFINED", "_WIN32",
154
                                   "_WIN32_WCE", "_WIN64", "_Wp64",
155 156 157 158 159 160 161 162 163 164 165 166 167
                                   "__DATE__", "__TIME__", "__TIMESTAMP__",
                                   0};
    QByteArray file = "#define __PPOUT__(x) V##x=x\n\n";
    for (int i = 0; macros[i] != 0; ++i) {
        const QByteArray macro(macros[i]);
        file += "#if defined(" + macro + ")\n__PPOUT__("
                + macro + ")\n#endif\n";
    }
    file += "\nvoid main(){}\n\n";
    return file;
}

// Run MSVC 'cl' compiler to obtain #defines.
168 169
QByteArray MsvcToolChain::msvcPredefinedMacros(const QStringList cxxflags,
                                               const Utils::Environment &env) const
170
{
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    QByteArray predefinedMacros = AbstractMsvcToolChain::msvcPredefinedMacros(cxxflags, env);

    QStringList toProcess;
    foreach (const QString &arg, cxxflags) {
        if (arg.startsWith(QLatin1String("/D"))) {
            QString define = arg.mid(2);
            int pos = define.indexOf(QLatin1Char('='));
            if (pos < 0) {
                predefinedMacros += "#define ";
                predefinedMacros += define.toLocal8Bit();
                predefinedMacros += '\n';
            } else {
                predefinedMacros += "#define ";
                predefinedMacros += define.left(pos).toLocal8Bit();
                predefinedMacros += ' ';
                predefinedMacros += define.mid(pos + 1).toLocal8Bit();
                predefinedMacros += '\n';
            }
        } else if (arg.startsWith(QLatin1String("/U"))) {
            predefinedMacros += "#undef ";
            predefinedMacros += arg.mid(2).toLocal8Bit();
            predefinedMacros += '\n';
193 194
        } else if (arg.startsWith(QLatin1String("-I"))) {
            // Include paths should not have any effect on defines
195 196 197 198
        } else {
            toProcess.append(arg);
        }
    }
199

200
    Utils::TempFileSaver saver(QDir::tempPath() + QLatin1String("/envtestXXXXXX.cpp"));
201 202 203 204
    saver.write(msvcCompilationFile());
    if (!saver.finalize()) {
        qWarning("%s: %s", Q_FUNC_INFO, qPrintable(saver.errorString()));
        return predefinedMacros;
205 206
    }
    QProcess cpp;
Friedemann Kleint's avatar
Friedemann Kleint committed
207
    cpp.setEnvironment(env.toStringList());
208 209
    cpp.setWorkingDirectory(QDir::tempPath());
    QStringList arguments;
Friedemann Kleint's avatar
Friedemann Kleint committed
210 211 212 213 214 215
    const QString binary = env.searchInPath(QLatin1String("cl.exe"));
    if (binary.isEmpty()) {
        qWarning("%s: The compiler binary cl.exe could not be found in the path.", Q_FUNC_INFO);
        return predefinedMacros;
    }

216
    arguments << toProcess << QLatin1String("/EP") << QDir::toNativeSeparators(saver.fileName());
Friedemann Kleint's avatar
Friedemann Kleint committed
217
    cpp.start(binary, arguments);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    if (!cpp.waitForStarted()) {
        qWarning("%s: Cannot start '%s': %s", Q_FUNC_INFO, qPrintable(binary),
            qPrintable(cpp.errorString()));
        return predefinedMacros;
    }
    cpp.closeWriteChannel();
    if (!cpp.waitForFinished()) {
        Utils::SynchronousProcess::stopProcess(cpp);
        qWarning("%s: Timeout running '%s'.", Q_FUNC_INFO, qPrintable(binary));
        return predefinedMacros;
    }
    if (cpp.exitStatus() != QProcess::NormalExit) {
        qWarning("%s: '%s' crashed.", Q_FUNC_INFO, qPrintable(binary));
        return predefinedMacros;
    }

    const QList<QByteArray> output = cpp.readAllStandardOutput().split('\n');
    foreach (const QByteArray& line, output) {
        if (line.startsWith('V')) {
            QList<QByteArray> split = line.split('=');
            const QByteArray key = split.at(0).mid(1);
            QByteArray value = split.at(1);
240
            if (!value.isEmpty())
241 242 243 244 245 246 247 248
                value.chop(1); //remove '\n'
            predefinedMacros += "#define ";
            predefinedMacros += key;
            predefinedMacros += ' ';
            predefinedMacros += value;
            predefinedMacros += '\n';
        }
    }
Friedemann Kleint's avatar
Friedemann Kleint committed
249 250
    if (debug)
        qDebug() << "msvcPredefinedMacros" << predefinedMacros;
251 252 253
    return predefinedMacros;
}

Friedemann Kleint's avatar
Friedemann Kleint committed
254 255 256
// Windows: Expand the delayed evaluation references returned by the
// SDK setup scripts: "PATH=!Path!;foo". Some values might expand
// to empty and should not be added
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static QString winExpandDelayedEnvReferences(QString in, const Utils::Environment &env)
{
    const QChar exclamationMark = QLatin1Char('!');
    for (int pos = 0; pos < in.size(); ) {
        // Replace "!REF!" by its value in process environment
        pos = in.indexOf(exclamationMark, pos);
        if (pos == -1)
            break;
        const int nextPos = in.indexOf(exclamationMark, pos + 1);
        if (nextPos == -1)
            break;
        const QString var = in.mid(pos + 1, nextPos - pos - 1);
        const QString replacement = env.value(var.toUpper());
        in.replace(pos, nextPos + 1 - pos, replacement);
        pos += replacement.size();
    }
    return in;
}

SteveKing's avatar
SteveKing committed
276
Utils::Environment MsvcToolChain::readEnvironmentSetting(Utils::Environment& env) const
277
{
Friedemann Kleint's avatar
Friedemann Kleint committed
278
    Utils::Environment result = env;
SteveKing's avatar
SteveKing committed
279
    if (!QFileInfo(m_vcvarsBat).exists())
Friedemann Kleint's avatar
Friedemann Kleint committed
280
        return result;
281

SteveKing's avatar
SteveKing committed
282 283
    QMap<QString, QString> envPairs;
    if (!generateEnvironmentSettings(env, m_vcvarsBat, m_varsBatArg, envPairs))
284
        return result;
Friedemann Kleint's avatar
Friedemann Kleint committed
285

SteveKing's avatar
SteveKing committed
286 287 288 289 290 291
    // Now loop through and process them
    QMap<QString,QString>::const_iterator envIter;
    for (envIter = envPairs.begin(); envIter!=envPairs.end(); ++envIter) {
        const QString expandedValue = winExpandDelayedEnvReferences(envIter.value(), env);
        if (!expandedValue.isEmpty())
            result.set(envIter.key(), expandedValue);
292 293
    }

Friedemann Kleint's avatar
Friedemann Kleint committed
294 295 296 297 298 299 300 301 302 303
    if (debug) {
        const QStringList newVars = result.toStringList();
        const QStringList oldVars = env.toStringList();
        QDebug nsp = qDebug().nospace();
        foreach (const QString &n, newVars) {
            if (!oldVars.contains(n))
                nsp << n << '\n';
        }
    }
    return result;
304 305 306 307 308 309
}

// --------------------------------------------------------------------------
// MsvcToolChain
// --------------------------------------------------------------------------

310
MsvcToolChain::MsvcToolChain(const QString &name, const Abi &abi,
311 312
                             const QString &varsBat, const QString &varsBatArg, Detection d) :
    AbstractMsvcToolChain(QLatin1String(Constants::MSVC_TOOLCHAIN_ID), d, abi, varsBat),
SteveKing's avatar
SteveKing committed
313
    m_varsBatArg(varsBatArg)
314 315 316
{
    Q_ASSERT(!name.isEmpty());

317
    setDisplayName(name);
318 319
}

320 321 322 323 324 325 326 327
bool MsvcToolChain::isValid() const
{
    if (!AbstractMsvcToolChain::isValid())
        return false;
    QString vcVarsBat = MsvcToolChainFactory::vcVarsBatFor(QFileInfo(m_vcvarsBat).absolutePath(), m_varsBatArg);
    return QFileInfo(vcVarsBat).exists();
}

328
MsvcToolChain::MsvcToolChain() :
329
    AbstractMsvcToolChain(QLatin1String(Constants::MSVC_TOOLCHAIN_ID), ManualDetection)
330 331 332 333 334 335 336 337 338 339 340 341
{
}

MsvcToolChain *MsvcToolChain::readFromMap(const QVariantMap &data)
{
    MsvcToolChain *tc = new MsvcToolChain;
    if (tc->fromMap(data))
        return tc;
    delete tc;
    return 0;
}

Tobias Hunger's avatar
Tobias Hunger committed
342 343 344 345 346 347
QString MsvcToolChain::type() const
{
    return QLatin1String("msvc");
}

QString MsvcToolChain::typeDisplayName() const
348 349 350 351
{
    return MsvcToolChainFactory::tr("MSVC");
}

352
QList<Utils::FileName> MsvcToolChain::suggestedMkspecList() const
353
{
Tobias Hunger's avatar
Tobias Hunger committed
354 355
    switch (m_abi.osFlavor()) {
    case ProjectExplorer::Abi::WindowsMsvc2005Flavor:
356
        return QList<Utils::FileName>() << Utils::FileName::fromLatin1("win32-msvc2005");
Tobias Hunger's avatar
Tobias Hunger committed
357
    case ProjectExplorer::Abi::WindowsMsvc2008Flavor:
358
        return QList<Utils::FileName>() << Utils::FileName::fromLatin1("win32-msvc2008");
Tobias Hunger's avatar
Tobias Hunger committed
359
    case ProjectExplorer::Abi::WindowsMsvc2010Flavor:
360
        return QList<Utils::FileName>() << Utils::FileName::fromLatin1("win32-msvc2010");
Tobias Hunger's avatar
Tobias Hunger committed
361 362
    case ProjectExplorer::Abi::WindowsMsvc2012Flavor:
        QList<Utils::FileName>()
363 364
            << Utils::FileName::fromLatin1("win32-msvc2012")
            << Utils::FileName::fromLatin1("win32-msvc2010");
Tobias Hunger's avatar
Tobias Hunger committed
365
        break;
Yuchen Deng's avatar
Yuchen Deng committed
366 367
    case ProjectExplorer::Abi::WindowsMsvc2013Flavor:
        QList<Utils::FileName>()
368 369 370
            << Utils::FileName::fromLatin1("win32-msvc2013")
            << Utils::FileName::fromLatin1("win32-msvc2012")
            << Utils::FileName::fromLatin1("win32-msvc2010");
Yuchen Deng's avatar
Yuchen Deng committed
371
        break;
Tobias Hunger's avatar
Tobias Hunger committed
372 373 374
    default:
        break;
    }
375
    return QList<Utils::FileName>();
376 377
}

378 379
QVariantMap MsvcToolChain::toMap() const
{
380
    QVariantMap data = AbstractMsvcToolChain::toMap();
SteveKing's avatar
SteveKing committed
381
    data.insert(QLatin1String(varsBatKeyC), m_vcvarsBat);
382 383 384
    if (!m_varsBatArg.isEmpty())
        data.insert(QLatin1String(varsBatArgKeyC), m_varsBatArg);
    data.insert(QLatin1String(supportedAbiKeyC), m_abi.toString());
385 386 387 388 389 390 391
    return data;
}

bool MsvcToolChain::fromMap(const QVariantMap &data)
{
    if (!ToolChain::fromMap(data))
        return false;
392
    m_vcvarsBat = QDir::fromNativeSeparators(data.value(QLatin1String(varsBatKeyC)).toString());
393 394 395
    m_varsBatArg = data.value(QLatin1String(varsBatArgKeyC)).toString();
    const QString abiString = data.value(QLatin1String(supportedAbiKeyC)).toString();
    m_abi = Abi(abiString);
396

SteveKing's avatar
SteveKing committed
397
    return !m_vcvarsBat.isEmpty() && m_abi.isValid();
398 399
}

SteveKing's avatar
SteveKing committed
400

401 402 403 404 405 406 407
ToolChainConfigWidget *MsvcToolChain::configurationWidget()
{
    return new MsvcToolChainConfigWidget(this);
}

ToolChain *MsvcToolChain::clone() const
{
408
    return new MsvcToolChain(*this);
409 410 411 412 413 414 415
}

// --------------------------------------------------------------------------
// MsvcToolChainConfigWidget
// --------------------------------------------------------------------------

MsvcToolChainConfigWidget::MsvcToolChainConfigWidget(ToolChain *tc) :
416 417
    ToolChainConfigWidget(tc),
    m_varsBatDisplayLabel(new QLabel(this))
418
{
419
    m_mainLayout->addRow(new QLabel(tc->displayName()));
420
    m_varsBatDisplayLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
421 422
    m_mainLayout->addRow(tr("Initialization:"), m_varsBatDisplayLabel);
    addErrorLabel();
423
    setFromToolChain();
424 425
}

426
void MsvcToolChainConfigWidget::setFromToolChain()
427
{
428 429
    MsvcToolChain *tc = static_cast<MsvcToolChain *>(toolChain());
    QTC_ASSERT(tc, return);
430
    QString varsBatDisplay = QDir::toNativeSeparators(tc->varsBat());
431 432 433 434 435
    if (!tc->varsBatArg().isEmpty()) {
        varsBatDisplay += QLatin1Char(' ');
        varsBatDisplay += tc->varsBatArg();
    }
    m_varsBatDisplayLabel->setText(varsBatDisplay);
436 437 438 439 440 441
}

// --------------------------------------------------------------------------
// MsvcToolChainFactory
// --------------------------------------------------------------------------

442
MsvcToolChainFactory::MsvcToolChainFactory()
443
{
444 445
    setId(Constants::MSVC_TOOLCHAIN_ID);
    setDisplayName(tr("MSVC"));
446 447
}

448 449 450 451 452 453 454 455 456 457 458 459 460
bool MsvcToolChainFactory::checkForVisualStudioInstallation(const QString &vsName)
{
    const QSettings vsRegistry(
#ifdef Q_OS_WIN64
                QLatin1String("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VS7"),
#else
                QLatin1String("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7"),
#endif
                QSettings::NativeFormat);

    return vsRegistry.contains(vsName);
}

461 462
QString MsvcToolChainFactory::vcVarsBatFor(const QString &basePath, const QString &toolchainName)
{
463 464
    if (toolchainName.startsWith(QLatin1Char('/'))) // windows sdk case, all use SetEnv.cmd
        return basePath + QLatin1String("/SetEnv.cmd");
465 466
    if (toolchainName == QLatin1String("x86"))
        return basePath + QLatin1String("/bin/vcvars32.bat");
467 468
    if (toolchainName == QLatin1String("amd64_arm"))
        return basePath + QLatin1String("/bin/amd64_arm/vcvarsamd64_arm.bat");
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    if (toolchainName == QLatin1String("x86_amd64"))
        return basePath + QLatin1String("/bin/x86_amd64/vcvarsx86_amd64.bat");
    if (toolchainName == QLatin1String("amd64"))
        return basePath + QLatin1String("/bin/amd64/vcvars64.bat");
    if (toolchainName == QLatin1String("x86_arm"))
        return basePath + QLatin1String("/bin/x86_arm/vcvarsx86_arm.bat");
    if (toolchainName == QLatin1String("arm"))
        return basePath + QLatin1String("/bin/arm/vcvarsarm.bat");
    if (toolchainName == QLatin1String("ia64"))
        return basePath + QLatin1String("/bin/ia64/vcvars64.bat");
    if (toolchainName == QLatin1String("x86_ia64"))
        return basePath + QLatin1String("/bin/x86_ia64/vcvarsx86_ia64.bat");

    return QString();
}

485 486 487 488 489
QList<ToolChain *> MsvcToolChainFactory::autoDetect()
{
    QList<ToolChain *> results;

    // 1) Installed SDKs preferred over standalone Visual studio
490
    const QSettings sdkRegistry(QLatin1String("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows"),
491 492 493 494 495 496 497 498 499
                                QSettings::NativeFormat);
    const QString defaultSdkPath = sdkRegistry.value(QLatin1String("CurrentInstallFolder")).toString();
    if (!defaultSdkPath.isEmpty()) {
        foreach (const QString &sdkKey, sdkRegistry.childGroups()) {
            const QString name = sdkRegistry.value(sdkKey + QLatin1String("/ProductName")).toString();
            const QString folder = sdkRegistry.value(sdkKey + QLatin1String("/InstallationFolder")).toString();
            if (folder.isEmpty())
                continue;

Yuchen Deng's avatar
Yuchen Deng committed
500 501 502 503 504
            QDir dir(folder);
            if (!dir.cd(QLatin1String("bin")))
                continue;
            QFileInfo fi(dir, QLatin1String("SetEnv.cmd"));
            if (!fi.exists())
505
                continue;
506

Yuchen Deng's avatar
Yuchen Deng committed
507
            QList<ToolChain *> tmp;
508
            tmp.append(new MsvcToolChain(generateDisplayName(name, MsvcToolChain::WindowsSDK, MsvcToolChain::x86),
Daniel Teske's avatar
Daniel Teske committed
509
                                         findAbiOfMsvc(MsvcToolChain::WindowsSDK, MsvcToolChain::x86, sdkKey),
510
                                         fi.absoluteFilePath(), QLatin1String("/x86"), ToolChain::AutoDetection));
511
            // Add all platforms, cross-compiler is automatically selected by SetEnv.cmd if needed
512
            tmp.append(new MsvcToolChain(generateDisplayName(name, MsvcToolChain::WindowsSDK, MsvcToolChain::amd64),
Daniel Teske's avatar
Daniel Teske committed
513
                                         findAbiOfMsvc(MsvcToolChain::WindowsSDK, MsvcToolChain::amd64, sdkKey),
514
                                         fi.absoluteFilePath(), QLatin1String("/x64"), ToolChain::AutoDetection));
515
            tmp.append(new MsvcToolChain(generateDisplayName(name, MsvcToolChain::WindowsSDK, MsvcToolChain::ia64),
Daniel Teske's avatar
Daniel Teske committed
516
                                         findAbiOfMsvc(MsvcToolChain::WindowsSDK, MsvcToolChain::ia64, sdkKey),
517
                                         fi.absoluteFilePath(), QLatin1String("/ia64"), ToolChain::AutoDetection));
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
            // Make sure the default is front.
            if (folder == defaultSdkPath)
                results = tmp + results;
            else
                results += tmp;
        } // foreach
    }

    // 2) Installed MSVCs
    const QSettings vsRegistry(
#ifdef Q_OS_WIN64
                QLatin1String("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7"),
#else
                QLatin1String("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VC7"),
#endif
                QSettings::NativeFormat);
    foreach (const QString &vsName, vsRegistry.allKeys()) {
        // Scan for version major.minor
        const int dotPos = vsName.indexOf(QLatin1Char('.'));
        if (dotPos == -1)
            continue;
539 540
        if (!checkForVisualStudioInstallation(vsName))
            continue;
541

542
        QString path = QDir::fromNativeSeparators(vsRegistry.value(vsName).toString());
543 544
        if (path.endsWith(QLatin1Char('/')))
            path.chop(1);
545
        const int version = vsName.left(dotPos).toInt();
546
        const QString vcvarsAllbat = path + QLatin1String("/vcvarsall.bat");
Daniel Teske's avatar
Daniel Teske committed
547 548
        if (QFileInfo(vcvarsAllbat).isFile()) {
            Utils::HostOsInfo::HostArchitecture arch = Utils::HostOsInfo::hostArchitecture();
549
            if (QFileInfo(vcVarsBatFor(path, QLatin1String("x86"))).isFile())
550 551
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::x86),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::x86, vsName),
552
                                                 vcvarsAllbat, QLatin1String("x86"), ToolChain::AutoDetection));
Daniel Teske's avatar
Daniel Teske committed
553 554

            if (arch == Utils::HostOsInfo::HostArchitectureX86) {
555
                if (QFileInfo(vcVarsBatFor(path, QLatin1String("x86_amd64"))).isFile())
Daniel Teske's avatar
Daniel Teske committed
556 557
                    results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::amd64),
                                                     findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::amd64, vsName),
558
                                                     vcvarsAllbat, QLatin1String("x86_amd64"), ToolChain::AutoDetection));
Daniel Teske's avatar
Daniel Teske committed
559
            } else if (arch == Utils::HostOsInfo::HostArchitectureAMD64) {
560
                if (QFileInfo(vcVarsBatFor(path, QLatin1String("amd64"))).isFile()) {
Daniel Teske's avatar
Daniel Teske committed
561 562
                    results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::amd64),
                                                     findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::amd64, vsName),
563
                                                     vcvarsAllbat, QLatin1String("amd64"), ToolChain::AutoDetection));
564
                } else if (QFileInfo(vcVarsBatFor(path, QLatin1String("x86_amd64"))).isFile()) {
Daniel Teske's avatar
Daniel Teske committed
565 566 567
                    // Fall back to 32 bit to 4 bit
                    results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::amd64),
                                                     findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::amd64, vsName),
568
                                                     vcvarsAllbat, QLatin1String("x86_amd64"), ToolChain::AutoDetection));
Daniel Teske's avatar
Daniel Teske committed
569 570 571
                }
            }

572
            if (arch == Utils::HostOsInfo::HostArchitectureX86) {
573
                if (QFileInfo(vcVarsBatFor(path, QLatin1String("x86_arm"))).isFile())
Daniel Teske's avatar
Daniel Teske committed
574 575
                    results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::arm),
                                                     findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::arm, vsName),
576
                                                     vcvarsAllbat, QLatin1String("x86_arm"), ToolChain::AutoDetection));
577 578 579 580 581 582 583 584 585 586
            } else if (arch == Utils::HostOsInfo::HostArchitectureAMD64) {
                if (QFileInfo(vcVarsBatFor(path, QLatin1String("amd64_arm"))).isFile()) {
                    results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::arm),
                                                     findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::arm, vsName),
                                                     vcvarsAllbat, QLatin1String("amd64_arm"), ToolChain::AutoDetection));
                } else if (QFileInfo(vcVarsBatFor(path, QLatin1String("x86_arm"))).isFile()) {
                    results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::arm),
                                                     findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::arm, vsName),
                                                     vcvarsAllbat, QLatin1String("x86_arm"), ToolChain::AutoDetection));
                }
Daniel Teske's avatar
Daniel Teske committed
587
            } else if (arch == Utils::HostOsInfo::HostArchitectureArm) {
588
                if (QFileInfo(vcVarsBatFor(path, QLatin1String("arm"))).isFile())
Daniel Teske's avatar
Daniel Teske committed
589 590
                    results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::arm),
                                                     findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::arm, vsName),
591
                                                     vcvarsAllbat, QLatin1String("arm"), ToolChain::AutoDetection));
Daniel Teske's avatar
Daniel Teske committed
592 593 594
            }

            if (arch == Utils::HostOsInfo::HostArchitectureItanium) {
595
                if (QFileInfo(vcVarsBatFor(path, QLatin1String("ia64"))).isFile())
Daniel Teske's avatar
Daniel Teske committed
596 597
                    results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::ia64),
                                                     findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::ia64, vsName),
598
                                                     vcvarsAllbat, QLatin1String("ia64"), ToolChain::AutoDetection));
Daniel Teske's avatar
Daniel Teske committed
599 600
            } else if (arch == Utils::HostOsInfo::HostArchitectureX86
                       || arch == Utils::HostOsInfo::HostArchitectureAMD64) {
601
                if (QFileInfo(vcVarsBatFor(path, QLatin1String("x86_ia64"))).isFile())
Daniel Teske's avatar
Daniel Teske committed
602 603
                    results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::ia64),
                                                     findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::ia64, vsName),
604
                                                     vcvarsAllbat, QLatin1String("x86_ia64"), ToolChain::AutoDetection));
605 606
            }
        } else {
Daniel Teske's avatar
Daniel Teske committed
607
            qWarning("Unable to find MSVC setup script %s in version %d", qPrintable(vcvarsAllbat), version);
608 609
        }
    }
SteveKing's avatar
SteveKing committed
610

611 612 613
    return results;
}

614 615 616 617 618 619 620 621
bool MsvcToolChain::operator ==(const ToolChain &other) const
{
    if (!AbstractMsvcToolChain::operator ==(other))
        return false;
    const MsvcToolChain *msvcTc = static_cast<const MsvcToolChain *>(&other);
    return m_varsBatArg == msvcTc->m_varsBatArg;
}

622 623 624 625 626
bool MsvcToolChainFactory::canRestore(const QVariantMap &data)
{
    return idFromMap(data).startsWith(QLatin1String(Constants::MSVC_TOOLCHAIN_ID) + QLatin1Char(':'));
}

627 628
} // namespace Internal
} // namespace ProjectExplorer