msvctoolchain.cpp 24.9 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** 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
#include "msvcparser.h"
#include "projectexplorerconstants.h"
34
#include "headerpath.h"
35 36 37 38

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorersettings.h>

39
#include <utils/fileutils.h>
40
#include <utils/synchronousprocess.h>
41
#include <utils/winutils.h>
SteveKing's avatar
SteveKing committed
42
#include <utils/qtcassert.h>
43

44
#include <QDir>
45
#include <QFileInfo>
46 47 48 49 50
#include <QProcess>
#include <QSettings>
#include <QUrl>
#include <QFormLayout>
#include <QDesktopServices>
51

52 53 54 55
#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";
56

Friedemann Kleint's avatar
Friedemann Kleint committed
57 58
enum { debug = 0 };

59 60 61 62 63 64 65 66 67 68
namespace ProjectExplorer {
namespace Internal {

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

static QString platformName(MsvcToolChain::Platform t)
{
    switch (t) {
69
    case MsvcToolChain::x86:
70 71 72
        return QLatin1String(" (x86)");
    case MsvcToolChain::amd64:
        return QLatin1String(" (amd64)");
73 74 75 76 77 78
    case MsvcToolChain::x86_amd64:
        return QLatin1String(" (x86_amd64)");
    case MsvcToolChain::ia64:
        return QLatin1String(" (ia64)");
    case MsvcToolChain::x86_ia64:
        return QLatin1String(" (x86_ia64)");
79 80 81 82
    }
    return QString();
}

83 84 85 86 87 88 89 90
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)
    {
91
    case ProjectExplorer::Internal::MsvcToolChain::x86:
92 93 94
        wordWidth = 32;
        break;
    case ProjectExplorer::Internal::MsvcToolChain::ia64:
95
    case ProjectExplorer::Internal::MsvcToolChain::x86_ia64:
96 97 98
        arch = Abi::ItaniumArchitecture;
        break;
    case ProjectExplorer::Internal::MsvcToolChain::amd64:
99
    case ProjectExplorer::Internal::MsvcToolChain::x86_amd64:
100 101 102 103 104
        break;
    };

    QString msvcVersionString = version;
    if (type == MsvcToolChain::WindowsSDK) {
105 106 107 108 109 110
        if (version.startsWith(QLatin1String("7.")))
            msvcVersionString = QLatin1String("10.0");
        else if (version.startsWith(QLatin1String("6.1"))
                 || (version.startsWith(QLatin1String("6.0")) && version != QLatin1String("6.0")))
            // The 6.0 SDK is shipping MSVC2005, Starting at 6.0a it is MSVC2008.
            msvcVersionString = QLatin1String("9.0");
111
        else
112
            msvcVersionString = QLatin1String("8.0");
113
    }
Tobias Hunger's avatar
Tobias Hunger committed
114 115 116
    if (msvcVersionString.startsWith(QLatin1String("11.")))
        flavor = Abi::WindowsMsvc2012Flavor;
    else if (msvcVersionString.startsWith(QLatin1String("10.")))
117
        flavor = Abi::WindowsMsvc2010Flavor;
118
    else if (msvcVersionString.startsWith(QLatin1String("9.")))
119 120 121
        flavor = Abi::WindowsMsvc2008Flavor;
    else
        flavor = Abi::WindowsMsvc2005Flavor;
Tobias Hunger's avatar
Tobias Hunger committed
122 123 124 125 126
    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;
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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__",
151 152 153 154 155 156 157
                                   "__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",
158 159
                                   "_MT", "_NATIVE_WCHAR_T_DEFINED", "_OPENMP",
                                   "_VC_NODEFAULTLIB", "_WCHAR_T_DEFINED", "_WIN32",
160
                                   "_WIN32_WCE", "_WIN64", "_Wp64",
161 162 163 164 165 166 167 168 169 170 171 172 173
                                   "__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.
174 175
QByteArray MsvcToolChain::msvcPredefinedMacros(const QStringList cxxflags,
                                               const Utils::Environment &env) const
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
    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';
        } else {
            toProcess.append(arg);
        }
    }
203

204
    Utils::TempFileSaver saver(QDir::tempPath() + QLatin1String("/envtestXXXXXX.cpp"));
205 206 207 208
    saver.write(msvcCompilationFile());
    if (!saver.finalize()) {
        qWarning("%s: %s", Q_FUNC_INFO, qPrintable(saver.errorString()));
        return predefinedMacros;
209 210
    }
    QProcess cpp;
Friedemann Kleint's avatar
Friedemann Kleint committed
211
    cpp.setEnvironment(env.toStringList());
212 213
    cpp.setWorkingDirectory(QDir::tempPath());
    QStringList arguments;
Friedemann Kleint's avatar
Friedemann Kleint committed
214 215 216 217 218 219
    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;
    }

220
    arguments << toProcess << QLatin1String("/EP") << QDir::toNativeSeparators(saver.fileName());
Friedemann Kleint's avatar
Friedemann Kleint committed
221
    cpp.start(binary, arguments);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    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);
244
            if (!value.isEmpty())
245 246 247 248 249 250 251 252
                value.chop(1); //remove '\n'
            predefinedMacros += "#define ";
            predefinedMacros += key;
            predefinedMacros += ' ';
            predefinedMacros += value;
            predefinedMacros += '\n';
        }
    }
Friedemann Kleint's avatar
Friedemann Kleint committed
253 254
    if (debug)
        qDebug() << "msvcPredefinedMacros" << predefinedMacros;
255 256 257
    return predefinedMacros;
}

Friedemann Kleint's avatar
Friedemann Kleint committed
258 259 260
// 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
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
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
280
Utils::Environment MsvcToolChain::readEnvironmentSetting(Utils::Environment& env) const
281
{
Friedemann Kleint's avatar
Friedemann Kleint committed
282
    Utils::Environment result = env;
SteveKing's avatar
SteveKing committed
283
    if (!QFileInfo(m_vcvarsBat).exists())
Friedemann Kleint's avatar
Friedemann Kleint committed
284
        return result;
285

SteveKing's avatar
SteveKing committed
286 287
    QMap<QString, QString> envPairs;
    if (!generateEnvironmentSettings(env, m_vcvarsBat, m_varsBatArg, envPairs))
288
        return result;
Friedemann Kleint's avatar
Friedemann Kleint committed
289

SteveKing's avatar
SteveKing committed
290 291 292 293 294 295
    // 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);
296 297
    }

Friedemann Kleint's avatar
Friedemann Kleint committed
298 299 300 301 302 303 304 305 306 307
    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;
308 309 310 311 312 313
}

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

314
MsvcToolChain::MsvcToolChain(const QString &name, const Abi &abi,
315
                             const QString &varsBat, const QString &varsBatArg, bool autodetect) :
SteveKing's avatar
SteveKing committed
316 317
    AbstractMsvcToolChain(QLatin1String(Constants::MSVC_TOOLCHAIN_ID), autodetect, abi, varsBat),
    m_varsBatArg(varsBatArg)
318 319 320
{
    Q_ASSERT(!name.isEmpty());

321
    setDisplayName(name);
322 323
}

324
MsvcToolChain::MsvcToolChain() :
SteveKing's avatar
SteveKing committed
325
    AbstractMsvcToolChain(QLatin1String(Constants::MSVC_TOOLCHAIN_ID), false)
326 327 328 329 330 331 332 333 334 335 336 337
{
}

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
338 339 340 341 342 343
QString MsvcToolChain::type() const
{
    return QLatin1String("msvc");
}

QString MsvcToolChain::typeDisplayName() const
344 345 346 347
{
    return MsvcToolChainFactory::tr("MSVC");
}

348
QList<Utils::FileName> MsvcToolChain::suggestedMkspecList() const
349
{
Tobias Hunger's avatar
Tobias Hunger committed
350 351
    switch (m_abi.osFlavor()) {
    case ProjectExplorer::Abi::WindowsMsvc2005Flavor:
352
        return QList<Utils::FileName>() << Utils::FileName::fromString(QLatin1String("win32-msvc2005"));
Tobias Hunger's avatar
Tobias Hunger committed
353
    case ProjectExplorer::Abi::WindowsMsvc2008Flavor:
354
        return QList<Utils::FileName>() << Utils::FileName::fromString(QLatin1String("win32-msvc2008"));
Tobias Hunger's avatar
Tobias Hunger committed
355
    case ProjectExplorer::Abi::WindowsMsvc2010Flavor:
356
        return QList<Utils::FileName>() << Utils::FileName::fromString(QLatin1String("win32-msvc2010"));
Tobias Hunger's avatar
Tobias Hunger committed
357 358 359 360 361 362 363 364
    case ProjectExplorer::Abi::WindowsMsvc2012Flavor:
        QList<Utils::FileName>()
            << Utils::FileName::fromString(QLatin1String("win32-msvc2012"))
            << Utils::FileName::fromString(QLatin1String("win32-msvc2010"));
        break;
    default:
        break;
    }
365
    return QList<Utils::FileName>();
366 367
}

368 369 370
QVariantMap MsvcToolChain::toMap() const
{
    QVariantMap data = ToolChain::toMap();
SteveKing's avatar
SteveKing committed
371
    data.insert(QLatin1String(varsBatKeyC), m_vcvarsBat);
372 373 374
    if (!m_varsBatArg.isEmpty())
        data.insert(QLatin1String(varsBatArgKeyC), m_varsBatArg);
    data.insert(QLatin1String(supportedAbiKeyC), m_abi.toString());
375 376 377 378 379 380 381
    return data;
}

bool MsvcToolChain::fromMap(const QVariantMap &data)
{
    if (!ToolChain::fromMap(data))
        return false;
SteveKing's avatar
SteveKing committed
382
    m_vcvarsBat = data.value(QLatin1String(varsBatKeyC)).toString();
383 384 385
    m_varsBatArg = data.value(QLatin1String(varsBatArgKeyC)).toString();
    const QString abiString = data.value(QLatin1String(supportedAbiKeyC)).toString();
    m_abi = Abi(abiString);
386

SteveKing's avatar
SteveKing committed
387
    return !m_vcvarsBat.isEmpty() && m_abi.isValid();
388 389
}

SteveKing's avatar
SteveKing committed
390

391 392 393 394 395 396 397
ToolChainConfigWidget *MsvcToolChain::configurationWidget()
{
    return new MsvcToolChainConfigWidget(this);
}

ToolChain *MsvcToolChain::clone() const
{
398
    return new MsvcToolChain(*this);
399 400 401 402 403 404 405
}

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

MsvcToolChainConfigWidget::MsvcToolChainConfigWidget(ToolChain *tc) :
406 407
    ToolChainConfigWidget(tc),
    m_varsBatDisplayLabel(new QLabel(this))
408
{
409
    m_mainLayout->addRow(new QLabel(tc->displayName()));
410
    m_varsBatDisplayLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
411 412
    m_mainLayout->addRow(tr("Initialization:"), m_varsBatDisplayLabel);
    addErrorLabel();
413
    setFromToolChain();
414 415
}

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

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

QString MsvcToolChainFactory::displayName() const
{
    return tr("MSVC");
}

QString MsvcToolChainFactory::id() const
{
    return QLatin1String(Constants::MSVC_TOOLCHAIN_ID);
}

442 443 444 445 446 447 448 449 450 451 452 453 454
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);
}

455 456 457 458 459
QList<ToolChain *> MsvcToolChainFactory::autoDetect()
{
    QList<ToolChain *> results;

    // 1) Installed SDKs preferred over standalone Visual studio
460
    const QSettings sdkRegistry(QLatin1String("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows"),
461 462 463 464 465
                                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();
466
            const QString version = sdkRegistry.value(sdkKey + QLatin1String("/ProductVersion")).toString();
467 468 469 470
            const QString folder = sdkRegistry.value(sdkKey + QLatin1String("/InstallationFolder")).toString();
            if (folder.isEmpty())
                continue;

Yuchen Deng's avatar
Yuchen Deng committed
471 472 473 474 475
            QDir dir(folder);
            if (!dir.cd(QLatin1String("bin")))
                continue;
            QFileInfo fi(dir, QLatin1String("SetEnv.cmd"));
            if (!fi.exists())
476
                continue;
477

Yuchen Deng's avatar
Yuchen Deng committed
478
            QList<ToolChain *> tmp;
479 480
            tmp.append(new MsvcToolChain(generateDisplayName(name, MsvcToolChain::WindowsSDK, MsvcToolChain::x86),
                                         findAbiOfMsvc(MsvcToolChain::WindowsSDK, MsvcToolChain::x86, version),
Yuchen Deng's avatar
Yuchen Deng committed
481
                                         fi.absoluteFilePath(), QLatin1String("/x86"), true));
482
            // Add all platforms, cross-compiler is automatically selected by SetEnv.cmd if needed
483 484
            tmp.append(new MsvcToolChain(generateDisplayName(name, MsvcToolChain::WindowsSDK, MsvcToolChain::amd64),
                                         findAbiOfMsvc(MsvcToolChain::WindowsSDK, MsvcToolChain::amd64, version),
485
                                         fi.absoluteFilePath(), QLatin1String("/x64"), true));
486 487
            tmp.append(new MsvcToolChain(generateDisplayName(name, MsvcToolChain::WindowsSDK, MsvcToolChain::x86_amd64),
                                         findAbiOfMsvc(MsvcToolChain::WindowsSDK, MsvcToolChain::x86_amd64, version),
488
                                         fi.absoluteFilePath(), QLatin1String("/x64"), true));
489 490
            tmp.append(new MsvcToolChain(generateDisplayName(name, MsvcToolChain::WindowsSDK, MsvcToolChain::ia64),
                                         findAbiOfMsvc(MsvcToolChain::WindowsSDK, MsvcToolChain::ia64, version),
Yuchen Deng's avatar
Yuchen Deng committed
491
                                         fi.absoluteFilePath(), QLatin1String("/ia64"), true));
492 493
            tmp.append(new MsvcToolChain(generateDisplayName(name, MsvcToolChain::WindowsSDK, MsvcToolChain::x86_ia64),
                                         findAbiOfMsvc(MsvcToolChain::WindowsSDK, MsvcToolChain::x86_ia64, version),
494
                                         fi.absoluteFilePath(), QLatin1String("/ia64"), true));
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
            // 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;
516 517
        if (!checkForVisualStudioInstallation(vsName))
            continue;
518 519 520 521 522 523

        const QString path = vsRegistry.value(vsName).toString();
        const int version = vsName.left(dotPos).toInt();
        // Check existence of various install scripts
        const QString vcvars32bat = path + QLatin1String("bin\\vcvars32.bat");
        if (QFileInfo(vcvars32bat).isFile())
524 525
            results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::x86),
                                             findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::x86, vsName),
526 527 528 529 530
                                             vcvars32bat, QString(), true));
        if (version >= 10) {
            // Just one common file
            const QString vcvarsAllbat = path + QLatin1String("vcvarsall.bat");
            if (QFileInfo(vcvarsAllbat).isFile()) {
531 532
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::x86),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::x86, vsName),
533
                                                 vcvarsAllbat, QLatin1String("x86"), true));
534 535
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::amd64),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::amd64, vsName),
536
                                                 vcvarsAllbat, QLatin1String("amd64"), true));
537 538 539
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::x86_amd64),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::x86_amd64, vsName),
                                                 vcvarsAllbat, QLatin1String("x86_amd64"), true));
540 541
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::ia64),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::ia64, vsName),
542
                                                 vcvarsAllbat, QLatin1String("ia64"), true));
543 544
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::x86_ia64),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::x86_ia64, vsName),
545
                                                 vcvarsAllbat, QLatin1String("x86_ia64"), true));
546 547 548 549 550 551 552
            } else {
                qWarning("Unable to find MSVC setup script %s in version %d", qPrintable(vcvarsAllbat), version);
            }
        } else {
            // Amd 64 is the preferred 64bit platform
            const QString vcvarsAmd64bat = path + QLatin1String("bin\\amd64\\vcvarsamd64.bat");
            if (QFileInfo(vcvarsAmd64bat).isFile())
553 554
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::amd64),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::amd64, vsName),
555
                                                 vcvarsAmd64bat, QString(), true));
556 557 558 559 560
            const QString vcvarsX86_amd64bat = path + QLatin1String("bin\\vcvarsx86_amd64.bat");
            if (QFileInfo(vcvarsX86_amd64bat).isFile())
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::x86_amd64),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::x86_amd64, vsName),
                                                 vcvarsX86_amd64bat, QString(), true));
561 562
            const QString vcvars64bat = path + QLatin1String("bin\\vcvars64.bat");
            if (QFileInfo(vcvars64bat).isFile())
563 564
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::amd64),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::amd64, vsName),
565
                                                 vcvars64bat, QString(), true));
566 567 568 569 570
            const QString vcvarsX86_ia64bat = path + QLatin1String("bin\\vcvarsx86_ia64.bat");
            if (QFileInfo(vcvarsX86_ia64bat).isFile())
                results.append(new MsvcToolChain(generateDisplayName(vsName, MsvcToolChain::VS, MsvcToolChain::x86_ia64),
                                                 findAbiOfMsvc(MsvcToolChain::VS, MsvcToolChain::x86_ia64, vsName),
                                                 vcvarsX86_ia64bat, QString(), true));
571 572
        }
    }
SteveKing's avatar
SteveKing committed
573

574 575 576
    return results;
}

577 578 579 580 581 582 583 584
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;
}

585 586 587 588 589
bool MsvcToolChainFactory::canRestore(const QVariantMap &data)
{
    return idFromMap(data).startsWith(QLatin1String(Constants::MSVC_TOOLCHAIN_ID) + QLatin1Char(':'));
}

590 591
} // namespace Internal
} // namespace ProjectExplorer