blob: f20f58a119f47cb3bc87a76ce5efcf17b13b5288 [file] [log] [blame]
Chandler Carruth1fc603e2011-12-17 23:10:01 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
David Majnemere11d3732015-06-08 00:22:46 +000011#include "Tools.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000012#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "clang/Basic/Version.h"
Chandler Carruth1fc603e2011-12-17 23:10:01 +000014#include "clang/Driver/Compilation.h"
15#include "clang/Driver/Driver.h"
Rafael Espindola79764462013-03-24 15:06:53 +000016#include "clang/Driver/DriverDiagnostic.h"
Chandler Carruth1fc603e2011-12-17 23:10:01 +000017#include "clang/Driver/Options.h"
Zachary Turner0eaf8fc2014-10-22 20:40:28 +000018#include "llvm/ADT/StringExtras.h"
Alp Tokerf1ffc842014-06-22 04:31:15 +000019#include "llvm/Config/llvm-config.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000020#include "llvm/Option/Arg.h"
21#include "llvm/Option/ArgList.h"
Chandler Carruth1fc603e2011-12-17 23:10:01 +000022#include "llvm/Support/ErrorHandling.h"
Zachary Turner0eaf8fc2014-10-22 20:40:28 +000023#include "llvm/Support/FileSystem.h"
24#include "llvm/Support/Process.h"
Reid Kleckner6b7156b2015-01-23 19:16:25 +000025#include <cstdio>
26
Chandler Carruth1fc603e2011-12-17 23:10:01 +000027// Include the necessary headers to interface with the Windows registry and
28// environment.
Alp Tokerf1ffc842014-06-22 04:31:15 +000029#if defined(LLVM_ON_WIN32)
Alp Tokerfcce1832014-06-22 03:27:45 +000030#define USE_WIN32
31#endif
32
33#ifdef USE_WIN32
Chandler Carruth1fc603e2011-12-17 23:10:01 +000034 #define WIN32_LEAN_AND_MEAN
35 #define NOGDI
Yaron Keren7fc6f1e2014-12-04 21:46:50 +000036 #ifndef NOMINMAX
37 #define NOMINMAX
38 #endif
Logan Chien733e3c62014-06-24 16:18:10 +000039 #include <windows.h>
Chandler Carruth1fc603e2011-12-17 23:10:01 +000040#endif
41
42using namespace clang::driver;
43using namespace clang::driver::toolchains;
44using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000045using namespace llvm::opt;
Chandler Carruth1fc603e2011-12-17 23:10:01 +000046
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000047MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple& Triple,
48 const ArgList &Args)
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000049 : ToolChain(D, Triple, Args) {
Zachary Turner719f58c2014-12-01 23:06:47 +000050 getProgramPaths().push_back(getDriver().getInstalledDir());
51 if (getDriver().getInstalledDir() != getDriver().Dir)
52 getProgramPaths().push_back(getDriver().Dir);
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000053}
54
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000055Tool *MSVCToolChain::buildLinker() const {
Douglas Katzman95354292015-06-23 20:42:09 +000056 return new tools::visualstudio::Linker(*this);
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000057}
58
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000059Tool *MSVCToolChain::buildAssembler() const {
Saleem Abdulrasool377066a2014-03-27 22:50:18 +000060 if (getTriple().isOSBinFormatMachO())
Douglas Katzman95354292015-06-23 20:42:09 +000061 return new tools::darwin::Assembler(*this);
Alp Tokerc8d4f0f2013-11-22 08:27:46 +000062 getDriver().Diag(clang::diag::err_no_external_assembler);
Craig Topper92fc2df2014-05-17 16:56:41 +000063 return nullptr;
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000064}
65
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000066bool MSVCToolChain::IsIntegratedAssemblerDefault() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000067 return true;
68}
69
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000070bool MSVCToolChain::IsUnwindTablesDefault() const {
Reid Kleckner6b3a9402014-09-04 18:13:12 +000071 // Emit unwind tables by default on Win64. All non-x86_32 Windows platforms
72 // such as ARM and PPC actually require unwind tables, but LLVM doesn't know
73 // how to generate them yet.
74 return getArch() == llvm::Triple::x86_64;
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000075}
76
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000077bool MSVCToolChain::isPICDefault() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000078 return getArch() == llvm::Triple::x86_64;
79}
80
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000081bool MSVCToolChain::isPIEDefault() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000082 return false;
83}
84
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000085bool MSVCToolChain::isPICDefaultForced() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000086 return getArch() == llvm::Triple::x86_64;
87}
88
Zachary Turner0eaf8fc2014-10-22 20:40:28 +000089#ifdef USE_WIN32
90static bool readFullStringValue(HKEY hkey, const char *valueName,
91 std::string &value) {
92 // FIXME: We should be using the W versions of the registry functions, but
93 // doing so requires UTF8 / UTF16 conversions similar to how we handle command
94 // line arguments. The UTF8 conversion functions are not exposed publicly
95 // from LLVM though, so in order to do this we will probably need to create
96 // a registry abstraction in LLVMSupport that is Windows only.
97 DWORD result = 0;
98 DWORD valueSize = 0;
99 DWORD type = 0;
100 // First just query for the required size.
101 result = RegQueryValueEx(hkey, valueName, NULL, &type, NULL, &valueSize);
102 if (result != ERROR_SUCCESS || type != REG_SZ)
103 return false;
104 std::vector<BYTE> buffer(valueSize);
105 result = RegQueryValueEx(hkey, valueName, NULL, NULL, &buffer[0], &valueSize);
106 if (result == ERROR_SUCCESS)
107 value.assign(reinterpret_cast<const char *>(buffer.data()));
108 return result;
109}
110#endif
111
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000112/// \brief Read registry string.
113/// This also supports a means to look for high-versioned keys by use
114/// of a $VERSION placeholder in the key path.
115/// $VERSION in the key path is a placeholder for the version number,
116/// causing the highest value path to be searched for and used.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000117/// I.e. "SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
118/// There can be additional characters in the component. Only the numeric
119/// characters are compared. This function only searches HKLM.
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000120static bool getSystemRegistryString(const char *keyPath, const char *valueName,
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000121 std::string &value, std::string *phValue) {
Alp Tokerfcce1832014-06-22 03:27:45 +0000122#ifndef USE_WIN32
123 return false;
124#else
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000125 HKEY hRootKey = HKEY_LOCAL_MACHINE;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000126 HKEY hKey = NULL;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000127 long lResult;
128 bool returnValue = false;
129
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000130 const char *placeHolder = strstr(keyPath, "$VERSION");
131 std::string bestName;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000132 // If we have a $VERSION placeholder, do the highest-version search.
133 if (placeHolder) {
134 const char *keyEnd = placeHolder - 1;
135 const char *nextKey = placeHolder;
136 // Find end of previous key.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000137 while ((keyEnd > keyPath) && (*keyEnd != '\\'))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000138 keyEnd--;
139 // Find end of key containing $VERSION.
140 while (*nextKey && (*nextKey != '\\'))
141 nextKey++;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000142 size_t partialKeyLength = keyEnd - keyPath;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000143 char partialKey[256];
144 if (partialKeyLength > sizeof(partialKey))
145 partialKeyLength = sizeof(partialKey);
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000146 strncpy(partialKey, keyPath, partialKeyLength);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000147 partialKey[partialKeyLength] = '\0';
148 HKEY hTopKey = NULL;
Hans Wennborg935d01d2013-10-09 23:41:48 +0000149 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
150 &hTopKey);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000151 if (lResult == ERROR_SUCCESS) {
152 char keyName[256];
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000153 double bestValue = 0.0;
154 DWORD index, size = sizeof(keyName) - 1;
155 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
156 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
157 const char *sp = keyName;
Jordan Rosea7d03842013-02-08 22:30:41 +0000158 while (*sp && !isDigit(*sp))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000159 sp++;
160 if (!*sp)
161 continue;
162 const char *ep = sp + 1;
Jordan Rosea7d03842013-02-08 22:30:41 +0000163 while (*ep && (isDigit(*ep) || (*ep == '.')))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000164 ep++;
165 char numBuf[32];
166 strncpy(numBuf, sp, sizeof(numBuf) - 1);
167 numBuf[sizeof(numBuf) - 1] = '\0';
Hans Wennborgd2192312013-10-10 18:03:08 +0000168 double dvalue = strtod(numBuf, NULL);
169 if (dvalue > bestValue) {
170 // Test that InstallDir is indeed there before keeping this index.
171 // Open the chosen key path remainder.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000172 bestName = keyName;
Hans Wennborgd2192312013-10-10 18:03:08 +0000173 // Append rest of key.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000174 bestName.append(nextKey);
175 lResult = RegOpenKeyEx(hTopKey, bestName.c_str(), 0,
Hans Wennborgd2192312013-10-10 18:03:08 +0000176 KEY_READ | KEY_WOW64_32KEY, &hKey);
177 if (lResult == ERROR_SUCCESS) {
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000178 lResult = readFullStringValue(hKey, valueName, value);
Hans Wennborgd2192312013-10-10 18:03:08 +0000179 if (lResult == ERROR_SUCCESS) {
Hans Wennborgd2192312013-10-10 18:03:08 +0000180 bestValue = dvalue;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000181 if (phValue)
182 *phValue = bestName;
Hans Wennborgd2192312013-10-10 18:03:08 +0000183 returnValue = true;
184 }
185 RegCloseKey(hKey);
186 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000187 }
188 size = sizeof(keyName) - 1;
189 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000190 RegCloseKey(hTopKey);
191 }
192 } else {
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000193 lResult =
194 RegOpenKeyEx(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000195 if (lResult == ERROR_SUCCESS) {
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000196 lResult = readFullStringValue(hKey, valueName, value);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000197 if (lResult == ERROR_SUCCESS)
198 returnValue = true;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000199 if (phValue)
200 phValue->clear();
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000201 RegCloseKey(hKey);
202 }
203 }
204 return returnValue;
Alp Tokerfcce1832014-06-22 03:27:45 +0000205#endif // USE_WIN32
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000206}
207
208/// \brief Get Windows SDK installation directory.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000209bool MSVCToolChain::getWindowsSDKDir(std::string &path, int &major,
210 int &minor) const {
211 std::string sdkVersion;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000212 // Try the Windows registry.
213 bool hasSDKDir = getSystemRegistryString(
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000214 "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
215 "InstallationFolder", path, &sdkVersion);
216 if (!sdkVersion.empty())
Reid Kleckner6b7156b2015-01-23 19:16:25 +0000217 std::sscanf(sdkVersion.c_str(), "v%d.%d", &major, &minor);
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000218 return hasSDKDir && !path.empty();
219}
220
Zachary Turner10d75b22014-10-22 20:40:43 +0000221// Gets the library path required to link against the Windows SDK.
222bool MSVCToolChain::getWindowsSDKLibraryPath(std::string &path) const {
223 std::string sdkPath;
224 int sdkMajor = 0;
225 int sdkMinor = 0;
226
227 path.clear();
228 if (!getWindowsSDKDir(sdkPath, sdkMajor, sdkMinor))
229 return false;
230
231 llvm::SmallString<128> libPath(sdkPath);
232 llvm::sys::path::append(libPath, "Lib");
233 if (sdkMajor <= 7) {
234 switch (getArch()) {
235 // In Windows SDK 7.x, x86 libraries are directly in the Lib folder.
236 case llvm::Triple::x86:
237 break;
238 case llvm::Triple::x86_64:
239 llvm::sys::path::append(libPath, "x64");
240 break;
241 case llvm::Triple::arm:
242 // It is not necessary to link against Windows SDK 7.x when targeting ARM.
243 return false;
244 default:
245 return false;
246 }
247 } else {
248 // Windows SDK 8.x installs libraries in a folder whose names depend on the
249 // version of the OS you're targeting. By default choose the newest, which
250 // usually corresponds to the version of the OS you've installed the SDK on.
Zachary Turner34eb9432014-10-22 21:48:56 +0000251 const char *tests[] = {"winv6.3", "win8", "win7"};
Zachary Turner10d75b22014-10-22 20:40:43 +0000252 bool found = false;
Zachary Turner34eb9432014-10-22 21:48:56 +0000253 for (const char *test : tests) {
Zachary Turner10d75b22014-10-22 20:40:43 +0000254 llvm::SmallString<128> testPath(libPath);
255 llvm::sys::path::append(testPath, test);
256 if (llvm::sys::fs::exists(testPath.c_str())) {
257 libPath = testPath;
258 found = true;
259 break;
260 }
261 }
262
263 if (!found)
264 return false;
265
266 llvm::sys::path::append(libPath, "um");
267 switch (getArch()) {
268 case llvm::Triple::x86:
269 llvm::sys::path::append(libPath, "x86");
270 break;
271 case llvm::Triple::x86_64:
272 llvm::sys::path::append(libPath, "x64");
273 break;
274 case llvm::Triple::arm:
275 llvm::sys::path::append(libPath, "arm");
276 break;
277 default:
278 return false;
279 }
280 }
281
282 path = libPath.str();
283 return true;
284}
285
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000286// Get the location to use for Visual Studio binaries. The location priority
287// is: %VCINSTALLDIR% > %PATH% > newest copy of Visual Studio installed on
288// system (as reported by the registry).
289bool MSVCToolChain::getVisualStudioBinariesFolder(const char *clangProgramPath,
290 std::string &path) const {
291 path.clear();
292
293 SmallString<128> BinDir;
294
295 // First check the environment variables that vsvars32.bat sets.
296 llvm::Optional<std::string> VcInstallDir =
297 llvm::sys::Process::GetEnv("VCINSTALLDIR");
298 if (VcInstallDir.hasValue()) {
299 BinDir = VcInstallDir.getValue();
300 llvm::sys::path::append(BinDir, "bin");
301 } else {
302 // Next walk the PATH, trying to find a cl.exe in the path. If we find one,
303 // use that. However, make sure it's not clang's cl.exe.
304 llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("PATH");
305 if (OptPath.hasValue()) {
306 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
307 SmallVector<StringRef, 8> PathSegments;
308 llvm::SplitString(OptPath.getValue(), PathSegments, EnvPathSeparatorStr);
309
310 for (StringRef PathSegment : PathSegments) {
311 if (PathSegment.empty())
312 continue;
313
314 SmallString<128> FilePath(PathSegment);
315 llvm::sys::path::append(FilePath, "cl.exe");
316 if (llvm::sys::fs::can_execute(FilePath.c_str()) &&
317 !llvm::sys::fs::equivalent(FilePath.c_str(), clangProgramPath)) {
318 // If we found it on the PATH, use it exactly as is with no
319 // modifications.
320 path = PathSegment;
321 return true;
322 }
323 }
324 }
325
326 std::string installDir;
327 // With no VCINSTALLDIR and nothing on the PATH, if we can't find it in the
328 // registry then we have no choice but to fail.
329 if (!getVisualStudioInstallDir(installDir))
330 return false;
331
332 // Regardless of what binary we're ultimately trying to find, we make sure
333 // that this is a Visual Studio directory by checking for cl.exe. We use
334 // cl.exe instead of other binaries like link.exe because programs such as
335 // GnuWin32 also have a utility called link.exe, so cl.exe is the least
336 // ambiguous.
337 BinDir = installDir;
338 llvm::sys::path::append(BinDir, "VC", "bin");
339 SmallString<128> ClPath(BinDir);
340 llvm::sys::path::append(ClPath, "cl.exe");
341
342 if (!llvm::sys::fs::can_execute(ClPath.c_str()))
343 return false;
Hans Wennborge6b994e2014-10-20 23:26:03 +0000344 }
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000345
346 if (BinDir.empty())
347 return false;
348
349 switch (getArch()) {
350 case llvm::Triple::x86:
351 break;
352 case llvm::Triple::x86_64:
353 llvm::sys::path::append(BinDir, "amd64");
354 break;
355 case llvm::Triple::arm:
356 llvm::sys::path::append(BinDir, "arm");
357 break;
358 default:
359 // Whatever this is, Visual Studio doesn't have a toolchain for it.
360 return false;
361 }
362 path = BinDir.str();
363 return true;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000364}
365
Alp Tokerfcce1832014-06-22 03:27:45 +0000366// Get Visual Studio installation directory.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000367bool MSVCToolChain::getVisualStudioInstallDir(std::string &path) const {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000368 // First check the environment variables that vsvars32.bat sets.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000369 const char *vcinstalldir = getenv("VCINSTALLDIR");
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000370 if (vcinstalldir) {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000371 path = vcinstalldir;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000372 path = path.substr(0, path.find("\\VC"));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000373 return true;
374 }
375
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000376 std::string vsIDEInstallDir;
377 std::string vsExpressIDEInstallDir;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000378 // Then try the windows registry.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000379 bool hasVCDir =
380 getSystemRegistryString("SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
381 "InstallDir", vsIDEInstallDir, nullptr);
382 if (hasVCDir && !vsIDEInstallDir.empty()) {
383 path = vsIDEInstallDir.substr(0, vsIDEInstallDir.find("\\Common7\\IDE"));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000384 return true;
385 }
386
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000387 bool hasVCExpressDir =
388 getSystemRegistryString("SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
389 "InstallDir", vsExpressIDEInstallDir, nullptr);
390 if (hasVCExpressDir && !vsExpressIDEInstallDir.empty()) {
391 path = vsExpressIDEInstallDir.substr(
392 0, vsIDEInstallDir.find("\\Common7\\IDE"));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000393 return true;
394 }
395
396 // Try the environment.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000397 const char *vs120comntools = getenv("VS120COMNTOOLS");
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000398 const char *vs100comntools = getenv("VS100COMNTOOLS");
399 const char *vs90comntools = getenv("VS90COMNTOOLS");
400 const char *vs80comntools = getenv("VS80COMNTOOLS");
Alp Tokerfcce1832014-06-22 03:27:45 +0000401
402 const char *vscomntools = nullptr;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000403
Alp Tokera2074402014-06-22 03:27:52 +0000404 // Find any version we can
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000405 if (vs120comntools)
406 vscomntools = vs120comntools;
407 else if (vs100comntools)
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000408 vscomntools = vs100comntools;
409 else if (vs90comntools)
410 vscomntools = vs90comntools;
411 else if (vs80comntools)
412 vscomntools = vs80comntools;
413
414 if (vscomntools && *vscomntools) {
415 const char *p = strstr(vscomntools, "\\Common7\\Tools");
416 path = p ? std::string(vscomntools, p) : vscomntools;
417 return true;
418 }
419 return false;
420}
421
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000422void MSVCToolChain::AddSystemIncludeWithSubfolder(const ArgList &DriverArgs,
423 ArgStringList &CC1Args,
424 const std::string &folder,
425 const char *subfolder) const {
426 llvm::SmallString<128> path(folder);
427 llvm::sys::path::append(path, subfolder);
Yaron Keren92e1b622015-03-18 10:17:07 +0000428 addSystemInclude(DriverArgs, CC1Args, path);
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000429}
430
Saleem Abdulrasool819f3912014-10-22 02:37:29 +0000431void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
432 ArgStringList &CC1Args) const {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000433 if (DriverArgs.hasArg(options::OPT_nostdinc))
434 return;
435
436 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Rafael Espindola76565f02013-06-26 03:39:10 +0000437 SmallString<128> P(getDriver().ResourceDir);
438 llvm::sys::path::append(P, "include");
Yaron Keren92e1b622015-03-18 10:17:07 +0000439 addSystemInclude(DriverArgs, CC1Args, P);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000440 }
441
442 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
443 return;
444
Joao Matos792d7af2012-09-04 17:29:52 +0000445 // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
446 if (const char *cl_include_dir = getenv("INCLUDE")) {
447 SmallVector<StringRef, 8> Dirs;
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000448 StringRef(cl_include_dir)
449 .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
450 for (StringRef Dir : Dirs)
451 addSystemInclude(DriverArgs, CC1Args, Dir);
452 if (!Dirs.empty())
453 return;
Joao Matos792d7af2012-09-04 17:29:52 +0000454 }
455
456 std::string VSDir;
Joao Matos792d7af2012-09-04 17:29:52 +0000457
458 // When built with access to the proper Windows APIs, try to actually find
459 // the correct include paths first.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000460 if (getVisualStudioInstallDir(VSDir)) {
461 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, VSDir, "VC\\include");
462
463 std::string WindowsSDKDir;
464 int major, minor;
465 if (getWindowsSDKDir(WindowsSDKDir, major, minor)) {
466 if (major >= 8) {
467 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
468 "include\\shared");
469 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
470 "include\\um");
471 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
472 "include\\winrt");
473 } else {
474 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
475 "include");
476 }
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000477 } else {
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000478 addSystemInclude(DriverArgs, CC1Args, VSDir);
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000479 }
Joao Matos792d7af2012-09-04 17:29:52 +0000480 return;
481 }
Joao Matos792d7af2012-09-04 17:29:52 +0000482
483 // As a fallback, select default install paths.
Alp Tokerfcce1832014-06-22 03:27:45 +0000484 // FIXME: Don't guess drives and paths like this on Windows.
Joao Matos792d7af2012-09-04 17:29:52 +0000485 const StringRef Paths[] = {
486 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
487 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
488 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
489 "C:/Program Files/Microsoft Visual Studio 8/VC/include",
490 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
491 };
492 addSystemIncludes(DriverArgs, CC1Args, Paths);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000493}
494
Saleem Abdulrasool819f3912014-10-22 02:37:29 +0000495void MSVCToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
496 ArgStringList &CC1Args) const {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000497 // FIXME: There should probably be logic here to find libc++ on Windows.
498}
David Majnemere11d3732015-06-08 00:22:46 +0000499
500std::string
501MSVCToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
502 types::ID InputType) const {
503 std::string TripleStr =
504 ToolChain::ComputeEffectiveClangTriple(Args, InputType);
505 llvm::Triple Triple(TripleStr);
506 VersionTuple MSVT =
507 tools::visualstudio::getMSVCVersion(/*D=*/nullptr, Triple, Args,
508 /*IsWindowsMSVC=*/true);
509 if (MSVT.empty())
510 return TripleStr;
511
512 MSVT = VersionTuple(MSVT.getMajor(), MSVT.getMinor().getValueOr(0),
513 MSVT.getSubminor().getValueOr(0));
514
David Majnemer75fdd6b2015-06-09 06:30:01 +0000515 if (Triple.getEnvironment() == llvm::Triple::MSVC) {
516 StringRef ObjFmt = Triple.getEnvironmentName().split('-').second;
517 if (ObjFmt.empty())
518 Triple.setEnvironmentName((Twine("msvc") + MSVT.getAsString()).str());
519 else
520 Triple.setEnvironmentName(
521 (Twine("msvc") + MSVT.getAsString() + Twine('-') + ObjFmt).str());
522 }
David Majnemere11d3732015-06-08 00:22:46 +0000523 return Triple.getTriple();
524}
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000525
526SanitizerMask MSVCToolChain::getSupportedSanitizers() const {
527 SanitizerMask Res = ToolChain::getSupportedSanitizers();
528 Res |= SanitizerKind::Address;
529 // CFI checks are not implemented for MSVC ABI for now.
530 Res &= ~SanitizerKind::CFI;
531 Res &= ~SanitizerKind::CFICastStrict;
532 return Res;
533}