1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "toolchain.h"
36 #include "cesdkhandler.h"
37 #include "projectexplorersettings.h"
38 #include "gccparser.h"
39 #include "msvcparser.h"
40 #include "linuxiccparser.h"
42 #include <utils/synchronousprocess.h>
43 #include <utils/qtcprocess.h>
45 #include <QtCore/QDebug>
46 #include <QtCore/QFileInfo>
47 #include <QtCore/QProcess>
48 #include <QtCore/QSettings>
49 #include <QtCore/QDir>
50 #include <QtCore/QTemporaryFile>
51 #include <QtCore/QString>
52 #include <QtCore/QCoreApplication>
54 using namespace ProjectExplorer;
55 using namespace ProjectExplorer::Internal;
60 static const char MSVC_RegKey[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7";
62 static const char MSVC_RegKey[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VC7";
65 bool ToolChain::equals(const ToolChain *a, const ToolChain *b)
71 if (a->type() == b->type())
76 ToolChain::ToolChain()
80 ToolChain::~ToolChain()
84 ToolChain *ToolChain::createGccToolChain(const QString &gcc)
86 return new GccToolChain(gcc);
89 ToolChain *ToolChain::createMinGWToolChain(const QString &gcc, const QString &mingwPath)
91 return new MinGWToolChain(gcc, mingwPath);
94 ToolChain *ToolChain::createLinuxIccToolChain()
96 return new LinuxIccToolChain();
99 ToolChain *ToolChain::createMSVCToolChain(const QString &name, bool amd64)
101 return MSVCToolChain::create(name, amd64);
104 ToolChain *ToolChain::createWinCEToolChain(const QString &name, const QString &platform)
106 return WinCEToolChain::create(name, platform);
109 QStringList ToolChain::availableMSVCVersions()
112 foreach(const MSVCToolChain::Installation &i, MSVCToolChain::installations())
113 rc.push_back(i.name);
117 QStringList ToolChain::availableMSVCVersions(bool amd64)
120 foreach(const MSVCToolChain::Installation &i, MSVCToolChain::installations())
121 if (i.is64bit() == amd64)
122 rc.push_back(i.name);
126 QList<ToolChainType> ToolChain::supportedToolChains()
128 QList<ToolChainType> toolChains;
129 for (int i = 0; i < ToolChain_LAST_VALID; ++i) {
130 toolChains.append(ToolChainType(i));
135 QString ToolChain::toolChainName(ToolChainType tc)
139 return QCoreApplication::translate("ToolChain", "GCC");
140 case ToolChain_LINUX_ICC:
141 return QCoreApplication::translate("ToolChain", "Intel C++ Compiler (Linux)");
142 case ToolChain_MinGW:
143 return QString::fromLatin1("MinGW");
145 return QCoreApplication::translate("ToolChain", "Microsoft Visual C++");
146 case ToolChain_WINCE:
147 return QCoreApplication::translate("ToolChain", "Windows CE");
148 case ToolChain_WINSCW:
149 return QCoreApplication::translate("ToolChain", "WINSCW");
151 return QCoreApplication::translate("ToolChain", "GCCE");
152 case ToolChain_GCCE_GNUPOC:
153 return QCoreApplication::translate("ToolChain", "GCCE/GnuPoc");
154 case ToolChain_RVCT_ARMV5_GNUPOC:
155 return QCoreApplication::translate("ToolChain", "RVCT (ARMV6)/GnuPoc");
156 case ToolChain_RVCT2_ARMV5:
157 return QCoreApplication::translate("ToolChain", "RVCT 2 (ARMV5)");
158 case ToolChain_RVCT2_ARMV6:
159 return QCoreApplication::translate("ToolChain", "RVCT 2 (ARMV6)");
160 case ToolChain_RVCT4_ARMV5:
161 return QCoreApplication::translate("ToolChain", "RVCT 4 (ARMV5)");
162 case ToolChain_RVCT4_ARMV6:
163 return QCoreApplication::translate("ToolChain", "RVCT 4 (ARMV6)");
164 case ToolChain_GCC_MAEMO:
165 return QCoreApplication::translate("ToolChain", "GCC for Maemo");
166 case ToolChain_OTHER:
167 return QCoreApplication::translate("ToolChain", "Other");
168 case ToolChain_INVALID:
169 return QCoreApplication::translate("ToolChain", "<Invalid>");
170 case ToolChain_UNKNOWN:
173 Q_ASSERT("Missing name for Toolchaintype");
175 return QCoreApplication::translate("ToolChain", "<Unknown>");
178 GccToolChain::GccToolChain(const QString &gcc)
184 ToolChainType GccToolChain::type() const
186 return ToolChain_GCC;
189 static QByteArray gccPredefinedMacros(const QString &gcc, const QStringList &env)
191 QStringList arguments;
192 arguments << QLatin1String("-xc++")
193 << QLatin1String("-E")
194 << QLatin1String("-dM")
195 << QLatin1String("-");
198 cpp.setEnvironment(env);
199 cpp.start(gcc, arguments);
200 if (!cpp.waitForStarted()) {
201 qWarning("%s: Cannot start '%s': %s", Q_FUNC_INFO, qPrintable(gcc),
202 qPrintable(cpp.errorString()));
205 cpp.closeWriteChannel();
206 if (!cpp.waitForFinished()) {
207 Utils::SynchronousProcess::stopProcess(cpp);
208 qWarning("%s: Timeout running '%s'.", Q_FUNC_INFO, qPrintable(gcc));
211 if (cpp.exitStatus() != QProcess::NormalExit) {
212 qWarning("%s: '%s' crashed.", Q_FUNC_INFO, qPrintable(gcc));
215 QByteArray predefinedMacros = cpp.readAllStandardOutput();
217 // Turn off flag indicating Apple's blocks support
218 const QByteArray blocksDefine("#define __BLOCKS__ 1");
219 const QByteArray blocksUndefine("#undef __BLOCKS__");
220 const int idx = predefinedMacros.indexOf(blocksDefine);
222 predefinedMacros.replace(idx, blocksDefine.length(), blocksUndefine);
225 // Define __strong and __weak (used for Apple's GC extension of C) to be empty
226 predefinedMacros.append("#define __strong\n");
227 predefinedMacros.append("#define __weak\n");
229 return predefinedMacros;
232 QByteArray GccToolChain::predefinedMacros()
234 if (m_predefinedMacros.isEmpty()) {
235 Utils::Environment env = Utils::Environment::systemEnvironment();
236 addToEnvironment(env);
237 m_predefinedMacros = gccPredefinedMacros(m_gcc, env.toStringList());
239 return m_predefinedMacros;
242 static QList<HeaderPath> gccSystemHeaderPaths(const QString &gcc, Utils::Environment env)
244 QList<HeaderPath> systemHeaderPaths;
245 QStringList arguments;
246 arguments << QLatin1String("-xc++")
247 << QLatin1String("-E")
248 << QLatin1String("-v")
249 << QLatin1String("-");
252 env.set(QLatin1String("LC_ALL"), QLatin1String("C")); //override current locale settings
253 cpp.setEnvironment(env.toStringList());
254 cpp.setReadChannelMode(QProcess::MergedChannels);
255 cpp.start(gcc, arguments);
256 if (!cpp.waitForStarted()) {
257 qWarning("%s: Cannot start '%s': %s", Q_FUNC_INFO, qPrintable(gcc),
258 qPrintable(cpp.errorString()));
259 return systemHeaderPaths;
261 cpp.closeWriteChannel();
262 if (!cpp.waitForFinished()) {
263 Utils::SynchronousProcess::stopProcess(cpp);
264 qWarning("%s: Timeout running '%s'.", Q_FUNC_INFO, qPrintable(gcc));
265 return systemHeaderPaths;
267 if (cpp.exitStatus() != QProcess::NormalExit) {
268 qWarning("%s: '%s' crashed.", Q_FUNC_INFO, qPrintable(gcc));
269 return systemHeaderPaths;
272 while (cpp.canReadLine()) {
273 line = cpp.readLine();
274 if (line.startsWith("#include"))
278 if (! line.isEmpty() && line.startsWith("#include")) {
279 HeaderPath::Kind kind = HeaderPath::UserHeaderPath;
280 while (cpp.canReadLine()) {
281 line = cpp.readLine();
282 if (line.startsWith("#include")) {
283 kind = HeaderPath::GlobalHeaderPath;
284 } else if (! line.isEmpty() && QChar(line.at(0)).isSpace()) {
285 HeaderPath::Kind thisHeaderKind = kind;
287 line = line.trimmed();
288 if (line.endsWith('\n'))
291 const int index = line.indexOf(" (framework directory)");
293 line.truncate(index);
294 thisHeaderKind = HeaderPath::FrameworkHeaderPath;
297 systemHeaderPaths.append(HeaderPath(QFile::decodeName(line), thisHeaderKind));
298 } else if (line.startsWith("End of search list.")) {
301 qWarning() << "ignore line:" << line;
305 return systemHeaderPaths;
308 QList<HeaderPath> GccToolChain::systemHeaderPaths()
310 if (m_systemHeaderPaths.isEmpty()) {
311 Utils::Environment env = Utils::Environment::systemEnvironment();
312 addToEnvironment(env);
313 m_systemHeaderPaths = gccSystemHeaderPaths(m_gcc, env);
315 return m_systemHeaderPaths;
318 void GccToolChain::addToEnvironment(Utils::Environment &env)
323 QString GccToolChain::makeCommand() const
325 return QLatin1String("make");
328 IOutputParser *GccToolChain::outputParser() const
330 return new GccParser;
333 bool GccToolChain::equals(const ToolChain *other) const
335 return (m_gcc == static_cast<const GccToolChain *>(other)->m_gcc);
338 MinGWToolChain::MinGWToolChain(const QString &gcc, const QString &mingwPath)
339 : GccToolChain(gcc), m_mingwPath(mingwPath)
344 ToolChainType MinGWToolChain::type() const
346 return ToolChain_MinGW;
349 bool MinGWToolChain::equals(const ToolChain *other) const
351 const MinGWToolChain *o = static_cast<const MinGWToolChain *>(other);
352 return (m_mingwPath == o->m_mingwPath && this->GccToolChain::equals(other));
355 void MinGWToolChain::addToEnvironment(Utils::Environment &env)
358 qDebug() << "MinGWToolChain::addToEnvironment" << m_mingwPath;
359 if (m_mingwPath.isEmpty())
361 const QString binDir = m_mingwPath + "/bin";
362 if (QFileInfo(binDir).exists())
363 env.prependOrSetPath(binDir);
366 QString MinGWToolChain::makeCommand() const
368 return QLatin1String("mingw32-make.exe");
371 IOutputParser *MinGWToolChain::outputParser() const
373 return new GccParser;
376 LinuxIccToolChain::LinuxIccToolChain()
377 : GccToolChain(QLatin1String("icpc"))
381 ToolChainType LinuxIccToolChain::type() const
383 return ToolChain_LINUX_ICC;
386 IOutputParser *LinuxIccToolChain::outputParser() const
388 return new LinuxIccParser;
391 // ---------------- MSVC installation location code
393 // Format the name of an SDK or VC installation version with platform
394 static inline QString installationName(const QString &name,
395 MSVCToolChain::Installation::Type t,
396 MSVCToolChain::Installation::Platform p)
398 if (t == MSVCToolChain::Installation::WindowsSDK) {
399 QString sdkName = name;
400 sdkName += QLatin1String(" (");
401 sdkName += MSVCToolChain::Installation::platformName(p);
402 sdkName += QLatin1Char(')');
405 // Comes as "9.0" from the registry
406 QString vcName = QLatin1String("Microsoft Visual C++ Compilers ");
408 vcName+= QLatin1String(" (");
409 vcName += MSVCToolChain::Installation::platformName(p);
410 vcName += QLatin1Char(')');
414 MSVCToolChain::Installation::Installation(Type t, const QString &n, Platform p,
415 const QString &v, const QString &a) :
416 type(t), name(installationName(n, t, p)), platform(p), varsBat(v), varsBatArg(a)
420 MSVCToolChain::Installation::Installation() : platform(s32)
424 QString MSVCToolChain::Installation::platformName(Platform t)
428 return QLatin1String("x86");
430 return QLatin1String("x64");
432 return QLatin1String("ia64");
434 return QLatin1String("amd64");
439 bool MSVCToolChain::Installation::is64bit() const
441 return platform != s32;
444 MSVCToolChain::InstallationList MSVCToolChain::installations()
446 static InstallationList installs;
447 static bool firstTime = true;
450 // 1) Installed SDKs preferred over standalone Visual studio
451 const char sdk_RegKeyC[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows";
452 const QSettings sdkRegistry(sdk_RegKeyC, QSettings::NativeFormat);
453 const QString defaultSdkPath = sdkRegistry.value(QLatin1String("CurrentInstallFolder")).toString();
454 if (!defaultSdkPath.isEmpty()) {
455 foreach(const QString &sdkKey, sdkRegistry.childGroups()) {
456 const QString name = sdkRegistry.value(sdkKey + QLatin1String("/ProductName")).toString();
457 const QString folder = sdkRegistry.value(sdkKey + QLatin1String("/InstallationFolder")).toString();
458 if (!folder.isEmpty()) {
459 const QString sdkVcVarsBat = folder + QLatin1String("bin\\SetEnv.cmd");
460 if (QFileInfo(sdkVcVarsBat).exists()) {
462 InstallationList newInstalls;
463 newInstalls.push_back(Installation(Installation::WindowsSDK, name, Installation::s32, sdkVcVarsBat, QLatin1String("/x86")));
465 newInstalls.push_back(Installation(Installation::WindowsSDK, name, Installation::s64, sdkVcVarsBat, QLatin1String("/x64")));
466 newInstalls.push_back(Installation(Installation::WindowsSDK, name, Installation::ia64, sdkVcVarsBat, QLatin1String("/ia64")));
468 // Make sure the default is front.
469 if (folder == defaultSdkPath && !installs.empty()) {
470 const InstallationList old = installs;
471 installs = newInstalls + old;
473 installs.append(newInstalls);
479 // 2) Installed MSVCs
480 const QSettings vsRegistry(MSVC_RegKey, QSettings::NativeFormat);
481 foreach(const QString &vsName, vsRegistry.allKeys()) {
482 const int dotPos = vsName.indexOf(QLatin1Char('.'));
483 if (dotPos != -1) { // Scan for version major.minor
484 const QString path = vsRegistry.value(vsName).toString();
485 const int version = vsName.left(dotPos).toInt();
486 // Check existence of various install scripts
487 const QString vcvars32bat = path + QLatin1String("bin\\vcvars32.bat");
488 if (QFileInfo(vcvars32bat).isFile())
489 installs.push_back(Installation(Installation::VS, vsName, Installation::s32, vcvars32bat));
491 // Just one common file
492 const QString vcvarsAllbat = path + QLatin1String("vcvarsall.bat");
493 if (QFileInfo(vcvarsAllbat).isFile()) {
494 installs.push_back(Installation(Installation::VS, vsName, Installation::s32, vcvarsAllbat, QLatin1String("x86")));
495 installs.push_back(Installation(Installation::VS, vsName, Installation::amd64, vcvarsAllbat, QLatin1String("amd64")));
496 installs.push_back(Installation(Installation::VS, vsName, Installation::s64, vcvarsAllbat, QLatin1String("x64")));
497 installs.push_back(Installation(Installation::VS, vsName, Installation::ia64, vcvarsAllbat, QLatin1String("ia64")));
499 qWarning("Unable to find MSVC setup script %s in version %d", qPrintable(vcvarsAllbat), version);
502 // Amd 64 is the preferred 64bit platform
503 const QString vcvarsAmd64bat = path + QLatin1String("bin\\amd64\\vcvarsamd64.bat");
504 if (QFileInfo(vcvarsAmd64bat).isFile())
505 installs.push_back(Installation(Installation::VS, vsName, Installation::amd64, vcvarsAmd64bat));
506 const QString vcvarsAmd64bat2 = path + QLatin1String("bin\\vcvarsx86_amd64.bat");
507 if (QFileInfo(vcvarsAmd64bat2).isFile())
508 installs.push_back(Installation(Installation::VS, vsName, Installation::amd64, vcvarsAmd64bat2));
509 const QString vcvars64bat = path + QLatin1String("bin\\vcvars64.bat");
510 if (QFileInfo(vcvars64bat).isFile())
511 installs.push_back(Installation(Installation::VS, vsName, Installation::s64, vcvars64bat));
512 const QString vcvarsIA64bat = path + QLatin1String("bin\\vcvarsx86_ia64.bat");
513 if (QFileInfo(vcvarsIA64bat).isFile())
514 installs.push_back(Installation(Installation::VS, vsName, Installation::ia64, vcvarsIA64bat));
520 foreach(const Installation &i, installs)
525 // Return a substring to match the MSVC official version against by mkSpec name.
526 static inline const QString msvcVersionStringFromMkSpec(const QString &mkSpec)
528 if (mkSpec.isEmpty())
530 if (mkSpec.endsWith(QLatin1String("msvc2002")))
531 return QLatin1String(" 7.0");
532 if (mkSpec.endsWith(QLatin1String("msvc2003")))
533 return QLatin1String(" 7.1");
534 if (mkSpec.endsWith(QLatin1String("msvc2005")))
535 return QLatin1String(" 8.0");
536 if (mkSpec.endsWith(QLatin1String("msvc2008")))
537 return QLatin1String(" 9.0");
538 if (mkSpec.endsWith(QLatin1String("msvc2010")))
539 return QLatin1String(" 10.0");
543 MSVCToolChain::Installation MSVCToolChain::findInstallationByMkSpec(bool is64Bit,
544 const QString &mkSpec,
547 const QString mkSpecMatchString = msvcVersionStringFromMkSpec(mkSpec);
548 if (!mkSpecMatchString.isEmpty()) {
549 foreach(const Installation &i, installations()) {
550 if ((i.type == Installation::VS) && (i.is64bit() == is64Bit)
551 && (i.name.indexOf(mkSpecMatchString) != -1))
555 return findInstallationByName(is64Bit, QString(), excludeSDK);
558 MSVCToolChain::Installation MSVCToolChain::findInstallationByName(bool is64Bit,
563 qDebug() << "find" << (is64Bit ? 64 : 32) << name << excludeSDK;
564 foreach(const Installation &i, installations()) {
565 if (i.type != Installation::WindowsSDK || !excludeSDK) {
566 if ((i.is64bit() == is64Bit) && (name.isEmpty() || name == i.name))
570 return Installation();
573 namespace ProjectExplorer {
574 PROJECTEXPLORER_EXPORT QDebug operator<<(QDebug in, const MSVCToolChain::Installation &i)
576 QDebug nsp = in.nospace();
577 nsp << "Type: " << i.type << " Platform: " << i.platform << " Name: " << i.name
578 << "\nSetup: " << i.varsBat;
579 if (!i.varsBatArg.isEmpty())
580 nsp << "\nSetup argument: " << i.varsBatArg;
585 MSVCToolChain *MSVCToolChain::create(const QString &name, bool amd64)
587 return new MSVCToolChain(MSVCToolChain::findInstallationByName(amd64, name));
590 MSVCToolChain::MSVCToolChain(const Installation &in) :
595 qDebug() << "\nMSVCToolChain::CT\n" << m_installation;
598 ToolChainType MSVCToolChain::type() const
600 return ToolChain_MSVC;
603 bool MSVCToolChain::equals(const ToolChain *other) const
605 const MSVCToolChain *o = static_cast<const MSVCToolChain *>(other);
606 return (m_installation.name == o->m_installation.name);
609 QByteArray msvcCompilationFile() {
610 static const char* macros[] = {"_ATL_VER", "_CHAR_UNSIGNED", "__CLR_VER",
611 "__cplusplus_cli", "__COUNTER__", "__cplusplus",
612 "_CPPLIB_VER", "_CPPRTTI", "_CPPUNWIND",
613 "_DEBUG", "_DLL", "__FUNCDNAME__",
614 "__FUNCSIG__","__FUNCTION__","_INTEGRAL_MAX_BITS",
615 "_M_ALPHA","_M_CEE","_M_CEE_PURE",
616 "_M_CEE_SAFE","_M_IX86","_M_IA64",
617 "_M_IX86_FP","_M_MPPC","_M_MRX000",
618 "_M_PPC","_M_X64","_MANAGED",
619 "_MFC_VER","_MSC_BUILD", /* "_MSC_EXTENSIONS", */
620 "_MSC_FULL_VER","_MSC_VER","__MSVC_RUNTIME_CHECKS",
621 "_MT", "_NATIVE_WCHAR_T_DEFINED", "_OPENMP",
622 "_VC_NODEFAULTLIB", "_WCHAR_T_DEFINED", "_WIN32",
623 "_WIN32_WCE", "_WIN64", "_Wp64", "__DATE__",
624 "__DATE__", "__TIME__", "__TIMESTAMP__",
626 QByteArray file = "#define __PPOUT__(x) V##x=x\n\n";
628 while (macros[i] != 0) {
629 const QByteArray macro(macros[i]);
630 file += "#if defined(" + macro + ")\n__PPOUT__("
631 + macro + ")\n#endif\n";
634 file += "\nvoid main(){}\n\n";
638 // Run MSVC 'cl' compiler to obtain #defines.
639 static QByteArray msvcPredefinedMacros(const QStringList &env)
641 QByteArray predefinedMacros = "#define __MSVCRT__\n"
643 "#define __int64 long long\n"
644 "#define __int32 long\n"
645 "#define __int16 short\n"
646 "#define __int8 char\n"
652 // QTemporaryFile is buggy and will not unlock the file for cl.exe
653 QTemporaryFile tmpFile(QDir::tempPath()+"/envtestXXXXXX.cpp");
654 tmpFile.setAutoRemove(false);
656 return predefinedMacros;
657 tmpFilePath = QFileInfo(tmpFile).canonicalFilePath();
658 tmpFile.write(msvcCompilationFile());
662 cpp.setEnvironment(env);
663 cpp.setWorkingDirectory(QDir::tempPath());
664 QStringList arguments;
665 const QString binary = QLatin1String("cl.exe");
666 arguments << QLatin1String("/EP") << QDir::toNativeSeparators(tmpFilePath);
667 cpp.start(QLatin1String("cl.exe"), arguments);
668 if (!cpp.waitForStarted()) {
669 qWarning("%s: Cannot start '%s': %s", Q_FUNC_INFO, qPrintable(binary),
670 qPrintable(cpp.errorString()));
671 return predefinedMacros;
673 cpp.closeWriteChannel();
674 if (!cpp.waitForFinished()) {
675 Utils::SynchronousProcess::stopProcess(cpp);
676 qWarning("%s: Timeout running '%s'.", Q_FUNC_INFO, qPrintable(binary));
677 return predefinedMacros;
679 if (cpp.exitStatus() != QProcess::NormalExit) {
680 qWarning("%s: '%s' crashed.", Q_FUNC_INFO, qPrintable(binary));
681 return predefinedMacros;
684 const QList<QByteArray> output = cpp.readAllStandardOutput().split('\n');
685 foreach (const QByteArray& line, output) {
686 if (line.startsWith('V')) {
687 QList<QByteArray> split = line.split('=');
688 const QByteArray key = split.at(0).mid(1);
689 QByteArray value = split.at(1);
690 if (!value.isEmpty()) {
691 value.chop(1); //remove '\n'
693 predefinedMacros += "#define ";
694 predefinedMacros += key;
695 predefinedMacros += ' ';
696 predefinedMacros += value;
697 predefinedMacros += '\n';
700 QFile::remove(tmpFilePath);
701 return predefinedMacros;
704 QByteArray MSVCToolChain::predefinedMacros()
706 if (m_predefinedMacros.isEmpty()) {
707 Utils::Environment env = Utils::Environment::systemEnvironment();
708 addToEnvironment(env);
709 m_predefinedMacros = msvcPredefinedMacros(env.toStringList());
711 return m_predefinedMacros;
714 QList<HeaderPath> MSVCToolChain::systemHeaderPaths()
717 Utils::Environment env = Utils::Environment::systemEnvironment();
718 addToEnvironment(env);
719 QList<HeaderPath> headerPaths;
720 foreach(const QString &path, env.value("INCLUDE").split(QLatin1Char(';'))) {
721 headerPaths.append(HeaderPath(path, HeaderPath::GlobalHeaderPath));
726 MSVCToolChain::StringStringPairList MSVCToolChain::readEnvironmentSetting(const QString &varsBat,
727 const QStringList &args,
728 const Utils::Environment &env)
730 const StringStringPairList rc = readEnvironmentSettingI(varsBat, args, env);
732 qDebug() << "Running: " << varsBat << args;
734 qDebug() << "Incoming: " << env.toStringList();
735 foreach(const StringStringPair &e, rc)
736 qDebug() << e.first << e.second;
738 qDebug() << "Read: " << rc.size() << " variables.";
744 // Windows: Expand the delayed evaluation references returned by the
745 // SDK setup scripts: "PATH=!Path!;foo". Some values might expand
746 // to empty and should not be added
747 static inline QString winExpandDelayedEnvReferences(QString in, const Utils::Environment &env)
749 const QChar exclamationMark = QLatin1Char('!');
750 for (int pos = 0; pos < in.size(); ) {
751 // Replace "!REF!" by its value in process environment
752 pos = in.indexOf(exclamationMark, pos);
755 const int nextPos = in.indexOf(exclamationMark, pos + 1);
758 const QString var = in.mid(pos + 1, nextPos - pos - 1);
759 const QString replacement = env.value(var.toUpper());
760 in.replace(pos, nextPos + 1 - pos, replacement);
761 pos += replacement.size();
766 MSVCToolChain::StringStringPairList MSVCToolChain::readEnvironmentSettingI(const QString &varsBat,
767 const QStringList &args,
768 const Utils::Environment &env)
770 // Run the setup script and extract the variables
771 if (!QFileInfo(varsBat).exists())
772 return StringStringPairList();
773 const QString tempOutputFileName = QDir::tempPath() + QLatin1String("\\qtcreator-msvc-environment.txt");
774 QTemporaryFile tf(QDir::tempPath() + "\\XXXXXX.bat");
775 tf.setAutoRemove(true);
777 return StringStringPairList();
778 const QString filename = tf.fileName();
779 QByteArray call = "call ";
780 call += Utils::QtcProcess::quoteArg(varsBat).toLocal8Bit();
781 if (!args.isEmpty()) {
783 call += Utils::QtcProcess::joinArgs(args).toLocal8Bit();
787 const QByteArray redirect = "set > " + Utils::QtcProcess::quoteArg(
788 QDir::toNativeSeparators(tempOutputFileName)).toLocal8Bit() + "\r\n";
791 tf.waitForBytesWritten(30000);
794 run.setEnvironment(env.toStringList());
795 const QString cmdPath = QString::fromLocal8Bit(qgetenv("COMSPEC"));
796 run.start(cmdPath, QStringList()<< QLatin1String("/c")<<QDir::toNativeSeparators(filename));
797 if (!run.waitForStarted()) {
798 qWarning("%s: Unable to run '%s': %s", Q_FUNC_INFO, qPrintable(varsBat),
799 qPrintable(run.errorString()));
800 return StringStringPairList();
802 if (!run.waitForFinished()) {
803 qWarning("%s: Timeout running '%s'", Q_FUNC_INFO, qPrintable(varsBat));
804 Utils::SynchronousProcess::stopProcess(run);
805 return StringStringPairList();
809 QFile varsFile(tempOutputFileName);
810 if (!varsFile.open(QIODevice::ReadOnly|QIODevice::Text))
811 return StringStringPairList();
813 QRegExp regexp(QLatin1String("(\\w*)=(.*)"));
814 StringStringPairList rc;
815 while (!varsFile.atEnd()) {
816 const QString line = QString::fromLocal8Bit(varsFile.readLine()).trimmed();
817 if (regexp.exactMatch(line)) {
818 const QString varName = regexp.cap(1);
819 const QString expandedValue = winExpandDelayedEnvReferences(regexp.cap(2), env);
820 if (!expandedValue.isEmpty())
821 rc.append(StringStringPair(varName, expandedValue));
829 void MSVCToolChain::addToEnvironment(Utils::Environment &env)
832 qDebug() << "MSVCToolChain::addToEnvironment" << m_installation.name;
833 if (m_installation.name.isEmpty() || m_installation.varsBat.isEmpty()) {
834 qWarning("%s: Attempt to set up invalid MSVC Toolchain.", Q_FUNC_INFO);
837 // We cache the full environment (incoming + modifications by setup script).
838 if (!m_valuesSet || env != m_lastEnvironment) {
839 m_lastEnvironment = env;
840 const QStringList args = m_installation.varsBatArg.isEmpty() ?
841 QStringList() : QStringList(m_installation.varsBatArg);
842 m_values = readEnvironmentSetting(m_installation.varsBat, args, env);
846 const StringStringPairList::const_iterator end = m_values.constEnd();
847 for (StringStringPairList::const_iterator it = m_values.constBegin(); it != end; ++it)
848 env.set((*it).first, (*it).second);
851 QString MSVCToolChain::makeCommand() const
853 if (ProjectExplorerPlugin::instance()->projectExplorerSettings().useJom) {
854 // We want jom! Try to find it.
855 QString jom = QCoreApplication::applicationDirPath() + QLatin1String("/jom.exe");
856 if (QFileInfo(jom).exists())
859 return QLatin1String("jom.exe");
861 return QLatin1String("nmake.exe");
864 IOutputParser *MSVCToolChain::outputParser() const
866 return new MsvcParser;
869 WinCEToolChain *WinCEToolChain::create(const QString &name, const QString &platform)
871 const bool excludeSDK = true;
872 return new WinCEToolChain(findInstallationByName(false, name, excludeSDK), platform);
875 WinCEToolChain::WinCEToolChain(const Installation &in, const QString &platform) :
881 ToolChainType WinCEToolChain::type() const
883 return ToolChain_WINCE;
886 bool WinCEToolChain::equals(const ToolChain *other) const
888 const WinCEToolChain *o = static_cast<const WinCEToolChain *>(other);
889 return (m_platform == o->m_platform && this->MSVCToolChain::equals(other));
892 QByteArray WinCEToolChain::predefinedMacros()
895 return MSVCToolChain::predefinedMacros();
898 QList<HeaderPath> WinCEToolChain::systemHeaderPaths()
901 Utils::Environment env = Utils::Environment::systemEnvironment();
902 addToEnvironment(env);
904 QList<HeaderPath> headerPaths;
906 const QStringList includes = env.value("INCLUDE").split(QLatin1Char(';'));
908 foreach (const QString &path, includes) {
909 const HeaderPath headerPath(path, HeaderPath::GlobalHeaderPath);
910 headerPaths.append(headerPath);
916 void WinCEToolChain::addToEnvironment(Utils::Environment &env)
918 MSVCToolChain::addToEnvironment(env);
919 QSettings registry(MSVC_RegKey, QSettings::NativeFormat);
920 QString path = registry.value(m_installation.name).toString();
924 path += QLatin1Char('/');
926 // Find Platform name
927 CeSdkHandler cesdkhandler;
928 cesdkhandler.parse(path);
929 cesdkhandler.find(m_platform).addToEnvironment(env);