1*0a6a1f1dSLionel Sambuc //===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc
10*0a6a1f1dSLionel Sambuc #include "ToolChains.h"
11*0a6a1f1dSLionel Sambuc #include "clang/Basic/CharInfo.h"
12*0a6a1f1dSLionel Sambuc #include "clang/Basic/Version.h"
13*0a6a1f1dSLionel Sambuc #include "clang/Driver/Compilation.h"
14*0a6a1f1dSLionel Sambuc #include "clang/Driver/Driver.h"
15*0a6a1f1dSLionel Sambuc #include "clang/Driver/DriverDiagnostic.h"
16*0a6a1f1dSLionel Sambuc #include "clang/Driver/Options.h"
17*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringExtras.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/Config/llvm-config.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/Option/Arg.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/Option/ArgList.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Support/FileSystem.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/Support/Process.h"
24*0a6a1f1dSLionel Sambuc
25*0a6a1f1dSLionel Sambuc // Include the necessary headers to interface with the Windows registry and
26*0a6a1f1dSLionel Sambuc // environment.
27*0a6a1f1dSLionel Sambuc #if defined(LLVM_ON_WIN32)
28*0a6a1f1dSLionel Sambuc #define USE_WIN32
29*0a6a1f1dSLionel Sambuc #endif
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel Sambuc #ifdef USE_WIN32
32*0a6a1f1dSLionel Sambuc #define WIN32_LEAN_AND_MEAN
33*0a6a1f1dSLionel Sambuc #define NOGDI
34*0a6a1f1dSLionel Sambuc #ifndef NOMINMAX
35*0a6a1f1dSLionel Sambuc #define NOMINMAX
36*0a6a1f1dSLionel Sambuc #endif
37*0a6a1f1dSLionel Sambuc #include <windows.h>
38*0a6a1f1dSLionel Sambuc #endif
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc using namespace clang::driver;
41*0a6a1f1dSLionel Sambuc using namespace clang::driver::toolchains;
42*0a6a1f1dSLionel Sambuc using namespace clang;
43*0a6a1f1dSLionel Sambuc using namespace llvm::opt;
44*0a6a1f1dSLionel Sambuc
MSVCToolChain(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)45*0a6a1f1dSLionel Sambuc MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple& Triple,
46*0a6a1f1dSLionel Sambuc const ArgList &Args)
47*0a6a1f1dSLionel Sambuc : ToolChain(D, Triple, Args) {
48*0a6a1f1dSLionel Sambuc getProgramPaths().push_back(getDriver().getInstalledDir());
49*0a6a1f1dSLionel Sambuc if (getDriver().getInstalledDir() != getDriver().Dir)
50*0a6a1f1dSLionel Sambuc getProgramPaths().push_back(getDriver().Dir);
51*0a6a1f1dSLionel Sambuc }
52*0a6a1f1dSLionel Sambuc
buildLinker() const53*0a6a1f1dSLionel Sambuc Tool *MSVCToolChain::buildLinker() const {
54*0a6a1f1dSLionel Sambuc return new tools::visualstudio::Link(*this);
55*0a6a1f1dSLionel Sambuc }
56*0a6a1f1dSLionel Sambuc
buildAssembler() const57*0a6a1f1dSLionel Sambuc Tool *MSVCToolChain::buildAssembler() const {
58*0a6a1f1dSLionel Sambuc if (getTriple().isOSBinFormatMachO())
59*0a6a1f1dSLionel Sambuc return new tools::darwin::Assemble(*this);
60*0a6a1f1dSLionel Sambuc getDriver().Diag(clang::diag::err_no_external_assembler);
61*0a6a1f1dSLionel Sambuc return nullptr;
62*0a6a1f1dSLionel Sambuc }
63*0a6a1f1dSLionel Sambuc
IsIntegratedAssemblerDefault() const64*0a6a1f1dSLionel Sambuc bool MSVCToolChain::IsIntegratedAssemblerDefault() const {
65*0a6a1f1dSLionel Sambuc return true;
66*0a6a1f1dSLionel Sambuc }
67*0a6a1f1dSLionel Sambuc
IsUnwindTablesDefault() const68*0a6a1f1dSLionel Sambuc bool MSVCToolChain::IsUnwindTablesDefault() const {
69*0a6a1f1dSLionel Sambuc // Emit unwind tables by default on Win64. All non-x86_32 Windows platforms
70*0a6a1f1dSLionel Sambuc // such as ARM and PPC actually require unwind tables, but LLVM doesn't know
71*0a6a1f1dSLionel Sambuc // how to generate them yet.
72*0a6a1f1dSLionel Sambuc return getArch() == llvm::Triple::x86_64;
73*0a6a1f1dSLionel Sambuc }
74*0a6a1f1dSLionel Sambuc
isPICDefault() const75*0a6a1f1dSLionel Sambuc bool MSVCToolChain::isPICDefault() const {
76*0a6a1f1dSLionel Sambuc return getArch() == llvm::Triple::x86_64;
77*0a6a1f1dSLionel Sambuc }
78*0a6a1f1dSLionel Sambuc
isPIEDefault() const79*0a6a1f1dSLionel Sambuc bool MSVCToolChain::isPIEDefault() const {
80*0a6a1f1dSLionel Sambuc return false;
81*0a6a1f1dSLionel Sambuc }
82*0a6a1f1dSLionel Sambuc
isPICDefaultForced() const83*0a6a1f1dSLionel Sambuc bool MSVCToolChain::isPICDefaultForced() const {
84*0a6a1f1dSLionel Sambuc return getArch() == llvm::Triple::x86_64;
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc
87*0a6a1f1dSLionel Sambuc #ifdef USE_WIN32
readFullStringValue(HKEY hkey,const char * valueName,std::string & value)88*0a6a1f1dSLionel Sambuc static bool readFullStringValue(HKEY hkey, const char *valueName,
89*0a6a1f1dSLionel Sambuc std::string &value) {
90*0a6a1f1dSLionel Sambuc // FIXME: We should be using the W versions of the registry functions, but
91*0a6a1f1dSLionel Sambuc // doing so requires UTF8 / UTF16 conversions similar to how we handle command
92*0a6a1f1dSLionel Sambuc // line arguments. The UTF8 conversion functions are not exposed publicly
93*0a6a1f1dSLionel Sambuc // from LLVM though, so in order to do this we will probably need to create
94*0a6a1f1dSLionel Sambuc // a registry abstraction in LLVMSupport that is Windows only.
95*0a6a1f1dSLionel Sambuc DWORD result = 0;
96*0a6a1f1dSLionel Sambuc DWORD valueSize = 0;
97*0a6a1f1dSLionel Sambuc DWORD type = 0;
98*0a6a1f1dSLionel Sambuc // First just query for the required size.
99*0a6a1f1dSLionel Sambuc result = RegQueryValueEx(hkey, valueName, NULL, &type, NULL, &valueSize);
100*0a6a1f1dSLionel Sambuc if (result != ERROR_SUCCESS || type != REG_SZ)
101*0a6a1f1dSLionel Sambuc return false;
102*0a6a1f1dSLionel Sambuc std::vector<BYTE> buffer(valueSize);
103*0a6a1f1dSLionel Sambuc result = RegQueryValueEx(hkey, valueName, NULL, NULL, &buffer[0], &valueSize);
104*0a6a1f1dSLionel Sambuc if (result == ERROR_SUCCESS)
105*0a6a1f1dSLionel Sambuc value.assign(reinterpret_cast<const char *>(buffer.data()));
106*0a6a1f1dSLionel Sambuc return result;
107*0a6a1f1dSLionel Sambuc }
108*0a6a1f1dSLionel Sambuc #endif
109*0a6a1f1dSLionel Sambuc
110*0a6a1f1dSLionel Sambuc /// \brief Read registry string.
111*0a6a1f1dSLionel Sambuc /// This also supports a means to look for high-versioned keys by use
112*0a6a1f1dSLionel Sambuc /// of a $VERSION placeholder in the key path.
113*0a6a1f1dSLionel Sambuc /// $VERSION in the key path is a placeholder for the version number,
114*0a6a1f1dSLionel Sambuc /// causing the highest value path to be searched for and used.
115*0a6a1f1dSLionel Sambuc /// I.e. "SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
116*0a6a1f1dSLionel Sambuc /// There can be additional characters in the component. Only the numeric
117*0a6a1f1dSLionel Sambuc /// characters are compared. This function only searches HKLM.
getSystemRegistryString(const char * keyPath,const char * valueName,std::string & value,std::string * phValue)118*0a6a1f1dSLionel Sambuc static bool getSystemRegistryString(const char *keyPath, const char *valueName,
119*0a6a1f1dSLionel Sambuc std::string &value, std::string *phValue) {
120*0a6a1f1dSLionel Sambuc #ifndef USE_WIN32
121*0a6a1f1dSLionel Sambuc return false;
122*0a6a1f1dSLionel Sambuc #else
123*0a6a1f1dSLionel Sambuc HKEY hRootKey = HKEY_LOCAL_MACHINE;
124*0a6a1f1dSLionel Sambuc HKEY hKey = NULL;
125*0a6a1f1dSLionel Sambuc long lResult;
126*0a6a1f1dSLionel Sambuc bool returnValue = false;
127*0a6a1f1dSLionel Sambuc
128*0a6a1f1dSLionel Sambuc const char *placeHolder = strstr(keyPath, "$VERSION");
129*0a6a1f1dSLionel Sambuc std::string bestName;
130*0a6a1f1dSLionel Sambuc // If we have a $VERSION placeholder, do the highest-version search.
131*0a6a1f1dSLionel Sambuc if (placeHolder) {
132*0a6a1f1dSLionel Sambuc const char *keyEnd = placeHolder - 1;
133*0a6a1f1dSLionel Sambuc const char *nextKey = placeHolder;
134*0a6a1f1dSLionel Sambuc // Find end of previous key.
135*0a6a1f1dSLionel Sambuc while ((keyEnd > keyPath) && (*keyEnd != '\\'))
136*0a6a1f1dSLionel Sambuc keyEnd--;
137*0a6a1f1dSLionel Sambuc // Find end of key containing $VERSION.
138*0a6a1f1dSLionel Sambuc while (*nextKey && (*nextKey != '\\'))
139*0a6a1f1dSLionel Sambuc nextKey++;
140*0a6a1f1dSLionel Sambuc size_t partialKeyLength = keyEnd - keyPath;
141*0a6a1f1dSLionel Sambuc char partialKey[256];
142*0a6a1f1dSLionel Sambuc if (partialKeyLength > sizeof(partialKey))
143*0a6a1f1dSLionel Sambuc partialKeyLength = sizeof(partialKey);
144*0a6a1f1dSLionel Sambuc strncpy(partialKey, keyPath, partialKeyLength);
145*0a6a1f1dSLionel Sambuc partialKey[partialKeyLength] = '\0';
146*0a6a1f1dSLionel Sambuc HKEY hTopKey = NULL;
147*0a6a1f1dSLionel Sambuc lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
148*0a6a1f1dSLionel Sambuc &hTopKey);
149*0a6a1f1dSLionel Sambuc if (lResult == ERROR_SUCCESS) {
150*0a6a1f1dSLionel Sambuc char keyName[256];
151*0a6a1f1dSLionel Sambuc double bestValue = 0.0;
152*0a6a1f1dSLionel Sambuc DWORD index, size = sizeof(keyName) - 1;
153*0a6a1f1dSLionel Sambuc for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
154*0a6a1f1dSLionel Sambuc NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
155*0a6a1f1dSLionel Sambuc const char *sp = keyName;
156*0a6a1f1dSLionel Sambuc while (*sp && !isDigit(*sp))
157*0a6a1f1dSLionel Sambuc sp++;
158*0a6a1f1dSLionel Sambuc if (!*sp)
159*0a6a1f1dSLionel Sambuc continue;
160*0a6a1f1dSLionel Sambuc const char *ep = sp + 1;
161*0a6a1f1dSLionel Sambuc while (*ep && (isDigit(*ep) || (*ep == '.')))
162*0a6a1f1dSLionel Sambuc ep++;
163*0a6a1f1dSLionel Sambuc char numBuf[32];
164*0a6a1f1dSLionel Sambuc strncpy(numBuf, sp, sizeof(numBuf) - 1);
165*0a6a1f1dSLionel Sambuc numBuf[sizeof(numBuf) - 1] = '\0';
166*0a6a1f1dSLionel Sambuc double dvalue = strtod(numBuf, NULL);
167*0a6a1f1dSLionel Sambuc if (dvalue > bestValue) {
168*0a6a1f1dSLionel Sambuc // Test that InstallDir is indeed there before keeping this index.
169*0a6a1f1dSLionel Sambuc // Open the chosen key path remainder.
170*0a6a1f1dSLionel Sambuc bestName = keyName;
171*0a6a1f1dSLionel Sambuc // Append rest of key.
172*0a6a1f1dSLionel Sambuc bestName.append(nextKey);
173*0a6a1f1dSLionel Sambuc lResult = RegOpenKeyEx(hTopKey, bestName.c_str(), 0,
174*0a6a1f1dSLionel Sambuc KEY_READ | KEY_WOW64_32KEY, &hKey);
175*0a6a1f1dSLionel Sambuc if (lResult == ERROR_SUCCESS) {
176*0a6a1f1dSLionel Sambuc lResult = readFullStringValue(hKey, valueName, value);
177*0a6a1f1dSLionel Sambuc if (lResult == ERROR_SUCCESS) {
178*0a6a1f1dSLionel Sambuc bestValue = dvalue;
179*0a6a1f1dSLionel Sambuc if (phValue)
180*0a6a1f1dSLionel Sambuc *phValue = bestName;
181*0a6a1f1dSLionel Sambuc returnValue = true;
182*0a6a1f1dSLionel Sambuc }
183*0a6a1f1dSLionel Sambuc RegCloseKey(hKey);
184*0a6a1f1dSLionel Sambuc }
185*0a6a1f1dSLionel Sambuc }
186*0a6a1f1dSLionel Sambuc size = sizeof(keyName) - 1;
187*0a6a1f1dSLionel Sambuc }
188*0a6a1f1dSLionel Sambuc RegCloseKey(hTopKey);
189*0a6a1f1dSLionel Sambuc }
190*0a6a1f1dSLionel Sambuc } else {
191*0a6a1f1dSLionel Sambuc lResult =
192*0a6a1f1dSLionel Sambuc RegOpenKeyEx(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
193*0a6a1f1dSLionel Sambuc if (lResult == ERROR_SUCCESS) {
194*0a6a1f1dSLionel Sambuc lResult = readFullStringValue(hKey, valueName, value);
195*0a6a1f1dSLionel Sambuc if (lResult == ERROR_SUCCESS)
196*0a6a1f1dSLionel Sambuc returnValue = true;
197*0a6a1f1dSLionel Sambuc if (phValue)
198*0a6a1f1dSLionel Sambuc phValue->clear();
199*0a6a1f1dSLionel Sambuc RegCloseKey(hKey);
200*0a6a1f1dSLionel Sambuc }
201*0a6a1f1dSLionel Sambuc }
202*0a6a1f1dSLionel Sambuc return returnValue;
203*0a6a1f1dSLionel Sambuc #endif // USE_WIN32
204*0a6a1f1dSLionel Sambuc }
205*0a6a1f1dSLionel Sambuc
206*0a6a1f1dSLionel Sambuc /// \brief Get Windows SDK installation directory.
getWindowsSDKDir(std::string & path,int & major,int & minor) const207*0a6a1f1dSLionel Sambuc bool MSVCToolChain::getWindowsSDKDir(std::string &path, int &major,
208*0a6a1f1dSLionel Sambuc int &minor) const {
209*0a6a1f1dSLionel Sambuc std::string sdkVersion;
210*0a6a1f1dSLionel Sambuc // Try the Windows registry.
211*0a6a1f1dSLionel Sambuc bool hasSDKDir = getSystemRegistryString(
212*0a6a1f1dSLionel Sambuc "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
213*0a6a1f1dSLionel Sambuc "InstallationFolder", path, &sdkVersion);
214*0a6a1f1dSLionel Sambuc if (!sdkVersion.empty())
215*0a6a1f1dSLionel Sambuc ::sscanf(sdkVersion.c_str(), "v%d.%d", &major, &minor);
216*0a6a1f1dSLionel Sambuc return hasSDKDir && !path.empty();
217*0a6a1f1dSLionel Sambuc }
218*0a6a1f1dSLionel Sambuc
219*0a6a1f1dSLionel Sambuc // Gets the library path required to link against the Windows SDK.
getWindowsSDKLibraryPath(std::string & path) const220*0a6a1f1dSLionel Sambuc bool MSVCToolChain::getWindowsSDKLibraryPath(std::string &path) const {
221*0a6a1f1dSLionel Sambuc std::string sdkPath;
222*0a6a1f1dSLionel Sambuc int sdkMajor = 0;
223*0a6a1f1dSLionel Sambuc int sdkMinor = 0;
224*0a6a1f1dSLionel Sambuc
225*0a6a1f1dSLionel Sambuc path.clear();
226*0a6a1f1dSLionel Sambuc if (!getWindowsSDKDir(sdkPath, sdkMajor, sdkMinor))
227*0a6a1f1dSLionel Sambuc return false;
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc llvm::SmallString<128> libPath(sdkPath);
230*0a6a1f1dSLionel Sambuc llvm::sys::path::append(libPath, "Lib");
231*0a6a1f1dSLionel Sambuc if (sdkMajor <= 7) {
232*0a6a1f1dSLionel Sambuc switch (getArch()) {
233*0a6a1f1dSLionel Sambuc // In Windows SDK 7.x, x86 libraries are directly in the Lib folder.
234*0a6a1f1dSLionel Sambuc case llvm::Triple::x86:
235*0a6a1f1dSLionel Sambuc break;
236*0a6a1f1dSLionel Sambuc case llvm::Triple::x86_64:
237*0a6a1f1dSLionel Sambuc llvm::sys::path::append(libPath, "x64");
238*0a6a1f1dSLionel Sambuc break;
239*0a6a1f1dSLionel Sambuc case llvm::Triple::arm:
240*0a6a1f1dSLionel Sambuc // It is not necessary to link against Windows SDK 7.x when targeting ARM.
241*0a6a1f1dSLionel Sambuc return false;
242*0a6a1f1dSLionel Sambuc default:
243*0a6a1f1dSLionel Sambuc return false;
244*0a6a1f1dSLionel Sambuc }
245*0a6a1f1dSLionel Sambuc } else {
246*0a6a1f1dSLionel Sambuc // Windows SDK 8.x installs libraries in a folder whose names depend on the
247*0a6a1f1dSLionel Sambuc // version of the OS you're targeting. By default choose the newest, which
248*0a6a1f1dSLionel Sambuc // usually corresponds to the version of the OS you've installed the SDK on.
249*0a6a1f1dSLionel Sambuc const char *tests[] = {"winv6.3", "win8", "win7"};
250*0a6a1f1dSLionel Sambuc bool found = false;
251*0a6a1f1dSLionel Sambuc for (const char *test : tests) {
252*0a6a1f1dSLionel Sambuc llvm::SmallString<128> testPath(libPath);
253*0a6a1f1dSLionel Sambuc llvm::sys::path::append(testPath, test);
254*0a6a1f1dSLionel Sambuc if (llvm::sys::fs::exists(testPath.c_str())) {
255*0a6a1f1dSLionel Sambuc libPath = testPath;
256*0a6a1f1dSLionel Sambuc found = true;
257*0a6a1f1dSLionel Sambuc break;
258*0a6a1f1dSLionel Sambuc }
259*0a6a1f1dSLionel Sambuc }
260*0a6a1f1dSLionel Sambuc
261*0a6a1f1dSLionel Sambuc if (!found)
262*0a6a1f1dSLionel Sambuc return false;
263*0a6a1f1dSLionel Sambuc
264*0a6a1f1dSLionel Sambuc llvm::sys::path::append(libPath, "um");
265*0a6a1f1dSLionel Sambuc switch (getArch()) {
266*0a6a1f1dSLionel Sambuc case llvm::Triple::x86:
267*0a6a1f1dSLionel Sambuc llvm::sys::path::append(libPath, "x86");
268*0a6a1f1dSLionel Sambuc break;
269*0a6a1f1dSLionel Sambuc case llvm::Triple::x86_64:
270*0a6a1f1dSLionel Sambuc llvm::sys::path::append(libPath, "x64");
271*0a6a1f1dSLionel Sambuc break;
272*0a6a1f1dSLionel Sambuc case llvm::Triple::arm:
273*0a6a1f1dSLionel Sambuc llvm::sys::path::append(libPath, "arm");
274*0a6a1f1dSLionel Sambuc break;
275*0a6a1f1dSLionel Sambuc default:
276*0a6a1f1dSLionel Sambuc return false;
277*0a6a1f1dSLionel Sambuc }
278*0a6a1f1dSLionel Sambuc }
279*0a6a1f1dSLionel Sambuc
280*0a6a1f1dSLionel Sambuc path = libPath.str();
281*0a6a1f1dSLionel Sambuc return true;
282*0a6a1f1dSLionel Sambuc }
283*0a6a1f1dSLionel Sambuc
284*0a6a1f1dSLionel Sambuc // Get the location to use for Visual Studio binaries. The location priority
285*0a6a1f1dSLionel Sambuc // is: %VCINSTALLDIR% > %PATH% > newest copy of Visual Studio installed on
286*0a6a1f1dSLionel Sambuc // system (as reported by the registry).
getVisualStudioBinariesFolder(const char * clangProgramPath,std::string & path) const287*0a6a1f1dSLionel Sambuc bool MSVCToolChain::getVisualStudioBinariesFolder(const char *clangProgramPath,
288*0a6a1f1dSLionel Sambuc std::string &path) const {
289*0a6a1f1dSLionel Sambuc path.clear();
290*0a6a1f1dSLionel Sambuc
291*0a6a1f1dSLionel Sambuc SmallString<128> BinDir;
292*0a6a1f1dSLionel Sambuc
293*0a6a1f1dSLionel Sambuc // First check the environment variables that vsvars32.bat sets.
294*0a6a1f1dSLionel Sambuc llvm::Optional<std::string> VcInstallDir =
295*0a6a1f1dSLionel Sambuc llvm::sys::Process::GetEnv("VCINSTALLDIR");
296*0a6a1f1dSLionel Sambuc if (VcInstallDir.hasValue()) {
297*0a6a1f1dSLionel Sambuc BinDir = VcInstallDir.getValue();
298*0a6a1f1dSLionel Sambuc llvm::sys::path::append(BinDir, "bin");
299*0a6a1f1dSLionel Sambuc } else {
300*0a6a1f1dSLionel Sambuc // Next walk the PATH, trying to find a cl.exe in the path. If we find one,
301*0a6a1f1dSLionel Sambuc // use that. However, make sure it's not clang's cl.exe.
302*0a6a1f1dSLionel Sambuc llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("PATH");
303*0a6a1f1dSLionel Sambuc if (OptPath.hasValue()) {
304*0a6a1f1dSLionel Sambuc const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
305*0a6a1f1dSLionel Sambuc SmallVector<StringRef, 8> PathSegments;
306*0a6a1f1dSLionel Sambuc llvm::SplitString(OptPath.getValue(), PathSegments, EnvPathSeparatorStr);
307*0a6a1f1dSLionel Sambuc
308*0a6a1f1dSLionel Sambuc for (StringRef PathSegment : PathSegments) {
309*0a6a1f1dSLionel Sambuc if (PathSegment.empty())
310*0a6a1f1dSLionel Sambuc continue;
311*0a6a1f1dSLionel Sambuc
312*0a6a1f1dSLionel Sambuc SmallString<128> FilePath(PathSegment);
313*0a6a1f1dSLionel Sambuc llvm::sys::path::append(FilePath, "cl.exe");
314*0a6a1f1dSLionel Sambuc if (llvm::sys::fs::can_execute(FilePath.c_str()) &&
315*0a6a1f1dSLionel Sambuc !llvm::sys::fs::equivalent(FilePath.c_str(), clangProgramPath)) {
316*0a6a1f1dSLionel Sambuc // If we found it on the PATH, use it exactly as is with no
317*0a6a1f1dSLionel Sambuc // modifications.
318*0a6a1f1dSLionel Sambuc path = PathSegment;
319*0a6a1f1dSLionel Sambuc return true;
320*0a6a1f1dSLionel Sambuc }
321*0a6a1f1dSLionel Sambuc }
322*0a6a1f1dSLionel Sambuc }
323*0a6a1f1dSLionel Sambuc
324*0a6a1f1dSLionel Sambuc std::string installDir;
325*0a6a1f1dSLionel Sambuc // With no VCINSTALLDIR and nothing on the PATH, if we can't find it in the
326*0a6a1f1dSLionel Sambuc // registry then we have no choice but to fail.
327*0a6a1f1dSLionel Sambuc if (!getVisualStudioInstallDir(installDir))
328*0a6a1f1dSLionel Sambuc return false;
329*0a6a1f1dSLionel Sambuc
330*0a6a1f1dSLionel Sambuc // Regardless of what binary we're ultimately trying to find, we make sure
331*0a6a1f1dSLionel Sambuc // that this is a Visual Studio directory by checking for cl.exe. We use
332*0a6a1f1dSLionel Sambuc // cl.exe instead of other binaries like link.exe because programs such as
333*0a6a1f1dSLionel Sambuc // GnuWin32 also have a utility called link.exe, so cl.exe is the least
334*0a6a1f1dSLionel Sambuc // ambiguous.
335*0a6a1f1dSLionel Sambuc BinDir = installDir;
336*0a6a1f1dSLionel Sambuc llvm::sys::path::append(BinDir, "VC", "bin");
337*0a6a1f1dSLionel Sambuc SmallString<128> ClPath(BinDir);
338*0a6a1f1dSLionel Sambuc llvm::sys::path::append(ClPath, "cl.exe");
339*0a6a1f1dSLionel Sambuc
340*0a6a1f1dSLionel Sambuc if (!llvm::sys::fs::can_execute(ClPath.c_str()))
341*0a6a1f1dSLionel Sambuc return false;
342*0a6a1f1dSLionel Sambuc }
343*0a6a1f1dSLionel Sambuc
344*0a6a1f1dSLionel Sambuc if (BinDir.empty())
345*0a6a1f1dSLionel Sambuc return false;
346*0a6a1f1dSLionel Sambuc
347*0a6a1f1dSLionel Sambuc switch (getArch()) {
348*0a6a1f1dSLionel Sambuc case llvm::Triple::x86:
349*0a6a1f1dSLionel Sambuc break;
350*0a6a1f1dSLionel Sambuc case llvm::Triple::x86_64:
351*0a6a1f1dSLionel Sambuc llvm::sys::path::append(BinDir, "amd64");
352*0a6a1f1dSLionel Sambuc break;
353*0a6a1f1dSLionel Sambuc case llvm::Triple::arm:
354*0a6a1f1dSLionel Sambuc llvm::sys::path::append(BinDir, "arm");
355*0a6a1f1dSLionel Sambuc break;
356*0a6a1f1dSLionel Sambuc default:
357*0a6a1f1dSLionel Sambuc // Whatever this is, Visual Studio doesn't have a toolchain for it.
358*0a6a1f1dSLionel Sambuc return false;
359*0a6a1f1dSLionel Sambuc }
360*0a6a1f1dSLionel Sambuc path = BinDir.str();
361*0a6a1f1dSLionel Sambuc return true;
362*0a6a1f1dSLionel Sambuc }
363*0a6a1f1dSLionel Sambuc
364*0a6a1f1dSLionel Sambuc // Get Visual Studio installation directory.
getVisualStudioInstallDir(std::string & path) const365*0a6a1f1dSLionel Sambuc bool MSVCToolChain::getVisualStudioInstallDir(std::string &path) const {
366*0a6a1f1dSLionel Sambuc // First check the environment variables that vsvars32.bat sets.
367*0a6a1f1dSLionel Sambuc const char *vcinstalldir = getenv("VCINSTALLDIR");
368*0a6a1f1dSLionel Sambuc if (vcinstalldir) {
369*0a6a1f1dSLionel Sambuc path = vcinstalldir;
370*0a6a1f1dSLionel Sambuc path = path.substr(0, path.find("\\VC"));
371*0a6a1f1dSLionel Sambuc return true;
372*0a6a1f1dSLionel Sambuc }
373*0a6a1f1dSLionel Sambuc
374*0a6a1f1dSLionel Sambuc std::string vsIDEInstallDir;
375*0a6a1f1dSLionel Sambuc std::string vsExpressIDEInstallDir;
376*0a6a1f1dSLionel Sambuc // Then try the windows registry.
377*0a6a1f1dSLionel Sambuc bool hasVCDir =
378*0a6a1f1dSLionel Sambuc getSystemRegistryString("SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
379*0a6a1f1dSLionel Sambuc "InstallDir", vsIDEInstallDir, nullptr);
380*0a6a1f1dSLionel Sambuc if (hasVCDir && !vsIDEInstallDir.empty()) {
381*0a6a1f1dSLionel Sambuc path = vsIDEInstallDir.substr(0, vsIDEInstallDir.find("\\Common7\\IDE"));
382*0a6a1f1dSLionel Sambuc return true;
383*0a6a1f1dSLionel Sambuc }
384*0a6a1f1dSLionel Sambuc
385*0a6a1f1dSLionel Sambuc bool hasVCExpressDir =
386*0a6a1f1dSLionel Sambuc getSystemRegistryString("SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
387*0a6a1f1dSLionel Sambuc "InstallDir", vsExpressIDEInstallDir, nullptr);
388*0a6a1f1dSLionel Sambuc if (hasVCExpressDir && !vsExpressIDEInstallDir.empty()) {
389*0a6a1f1dSLionel Sambuc path = vsExpressIDEInstallDir.substr(
390*0a6a1f1dSLionel Sambuc 0, vsIDEInstallDir.find("\\Common7\\IDE"));
391*0a6a1f1dSLionel Sambuc return true;
392*0a6a1f1dSLionel Sambuc }
393*0a6a1f1dSLionel Sambuc
394*0a6a1f1dSLionel Sambuc // Try the environment.
395*0a6a1f1dSLionel Sambuc const char *vs120comntools = getenv("VS120COMNTOOLS");
396*0a6a1f1dSLionel Sambuc const char *vs100comntools = getenv("VS100COMNTOOLS");
397*0a6a1f1dSLionel Sambuc const char *vs90comntools = getenv("VS90COMNTOOLS");
398*0a6a1f1dSLionel Sambuc const char *vs80comntools = getenv("VS80COMNTOOLS");
399*0a6a1f1dSLionel Sambuc
400*0a6a1f1dSLionel Sambuc const char *vscomntools = nullptr;
401*0a6a1f1dSLionel Sambuc
402*0a6a1f1dSLionel Sambuc // Find any version we can
403*0a6a1f1dSLionel Sambuc if (vs120comntools)
404*0a6a1f1dSLionel Sambuc vscomntools = vs120comntools;
405*0a6a1f1dSLionel Sambuc else if (vs100comntools)
406*0a6a1f1dSLionel Sambuc vscomntools = vs100comntools;
407*0a6a1f1dSLionel Sambuc else if (vs90comntools)
408*0a6a1f1dSLionel Sambuc vscomntools = vs90comntools;
409*0a6a1f1dSLionel Sambuc else if (vs80comntools)
410*0a6a1f1dSLionel Sambuc vscomntools = vs80comntools;
411*0a6a1f1dSLionel Sambuc
412*0a6a1f1dSLionel Sambuc if (vscomntools && *vscomntools) {
413*0a6a1f1dSLionel Sambuc const char *p = strstr(vscomntools, "\\Common7\\Tools");
414*0a6a1f1dSLionel Sambuc path = p ? std::string(vscomntools, p) : vscomntools;
415*0a6a1f1dSLionel Sambuc return true;
416*0a6a1f1dSLionel Sambuc }
417*0a6a1f1dSLionel Sambuc return false;
418*0a6a1f1dSLionel Sambuc }
419*0a6a1f1dSLionel Sambuc
AddSystemIncludeWithSubfolder(const ArgList & DriverArgs,ArgStringList & CC1Args,const std::string & folder,const char * subfolder) const420*0a6a1f1dSLionel Sambuc void MSVCToolChain::AddSystemIncludeWithSubfolder(const ArgList &DriverArgs,
421*0a6a1f1dSLionel Sambuc ArgStringList &CC1Args,
422*0a6a1f1dSLionel Sambuc const std::string &folder,
423*0a6a1f1dSLionel Sambuc const char *subfolder) const {
424*0a6a1f1dSLionel Sambuc llvm::SmallString<128> path(folder);
425*0a6a1f1dSLionel Sambuc llvm::sys::path::append(path, subfolder);
426*0a6a1f1dSLionel Sambuc addSystemInclude(DriverArgs, CC1Args, path.str());
427*0a6a1f1dSLionel Sambuc }
428*0a6a1f1dSLionel Sambuc
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const429*0a6a1f1dSLionel Sambuc void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
430*0a6a1f1dSLionel Sambuc ArgStringList &CC1Args) const {
431*0a6a1f1dSLionel Sambuc if (DriverArgs.hasArg(options::OPT_nostdinc))
432*0a6a1f1dSLionel Sambuc return;
433*0a6a1f1dSLionel Sambuc
434*0a6a1f1dSLionel Sambuc if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
435*0a6a1f1dSLionel Sambuc SmallString<128> P(getDriver().ResourceDir);
436*0a6a1f1dSLionel Sambuc llvm::sys::path::append(P, "include");
437*0a6a1f1dSLionel Sambuc addSystemInclude(DriverArgs, CC1Args, P.str());
438*0a6a1f1dSLionel Sambuc }
439*0a6a1f1dSLionel Sambuc
440*0a6a1f1dSLionel Sambuc if (DriverArgs.hasArg(options::OPT_nostdlibinc))
441*0a6a1f1dSLionel Sambuc return;
442*0a6a1f1dSLionel Sambuc
443*0a6a1f1dSLionel Sambuc // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
444*0a6a1f1dSLionel Sambuc if (const char *cl_include_dir = getenv("INCLUDE")) {
445*0a6a1f1dSLionel Sambuc SmallVector<StringRef, 8> Dirs;
446*0a6a1f1dSLionel Sambuc StringRef(cl_include_dir)
447*0a6a1f1dSLionel Sambuc .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
448*0a6a1f1dSLionel Sambuc for (StringRef Dir : Dirs)
449*0a6a1f1dSLionel Sambuc addSystemInclude(DriverArgs, CC1Args, Dir);
450*0a6a1f1dSLionel Sambuc if (!Dirs.empty())
451*0a6a1f1dSLionel Sambuc return;
452*0a6a1f1dSLionel Sambuc }
453*0a6a1f1dSLionel Sambuc
454*0a6a1f1dSLionel Sambuc std::string VSDir;
455*0a6a1f1dSLionel Sambuc
456*0a6a1f1dSLionel Sambuc // When built with access to the proper Windows APIs, try to actually find
457*0a6a1f1dSLionel Sambuc // the correct include paths first.
458*0a6a1f1dSLionel Sambuc if (getVisualStudioInstallDir(VSDir)) {
459*0a6a1f1dSLionel Sambuc AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, VSDir, "VC\\include");
460*0a6a1f1dSLionel Sambuc
461*0a6a1f1dSLionel Sambuc std::string WindowsSDKDir;
462*0a6a1f1dSLionel Sambuc int major, minor;
463*0a6a1f1dSLionel Sambuc if (getWindowsSDKDir(WindowsSDKDir, major, minor)) {
464*0a6a1f1dSLionel Sambuc if (major >= 8) {
465*0a6a1f1dSLionel Sambuc AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
466*0a6a1f1dSLionel Sambuc "include\\shared");
467*0a6a1f1dSLionel Sambuc AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
468*0a6a1f1dSLionel Sambuc "include\\um");
469*0a6a1f1dSLionel Sambuc AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
470*0a6a1f1dSLionel Sambuc "include\\winrt");
471*0a6a1f1dSLionel Sambuc } else {
472*0a6a1f1dSLionel Sambuc AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
473*0a6a1f1dSLionel Sambuc "include");
474*0a6a1f1dSLionel Sambuc }
475*0a6a1f1dSLionel Sambuc } else {
476*0a6a1f1dSLionel Sambuc addSystemInclude(DriverArgs, CC1Args, VSDir);
477*0a6a1f1dSLionel Sambuc }
478*0a6a1f1dSLionel Sambuc return;
479*0a6a1f1dSLionel Sambuc }
480*0a6a1f1dSLionel Sambuc
481*0a6a1f1dSLionel Sambuc // As a fallback, select default install paths.
482*0a6a1f1dSLionel Sambuc // FIXME: Don't guess drives and paths like this on Windows.
483*0a6a1f1dSLionel Sambuc const StringRef Paths[] = {
484*0a6a1f1dSLionel Sambuc "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
485*0a6a1f1dSLionel Sambuc "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
486*0a6a1f1dSLionel Sambuc "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
487*0a6a1f1dSLionel Sambuc "C:/Program Files/Microsoft Visual Studio 8/VC/include",
488*0a6a1f1dSLionel Sambuc "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
489*0a6a1f1dSLionel Sambuc };
490*0a6a1f1dSLionel Sambuc addSystemIncludes(DriverArgs, CC1Args, Paths);
491*0a6a1f1dSLionel Sambuc }
492*0a6a1f1dSLionel Sambuc
AddClangCXXStdlibIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const493*0a6a1f1dSLionel Sambuc void MSVCToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
494*0a6a1f1dSLionel Sambuc ArgStringList &CC1Args) const {
495*0a6a1f1dSLionel Sambuc // FIXME: There should probably be logic here to find libc++ on Windows.
496*0a6a1f1dSLionel Sambuc }
497