wincetoolchain.cpp 16.9 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
SteveKing's avatar
SteveKing committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
SteveKing's avatar
SteveKing committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
SteveKing's avatar
SteveKing committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
SteveKing's avatar
SteveKing committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** 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.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
SteveKing's avatar
SteveKing committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
SteveKing's avatar
SteveKing committed
30 31 32 33 34 35 36 37

#include "wincetoolchain.h"

#include "msvcparser.h"
#include "projectexplorerconstants.h"

#include <utils/qtcassert.h>

38 39 40
#include <QDir>
#include <QFileInfo>
#include <QSettings>
SteveKing's avatar
SteveKing committed
41

42 43
#include <QFormLayout>
#include <QLabel>
SteveKing's avatar
SteveKing committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

#include <QXmlStreamReader>

#define KEY_ROOT "ProjectExplorer.WinCEToolChain."
static const char msvcVerKeyC[] = KEY_ROOT"MSVCVer";
static const char ceVerKeyC[] = KEY_ROOT"CEVer";
static const char binPathKeyC[] = KEY_ROOT"BinPath";
static const char includePathKeyC[] = KEY_ROOT"IncludePath";
static const char libPathKeyC[] = KEY_ROOT"LibPath";
static const char supportedAbiKeyC[] = KEY_ROOT"SupportedAbi";
static const char vcVarsKeyC[] = KEY_ROOT"VCVars";

enum { debug = 0 };

namespace ProjectExplorer {
namespace Internal {

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

// Just decodes from the integer version to the string used in Qt mkspecs
static QString findMsvcVer(int version)
{
hjk's avatar
hjk committed
68
    if (version == 10)
SteveKing's avatar
SteveKing committed
69
        return QLatin1String("msvc2010");
hjk's avatar
hjk committed
70
    if (version == 9)
Nikolai Kosjar's avatar
Nikolai Kosjar committed
71
        return QLatin1String("msvc2008");
SteveKing's avatar
SteveKing committed
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
    return QLatin1String("msvc2005");
}


// 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
static QString winExpandDelayedEnvReferences(QString in, const Utils::Environment &env)
{
    const QString firstDelimit = QLatin1String("$(");
    const QChar secondDelimit = QLatin1Char(')');
    for (int pos = 0; pos < in.size(); ) {
        // Replace "$(REF)" by its value in process environment
        pos = in.indexOf(firstDelimit, pos);
        if (pos == -1)
            break;
        const int replaceStart = pos + firstDelimit.size();
        const int nextPos = in.indexOf(secondDelimit, replaceStart);
        if (nextPos == -1)
            break;
        const QString var = in.mid(replaceStart, nextPos - replaceStart);
        QString replacement = env.value(var.toUpper());
        if (replacement.isEmpty()) {
            qWarning() << "No replacement for var: " << var;
            pos = nextPos;
97
        } else {
SteveKing's avatar
SteveKing committed
98
            // Not sure about this, but we need to account for the case where
Sergio Ahumada's avatar
Sergio Ahumada committed
99
            // the end of the replacement doesn't have the directory separator and
SteveKing's avatar
SteveKing committed
100 101 102 103
            // neither does the start of the insert. This solution assumes:
            //  1) Having \\ in a path is valid (it is on WinXP)
            //  2) We're only replacing in paths. This will cause problems if there's
            //     a replace of a string
104 105
            if (!replacement.endsWith(QLatin1Char('\\')))
                replacement += QLatin1Char('\\');
SteveKing's avatar
SteveKing committed
106 107 108 109 110 111 112 113 114 115 116

            in.replace(pos, nextPos + 1 - pos, replacement);
            pos += replacement.size();
        }
    }
    return in;
}

// This is pretty much the same as the ReadEnvironmentSetting in the msvctoolchain.cpp, but
// this takes account of the library, binary and include paths to replace the vcvars versions
// with the ones for this toolchain.
hjk's avatar
hjk committed
117
Utils::Environment WinCEToolChain::readEnvironmentSetting(Utils::Environment &env) const
SteveKing's avatar
SteveKing committed
118 119
{
    Utils::Environment result = env;
120
    if (!QFileInfo::exists(m_vcvarsBat))
SteveKing's avatar
SteveKing committed
121 122 123 124 125 126 127 128 129
        return result;

    // Get the env pairs
    QMap<QString, QString> envPairs;

    if (!generateEnvironmentSettings(env, m_vcvarsBat, QString(), envPairs))
        return result;

    QMap<QString,QString>::const_iterator envPairIter;
130
    for (envPairIter = envPairs.constBegin(); envPairIter!=envPairs.constEnd(); ++envPairIter) {
SteveKing's avatar
SteveKing committed
131 132
        // Replace the env values with those from the WinCE SDK
        QString varValue = envPairIter.value();
133 134 135
        if (envPairIter.key() == QLatin1String("PATH"))
            varValue = m_binPath + QLatin1Char(';') + varValue;
        else if (envPairIter.key() == QLatin1String("INCLUDE"))
SteveKing's avatar
SteveKing committed
136
            varValue = m_includePath;
137
        else if (envPairIter.key() == QLatin1String("LIB"))
SteveKing's avatar
SteveKing committed
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
            varValue = m_libPath;

        if (!varValue.isEmpty())
            result.set(envPairIter.key(), varValue);
    }


    // Now loop round and do the delayed expansion
    Utils::Environment::const_iterator envIter = result.constBegin();
    while (envIter != result.constEnd()) {
        const QString key = result.key(envIter);
        const QString unexpandedValue = result.value(envIter);

        const QString expandedValue = winExpandDelayedEnvReferences(unexpandedValue, result);

        result.set(key, expandedValue);

        ++envIter;
    }

    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;
}

// Used to parse an SDK entry in the config file and extract information about this SDK
static bool parseSDK(QXmlStreamReader& theReader,
                     Abi::Architecture& sdkArch,
                     QString& sdkName,
                     QString& ceVer,
                     QString& binPath,
                     QString& includePath,
                     QString& libPath)
{
    sdkArch = Abi::UnknownArchitecture;
180
    sdkName.clear();
SteveKing's avatar
SteveKing committed
181 182 183 184 185 186 187 188

    // Loop through until either the end of the file or until is gets to the next
    // end element.
    while (!theReader.atEnd()) {
        theReader.readNext();

        if (theReader.isEndElement()) {
            // Got to the end element so return...
189
            if (theReader.name() == QLatin1String("Platform"))
SteveKing's avatar
SteveKing committed
190 191 192
                return (sdkArch!=Abi::UnknownArchitecture && !sdkName.isEmpty());
        } else if (theReader.isStartElement()) {
            const QStringRef elemName = theReader.name();
193
            if (elemName == QLatin1String("PlatformName")) {
SteveKing's avatar
SteveKing committed
194
                sdkName = theReader.readElementText();
195
            } else if (elemName == QLatin1String("Directories")) {
SteveKing's avatar
SteveKing committed
196 197
                // Populate the paths from this element. Note: we remove the
                // $(PATH) from the binPath as this will be pre-pended in code
198 199 200 201 202
                binPath = theReader.attributes().value(QLatin1String("Path")).toString();
                binPath.remove(QLatin1String("$(PATH)"));
                includePath = theReader.attributes().value(QLatin1String("Include")).toString();
                libPath = theReader.attributes().value(QLatin1String("Library")).toString();
            } else if (elemName == QLatin1String("OSMajorVersion")) {
SteveKing's avatar
SteveKing committed
203 204 205 206
                // Qt only supports CE5 and higher so drop out here if this version is
                // invalid
                ceVer = theReader.readElementText();
                if (ceVer.toInt() < 5) {
207 208
                    if (debug)
                        qDebug("Ignoring SDK '%s'. Windows CE version %d is unsupported.", qPrintable(sdkName), ceVer.toInt());
SteveKing's avatar
SteveKing committed
209 210
                    return false;
                }
211
            } else if (elemName == QLatin1String("Macro")) {
SteveKing's avatar
SteveKing committed
212
                // Pull out the architecture from the macro values.
213 214
                if (theReader.attributes().value(QLatin1String("Name")) == QLatin1String("ARCHFAM")) {
                    const QStringRef archFam = theReader.attributes().value(QLatin1String("Value"));
SteveKing's avatar
SteveKing committed
215

216
                    if (archFam == QLatin1String("ARM"))
SteveKing's avatar
SteveKing committed
217
                        sdkArch = Abi::ArmArchitecture;
218
                    else if (archFam == QLatin1String("x86"))
SteveKing's avatar
SteveKing committed
219
                        sdkArch = Abi::X86Architecture;
220
                    else if (archFam == QLatin1String("MIPS"))
SteveKing's avatar
SteveKing committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                        sdkArch = Abi::MipsArchitecture;
                }
            }
        }
    }

    // If we've got to here then the end of the file has been reached before the
    // end of element tag, so return error.
    return false;
}

// --------------------------------------------------------------------------
// WinCEToolChain
// --------------------------------------------------------------------------

WinCEToolChain::WinCEToolChain(const QString &name,
                               const Abi &abi,
                               const QString &vcvarsBat,
                               const QString &msvcVer,
                               const QString &ceVer,
                               const QString &binPath,
                               const QString &includePath,
                               const QString &libPath,
244
                               Detection d) :
245
    AbstractMsvcToolChain(Constants::WINCE_TOOLCHAIN_TYPEID, d, abi, vcvarsBat),
SteveKing's avatar
SteveKing committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    m_msvcVer(msvcVer),
    m_ceVer(ceVer),
    m_binPath(binPath),
    m_includePath(includePath),
    m_libPath(libPath)
{
    Q_ASSERT(!name.isEmpty());
    Q_ASSERT(!m_binPath.isEmpty());
    Q_ASSERT(!m_includePath.isEmpty());
    Q_ASSERT(!m_libPath.isEmpty());

    setDisplayName(name);
}

WinCEToolChain::WinCEToolChain() :
261
    AbstractMsvcToolChain(Constants::WINCE_TOOLCHAIN_TYPEID, ManualDetection)
SteveKing's avatar
SteveKing committed
262 263 264 265 266 267 268 269 270 271 272 273
{
}

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

Tobias Hunger's avatar
Tobias Hunger committed
274
QString WinCEToolChain::typeDisplayName() const
SteveKing's avatar
SteveKing committed
275 276 277 278
{
    return WinCEToolChainFactory::tr("WinCE");
}

279
QList<Utils::FileName> WinCEToolChain::suggestedMkspecList() const
SteveKing's avatar
SteveKing committed
280
{
281
    const QChar specSeperator(QLatin1Char('-'));
SteveKing's avatar
SteveKing committed
282 283 284 285 286 287 288 289 290

    QString specString = QLatin1String("wince");

    specString += m_ceVer;
    specString += specSeperator;
    specString += Abi::toString(m_abi.architecture());
    specString += specSeperator;
    specString += m_msvcVer;

291
    return QList<Utils::FileName>() << Utils::FileName::fromString(specString);
SteveKing's avatar
SteveKing committed
292 293 294 295 296 297 298 299 300 301 302
}


QString WinCEToolChain::ceVer() const
{
    return m_ceVer;
}


QVariantMap WinCEToolChain::toMap() const
{
303
    QVariantMap data = AbstractMsvcToolChain::toMap();
SteveKing's avatar
SteveKing committed
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

    data.insert(QLatin1String(msvcVerKeyC), m_msvcVer);
    data.insert(QLatin1String(ceVerKeyC), m_ceVer);
    data.insert(QLatin1String(binPathKeyC), m_binPath);
    data.insert(QLatin1String(includePathKeyC), m_includePath);
    data.insert(QLatin1String(libPathKeyC), m_libPath);
    data.insert(QLatin1String(vcVarsKeyC), m_vcvarsBat);

    data.insert(QLatin1String(supportedAbiKeyC), m_abi.toString());
    return data;
}

bool WinCEToolChain::fromMap(const QVariantMap &data)
{
    if (!ToolChain::fromMap(data))
        return false;

    m_msvcVer = data.value(QLatin1String(msvcVerKeyC)).toString();
    m_ceVer = data.value(QLatin1String(ceVerKeyC)).toString();
    m_binPath = data.value(QLatin1String(binPathKeyC)).toString();
    m_includePath = data.value(QLatin1String(includePathKeyC)).toString();
    m_libPath = data.value(QLatin1String(libPathKeyC)).toString();
    m_vcvarsBat = data.value(QLatin1String(vcVarsKeyC)).toString();

    const QString abiString = data.value(QLatin1String(supportedAbiKeyC)).toString();
    m_abi = Abi(abiString);

    return isValid();
}

ToolChainConfigWidget *WinCEToolChain::configurationWidget()
{
    return new WinCEToolChainConfigWidget(this);
}

ToolChain *WinCEToolChain::clone() const
{
    return new WinCEToolChain(*this);
}



// --------------------------------------------------------------------------
// WinCEToolChainFactory
// --------------------------------------------------------------------------

350
WinCEToolChainFactory::WinCEToolChainFactory()
SteveKing's avatar
SteveKing committed
351
{
352
    setDisplayName(tr("WinCE"));
SteveKing's avatar
SteveKing committed
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
}


QList<ToolChain *> WinCEToolChainFactory::autoDetect()
{
    QList<ToolChain *> results;

    // 1) Installed WinCEs
    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;

        const QString path = QDir::fromNativeSeparators(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");
380
        QFile cePlatforms(path + QLatin1String("vcpackages/WCE.VCPlatform.config"));
SteveKing's avatar
SteveKing committed
381 382 383 384 385 386 387 388 389 390

        if (cePlatforms.exists()) {
            const QString msvcVer = findMsvcVer(version);
            cePlatforms.open(QIODevice::ReadOnly);
            QXmlStreamReader platformReader(&cePlatforms);

            // Rip through the config file getting all of the installed platforms.
            while (!platformReader.atEnd()) {
                platformReader.readNext();
                if (platformReader.isStartElement()) {
391
                    if (platformReader.name() == QLatin1String("Platform")) {
SteveKing's avatar
SteveKing committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
                        Abi::Architecture theArch;
                        QString thePlat;
                        QString binPath;
                        QString includePath;
                        QString libPath;
                        QString ceVer;

                        if (parseSDK(platformReader, theArch, thePlat, ceVer, binPath, includePath, libPath)) {
                            WinCEToolChain *pChain = new WinCEToolChain(thePlat,
                                                                        Abi(theArch, Abi::WindowsOS, Abi::WindowsCEFlavor, Abi::PEFormat, 32),
                                                                        vcvars32bat,
                                                                        msvcVer,
                                                                        ceVer,
                                                                        binPath,
                                                                        includePath,
                                                                        libPath,
408
                                                                        ToolChain::AutoDetection);
SteveKing's avatar
SteveKing committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422
                            results.append(pChain);
                        }
                    }
                }
            }
        }
    }

    return results;
}


QString WinCEToolChain::autoDetectCdbDebugger(QStringList *checkedDirectories /* = 0 */)
{
423
    Q_UNUSED(checkedDirectories);
SteveKing's avatar
SteveKing committed
424 425 426 427 428
    return QString();
}

bool WinCEToolChainFactory::canRestore(const QVariantMap &data)
{
429
    return typeIdFromMap(data) == Constants::WINCE_TOOLCHAIN_TYPEID;
SteveKing's avatar
SteveKing committed
430 431
}

432 433 434 435 436 437 438 439 440
bool WinCEToolChain::operator ==(const ToolChain &other) const
{
    if (!AbstractMsvcToolChain::operator ==(other))
        return false;

    const WinCEToolChain *ceTc = static_cast<const WinCEToolChain *>(&other);
    return m_ceVer == ceTc->m_ceVer;
}

SteveKing's avatar
SteveKing committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
ToolChain *WinCEToolChainFactory::restore(const QVariantMap &data)
{
    return WinCEToolChain::readFromMap(data);
}

// --------------------------------------------------------------------------
// WinCEToolChainConfigWidget
// --------------------------------------------------------------------------

WinCEToolChainConfigWidget::WinCEToolChainConfigWidget(ToolChain *tc) :
    ToolChainConfigWidget(tc)
{
    WinCEToolChain *toolChain = static_cast<WinCEToolChain *>(tc);
    QTC_ASSERT(tc, return);

456 457 458 459
    m_mainLayout->addRow(tr("SDK:"), new QLabel(toolChain->displayName()));
    m_mainLayout->addRow(tr("WinCE Version:"), new QLabel(toolChain->ceVer()));
    m_mainLayout->addRow(tr("ABI:"), new QLabel(toolChain->targetAbi().toString()));
    addErrorLabel();
SteveKing's avatar
SteveKing committed
460 461 462 463
}

} // namespace Internal
} // namespace ProjectExplorer