1176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines//===--- ToolChains.cpp - ToolChain Implementations -----------------------===// 2176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// 3176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// The LLVM Compiler Infrastructure 4176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// 5176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// This file is distributed under the University of Illinois Open Source 6176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// License. See LICENSE.TXT for details. 7176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// 8176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines//===----------------------------------------------------------------------===// 9176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 10176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "ToolChains.h" 11b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar#include "Tools.h" 12176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "clang/Basic/CharInfo.h" 13176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "clang/Basic/Version.h" 14176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "clang/Driver/Compilation.h" 15176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "clang/Driver/Driver.h" 16176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "clang/Driver/DriverDiagnostic.h" 17176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "clang/Driver/Options.h" 18176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "llvm/ADT/StringExtras.h" 19176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "llvm/Config/llvm-config.h" 20176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "llvm/Option/Arg.h" 21176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "llvm/Option/ArgList.h" 22176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "llvm/Support/ErrorHandling.h" 23176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "llvm/Support/FileSystem.h" 24176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "llvm/Support/Process.h" 250e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#include <cstdio> 26176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 27176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// Include the necessary headers to interface with the Windows registry and 28176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// environment. 29176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#if defined(LLVM_ON_WIN32) 30176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#define USE_WIN32 31176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#endif 32176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 33176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#ifdef USE_WIN32 34176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines #define WIN32_LEAN_AND_MEAN 35176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines #define NOGDI 360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines #ifndef NOMINMAX 370e2c34f92f00628d48968dfea096d36381f494cbStephen Hines #define NOMINMAX 380e2c34f92f00628d48968dfea096d36381f494cbStephen Hines #endif 39176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines #include <windows.h> 40176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#endif 41176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 42176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesusing namespace clang::driver; 43176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesusing namespace clang::driver::toolchains; 44176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesusing namespace clang; 45176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesusing namespace llvm::opt; 46176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 47176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesMSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple& Triple, 48176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const ArgList &Args) 49176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines : ToolChain(D, Triple, Args) { 500e2c34f92f00628d48968dfea096d36381f494cbStephen Hines getProgramPaths().push_back(getDriver().getInstalledDir()); 510e2c34f92f00628d48968dfea096d36381f494cbStephen Hines if (getDriver().getInstalledDir() != getDriver().Dir) 520e2c34f92f00628d48968dfea096d36381f494cbStephen Hines getProgramPaths().push_back(getDriver().Dir); 53176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 54176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 55176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesTool *MSVCToolChain::buildLinker() const { 56a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return new tools::visualstudio::Linker(*this); 57176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 58176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 59176edba5311f6eff0cad2631449885ddf4fbc9eaStephen HinesTool *MSVCToolChain::buildAssembler() const { 60176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (getTriple().isOSBinFormatMachO()) 61a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return new tools::darwin::Assembler(*this); 62176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines getDriver().Diag(clang::diag::err_no_external_assembler); 63176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return nullptr; 64176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 65176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 66176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool MSVCToolChain::IsIntegratedAssemblerDefault() const { 67176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return true; 68176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 69176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 70176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool MSVCToolChain::IsUnwindTablesDefault() const { 71176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Emit unwind tables by default on Win64. All non-x86_32 Windows platforms 72176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // such as ARM and PPC actually require unwind tables, but LLVM doesn't know 73176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // how to generate them yet. 74176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return getArch() == llvm::Triple::x86_64; 75176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 76176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 77176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool MSVCToolChain::isPICDefault() const { 78176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return getArch() == llvm::Triple::x86_64; 79176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 80176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 81176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool MSVCToolChain::isPIEDefault() const { 82176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 83176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 84176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 85176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool MSVCToolChain::isPICDefaultForced() const { 86176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return getArch() == llvm::Triple::x86_64; 87176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 88176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 89176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#ifdef USE_WIN32 90176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesstatic bool readFullStringValue(HKEY hkey, const char *valueName, 91176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string &value) { 92176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // FIXME: We should be using the W versions of the registry functions, but 93176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // doing so requires UTF8 / UTF16 conversions similar to how we handle command 94176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // line arguments. The UTF8 conversion functions are not exposed publicly 95176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // from LLVM though, so in order to do this we will probably need to create 96176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // a registry abstraction in LLVMSupport that is Windows only. 97176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines DWORD result = 0; 98176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines DWORD valueSize = 0; 99176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines DWORD type = 0; 100176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // First just query for the required size. 101176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines result = RegQueryValueEx(hkey, valueName, NULL, &type, NULL, &valueSize); 102176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (result != ERROR_SUCCESS || type != REG_SZ) 103176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 104176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::vector<BYTE> buffer(valueSize); 105176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines result = RegQueryValueEx(hkey, valueName, NULL, NULL, &buffer[0], &valueSize); 106176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (result == ERROR_SUCCESS) 107176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines value.assign(reinterpret_cast<const char *>(buffer.data())); 108176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return result; 109176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 110176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#endif 111176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 112176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// \brief Read registry string. 113176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// This also supports a means to look for high-versioned keys by use 114176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// of a $VERSION placeholder in the key path. 115176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// $VERSION in the key path is a placeholder for the version number, 116176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// causing the highest value path to be searched for and used. 117176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// I.e. "SOFTWARE\\Microsoft\\VisualStudio\\$VERSION". 118176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// There can be additional characters in the component. Only the numeric 119176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// characters are compared. This function only searches HKLM. 120176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesstatic bool getSystemRegistryString(const char *keyPath, const char *valueName, 121176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string &value, std::string *phValue) { 122176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#ifndef USE_WIN32 123176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 124176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#else 125176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines HKEY hRootKey = HKEY_LOCAL_MACHINE; 126176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines HKEY hKey = NULL; 127176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines long lResult; 128176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines bool returnValue = false; 129176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 130176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *placeHolder = strstr(keyPath, "$VERSION"); 131176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string bestName; 132176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // If we have a $VERSION placeholder, do the highest-version search. 133176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (placeHolder) { 134176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *keyEnd = placeHolder - 1; 135176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *nextKey = placeHolder; 136176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Find end of previous key. 137176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines while ((keyEnd > keyPath) && (*keyEnd != '\\')) 138176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines keyEnd--; 139176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Find end of key containing $VERSION. 140176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines while (*nextKey && (*nextKey != '\\')) 141176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines nextKey++; 142176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines size_t partialKeyLength = keyEnd - keyPath; 143176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines char partialKey[256]; 144176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (partialKeyLength > sizeof(partialKey)) 145176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines partialKeyLength = sizeof(partialKey); 146176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines strncpy(partialKey, keyPath, partialKeyLength); 147176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines partialKey[partialKeyLength] = '\0'; 148176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines HKEY hTopKey = NULL; 149176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY, 150176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines &hTopKey); 151176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (lResult == ERROR_SUCCESS) { 152176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines char keyName[256]; 153176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines double bestValue = 0.0; 154176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines DWORD index, size = sizeof(keyName) - 1; 155176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL, 156176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines NULL, NULL, NULL) == ERROR_SUCCESS; index++) { 157176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *sp = keyName; 158176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines while (*sp && !isDigit(*sp)) 159176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines sp++; 160176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (!*sp) 161176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines continue; 162176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *ep = sp + 1; 163176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines while (*ep && (isDigit(*ep) || (*ep == '.'))) 164176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines ep++; 165176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines char numBuf[32]; 166176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines strncpy(numBuf, sp, sizeof(numBuf) - 1); 167176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines numBuf[sizeof(numBuf) - 1] = '\0'; 168176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines double dvalue = strtod(numBuf, NULL); 169176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (dvalue > bestValue) { 170176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Test that InstallDir is indeed there before keeping this index. 171176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Open the chosen key path remainder. 172176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines bestName = keyName; 173176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Append rest of key. 174176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines bestName.append(nextKey); 175176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines lResult = RegOpenKeyEx(hTopKey, bestName.c_str(), 0, 176176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines KEY_READ | KEY_WOW64_32KEY, &hKey); 177176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (lResult == ERROR_SUCCESS) { 178176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines lResult = readFullStringValue(hKey, valueName, value); 179176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (lResult == ERROR_SUCCESS) { 180176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines bestValue = dvalue; 181176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (phValue) 182176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines *phValue = bestName; 183176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines returnValue = true; 184176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 185176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines RegCloseKey(hKey); 186176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 187176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 188176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines size = sizeof(keyName) - 1; 189176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 190176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines RegCloseKey(hTopKey); 191176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 192176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } else { 193176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines lResult = 194176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines RegOpenKeyEx(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey); 195176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (lResult == ERROR_SUCCESS) { 196176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines lResult = readFullStringValue(hKey, valueName, value); 197176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (lResult == ERROR_SUCCESS) 198176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines returnValue = true; 199176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (phValue) 200176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines phValue->clear(); 201176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines RegCloseKey(hKey); 202176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 203176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 204176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return returnValue; 205176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#endif // USE_WIN32 206176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 207176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 208a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// Convert LLVM's ArchType 209a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// to the corresponding name of Windows SDK libraries subfolder 210a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarstatic StringRef getWindowsSDKArch(llvm::Triple::ArchType Arch) { 211a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar switch (Arch) { 212a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case llvm::Triple::x86: 213a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return "x86"; 214a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case llvm::Triple::x86_64: 215a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return "x64"; 216a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case llvm::Triple::arm: 217a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return "arm"; 218a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar default: 219a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return ""; 220a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 221a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar} 222a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 223a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// Find the most recent version of Universal CRT or Windows 10 SDK. 224a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// vcvarsqueryregistry.bat from Visual Studio 2015 sorts entries in the include 225a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// directory by name and uses the last one of the list. 226a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// So we compare entry names lexicographically to find the greatest one. 227a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarstatic bool getWindows10SDKVersion(const std::string &SDKPath, 228a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string &SDKVersion) { 229a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar SDKVersion.clear(); 230a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 231a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::error_code EC; 232a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::SmallString<128> IncludePath(SDKPath); 233a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::sys::path::append(IncludePath, "Include"); 234a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar for (llvm::sys::fs::directory_iterator DirIt(IncludePath, EC), DirEnd; 235a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DirIt != DirEnd && !EC; DirIt.increment(EC)) { 236a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (!llvm::sys::fs::is_directory(DirIt->path())) 237a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar continue; 238a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar StringRef CandidateName = llvm::sys::path::filename(DirIt->path()); 239a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // If WDK is installed, there could be subfolders like "wdf" in the 240a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // "Include" directory. 241a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // Allow only directories which names start with "10.". 242a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (!CandidateName.startswith("10.")) 243a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar continue; 244a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (CandidateName > SDKVersion) 245a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar SDKVersion = CandidateName; 246a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 247a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 248a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return !SDKVersion.empty(); 249a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar} 250a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 251176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines/// \brief Get Windows SDK installation directory. 252a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarbool MSVCToolChain::getWindowsSDKDir(std::string &Path, int &Major, 253a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string &WindowsSDKIncludeVersion, 254a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string &WindowsSDKLibVersion) const { 255a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string RegistrySDKVersion; 256176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Try the Windows registry. 257a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (!getSystemRegistryString( 258a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION", 259a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "InstallationFolder", Path, &RegistrySDKVersion)) 260a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return false; 261a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (Path.empty() || RegistrySDKVersion.empty()) 262a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return false; 263a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 264a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar WindowsSDKIncludeVersion.clear(); 265a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar WindowsSDKLibVersion.clear(); 266a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar Major = 0; 267a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::sscanf(RegistrySDKVersion.c_str(), "v%d.", &Major); 268a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (Major <= 7) 269a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return true; 270a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (Major == 8) { 271a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // Windows SDK 8.x installs libraries in a folder whose names depend on the 272a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // version of the OS you're targeting. By default choose the newest, which 273a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // usually corresponds to the version of the OS you've installed the SDK on. 274a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const char *Tests[] = {"winv6.3", "win8", "win7"}; 275a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar for (const char *Test : Tests) { 276a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::SmallString<128> TestPath(Path); 277a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::sys::path::append(TestPath, "Lib", Test); 278a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (llvm::sys::fs::exists(TestPath.c_str())) { 279a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar WindowsSDKLibVersion = Test; 280a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar break; 281a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 282a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 283a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return !WindowsSDKLibVersion.empty(); 284a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 285a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (Major == 10) { 286a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (!getWindows10SDKVersion(Path, WindowsSDKIncludeVersion)) 287a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return false; 288a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar WindowsSDKLibVersion = WindowsSDKIncludeVersion; 289a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return true; 290a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 291a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // Unsupported SDK version 292a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return false; 293176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 294176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 295176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// Gets the library path required to link against the Windows SDK. 296176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool MSVCToolChain::getWindowsSDKLibraryPath(std::string &path) const { 297176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string sdkPath; 298176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines int sdkMajor = 0; 299a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string windowsSDKIncludeVersion; 300a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string windowsSDKLibVersion; 301176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 302176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path.clear(); 303a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (!getWindowsSDKDir(sdkPath, sdkMajor, windowsSDKIncludeVersion, 304a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar windowsSDKLibVersion)) 305176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 306176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 307176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::SmallString<128> libPath(sdkPath); 308176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::sys::path::append(libPath, "Lib"); 309176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (sdkMajor <= 7) { 310176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines switch (getArch()) { 311176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // In Windows SDK 7.x, x86 libraries are directly in the Lib folder. 312176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines case llvm::Triple::x86: 313176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines break; 314176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines case llvm::Triple::x86_64: 315176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::sys::path::append(libPath, "x64"); 316176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines break; 317176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines case llvm::Triple::arm: 318176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // It is not necessary to link against Windows SDK 7.x when targeting ARM. 319176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 320176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines default: 321176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 322176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 323176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } else { 324a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const StringRef archName = getWindowsSDKArch(getArch()); 325a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (archName.empty()) 326176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 327a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::sys::path::append(libPath, windowsSDKLibVersion, "um", archName); 328176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 329176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 330176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path = libPath.str(); 331176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return true; 332176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 333176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 334a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// Check if the Include path of a specified version of Visual Studio contains 335a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// specific header files. If not, they are probably shipped with Universal CRT. 336a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarbool clang::driver::toolchains::MSVCToolChain::useUniversalCRT( 337a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string &VisualStudioDir) const { 338a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::SmallString<128> TestPath(VisualStudioDir); 339a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::sys::path::append(TestPath, "VC\\include\\stdlib.h"); 340a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 341a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return !llvm::sys::fs::exists(TestPath); 342a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar} 343a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 344a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarbool MSVCToolChain::getUniversalCRTSdkDir(std::string &Path, 345a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string &UCRTVersion) const { 346a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // vcvarsqueryregistry.bat for Visual Studio 2015 queries the registry 347a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // for the specific key "KitsRoot10". So do we. 348a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (!getSystemRegistryString( 349a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots", "KitsRoot10", 350a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar Path, nullptr)) 351a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return false; 352a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 353a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return getWindows10SDKVersion(Path, UCRTVersion); 354a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar} 355a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 356a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarbool MSVCToolChain::getUniversalCRTLibraryPath(std::string &Path) const { 357a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string UniversalCRTSdkPath; 358a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string UCRTVersion; 359a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 360a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar Path.clear(); 361a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (!getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion)) 362a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return false; 363a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 364a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar StringRef ArchName = getWindowsSDKArch(getArch()); 365a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (ArchName.empty()) 366a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return false; 367a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 368a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::SmallString<128> LibPath(UniversalCRTSdkPath); 369a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::sys::path::append(LibPath, "Lib", UCRTVersion, "ucrt", ArchName); 370a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 371a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar Path = LibPath.str(); 372a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return true; 373a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar} 374a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 375176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// Get the location to use for Visual Studio binaries. The location priority 376176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// is: %VCINSTALLDIR% > %PATH% > newest copy of Visual Studio installed on 377176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// system (as reported by the registry). 378176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool MSVCToolChain::getVisualStudioBinariesFolder(const char *clangProgramPath, 379176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string &path) const { 380176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path.clear(); 381176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 382176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines SmallString<128> BinDir; 383176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 384176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // First check the environment variables that vsvars32.bat sets. 385176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::Optional<std::string> VcInstallDir = 386176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::sys::Process::GetEnv("VCINSTALLDIR"); 387176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (VcInstallDir.hasValue()) { 388176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines BinDir = VcInstallDir.getValue(); 389176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::sys::path::append(BinDir, "bin"); 390176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } else { 391176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Next walk the PATH, trying to find a cl.exe in the path. If we find one, 392176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // use that. However, make sure it's not clang's cl.exe. 393176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("PATH"); 394176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (OptPath.hasValue()) { 395176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'}; 396176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines SmallVector<StringRef, 8> PathSegments; 397176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::SplitString(OptPath.getValue(), PathSegments, EnvPathSeparatorStr); 398176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 399176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines for (StringRef PathSegment : PathSegments) { 400176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (PathSegment.empty()) 401176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines continue; 402176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 403176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines SmallString<128> FilePath(PathSegment); 404176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::sys::path::append(FilePath, "cl.exe"); 405176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (llvm::sys::fs::can_execute(FilePath.c_str()) && 406176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines !llvm::sys::fs::equivalent(FilePath.c_str(), clangProgramPath)) { 407176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // If we found it on the PATH, use it exactly as is with no 408176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // modifications. 409176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path = PathSegment; 410176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return true; 411176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 412176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 413176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 414176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 415176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string installDir; 416176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // With no VCINSTALLDIR and nothing on the PATH, if we can't find it in the 417176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // registry then we have no choice but to fail. 418176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (!getVisualStudioInstallDir(installDir)) 419176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 420176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 421176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Regardless of what binary we're ultimately trying to find, we make sure 422176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // that this is a Visual Studio directory by checking for cl.exe. We use 423176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // cl.exe instead of other binaries like link.exe because programs such as 424176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // GnuWin32 also have a utility called link.exe, so cl.exe is the least 425176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // ambiguous. 426176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines BinDir = installDir; 427176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::sys::path::append(BinDir, "VC", "bin"); 428176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines SmallString<128> ClPath(BinDir); 429176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::sys::path::append(ClPath, "cl.exe"); 430176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 431176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (!llvm::sys::fs::can_execute(ClPath.c_str())) 432176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 433176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 434176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 435176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (BinDir.empty()) 436176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 437176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 438176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines switch (getArch()) { 439176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines case llvm::Triple::x86: 440176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines break; 441176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines case llvm::Triple::x86_64: 442176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::sys::path::append(BinDir, "amd64"); 443176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines break; 444176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines case llvm::Triple::arm: 445176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::sys::path::append(BinDir, "arm"); 446176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines break; 447176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines default: 448176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Whatever this is, Visual Studio doesn't have a toolchain for it. 449176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 450176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 451176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path = BinDir.str(); 452176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return true; 453176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 454176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 455176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines// Get Visual Studio installation directory. 456176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesbool MSVCToolChain::getVisualStudioInstallDir(std::string &path) const { 457176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // First check the environment variables that vsvars32.bat sets. 458176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *vcinstalldir = getenv("VCINSTALLDIR"); 459176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (vcinstalldir) { 460176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path = vcinstalldir; 461176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path = path.substr(0, path.find("\\VC")); 462176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return true; 463176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 464176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 465176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string vsIDEInstallDir; 466176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string vsExpressIDEInstallDir; 467176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Then try the windows registry. 468176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines bool hasVCDir = 469176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines getSystemRegistryString("SOFTWARE\\Microsoft\\VisualStudio\\$VERSION", 470176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines "InstallDir", vsIDEInstallDir, nullptr); 471176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (hasVCDir && !vsIDEInstallDir.empty()) { 472176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path = vsIDEInstallDir.substr(0, vsIDEInstallDir.find("\\Common7\\IDE")); 473176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return true; 474176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 475176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 476176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines bool hasVCExpressDir = 477176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines getSystemRegistryString("SOFTWARE\\Microsoft\\VCExpress\\$VERSION", 478176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines "InstallDir", vsExpressIDEInstallDir, nullptr); 479176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (hasVCExpressDir && !vsExpressIDEInstallDir.empty()) { 480176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path = vsExpressIDEInstallDir.substr( 481176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 0, vsIDEInstallDir.find("\\Common7\\IDE")); 482176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return true; 483176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 484176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 485176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Try the environment. 486176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *vs120comntools = getenv("VS120COMNTOOLS"); 487176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *vs100comntools = getenv("VS100COMNTOOLS"); 488176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *vs90comntools = getenv("VS90COMNTOOLS"); 489176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *vs80comntools = getenv("VS80COMNTOOLS"); 490176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 491176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *vscomntools = nullptr; 492176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 493176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Find any version we can 494176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (vs120comntools) 495176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines vscomntools = vs120comntools; 496176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines else if (vs100comntools) 497176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines vscomntools = vs100comntools; 498176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines else if (vs90comntools) 499176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines vscomntools = vs90comntools; 500176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines else if (vs80comntools) 501176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines vscomntools = vs80comntools; 502176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 503176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (vscomntools && *vscomntools) { 504176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const char *p = strstr(vscomntools, "\\Common7\\Tools"); 505176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines path = p ? std::string(vscomntools, p) : vscomntools; 506176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return true; 507176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 508176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return false; 509176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 510176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 511a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarvoid MSVCToolChain::AddSystemIncludeWithSubfolder( 512a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const ArgList &DriverArgs, ArgStringList &CC1Args, 513a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const std::string &folder, const Twine &subfolder1, const Twine &subfolder2, 514a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const Twine &subfolder3) const { 515176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::SmallString<128> path(folder); 516a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar llvm::sys::path::append(path, subfolder1, subfolder2, subfolder3); 5173ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar addSystemInclude(DriverArgs, CC1Args, path); 518176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 519176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 520176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesvoid MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 521176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines ArgStringList &CC1Args) const { 522176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (DriverArgs.hasArg(options::OPT_nostdinc)) 523176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return; 524176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 525176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 526a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, getDriver().ResourceDir, 527a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "include"); 528176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 529176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 530176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 531176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return; 532176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 533176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat. 534176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (const char *cl_include_dir = getenv("INCLUDE")) { 535176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines SmallVector<StringRef, 8> Dirs; 536176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines StringRef(cl_include_dir) 537176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 538176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines for (StringRef Dir : Dirs) 539176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines addSystemInclude(DriverArgs, CC1Args, Dir); 540176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (!Dirs.empty()) 541176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return; 542176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 543176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 544176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string VSDir; 545176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 546176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // When built with access to the proper Windows APIs, try to actually find 547176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // the correct include paths first. 548176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (getVisualStudioInstallDir(VSDir)) { 549176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, VSDir, "VC\\include"); 550176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 551a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (useUniversalCRT(VSDir)) { 552a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string UniversalCRTSdkPath; 553a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string UCRTVersion; 554a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion)) { 555a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, UniversalCRTSdkPath, 556a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "Include", UCRTVersion, "ucrt"); 557a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 558a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 559a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 560176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::string WindowsSDKDir; 561a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar int major; 562a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string windowsSDKIncludeVersion; 563a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar std::string windowsSDKLibVersion; 564a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (getWindowsSDKDir(WindowsSDKDir, major, windowsSDKIncludeVersion, 565a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar windowsSDKLibVersion)) { 566176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (major >= 8) { 567a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // Note: windowsSDKIncludeVersion is empty for SDKs prior to v10. 568a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // Anyway, llvm::sys::path::append is able to manage it. 569176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, 570a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "include", windowsSDKIncludeVersion, 571a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "shared"); 572176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, 573a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "include", windowsSDKIncludeVersion, 574a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "um"); 575176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, 576a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "include", windowsSDKIncludeVersion, 577a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar "winrt"); 578176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } else { 579176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, 580176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines "include"); 581176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 582176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } else { 583176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines addSystemInclude(DriverArgs, CC1Args, VSDir); 584176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 585176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return; 586176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines } 587176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 588176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // As a fallback, select default install paths. 589176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // FIXME: Don't guess drives and paths like this on Windows. 590176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines const StringRef Paths[] = { 591176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines "C:/Program Files/Microsoft Visual Studio 10.0/VC/include", 592176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines "C:/Program Files/Microsoft Visual Studio 9.0/VC/include", 593176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", 594176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines "C:/Program Files/Microsoft Visual Studio 8/VC/include", 595176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include" 596176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines }; 597176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines addSystemIncludes(DriverArgs, CC1Args, Paths); 598176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 599176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines 600176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hinesvoid MSVCToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 601176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines ArgStringList &CC1Args) const { 602176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines // FIXME: There should probably be logic here to find libc++ on Windows. 603176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines} 604b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar 605b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainarstd::string 606b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga NainarMSVCToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 607b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar types::ID InputType) const { 608b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar std::string TripleStr = 609b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar ToolChain::ComputeEffectiveClangTriple(Args, InputType); 610b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar llvm::Triple Triple(TripleStr); 611b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar VersionTuple MSVT = 612b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar tools::visualstudio::getMSVCVersion(/*D=*/nullptr, Triple, Args, 613b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar /*IsWindowsMSVC=*/true); 614b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar if (MSVT.empty()) 615b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar return TripleStr; 616b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar 617b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar MSVT = VersionTuple(MSVT.getMajor(), MSVT.getMinor().getValueOr(0), 618b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar MSVT.getSubminor().getValueOr(0)); 619b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar 620b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar if (Triple.getEnvironment() == llvm::Triple::MSVC) { 621b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar StringRef ObjFmt = Triple.getEnvironmentName().split('-').second; 622b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar if (ObjFmt.empty()) 623b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar Triple.setEnvironmentName((Twine("msvc") + MSVT.getAsString()).str()); 624b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar else 625b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar Triple.setEnvironmentName( 626b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar (Twine("msvc") + MSVT.getAsString() + Twine('-') + ObjFmt).str()); 627b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar } 628b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar return Triple.getTriple(); 629b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar} 630a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 631a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga NainarSanitizerMask MSVCToolChain::getSupportedSanitizers() const { 632a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar SanitizerMask Res = ToolChain::getSupportedSanitizers(); 633a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar Res |= SanitizerKind::Address; 634a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return Res; 635a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar} 636a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 637a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarllvm::opt::DerivedArgList * 638a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga NainarMSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, 639a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const char *BoundArch) const { 640a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); 641a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const OptTable &Opts = getDriver().getOpts(); 642a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 643a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // /Oy and /Oy- only has an effect under X86-32. 644a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar bool SupportsForcingFramePointer = getArch() == llvm::Triple::x86; 645a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 646a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // The -O[12xd] flag actually expands to several flags. We must desugar the 647a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // flags so that options embedded can be negated. For example, the '-O2' flag 648a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // enables '-Oy'. Expanding '-O2' into its constituent flags allows us to 649a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // correctly handle '-O2 -Oy-' where the trailing '-Oy-' disables a single 650a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // aspect of '-O2'. 651a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // 652a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // Note that this expansion logic only applies to the *last* of '[12xd]'. 653a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 654a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // First step is to search for the character we'd like to expand. 655a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const char *ExpandChar = nullptr; 656a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar for (Arg *A : Args) { 657a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (!A->getOption().matches(options::OPT__SLASH_O)) 658a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar continue; 659a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar StringRef OptStr = A->getValue(); 660a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar for (size_t I = 0, E = OptStr.size(); I != E; ++I) { 661a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const char &OptChar = *(OptStr.data() + I); 662a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (OptChar == '1' || OptChar == '2' || OptChar == 'x' || OptChar == 'd') 663a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar ExpandChar = OptStr.data() + I; 664a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 665a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 666a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 667a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // The -O flag actually takes an amalgam of other options. For example, 668a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar // '/Ogyb2' is equivalent to '/Og' '/Oy' '/Ob2'. 669a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar for (Arg *A : Args) { 670a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (!A->getOption().matches(options::OPT__SLASH_O)) { 671a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->append(A); 672a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar continue; 673a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 674a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar 675a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar StringRef OptStr = A->getValue(); 676a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar for (size_t I = 0, E = OptStr.size(); I != E; ++I) { 677a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar const char &OptChar = *(OptStr.data() + I); 678a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar switch (OptChar) { 679a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar default: 680a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar break; 681a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case '1': 682a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case '2': 683a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case 'x': 684a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case 'd': 685a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (&OptChar == ExpandChar) { 686a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (OptChar == 'd') { 687a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddFlagArg(A, Opts.getOption(options::OPT_O0)); 688a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } else { 689a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (OptChar == '1') { 690a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddJoinedArg(A, Opts.getOption(options::OPT_O), "s"); 691a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } else if (OptChar == '2' || OptChar == 'x') { 692a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin)); 693a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddJoinedArg(A, Opts.getOption(options::OPT_O), "2"); 694a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 695a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (SupportsForcingFramePointer) 696a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddFlagArg(A, 697a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar Opts.getOption(options::OPT_fomit_frame_pointer)); 698a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (OptChar == '1' || OptChar == '2') 699a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddFlagArg(A, 700a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar Opts.getOption(options::OPT_ffunction_sections)); 701a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 702a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 703a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar break; 704a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case 'b': 705a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (I + 1 != E && isdigit(OptStr[I + 1])) 706a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar ++I; 707a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar break; 708a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case 'g': 709a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar break; 710a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case 'i': 711a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (I + 1 != E && OptStr[I + 1] == '-') { 712a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar ++I; 713a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddFlagArg(A, Opts.getOption(options::OPT_fno_builtin)); 714a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } else { 715a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin)); 716a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 717a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar break; 718a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case 's': 719a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddJoinedArg(A, Opts.getOption(options::OPT_O), "s"); 720a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar break; 721a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case 't': 722a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddJoinedArg(A, Opts.getOption(options::OPT_O), "2"); 723a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar break; 724a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar case 'y': { 725a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar bool OmitFramePointer = true; 726a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (I + 1 != E && OptStr[I + 1] == '-') { 727a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar OmitFramePointer = false; 728a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar ++I; 729a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 730a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (SupportsForcingFramePointer) { 731a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar if (OmitFramePointer) 732a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddFlagArg(A, 733a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar Opts.getOption(options::OPT_fomit_frame_pointer)); 734a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar else 735a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar DAL->AddFlagArg( 736a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar A, Opts.getOption(options::OPT_fno_omit_frame_pointer)); 737a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 738a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar break; 739a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 740a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 741a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 742a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar } 743a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar return DAL; 744a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar} 745