blob: 4167ac3a388f08601276e746bd37724328ac80cb [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"
Adrian McCarthye4b26fc2016-05-13 23:20:11 +000022#include "llvm/Support/ConvertUTF.h"
Chandler Carruth1fc603e2011-12-17 23:10:01 +000023#include "llvm/Support/ErrorHandling.h"
Zachary Turner0eaf8fc2014-10-22 20:40:28 +000024#include "llvm/Support/FileSystem.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000025#include "llvm/Support/Path.h"
Zachary Turner0eaf8fc2014-10-22 20:40:28 +000026#include "llvm/Support/Process.h"
Reid Kleckner6b7156b2015-01-23 19:16:25 +000027#include <cstdio>
28
Chandler Carruth1fc603e2011-12-17 23:10:01 +000029// Include the necessary headers to interface with the Windows registry and
30// environment.
Alp Tokerf1ffc842014-06-22 04:31:15 +000031#if defined(LLVM_ON_WIN32)
Alp Tokerfcce1832014-06-22 03:27:45 +000032#define USE_WIN32
33#endif
34
35#ifdef USE_WIN32
Chandler Carruth1fc603e2011-12-17 23:10:01 +000036 #define WIN32_LEAN_AND_MEAN
37 #define NOGDI
Yaron Keren7fc6f1e2014-12-04 21:46:50 +000038 #ifndef NOMINMAX
39 #define NOMINMAX
40 #endif
Logan Chien733e3c62014-06-24 16:18:10 +000041 #include <windows.h>
Chandler Carruth1fc603e2011-12-17 23:10:01 +000042#endif
43
44using namespace clang::driver;
45using namespace clang::driver::toolchains;
46using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000047using namespace llvm::opt;
Chandler Carruth1fc603e2011-12-17 23:10:01 +000048
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000049MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple& Triple,
50 const ArgList &Args)
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000051 : ToolChain(D, Triple, Args) {
Zachary Turner719f58c2014-12-01 23:06:47 +000052 getProgramPaths().push_back(getDriver().getInstalledDir());
53 if (getDriver().getInstalledDir() != getDriver().Dir)
54 getProgramPaths().push_back(getDriver().Dir);
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000055}
56
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000057Tool *MSVCToolChain::buildLinker() const {
Douglas Katzman95354292015-06-23 20:42:09 +000058 return new tools::visualstudio::Linker(*this);
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000059}
60
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000061Tool *MSVCToolChain::buildAssembler() const {
Saleem Abdulrasool377066a2014-03-27 22:50:18 +000062 if (getTriple().isOSBinFormatMachO())
Douglas Katzman95354292015-06-23 20:42:09 +000063 return new tools::darwin::Assembler(*this);
Alp Tokerc8d4f0f2013-11-22 08:27:46 +000064 getDriver().Diag(clang::diag::err_no_external_assembler);
Craig Topper92fc2df2014-05-17 16:56:41 +000065 return nullptr;
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000066}
67
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000068bool MSVCToolChain::IsIntegratedAssemblerDefault() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000069 return true;
70}
71
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000072bool MSVCToolChain::IsUnwindTablesDefault() const {
Reid Kleckner6b3a9402014-09-04 18:13:12 +000073 // Emit unwind tables by default on Win64. All non-x86_32 Windows platforms
74 // such as ARM and PPC actually require unwind tables, but LLVM doesn't know
75 // how to generate them yet.
Akira Hatanakae4218132016-05-05 01:41:07 +000076
77 // Don't emit unwind tables by default for MachO targets.
78 if (getTriple().isOSBinFormatMachO())
79 return false;
80
Reid Kleckner6b3a9402014-09-04 18:13:12 +000081 return getArch() == llvm::Triple::x86_64;
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000082}
83
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000084bool MSVCToolChain::isPICDefault() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000085 return getArch() == llvm::Triple::x86_64;
86}
87
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000088bool MSVCToolChain::isPIEDefault() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000089 return false;
90}
91
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000092bool MSVCToolChain::isPICDefaultForced() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000093 return getArch() == llvm::Triple::x86_64;
94}
95
Zachary Turner0eaf8fc2014-10-22 20:40:28 +000096#ifdef USE_WIN32
97static bool readFullStringValue(HKEY hkey, const char *valueName,
98 std::string &value) {
Aaron Ballmanb06a3592016-06-23 14:33:53 +000099 std::wstring WideValueName;
100 if (!llvm::ConvertUTF8toWide(valueName, WideValueName))
101 return false;
102
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000103 DWORD result = 0;
104 DWORD valueSize = 0;
105 DWORD type = 0;
106 // First just query for the required size.
Aaron Ballmanb06a3592016-06-23 14:33:53 +0000107 result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, &type, NULL,
108 &valueSize);
109 if (result != ERROR_SUCCESS || type != REG_SZ || !valueSize)
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000110 return false;
111 std::vector<BYTE> buffer(valueSize);
Aaron Ballmanb06a3592016-06-23 14:33:53 +0000112 result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, NULL, &buffer[0],
113 &valueSize);
114 if (result == ERROR_SUCCESS) {
115 std::wstring WideValue(reinterpret_cast<const wchar_t *>(buffer.data()),
116 valueSize / sizeof(wchar_t));
Zachary Turnere78a3472016-07-28 17:13:32 +0000117 if (valueSize && WideValue.back() == L'\0') {
Etienne Bergeron982a3bc2016-08-30 18:38:25 +0000118 WideValue.pop_back();
Zachary Turnere78a3472016-07-28 17:13:32 +0000119 }
Aaron Ballmanb06a3592016-06-23 14:33:53 +0000120 // The destination buffer must be empty as an invariant of the conversion
121 // function; but this function is sometimes called in a loop that passes in
122 // the same buffer, however. Simply clear it out so we can overwrite it.
123 value.clear();
124 return llvm::convertWideToUTF8(WideValue, value);
125 }
126 return false;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000127}
128#endif
129
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000130/// \brief Read registry string.
131/// This also supports a means to look for high-versioned keys by use
132/// of a $VERSION placeholder in the key path.
133/// $VERSION in the key path is a placeholder for the version number,
134/// causing the highest value path to be searched for and used.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000135/// I.e. "SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
136/// There can be additional characters in the component. Only the numeric
137/// characters are compared. This function only searches HKLM.
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000138static bool getSystemRegistryString(const char *keyPath, const char *valueName,
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000139 std::string &value, std::string *phValue) {
Alp Tokerfcce1832014-06-22 03:27:45 +0000140#ifndef USE_WIN32
141 return false;
142#else
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000143 HKEY hRootKey = HKEY_LOCAL_MACHINE;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000144 HKEY hKey = NULL;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000145 long lResult;
146 bool returnValue = false;
147
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000148 const char *placeHolder = strstr(keyPath, "$VERSION");
149 std::string bestName;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000150 // If we have a $VERSION placeholder, do the highest-version search.
151 if (placeHolder) {
152 const char *keyEnd = placeHolder - 1;
153 const char *nextKey = placeHolder;
154 // Find end of previous key.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000155 while ((keyEnd > keyPath) && (*keyEnd != '\\'))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000156 keyEnd--;
157 // Find end of key containing $VERSION.
158 while (*nextKey && (*nextKey != '\\'))
159 nextKey++;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000160 size_t partialKeyLength = keyEnd - keyPath;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000161 char partialKey[256];
Daniel Marjamakie1146692016-01-27 07:33:50 +0000162 if (partialKeyLength >= sizeof(partialKey))
163 partialKeyLength = sizeof(partialKey) - 1;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000164 strncpy(partialKey, keyPath, partialKeyLength);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000165 partialKey[partialKeyLength] = '\0';
166 HKEY hTopKey = NULL;
Aaron Ballmanb06a3592016-06-23 14:33:53 +0000167 lResult = RegOpenKeyExA(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
168 &hTopKey);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000169 if (lResult == ERROR_SUCCESS) {
170 char keyName[256];
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000171 double bestValue = 0.0;
172 DWORD index, size = sizeof(keyName) - 1;
Aaron Ballmanb06a3592016-06-23 14:33:53 +0000173 for (index = 0; RegEnumKeyExA(hTopKey, index, keyName, &size, NULL, NULL,
174 NULL, NULL) == ERROR_SUCCESS;
175 index++) {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000176 const char *sp = keyName;
Jordan Rosea7d03842013-02-08 22:30:41 +0000177 while (*sp && !isDigit(*sp))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000178 sp++;
179 if (!*sp)
180 continue;
181 const char *ep = sp + 1;
Jordan Rosea7d03842013-02-08 22:30:41 +0000182 while (*ep && (isDigit(*ep) || (*ep == '.')))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000183 ep++;
184 char numBuf[32];
185 strncpy(numBuf, sp, sizeof(numBuf) - 1);
186 numBuf[sizeof(numBuf) - 1] = '\0';
Hans Wennborgd2192312013-10-10 18:03:08 +0000187 double dvalue = strtod(numBuf, NULL);
188 if (dvalue > bestValue) {
189 // Test that InstallDir is indeed there before keeping this index.
190 // Open the chosen key path remainder.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000191 bestName = keyName;
Hans Wennborgd2192312013-10-10 18:03:08 +0000192 // Append rest of key.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000193 bestName.append(nextKey);
Aaron Ballmanb06a3592016-06-23 14:33:53 +0000194 lResult = RegOpenKeyExA(hTopKey, bestName.c_str(), 0,
195 KEY_READ | KEY_WOW64_32KEY, &hKey);
Hans Wennborgd2192312013-10-10 18:03:08 +0000196 if (lResult == ERROR_SUCCESS) {
Zachary Turnere78a3472016-07-28 17:13:32 +0000197 if (readFullStringValue(hKey, valueName, value)) {
Hans Wennborgd2192312013-10-10 18:03:08 +0000198 bestValue = dvalue;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000199 if (phValue)
200 *phValue = bestName;
Hans Wennborgd2192312013-10-10 18:03:08 +0000201 returnValue = true;
202 }
203 RegCloseKey(hKey);
204 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000205 }
206 size = sizeof(keyName) - 1;
207 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000208 RegCloseKey(hTopKey);
209 }
210 } else {
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000211 lResult =
Aaron Ballmanb06a3592016-06-23 14:33:53 +0000212 RegOpenKeyExA(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000213 if (lResult == ERROR_SUCCESS) {
Zachary Turnere78a3472016-07-28 17:13:32 +0000214 if (readFullStringValue(hKey, valueName, value))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000215 returnValue = true;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000216 if (phValue)
217 phValue->clear();
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000218 RegCloseKey(hKey);
219 }
220 }
221 return returnValue;
Alp Tokerfcce1832014-06-22 03:27:45 +0000222#endif // USE_WIN32
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000223}
224
Reid Kleckner7531f7d2015-09-11 00:09:39 +0000225// Convert LLVM's ArchType
226// to the corresponding name of Windows SDK libraries subfolder
227static StringRef getWindowsSDKArch(llvm::Triple::ArchType Arch) {
228 switch (Arch) {
229 case llvm::Triple::x86:
230 return "x86";
231 case llvm::Triple::x86_64:
232 return "x64";
233 case llvm::Triple::arm:
234 return "arm";
235 default:
236 return "";
237 }
238}
239
Igor Kudrinf2e75242015-09-24 05:16:36 +0000240// Find the most recent version of Universal CRT or Windows 10 SDK.
241// vcvarsqueryregistry.bat from Visual Studio 2015 sorts entries in the include
242// directory by name and uses the last one of the list.
243// So we compare entry names lexicographically to find the greatest one.
244static bool getWindows10SDKVersion(const std::string &SDKPath,
245 std::string &SDKVersion) {
246 SDKVersion.clear();
247
248 std::error_code EC;
249 llvm::SmallString<128> IncludePath(SDKPath);
250 llvm::sys::path::append(IncludePath, "Include");
251 for (llvm::sys::fs::directory_iterator DirIt(IncludePath, EC), DirEnd;
252 DirIt != DirEnd && !EC; DirIt.increment(EC)) {
253 if (!llvm::sys::fs::is_directory(DirIt->path()))
254 continue;
255 StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
256 // If WDK is installed, there could be subfolders like "wdf" in the
257 // "Include" directory.
258 // Allow only directories which names start with "10.".
259 if (!CandidateName.startswith("10."))
260 continue;
261 if (CandidateName > SDKVersion)
262 SDKVersion = CandidateName;
263 }
264
265 return !SDKVersion.empty();
266}
267
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000268/// \brief Get Windows SDK installation directory.
Igor Kudrinf2e75242015-09-24 05:16:36 +0000269bool MSVCToolChain::getWindowsSDKDir(std::string &Path, int &Major,
270 std::string &WindowsSDKIncludeVersion,
271 std::string &WindowsSDKLibVersion) const {
272 std::string RegistrySDKVersion;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000273 // Try the Windows registry.
Igor Kudrinf2e75242015-09-24 05:16:36 +0000274 if (!getSystemRegistryString(
275 "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
276 "InstallationFolder", Path, &RegistrySDKVersion))
277 return false;
278 if (Path.empty() || RegistrySDKVersion.empty())
279 return false;
280
281 WindowsSDKIncludeVersion.clear();
282 WindowsSDKLibVersion.clear();
283 Major = 0;
284 std::sscanf(RegistrySDKVersion.c_str(), "v%d.", &Major);
285 if (Major <= 7)
286 return true;
287 if (Major == 8) {
288 // Windows SDK 8.x installs libraries in a folder whose names depend on the
289 // version of the OS you're targeting. By default choose the newest, which
290 // usually corresponds to the version of the OS you've installed the SDK on.
291 const char *Tests[] = {"winv6.3", "win8", "win7"};
292 for (const char *Test : Tests) {
293 llvm::SmallString<128> TestPath(Path);
294 llvm::sys::path::append(TestPath, "Lib", Test);
295 if (llvm::sys::fs::exists(TestPath.c_str())) {
296 WindowsSDKLibVersion = Test;
297 break;
298 }
299 }
300 return !WindowsSDKLibVersion.empty();
301 }
302 if (Major == 10) {
303 if (!getWindows10SDKVersion(Path, WindowsSDKIncludeVersion))
304 return false;
305 WindowsSDKLibVersion = WindowsSDKIncludeVersion;
306 return true;
307 }
308 // Unsupported SDK version
309 return false;
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000310}
311
Zachary Turner10d75b22014-10-22 20:40:43 +0000312// Gets the library path required to link against the Windows SDK.
313bool MSVCToolChain::getWindowsSDKLibraryPath(std::string &path) const {
314 std::string sdkPath;
315 int sdkMajor = 0;
Igor Kudrinf2e75242015-09-24 05:16:36 +0000316 std::string windowsSDKIncludeVersion;
317 std::string windowsSDKLibVersion;
Zachary Turner10d75b22014-10-22 20:40:43 +0000318
319 path.clear();
Igor Kudrinf2e75242015-09-24 05:16:36 +0000320 if (!getWindowsSDKDir(sdkPath, sdkMajor, windowsSDKIncludeVersion,
321 windowsSDKLibVersion))
Zachary Turner10d75b22014-10-22 20:40:43 +0000322 return false;
323
324 llvm::SmallString<128> libPath(sdkPath);
325 llvm::sys::path::append(libPath, "Lib");
326 if (sdkMajor <= 7) {
327 switch (getArch()) {
328 // In Windows SDK 7.x, x86 libraries are directly in the Lib folder.
329 case llvm::Triple::x86:
330 break;
331 case llvm::Triple::x86_64:
332 llvm::sys::path::append(libPath, "x64");
333 break;
334 case llvm::Triple::arm:
335 // It is not necessary to link against Windows SDK 7.x when targeting ARM.
336 return false;
337 default:
338 return false;
339 }
340 } else {
Reid Kleckner7531f7d2015-09-11 00:09:39 +0000341 const StringRef archName = getWindowsSDKArch(getArch());
342 if (archName.empty())
Zachary Turner10d75b22014-10-22 20:40:43 +0000343 return false;
Igor Kudrinf2e75242015-09-24 05:16:36 +0000344 llvm::sys::path::append(libPath, windowsSDKLibVersion, "um", archName);
Zachary Turner10d75b22014-10-22 20:40:43 +0000345 }
346
347 path = libPath.str();
348 return true;
349}
350
Reid Kleckner7531f7d2015-09-11 00:09:39 +0000351// Check if the Include path of a specified version of Visual Studio contains
352// specific header files. If not, they are probably shipped with Universal CRT.
353bool clang::driver::toolchains::MSVCToolChain::useUniversalCRT(
354 std::string &VisualStudioDir) const {
355 llvm::SmallString<128> TestPath(VisualStudioDir);
356 llvm::sys::path::append(TestPath, "VC\\include\\stdlib.h");
357
358 return !llvm::sys::fs::exists(TestPath);
359}
360
361bool MSVCToolChain::getUniversalCRTSdkDir(std::string &Path,
362 std::string &UCRTVersion) const {
363 // vcvarsqueryregistry.bat for Visual Studio 2015 queries the registry
364 // for the specific key "KitsRoot10". So do we.
365 if (!getSystemRegistryString(
366 "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots", "KitsRoot10",
367 Path, nullptr))
368 return false;
369
Igor Kudrinf2e75242015-09-24 05:16:36 +0000370 return getWindows10SDKVersion(Path, UCRTVersion);
Reid Kleckner7531f7d2015-09-11 00:09:39 +0000371}
372
373bool MSVCToolChain::getUniversalCRTLibraryPath(std::string &Path) const {
374 std::string UniversalCRTSdkPath;
375 std::string UCRTVersion;
376
377 Path.clear();
378 if (!getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion))
379 return false;
380
381 StringRef ArchName = getWindowsSDKArch(getArch());
382 if (ArchName.empty())
383 return false;
384
385 llvm::SmallString<128> LibPath(UniversalCRTSdkPath);
386 llvm::sys::path::append(LibPath, "Lib", UCRTVersion, "ucrt", ArchName);
387
388 Path = LibPath.str();
389 return true;
390}
391
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000392// Get the location to use for Visual Studio binaries. The location priority
393// is: %VCINSTALLDIR% > %PATH% > newest copy of Visual Studio installed on
394// system (as reported by the registry).
395bool MSVCToolChain::getVisualStudioBinariesFolder(const char *clangProgramPath,
396 std::string &path) const {
397 path.clear();
398
399 SmallString<128> BinDir;
400
401 // First check the environment variables that vsvars32.bat sets.
402 llvm::Optional<std::string> VcInstallDir =
403 llvm::sys::Process::GetEnv("VCINSTALLDIR");
404 if (VcInstallDir.hasValue()) {
405 BinDir = VcInstallDir.getValue();
406 llvm::sys::path::append(BinDir, "bin");
407 } else {
408 // Next walk the PATH, trying to find a cl.exe in the path. If we find one,
409 // use that. However, make sure it's not clang's cl.exe.
410 llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("PATH");
411 if (OptPath.hasValue()) {
412 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
413 SmallVector<StringRef, 8> PathSegments;
414 llvm::SplitString(OptPath.getValue(), PathSegments, EnvPathSeparatorStr);
415
416 for (StringRef PathSegment : PathSegments) {
417 if (PathSegment.empty())
418 continue;
419
420 SmallString<128> FilePath(PathSegment);
421 llvm::sys::path::append(FilePath, "cl.exe");
Adrian McCarthy8e0879a92016-05-20 15:46:23 +0000422 // Checking if cl.exe exists is a small optimization over calling
423 // can_execute, which really only checks for existence but will also do
424 // extra checks for cl.exe.exe. These add up when walking a long path.
425 if (llvm::sys::fs::exists(FilePath.c_str()) &&
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000426 !llvm::sys::fs::equivalent(FilePath.c_str(), clangProgramPath)) {
427 // If we found it on the PATH, use it exactly as is with no
428 // modifications.
429 path = PathSegment;
430 return true;
431 }
432 }
433 }
434
435 std::string installDir;
436 // With no VCINSTALLDIR and nothing on the PATH, if we can't find it in the
437 // registry then we have no choice but to fail.
438 if (!getVisualStudioInstallDir(installDir))
439 return false;
440
441 // Regardless of what binary we're ultimately trying to find, we make sure
442 // that this is a Visual Studio directory by checking for cl.exe. We use
443 // cl.exe instead of other binaries like link.exe because programs such as
444 // GnuWin32 also have a utility called link.exe, so cl.exe is the least
445 // ambiguous.
446 BinDir = installDir;
447 llvm::sys::path::append(BinDir, "VC", "bin");
448 SmallString<128> ClPath(BinDir);
449 llvm::sys::path::append(ClPath, "cl.exe");
450
451 if (!llvm::sys::fs::can_execute(ClPath.c_str()))
452 return false;
Hans Wennborge6b994e2014-10-20 23:26:03 +0000453 }
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000454
455 if (BinDir.empty())
456 return false;
457
458 switch (getArch()) {
459 case llvm::Triple::x86:
460 break;
461 case llvm::Triple::x86_64:
462 llvm::sys::path::append(BinDir, "amd64");
463 break;
464 case llvm::Triple::arm:
465 llvm::sys::path::append(BinDir, "arm");
466 break;
467 default:
468 // Whatever this is, Visual Studio doesn't have a toolchain for it.
469 return false;
470 }
471 path = BinDir.str();
472 return true;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000473}
474
Adrian McCarthye4b26fc2016-05-13 23:20:11 +0000475VersionTuple MSVCToolChain::getMSVCVersionFromExe() const {
476 VersionTuple Version;
477#ifdef USE_WIN32
478 std::string BinPath;
479 if (!getVisualStudioBinariesFolder("", BinPath))
480 return Version;
NAKAMURA Takumi4b8e3e52016-05-14 08:09:12 +0000481 SmallString<128> ClExe(BinPath);
Adrian McCarthye4b26fc2016-05-13 23:20:11 +0000482 llvm::sys::path::append(ClExe, "cl.exe");
483
484 std::wstring ClExeWide;
485 if (!llvm::ConvertUTF8toWide(ClExe.c_str(), ClExeWide))
486 return Version;
487
488 const DWORD VersionSize = ::GetFileVersionInfoSizeW(ClExeWide.c_str(),
489 nullptr);
490 if (VersionSize == 0)
491 return Version;
492
493 SmallVector<uint8_t, 4 * 1024> VersionBlock(VersionSize);
494 if (!::GetFileVersionInfoW(ClExeWide.c_str(), 0, VersionSize,
495 VersionBlock.data()))
496 return Version;
497
498 VS_FIXEDFILEINFO *FileInfo = nullptr;
499 UINT FileInfoSize = 0;
500 if (!::VerQueryValueW(VersionBlock.data(), L"\\",
501 reinterpret_cast<LPVOID *>(&FileInfo), &FileInfoSize) ||
502 FileInfoSize < sizeof(*FileInfo))
503 return Version;
504
505 const unsigned Major = (FileInfo->dwFileVersionMS >> 16) & 0xFFFF;
506 const unsigned Minor = (FileInfo->dwFileVersionMS ) & 0xFFFF;
507 const unsigned Micro = (FileInfo->dwFileVersionLS >> 16) & 0xFFFF;
508
509 Version = VersionTuple(Major, Minor, Micro);
510#endif
511 return Version;
512}
513
Alp Tokerfcce1832014-06-22 03:27:45 +0000514// Get Visual Studio installation directory.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000515bool MSVCToolChain::getVisualStudioInstallDir(std::string &path) const {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000516 // First check the environment variables that vsvars32.bat sets.
David Majnemer85c25b42016-07-24 17:44:03 +0000517 if (llvm::Optional<std::string> VcInstallDir =
518 llvm::sys::Process::GetEnv("VCINSTALLDIR")) {
519 path = std::move(*VcInstallDir);
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000520 path = path.substr(0, path.find("\\VC"));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000521 return true;
522 }
523
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000524 std::string vsIDEInstallDir;
525 std::string vsExpressIDEInstallDir;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000526 // Then try the windows registry.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000527 bool hasVCDir =
528 getSystemRegistryString("SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
529 "InstallDir", vsIDEInstallDir, nullptr);
530 if (hasVCDir && !vsIDEInstallDir.empty()) {
531 path = vsIDEInstallDir.substr(0, vsIDEInstallDir.find("\\Common7\\IDE"));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000532 return true;
533 }
534
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000535 bool hasVCExpressDir =
536 getSystemRegistryString("SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
537 "InstallDir", vsExpressIDEInstallDir, nullptr);
538 if (hasVCExpressDir && !vsExpressIDEInstallDir.empty()) {
539 path = vsExpressIDEInstallDir.substr(
540 0, vsIDEInstallDir.find("\\Common7\\IDE"));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000541 return true;
542 }
543
544 // Try the environment.
David Majnemer85c25b42016-07-24 17:44:03 +0000545 std::string vcomntools;
546 if (llvm::Optional<std::string> vs120comntools =
547 llvm::sys::Process::GetEnv("VS120COMNTOOLS"))
548 vcomntools = std::move(*vs120comntools);
549 else if (llvm::Optional<std::string> vs100comntools =
550 llvm::sys::Process::GetEnv("VS100COMNTOOLS"))
551 vcomntools = std::move(*vs100comntools);
552 else if (llvm::Optional<std::string> vs90comntools =
553 llvm::sys::Process::GetEnv("VS90COMNTOOLS"))
554 vcomntools = std::move(*vs90comntools);
555 else if (llvm::Optional<std::string> vs80comntools =
556 llvm::sys::Process::GetEnv("VS80COMNTOOLS"))
557 vcomntools = std::move(*vs80comntools);
Alp Tokerfcce1832014-06-22 03:27:45 +0000558
David Majnemer85c25b42016-07-24 17:44:03 +0000559 // Find any version we can.
560 if (!vcomntools.empty()) {
561 size_t p = vcomntools.find("\\Common7\\Tools");
562 if (p != std::string::npos)
563 vcomntools.resize(p);
564 path = std::move(vcomntools);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000565 return true;
566 }
567 return false;
568}
569
Igor Kudrinf2e75242015-09-24 05:16:36 +0000570void MSVCToolChain::AddSystemIncludeWithSubfolder(
571 const ArgList &DriverArgs, ArgStringList &CC1Args,
572 const std::string &folder, const Twine &subfolder1, const Twine &subfolder2,
573 const Twine &subfolder3) const {
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000574 llvm::SmallString<128> path(folder);
Igor Kudrinf2e75242015-09-24 05:16:36 +0000575 llvm::sys::path::append(path, subfolder1, subfolder2, subfolder3);
Yaron Keren92e1b622015-03-18 10:17:07 +0000576 addSystemInclude(DriverArgs, CC1Args, path);
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000577}
578
Saleem Abdulrasool819f3912014-10-22 02:37:29 +0000579void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
580 ArgStringList &CC1Args) const {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000581 if (DriverArgs.hasArg(options::OPT_nostdinc))
582 return;
583
584 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Igor Kudrinf2e75242015-09-24 05:16:36 +0000585 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, getDriver().ResourceDir,
586 "include");
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000587 }
588
Nico Weberfd3e1ad2016-04-12 19:04:37 +0000589 // Add %INCLUDE%-like directories from the -imsvc flag.
590 for (const auto &Path : DriverArgs.getAllArgValues(options::OPT__SLASH_imsvc))
591 addSystemInclude(DriverArgs, CC1Args, Path);
592
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000593 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
594 return;
595
Joao Matos792d7af2012-09-04 17:29:52 +0000596 // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
David Majnemer85c25b42016-07-24 17:44:03 +0000597 if (llvm::Optional<std::string> cl_include_dir =
598 llvm::sys::Process::GetEnv("INCLUDE")) {
Joao Matos792d7af2012-09-04 17:29:52 +0000599 SmallVector<StringRef, 8> Dirs;
David Majnemer85c25b42016-07-24 17:44:03 +0000600 StringRef(*cl_include_dir)
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000601 .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
602 for (StringRef Dir : Dirs)
603 addSystemInclude(DriverArgs, CC1Args, Dir);
604 if (!Dirs.empty())
605 return;
Joao Matos792d7af2012-09-04 17:29:52 +0000606 }
607
608 std::string VSDir;
Joao Matos792d7af2012-09-04 17:29:52 +0000609
610 // When built with access to the proper Windows APIs, try to actually find
611 // the correct include paths first.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000612 if (getVisualStudioInstallDir(VSDir)) {
613 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, VSDir, "VC\\include");
614
Reid Kleckner7531f7d2015-09-11 00:09:39 +0000615 if (useUniversalCRT(VSDir)) {
616 std::string UniversalCRTSdkPath;
617 std::string UCRTVersion;
618 if (getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion)) {
Igor Kudrinf2e75242015-09-24 05:16:36 +0000619 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, UniversalCRTSdkPath,
620 "Include", UCRTVersion, "ucrt");
Reid Kleckner7531f7d2015-09-11 00:09:39 +0000621 }
622 }
623
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000624 std::string WindowsSDKDir;
Igor Kudrinf2e75242015-09-24 05:16:36 +0000625 int major;
626 std::string windowsSDKIncludeVersion;
627 std::string windowsSDKLibVersion;
628 if (getWindowsSDKDir(WindowsSDKDir, major, windowsSDKIncludeVersion,
629 windowsSDKLibVersion)) {
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000630 if (major >= 8) {
Igor Kudrinf2e75242015-09-24 05:16:36 +0000631 // Note: windowsSDKIncludeVersion is empty for SDKs prior to v10.
632 // Anyway, llvm::sys::path::append is able to manage it.
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000633 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
Igor Kudrinf2e75242015-09-24 05:16:36 +0000634 "include", windowsSDKIncludeVersion,
635 "shared");
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000636 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
Igor Kudrinf2e75242015-09-24 05:16:36 +0000637 "include", windowsSDKIncludeVersion,
638 "um");
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000639 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
Igor Kudrinf2e75242015-09-24 05:16:36 +0000640 "include", windowsSDKIncludeVersion,
641 "winrt");
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000642 } else {
643 AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
644 "include");
645 }
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000646 } else {
Zachary Turner0eaf8fc2014-10-22 20:40:28 +0000647 addSystemInclude(DriverArgs, CC1Args, VSDir);
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000648 }
Joao Matos792d7af2012-09-04 17:29:52 +0000649 return;
650 }
Joao Matos792d7af2012-09-04 17:29:52 +0000651
David Majnemerd5f7d192016-07-25 04:47:44 +0000652#if defined(LLVM_ON_WIN32)
Joao Matos792d7af2012-09-04 17:29:52 +0000653 // As a fallback, select default install paths.
Alp Tokerfcce1832014-06-22 03:27:45 +0000654 // FIXME: Don't guess drives and paths like this on Windows.
Joao Matos792d7af2012-09-04 17:29:52 +0000655 const StringRef Paths[] = {
656 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
657 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
658 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
659 "C:/Program Files/Microsoft Visual Studio 8/VC/include",
660 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
661 };
662 addSystemIncludes(DriverArgs, CC1Args, Paths);
David Majnemerd5f7d192016-07-25 04:47:44 +0000663#endif
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000664}
665
Saleem Abdulrasool819f3912014-10-22 02:37:29 +0000666void MSVCToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
667 ArgStringList &CC1Args) const {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000668 // FIXME: There should probably be logic here to find libc++ on Windows.
669}
David Majnemere11d3732015-06-08 00:22:46 +0000670
671std::string
672MSVCToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
673 types::ID InputType) const {
674 std::string TripleStr =
675 ToolChain::ComputeEffectiveClangTriple(Args, InputType);
676 llvm::Triple Triple(TripleStr);
677 VersionTuple MSVT =
Adrian McCarthye4b26fc2016-05-13 23:20:11 +0000678 tools::visualstudio::getMSVCVersion(/*D=*/nullptr, *this, Triple, Args,
David Majnemere11d3732015-06-08 00:22:46 +0000679 /*IsWindowsMSVC=*/true);
680 if (MSVT.empty())
681 return TripleStr;
682
683 MSVT = VersionTuple(MSVT.getMajor(), MSVT.getMinor().getValueOr(0),
684 MSVT.getSubminor().getValueOr(0));
685
David Majnemer75fdd6b2015-06-09 06:30:01 +0000686 if (Triple.getEnvironment() == llvm::Triple::MSVC) {
687 StringRef ObjFmt = Triple.getEnvironmentName().split('-').second;
688 if (ObjFmt.empty())
689 Triple.setEnvironmentName((Twine("msvc") + MSVT.getAsString()).str());
690 else
691 Triple.setEnvironmentName(
692 (Twine("msvc") + MSVT.getAsString() + Twine('-') + ObjFmt).str());
693 }
David Majnemere11d3732015-06-08 00:22:46 +0000694 return Triple.getTriple();
695}
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000696
697SanitizerMask MSVCToolChain::getSupportedSanitizers() const {
698 SanitizerMask Res = ToolChain::getSupportedSanitizers();
699 Res |= SanitizerKind::Address;
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000700 return Res;
701}
David Majnemer015ce0f2015-07-27 07:32:11 +0000702
Hans Wennborg21d73d22016-01-12 23:17:03 +0000703static void TranslateOptArg(Arg *A, llvm::opt::DerivedArgList &DAL,
704 bool SupportsForcingFramePointer,
705 const char *ExpandChar, const OptTable &Opts) {
706 assert(A->getOption().matches(options::OPT__SLASH_O));
707
708 StringRef OptStr = A->getValue();
709 for (size_t I = 0, E = OptStr.size(); I != E; ++I) {
710 const char &OptChar = *(OptStr.data() + I);
711 switch (OptChar) {
712 default:
713 break;
714 case '1':
715 case '2':
716 case 'x':
717 case 'd':
718 if (&OptChar == ExpandChar) {
719 if (OptChar == 'd') {
720 DAL.AddFlagArg(A, Opts.getOption(options::OPT_O0));
721 } else {
722 if (OptChar == '1') {
723 DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "s");
724 } else if (OptChar == '2' || OptChar == 'x') {
725 DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin));
726 DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "2");
727 }
David Majnemer259d71a2016-01-21 23:01:11 +0000728 if (SupportsForcingFramePointer &&
729 !DAL.hasArgNoClaim(options::OPT_fno_omit_frame_pointer))
Hans Wennborg21d73d22016-01-12 23:17:03 +0000730 DAL.AddFlagArg(A,
731 Opts.getOption(options::OPT_fomit_frame_pointer));
732 if (OptChar == '1' || OptChar == '2')
733 DAL.AddFlagArg(A,
734 Opts.getOption(options::OPT_ffunction_sections));
735 }
736 }
737 break;
738 case 'b':
Hans Wennborg3270bdb2016-05-24 21:23:29 +0000739 if (I + 1 != E && isdigit(OptStr[I + 1])) {
740 switch (OptStr[I + 1]) {
741 case '0':
742 DAL.AddFlagArg(A, Opts.getOption(options::OPT_fno_inline));
743 break;
744 case '1':
Hans Wennborg44d061a2016-06-22 16:56:16 +0000745 DAL.AddFlagArg(A, Opts.getOption(options::OPT_finline_hint_functions));
Hans Wennborg3270bdb2016-05-24 21:23:29 +0000746 break;
747 case '2':
748 DAL.AddFlagArg(A, Opts.getOption(options::OPT_finline_functions));
749 break;
750 }
Hans Wennborg21d73d22016-01-12 23:17:03 +0000751 ++I;
Hans Wennborg3270bdb2016-05-24 21:23:29 +0000752 }
Hans Wennborg21d73d22016-01-12 23:17:03 +0000753 break;
754 case 'g':
755 break;
756 case 'i':
757 if (I + 1 != E && OptStr[I + 1] == '-') {
758 ++I;
759 DAL.AddFlagArg(A, Opts.getOption(options::OPT_fno_builtin));
760 } else {
761 DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin));
762 }
763 break;
764 case 's':
765 DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "s");
766 break;
767 case 't':
768 DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "2");
769 break;
770 case 'y': {
771 bool OmitFramePointer = true;
772 if (I + 1 != E && OptStr[I + 1] == '-') {
773 OmitFramePointer = false;
774 ++I;
775 }
776 if (SupportsForcingFramePointer) {
777 if (OmitFramePointer)
778 DAL.AddFlagArg(A,
779 Opts.getOption(options::OPT_fomit_frame_pointer));
780 else
781 DAL.AddFlagArg(
782 A, Opts.getOption(options::OPT_fno_omit_frame_pointer));
Nico Weber9c3fca32016-03-23 15:37:41 +0000783 } else {
784 // Don't warn about /Oy- in 64-bit builds (where
785 // SupportsForcingFramePointer is false). The flag having no effect
786 // there is a compiler-internal optimization, and people shouldn't have
787 // to special-case their build files for 64-bit clang-cl.
788 A->claim();
Hans Wennborg21d73d22016-01-12 23:17:03 +0000789 }
790 break;
791 }
792 }
793 }
794}
795
796static void TranslateDArg(Arg *A, llvm::opt::DerivedArgList &DAL,
797 const OptTable &Opts) {
798 assert(A->getOption().matches(options::OPT_D));
799
800 StringRef Val = A->getValue();
801 size_t Hash = Val.find('#');
802 if (Hash == StringRef::npos || Hash > Val.find('=')) {
803 DAL.append(A);
804 return;
805 }
806
807 std::string NewVal = Val;
808 NewVal[Hash] = '=';
809 DAL.AddJoinedArg(A, Opts.getOption(options::OPT_D), NewVal);
810}
811
David Majnemer015ce0f2015-07-27 07:32:11 +0000812llvm::opt::DerivedArgList *
813MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
814 const char *BoundArch) const {
815 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
816 const OptTable &Opts = getDriver().getOpts();
817
David Majnemer7ab76f22015-08-25 00:46:45 +0000818 // /Oy and /Oy- only has an effect under X86-32.
819 bool SupportsForcingFramePointer = getArch() == llvm::Triple::x86;
820
David Majnemer015ce0f2015-07-27 07:32:11 +0000821 // The -O[12xd] flag actually expands to several flags. We must desugar the
822 // flags so that options embedded can be negated. For example, the '-O2' flag
823 // enables '-Oy'. Expanding '-O2' into its constituent flags allows us to
824 // correctly handle '-O2 -Oy-' where the trailing '-Oy-' disables a single
825 // aspect of '-O2'.
826 //
827 // Note that this expansion logic only applies to the *last* of '[12xd]'.
828
829 // First step is to search for the character we'd like to expand.
830 const char *ExpandChar = nullptr;
831 for (Arg *A : Args) {
832 if (!A->getOption().matches(options::OPT__SLASH_O))
833 continue;
834 StringRef OptStr = A->getValue();
835 for (size_t I = 0, E = OptStr.size(); I != E; ++I) {
Hans Wennborgdebfed92016-05-25 00:43:45 +0000836 char OptChar = OptStr[I];
837 char PrevChar = I > 0 ? OptStr[I - 1] : '0';
838 if (PrevChar == 'b') {
839 // OptChar does not expand; it's an argument to the previous char.
840 continue;
841 }
David Majnemer015ce0f2015-07-27 07:32:11 +0000842 if (OptChar == '1' || OptChar == '2' || OptChar == 'x' || OptChar == 'd')
843 ExpandChar = OptStr.data() + I;
844 }
845 }
846
David Majnemer015ce0f2015-07-27 07:32:11 +0000847 for (Arg *A : Args) {
Hans Wennborg21d73d22016-01-12 23:17:03 +0000848 if (A->getOption().matches(options::OPT__SLASH_O)) {
849 // The -O flag actually takes an amalgam of other options. For example,
850 // '/Ogyb2' is equivalent to '/Og' '/Oy' '/Ob2'.
851 TranslateOptArg(A, *DAL, SupportsForcingFramePointer, ExpandChar, Opts);
852 } else if (A->getOption().matches(options::OPT_D)) {
853 // Translate -Dfoo#bar into -Dfoo=bar.
854 TranslateDArg(A, *DAL, Opts);
855 } else {
David Majnemer015ce0f2015-07-27 07:32:11 +0000856 DAL->append(A);
David Majnemer015ce0f2015-07-27 07:32:11 +0000857 }
858 }
Hans Wennborg21d73d22016-01-12 23:17:03 +0000859
David Majnemer015ce0f2015-07-27 07:32:11 +0000860 return DAL;
861}